Skip to content
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

[Enhancement] add unionToValues optimizer rule (backport #42915) #54373

Merged
merged 3 commits into from
Dec 27, 2024

Conversation

mergify[bot]
Copy link
Contributor

@mergify mergify bot commented Dec 26, 2024

Why I'm doing:

For performing a UNION ALL with N SELECT constants, currently, N SELECT constants Fragments and one UNION ALL Fragment are generated. However, some user queries include UNION ALLs with several hundred or even thousands of constants, thus generating a massive amount of fragments and pipeline drivers, which significantly impacts performance.

eg:

select 1 union all select 10 + 'a' union all select 3
mysql> explain logical select 1 union all select 10 + 'a' union all select 3;
+-----------------------------------------------------------------------------------------+
| Explain String                                                                          |
+-----------------------------------------------------------------------------------------+
| - Output => [9:cast]                                                                    |
|     - UNION => [9: cast]                                                                |
|             Estimates: {row: 3, cpu: ?, memory: ?, network: ?, cost: 0.0}               |
|         - VALUES => [3:cast]                                                            |
|                 Estimates: {row: 1, cpu: 0.00, memory: 0.00, network: 0.00, cost: 0.00} |
|                 3:cast := 1.0                                                           |
|         - VALUES => [5:expr]                                                            |
|                 Estimates: {row: 1, cpu: ?, memory: ?, network: ?, cost: 0.0}           |
|                 5:expr := 10.0 + cast('a' as double)                                    |
|         - VALUES => [8:cast]                                                            |
|                 Estimates: {row: 1, cpu: 0.00, memory: 0.00, network: 0.00, cost: 0.00} |
|                 8:cast := 3.0                                                           |
+-----------------------------------------------------------------------------------------+
12 rows in set (0.01 sec)

manually changing it to use values greatly reduces the number of fragments:

mysql> explain logical select * from (values (1), (10 + 'a'), (3));
+---------------------------------------------------------------------------+
| Explain String                                                            |
+---------------------------------------------------------------------------+
| - Output => [1:column_0]                                                  |
|     - VALUES => {1.0}, {10.0 + cast('a' as double)}, {3.0}                |
|             Estimates: {row: 3, cpu: ?, memory: ?, network: ?, cost: 0.0} |
+---------------------------------------------------------------------------+
3 rows in set (0.04 sec)

What I'm doing:

For this scenario, add an optimization rule to transform all constant UNION ALLs into a single Values operator to avoid generating a massive number of fragments.

mysql> explain logical select 1 union all select 10 + 'a' union all select 3;
+---------------------------------------------------------------------------+
| Explain String                                                            |
+---------------------------------------------------------------------------+
| - Output => [9:cast]                                                      |
|     - VALUES => {1.0}, {10.0 + cast('a' as double)}, {3.0}                |
|             Estimates: {row: 3, cpu: ?, memory: ?, network: ?, cost: 0.0} |
+---------------------------------------------------------------------------+
3 rows in set (0.02 sec)

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
  • I have added documentation for my new feature or new function
  • This is a backport pr

Signed-off-by: liubotao <[email protected]>
(cherry picked from commit 52290a0)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/sql/optimizer/Optimizer.java
#	fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rule/RuleType.java
@mergify mergify bot added the conflicts label Dec 26, 2024
Copy link
Contributor Author

mergify bot commented Dec 26, 2024

Cherry-pick of 52290a0 has failed:

On branch mergify/bp/branch-3.2/pr-42915
Your branch is up to date with 'origin/branch-3.2'.

You are currently cherry-picking commit 52290a02c7.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   fe/fe-core/src/main/java/com/starrocks/sql/optimizer/operator/logical/LogicalValuesOperator.java
	new file:   fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rule/transformation/UnionToValuesRule.java
	modified:   fe/fe-core/src/test/java/com/starrocks/sql/plan/SelectConstTest.java
	modified:   fe/fe-core/src/test/java/com/starrocks/sql/plan/SetTest.java

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   fe/fe-core/src/main/java/com/starrocks/sql/optimizer/Optimizer.java
	both modified:   fe/fe-core/src/main/java/com/starrocks/sql/optimizer/rule/RuleType.java

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

Copy link
Contributor Author

mergify bot commented Dec 26, 2024

@mergify[bot]: Backport conflict, please reslove the conflict and resubmit the pr

@mergify mergify bot closed this Dec 26, 2024
@ZiheLiu ZiheLiu reopened this Dec 26, 2024
@wanpengfei-git wanpengfei-git enabled auto-merge (squash) December 26, 2024 06:38
@wanpengfei-git wanpengfei-git merged commit 24761ec into branch-3.2 Dec 27, 2024
29 checks passed
@wanpengfei-git wanpengfei-git deleted the mergify/bp/branch-3.2/pr-42915 branch December 27, 2024 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants