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

feat: Support decimal type for Spark in function #11947

Open
wants to merge 4 commits into
base: main
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
2 changes: 1 addition & 1 deletion velox/docs/functions/spark/array.rst
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ Array Functions
.. spark:function:: in(value, array(E)) -> boolean

Returns true if value matches at least one of the elements of the array.
Supports BOOLEAN, REAL, DOUBLE, BIGINT, VARCHAR, TIMESTAMP, DATE input types.
Supports BOOLEAN, REAL, DOUBLE, BIGINT, VARCHAR, TIMESTAMP, DATE, DECIMAL input types.
zhli1142015 marked this conversation as resolved.
Show resolved Hide resolved

.. spark:function:: shuffle(array(E), seed) -> array(E)

Expand Down
4 changes: 4 additions & 0 deletions velox/docs/functions/spark/decimal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ Returns NULL when the actual result cannot be represented with the calculated de
Decimal Functions
-----------------

.. spark:function:: in(x: decimal(p, s), array(decimal(p, s))) -> boolean
Returns true if ``x`` matches at least one of the elements of the array.

.. spark:function:: unaryminus(x: decimal(p, s)) -> r: decimal(p, s)
Returns negated value of x (r = -x). Corresponds to Spark's operator ``-``.
Expand Down
2 changes: 2 additions & 0 deletions velox/functions/sparksql/In.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ void registerIn(const std::string& prefix) {
registerInFn<Varchar>(prefix);
registerInFn<Timestamp>(prefix);
registerInFn<Date>(prefix);
registerInFn<ShortDecimal<P1, S1>>(prefix);
registerInFn<LongDecimal<P1, S1>>(prefix);
}

} // namespace facebook::velox::functions::sparksql
36 changes: 36 additions & 0 deletions velox/functions/sparksql/tests/InTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,42 @@ TEST_F(InTest, Bool) {
EXPECT_EQ(in<bool>(false, {false}), true);
}

TEST_F(InTest, shortDecimal) {
EXPECT_EQ(in<int64_t>(1, {1, 2}, DECIMAL(2, 1)), true);
EXPECT_EQ(in<int64_t>(2, {1, 2}, DECIMAL(10, 5)), true);
EXPECT_EQ(in<int64_t>(3, {1, 2}, DECIMAL(17, 11)), false);
EXPECT_EQ(
in<int64_t>(
DecimalUtil::kShortDecimalMin,
{DecimalUtil::kShortDecimalMin, DecimalUtil::kShortDecimalMax},
DECIMAL(18, 9)),
true);
EXPECT_EQ(
in<int64_t>(
DecimalUtil::kShortDecimalMax,
{DecimalUtil::kShortDecimalMin, DecimalUtil::kShortDecimalMax},
DECIMAL(18, 9)),
true);
}

TEST_F(InTest, longDecimal) {
EXPECT_EQ(in<int128_t>(1, {1, 2}, DECIMAL(21, 2)), true);
EXPECT_EQ(in<int128_t>(2, {1, 2}, DECIMAL(29, 10)), true);
EXPECT_EQ(in<int128_t>(3, {1, 2}, DECIMAL(35, 20)), false);
EXPECT_EQ(
in<int128_t>(
DecimalUtil::kLongDecimalMin,
{DecimalUtil::kLongDecimalMin, DecimalUtil::kLongDecimalMax},
DECIMAL(38, 19)),
true);
EXPECT_EQ(
in<int128_t>(
DecimalUtil::kLongDecimalMax,
{DecimalUtil::kLongDecimalMin, DecimalUtil::kLongDecimalMax},
DECIMAL(38, 19)),
true);
}

TEST_F(InTest, Const) {
const auto eval = [&](const std::string& expr) {
return evaluateOnce<bool, bool>(expr, false);
Expand Down
Loading