This is an automated email from the ASF dual-hosted git repository.
morrysnow pushed a commit to branch branch-3.1
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-3.1 by this push:
new 35279a670dc branch-3.1: [feature](Nereids): Convert log(x) to ln(x)
#52862 (#53276)
35279a670dc is described below
commit 35279a670dcc6538b39e7eae75df5159d2c22bd8
Author: zhangstar333 <[email protected]>
AuthorDate: Wed Jul 16 17:51:36 2025 +0800
branch-3.1: [feature](Nereids): Convert log(x) to ln(x) #52862 (#53276)
cherry-picked from #52862
---
.../rules/expression/ExpressionOptimization.java | 4 +-
.../nereids/rules/expression/rules/LogToLn.java | 54 +++++++++++++++++++++
.../trees/expressions/functions/scalar/Log.java | 17 ++++++-
.../test_math_unary_always_nullable.out | Bin 1032 -> 1768 bytes
.../test_math_unary_always_nullable.groovy | 9 +++-
5 files changed, 80 insertions(+), 4 deletions(-)
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java
index b91f9a3d0ec..220c52919c0 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java
@@ -24,6 +24,7 @@ import
org.apache.doris.nereids.rules.expression.rules.DateFunctionRewrite;
import org.apache.doris.nereids.rules.expression.rules.DistinctPredicatesRule;
import org.apache.doris.nereids.rules.expression.rules.ExtractCommonFactorRule;
import org.apache.doris.nereids.rules.expression.rules.LikeToEqualRewrite;
+import org.apache.doris.nereids.rules.expression.rules.LogToLn;
import org.apache.doris.nereids.rules.expression.rules.NullSafeEqualToEqual;
import
org.apache.doris.nereids.rules.expression.rules.SimplifyComparisonPredicate;
import
org.apache.doris.nereids.rules.expression.rules.SimplifyConflictCompound;
@@ -61,7 +62,8 @@ public class ExpressionOptimization extends ExpressionRewrite
{
TopnToMax.INSTANCE,
NullSafeEqualToEqual.INSTANCE,
LikeToEqualRewrite.INSTANCE,
- BetweenToEqual.INSTANCE
+ BetweenToEqual.INSTANCE,
+ LogToLn.INSTANCE
)
);
private static final ExpressionRuleExecutor EXECUTOR = new
ExpressionRuleExecutor(OPTIMIZE_REWRITE_RULES);
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/LogToLn.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/LogToLn.java
new file mode 100644
index 00000000000..ef9f9c3cc19
--- /dev/null
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/LogToLn.java
@@ -0,0 +1,54 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.rules.expression.rules;
+
+import org.apache.doris.nereids.rules.expression.ExpressionPatternMatcher;
+import org.apache.doris.nereids.rules.expression.ExpressionPatternRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.Ln;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.Log;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+
+/**
+ * In MySQL, LOG(col, e) is equivalent to LN(col), where e is Euler’s number
(~2.71828).
+ * Currently, MySQL also supports LOG(col) as a shorthand for LOG(col, e)
(i.e., natural logarithm).
+ * This conversion replaces LOG(col) with LN(col) when only a single argument
is provided.
+ */
+public class LogToLn implements ExpressionPatternRuleFactory {
+
+ public static final LogToLn INSTANCE = new LogToLn();
+
+ @Override
+ public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
+ return ImmutableList.of(
+ matchesType(Log.class).then(LogToLn::rewrite)
+ );
+ }
+
+ /** rewrite */
+ public static Expression rewrite(Log log) {
+ if (log.arity() == 1) {
+ return new Ln(log.child(0));
+ } else {
+ return log;
+ }
+ }
+}
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Log.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Log.java
index d8f23e21e05..559bc7a0dbb 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Log.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Log.java
@@ -37,9 +37,17 @@ public class Log extends ScalarFunction
implements BinaryExpression, ExplicitlyCastableSignature,
AlwaysNullable {
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
+
FunctionSignature.ret(DoubleType.INSTANCE).args(DoubleType.INSTANCE),
FunctionSignature.ret(DoubleType.INSTANCE).args(DoubleType.INSTANCE,
DoubleType.INSTANCE)
);
+ /**
+ * constructor with 1 arguments.
+ */
+ public Log(Expression arg0) {
+ super("log", arg0);
+ }
+
/**
* constructor with 2 arguments.
*/
@@ -52,8 +60,13 @@ public class Log extends ScalarFunction
*/
@Override
public Log withChildren(List<Expression> children) {
- Preconditions.checkArgument(children.size() == 2);
- return new Log(children.get(0), children.get(1));
+ Preconditions.checkArgument(children.size() == 1 || children.size() ==
2,
+ "Log function should have 1 or 2 children, but got: %s",
children.size());
+ if (children.size() == 1) {
+ return new Log(children.get(0));
+ } else {
+ return new Log(children.get(0), children.get(1));
+ }
}
@Override
diff --git
a/regression-test/data/query_p0/sql_functions/math_functions/test_math_unary_always_nullable.out
b/regression-test/data/query_p0/sql_functions/math_functions/test_math_unary_always_nullable.out
index 0a190f0bd6b..2067055655f 100644
Binary files
a/regression-test/data/query_p0/sql_functions/math_functions/test_math_unary_always_nullable.out
and
b/regression-test/data/query_p0/sql_functions/math_functions/test_math_unary_always_nullable.out
differ
diff --git
a/regression-test/suites/query_p0/sql_functions/math_functions/test_math_unary_always_nullable.groovy
b/regression-test/suites/query_p0/sql_functions/math_functions/test_math_unary_always_nullable.groovy
index 282d4e3c575..a8505e9b512 100644
---
a/regression-test/suites/query_p0/sql_functions/math_functions/test_math_unary_always_nullable.groovy
+++
b/regression-test/suites/query_p0/sql_functions/math_functions/test_math_unary_always_nullable.groovy
@@ -81,5 +81,12 @@ suite("test_math_unary_alway_nullable") {
qt_dsqrt_tbl_1 """
select rowid, dsqrt(val), dsqrt(val) is null from
test_math_unary_alway_nullable order by rowid;
"""
+ sql """
+ insert into test_math_unary_alway_nullable values
+ (9, 1.2), (10, 2.2), (11, 3.4), (12, 5.6)
+ """
-}
\ No newline at end of file
+ qt_dlog_tbl_1 """
+ select rowid, log(val), ln(val), log(3), ln(3) from
test_math_unary_alway_nullable order by rowid;
+ """
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]