diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java index edbd2dd6226..bf061923bfa 100644 --- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java +++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java @@ -2794,9 +2794,9 @@ public static double log(BigDecimal d0, BigDecimal d1) { } /** SQL {@code LOG2(number)} function applied to - * BigDecimal and double values. */ + * BigDecimal values. */ public static @Nullable Double log2(BigDecimal d0) { - return (d0.doubleValue() < 0.0 || d0.doubleValue() == 0.0) ? null : log(d0, 2); + return log2(d0.doubleValue()); } // MOD diff --git a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java index d9a4b5cf132..97b3e52d148 100644 --- a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java +++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java @@ -2157,7 +2157,7 @@ private static RelDataType deriveTypeMapFromEntries(SqlOperatorBinding opBinding @LibraryOperator(libraries = {MYSQL, SPARK}) public static final SqlFunction LOG2 = SqlBasicFunction.create("LOG2", - ReturnTypes.DOUBLE_NULLABLE, + ReturnTypes.DOUBLE_FORCE_NULLABLE, OperandTypes.NUMERIC, SqlFunctionCategory.NUMERIC); 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 f8696893b04..df21ab9754d 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 @@ -396,6 +396,13 @@ public static SqlCall stripSeparator(SqlCall call) { public static final SqlReturnTypeInference DOUBLE_NULLABLE = DOUBLE.andThen(SqlTypeTransforms.TO_NULLABLE); + /** + * Type-inference strategy whereby the result type of a call is a nullable + * Double. + */ + public static final SqlReturnTypeInference DOUBLE_FORCE_NULLABLE = + DOUBLE.andThen(SqlTypeTransforms.FORCE_NULLABLE); + /** * Type-inference strategy whereby the result type of a call is a Char. */