Skip to content

Commit

Permalink
[Refactor] refactor pattern logical (StarRocks#55048)
Browse files Browse the repository at this point in the history
Signed-off-by: Seaven <[email protected]>
  • Loading branch information
Seaven authored Jan 16, 2025
1 parent c653f83 commit 1fa3cd0
Show file tree
Hide file tree
Showing 27 changed files with 431 additions and 390 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.starrocks.sql.optimizer.operator.logical.LogicalAggregationOperator;
import com.starrocks.sql.optimizer.operator.logical.LogicalOlapScanOperator;
import com.starrocks.sql.optimizer.operator.logical.LogicalScanOperator;
import com.starrocks.sql.optimizer.operator.pattern.Pattern;
import com.starrocks.sql.optimizer.operator.pattern.MultiOpPattern;
import com.starrocks.sql.optimizer.operator.scalar.ColumnRefOperator;
import com.starrocks.sql.optimizer.operator.scalar.ScalarOperator;
import com.starrocks.sql.optimizer.rule.transformation.materialization.MaterializedViewRewriter;
Expand Down Expand Up @@ -323,7 +323,7 @@ private static int getOperatorOrdering(OperatorType op) {
return 1;
} else if (op == OperatorType.LOGICAL_JOIN) {
return 2;
} else if (Pattern.isScanOperator(op)) {
} else if (MultiOpPattern.ALL_SCAN_TYPES.contains(op)) {
return 3;
} else {
return 4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,7 @@ private OptExpression logicalRuleRewrite(
ruleBasedMaterializedViewRewrite(tree, rootTaskContext, requiredColumns);

// this rewrite rule should be after mv.
scheduler.rewriteIterative(tree, rootTaskContext, RewriteSimpleAggToHDFSScanRule.HIVE_SCAN_NO_PROJECT);
scheduler.rewriteIterative(tree, rootTaskContext, RewriteSimpleAggToHDFSScanRule.ICEBERG_SCAN_NO_PROJECT);
scheduler.rewriteIterative(tree, rootTaskContext, RewriteSimpleAggToHDFSScanRule.FILE_SCAN_NO_PROJECT);
scheduler.rewriteOnce(tree, rootTaskContext, RewriteSimpleAggToHDFSScanRule.SCAN_NO_PROJECT);

// NOTE: This rule should be after MV Rewrite because MV Rewrite cannot handle
// select count(distinct c) from t group by a, b
Expand Down Expand Up @@ -842,16 +840,12 @@ private OptExpression pushDownAggregation(OptExpression tree, TaskContext rootTa
}

private void skewJoinOptimize(OptExpression tree, TaskContext rootTaskContext) {
SkewJoinOptimizeRule rule = new SkewJoinOptimizeRule();
if (context.getSessionVariable().isEnableStatsToOptimizeSkewJoin()) {
// merge projects before calculate statistics
scheduler.rewriteOnce(tree, rootTaskContext, new MergeTwoProjectRule());
Utils.calculateStatistics(tree, rootTaskContext.getOptimizerContext());
}
if (scheduler.rewriteOnce(tree, rootTaskContext, rule)) {
// skew join generate new join and on predicate, need to push down join on expression to child project again
scheduler.rewriteOnce(tree, rootTaskContext, new PushDownJoinOnExpressionToChildProject());
}
scheduler.rewriteOnce(tree, rootTaskContext, new SkewJoinOptimizeRule());
}

private OptExpression pruneSubfield(OptExpression tree, TaskContext rootTaskContext, ColumnRefSet requiredColumns) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.starrocks.sql.optimizer.operator.pattern;

import com.starrocks.sql.optimizer.operator.OperatorType;

public class AnyPattern extends OpPattern {
protected AnyPattern() {
super(OperatorType.PATTERN_LEAF);
}

@Override
public boolean isFixedPattern() {
return false;
}

@Override
protected boolean matchWithoutChild(OperatorType op) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.starrocks.sql.optimizer.operator.pattern;

import com.starrocks.sql.optimizer.operator.OperatorType;

public class MultiJoinPattern extends OpPattern {
protected MultiJoinPattern() {
super(OperatorType.PATTERN_MULTIJOIN);
}

@Override
public boolean isFixedPattern() {
return false;
}

@Override
protected boolean matchWithoutChild(OperatorType op) {
return op.equals(OperatorType.LOGICAL_JOIN) || MultiOpPattern.ALL_SCAN_TYPES.contains(op);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.starrocks.sql.optimizer.operator.pattern;

import com.starrocks.sql.optimizer.operator.OperatorType;

public class MultiLeafPattern extends OpPattern {
protected MultiLeafPattern() {
super(OperatorType.PATTERN_MULTI_LEAF);
}

@Override
public boolean isFixedPattern() {
return false;
}

@Override
protected boolean matchWithoutChild(OperatorType op) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.starrocks.sql.optimizer.operator.pattern;

import com.google.common.collect.ImmutableSet;
import com.starrocks.sql.optimizer.operator.OperatorType;

import java.util.Set;

public class MultiOpPattern extends Pattern {
public static final ImmutableSet<OperatorType> ALL_SCAN_TYPES = ImmutableSet.<OperatorType>builder()
.add(OperatorType.LOGICAL_OLAP_SCAN)
.add(OperatorType.LOGICAL_HIVE_SCAN)
.add(OperatorType.LOGICAL_ICEBERG_SCAN)
.add(OperatorType.LOGICAL_HUDI_SCAN)
.add(OperatorType.LOGICAL_FILE_SCAN)
.add(OperatorType.LOGICAL_SCHEMA_SCAN)
.add(OperatorType.LOGICAL_MYSQL_SCAN)
.add(OperatorType.LOGICAL_ES_SCAN)
.add(OperatorType.LOGICAL_META_SCAN)
.add(OperatorType.LOGICAL_JDBC_SCAN)
.add(OperatorType.LOGICAL_BINLOG_SCAN)
.add(OperatorType.LOGICAL_VIEW_SCAN)
.add(OperatorType.LOGICAL_PAIMON_SCAN)
.add(OperatorType.PATTERN_SCAN)
.build();

private final Set<OperatorType> ops;
protected MultiOpPattern(Set<OperatorType> ops) {
super();
this.ops = ops;
}

@Override
protected boolean matchWithoutChild(OperatorType op) {
return ops.contains(op);
}

public static Pattern ofAllScan() {
return of(ALL_SCAN_TYPES);
}

public static Pattern of(OperatorType... types) {
return new MultiOpPattern(Set.of(types));
}

public static Pattern of(Set<OperatorType> types) {
return new MultiOpPattern(types);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.starrocks.sql.optimizer.operator.pattern;

import com.starrocks.sql.optimizer.operator.OperatorType;

public class OpPattern extends Pattern {

protected final OperatorType opType;

protected OpPattern(OperatorType opType) {
super();
this.opType = opType;
}

public OperatorType getOpType() {
return opType;
}

@Override
public boolean is(OperatorType opType) {
return this.opType.equals(opType);
}

@Override
public boolean isFixedPattern() {
return true;
}

@Override
protected boolean matchWithoutChild(OperatorType op) {
return opType.equals(op);
}
}
Loading

0 comments on commit 1fa3cd0

Please sign in to comment.