Skip to content

Commit

Permalink
opt small query2
Browse files Browse the repository at this point in the history
  • Loading branch information
924060929 committed Nov 9, 2024
1 parent 5a42e5f commit 7f7fd00
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,17 @@ public Expression visitConnectionId(ConnectionId connectionId, ExpressionRewrite
public Expression visitAnd(And and, ExpressionRewriteContext context) {
List<Expression> 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);
}
}

Expand All @@ -399,15 +401,15 @@ 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) {
// null and true
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);
Expand All @@ -418,15 +420,17 @@ public Expression visitAnd(And and, ExpressionRewriteContext context) {
public Expression visitOr(Or or, ExpressionRewriteContext context) {
List<Expression> 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);
}
}

Expand All @@ -440,15 +444,15 @@ 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) {
// null or false
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -92,7 +92,7 @@ public List<ExpressionPatternMatcher<? extends Expression>> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Integer> kv : exprFeature.slotCount.entrySet()) {
Integer slotId = kv.getKey();
Expand All @@ -25,24 +25,31 @@ default boolean isSimpleExpr(Expression expression) {
return true;
}

default ExprFeature computeExprFeature(Expression expr) {
public static ExprFeature computeExprFeature(Expression expr) {
Map<Integer, Integer> slotCount = Maps.newHashMap();
Map<Integer, Integer> 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<Integer, Integer> slotCount, Map<Integer, Integer> 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<Integer, Integer> slotCount;
private Map<Integer, Integer> slotIsNullCount;

Expand Down

0 comments on commit 7f7fd00

Please sign in to comment.