This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git

commit 8b61b7c6cd74023a6b2ce1a3cba03ba14a2ca262
Author: yangshijie <sjyang2...@zju.edu.cn>
AuthorDate: Wed Jan 31 10:32:02 2024 +0800

    [exec](function) Add tanh func (#30555)
---
 be/src/vec/functions/math.cpp                      |  6 ++
 .../sql-functions/numeric-functions/tanh.md        | 52 +++++++++++++++++
 docs/sidebars.json                                 |  1 +
 .../sql-functions/numeric-functions/tanh.md        | 52 +++++++++++++++++
 .../doris/catalog/BuiltinScalarFunctions.java      |  2 +
 .../trees/expressions/functions/scalar/Tanh.java   | 68 ++++++++++++++++++++++
 .../expressions/visitor/ScalarFunctionVisitor.java |  5 ++
 gensrc/script/doris_builtins_functions.py          |  1 +
 .../data/nereids_function_p0/scalar_function/T.out | 29 +++++++++
 .../nereids_function_p0/scalar_function/T.groovy   |  2 +
 10 files changed, 218 insertions(+)

diff --git a/be/src/vec/functions/math.cpp b/be/src/vec/functions/math.cpp
index c3a5111ac57..7afdde90860 100644
--- a/be/src/vec/functions/math.cpp
+++ b/be/src/vec/functions/math.cpp
@@ -224,6 +224,11 @@ struct TanName {
 };
 using FunctionTan = FunctionMathUnary<UnaryFunctionPlain<TanName, std::tan>>;
 
+struct TanhName {
+    static constexpr auto name = "tanh";
+};
+using FunctionTanh = FunctionMathUnary<UnaryFunctionPlain<TanhName, 
std::tanh>>;
+
 template <typename A>
 struct RadiansImpl {
     using ResultType = A;
@@ -407,6 +412,7 @@ void register_function_math(SimpleFunctionFactory& factory) 
{
     factory.register_alias("sqrt", "dsqrt");
     factory.register_function<FunctionCbrt>();
     factory.register_function<FunctionTan>();
+    factory.register_function<FunctionTanh>();
     factory.register_alias("floor", "dfloor");
     factory.register_function<FunctionPow>();
     factory.register_alias("pow", "power");
diff --git a/docs/en/docs/sql-manual/sql-functions/numeric-functions/tanh.md 
b/docs/en/docs/sql-manual/sql-functions/numeric-functions/tanh.md
new file mode 100644
index 00000000000..81336354895
--- /dev/null
+++ b/docs/en/docs/sql-manual/sql-functions/numeric-functions/tanh.md
@@ -0,0 +1,52 @@
+---
+{
+    "title": "TANH",
+    "language": "en"
+}
+---
+
+<!-- 
+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.
+-->
+
+## tanh
+
+### description
+#### Syntax
+
+`DOUBLE tanh(DOUBLE x)`
+Returns the hyperbolic tangent of `x`, tanh(x) = sinh(x) / cosh(x).
+
+### example
+
+```
+mysql> select tanh(0);
++---------+
+| tanh(0) |
++---------+
+|       0 |
++---------+
+
+mysql> select tanh(1);
++---------------------+
+| tanh(1)             |
++---------------------+
+| 0.76159415595576485 |
++---------------------+
+```
+
+### keywords
+       TANH
diff --git a/docs/sidebars.json b/docs/sidebars.json
index 22711549712..3acc778678a 100644
--- a/docs/sidebars.json
+++ b/docs/sidebars.json
@@ -712,6 +712,7 @@
                                 
"sql-manual/sql-functions/numeric-functions/sin",
                                 
"sql-manual/sql-functions/numeric-functions/cos",
                                 
"sql-manual/sql-functions/numeric-functions/tan",
+                                
"sql-manual/sql-functions/numeric-functions/tanh",
                                 
"sql-manual/sql-functions/numeric-functions/asin",
                                 
"sql-manual/sql-functions/numeric-functions/acos",
                                 
"sql-manual/sql-functions/numeric-functions/atan",
diff --git a/docs/zh-CN/docs/sql-manual/sql-functions/numeric-functions/tanh.md 
b/docs/zh-CN/docs/sql-manual/sql-functions/numeric-functions/tanh.md
new file mode 100644
index 00000000000..d8ca178d846
--- /dev/null
+++ b/docs/zh-CN/docs/sql-manual/sql-functions/numeric-functions/tanh.md
@@ -0,0 +1,52 @@
+---
+{
+    "title": "TANH",
+    "language": "zh-CN"
+}
+---
+
+<!-- 
+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.
+-->
+
+## tanh
+
+### description
+#### Syntax
+
+`DOUBLE tanh(DOUBLE x)`
+返回`x`的双曲正切值,tanh(x) = sinh(x) / cosh(x).
+
+### example
+
+```
+mysql> select tanh(0);
++---------+
+| tanh(0) |
++---------+
+|       0 |
++---------+
+
+mysql> select tanh(1);
++---------------------+
+| tanh(1)             |
++---------------------+
+| 0.76159415595576485 |
++---------------------+
+```
+
+### keywords
+       TANH
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinScalarFunctions.java 
b/fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinScalarFunctions.java
index 94a1aaaaace..5eac2d885f1 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinScalarFunctions.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinScalarFunctions.java
@@ -385,6 +385,7 @@ import 
org.apache.doris.nereids.trees.expressions.functions.scalar.SubReplace;
 import org.apache.doris.nereids.trees.expressions.functions.scalar.Substring;
 import 
org.apache.doris.nereids.trees.expressions.functions.scalar.SubstringIndex;
 import org.apache.doris.nereids.trees.expressions.functions.scalar.Tan;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.Tanh;
 import org.apache.doris.nereids.trees.expressions.functions.scalar.TimeDiff;
 import org.apache.doris.nereids.trees.expressions.functions.scalar.TimeToSec;
 import org.apache.doris.nereids.trees.expressions.functions.scalar.Timestamp;
@@ -837,6 +838,7 @@ public class BuiltinScalarFunctions implements 
FunctionHelper {
             scalar(Substring.class, "substr", "substring"),
             scalar(SubstringIndex.class, "substring_index"),
             scalar(Tan.class, "tan"),
+            scalar(Tanh.class, "tanh"),
             scalar(TimeDiff.class, "timediff"),
             scalar(TimeToSec.class, "time_to_sec"),
             scalar(Timestamp.class, "timestamp"),
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Tanh.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Tanh.java
new file mode 100644
index 00000000000..eb5b814cd8a
--- /dev/null
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Tanh.java
@@ -0,0 +1,68 @@
+// 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.trees.expressions.functions.scalar;
+
+import org.apache.doris.catalog.FunctionSignature;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import 
org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
+import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
+import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
+import org.apache.doris.nereids.types.DoubleType;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+
+/**
+ * ScalarFunction 'tanh'. This class is generated by GenerateFunction.
+ */
+public class Tanh extends ScalarFunction
+        implements UnaryExpression, ExplicitlyCastableSignature, 
PropagateNullable {
+
+    public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
+            
FunctionSignature.ret(DoubleType.INSTANCE).args(DoubleType.INSTANCE)
+    );
+
+    /**
+     * constructor with 1 argument.
+     */
+    public Tanh(Expression arg) {
+        super("tanh", arg);
+    }
+
+    /**
+     * withChildren.
+     */
+    @Override
+    public Tanh withChildren(List<Expression> children) {
+        Preconditions.checkArgument(children.size() == 1);
+        return new Tanh(children.get(0));
+    }
+
+    @Override
+    public List<FunctionSignature> getSignatures() {
+        return SIGNATURES;
+    }
+
+    @Override
+    public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
+        return visitor.visitTanh(this, context);
+    }
+}
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ScalarFunctionVisitor.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ScalarFunctionVisitor.java
index 9a1b970b72f..fd72711bfe3 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ScalarFunctionVisitor.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ScalarFunctionVisitor.java
@@ -376,6 +376,7 @@ import 
org.apache.doris.nereids.trees.expressions.functions.scalar.SubReplace;
 import org.apache.doris.nereids.trees.expressions.functions.scalar.Substring;
 import 
org.apache.doris.nereids.trees.expressions.functions.scalar.SubstringIndex;
 import org.apache.doris.nereids.trees.expressions.functions.scalar.Tan;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.Tanh;
 import org.apache.doris.nereids.trees.expressions.functions.scalar.TimeDiff;
 import org.apache.doris.nereids.trees.expressions.functions.scalar.Timestamp;
 import org.apache.doris.nereids.trees.expressions.functions.scalar.ToBase64;
@@ -1851,6 +1852,10 @@ public interface ScalarFunctionVisitor<R, C> {
         return visitScalarFunction(tan, context);
     }
 
+    default R visitTanh(Tanh tanh, C context) {
+        return visitScalarFunction(tanh, context);
+    }
+
     default R visitTimeDiff(TimeDiff timeDiff, C context) {
         return visitScalarFunction(timeDiff, context);
     }
diff --git a/gensrc/script/doris_builtins_functions.py 
b/gensrc/script/doris_builtins_functions.py
index 970c50beae7..cb3fc47405e 100644
--- a/gensrc/script/doris_builtins_functions.py
+++ b/gensrc/script/doris_builtins_functions.py
@@ -1396,6 +1396,7 @@ visible_functions = {
         [['sqrt', 'dsqrt'], 'DOUBLE', ['DOUBLE'], ''],
         
         [['tan'], 'DOUBLE', ['DOUBLE'], ''],
+        [['tanh'], 'DOUBLE', ['DOUBLE'], ''],
         [['truncate'], 'DOUBLE', ['DOUBLE'], ''],
         [['truncate'], 'DOUBLE', ['DOUBLE', 'INT'], ''],
         [['truncate'], 'DECIMAL32', ['DECIMAL32', 'INT'], ''],
diff --git a/regression-test/data/nereids_function_p0/scalar_function/T.out 
b/regression-test/data/nereids_function_p0/scalar_function/T.out
index 9daa179d738..d293ed580a6 100644
--- a/regression-test/data/nereids_function_p0/scalar_function/T.out
+++ b/regression-test/data/nereids_function_p0/scalar_function/T.out
@@ -28,6 +28,35 @@
 1.9647596572486525
 2.5721516221263183
 
+-- !sql_tanh_Double --
+\N
+0.09966799462495582
+0.197375320224904
+0.2913126124515909
+0.3799489622552249
+0.46211715726000974
+0.5370495669980353
+0.6043677771171636
+0.6640367702678491
+0.7162978701990245
+0.7615941559557649
+0.8004990217606297
+0.8336546070121552
+
+-- !sql_tanh_Double_notnull --
+0.09966799462495582
+0.197375320224904
+0.2913126124515909
+0.3799489622552249
+0.46211715726000974
+0.5370495669980353
+0.6043677771171636
+0.6640367702678491
+0.7162978701990245
+0.7615941559557649
+0.8004990217606297
+0.8336546070121552
+
 -- !sql_timediff_DateTime_DateTime --
 \N
 00:00:00
diff --git 
a/regression-test/suites/nereids_function_p0/scalar_function/T.groovy 
b/regression-test/suites/nereids_function_p0/scalar_function/T.groovy
index 638a467236c..86db66358c4 100644
--- a/regression-test/suites/nereids_function_p0/scalar_function/T.groovy
+++ b/regression-test/suites/nereids_function_p0/scalar_function/T.groovy
@@ -21,6 +21,8 @@ suite("nereids_scalar_fn_T") {
        sql 'set enable_fallback_to_original_planner=false'
        qt_sql_tan_Double "select tan(kdbl) from fn_test order by kdbl"
        qt_sql_tan_Double_notnull "select tan(kdbl) from fn_test_not_nullable 
order by kdbl"
+       qt_sql_tanh_Double "select tanh(kdbl) from fn_test order by kdbl"
+       qt_sql_tanh_Double_notnull "select tanh(kdbl) from fn_test_not_nullable 
order by kdbl"
        qt_sql_timediff_DateTime_DateTime "select timediff(kdtm, kdtm) from 
fn_test order by kdtm, kdtm"
        qt_sql_timediff_DateTime_DateTime_notnull "select timediff(kdtm, kdtm) 
from fn_test_not_nullable order by kdtm, kdtm"
        qt_sql_timediff_DateTimeV2_DateTimeV2 "select timediff(kdtmv2s1, 
kdtmv2s1) from fn_test order by kdtmv2s1, kdtmv2s1"


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to