Skip to content

Commit

Permalink
Adding sin, cos, tan, cosh and tanh presto functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit Dutta committed Sep 27, 2021
1 parent 5350427 commit 003418d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
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

0 comments on commit 003418d

Please sign in to comment.