fix: TopK aggregation drops groups whose MIN/MAX value is NULL#23684
Open
u70b3 wants to merge 1 commit into
Open
fix: TopK aggregation drops groups whose MIN/MAX value is NULL#23684u70b3 wants to merge 1 commit into
u70b3 wants to merge 1 commit into
Conversation
Grouped TopK aggregation previously dropped groups whose MIN/MAX inputs were all NULL. Track bounded all-NULL candidates alongside valued groups and emit them for the final sort to rank and truncate. Handle NULL-to-value transitions, fully reuse freed hash-table slots, and keep NULL bookkeeping off the common no-NULL hot path. Nullable MIN/MAX with NULLS FIRST is not monotonic for bounded aggregation: a group can move from NULL to a worse non-NULL rank. Skip the TopK pushdown in that case to preserve exact results while retaining it for NULLS LAST and non-nullable inputs. Closes apache#23440, closes apache#22190.
u70b3
force-pushed
the
fix/grouped-topk-null-aggregate-values
branch
from
July 20, 2026 06:58
9238ac1 to
a7f380c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
When
TopKAggregationpushes aLIMITinto a MIN/MAX aggregate, a group whose aggregate inputs are all NULL can disappear instead of being returned with a NULL aggregate value.The stream previously skipped NULL aggregate inputs without registering the group. This is correct for an individual MIN/MAX input, but not for a group whose inputs are all NULL.
There is also an important correctness boundary: nullable MIN/MAX with
NULLS FIRSTis not monotonic for a bounded aggregation. A group can start at NULL, later become non-NULL, and thereby move to a worse rank. Keeping onlylimitNULL candidates can therefore discard a group that belongs in the final result.What changes are included?
limitall-NULL candidates alongside valued TopK groups and emit them for the parent sort to rank and truncate.NULLS FIRST; regular aggregation is used for exact results. TopK remains enabled forNULLS LAST, non-nullable MIN/MAX inputs, and GROUP BY-only/DISTINCT queries.Are these changes tested?
Yes. The following passed on the final commit:
cargo fmt --all -- --checkcargo clippy --all-targets --all-features -- -D warningsaggregates_topk.sltand affectedgroup_by.slttestsavro,json,backtrace,extended_tests,recursive_protection,parquet_encryption, including all 495 sqllogic files and extended/fuzz suitesPerformance
The
topk_aggregate10-million-row time-series benchmark exposed an initial ~5.5% regression from checking NULL state on every row. Moving NULL handling to a batch-selected slow path removed the measurable regression.Final 30-sample 95% intervals on the same machine and settings:
main: 25.630–26.436 msAre there any user-facing changes?
Queries that previously dropped all-NULL groups under
ORDER BY <min/max> ... LIMITnow return correct SQL results. Nullable MIN/MAX queries usingNULLS FIRSTmay use regular aggregation rather than the bounded TopK optimization to guarantee correctness. There are no API or configuration changes.