Skip to content

Commit

Permalink
[CALCITE-6325] Add LOG function (enabled in Mysql Style library)
Browse files Browse the repository at this point in the history
Co-authored-by: Tanner Clary <[email protected]>
  • Loading branch information
caicancai and tanclary committed Apr 1, 2024
1 parent 22a8767 commit 3a5f076
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
import static org.apache.calcite.sql.fun.SqlLibraryOperators.LOG2;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.LOGICAL_AND;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.LOGICAL_OR;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.LOG_MYSQL;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.LOG_MYSQL_STYLE;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.LPAD;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.MAP;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.MAP_CONCAT;
Expand Down Expand Up @@ -651,7 +651,7 @@ Builder populate() {

LogImplementor logMysqlImplementor =
new LogImplementor(BuiltInMethod.LOG_MYSQL.method);
map.put(LOG_MYSQL, logMysqlImplementor);
map.put(LOG_MYSQL_STYLE, logMysqlImplementor);
map.put(LOG2, logMysqlImplementor);

defineReflective(RAND, BuiltInMethod.RAND.method,
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2790,26 +2790,26 @@ public static double log(BigDecimal d0, BigDecimal d1) {

/** SQL {@code LOG(number, number2)} function applied to double values.
* but return null when number2 is 0. */
public static @Nullable Double logMysql(double number, double number2) {
public static @Nullable Double logMysqlStyle(double number, double number2) {
return (number <= 0) ? null : log(number, number2);
}

/** SQL {@code LOG(number, number2)} function applied to double and BigDecimal values.
* but return null when number2 is 0. */
public static @Nullable Double logMysql(double number, BigDecimal number2) {
return logMysql(number, number2.doubleValue());
public static @Nullable Double logMysqlStyle(double number, BigDecimal number2) {
return logMysqlStyle(number, number2.doubleValue());
}

/** SQL {@code LOG(number, number2)} function applied to BigDecimal and double values.
* but return null when number2 is 0. */
public static @Nullable Double logMysql(BigDecimal number, double number2) {
return logMysql(number.doubleValue(), number2);
public static @Nullable Double logMysqlStyle(BigDecimal number, double number2) {
return logMysqlStyle(number.doubleValue(), number2);
}

/** SQL {@code LOG(number, number2)} function applied to double values.
* but return null when number2 is 0. */
public static @Nullable Double logMysql(BigDecimal number, BigDecimal number2) {
return logMysql(number.doubleValue(), number2.doubleValue());
public static @Nullable Double logMysqlStyle(BigDecimal number, BigDecimal number2) {
return logMysqlStyle(number.doubleValue(), number2.doubleValue());
}

// MOD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2207,7 +2207,7 @@ private static RelDataType deriveTypeMapFromEntries(SqlOperatorBinding opBinding

/** The "LOG(numeric1, numeric2)" function. Returns the base numeric1 logarithm of numeric. */
@LibraryOperator(libraries = {MYSQL, SPARK})
public static final SqlFunction LOG_MYSQL =
public static final SqlFunction LOG_MYSQL_STYLE =
SqlBasicFunction.create("LOG",
ReturnTypes.DOUBLE_FORCE_NULLABLE,
OperandTypes.NUMERIC_OPTIONAL_NUMERIC,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ public enum BuiltInMethod {
SAFE_MULTIPLY(SqlFunctions.class, "safeMultiply", double.class, double.class),
SAFE_SUBTRACT(SqlFunctions.class, "safeSubtract", double.class, double.class),
LOG(SqlFunctions.class, "log", long.class, long.class),
LOG_MYSQL(SqlFunctions.class, "logMysql", long.class, long.class),
LOG_MYSQL(SqlFunctions.class, "logMysqlStyle", long.class, long.class),
SEC(SqlFunctions.class, "sec", double.class),
SECH(SqlFunctions.class, "sech", double.class),
SIGN(SqlFunctions.class, "sign", long.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6340,12 +6340,12 @@ 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 library)</a>. */
* Add LOG function (enabled in Mysql Style library)</a>. */
@Test void testLogMysqlFunc() {
final SqlOperatorFixture f0 = fixture();
f0.checkFails("^log(100, 10)^",
"No match found for function signature LOG\\(<NUMERIC>, <NUMERIC>\\)", false);
f0.setFor(SqlLibraryOperators.LOG_MYSQL);
f0.setFor(SqlLibraryOperators.LOG_MYSQL_STYLE);
final Consumer<SqlOperatorFixture> consumer = f -> {
f.checkScalarApprox("log(10, 10)", "DOUBLE",
isWithin(1.0, 0.000001));
Expand Down

0 comments on commit 3a5f076

Please sign in to comment.