From 8b61b7c6cd74023a6b2ce1a3cba03ba14a2ca262 Mon Sep 17 00:00:00 2001 From: yangshijie Date: Wed, 31 Jan 2024 10:32:02 +0800 Subject: [PATCH] [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 + .../expressions/functions/scalar/Tanh.java | 68 +++++++++++++++++++ .../visitor/ScalarFunctionVisitor.java | 5 ++ gensrc/script/doris_builtins_functions.py | 1 + .../nereids_function_p0/scalar_function/T.out | 29 ++++++++ .../scalar_function/T.groovy | 2 + 10 files changed, 218 insertions(+) create mode 100644 docs/en/docs/sql-manual/sql-functions/numeric-functions/tanh.md create mode 100644 docs/zh-CN/docs/sql-manual/sql-functions/numeric-functions/tanh.md create mode 100644 fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Tanh.java diff --git a/be/src/vec/functions/math.cpp b/be/src/vec/functions/math.cpp index c3a5111ac5..7afdde9086 100644 --- a/be/src/vec/functions/math.cpp +++ b/be/src/vec/functions/math.cpp @@ -224,6 +224,11 @@ struct TanName { }; using FunctionTan = FunctionMathUnary>; +struct TanhName { + static constexpr auto name = "tanh"; +}; +using FunctionTanh = FunctionMathUnary>; + template struct RadiansImpl { using ResultType = A; @@ -407,6 +412,7 @@ void register_function_math(SimpleFunctionFactory& factory) { factory.register_alias("sqrt", "dsqrt"); factory.register_function(); factory.register_function(); + factory.register_function(); factory.register_alias("floor", "dfloor"); factory.register_function(); 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 0000000000..8133635489 --- /dev/null +++ b/docs/en/docs/sql-manual/sql-functions/numeric-functions/tanh.md @@ -0,0 +1,52 @@ +--- +{ + "title": "TANH", + "language": "en" +} +--- + + + +## 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 2271154971..3acc778678 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 0000000000..d8ca178d84 --- /dev/null +++ b/docs/zh-CN/docs/sql-manual/sql-functions/numeric-functions/tanh.md @@ -0,0 +1,52 @@ +--- +{ + "title": "TANH", + "language": "zh-CN" +} +--- + + + +## 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 94a1aaaaac..5eac2d885f 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 0000000000..eb5b814cd8 --- /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 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 children) { + Preconditions.checkArgument(children.size() == 1); + return new Tanh(children.get(0)); + } + + @Override + public List getSignatures() { + return SIGNATURES; + } + + @Override + public R accept(ExpressionVisitor 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 9a1b970b72..fd72711bfe 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 { 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 970c50beae..cb3fc47405 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 9daa179d73..d293ed580a 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 638a467236..86db66358c 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"