Hrushikesh-ramilla opened a new pull request, #27230: URL: https://github.com/apache/datafusion/pull/27230
## Which issue does this PR close? - Closes #27219. ## Rationale for this change When a materialized CTE (`MaterializedCteExec`) is queried under an outer `ORDER BY ... LIMIT`, `TopKAggregation` traverses down through all children because `MaterializedCteExec` reports `CardinalityEffect::Equal`. Because child 0 is the shared CTE body that materializes rows for all references, pushing the `limit` hint into an aggregate inside the CTE body truncated the materialized rows, leading to incorrect query results for downstream consumers. ## What changes are included in this PR? - Updated `TopKAggregation` to explicitly intercept `MaterializedCteExec`. - Only optimize and traverse into child 1 (the continuation), skipping child 0 (the shared CTE body) using `TreeNodeRecursion::Jump`. - Rebuilt `MaterializedCteExec` via `replace_children_if_necessary` when the continuation is transformed. ## What is the testing strategy for this PR? - Added regression test cases in `datafusion/sqllogictest/test_files/cte_materialized.slt` covering both filtered and cross-join queries with materialized CTEs under `ORDER BY ... LIMIT`. - Ran `cargo test -p datafusion-physical-optimizer` (51 tests passed). - Ran `cargo test --test sqllogictests -- cte_materialized` (passed). ## Are there any user-facing changes? No. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
