Repository navigation
fix: wrong aggregate functional dependencies when a GROUP BY column is not determined by the key - #27113
Open
asolimando wants to merge 1 commit into
Open
fix: wrong aggregate functional dependencies when a GROUP BY column is not determined by the key#27113asolimando wants to merge 1 commit into
asolimando wants to merge 1 commit into
Conversation
…s not determined by the key `aggregate_functional_dependencies` marked a key as `Single` on the aggregate output when another GROUP BY column was determined by no input dependency, and gave every re-expressed dependency all output columns as targets. Rules that trust these dependencies, such as `EliminateJoin`, returned wrong results. Closes apache#27112.
asolimando
marked this pull request as ready for review
November 5, 2026 08:53
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
The subquery below groups by
i_item_sk, s_date. Item 1 has two dates, so it forms two groups:aggregate_functional_dependenciesmarkedi_item_skasDependency::Singleon the output off(a key: at most one row per value). That is wrong, because item 1 has two rows.It did so because nothing determines
s_date. The check compared whati_item_skdetermines with what the whole GROUP BY list determines.s_datewas in neither set, so the two sets were equal.EliminateJointrusted the wrong key and turned the join into a semi join (eachordersrow kept once instead of once per matching group). Item 1's order was counted once, hence 30.The function also gave every dependency all output columns as
target_indices. That claimsi_item_skdeterminess_date, which breaks other rules, for example an outerGROUP BY i_item_sk, s_datedropss_date.What changes are included in this PR?
Singleonly if it isSinglein the input or its determinant determines every GROUP BY expression. A computed GROUP BY expression never counts as determined.Singledependency replaces it, and its extraneous columns are removed withget_required_group_by_exprs_indices. The SQL planner adds determined columns to the GROUP BY, so without this the dependency was lost when a projection removed them, and selecting an aggregate value through such a projection failed to plan.What is the testing strategy for this PR?
SQL logic tests in
functional_dependencies.slt:The first and third fail without the fix, and the last fails without the change to the GROUP BY dependency. All sqllogictests pass.
Are there any user-facing changes?
Yes. Queries such as the one above now return the correct result. Queries that relied on the wrong dependencies, such as
SELECT s_date ... GROUP BY i_item_skoverf, are now rejected at planning. This also rejects selecting an aggregate value grouped byi_item_skalone overGROUP BY i_item_sk, upper(i_desc): it is correct, but proving it needs dependencies on expressions, which are not tracked. There are no API changes.Disclaimer: I used AI to assist in the code generation, I have manually reviewed the output and it matches my intention and understanding.