Skip to content

Commit

Permalink
[CALCITE-6549] Add LOG1P function (enabled in Spark library)
Browse files Browse the repository at this point in the history
  • Loading branch information
caicancai authored and NobiGo committed Sep 13, 2024
1 parent 890f9ad commit 91fe118
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
import static org.apache.calcite.sql.fun.SqlLibraryOperators.LEFT;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.LEVENSHTEIN;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.LOG;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.LOG1P;
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;
Expand Down Expand Up @@ -747,6 +748,7 @@ Builder populate() {
defineMethod(TANH, BuiltInMethod.TANH.method, NullPolicy.STRICT);
defineMethod(TRUNC_BIG_QUERY, BuiltInMethod.STRUNCATE.method, NullPolicy.STRICT);
defineMethod(TRUNCATE, BuiltInMethod.STRUNCATE.method, NullPolicy.STRICT);
defineMethod(LOG1P, BuiltInMethod.LOG1P.method, NullPolicy.STRICT);

map.put(SAFE_ADD,
new SafeArithmeticImplementor(BuiltInMethod.SAFE_ADD.method));
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3061,6 +3061,16 @@ public static double power(BigDecimal b0, BigDecimal b1) {
return Math.log(number.doubleValue()) / Math.log(base.doubleValue());
}

/** SQL <code>LOG1P</code> operator applied to double values. */
public static @Nullable Double log1p(double b0) {
return b0 <= -1 ? null : Math.log1p(b0);
}

/** SQL <code>LOG1P</code> operator applied to BigDecimal values. */
public static @Nullable Double log1p(BigDecimal b0) {
return b0.doubleValue() <= -1 ? null : Math.log1p(b0.doubleValue());
}

// MOD

/** SQL <code>MOD</code> operator applied to byte values. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2525,6 +2525,14 @@ private static RelDataType deriveTypeMapFromEntries(SqlOperatorBinding opBinding
OperandTypes.NUMERIC,
SqlFunctionCategory.NUMERIC);

/** The "LOG1p(numeric)" function. Returns log(1 + numeric). */
@LibraryOperator(libraries = {SPARK})
public static final SqlFunction LOG1P =
SqlBasicFunction.create("LOG1P",
ReturnTypes.DOUBLE_FORCE_NULLABLE,
OperandTypes.NUMERIC,
SqlFunctionCategory.NUMERIC);

@LibraryOperator(libraries = {BIG_QUERY, SPARK})
public static final SqlFunction POW =
SqlBasicFunction.create("POW",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,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, boolean.class),
LOG1P(SqlFunctions.class, "log1p", long.class),
SEC(SqlFunctions.class, "sec", double.class),
SECH(SqlFunctions.class, "sech", double.class),
SIGN(SqlFunctions.class, "sign", long.class),
Expand Down
1 change: 1 addition & 0 deletions site/_docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2853,6 +2853,7 @@ In the following:
| m s | LOG([, base ], numeric1) | Returns the logarithm of *numeric1* to base *base*, or base e if *base* is not present, or null if *numeric1* is 0 or negative
| p | LOG([, base ], numeric1 ) | Returns the logarithm of *numeric1* to base *base*, or base 10 if *numeric1* is not present, or error if *numeric1* is 0 or negative
| m s | LOG2(numeric) | Returns the base 2 logarithm of *numeric*
| s | LOG1P(numeric) | Returns the natural logarithm of 1 plus *numeric*
| b o p r 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
| b | FROM_BASE32(string) | Returns the decoded result of a base-32 *string* as a string
Expand Down
25 changes: 25 additions & 0 deletions testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7372,6 +7372,31 @@ void checkRegexpExtract(SqlOperatorFixture f0, FunctionAlias functionAlias) {
"Cannot take logarithm of zero or negative number", true);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6549">[CALCITE-6549]
* Add LOG1P function (enabled in Spark library)</a>. */
@Test void testLog1PFunc() {
final SqlOperatorFixture f0 = fixture()
.setFor(SqlLibraryOperators.LOG1P, VmName.EXPAND);
f0.checkFails("^log1p(4)^",
"No match found for function signature LOG1P\\(<NUMERIC>\\)", false);
final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.SPARK);
f.checkScalarApprox("log1p(0)", "DOUBLE",
isWithin(0.0, 0.000001));
f.checkScalarApprox("log1p(1)", "DOUBLE",
isWithin(0.6931471805599453, 0.000001));
f.checkScalarApprox("log1p(1e+22)", "DOUBLE",
isWithin(50.65687204586901, 0.000001));
f.checkScalarApprox("log1p(1.2)", "DOUBLE",
isWithin(0.7884573603642702, 0.000001));
f.checkScalarApprox("log1p(2.0/3)", "DOUBLE",
isWithin(0.5108256237659907, 0.000001));
f.checkNull("log1p(cast(null as real))");
f.checkNull("log1p(-1)");
f.checkNull("log1p(null)");
f.checkFails("^log1p()^", INVALID_ARGUMENTS_NUMBER, false);
}

@Test void testRandFunc() {
final SqlOperatorFixture f = fixture();
f.setFor(SqlStdOperatorTable.RAND, VmName.EXPAND);
Expand Down

0 comments on commit 91fe118

Please sign in to comment.