From 7f7fd008095789eecc7701e659f82b742f9341d9 Mon Sep 17 00:00:00 2001 From: 924060929 Date: Sun, 10 Nov 2024 02:24:29 +0800 Subject: [PATCH] opt small query2 --- .../rules/ExtractCommonFactorRule.java | 4 +- .../rules/FoldConstantRuleOnFE.java | 36 ++++++++++-------- .../rules/expression/rules/SimplifyRange.java | 4 +- .../rules/rewrite/SkipSimpleExprs.java | 37 +++++++++++-------- 4 files changed, 46 insertions(+), 35 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/ExtractCommonFactorRule.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/ExtractCommonFactorRule.java index 58ab1ecec09724..44b111fdd13081 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/ExtractCommonFactorRule.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/ExtractCommonFactorRule.java @@ -49,7 +49,7 @@ * transform (a and b) or (a and c) to a and (b or c) */ @Developing -public class ExtractCommonFactorRule implements ExpressionPatternRuleFactory, SkipSimpleExprs { +public class ExtractCommonFactorRule implements ExpressionPatternRuleFactory { public static final ExtractCommonFactorRule INSTANCE = new ExtractCommonFactorRule(); @Override @@ -64,7 +64,7 @@ private Expression extractCommonFactor(CompoundPredicate originExpr) { if (!(originExpr.left() instanceof CompoundPredicate || originExpr.left() instanceof BooleanLiteral) && !(originExpr.right() instanceof CompoundPredicate || originExpr.right() instanceof BooleanLiteral)) { return originExpr; - } else if (isSimpleExpr(originExpr)) { + } else if (SkipSimpleExprs.isSimpleExpr(originExpr)) { return originExpr; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java index 1e01e544a2510d..f68700da46c499 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java @@ -377,15 +377,17 @@ public Expression visitConnectionId(ConnectionId connectionId, ExpressionRewrite public Expression visitAnd(And and, ExpressionRewriteContext context) { List nonTrueLiteral = Lists.newArrayList(); int nullCount = 0; + boolean changed = false; for (Expression e : and.children()) { - e = deepRewrite ? e.accept(this, context) : e; - if (BooleanLiteral.FALSE.equals(e)) { + Expression newExpr = deepRewrite ? e.accept(this, context) : e; + if (BooleanLiteral.FALSE.equals(newExpr)) { return BooleanLiteral.FALSE; - } else if (e instanceof NullLiteral) { + } else if (newExpr instanceof NullLiteral) { nullCount++; - nonTrueLiteral.add(e); - } else if (!BooleanLiteral.TRUE.equals(e)) { - nonTrueLiteral.add(e); + nonTrueLiteral.add(newExpr); + } else if (!BooleanLiteral.TRUE.equals(newExpr)) { + changed |= !e.equals(newExpr); + nonTrueLiteral.add(newExpr); } } @@ -399,7 +401,7 @@ public Expression visitAnd(And and, ExpressionRewriteContext context) { return nonTrueLiteral.get(0); default: // x and y - return and.withChildren(nonTrueLiteral); + return changed ? and.withChildren(nonTrueLiteral) : and; } } else if (nullCount == 1) { if (nonTrueLiteral.size() == 1) { @@ -407,7 +409,7 @@ public Expression visitAnd(And and, ExpressionRewriteContext context) { return new NullLiteral(BooleanType.INSTANCE); } // null and x - return and.withChildren(nonTrueLiteral); + return changed ? and.withChildren(nonTrueLiteral) : and; } else { // null and null return new NullLiteral(BooleanType.INSTANCE); @@ -418,15 +420,17 @@ public Expression visitAnd(And and, ExpressionRewriteContext context) { public Expression visitOr(Or or, ExpressionRewriteContext context) { List nonFalseLiteral = Lists.newArrayList(); int nullCount = 0; + boolean changed = false; for (Expression e : or.children()) { - e = deepRewrite ? e.accept(this, context) : e; - if (BooleanLiteral.TRUE.equals(e)) { + Expression newExpr = deepRewrite ? e.accept(this, context) : e; + if (BooleanLiteral.TRUE.equals(newExpr)) { return BooleanLiteral.TRUE; - } else if (e instanceof NullLiteral) { + } else if (newExpr instanceof NullLiteral) { nullCount++; - nonFalseLiteral.add(e); - } else if (!BooleanLiteral.FALSE.equals(e)) { - nonFalseLiteral.add(e); + nonFalseLiteral.add(newExpr); + } else if (!BooleanLiteral.FALSE.equals(newExpr)) { + changed |= !newExpr.equals(newExpr); + nonFalseLiteral.add(newExpr); } } @@ -440,7 +444,7 @@ public Expression visitOr(Or or, ExpressionRewriteContext context) { return nonFalseLiteral.get(0); default: // x or y - return or.withChildren(nonFalseLiteral); + return changed ? or.withChildren(nonFalseLiteral) : or; } } else if (nullCount == 1) { if (nonFalseLiteral.size() == 1) { @@ -448,7 +452,7 @@ public Expression visitOr(Or or, ExpressionRewriteContext context) { return new NullLiteral(BooleanType.INSTANCE); } // null or x - return or.withChildren(nonFalseLiteral); + return changed ? or.withChildren(nonFalseLiteral) : or; } else { // null or null return new NullLiteral(BooleanType.INSTANCE); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyRange.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyRange.java index 0c5dde0987b0a3..d20f6439c2e113 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyRange.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyRange.java @@ -79,7 +79,7 @@ * 2. for `Or` expression (similar to `And`). * todo: support a > 10 and (a < 10 or a > 20 ) => a > 20 */ -public class SimplifyRange implements ExpressionPatternRuleFactory, SkipSimpleExprs { +public class SimplifyRange implements ExpressionPatternRuleFactory { public static final SimplifyRange INSTANCE = new SimplifyRange(); @Override @@ -92,7 +92,7 @@ public List> buildRules() { /** rewrite */ public Expression rewrite(CompoundPredicate expr, ExpressionRewriteContext context) { - if (isSimpleExpr(expr)) { + if (SkipSimpleExprs.isSimpleExpr(expr)) { return expr; } ValueDesc valueDesc = expr.accept(new RangeInference(), context); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/SkipSimpleExprs.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/SkipSimpleExprs.java index 405133c16bfae9..ddd9663f7f87f0 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/SkipSimpleExprs.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/SkipSimpleExprs.java @@ -9,8 +9,8 @@ import java.util.Map; import java.util.Map.Entry; -public interface SkipSimpleExprs { - default boolean isSimpleExpr(Expression expression) { +public class SkipSimpleExprs { + public static boolean isSimpleExpr(Expression expression) { ExprFeature exprFeature = computeExprFeature(expression); for (Entry kv : exprFeature.slotCount.entrySet()) { Integer slotId = kv.getKey(); @@ -25,24 +25,31 @@ default boolean isSimpleExpr(Expression expression) { return true; } - default ExprFeature computeExprFeature(Expression expr) { + public static ExprFeature computeExprFeature(Expression expr) { Map slotCount = Maps.newHashMap(); Map slotIsNullCount = Maps.newHashMap(); - expr.foreach(e -> { - if (e instanceof Slot) { - int slotId = ((Slot) e).getExprId().asInt(); - Integer count = slotCount.get(slotId); - slotCount.put(slotId, count == null ? 1 : count + 1); - } else if (e instanceof IsNull && e.child(0) instanceof Slot) { - int slotId = ((Slot) e.child(0)).getExprId().asInt(); - Integer count = slotIsNullCount.get(slotId); - slotIsNullCount.put(slotId, count == null ? 1 : count + 1); - } - }); + computeExprFeature(expr, slotCount, slotIsNullCount); return new ExprFeature(slotCount, slotIsNullCount); } - class ExprFeature { + private static void computeExprFeature( + Expression e, Map slotCount, Map slotIsNullCount) { + if (e instanceof Slot) { + int slotId = ((Slot) e).getExprId().asInt(); + Integer count = slotCount.get(slotId); + slotCount.put(slotId, count == null ? 1 : count + 1); + } else if (e instanceof IsNull && e.child(0) instanceof Slot) { + int slotId = ((Slot) e.child(0)).getExprId().asInt(); + Integer count = slotIsNullCount.get(slotId); + slotIsNullCount.put(slotId, count == null ? 1 : count + 1); + } else { + for (Expression child : e.children()) { + computeExprFeature(child, slotCount, slotIsNullCount); + } + } + } + + private static class ExprFeature { private Map slotCount; private Map slotIsNullCount;