Skip to content
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

Add sin, cos, tan, cosh and tanh Presto functions. #313

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 21 additions & 0 deletions velox/docs/functions/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,24 @@ Trigonometric Functions
.. function:: atan2(y, x) -> double

Returns the arc tangent of ``y / x``.

.. function:: cos(x) -> double

Returns the cosine of ``x``.

.. function:: cosh(x) -> double

Returns the hyperbolic cosine of ``x``.

.. function:: sin(x) -> double

Returns the sine of ``x``.

.. function:: tan(x) -> double

Returns the tangent of ``x``.

.. function:: tanh(x) -> double

Returns the hyperbolic tangent of ``x``.

35 changes: 35 additions & 0 deletions velox/functions/prestosql/Arithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,55 @@ FOLLY_ALWAYS_INLINE bool call(double& result, double a) {
}
VELOX_UDF_END();

VELOX_UDF_BEGIN(cos)
FOLLY_ALWAYS_INLINE bool call(double& result, double a) {
result = std::cos(a);
return true;
}
VELOX_UDF_END();

VELOX_UDF_BEGIN(cosh)
FOLLY_ALWAYS_INLINE bool call(double& result, double a) {
result = std::cosh(a);
return true;
}
VELOX_UDF_END();

VELOX_UDF_BEGIN(acos)
FOLLY_ALWAYS_INLINE bool call(double& result, double a) {
result = std::acos(a);
return true;
}
VELOX_UDF_END();

VELOX_UDF_BEGIN(sin)
FOLLY_ALWAYS_INLINE bool call(double& result, double a) {
result = std::sin(a);
return true;
}
VELOX_UDF_END();

VELOX_UDF_BEGIN(asin)
FOLLY_ALWAYS_INLINE bool call(double& result, double a) {
result = std::asin(a);
return true;
}
VELOX_UDF_END();

VELOX_UDF_BEGIN(tan)
FOLLY_ALWAYS_INLINE bool call(double& result, double a) {
result = std::tan(a);
return true;
}
VELOX_UDF_END();

VELOX_UDF_BEGIN(tanh)
FOLLY_ALWAYS_INLINE bool call(double& result, double a) {
result = std::tanh(a);
return true;
}
VELOX_UDF_END();

VELOX_UDF_BEGIN(atan)
FOLLY_ALWAYS_INLINE bool call(double& result, double a) {
result = std::atan(a);
Expand Down
5 changes: 5 additions & 0 deletions velox/functions/prestosql/RegisterArithmetic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ void registerArithmeticFunctions() {
{"clamp"});
registerFunction<udf_clamp<float>, float, float, float, float>({"clamp"});
registerFunction<udf_ln, double, double>({"ln"});
registerFunction<udf_cos, double, double>({"cos"});
registerFunction<udf_cosh, double, double>({"cosh"});
registerFunction<udf_acos, double, double>({"acos"});
registerFunction<udf_sin, double, double>({"sin"});
registerFunction<udf_asin, double, double>({"asin"});
registerFunction<udf_tan, double, double>({"tan"});
registerFunction<udf_tanh, double, double>({"tanh"});
registerFunction<udf_atan, double, double>({"atan"});
registerFunction<udf_atan2, double, double, double>({"atan2"});
registerFunction<udf_sqrt, double, double>({"sqrt"});
Expand Down
60 changes: 60 additions & 0 deletions velox/functions/prestosql/tests/ArithmeticTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,30 @@ TEST_F(ArithmeticTest, ln) {
EXPECT_EQ(std::nullopt, ln(std::nullopt));
}

TEST_F(ArithmeticTest, cos) {
const auto cosEval = [&](std::optional<double> a) {
return evaluateOnce<double>("cos(c0)", a);
};

for (double value : kDoubleValues) {
EXPECT_EQ(std::cos(value), cosEval(value));
}

EXPECT_EQ(std::nullopt, cosEval(std::nullopt));
}

TEST_F(ArithmeticTest, cosh) {
const auto coshEval = [&](std::optional<double> a) {
return evaluateOnce<double>("cosh(c0)", a);
};

for (double value : kDoubleValues) {
EXPECT_EQ(std::cosh(value), coshEval(value));
}

EXPECT_EQ(std::nullopt, coshEval(std::nullopt));
}

TEST_F(ArithmeticTest, acos) {
const auto acosEval = [&](std::optional<double> a) {
return evaluateOnce<double>("acos(c0)", a);
Expand All @@ -195,6 +219,18 @@ TEST_F(ArithmeticTest, acos) {
EXPECT_EQ(std::nullopt, acosEval(std::nullopt));
}

TEST_F(ArithmeticTest, sin) {
const auto sinEval = [&](std::optional<double> a) {
return evaluateOnce<double>("sin(c0)", a);
};

for (double value : kDoubleValues) {
EXPECT_EQ(std::sin(value), sinEval(value));
}

EXPECT_EQ(std::nullopt, sinEval(std::nullopt));
}

TEST_F(ArithmeticTest, asin) {
const auto asinEval = [&](std::optional<double> a) {
return evaluateOnce<double>("asin(c0)", a);
Expand All @@ -213,6 +249,30 @@ TEST_F(ArithmeticTest, asin) {
EXPECT_EQ(std::nullopt, asinEval(std::nullopt));
}

TEST_F(ArithmeticTest, tan) {
const auto tanEval = [&](std::optional<double> a) {
return evaluateOnce<double>("tan(c0)", a);
};

for (double value : kDoubleValues) {
EXPECT_EQ(std::tan(value), tanEval(value));
}

EXPECT_EQ(std::nullopt, tanEval(std::nullopt));
}

TEST_F(ArithmeticTest, tanh) {
const auto tanhEval = [&](std::optional<double> a) {
return evaluateOnce<double>("tanh(c0)", a);
};

for (double value : kDoubleValues) {
EXPECT_EQ(std::tanh(value), tanhEval(value));
}

EXPECT_EQ(std::nullopt, tanhEval(std::nullopt));
}

TEST_F(ArithmeticTest, atan) {
const auto atanEval = [&](std::optional<double> a) {
return evaluateOnce<double>("atan(c0)", a);
Expand Down