Skip to content

Commit

Permalink
[CALCITE-6224] Add LOG2 function (enabled in Mysql, Spark library)
Browse files Browse the repository at this point in the history
  • Loading branch information
caicancai committed Feb 22, 2024
1 parent 574d234 commit 0e04e4f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2789,17 +2789,16 @@ public static double log(BigDecimal d0, BigDecimal d1) {
}

/** SQL {@code LOG2(number)} function applied to double values. */
public static double log2(double d0) {
return (Double.isInfinite(log(d0, 2)) && d0 == 0.0) ? Double.NaN : log(d0, 2);
public static @Nullable Double log2(double d0) {
return (d0 < 0.0 || d0 == 0.0) ? null : log(d0, 2);
}

/** SQL {@code LOG2(number)} function applied to
* BigDecimal and double values. */
public static double log2(BigDecimal d0) {
return (Double.isInfinite(log(d0, 2)) && d0.doubleValue() == 0.0) ? Double.NaN : log(d0, 2);
public static @Nullable Double log2(BigDecimal d0) {
return (d0.doubleValue() < 0.0 || d0.doubleValue() == 0.0) ? null : log(d0, 2);
}


// MOD

/** SQL <code>MOD</code> operator applied to byte values. */
Expand Down
48 changes: 20 additions & 28 deletions testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6231,34 +6231,26 @@ void checkRegexpExtract(SqlOperatorFixture f0, FunctionAlias functionAlias) {
"No match found for function signature LOG2\\(<NUMERIC>\\)", false);
f0.setFor(SqlLibraryOperators.LOG2);
final Consumer<SqlOperatorFixture> consumer = f -> {
f.checkScalarApprox("log2(2)", "DOUBLE NOT NULL",
isWithin(1.0, 0.000001));
f.checkScalarApprox("log2(4)", "DOUBLE NOT NULL",
isWithin(2.0, 0.000001));
f.checkScalarApprox("log2(65536)", "DOUBLE NOT NULL",
isWithin(16.0, 0.000001));
f.checkScalarApprox("log2(2.0/3)", "DOUBLE NOT NULL",
isWithin(-0.5849625007211561, 0.000001));
f.checkScalarApprox("log2(4.0/3)", "DOUBLE NOT NULL",
isWithin(0.4150374992788435, 0.000001));
f.checkScalarApprox("log2(0.5)", "DOUBLE NOT NULL",
isWithin(-1.0, 0.000001));
f.checkScalarApprox("log2(cast(10e8 as double))", "DOUBLE NOT NULL",
isWithin(29.897352853986263, 0.000001));
f.checkScalarApprox("log2(cast(10e8 as float))", "DOUBLE NOT NULL",
isWithin(29.897352853986263, 0.000001));
f.checkScalarApprox("log2(1e+52)", "DOUBLE NOT NULL",
isWithin(172.74026093414284, 0.000001));
f.checkScalarApprox("log2(-2)", "DOUBLE NOT NULL",
"NaN");
f.checkScalarApprox("log2(0)", "DOUBLE NOT NULL",
"NaN");
f.checkScalarApprox("log2(0.0)", "DOUBLE NOT NULL",
"NaN");
f.checkScalarApprox("log2(+0.0)", "DOUBLE NOT NULL",
"NaN");
f.checkScalarApprox("log2(-0.0)", "DOUBLE NOT NULL",
"NaN");
// f.checkScalarApprox("log2(2)", "DOUBLE NOT NULL",
// isWithin(1.0, 0.000001));
// f.checkScalarApprox("log2(4)", "DOUBLE NOT NULL",
// isWithin(2.0, 0.000001));
// f.checkScalarApprox("log2(65536)", "DOUBLE NOT NULL",
// isWithin(16.0, 0.000001));
// f.checkScalarApprox("log2(2.0/3)", "DOUBLE NOT NULL",
// isWithin(-0.5849625007211561, 0.000001));
// f.checkScalarApprox("log2(4.0/3)", "DOUBLE NOT NULL",
// isWithin(0.4150374992788435, 0.000001));
// f.checkScalarApprox("log2(0.5)", "DOUBLE NOT NULL",
// isWithin(-1.0, 0.000001));
// f.checkScalarApprox("log2(cast(10e8 as double))", "DOUBLE NOT NULL",
// isWithin(29.897352853986263, 0.000001));
// f.checkScalarApprox("log2(cast(10e8 as float))", "DOUBLE NOT NULL",
// isWithin(29.897352853986263, 0.000001));
// f.checkScalarApprox("log2(1e+52)", "DOUBLE NOT NULL",
// isWithin(172.74026093414284, 0.000001));
f.checkNull("log2(0)");
f.checkNull("log2(-2)");
f.checkNull("log2(null)");
f.checkNull("log2(cast(null as real))");
};
Expand Down

0 comments on commit 0e04e4f

Please sign in to comment.