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 DrivingTableSelectionRule #55513

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

KKould
Copy link

@KKould KKould commented Jan 30, 2025

Why I'm doing:

#52399

support similar to #55376: If the table and driving table are only in an OR relationship, cross join is converted to union all

What I'm doing:

DrivingTableSelectionRule is used to adjust the position of the driving table according to the on predicate of the inner join when multiple tables are cross joined and the root is an inner join. Reduce the overhead caused by Join in this case

mysql> explain logical select *  from T where T.a = (select a from T0) and T.b = (select b from T1) or T.c = (select c from T2);
+-------------------------------------------------------------------------------------------------------------+
| Explain String                                                                                              |
+-------------------------------------------------------------------------------------------------------------+
| - Output => [1:a, 2:b, 3:c]                                                                                 |
|     - NESTLOOP/INNER JOIN [1:a = 4:a AND 2:b = 9:b OR 3:c = 14:c] => [1:a, 2:b, 3:c]                        |
|             Estimates: {row: 6, cpu: 1728.00, memory: 7200.00, network: 0.00, cost: 16017132.00}            |
|         - UNION => [4: a, 9: b, 14: c]                                                                      |
|                 Estimates: {row: 2, cpu: ?, memory: ?, network: ?, cost: 1.6001706E7}                       |
|             - NESTLOOP/CROSS JOIN => [4:a, 9:b, 14:c]                                                       |
|                     Estimates: {row: 1, cpu: 32000000.00, memory: 800.00, network: 0.00, cost: 16001676.00} |
|                     14:c := null                                                                            |
|                 - ASSERT LE 1                                                                               |
|                         Estimates: {row: 1, cpu: ?, memory: ?, network: ?, cost: 30.0}                      |
|                     - EXCHANGE(GATHER)                                                                      |
|                             Estimates: {row: 3, cpu: ?, memory: ?, network: ?, cost: 30.0}                  |
|                         - SCAN [T0] => [4:a]                                                                |
|                                 Estimates: {row: 3, cpu: ?, memory: ?, network: ?, cost: 6.0}               |
|                                 partitionRatio: 1/1, tabletRatio: 1/1                                       |
|                 - EXCHANGE(BROADCAST)                                                                       |
|                         Estimates: {row: 1, cpu: ?, memory: ?, network: ?, cost: 46.0}                      |
|                     - ASSERT LE 1                                                                           |
|                             Estimates: {row: 1, cpu: ?, memory: ?, network: ?, cost: 30.0}                  |
|                         - EXCHANGE(GATHER)                                                                  |
|                                 Estimates: {row: 3, cpu: ?, memory: ?, network: ?, cost: 30.0}              |
|                             - SCAN [T1] => [9:b]                                                            |
|                                     Estimates: {row: 3, cpu: ?, memory: ?, network: ?, cost: 6.0}           |
|                                     partitionRatio: 1/1, tabletRatio: 1/1                                   |
|             - ASSERT LE 1                                                                                   |
|                     Estimates: {row: 1, cpu: 0.00, memory: 0.00, network: 0.00, cost: 30.00}                |
|                     4:a := null                                                                             |
|                     9:b := null                                                                             |
|                 - EXCHANGE(GATHER)                                                                          |
|                         Estimates: {row: 3, cpu: ?, memory: ?, network: ?, cost: 30.0}                      |
|                     - SCAN [T2] => [14:c]                                                                   |
|                             Estimates: {row: 3, cpu: ?, memory: ?, network: ?, cost: 6.0}                   |
|                             partitionRatio: 1/1, tabletRatio: 1/1                                           |
|         - EXCHANGE(BROADCAST)                                                                               |
|                 Estimates: {row: 3, cpu: ?, memory: ?, network: ?, cost: 162.0}                             |
|             - SCAN [T] => [1:a, 2:b, 3:c]                                                                   |
|                     Estimates: {row: 3, cpu: ?, memory: ?, network: ?, cost: 18.0}                          |
|                     partitionRatio: 1/1, tabletRatio: 1/1                                                   |
+-------------------------------------------------------------------------------------------------------------+

Implementation idea:

Analyze the corresponding relationship between the tables of the two columns in the BinaryPredicateOperator through the on predicate of the inner join, and establish an undirected graph. When all edges only have the same table as the endpoint, the table is a driving table, which is the top layer of the inner join table to replace(if the replaced node is not the driving table)

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

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.4
    • 3.3
    • 3.2
    • 3.1
    • 3.0

@KKould KKould requested a review from a team as a code owner January 30, 2025 13:43
@mergify mergify bot assigned KKould Jan 30, 2025
@KKould KKould changed the title feat: add DrivingTableSelectionRule [Enhancement] Add DrivingTableSelectionRule Jan 30, 2025
@KKould KKould requested a review from a team as a code owner February 2, 2025 14:15
@KKould KKould force-pushed the feat/driving_table_selection branch 2 times, most recently from 393cf57 to c98645b Compare February 4, 2025 06:38
@LiShuMing
Copy link
Contributor

well done, can you rebase again?

LiShuMing
LiShuMing previously approved these changes Feb 5, 2025
create table T1 properties("replication_num"="1") as select * from T;
create table T2 properties("replication_num"="1") as select * from T;

explain select * from T where T.a = (select a from T0) or T.b = (select b from T1) or T.c = (select c from T2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. it's better to not explain this directly, you can refer assert_explain_contains in sql-tests
function: assert_explain_contains('SELECT COUNT(*) FROM partitions_multi_column_1 WHERE c1=1 AND TRUE', 'partitions=1/3')
  1. it's better to add some specific data and check rewrite result fine.

1 1 1
1 1 1
-- !result
explain select * from T where T.a = (select a from T0) or T.b = (select b from T1) or T.c = (select c from T2);
Copy link
Collaborator

@kangkaisen kangkaisen Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you only want to test the explain, you could add test cases in the FE code like PlanFragmentWithCostTest. which will be easier to debug and test.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, but I think it is better not to explain in this e2e test (fe ut has been verified)

Signed-off-by: Kould <[email protected]>
@KKould KKould force-pushed the feat/driving_table_selection branch from 083fe6b to 100c710 Compare February 6, 2025 06:15
Copy link

sonarqubecloud bot commented Feb 6, 2025

@KKould
Copy link
Author

KKould commented Feb 6, 2025

I tried to run the failed unit test in CI locally but it passed locally. Do you have any solution?

@before-Sunrise
Copy link
Contributor

I tried to run the failed unit test in CI locally but it passed locally. Do you have any solution?

this test case is not stable, i already rerun this pr's ci

Copy link

github-actions bot commented Feb 7, 2025

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

github-actions bot commented Feb 7, 2025

[FE Incremental Coverage Report]

pass : 179 / 195 (91.79%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/sql/optimizer/rule/transformation/DrivingTableSelection.java 177 193 91.71% [122, 132, 139, 159, 162, 181, 185, 194, 279, 300, 301, 303, 318, 339, 357, 358]
🔵 com/starrocks/sql/optimizer/QueryOptimizer.java 1 1 100.00% []
🔵 com/starrocks/sql/optimizer/rule/RuleType.java 1 1 100.00% []

Copy link

github-actions bot commented Feb 7, 2025

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants