Skip to content

[Enhancement](nereids, function, test) support the gamma math function #51576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions be/src/vec/functions/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ struct AtanhName {
using FunctionAtanh =
FunctionMathUnaryAlwayNullable<UnaryFunctionPlainAlwayNullable<AtanhName, std::atanh>>;

struct GammaName {
static constexpr auto name = "gamma";
static constexpr bool is_invalid_input(Float64 x) {
constexpr auto eps = std::numeric_limits<Float64>::epsilon();
return x <= 0 && std::abs(x - std::floor(x)) < eps;
}
};
using FunctionGamma =
FunctionMathUnaryAlwayNullable<UnaryFunctionPlainAlwayNullable<GammaName, std::tgamma>>;

struct LGammaName {
static constexpr auto name = "lgamma";
static constexpr bool is_invalid_input(Float64 x) {
constexpr Float64 eps = std::numeric_limits<Float64>::epsilon();
return x <= eps;
}
};
using FunctionLGamma =
FunctionMathUnaryAlwayNullable<UnaryFunctionPlainAlwayNullable<LGammaName, std::lgamma>>;

template <PrimitiveType AType, PrimitiveType BType>
struct Atan2Impl {
using A = typename PrimitiveTypeTraits<AType>::ColumnItemType;
Expand Down Expand Up @@ -538,6 +558,8 @@ void register_function_math(SimpleFunctionFactory& factory) {
factory.register_function<FunctionAtan>();
factory.register_function<FunctionAtanh>();
factory.register_function<FunctionAtan2>();
factory.register_function<FunctionGamma>();
factory.register_function<FunctionLGamma>();
factory.register_function<FunctionCos>();
factory.register_function<FunctionCosh>();
factory.register_function<FunctionE>();
Expand Down
38 changes: 38 additions & 0 deletions be/test/vec/function/function_math_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,44 @@ TEST(MathFunctionTest, atanh_test) {
check_function_all_arg_comb<DataTypeFloat64, true>(func_name, input_types, data_set));
}

TEST(MathFunctionTest, gamma_test) {
std::string func_name = "gamma";

InputTypeSet input_types = {PrimitiveType::TYPE_DOUBLE};

DataSet data_set = {{{0.5}, 1.772453850905516},
{{1.0}, 1.0},
{{2.0}, 1.0},
{{3.0}, 2.0},
{{5.0}, 24.0},
{{1.5}, 0.886226925452758},
{{0.0}, Null()},
{{-1.0}, Null()}};

static_cast<void>(
check_function_all_arg_comb<DataTypeFloat64, true>(func_name, input_types, data_set));
}

TEST(MathFunctionTest, lgamma_test) {
std::string func_name = "lgamma";

InputTypeSet input_types = {PrimitiveType::TYPE_DOUBLE};

DataSet data_set = {
{{0.5}, 0.5723649429247001},
{{1.0}, 0.0},
{{2.0}, 0.0},
{{3.0}, 0.6931471805599453},
{{4.0}, 1.791759469228055},
{{1.5}, -0.12078223763524522},
{{0.0}, Null()},
{{-0.1}, Null()},
};

static_cast<void>(
check_function_all_arg_comb<DataTypeFloat64, true>(func_name, input_types, data_set));
}

TEST(MathFunctionTest, cos_test) {
std::string func_name = "cos";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.FromSecond;
import org.apache.doris.nereids.trees.expressions.functions.scalar.FromUnixtime;
import org.apache.doris.nereids.trees.expressions.functions.scalar.G;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Gamma;
import org.apache.doris.nereids.trees.expressions.functions.scalar.GetJsonBigInt;
import org.apache.doris.nereids.trees.expressions.functions.scalar.GetJsonDouble;
import org.apache.doris.nereids.trees.expressions.functions.scalar.GetJsonInt;
Expand Down Expand Up @@ -283,6 +284,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.Least;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Left;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Length;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Lgamma;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Ln;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Locate;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Log;
Expand Down Expand Up @@ -683,6 +685,7 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(FromIso8601Date.class, "from_iso8601_date"),
scalar(FromUnixtime.class, "from_unixtime"),
scalar(G.class, "g"),
scalar(Gamma.class, "gamma"),
scalar(GetJsonBigInt.class, "get_json_bigint"),
scalar(GetJsonDouble.class, "get_json_double"),
scalar(GetJsonInt.class, "get_json_int"),
Expand Down Expand Up @@ -787,6 +790,7 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(Least.class, "least"),
scalar(Left.class, "left", "strleft"),
scalar(Length.class, "length"),
scalar(Lgamma.class, "lgamma"),
scalar(Crc32.class, "crc32"),
scalar(Like.class, "like"),
scalar(Ln.class, "ln", "dlog1"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.doris.nereids.types.DecimalV3Type;
import org.apache.doris.nereids.types.DoubleType;

import org.apache.commons.math3.special.Gamma;
import org.apache.commons.math3.util.FastMath;

import java.math.BigDecimal;
Expand Down Expand Up @@ -999,6 +1000,32 @@ public static Expression atan2(DoubleLiteral first, DoubleLiteral second) {
return checkOutputBoundary(new DoubleLiteral(Math.atan2(first.getValue(), second.getValue())));
}

/**
* gamma
*/
@ExecFunction(name = "gamma")
public static Expression gamma(DoubleLiteral first) {
double value = first.getValue();
final double epsilon = Math.ulp(1.0);
if (value <= 0 && Math.abs(value - Math.floor(value)) < epsilon) {
return new NullLiteral(DoubleType.INSTANCE);
}
return checkOutputBoundary(new DoubleLiteral(Gamma.gamma(value)));
}

/**
* gamma
*/
@ExecFunction(name = "lgamma")
public static Expression lgamma(DoubleLiteral first) {
double value = first.getValue();
final double epsilon = Math.ulp(1.0);
if (value <= epsilon) {
return new NullLiteral(DoubleType.INSTANCE);
}
return checkOutputBoundary(new DoubleLiteral(Gamma.logGamma(value)));
}

/**
* sign
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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.AlwaysNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullLiteral;
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;

/**
* gamma scala function
*/
public class Gamma extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(DoubleType.INSTANCE).args(DoubleType.INSTANCE)
);

/**
* constructor with 1 argument.
*/
public Gamma(Expression arg) {
super("gamma", arg);
}

/**
* withChildren.
*/
@Override
public Gamma withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
return new Gamma(children.get(0));
}

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitGamma(this, context);
}
}
Original file line number Diff line number Diff line change
@@ -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.AlwaysNullable;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullLiteral;
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;

/**
* Lgamma scala function
*/
public class Lgamma extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, AlwaysNullable, PropagateNullLiteral {
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(DoubleType.INSTANCE).args(DoubleType.INSTANCE)
);

/**
* constructor with 1 argument.
*/
public Lgamma(Expression arg) {
super("lgamma", arg);
}

/**
* withChildren.
*/
@Override
public Lgamma withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
return new Lgamma(children.get(0));
}

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitLgamma(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.FromIso8601Date;
import org.apache.doris.nereids.trees.expressions.functions.scalar.FromUnixtime;
import org.apache.doris.nereids.trees.expressions.functions.scalar.G;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Gamma;
import org.apache.doris.nereids.trees.expressions.functions.scalar.GetJsonBigInt;
import org.apache.doris.nereids.trees.expressions.functions.scalar.GetJsonDouble;
import org.apache.doris.nereids.trees.expressions.functions.scalar.GetJsonInt;
Expand Down Expand Up @@ -287,6 +288,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.Least;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Left;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Length;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Lgamma;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Ln;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Locate;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Log;
Expand Down Expand Up @@ -1271,6 +1273,10 @@ default R visitFromUnixtime(FromUnixtime fromUnixtime, C context) {
return visitScalarFunction(fromUnixtime, context);
}

default R visitGamma(Gamma gamma, C context) {
return visitScalarFunction(gamma, context);
}

default R visitGetJsonDouble(GetJsonDouble getJsonDouble, C context) {
return visitScalarFunction(getJsonDouble, context);
}
Expand Down Expand Up @@ -1587,6 +1593,10 @@ default R visitLength(Length length, C context) {
return visitScalarFunction(length, context);
}

default R visitLgamma(Lgamma lgamma, C context) {
return visitScalarFunction(lgamma, context);
}

default R visitCrc32(Crc32 crc32, C context) {
return visitScalarFunction(crc32, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,42 @@
0.0 false 8
0.0 false 9

-- !gamma_1 --
\N true

-- !gamma_2 --
24.0 false

-- !gamma_3 --
\N true 0
\N true 1
\N true 2
\N true 3
\N true 4
\N true 5
\N true 6
\N true 7
\N true 8
\N true 9

-- !lgamma_1 --
\N true

-- !lgamma_2 --
3.1780538303479458 false

-- !lgamma_3 --
\N true 0
\N true 1
\N true 2
\N true 3
\N true 4
\N true 5
\N true 6
\N true 7
\N true 8
\N true 9

-- !sqrt_1 --
\N true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,25 @@ suite("fold_constant_numeric_arithmatic") {
testFoldConst("SELECT FLOOR(10.123, 2)") // Decimal with precision
testFoldConst("SELECT FLOOR(-10.123, 1)") // Negative with precision

//Gamma function cases
testFoldConst("SELECT gamma(1) AS gamma_case_1")
testFoldConst("SELECT gamma(5) AS gamma_case_2")
testFoldConst("SELECT gamma(0.5) AS gamma_case_3")
testFoldConst("SELECT gamma(-1.5) AS gamma_case_4")
testFoldConst("SELECT gamma(0) AS gamma_case_zero")
testFoldConst("SELECT gamma(-1) AS gamma_case_neg_int")
testFoldConst("SELECT gamma(1.5), gamma(2.5), gamma(3.5), gamma(4.5)")
testFoldConst("SELECT gamma(-0.5), gamma(-1.5), gamma(-2.5)")
testFoldConst("SELECT gamma(NULL) AS gamma_case_null")

//Lgamma functio cases
testFoldConst("SELECT lgamma(1) AS lgamma_case_1")
testFoldConst("SELECT lgamma(5) AS lgamma_case_2")
testFoldConst("SELECT lgamma(0.5) AS lgamma_case_3")
testFoldConst("SELECT lgamma(0) AS lgamma_case_zero")
testFoldConst("SELECT lgamma(-1) AS lgamma_case_neg")
testFoldConst("SELECT lgamma(NULL) AS gamma_case_null")

//Fmod function cases
testFoldConst("SELECT MOD(10.5, 3.2) AS fmod_case_1") //fmod(10.5 % 3.2)
testFoldConst("SELECT MOD(-10.5, 3.2) AS fmod_case_2") //fmod(-10.5 % 3.2)
Expand Down
Loading