From adcbe2e13808182fd1f61ec7a284038fc5bd4f00 Mon Sep 17 00:00:00 2001 From: caicancai <2356672992@qq.com> Date: Tue, 19 Mar 2024 13:26:31 +0800 Subject: [PATCH] [CALCITE-6325] Add LOG function (enabled in Mysql library) --- .../calcite/adapter/enumerable/RexImpTable.java | 15 +++++++++------ .../org/apache/calcite/runtime/SqlFunctions.java | 14 ++++++++------ .../calcite/sql/fun/SqlLibraryOperators.java | 2 +- site/_docs/reference.md | 2 +- .../org/apache/calcite/test/SqlOperatorTest.java | 2 +- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java index a08adf35169..6a047303792 100644 --- a/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java +++ b/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java @@ -644,12 +644,15 @@ Builder populate() { defineMethod(POWER, BuiltInMethod.POWER.method, NullPolicy.STRICT); defineMethod(ABS, BuiltInMethod.ABS.method, NullPolicy.STRICT); - map.put(LN, new LogImplementor(BuiltInMethod.LOG.method)); - map.put(LOG, new LogImplementor(BuiltInMethod.LOG.method)); - map.put(LOG10, new LogImplementor(BuiltInMethod.LOG.method)); - - map.put(LOG_MYSQL_SPARK, new LogImplementor(BuiltInMethod.LOG_MYSQL_SPARK.method)); - map.put(LOG2, new LogImplementor(BuiltInMethod.LOG_MYSQL_SPARK.method)); + LogImplementor logImplementor = new LogImplementor(BuiltInMethod.LOG.method); + map.put(LN, logImplementor); + map.put(LOG, logImplementor); + map.put(LOG10, logImplementor); + + LogImplementor logMysqlSparkImplementor = + new LogImplementor(BuiltInMethod.LOG_MYSQL_SPARK.method); + map.put(LOG_MYSQL_SPARK, logMysqlSparkImplementor); + map.put(LOG2, logMysqlSparkImplementor); defineReflective(RAND, BuiltInMethod.RAND.method, BuiltInMethod.RAND_SEED.method); 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 b28b67265c7..23b5c621503 100644 --- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java +++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java @@ -2788,24 +2788,26 @@ public static double log(BigDecimal d0, BigDecimal d1) { return Math.log(d0.doubleValue()) / Math.log(d1.doubleValue()); } - /** SQL {@code LOGMySqlSpark(number, number2)} function applied to double values. */ + /** SQL {@code LOG(number, number2)} function applied to double values. + * but return null when number2 is 0. */ public static @Nullable Double logMysqlSpark(double number, double number2) { return (number <= 0) ? null : log(number, number2); } - /** SQL {@code LOGMySqlSpark(number, number2)} function applied to - * double and BigDecimal values. */ + /** SQL {@code LOG(number, number2)} function applied to double and BigDecimal values. + * but return null when number2 is 0. */ public static @Nullable Double logMysqlSpark(double number, BigDecimal number2) { return logMysqlSpark(number, number2.doubleValue()); } - /** SQL {@code LOGMySqlSpark(number, number2)} function applied to - * BigDecimal and double values. */ + /** SQL {@code LOG(number, number2)} function applied to BigDecimal and double values. + * but return null when number2 is 0. */ public static @Nullable Double logMysqlSpark(BigDecimal number, double number2) { return logMysqlSpark(number.doubleValue(), number2); } - /** SQL {@code LOGMySqlSpark(number, number2)} function applied to double values. */ + /** SQL {@code LOG(number, number2)} function applied to double values. + * but return null when number2 is 0. */ public static @Nullable Double logMysqlSpark(BigDecimal number, BigDecimal number2) { return logMysqlSpark(number.doubleValue(), number2.doubleValue()); } 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 99413054466..5f2b4ddbcc4 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 @@ -2205,7 +2205,7 @@ private static RelDataType deriveTypeMapFromEntries(SqlOperatorBinding opBinding OperandTypes.NUMERIC_OPTIONAL_NUMERIC, SqlFunctionCategory.NUMERIC); - /** The "LOG(numeric, numeric1)" function. Returns the base numeric1 logarithm of numeric. */ + /** The "LOG(numeric1, numeric2)" function. Returns the base numeric1 logarithm of numeric. */ @LibraryOperator(libraries = {MYSQL, SPARK}) public static final SqlFunction LOG_MYSQL_SPARK = SqlBasicFunction.create("LOG", diff --git a/site/_docs/reference.md b/site/_docs/reference.md index 5d1e4610593..7ac69bfa551 100644 --- a/site/_docs/reference.md +++ b/site/_docs/reference.md @@ -2785,7 +2785,7 @@ In the following: | b f s | LENGTH(string) | Equivalent to `CHAR_LENGTH(string)` | h s | LEVENSHTEIN(string1, string2) | Returns the Levenshtein distance between *string1* and *string2* | b | LOG(numeric1 [, numeric2 ]) | Returns the logarithm of *numeric1* to base *numeric2*, or base e if *numeric2* is not present -| m s | LOG(numeric1 [, numeric2 ]) | Returns the logarithm of *numeric1* to base *numeric2*, or base e if *numeric2* is not present +| m s | LOG(numeric1 [, numeric2 ]) | Returns the logarithm of *numeric1* to base *numeric2*, or base e if *numeric2* is not present but *numeric2* is zero returns NULL | m s | LOG2(numeric) | Returns the base 2 logarithm of *numeric* | b o s | LPAD(string, length [, pattern ]) | Returns a string or bytes value that consists of *string* prepended to *length* with *pattern* | b | TO_BASE32(string) | Converts the *string* to base-32 encoded form and returns an encoded string diff --git a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java index 51d0c56a4a2..d72c03e4e2a 100644 --- a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java +++ b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java @@ -6340,7 +6340,7 @@ void checkRegexpExtract(SqlOperatorFixture f0, FunctionAlias functionAlias) { /** Test case for * [CALCITE-6325] - * Add LOG function (enabled in MYSQL, Spark library). */ + * Add LOG function (enabled in MYSQL library). */ @Test void testLogMysqlSparkFunc() { final SqlOperatorFixture f0 = fixture(); f0.checkFails("^log(100, 10)^",