From b55ef9dbaa2a31c63baf0f4b233326f3e5d6b913 Mon Sep 17 00:00:00 2001 From: zhli1142015 Date: Mon, 6 Jan 2025 15:07:39 -0800 Subject: [PATCH] feat: Support decimal type for Spark floor function (#11951) Summary: Gluten removed the registration of Presto sql functions. This PR registers Presto floor function in Spark for reuse. Pull Request resolved: https://github.com/facebookincubator/velox/pull/11951 Reviewed By: bikramSingh91 Differential Revision: D67867355 Pulled By: kevinwilfong fbshipit-source-id: a19c35ba0ccd4684b3dc58271fcfc62c029fbe43 --- velox/docs/functions/spark/decimal.rst | 9 +++++++++ velox/docs/functions/spark/math.rst | 2 +- velox/expression/fuzzer/SparkExpressionFuzzerTest.cpp | 4 ++++ velox/functions/sparksql/registration/RegisterMath.cpp | 2 ++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/velox/docs/functions/spark/decimal.rst b/velox/docs/functions/spark/decimal.rst index 654f057b2607..7c9a1a006211 100644 --- a/velox/docs/functions/spark/decimal.rst +++ b/velox/docs/functions/spark/decimal.rst @@ -114,12 +114,20 @@ Returns NULL when the actual result cannot be represented with the calculated de Decimal Functions ----------------- +.. spark:function:: floor(x: decimal(p, s)) -> r: decimal(pr, 0) + + Returns ``x`` rounded down to the type ``decimal(min(38, p - s + min(1, s)), 0)``. + + :: + + SELECT floor(cast(1.23 as DECIMAL(3, 2))); -- 1 // Output type: decimal(2,0) .. spark:function:: unaryminus(x: decimal(p, s)) -> r: decimal(p, s) Returns negated value of x (r = -x). Corresponds to Spark's operator ``-``. :: + SELECT unaryminus(cast(-9999999999999999999.9999999999999999999 as DECIMAL(38, 19))); -- 9999999999999999999.9999999999999999999 .. spark:function:: unscaled_value(x) -> bigint @@ -145,6 +153,7 @@ Decimal Special Forms After rounding we may need one more digit in the integral part. :: + SELECT (round(cast (9.9 as decimal(2, 1)), 0)); -- decimal 10 SELECT (round(cast (99 as decimal(2, 0)), -1)); -- decimal 100 diff --git a/velox/docs/functions/spark/math.rst b/velox/docs/functions/spark/math.rst index 99b23144d1d1..b1fd8c1c3114 100644 --- a/velox/docs/functions/spark/math.rst +++ b/velox/docs/functions/spark/math.rst @@ -143,7 +143,7 @@ Mathematical Functions .. spark:function:: floor(x) -> [same as x] Returns ``x`` rounded down to the nearest integer. - Supported types are: BIGINT and DOUBLE. + Supported types are: BIGINT, DOUBLE and DECIMAL. .. spark:function:: hex(x) -> varchar diff --git a/velox/expression/fuzzer/SparkExpressionFuzzerTest.cpp b/velox/expression/fuzzer/SparkExpressionFuzzerTest.cpp index 60e1af2281ed..7e07f1232273 100644 --- a/velox/expression/fuzzer/SparkExpressionFuzzerTest.cpp +++ b/velox/expression/fuzzer/SparkExpressionFuzzerTest.cpp @@ -24,6 +24,7 @@ #include "velox/exec/fuzzer/ReferenceQueryRunner.h" #include "velox/expression/fuzzer/FuzzerRunner.h" +#include "velox/functions/prestosql/fuzzer/FloorAndRoundArgGenerator.h" #include "velox/functions/sparksql/fuzzer/AddSubtractArgGenerator.h" #include "velox/functions/sparksql/fuzzer/DivideArgGenerator.h" #include "velox/functions/sparksql/fuzzer/MakeTimestampArgGenerator.h" @@ -92,6 +93,9 @@ int main(int argc, char** argv) { {"divide", std::make_shared(true)}, {"divide_deny_precision_loss", std::make_shared(false)}, + {"floor", + std::make_shared< + facebook::velox::exec::test::FloorAndRoundArgGenerator>()}, {"unscaled_value", std::make_shared()}, {"make_timestamp", std::make_shared()}}; diff --git a/velox/functions/sparksql/registration/RegisterMath.cpp b/velox/functions/sparksql/registration/RegisterMath.cpp index 0532c5a82055..3d1d2201f36a 100644 --- a/velox/functions/sparksql/registration/RegisterMath.cpp +++ b/velox/functions/sparksql/registration/RegisterMath.cpp @@ -15,6 +15,7 @@ */ #include "velox/functions/lib/RegistrationHelpers.h" #include "velox/functions/prestosql/Arithmetic.h" +#include "velox/functions/prestosql/DecimalFunctions.h" #include "velox/functions/sparksql/Arithmetic.h" #include "velox/functions/sparksql/DecimalArithmetic.h" #include "velox/functions/sparksql/Rand.h" @@ -75,6 +76,7 @@ void registerMathFunctions(const std::string& prefix) { {prefix + "floor"}); registerFunction( {prefix + "floor"}); + registerDecimalFloor(prefix); registerFunction({prefix + "hypot"}); registerFunction({prefix + "log2"}); registerFunction({prefix + "log10"});