liyafan82 commented on a change in pull request #2233:
URL: https://github.com/apache/calcite/pull/2233#discussion_r514060146



##########
File path: core/src/main/java/org/apache/calcite/sql/fun/SqlSearchOperator.java
##########
@@ -16,18 +16,42 @@
  */
 package org.apache.calcite.sql.fun;
 
+import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.sql.SqlInternalOperator;
 import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlOperatorBinding;
 import org.apache.calcite.sql.type.InferTypes;
 import org.apache.calcite.sql.type.OperandTypes;
 import org.apache.calcite.sql.type.ReturnTypes;
+import org.apache.calcite.util.Sarg;
 
 /** Operator that tests whether its left operand is included in the range of
  * values covered by search arguments. */
 class SqlSearchOperator extends SqlInternalOperator {
   SqlSearchOperator() {
-    super("SEARCH", SqlKind.SEARCH, 30, true, ReturnTypes.BOOLEAN_NULLABLE,
+    super("SEARCH", SqlKind.SEARCH, 30, true,
+        ReturnTypes.BOOLEAN.andThen(SqlSearchOperator::makeNullable),
         InferTypes.FIRST_KNOWN,
         OperandTypes.COMPARABLE_UNORDERED_COMPARABLE_UNORDERED);
   }
+
+  /** Sets whether a call to SEARCH should allow nulls.
+   *
+   * <p>For example, if the type of {@code x} is NOT NULL, then
+   * {@code SEARCH(x, Sarg[10])} will never return UNKNOWN.
+   * It is evident from the expansion, "x = 10", but holds for all Sarg
+   * values.
+   *
+   * <p>If {@link Sarg#containsNull} is true, SEARCH will never return
+   * UNKNOWN. For example, {@code SEARCH(x, Sarg[10 OR NULL])} expands to
+   * {@code x = 10 OR x IS NOT NULL}, which returns {@code TRUE} if
+   * {@code x} is NULL, {@code TRUE} if {@code x} is 10, and {@code FALSE}
+   * for all other values.
+   */
+  private static RelDataType makeNullable(SqlOperatorBinding binding,
+      RelDataType type) {
+    final boolean nullable = binding.getOperandType(0).isNullable()
+        && !binding.getOperandLiteralValue(1, Sarg.class).containsNull;

Review comment:
       The condition should be `binding.getOperandLiteralValue(1, 
Sarg.class).containsNull`?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to