Repository navigation
fix(optimizer): keep the rows of a second DISTINCT after WITH DISTINCT on a primary key #1353
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
Open
zahariash
wants to merge
3
commits into
LadybugDB:main
Choose a base branch
from
zahariash:fix/removed-distinct-flattens
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+100
−16
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
e2c8ead
fix(optimizer): remove a primary-key DISTINCT before placing flattens
zahariash 6efd2e0
chore(optimizer): assert that the DISTINCT removal pass gets a flat plan
zahariash 3531dd2
test(optimizer): pin that a removed primary-key DISTINCT leaves no FL…
zahariash 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
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
51 changes: 51 additions & 0 deletions
51
test/test_files/optimizer/remove_unnecessary_distinct.test
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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| -DATASET CSV empty | ||
|
|
||
| -- | ||
|
|
||
| -CASE ExpressionOverRemovedDistinct | ||
| -STATEMENT CREATE NODE TABLE P(id INT64 PRIMARY KEY, flag BOOLEAN) | ||
| ---- ok | ||
| -STATEMENT CREATE (:P {id: 1, flag: true}), (:P {id: 2, flag: false}), (:P {id: 3, flag: true}), (:P {id: 4}) | ||
| ---- ok | ||
| -LOG n.flag depends on the key n.id, so the first DISTINCT has only primary keys and is removed | ||
| -STATEMENT MATCH (n:P) WITH DISTINCT n.id AS v, n.flag AS f WITH DISTINCT CASE WHEN f THEN v ELSE 1 END AS w RETURN w | ||
| ---- 2 | ||
| 1 | ||
| 3 | ||
| -STATEMENT MATCH (n:P) WITH DISTINCT n.id AS v WITH DISTINCT coalesce(v, 0) AS w RETURN w | ||
| ---- 4 | ||
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| -STATEMENT MATCH (n:P) WITH DISTINCT n.id AS v RETURN DISTINCT coalesce(v, 0) AS w | ||
| ---- 4 | ||
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| -STATEMENT MATCH (n:P) WITH DISTINCT n.id AS v WITH DISTINCT [v] AS w RETURN w | ||
| ---- 4 | ||
| [1] | ||
| [2] | ||
| [3] | ||
| [4] | ||
| -STATEMENT MATCH (n:P) WITH DISTINCT n.id AS v WITH DISTINCT v + 1 AS w RETURN w | ||
| ---- 4 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| -STATEMENT MATCH (n:P) WITH DISTINCT n.id AS v MATCH (m:P) WHERE m.id = v + 1 RETURN count(*) | ||
| ---- 1 | ||
| 3 | ||
| -STATEMENT MATCH (n:P) WITH DISTINCT n.id AS v UNWIND [v, v + 1] AS x RETURN count(DISTINCT x) | ||
| ---- 1 | ||
| 5 | ||
| -STATEMENT MATCH (n:P) WITH DISTINCT n.id AS v RETURN [v] AS w UNION ALL MATCH (n:P) WHERE n.id = 1 RETURN [n.id] AS w | ||
| ---- 5 | ||
| [1] | ||
| [1] | ||
| [2] | ||
| [3] | ||
| [4] |
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.
Low (non-blocking, fail-closed today):
computeFlatSchema()unconditionally throws on FLATTEN (logical_flatten.cpp:13-14). The invariant holds today (RemoveFactorizationRewriterstrips FLATTENs first and this pass runs beforeFactorizationRewriter), but it is implicit. Consider a one-line DASSERT/comment at the pass entry, or defensively bypassing FLATTEN nodes, so a future pass-order change fails loudly at the right place.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.
Done in 6efd2e0. With runtime checks, the pass now asserts at entry that the plan has no FLATTEN, next to a one-line comment saying why.
RemoveUnnecessaryDistinctRequiresFlatPlanruns the pass on an optimized plan and expects the error from that assertion; without it, the test fails onLogicalFlatten::computeFlatSchema() should never be used.Release builds skip the walk and still throw incomputeFlatSchema. I kept FLATTEN failing rather than bypassing it, since skipping it would hide a reorder.