Skip to content

Commit

Permalink
[CALCITE-6325] Add LOG function (enabled in Mysql library)
Browse files Browse the repository at this point in the history
  • Loading branch information
caicancai committed Mar 19, 2024
1 parent 4b946b9 commit adcbe2e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 8 additions & 6 deletions core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion site/_docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6340,7 +6340,7 @@ void checkRegexpExtract(SqlOperatorFixture f0, FunctionAlias functionAlias) {

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6325">[CALCITE-6325]
* Add LOG function (enabled in MYSQL, Spark library)</a>. */
* Add LOG function (enabled in MYSQL library)</a>. */
@Test void testLogMysqlSparkFunc() {
final SqlOperatorFixture f0 = fixture();
f0.checkFails("^log(100, 10)^",
Expand Down

0 comments on commit adcbe2e

Please sign in to comment.