Skip to content

Commit b55ef9d

Browse files
zhli1142015facebook-github-bot
authored andcommitted
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: #11951 Reviewed By: bikramSingh91 Differential Revision: D67867355 Pulled By: kevinwilfong fbshipit-source-id: a19c35ba0ccd4684b3dc58271fcfc62c029fbe43
1 parent b92e4bd commit b55ef9d

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

velox/docs/functions/spark/decimal.rst

+9
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,20 @@ Returns NULL when the actual result cannot be represented with the calculated de
114114

115115
Decimal Functions
116116
-----------------
117+
.. spark:function:: floor(x: decimal(p, s)) -> r: decimal(pr, 0)
118+
119+
Returns ``x`` rounded down to the type ``decimal(min(38, p - s + min(1, s)), 0)``.
120+
121+
::
122+
123+
SELECT floor(cast(1.23 as DECIMAL(3, 2))); -- 1 // Output type: decimal(2,0)
117124

118125
.. spark:function:: unaryminus(x: decimal(p, s)) -> r: decimal(p, s)
119126
120127
Returns negated value of x (r = -x). Corresponds to Spark's operator ``-``.
121128

122129
::
130+
123131
SELECT unaryminus(cast(-9999999999999999999.9999999999999999999 as DECIMAL(38, 19))); -- 9999999999999999999.9999999999999999999
124132

125133
.. spark:function:: unscaled_value(x) -> bigint
@@ -145,6 +153,7 @@ Decimal Special Forms
145153
After rounding we may need one more digit in the integral part.
146154

147155
::
156+
148157
SELECT (round(cast (9.9 as decimal(2, 1)), 0)); -- decimal 10
149158
SELECT (round(cast (99 as decimal(2, 0)), -1)); -- decimal 100
150159

velox/docs/functions/spark/math.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ Mathematical Functions
143143
.. spark:function:: floor(x) -> [same as x]
144144
145145
Returns ``x`` rounded down to the nearest integer.
146-
Supported types are: BIGINT and DOUBLE.
146+
Supported types are: BIGINT, DOUBLE and DECIMAL.
147147

148148
.. spark:function:: hex(x) -> varchar
149149

velox/expression/fuzzer/SparkExpressionFuzzerTest.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "velox/exec/fuzzer/ReferenceQueryRunner.h"
2626
#include "velox/expression/fuzzer/FuzzerRunner.h"
27+
#include "velox/functions/prestosql/fuzzer/FloorAndRoundArgGenerator.h"
2728
#include "velox/functions/sparksql/fuzzer/AddSubtractArgGenerator.h"
2829
#include "velox/functions/sparksql/fuzzer/DivideArgGenerator.h"
2930
#include "velox/functions/sparksql/fuzzer/MakeTimestampArgGenerator.h"
@@ -92,6 +93,9 @@ int main(int argc, char** argv) {
9293
{"divide", std::make_shared<DivideArgGenerator>(true)},
9394
{"divide_deny_precision_loss",
9495
std::make_shared<DivideArgGenerator>(false)},
96+
{"floor",
97+
std::make_shared<
98+
facebook::velox::exec::test::FloorAndRoundArgGenerator>()},
9599
{"unscaled_value", std::make_shared<UnscaledValueArgGenerator>()},
96100
{"make_timestamp", std::make_shared<MakeTimestampArgGenerator>()}};
97101

velox/functions/sparksql/registration/RegisterMath.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
#include "velox/functions/lib/RegistrationHelpers.h"
1717
#include "velox/functions/prestosql/Arithmetic.h"
18+
#include "velox/functions/prestosql/DecimalFunctions.h"
1819
#include "velox/functions/sparksql/Arithmetic.h"
1920
#include "velox/functions/sparksql/DecimalArithmetic.h"
2021
#include "velox/functions/sparksql/Rand.h"
@@ -75,6 +76,7 @@ void registerMathFunctions(const std::string& prefix) {
7576
{prefix + "floor"});
7677
registerFunction<sparksql::FloorFunction, int64_t, double>(
7778
{prefix + "floor"});
79+
registerDecimalFloor(prefix);
7880
registerFunction<HypotFunction, double, double, double>({prefix + "hypot"});
7981
registerFunction<sparksql::Log2Function, double, double>({prefix + "log2"});
8082
registerFunction<sparksql::Log10Function, double, double>({prefix + "log10"});

0 commit comments

Comments
 (0)