Tune algorithm that deduces the return type of AND expression
Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/2cef8594 Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/2cef8594 Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/2cef8594 Branch: refs/heads/master Commit: 2cef85941359c530e7f0ed40265ce3c6f9db6bf8 Parents: 4b519b9 Author: Julian Hyde <[email protected]> Authored: Sat Jul 11 08:18:13 2015 -0700 Committer: Julian Hyde <[email protected]> Committed: Sun Jan 10 00:51:25 2016 -0800 ---------------------------------------------------------------------- .../calcite/sql/fun/SqlStdOperatorTable.java | 6 ++--- .../apache/calcite/sql/type/ReturnTypes.java | 23 +++++++++++++++++--- 2 files changed, 23 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/2cef8594/core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java b/core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java index 6aa6306..464945d 100644 --- a/core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java +++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlStdOperatorTable.java @@ -138,7 +138,7 @@ public class SqlStdOperatorTable extends ReflectiveSqlOperatorTable { SqlKind.AND, 28, true, - ReturnTypes.BOOLEAN_NULLABLE, + ReturnTypes.ARG0_NULLABLE, // more efficient than BOOLEAN_NULLABLE InferTypes.BOOLEAN, OperandTypes.BOOLEAN_BOOLEAN); @@ -427,7 +427,7 @@ public class SqlStdOperatorTable extends ReflectiveSqlOperatorTable { SqlKind.OR, 26, true, - ReturnTypes.BOOLEAN_NULLABLE, + ReturnTypes.ARG0_NULLABLE, // more efficient than BOOLEAN_NULLABLE InferTypes.BOOLEAN, OperandTypes.BOOLEAN_BOOLEAN); @@ -628,7 +628,7 @@ public class SqlStdOperatorTable extends ReflectiveSqlOperatorTable { "NOT", SqlKind.NOT, 30, - ReturnTypes.BOOLEAN_NULLABLE, + ReturnTypes.ARG0, InferTypes.BOOLEAN, OperandTypes.BOOLEAN); http://git-wip-us.apache.org/repos/asf/calcite/blob/2cef8594/core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java b/core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java index fa99579..a81caf3 100644 --- a/core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java +++ b/core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java @@ -95,16 +95,33 @@ public abstract class ReturnTypes { * returned type will also be nullable. First Arg must be of string type. */ public static final SqlReturnTypeInference ARG0_NULLABLE_VARYING = - cascade( - ARG0, SqlTypeTransforms.TO_NULLABLE, + cascade(ARG0, SqlTypeTransforms.TO_NULLABLE, SqlTypeTransforms.TO_VARYING); + /** * Type-inference strategy whereby the result type of a call is the type of * the operand #0 (0-based). If any of the other operands are nullable the * returned type will also be nullable. */ public static final SqlReturnTypeInference ARG0_NULLABLE = - cascade(ARG0, SqlTypeTransforms.TO_NULLABLE); + new SqlReturnTypeInference() { + // Equivalent to + // cascade(ARG0, SqlTypeTransforms.TO_NULLABLE); + // but implemented by hand because used in AND, which is a very common + // operator. + public RelDataType inferReturnType(SqlOperatorBinding opBinding) { + final int n = opBinding.getOperandCount(); + RelDataType type1 = null; + for (int i = 0; i < n; i++) { + type1 = opBinding.getOperandType(i); + if (type1.isNullable()) { + break; + } + } + return type1; + } + }; + /** * Type-inference strategy whereby the result type of a call is the type of * the operand #0 (0-based), with nulls always allowed.
