Skip to content

Commit

Permalink
feat: Support decimal type for Spark floor function (#11951)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
zhli1142015 authored and facebook-github-bot committed Jan 6, 2025
1 parent b92e4bd commit b55ef9d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
9 changes: 9 additions & 0 deletions velox/docs/functions/spark/decimal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion velox/docs/functions/spark/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions velox/expression/fuzzer/SparkExpressionFuzzerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -92,6 +93,9 @@ int main(int argc, char** argv) {
{"divide", std::make_shared<DivideArgGenerator>(true)},
{"divide_deny_precision_loss",
std::make_shared<DivideArgGenerator>(false)},
{"floor",
std::make_shared<
facebook::velox::exec::test::FloorAndRoundArgGenerator>()},
{"unscaled_value", std::make_shared<UnscaledValueArgGenerator>()},
{"make_timestamp", std::make_shared<MakeTimestampArgGenerator>()}};

Expand Down
2 changes: 2 additions & 0 deletions velox/functions/sparksql/registration/RegisterMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -75,6 +76,7 @@ void registerMathFunctions(const std::string& prefix) {
{prefix + "floor"});
registerFunction<sparksql::FloorFunction, int64_t, double>(
{prefix + "floor"});
registerDecimalFloor(prefix);
registerFunction<HypotFunction, double, double, double>({prefix + "hypot"});
registerFunction<sparksql::Log2Function, double, double>({prefix + "log2"});
registerFunction<sparksql::Log10Function, double, double>({prefix + "log10"});
Expand Down

0 comments on commit b55ef9d

Please sign in to comment.