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

Implement Spark decimal multiply and divide #4613

Closed
Closed
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
1 change: 1 addition & 0 deletions CMake/resolve_dependency_modules/boost/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ set(BOOST_HEADER_ONLY
circular_buffer
math
multi_index
multiprecision
numeric_conversion
random
uuid
Expand Down
24 changes: 24 additions & 0 deletions velox/docs/functions/spark/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,25 @@ Mathematical Functions
.. spark:function:: divide(x, y) -> double

Returns the results of dividing x by y. Performs floating point division.
Supported type is DOUBLE.
Corresponds to Spark's operator ``/``. ::

SELECT 3 / 2; -- 1.5
SELECT 2L / 2L; -- 1.0
SELECT 3 / 0; -- NULL

.. spark:function:: divide(x, y) -> decimal

Returns the results of dividing x by y.
Supported type is DECIMAL which can be different precision and scale.
Performs floating point division.
The result type depends on the precision and scale of x and y.
Overflow results return null. Corresponds to Spark's operator ``/``. ::

SELECT CAST(1 as DECIMAL(17, 3)) / CAST(2 as DECIMAL(17, 3)); -- decimal 0.500000000000000000000
SELECT CAST(1 as DECIMAL(20, 3)) / CAST(20 as DECIMAL(20, 2)); -- decimal 0.0500000000000000000
SELECT CAST(1 as DECIMAL(20, 3)) / CAST(0 as DECIMAL(20, 3)); -- NULL

.. spark:function:: exp(x) -> double

Returns Euler's number raised to the power of ``x``.
Expand Down Expand Up @@ -89,6 +102,17 @@ Mathematical Functions
Returns the result of multiplying x by y. The types of x and y must be the same.
For integral types, overflow results in an error. Corresponds to Spark's operator ``*``.

.. spark:function:: multiply(x, y) -> [decimal]

Returns the result of multiplying x by y. The types of x and y must be decimal which can be different precision and scale.
The result type depends on the precision and scale of x and y.
Overflow results return null. Corresponds to Spark's operator ``*``. ::

SELECT CAST(1 as DECIMAL(17, 3)) * CAST(2 as DECIMAL(17, 3)); -- decimal 2.000000
SELECT CAST(1 as DECIMAL(20, 3)) * CAST(20 as DECIMAL(20, 2)); -- decimal 20.00000
SELECT CAST(1 as DECIMAL(20, 3)) * CAST(0 as DECIMAL(20, 3)); -- decimal 0.000000
SELECT CAST(201e-38 as DECIMAL(38, 38)) * CAST(301e-38 as DECIMAL(38, 38)); -- decimal 0.0000000000000000000000000000000000000

.. spark:function:: not(x) -> boolean

Logical not. ::
Expand Down
1 change: 1 addition & 0 deletions velox/functions/sparksql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ add_library(
ArraySort.cpp
Bitwise.cpp
Comparisons.cpp
DecimalArithmetic.cpp
Hash.cpp
In.cpp
LeastGreatest.cpp
Expand Down
Loading
Loading