Skip to content

Commit

Permalink
add session variable disable_nereids_expression_rules
Browse files Browse the repository at this point in the history
  • Loading branch information
yujun777 committed Nov 30, 2024
1 parent 8492f02 commit 7ed708d
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Field;
import java.util.BitSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -48,10 +49,13 @@ public Optional<Expression> matchesAndApply(Expression expr, ExpressionRewriteCo
List<ExpressionPatternMatchRule> rules = singleMappings.get(expr.getClass());
ExpressionMatchingContext<Expression> matchingContext
= new ExpressionMatchingContext<>(expr, parent, context);
BitSet disableRules = context.cascadesContext.getConnectContext().getSessionVariable()
.getDisableNereidsExpressionRules();
switch (rules.size()) {
case 0: {
for (ExpressionPatternMatchRule multiMatchRule : multiMappings) {
if (multiMatchRule.matchesTypeAndPredicates(matchingContext)) {
if (!disableRules.get(multiMatchRule.getExpressionRuleType().type())
&& multiMatchRule.matchesTypeAndPredicates(matchingContext)) {
Expression newExpr = multiMatchRule.apply(matchingContext);
if (!newExpr.equals(expr)) {
if (context.cascadesContext.isEnableExprTrace()) {
Expand All @@ -65,7 +69,8 @@ public Optional<Expression> matchesAndApply(Expression expr, ExpressionRewriteCo
}
case 1: {
ExpressionPatternMatchRule rule = rules.get(0);
if (rule.matchesPredicates(matchingContext)) {
if (!disableRules.get(rule.getExpressionRuleType().type())
&& rule.matchesPredicates(matchingContext)) {
Expression newExpr = rule.apply(matchingContext);
if (!newExpr.equals(expr)) {
if (context.cascadesContext.isEnableExprTrace()) {
Expand All @@ -78,7 +83,8 @@ public Optional<Expression> matchesAndApply(Expression expr, ExpressionRewriteCo
}
default: {
for (ExpressionPatternMatchRule rule : rules) {
if (rule.matchesPredicates(matchingContext)) {
if (!disableRules.get(rule.getExpressionRuleType().type())
&& rule.matchesPredicates(matchingContext)) {
Expression newExpr = rule.apply(matchingContext);
if (!expr.equals(newExpr)) {
if (context.cascadesContext.isEnableExprTrace()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@

/** ExpressionPatternMatcherRule */
public class ExpressionPatternMatchRule implements TypeMapping<Expression> {
public final ExpressionRuleType expressionRuleType;
public final Class<? extends Expression> typePattern;
public final List<Predicate<ExpressionMatchingContext<Expression>>> predicates;
public final ExpressionMatchingAction<Expression> matchingAction;

public ExpressionPatternMatchRule(ExpressionPatternMatcher patternMatcher) {
this.expressionRuleType = patternMatcher.expressionRuleType;
this.typePattern = patternMatcher.typePattern;
this.predicates = patternMatcher.predicates;
this.matchingAction = patternMatcher.matchingAction;
Expand Down Expand Up @@ -61,4 +63,8 @@ public Expression apply(ExpressionMatchingContext<Expression> context) {
public Class<? extends Expression> getType() {
return typePattern;
}

public ExpressionRuleType getExpressionRuleType() {
return expressionRuleType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,28 @@

/** ExpressionPattern */
public class ExpressionPatternMatcher<E extends Expression> {

public final ExpressionRuleType expressionRuleType;
public final Class<E> typePattern;
public final List<Predicate<ExpressionMatchingContext<E>>> predicates;
public final ExpressionMatchingAction<E> matchingAction;

public ExpressionPatternMatcher(Class<E> typePattern,
List<Predicate<ExpressionMatchingContext<E>>> predicates,
ExpressionMatchingAction<E> matchingAction) {
this(ExpressionRuleType.ANONYMOUS, typePattern, predicates, matchingAction);
}

public ExpressionPatternMatcher(ExpressionRuleType expressionRuleType, Class<E> typePattern,
List<Predicate<ExpressionMatchingContext<E>>> predicates,
ExpressionMatchingAction<E> matchingAction) {
this.expressionRuleType = Objects.requireNonNull(expressionRuleType, "expressionRuleType can not be null");
this.typePattern = Objects.requireNonNull(typePattern, "typePattern can not be null");
this.predicates = predicates == null ? ImmutableList.of() : predicates;
this.matchingAction = Objects.requireNonNull(matchingAction, "matchingAction can not be null");
}

public ExpressionPatternMatcher<E> toRule(ExpressionRuleType ruleType) {
return new ExpressionPatternMatcher<E>(ruleType, typePattern, predicates, matchingAction);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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
//
// http://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 org.apache.doris.nereids.rules.expression;

/**
* Type of rewrite expression rules.
*/
public enum ExpressionRuleType {
SIMPLIFY_COMPARISON_PREDICATE,
ANONYMOUS;

public int type() {
return ordinal();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.doris.nereids.rules.expression.ExpressionPatternMatcher;
import org.apache.doris.nereids.rules.expression.ExpressionPatternRuleFactory;
import org.apache.doris.nereids.rules.expression.ExpressionRewriteContext;
import org.apache.doris.nereids.rules.expression.ExpressionRuleType;
import org.apache.doris.nereids.trees.expressions.And;
import org.apache.doris.nereids.trees.expressions.Cast;
import org.apache.doris.nereids.trees.expressions.ComparisonPredicate;
Expand Down Expand Up @@ -81,6 +82,7 @@ enum AdjustType {
public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
return ImmutableList.of(
matchesType(ComparisonPredicate.class).then(SimplifyComparisonPredicate::simplify)
.toRule(ExpressionRuleType.SIMPLIFY_COMPARISON_PREDICATE)
);
}

Expand Down
26 changes: 26 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.doris.nereids.metrics.EventSwitchParser;
import org.apache.doris.nereids.parser.Dialect;
import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.rules.expression.ExpressionRuleType;
import org.apache.doris.nereids.trees.plans.commands.insert.InsertIntoTableCommand;
import org.apache.doris.nereids.trees.plans.logical.LogicalFileSink;
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
Expand Down Expand Up @@ -329,6 +330,7 @@ public class SessionVariable implements Serializable, Writable {
public static final String ENABLE_NEREIDS_DISTRIBUTE_PLANNER = "enable_nereids_distribute_planner";
public static final String DISABLE_NEREIDS_RULES = "disable_nereids_rules";
public static final String ENABLE_NEREIDS_RULES = "enable_nereids_rules";
public static final String DISABLE_NEREIDS_EXPRESSION_RULES = "disable_nereids_expression_rules";
public static final String ENABLE_NEW_COST_MODEL = "enable_new_cost_model";
public static final String ENABLE_FALLBACK_TO_ORIGINAL_PLANNER = "enable_fallback_to_original_planner";
public static final String ENABLE_NEREIDS_TIMEOUT = "enable_nereids_timeout";
Expand Down Expand Up @@ -1406,6 +1408,12 @@ public void setEnableLeftZigZag(boolean enableLeftZigZag) {
@VariableMgr.VarAttr(name = ENABLE_NEREIDS_RULES, needForward = true)
public String enableNereidsRules = "";

@VariableMgr.VarAttr(name = DISABLE_NEREIDS_EXPRESSION_RULES, needForward = true,
setter = "setDisableNereidsExpressionRules")
private String disableNereidsExpressionRules = "";

private BitSet disableNereidsExpressionRuleSet = new BitSet();

@VariableMgr.VarAttr(name = ENABLE_NEW_COST_MODEL, needForward = true)
private boolean enableNewCostModel = false;

Expand Down Expand Up @@ -3581,6 +3589,10 @@ public Set<Integer> getEnableNereidsRules() {
.collect(ImmutableSet.toImmutableSet());
}

public BitSet getDisableNereidsExpressionRules() {
return disableNereidsExpressionRuleSet;
}

public void setEnableNewCostModel(boolean enable) {
this.enableNewCostModel = enable;
}
Expand All @@ -3593,6 +3605,20 @@ public void setDisableNereidsRules(String disableNereidsRules) {
this.disableNereidsRules = disableNereidsRules;
}

public void setDisableNereidsExpressionRules(String disableNereidsExpressionRules) {
BitSet bitSet = new BitSet();
for (String ruleName : disableNereidsExpressionRules.split(",")) {
ruleName = ruleName.trim().toUpperCase(Locale.ROOT);
if (ruleName.isEmpty()) {
continue;
}
ExpressionRuleType ruleType = ExpressionRuleType.valueOf(ruleName);
bitSet.set(ruleType.type());
}
this.disableNereidsExpressionRuleSet = bitSet;
this.disableNereidsExpressionRules = disableNereidsExpressionRules;
}

public double getNereidsCboPenaltyFactor() {
return nereidsCboPenaltyFactor;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !shape_1 --
PhysicalResultSink
--PhysicalEmptyRelation

-- !shape_2 --
PhysicalResultSink
--PhysicalDistribute[DistributionSpecGather]
----filter((cast(a as DECIMALV3(11, 1)) = 1.1))
------PhysicalOlapScan[test_disable_nereids_expression_rule_tbl]

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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
//
// http://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.

suite('test_disable_nereids_expression_rule') {
def tbl = 'test_disable_nereids_expression_rule_tbl'
sql "DROP TABLE IF EXISTS ${tbl}"
sql "CREATE TABLE ${tbl}(a INT) PROPERTIES('replication_num' = '1')"
sql "INSERT INTO ${tbl} VALUES(10)"
qt_shape_1 "EXPLAIN SHAPE PLAN SELECT * FROM ${tbl} WHERE a = 1.1"
sql "SET disable_nereids_expression_rules='SIMPLIFY_COMPARISON_PREDICATE'"
qt_shape_2 "EXPLAIN SHAPE PLAN SELECT * FROM ${tbl} WHERE a = 1.1"
sql "DROP TABLE IF EXISTS ${tbl}"
}

0 comments on commit 7ed708d

Please sign in to comment.