mihaibudiu commented on code in PR #3839:
URL: https://github.com/apache/calcite/pull/3839#discussion_r1678632642


##########
core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java:
##########
@@ -4202,67 +4206,48 @@ private static class LogicalNotImplementor extends 
AbstractRexCallImplementor {
    * appropriate base (i.e. base e for LN).
    */
   private static class LogImplementor extends AbstractRexCallImplementor {
-    LogImplementor() {
+    private final SqlLibrary library;
+    LogImplementor(SqlLibrary library) {
       super("log", NullPolicy.STRICT, true);
+      this.library = library;
     }
 
     @Override Expression implementSafe(final RexToLixTranslator translator,
         final RexCall call, final List<Expression> argValueList) {
-      return Expressions.call(BuiltInMethod.LOG.method, args(call, 
argValueList));
+      boolean nonPositiveIsNull = library == SqlLibrary.MYSQL ? true : false;

Review Comment:
   this can be simplified by removing the ? and everything after it.



##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -6965,8 +6965,16 @@ void checkRegexpExtract(SqlOperatorFixture f0, 
FunctionAlias functionAlias) {
         isWithin(9.0, 0.000001));
     f.checkScalarApprox("log(cast(10e-3 as real), 10)", "DOUBLE NOT NULL",
         isWithin(-2.0, 0.000001));
+    f.checkScalarApprox("log(10)", "DOUBLE NOT NULL",
+        isWithin(2.302585092994046, 0.000001));
     f.checkNull("log(cast(null as real), 10)");
     f.checkNull("log(10, cast(null as real))");
+    f.checkFails("log(0)",
+        "Cannot take logarithm of zero or negative number", true);
+    f.checkFails("log(0, 64)",

Review Comment:
   The error message could probably be more descriptive, showing the value 
which is illegal.
   Also, in one of these two cases it's not the number which is illegal, but 
the base.



##########
core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java:
##########
@@ -4202,67 +4206,48 @@ private static class LogicalNotImplementor extends 
AbstractRexCallImplementor {
    * appropriate base (i.e. base e for LN).
    */
   private static class LogImplementor extends AbstractRexCallImplementor {
-    LogImplementor() {
+    private final SqlLibrary library;
+    LogImplementor(SqlLibrary library) {
       super("log", NullPolicy.STRICT, true);
+      this.library = library;
     }
 
     @Override Expression implementSafe(final RexToLixTranslator translator,
         final RexCall call, final List<Expression> argValueList) {
-      return Expressions.call(BuiltInMethod.LOG.method, args(call, 
argValueList));
+      boolean nonPositiveIsNull = library == SqlLibrary.MYSQL ? true : false;
+      return Expressions.
+          call(BuiltInMethod.LOG.method, args(call, argValueList, library, 
nonPositiveIsNull));
     }
 
     private static List<Expression> args(RexCall call,
-        List<Expression> argValueList) {
-      Expression operand0 = argValueList.get(0);
-      final Expressions.FluentList<Expression> list = 
Expressions.list(operand0);
-      switch (call.getOperator().getName()) {
-      case "LOG":
-        if (argValueList.size() == 2) {
-          return 
list.append(argValueList.get(1)).append(Expressions.constant(0));
-        }
-        // fall through
-      case "LN":
-        return 
list.append(Expressions.constant(Math.exp(1))).append(Expressions.constant(0));
-      case "LOG10":
-        return 
list.append(Expressions.constant(BigDecimal.TEN)).append(Expressions.constant(0));
-      default:
-        throw new AssertionError("Operator not found: " + call.getOperator());
+        List<Expression> argValueList, SqlLibrary library, boolean 
nonPositiveIsNull) {

Review Comment:
   I would add JavaDoc here.
   Since you are passing the library, you  as an argument, you can also compute 
nonPositiveIsNull in this function instead of passing it as an argument. I 
think this would be more consistent with the way you look at the other 
libraries.



##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -7017,24 +7025,26 @@ void checkRegexpExtract(SqlOperatorFixture f0, 
FunctionAlias functionAlias) {
     final Consumer<SqlOperatorFixture> consumer = f -> {
       f.checkScalarApprox("log(10, 10)", "DOUBLE",
           isWithin(1.0, 0.000001));
-      f.checkScalarApprox("log(64, 8)", "DOUBLE",
+      f.checkScalarApprox("log(8, 64)", "DOUBLE",
           isWithin(2.0, 0.000001));
-      f.checkScalarApprox("log(27,3)", "DOUBLE",
+      f.checkScalarApprox("log(3,27)", "DOUBLE",
           isWithin(3.0, 0.000001));
-      f.checkScalarApprox("log(100, 10)", "DOUBLE",
-          isWithin(2.0, 0.000001));
       f.checkScalarApprox("log(10, 100)", "DOUBLE",
+          isWithin(2.0, 0.000001));
+      f.checkScalarApprox("log(100, 10)", "DOUBLE",
           isWithin(0.5, 0.000001));
-      f.checkScalarApprox("log(cast(10e6 as double), 10)", "DOUBLE",
+      f.checkScalarApprox("log(10, cast(10e6 as double))", "DOUBLE",
           isWithin(7.0, 0.000001));
-      f.checkScalarApprox("log(cast(10e8 as float), 10)", "DOUBLE",
+      f.checkScalarApprox("log(10, cast(10e8 as float))", "DOUBLE",
           isWithin(9.0, 0.000001));
-      f.checkScalarApprox("log(cast(10e-3 as real), 10)", "DOUBLE",
+      f.checkScalarApprox("log(10, cast(10e-3 as real))", "DOUBLE",

Review Comment:
   maybe the fact that the log of a REAL is a DOUBLE should be documented



##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -7017,24 +7025,26 @@ void checkRegexpExtract(SqlOperatorFixture f0, 
FunctionAlias functionAlias) {
     final Consumer<SqlOperatorFixture> consumer = f -> {
       f.checkScalarApprox("log(10, 10)", "DOUBLE",
           isWithin(1.0, 0.000001));
-      f.checkScalarApprox("log(64, 8)", "DOUBLE",
+      f.checkScalarApprox("log(8, 64)", "DOUBLE",
           isWithin(2.0, 0.000001));
-      f.checkScalarApprox("log(27,3)", "DOUBLE",
+      f.checkScalarApprox("log(3,27)", "DOUBLE",
           isWithin(3.0, 0.000001));
-      f.checkScalarApprox("log(100, 10)", "DOUBLE",
-          isWithin(2.0, 0.000001));
       f.checkScalarApprox("log(10, 100)", "DOUBLE",
+          isWithin(2.0, 0.000001));
+      f.checkScalarApprox("log(100, 10)", "DOUBLE",
           isWithin(0.5, 0.000001));
-      f.checkScalarApprox("log(cast(10e6 as double), 10)", "DOUBLE",
+      f.checkScalarApprox("log(10, cast(10e6 as double))", "DOUBLE",

Review Comment:
   I think these tests would be easier to read if you wrote 1e7.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to