discord9 opened a new pull request, #25066: URL: https://github.com/apache/datafusion/pull/25066
## Which issue does this PR close? No linked issue. This is an independently reproduced correctness fix. ## Rationale for this change TopK aggregation can discard groups that are needed to compute window values. With two groups `g = 1, 2`, the default SQL pipeline returns `(1, 1)` instead of `(1, 2)` for: ```sql SELECT g, COUNT(*) OVER () AS n FROM t GROUP BY g ORDER BY g ASC LIMIT 1; ``` The window preserves the number of input rows, but its output values can depend on groups that an ancestor TopK would discard. Cardinality preservation alone does not make this propagation valid. ## What changes are included in this PR? Stop this ancestor TopK's aggregate-limit propagation at `WindowAggExec` and `BoundedWindowAggExec`, using the existing traversal flag. Keep aggregate/projection handling and independent optimization of eligible sorts within window inputs unchanged. This change is independent of #23800 and #25065 and does not modify their limit-pushdown or distribution-enforcement paths. ## What is the testing strategy for this PR? Added SQLLogicTest coverage in `aggregates_topk.slt` for: - `COUNT(*) OVER ()`, with TopK aggregation enabled and disabled; - a bounded preceding-row window ordered descending under an ascending outer TopK, ensuring the fetched outer sort remains present; - a no-outer-limit control and generated EXPLAIN plans for both window implementations. Both window guards were independently removed during regression verification: each corresponding enabled query changed from `(1, 2)` to `(1, 1)`. Both guards were restored, and the full test file passed. Verified locally: - `cargo check -p datafusion-physical-optimizer` - `cargo test -p datafusion-sqllogictest --test sqllogictests -- aggregates_topk` - `cargo fmt --all --check` - `cargo clippy --workspace --all-targets --all-features -- -D warnings` The full workspace runtime test suite was not run. ## Are there any user-facing changes? Correct window results for affected grouped TopK queries. No public API or configuration changes. This conservatively prevents an invalid optimization across windows rather than changing their execution semantics. -- 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]
