maxburke opened a new issue, #15886: URL: https://github.com/apache/datafusion/issues/15886
### Describe the bug Referenced discussion: https://the-asf.slack.com/archives/C01QUFS30TD/p1745875862723149 Given this table: ``` > create table d1 (ul_node_id string); ``` It appears that sorts are being removed from inner expressions. For example, here is a query with a sort expression: ``` > explain select * from d1 ORDER BY encode(ul_node_id, 'base64') ASC NULLS LAST; +---------------+-------------------------------+ | plan_type | plan | +---------------+-------------------------------+ | physical_plan | ┌───────────────────────────┐ | | | │ SortExec │ | | | │ -------------------- │ | | | │ encode(ul_node_id@0, │ | | | │ base64) ASC NULLS │ | | | │ LAST │ | | | └─────────────┬─────────────┘ | | | ┌─────────────┴─────────────┐ | | | │ DataSourceExec │ | | | │ -------------------- │ | | | │ bytes: 0 │ | | | │ format: memory │ | | | │ rows: 0 │ | | | └───────────────────────────┘ | | | | +---------------+-------------------------------+ 1 row(s) fetched. Elapsed 0.008 seconds.``` If I wrap it in a no-op CTE the `SortExec` stage disappears: ``` > explain WITH t0 AS (SELECT "ul_node_id", encode(ul_node_id, 'base64') FROM d1 ORDER BY encode(ul_node_id, 'base64') ASC NULLS LAST) SELECT * FROM t0; +---------------+-------------------------------+ | plan_type | plan | +---------------+-------------------------------+ | physical_plan | ┌───────────────────────────┐ | | | │ ProjectionExec │ | | | │ -------------------- │ | | | │ encode(d1.ul_node_id,Utf8(│ | | | │ "base64")): │ | | | │ encode(ul_node_id, base64)│ | | | │ │ | | | │ ul_node_id: │ | | | │ ul_node_id │ | | | └─────────────┬─────────────┘ | | | ┌─────────────┴─────────────┐ | | | │ DataSourceExec │ | | | │ -------------------- │ | | | │ bytes: 0 │ | | | │ format: memory │ | | | │ rows: 0 │ | | | └───────────────────────────┘ | | | | +---------------+-------------------------------+ 1 row(s) fetched.``` Manually hoisting out the ORDER BY from the CTE causes it to appear in the plan: ``` > explain WITH t0 AS (SELECT "ul_node_id", encode(ul_node_id, 'base64') FROM d1) SELECT * from t0 ORDER BY encode(ul_node_id, 'base64') ASC NULLS LAST; +---------------+-------------------------------+ | plan_type | plan | +---------------+-------------------------------+ | physical_plan | ┌───────────────────────────┐ | | | │ SortExec │ | | | │ -------------------- │ | | | │ encode(ul_node_id@0, │ | | | │ base64) ASC NULLS │ | | | │ LAST │ | | | └─────────────┬─────────────┘ | | | ┌─────────────┴─────────────┐ | | | │ ProjectionExec │ | | | │ -------------------- │ | | | │ encode(d1.ul_node_id,Utf8(│ | | | │ "base64")): │ | | | │ encode(ul_node_id, base64)│ | | | │ │ | | | │ ul_node_id: │ | | | │ ul_node_id │ | | | └─────────────┬─────────────┘ | | | ┌─────────────┴─────────────┐ | | | │ DataSourceExec │ | | | │ -------------------- │ | | | │ bytes: 0 │ | | | │ format: memory │ | | | │ rows: 0 │ | | | └───────────────────────────┘ | | | | +---------------+-------------------------------+ 1 row(s) fetched. ``` ### To Reproduce _No response_ ### Expected behavior _No response_ ### Additional context _No response_ -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org