-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: do not remove DISTINCT when a unique key was downgraded by a join #23548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+66
−6
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5641,3 +5641,56 @@ set datafusion.execution.target_partitions = 4; | |
|
|
||
| statement count 0 | ||
| drop table t; | ||
|
|
||
| # DISTINCT must not be removed when a unique key is downgraded to a | ||
| # non-unique functional dependency by a join: `u.id` is a primary key, but | ||
| # after the LEFT JOIN each `u` row can occur once per matching order. | ||
| statement ok | ||
| CREATE TABLE users_with_pk (id INT, name VARCHAR, primary key(id)) AS VALUES | ||
| (1, 'alice'), | ||
| (2, 'bob'); | ||
|
|
||
| statement ok | ||
| CREATE TABLE user_orders (user_id INT, amount INT) AS VALUES | ||
| (1, 10), | ||
| (1, 20), | ||
| (2, 30); | ||
|
|
||
| query I | ||
| SELECT DISTINCT u.id | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I verified this fails without the fix |
||
| FROM users_with_pk u | ||
| LEFT JOIN user_orders o ON u.id = o.user_id | ||
| ORDER BY u.id; | ||
| ---- | ||
| 1 | ||
| 2 | ||
|
|
||
| # The DISTINCT must be planned as an Aggregate; it cannot be removed based | ||
| # on the (join-downgraded) primary key of `users_with_pk`. | ||
| query TT | ||
| EXPLAIN SELECT DISTINCT u.id | ||
| FROM users_with_pk u | ||
| LEFT JOIN user_orders o ON u.id = o.user_id; | ||
| ---- | ||
| logical_plan | ||
| 01)Aggregate: groupBy=[[u.id]], aggr=[[]] | ||
| 02)--Projection: u.id | ||
| 03)----Left Join: u.id = o.user_id | ||
| 04)------SubqueryAlias: u | ||
| 05)--------TableScan: users_with_pk projection=[id] | ||
| 06)------SubqueryAlias: o | ||
| 07)--------TableScan: user_orders projection=[user_id] | ||
| physical_plan | ||
| 01)AggregateExec: mode=FinalPartitioned, gby=[id@0 as id], aggr=[] | ||
| 02)--RepartitionExec: partitioning=Hash([id@0], 4), input_partitions=4 | ||
| 03)----AggregateExec: mode=Partial, gby=[id@0 as id], aggr=[] | ||
| 04)------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1 | ||
| 05)--------HashJoinExec: mode=CollectLeft, join_type=Left, on=[(id@0, user_id@0)], projection=[id@0] | ||
| 06)----------DataSourceExec: partitions=1, partition_sizes=[1] | ||
| 07)----------DataSourceExec: partitions=1, partition_sizes=[1] | ||
|
|
||
| statement ok | ||
| drop table users_with_pk; | ||
|
|
||
| statement ok | ||
| drop table user_orders; | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a nice drive by change