From 85f38af25b4c2931dc09149f95d0466cc8859904 Mon Sep 17 00:00:00 2001
From: xuzifu666 <1206332514@qq.com>
Date: Sat, 21 Nov 2026 12:03:18 +0800
Subject: [PATCH] [CALCITE-6787] Simplification process fails to remove the 'is
not null' predicate
---
.../org/apache/calcite/rex/RexSimplify.java | 6 ++-
.../java/org/apache/calcite/rex/RexUtil.java | 51 ++++++++++++++++---
.../apache/calcite/rex/RexProgramTest.java | 37 ++++++++++++++
3 files changed, 86 insertions(+), 8 deletions(-)
diff --git a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
index c8e62cb74530..8af3957b7431 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexSimplify.java
@@ -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
diff --git a/core/src/main/java/org/apache/calcite/rex/RexUtil.java b/core/src/main/java/org/apache/calcite/rex/RexUtil.java
index b340e1ba7870..ef28281997d1 100644
--- a/core/src/main/java/org/apache/calcite/rex/RexUtil.java
+++ b/core/src/main/java/org/apache/calcite/rex/RexUtil.java
@@ -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}.
+ *
+ *
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
@@ -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 > RexNode sargRef(SqlParserPos pos, RexBuilder rexBuilder,
@@ -639,7 +669,12 @@ public static > RexNode sargRef(SqlParserPos pos, RexBui
}
final List 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()) {
@@ -665,7 +700,9 @@ public static > 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),
@@ -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) {
@@ -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:
diff --git a/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java b/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
index fe1fd1c241e3..698656fd0303 100644
--- a/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
+++ b/core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
@@ -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;
@@ -4567,6 +4568,42 @@ private void checkSarg(String message, Sarg sarg,
assertThat(values, hasSize(2));
}
+ /** Test case for
+ * [CALCITE-6787]
+ * Simplification process fails to remove the 'is not null' predicate. */
+ @Test void testExpandSearchUnknownAs() {
+ final RexNode ref = input(tInt(true), 0);
+ final Sarg 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 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));