-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Preserve constant values across union operations #13805
Open
gokselk
wants to merge
13
commits into
apache:main
Choose a base branch
from
gokselk:feature/const-expr-value-tracking
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.
+181
−48
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d4e41b1
Add value tracking to ConstExpr for improved union optimization
gokselk fc58594
Update PartialEq impl
gokselk 5b3278e
Minor change
gokselk 5201e3b
Add docstring for ConstExpr value
gokselk de8bc13
Improve constant propagation across union partitions
gokselk 35bfdc4
Add assertion for across_partitions
gokselk 76f497e
fix fmt
berkaysynnada 2721609
Update properties.rs
berkaysynnada 291257f
Remove redundant constant removal loop
gokselk 8bc7fd2
Remove unnecessary mut
gokselk 3051cd4
Set across_partitions=true when both sides are constant
gokselk 4c3f0d1
Extract and use constant values in filter expressions
gokselk a23faed
Add initial SLT for constant value tracking across UNION ALL
gokselk 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 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 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 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
50 changes: 50 additions & 0 deletions
50
datafusion/sqllogictest/test_files/const_value_tracking_across_union.slt
This file contains 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,50 @@ | ||
statement ok | ||
CREATE EXTERNAL TABLE aggregate_test_100 ( | ||
c1 VARCHAR NOT NULL, | ||
c2 TINYINT NOT NULL, | ||
c3 SMALLINT NOT NULL, | ||
c4 SMALLINT, | ||
c5 INT, | ||
c6 BIGINT NOT NULL, | ||
c7 SMALLINT NOT NULL, | ||
c8 INT NOT NULL, | ||
c9 BIGINT UNSIGNED NOT NULL, | ||
c10 VARCHAR NOT NULL, | ||
c11 FLOAT NOT NULL, | ||
c12 DOUBLE NOT NULL, | ||
c13 VARCHAR NOT NULL | ||
) | ||
STORED AS CSV | ||
LOCATION '../../testing/data/csv/aggregate_test_100.csv' | ||
OPTIONS ('format.has_header' 'true'); | ||
|
||
statement ok | ||
set datafusion.explain.physical_plan_only = true; | ||
|
||
statement ok | ||
set datafusion.execution.batch_size = 2; | ||
|
||
# Constant value tracking across union | ||
query TT | ||
explain | ||
SELECT * FROM( | ||
( | ||
SELECT * FROM aggregate_test_100 WHERE c1='a' | ||
) | ||
UNION ALL | ||
( | ||
SELECT * FROM aggregate_test_100 WHERE c1='a' | ||
)) | ||
ORDER BY c1 | ||
---- | ||
physical_plan | ||
01)CoalescePartitionsExec | ||
02)--UnionExec | ||
03)----CoalesceBatchesExec: target_batch_size=2 | ||
04)------FilterExec: c1@0 = a | ||
05)--------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1 | ||
06)----------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13], has_header=true | ||
07)----CoalesceBatchesExec: target_batch_size=2 | ||
08)------FilterExec: c1@0 = a | ||
09)--------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1 | ||
10)----------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13], has_header=true |
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.
Is there a way to crate an end to end
.slt
test that shows this behavior?For example, a
EXPLAIN PLAN
where a Sort is optimized away after the constant value is propagated through the union?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.
Good idea! I have one in my mind. Let me add it
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.
Hey @alamb, I tried it but after thinking more, we actually need one more step in planner to experience an end-to-end difference. Now we have the knowledge, but we are not using it. 2 possible optimizations are which come to my mind now:
Let's assume we have:
2nd one could be not really realistic, but the first one could be implemented without much effort with a few changes in replace_with_order_preserving_variants scope.
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.
Can you take a look at the first check @gokselk? It should take a few line changes in plan_with_order_preserving_variants() function. It should first look the order requirements, and if they are matched, then it would try to convert CoalescePartitionExec to SortPreservingMergeExec. But before that conversion, you can check across_partitions flag of the input constants, and if it is true, you can left the CoalescePartitionsExec as is.
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.
I've made changes to
FilterExec
for value extraction and added an initial SLT file. The query now showsCoalescePartitionExec
in the output, so I think your suggested changes toplan_with_order_preserving_variants()
might not be needed anymore. However, I'd appreciate your review to confirm this.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.
It appears that I broke some ORDER BY queries in my recent commits. I will investigate this further.
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.
To add more context, some tests are failing non-deterministically, which is why I didn't notice it beforehand.
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.
I'm not sure what the actual situation is w.r.t. those tests, but I'd advise to take a look at whether they were underspecified in the first place (i.e. the query itself may not be specifying a concrete output ordering, which could make the test flaky).
Do failing queries have top level ORDER BY clauses? If so, it is probably a bug that was introduced. Otherwise, maybe they were flaky in the first place.