Skip to content
Open
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
6 changes: 4 additions & 2 deletions core/src/main/java/org/apache/calcite/rex/RexSimplify.java
Original file line number Diff line number Diff line change
Expand Up @@ -2668,11 +2668,13 @@ private RexNode simplifySearch(RexCall call, RexUnknownAs unknownAs) {
} else if (searchOperand instanceof RexLiteral) {
// Subject is a constant; expand and re-simplify regardless of point count,
// since a constant subject can always be fully evaluated.
return simplify(RexUtil.expandSearch(rexBuilder, null, call), unknownAs);
return simplify(
RexUtil.expandSearch(rexBuilder, null, call, -1, unknownAs),
unknownAs);
} else if (sarg.isPoints() && sarg.pointCount <= 1) {
// Expand "SEARCH(x, Sarg([point]))" to "x = point"
// and "SEARCH(x, Sarg([]))" to "false".
return RexUtil.expandSearch(rexBuilder, null, call);
return RexUtil.expandSearch(rexBuilder, null, call, -1, unknownAs);
}
}
return operandUnchanged
Expand Down
51 changes: 45 additions & 6 deletions core/src/main/java/org/apache/calcite/rex/RexUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,24 @@ public static RexNode expandSearch(RexBuilder rexBuilder,
* whose complexity is greater than {@code maxComplexity} in an expression. */
public static RexNode expandSearch(RexBuilder rexBuilder,
@Nullable RexProgram program, RexNode node, int maxComplexity) {
return node.accept(searchShuttle(rexBuilder, program, maxComplexity));
return expandSearch(rexBuilder, program, node, maxComplexity,
RexUnknownAs.UNKNOWN);
}

/** Expands calls to {@link SqlStdOperatorTable#SEARCH}
* whose complexity is greater than {@code maxComplexity} in an expression,
* treating UNKNOWN values as {@code unknownAs}.
*
* <p>When {@code unknownAs} is {@link RexUnknownAs#FALSE} (for example, in
* a {@code FILTER} condition) the generated expression does not contain
* redundant {@code IS NOT NULL} predicates, because comparisons against
* NULL evaluate to UNKNOWN, which is already treated as FALSE. */
public static RexNode expandSearch(RexBuilder rexBuilder,
@Nullable RexProgram program, RexNode node, int maxComplexity,
RexUnknownAs unknownAs) {
return node.accept(
searchShuttle(rexBuilder, program, maxComplexity,
unknownAs));
}

/** Creates a shuttle that expands calls to
Expand All @@ -629,7 +646,20 @@ public static RexNode expandSearch(RexBuilder rexBuilder,
* such as {@code x IN (3, 5, 7) OR x IS NULL} as a Sarg. */
public static RexShuttle searchShuttle(RexBuilder rexBuilder,
@Nullable RexProgram program, int maxComplexity) {
return new SearchExpandingShuttle(program, rexBuilder, maxComplexity);
return searchShuttle(rexBuilder, program, maxComplexity,
RexUnknownAs.UNKNOWN);
}

/** Creates a shuttle that expands calls to
* {@link SqlStdOperatorTable#SEARCH}, treating UNKNOWN values as
* {@code unknownAs}.
*
* @see #expandSearch(RexBuilder, RexProgram, RexNode, int, RexUnknownAs) */
public static RexShuttle searchShuttle(RexBuilder rexBuilder,
@Nullable RexProgram program, int maxComplexity,
RexUnknownAs unknownAs) {
return new SearchExpandingShuttle(program, rexBuilder, maxComplexity,
unknownAs);
}

public static <C extends Comparable<C>> RexNode sargRef(SqlParserPos pos, RexBuilder rexBuilder,
Expand All @@ -639,7 +669,12 @@ public static <C extends Comparable<C>> RexNode sargRef(SqlParserPos pos, RexBui
}
final List<RexNode> orList = new ArrayList<>();
if (sarg.nullAs == RexUnknownAs.TRUE
&& unknownAs == RexUnknownAs.UNKNOWN) {
&& (unknownAs != RexUnknownAs.TRUE
|| sarg.rangeSet.isEmpty())) {
// If unknownAs is TRUE, the comparisons yield UNKNOWN for a NULL ref,
// which is treated as TRUE; 'IS NULL' would be redundant. The 'IS NULL'
// disjunct is still required if the range set is empty, for it is the
// only way to match NULL.
orList.add(rexBuilder.makeCall(pos, SqlStdOperatorTable.IS_NULL, ref));
}
if (sarg.isPoints()) {
Expand All @@ -665,7 +700,9 @@ public static <C extends Comparable<C>> RexNode sargRef(SqlParserPos pos, RexBui
}
RexNode node = composeDisjunction(rexBuilder, orList);
if (sarg.nullAs == RexUnknownAs.FALSE
&& unknownAs == RexUnknownAs.UNKNOWN) {
&& unknownAs != RexUnknownAs.FALSE) {
// If unknownAs is FALSE, the comparison yields UNKNOWN for a NULL ref,
// which is treated as FALSE; 'IS NOT NULL' would be redundant.
node =
rexBuilder.makeCall(pos, SqlStdOperatorTable.AND,
rexBuilder.makeCall(pos, SqlStdOperatorTable.IS_NOT_NULL, ref),
Expand Down Expand Up @@ -3810,12 +3847,14 @@ private static class SearchExpandingShuttle extends RexShuttle {
private final RexBuilder rexBuilder;
private final @Nullable RexProgram program;
private final int maxComplexity;
private final RexUnknownAs unknownAs;

SearchExpandingShuttle(@Nullable RexProgram program, RexBuilder rexBuilder,
int maxComplexity) {
int maxComplexity, RexUnknownAs unknownAs) {
this.program = program;
this.rexBuilder = rexBuilder;
this.maxComplexity = maxComplexity;
this.unknownAs = requireNonNull(unknownAs, "unknownAs");
}

@Override public RexNode visitCall(RexCall call) {
Expand Down Expand Up @@ -3844,7 +3883,7 @@ private static class SearchExpandingShuttle extends RexShuttle {
final Sarg sarg = requireNonNull(literal.getValueAs(Sarg.class), "Sarg");
if (maxComplexity < 0 || sarg.complexity() < maxComplexity) {
return sargRef(call.pos, rexBuilder, ref, sarg, literal.getType(),
RexUnknownAs.UNKNOWN);
unknownAs);
}
// Sarg is complex (therefore useful); fall through
default:
Expand Down
37 changes: 37 additions & 0 deletions core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import java.util.TreeSet;
import java.util.function.Supplier;

import static org.apache.calcite.test.Matchers.hasRex;
import static org.apache.calcite.test.Matchers.isRangeSet;

import static org.hamcrest.CoreMatchers.equalTo;
Expand Down Expand Up @@ -4567,6 +4568,42 @@ private void checkSarg(String message, Sarg sarg,
assertThat(values, hasSize(2));
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6787">[CALCITE-6787]
* Simplification process fails to remove the 'is not null' predicate</a>. */
@Test void testExpandSearchUnknownAs() {
final RexNode ref = input(tInt(true), 0);
final Sarg<BigDecimal> nullAsFalseSarg =
Sarg.of(
RexUnknownAs.FALSE, ImmutableRangeSet.of(
Range.closed(BigDecimal.ZERO, new BigDecimal(100))));
final RexNode nullAsFalseSearch =
rexBuilder.makeCall(SqlStdOperatorTable.SEARCH, ref,
rexBuilder.makeSearchArgumentLiteral(nullAsFalseSarg, ref.getType()));
// Treating UNKNOWN as UNKNOWN requires the 'IS NOT NULL' guard
assertThat(RexUtil.expandSearch(rexBuilder, null, nullAsFalseSearch),
hasRex("AND(IS NOT NULL($0), AND(>=($0, 0), <=($0, 100)))"));
// Treating UNKNOWN as FALSE makes the 'IS NOT NULL' guard redundant
assertThat(
RexUtil.expandSearch(rexBuilder, null, nullAsFalseSearch, -1,
RexUnknownAs.FALSE),
hasRex("AND(>=($0, 0), <=($0, 100))"));

final Sarg<BigDecimal> nullAsTrueSarg =
Sarg.of(RexUnknownAs.TRUE, ImmutableRangeSet.of(Range.greaterThan(BigDecimal.ZERO)));
final RexNode nullAsTrueSearch =
rexBuilder.makeCall(SqlStdOperatorTable.SEARCH, ref,
rexBuilder.makeSearchArgumentLiteral(nullAsTrueSarg, ref.getType()));
// Treating UNKNOWN as UNKNOWN requires the 'IS NULL' disjunct
assertThat(RexUtil.expandSearch(rexBuilder, null, nullAsTrueSearch),
hasRex("OR(IS NULL($0), >($0, 0))"));
// Treating UNKNOWN as TRUE makes the 'IS NULL' disjunct redundant
assertThat(
RexUtil.expandSearch(rexBuilder, null, nullAsTrueSearch, -1,
RexUnknownAs.TRUE),
hasRex(">($0, 0)"));
}

@Test void testInterpreter() {
assertThat(eval(trueLiteral), is(true));
assertThat(eval(nullInt), is(NullSentinel.INSTANCE));
Expand Down
Loading