Skip to content

Commit

Permalink
[Refactor] refactor optimizer code (StarRocks#52278)
Browse files Browse the repository at this point in the history
Signed-off-by: Seaven <seaven_7@qq.com>
  • Loading branch information
Seaven authored Oct 24, 2024
1 parent 16ffabe commit 4f5a6cb
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 20 deletions.
31 changes: 18 additions & 13 deletions fe/fe-core/src/main/java/com/starrocks/analysis/JoinOperator.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,19 @@
import java.util.Set;

public enum JoinOperator {
INNER_JOIN("INNER JOIN", TJoinOp.INNER_JOIN),
LEFT_OUTER_JOIN("LEFT OUTER JOIN", TJoinOp.LEFT_OUTER_JOIN),

LEFT_SEMI_JOIN("LEFT SEMI JOIN", TJoinOp.LEFT_SEMI_JOIN),
LEFT_ANTI_JOIN("LEFT ANTI JOIN", TJoinOp.LEFT_ANTI_JOIN),
RIGHT_SEMI_JOIN("RIGHT SEMI JOIN", TJoinOp.RIGHT_SEMI_JOIN),
RIGHT_ANTI_JOIN("RIGHT ANTI JOIN", TJoinOp.RIGHT_ANTI_JOIN),
RIGHT_OUTER_JOIN("RIGHT OUTER JOIN", TJoinOp.RIGHT_OUTER_JOIN),
FULL_OUTER_JOIN("FULL OUTER JOIN", TJoinOp.FULL_OUTER_JOIN),
CROSS_JOIN("CROSS JOIN", TJoinOp.CROSS_JOIN),
// Variant of the LEFT ANTI JOIN that is used for the equal of
INNER_JOIN("INNER JOIN", "⋈", TJoinOp.INNER_JOIN),
LEFT_OUTER_JOIN("LEFT OUTER JOIN", "⟕", TJoinOp.LEFT_OUTER_JOIN),

LEFT_SEMI_JOIN("LEFT SEMI JOIN", "⋉", TJoinOp.LEFT_SEMI_JOIN),
LEFT_ANTI_JOIN("LEFT ANTI JOIN", "◁", TJoinOp.LEFT_ANTI_JOIN),
RIGHT_SEMI_JOIN("RIGHT SEMI JOIN", "⋊", TJoinOp.RIGHT_SEMI_JOIN),
RIGHT_ANTI_JOIN("RIGHT ANTI JOIN", "▷", TJoinOp.RIGHT_ANTI_JOIN),
RIGHT_OUTER_JOIN("RIGHT OUTER JOIN", "⟖", TJoinOp.RIGHT_OUTER_JOIN),
FULL_OUTER_JOIN("FULL OUTER JOIN", "⟗", TJoinOp.FULL_OUTER_JOIN),
CROSS_JOIN("CROSS JOIN", "×", TJoinOp.CROSS_JOIN), // Variant of the LEFT ANTI JOIN that is used for the equal of
// NOT IN subqueries. It can have a single equality join conjunct
// that returns TRUE when the rhs is NULL.
NULL_AWARE_LEFT_ANTI_JOIN("NULL AWARE LEFT ANTI JOIN",
NULL_AWARE_LEFT_ANTI_JOIN("NULL AWARE LEFT ANTI JOIN", "▷*",
TJoinOp.NULL_AWARE_LEFT_ANTI_JOIN);

public static final String HINT_BUCKET = "BUCKET";
Expand All @@ -65,10 +64,12 @@ public enum JoinOperator {
public static final String HINT_UNREORDER = "UNREORDER";

private final String description;
private final String algebra;
private final TJoinOp thriftJoinOp;

private JoinOperator(String description, TJoinOp thriftJoinOp) {
JoinOperator(String description, String algebra, TJoinOp thriftJoinOp) {
this.description = description;
this.algebra = algebra;
this.thriftJoinOp = thriftJoinOp;
}

Expand All @@ -77,6 +78,10 @@ public String toString() {
return description;
}

public String toAlgebra() {
return algebra;
}

public TJoinOp toThrift() {
return thriftJoinOp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public String visitLogicalTableFunction(LogicalTableFunctionOperator node, Void

@Override
public String visitLogicalLimit(LogicalLimitOperator node, Void context) {
return "LogicalLimitOperator" + " {limit=" + node.getLimit() +
return "LogicalLimitOperator {" + node.getPhase().name() + " limit=" + node.getLimit() +
", offset=" + node.getOffset() +
"}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,9 @@ private String debugString(String headlinePrefix, String detailPrefix, int limit
StringBuilder sb = new StringBuilder();
sb.append(headlinePrefix).append(op.accept(new DebugOperatorTracer(), null));
limitLine -= 1;
sb.append('\n');
if (limitLine <= 0 || inputs.isEmpty()) {
return sb.toString();
}

String childHeadlinePrefix = detailPrefix + "-> ";
String childDetailPrefix = detailPrefix + " ";
for (OptExpression input : inputs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,8 @@ private OptExpression logicalRuleRewrite(
ruleRewriteIterative(tree, rootTaskContext, new MergeProjectWithChildRule());

ruleRewriteOnlyOnce(tree, rootTaskContext, new PushDownTopNBelowOuterJoinRule());
// intersect rewrite depend on statistics
Utils.calculateStatistics(tree, rootTaskContext.getOptimizerContext());
ruleRewriteOnlyOnce(tree, rootTaskContext, RuleSetType.INTERSECT_REWRITE);
ruleRewriteIterative(tree, rootTaskContext, new RemoveAggregationFromAggTable());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,18 @@ public ScalarOperator visitVariableReference(ColumnRefOperator column, Void cont
// The rewritten predicate will be rewritten continually,
// Rewiring predicate shouldn't change the origin project columnRefMap

ScalarOperator mapperOperator = operatorMap.get(column).clone();
if (isRecursively) {
while (mapperOperator.getChildren().isEmpty() && operatorMap.containsKey(mapperOperator)) {
ScalarOperator mapperOperator = operatorMap.get(column);
if (!isRecursively) {
return mapperOperator.clone();
} else {
while (mapperOperator instanceof ColumnRefOperator && operatorMap.containsKey(mapperOperator)) {
ScalarOperator mapped = operatorMap.get(mapperOperator);
if (mapped.equals(mapperOperator)) {
break;
}
mapperOperator = mapped.clone();
mapperOperator = mapped;
}
mapperOperator = mapperOperator.clone();
for (int i = 0; i < mapperOperator.getChildren().size(); ++i) {
mapperOperator.setChild(i, mapperOperator.getChild(i).accept(this, null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ public class RuleSet {
MergeLimitDirectRule.EXCEPT,
MergeLimitDirectRule.VALUES,
MergeLimitDirectRule.FILTER,
MergeLimitDirectRule.CTE_CONSUMER,
MergeLimitDirectRule.TABLE_FUNCTION,
MergeLimitDirectRule.TABLE_FUNCTION_TABLE_SCAN
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class MergeLimitDirectRule extends TransformationRule {
new MergeLimitDirectRule(OperatorType.LOGICAL_TABLE_FUNCTION);
public static final MergeLimitDirectRule TABLE_FUNCTION_TABLE_SCAN =
new MergeLimitDirectRule(OperatorType.LOGICAL_TABLE_FUNCTION_TABLE_SCAN);
public static final MergeLimitDirectRule CTE_CONSUMER =
new MergeLimitDirectRule(OperatorType.LOGICAL_CTE_CONSUME);

private MergeLimitDirectRule(OperatorType logicalOperatorType) {
super(RuleType.TF_MERGE_LIMIT_DIRECT, Pattern.create(OperatorType.LOGICAL_LIMIT)
Expand Down

0 comments on commit 4f5a6cb

Please sign in to comment.