Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ public RelOptPredicateList getPredicates(Union union, RelMetadataQuery mq) {
final RelOptPredicateList predicates = RelOptPredicateList.EMPTY;
final RexSimplify simplify =
new RexSimplify(rexBuilder, predicates, true, executor);
RexNode disjPred = simplify.simplifyOrs(finalResidualPreds);
RexNode disjPred = simplify.simplify(
rexBuilder.makeCall(SqlStdOperatorTable.OR, finalResidualPreds));
if (!disjPred.isAlwaysTrue()) {
preds.add(disjPred);
}
Expand Down
28 changes: 10 additions & 18 deletions core/src/main/java/org/apache/calcite/rex/RexSimplify.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

/**
* Context required to simplify a row-expression.
Expand Down Expand Up @@ -157,7 +156,8 @@ public RexNode simplifyPreservingType(RexNode e) {
* @param e Expression to simplify
*/
public RexNode simplify(RexNode e) {
return verify(e, simplifier -> simplifier.simplify_(e));
RexNode simplified = this.withParanoid(false).simplify_(e);
return verify(e, simplified);
}

private RexNode simplify_(RexNode e) {
Expand Down Expand Up @@ -276,7 +276,7 @@ private <C extends Comparable<C>> RexNode simplifyComparison(RexCall e,
/**
* Simplifies a conjunction of boolean expressions.
*/
public RexNode simplifyAnds(Iterable<? extends RexNode> nodes) {
private RexNode simplifyAnds(Iterable<? extends RexNode> nodes) {
final List<RexNode> terms = new ArrayList<>();
final List<RexNode> notTerms = new ArrayList<>();
for (RexNode e : nodes) {
Expand Down Expand Up @@ -666,8 +666,7 @@ private static List<Pair<RexNode, RexNode>> casePairs(RexBuilder rexBuilder,
return builder.build();
}

// public only to support a deprecated method; treat as private
public RexNode simplifyAnd(RexCall e) {
private RexNode simplifyAnd(RexCall e) {
final List<RexNode> terms = new ArrayList<>();
final List<RexNode> notTerms = new ArrayList<>();
RelOptUtil.decomposeConjunction(e, terms, notTerms);
Expand All @@ -686,8 +685,7 @@ public RexNode simplifyAnd(RexCall e) {
return simplifyAnd2(terms, notTerms);
}

// package-protected only to support a deprecated method; treat as private
RexNode simplifyAnd2(List<RexNode> terms, List<RexNode> notTerms) {
private RexNode simplifyAnd2(List<RexNode> terms, List<RexNode> notTerms) {
for (RexNode term : terms) {
if (term.isAlwaysFalse()) {
return rexBuilder.makeLiteral(false);
Expand Down Expand Up @@ -720,7 +718,7 @@ RexNode simplifyAnd2(List<RexNode> terms, List<RexNode> notTerms) {

/** As {@link #simplifyAnd2(List, List)} but we assume that if the expression
* returns UNKNOWN it will be interpreted as FALSE. */
RexNode simplifyAnd2ForUnknownAsFalse(List<RexNode> terms,
private RexNode simplifyAnd2ForUnknownAsFalse(List<RexNode> terms,
List<RexNode> notTerms) {
//noinspection unchecked
return simplifyAnd2ForUnknownAsFalse(terms, notTerms, Comparable.class);
Expand Down Expand Up @@ -1036,7 +1034,7 @@ private <C extends Comparable<C>> Range<C> residue(RexNode ref, Range<C> r0,
}

/** Simplifies OR(x, x) into x, and similar. */
public RexNode simplifyOr(RexCall call) {
private RexNode simplifyOr(RexCall call) {
assert call.getKind() == SqlKind.OR;
final List<RexNode> terms = RelOptUtil.disjunctions(call);
simplifyOrTerms(terms);
Expand All @@ -1045,11 +1043,7 @@ public RexNode simplifyOr(RexCall call) {

/** Simplifies a list of terms and combines them into an OR.
* Modifies the list in place. */
public RexNode simplifyOrs(List<RexNode> terms) {
if (paranoid) {
final RexNode before = RexUtil.composeDisjunction(rexBuilder, terms);
return verify(before, simplifier -> simplifier.simplifyOrs(terms));
}
private RexNode simplifyOrs(List<RexNode> terms) {
for (int i = 0; i < terms.size(); i++) {
final RexNode term = simplify_(terms.get(i));
switch (term.getKind()) {
Expand All @@ -1075,9 +1069,7 @@ public RexNode simplifyOrs(List<RexNode> terms) {
return RexUtil.composeDisjunction(rexBuilder, terms);
}

private RexNode verify(RexNode before,
Function<RexSimplify, RexNode> simplifier) {
final RexNode simplified = simplifier.apply(withParanoid(false));
private RexNode verify(RexNode before, RexNode simplified) {
if (!paranoid) {
return simplified;
}
Expand Down Expand Up @@ -1251,7 +1243,7 @@ private static boolean canRollUp(TimeUnit outer, TimeUnit inner) {
/** Removes any casts that change nullability but not type.
*
* <p>For example, {@code CAST(1 = 0 AS BOOLEAN)} becomes {@code 1 = 0}. */
public RexNode removeNullabilityCast(RexNode e) {
private RexNode removeNullabilityCast(RexNode e) {
return RexUtil.removeNullabilityCast(rexBuilder.getTypeFactory(), e);
}

Expand Down
25 changes: 15 additions & 10 deletions core/src/main/java/org/apache/calcite/rex/RexUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1757,15 +1757,15 @@ public static RexNode simplify(RexBuilder rexBuilder, RexNode e,
@Deprecated // to be removed before 2.0
public static RexNode simplifyAnds(RexBuilder rexBuilder,
Iterable<? extends RexNode> nodes) {
return new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, false,
EXECUTOR).simplifyAnds(nodes);
return simplifyAnds(rexBuilder, nodes, false);
}

@Deprecated // to be removed before 2.0
public static RexNode simplifyAnds(RexBuilder rexBuilder,
Iterable<? extends RexNode> nodes, boolean unknownAsFalse) {
return new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY,
unknownAsFalse, EXECUTOR).simplifyAnds(nodes);
unknownAsFalse, EXECUTOR).simplify(
RexUtil.composeConjunction(rexBuilder, nodes, false));
}

/** Negates a logical expression by adding or removing a NOT. */
Expand Down Expand Up @@ -1816,22 +1816,27 @@ static SqlOperator op(SqlKind kind) {
public static RexNode simplifyAnd(RexBuilder rexBuilder, RexCall e,
boolean unknownAsFalse) {
return new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY,
unknownAsFalse, EXECUTOR).simplifyAnd(e);
unknownAsFalse, EXECUTOR).simplify(e);
}

@Deprecated // to be removed before 2.0
public static RexNode simplifyAnd2(RexBuilder rexBuilder,
List<RexNode> terms, List<RexNode> notTerms) {
for (RexNode notDisjunction : notTerms) {
terms.add(rexBuilder.makeCall(SqlStdOperatorTable.NOT, notDisjunction));
}
return new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, false,
EXECUTOR).simplifyAnd2(terms, notTerms);
EXECUTOR).simplify(RexUtil.composeConjunction(rexBuilder, terms, false));
}

@Deprecated // to be removed before 2.0
public static RexNode simplifyAnd2ForUnknownAsFalse(RexBuilder rexBuilder,
List<RexNode> terms, List<RexNode> notTerms) {
final Class<Comparable> clazz = Comparable.class;
return new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, true,
EXECUTOR).simplifyAnd2ForUnknownAsFalse(terms, notTerms);
for (RexNode notDisjunction : notTerms) {
terms.add(rexBuilder.makeCall(SqlStdOperatorTable.NOT, notDisjunction));
}
return new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, false,
EXECUTOR).simplify(RexUtil.composeConjunction(rexBuilder, terms, false));
}

public static RexNode negate(RexBuilder rexBuilder, RexCall call) {
Expand Down Expand Up @@ -1865,14 +1870,14 @@ public static RexNode invert(RexBuilder rexBuilder, RexCall call) {
@Deprecated // to be removed before 2.0
public static RexNode simplifyOr(RexBuilder rexBuilder, RexCall call) {
return new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, false,
EXECUTOR).simplifyOr(call);
EXECUTOR).simplify(call);
}

@Deprecated // to be removed before 2.0
public static RexNode simplifyOrs(RexBuilder rexBuilder,
List<RexNode> terms) {
return new RexSimplify(rexBuilder, RelOptPredicateList.EMPTY, false,
EXECUTOR).simplifyOrs(terms);
EXECUTOR).simplify(rexBuilder.makeCall(SqlStdOperatorTable.OR, terms));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/apache/calcite/tools/RelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,8 @@ public RexNode and(RexNode... operands) {
* {@code e AND TRUE} becomes {@code e};
* {@code e AND e2 AND NOT e} becomes {@code e2}. */
public RexNode and(Iterable<? extends RexNode> operands) {
return simplifier.simplifyAnds(operands);
return simplifier.simplify(
RexUtil.composeConjunction(cluster.getRexBuilder(), operands, false));
}

/** Creates an OR. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4541,14 +4541,13 @@ LogicalProject(EXPR$0=[1])
<![CDATA[
LogicalProject(EXPR$0=[1])
LogicalJoin(condition=[=($0, $8)], joinType=[inner])
LogicalFilter(condition=[>($0, 7)])
LogicalUnion(all=[true])
LogicalProject(DEPTNO=[$7])
LogicalFilter(condition=[>($7, 7)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalProject(DEPTNO=[$7])
LogicalFilter(condition=[>($7, 10)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalUnion(all=[true])
LogicalProject(DEPTNO=[$7])
LogicalFilter(condition=[>($7, 7)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalProject(DEPTNO=[$7])
LogicalFilter(condition=[>($7, 10)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalFilter(condition=[>($7, 7)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
Expand Down