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] Support Generated Column rewrite in complex Query #50398

Merged
merged 1 commit into from
Sep 9, 2024

Conversation

srlch
Copy link
Contributor

@srlch srlch commented Aug 29, 2024

In this pr:

  1. Refactor Generated Column rewrite code:
    a. Collect generated column expr mapping From QueryAnalyzer.
    b. Introduce ReplaceScalarOperatorRule to replace the ScalarOperator match the generated column expression.
  2. Supprot View, JOIN with Subquery and CTE for Generated Column rewriting.
  3. Support simple equivalent expression rewrite for generated column.

Note: All query structure support Generated Column rewriting obey a simple rule: the Generated Column and its ref columns output directly into the outer scope.

Fixes #issue

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.3
    • 3.2
    • 3.1
    • 3.0
    • 2.5

@srlch srlch requested review from a team as code owners August 29, 2024 02:52
@mergify mergify bot assigned srlch Aug 29, 2024

GeneratedColumnVisitor visitor = new GeneratedColumnVisitor();
visitor.process(node, viewScope);

return viewScope;
}

Copy link

Choose a reason for hiding this comment

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

How can I help you today?

public Map<Expr, SlotRef> getGeneratedExprToColumnRef() {
return generatedExprToColumnRef;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
return visitor.visitRelation(this, context);
Copy link

Choose a reason for hiding this comment

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

The most risky bug in this code is:
Potential thread-safety issues with generatedExprToColumnRef.

You can modify the code like this:

import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
//...

private Map<Expr, SlotRef> generatedExprToColumnRef = new ConcurrentHashMap<>();

public void setGeneratedExprToColumnRef(Map<Expr, SlotRef> generatedExprToColumnRef) {
    this.generatedExprToColumnRef = Collections.synchronizedMap(generatedExprToColumnRef);
}

@srlch srlch force-pushed the gc_column_rewrite2 branch 3 times, most recently from 784e377 to 7583580 Compare August 29, 2024 09:05
@srlch srlch changed the title [Enhancement] Support Generated Column rewrite from View and JOIN with Subquery [Enhancement] Support Generated Column rewrite in complex Query Aug 29, 2024
@srlch srlch force-pushed the gc_column_rewrite2 branch 2 times, most recently from f8b5ab8 to 05deed6 Compare September 6, 2024 05:39
@srlch srlch requested a review from a team as a code owner September 6, 2024 05:39
Seaven
Seaven previously approved these changes Sep 6, 2024
In this pr:
1. Refactor Generated Column rewrite code:
   a. Collect generated column expr mapping From QueryAnalyzer.
   b. Introduce ReplaceScalarOperatorRule to replace the ScalarOperator match the
      generated column expression.
2. Supprot View, JOIN with Subquery and CTE for Generated Column rewriting.
3. Support simple equivalent expression rewrite for generated column.

Note: All query structure support Generated Column rewriting obey a simple rule:
the Generated Column and its ref columns output directly into the outer scope.

Signed-off-by: srlch <[email protected]>
Copy link

sonarcloud bot commented Sep 6, 2024

Copy link

github-actions bot commented Sep 6, 2024

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

github-actions bot commented Sep 6, 2024

[FE Incremental Coverage Report]

pass : 127 / 132 (96.21%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/sql/analyzer/QueryAnalyzer.java 98 103 95.15% [173, 204, 206, 258, 270]
🔵 com/starrocks/sql/optimizer/rewrite/scalar/ReplaceScalarOperatorRule.java 8 8 100.00% []
🔵 com/starrocks/sql/ast/Relation.java 4 4 100.00% []
🔵 com/starrocks/sql/optimizer/transformer/QueryTransformer.java 8 8 100.00% []
🔵 com/starrocks/sql/optimizer/transformer/ExpressionMapping.java 4 4 100.00% []
🔵 com/starrocks/sql/optimizer/transformer/OptExprBuilder.java 1 1 100.00% []
🔵 com/starrocks/sql/optimizer/rewrite/ScalarOperatorRewriter.java 2 2 100.00% []
🔵 com/starrocks/sql/optimizer/transformer/SqlToScalarOperatorTranslator.java 2 2 100.00% []

Copy link

github-actions bot commented Sep 6, 2024

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

@murphyatwork murphyatwork merged commit 96b97a7 into StarRocks:main Sep 9, 2024
53 checks passed
Copy link

github-actions bot commented Sep 9, 2024

@Mergifyio backport branch-3.3

@github-actions github-actions bot removed the 3.3 label Sep 9, 2024
Copy link
Contributor

mergify bot commented Sep 9, 2024

backport branch-3.3

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Sep 9, 2024
Signed-off-by: srlch <[email protected]>
(cherry picked from commit 96b97a7)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/sql/analyzer/QueryAnalyzer.java
srlch added a commit to srlch/starrocks that referenced this pull request Sep 9, 2024
srlch added a commit to srlch/starrocks that referenced this pull request Sep 9, 2024
srlch added a commit to srlch/starrocks that referenced this pull request Sep 9, 2024
srlch added a commit to srlch/starrocks that referenced this pull request Sep 9, 2024
murphyatwork pushed a commit that referenced this pull request Sep 9, 2024
murphyatwork pushed a commit that referenced this pull request Sep 9, 2024
HangyuanLiu pushed a commit to HangyuanLiu/starrocks that referenced this pull request Sep 12, 2024
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