Skip to content

Commit 4a7513d

Browse files
Mohammad LinjawiMohammad Linjawi
authored andcommitted
feat: Add ANSI mode support for Spark arithmetic operations
Add ANSI mode support for arithmetic operations (add, subtract, multiply, divide, unary minus) and interval arithmetic in Spark dialect. In ANSI mode: - Overflow throws errors instead of wrapping around - Division by zero throws errors instead of returning NULL - MIN_VALUE / -1 overflow throws errors In non-ANSI mode (default): - Overflow wraps around (two's complement) - Division by zero returns NULL - MIN_VALUE / -1 returns MIN_VALUE Key changes: - Add IntegerDivideFunction with ANSI overflow and division-by-zero checks - Update DivideFunction to support both float and double - Add ANSI overflow checks to Add, Subtract, Multiply functions - Add ANSI support for interval arithmetic (IntervalDayTime, IntervalYearMonth) - Add comprehensive tests for all arithmetic operations - Update documentation with ANSI behavior examples Addresses: apache/gluten#10134
1 parent f1f6e8e commit 4a7513d

File tree

4 files changed

+1085
-23
lines changed

4 files changed

+1085
-23
lines changed

velox/docs/functions/spark/math.rst

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,16 @@ Mathematical Functions
4646
4747
Returns inverse hyperbolic tangent of ``x``.
4848

49-
.. spark:function:: add(x, y) -> [same as x]
49+
.. spark:function:: add(x, y) -> [same as x] (ANSI compliant)
5050
5151
Returns the result of adding x to y. The types of x and y must be the same.
52-
Corresponds to sparks's operator ``+``.
52+
For integral types, overflow returns NULL when ANSI mode is disabled;
53+
throws an error otherwise.
54+
55+
Corresponds to Spark's operator ``+``. ::
56+
57+
SELECT 2147483647 + 1; -- -2147483648 (ANSI OFF) / ERROR (ANSI ON)
58+
SELECT 127 + 1; -- -128 (ANSI OFF) / ERROR (ANSI ON) for TINYINT
5359

5460
.. spark:function:: add(x, y) -> decimal
5561
@@ -138,12 +144,28 @@ Mathematical Functions
138144
.. spark:function:: divide(x, y) -> double
139145
140146
Returns the results of dividing x by y. Performs floating point division.
141-
Supported type is DOUBLE.
147+
Supported types are DOUBLE and FLOAT.
148+
Division by zero returns NULL (no error in any mode).
149+
Corresponds to Spark's operator ``/``. ::
150+
151+
SELECT 3.0 / 2.0; -- 1.5
152+
SELECT 2.0 / 2.0; -- 1.0
153+
SELECT 3.0 / 0.0; -- NULL
154+
155+
.. spark:function:: divide(x, y) -> [same as x] (ANSI compliant for integers)
156+
157+
Returns the results of dividing x by y. Performs integer division.
158+
Supported types are TINYINT, SMALLINT, INTEGER, and BIGINT.
159+
Division by zero returns NULL when ANSI mode is disabled; throws an error otherwise.
160+
Overflow (MIN_VALUE / -1) returns MIN_VALUE when ANSI mode is disabled; throws an error otherwise.
161+
142162
Corresponds to Spark's operator ``/``. ::
143163

144-
SELECT 3 / 2; -- 1.5
145-
SELECT 2L / 2L; -- 1.0
146-
SELECT 3 / 0; -- NULL
164+
SELECT 10 / 3; -- 3
165+
SELECT -10 / 3; -- -3
166+
SELECT 5 / 0; -- NULL (ANSI OFF) / ERROR (ANSI ON)
167+
SELECT -128 / -1; -- -128 (ANSI OFF) / ERROR (ANSI ON) for TINYINT
168+
SELECT -2147483648 / -1; -- -2147483648 (ANSI OFF) / ERROR (ANSI ON) for INTEGER
147169

148170
.. spark:function:: divide(x, y) -> decimal
149171
@@ -231,10 +253,16 @@ Mathematical Functions
231253
232254
Returns the square root of ``x``.
233255

234-
.. spark:function:: multiply(x, y) -> [same as x]
256+
.. spark:function:: multiply(x, y) -> [same as x] (ANSI compliant)
235257
236258
Returns the result of multiplying x by y. The types of x and y must be the same.
237-
Corresponds to Spark's operator ``*``.
259+
For integral types, overflow returns NULL when ANSI mode is disabled;
260+
throws an error otherwise.
261+
262+
Corresponds to Spark's operator ``*``. ::
263+
264+
SELECT 2147483647 * 2; -- -2 (ANSI OFF) / ERROR (ANSI ON) for INTEGER
265+
SELECT 127 * 2; -- -2 (ANSI OFF) / ERROR (ANSI ON) for TINYINT
238266

239267
.. spark:function:: multiply(x, y) -> [decimal]
240268
@@ -328,10 +356,16 @@ Mathematical Functions
328356
329357
Returns hyperbolic sine of ``x``.
330358

331-
.. spark:function:: subtract(x, y) -> [same as x]
359+
.. spark:function:: subtract(x, y) -> [same as x] (ANSI compliant)
332360
333361
Returns the result of subtracting y from x. The types of x and y must be the same.
334-
Corresponds to Spark's operator ``-``.
362+
For integral types, overflow returns NULL when ANSI mode is disabled;
363+
throws an error otherwise.
364+
365+
Corresponds to Spark's operator ``-``. ::
366+
367+
SELECT -2147483648 - 1; -- 2147483647 (ANSI OFF) / ERROR (ANSI ON) for INTEGER
368+
SELECT -128 - 1; -- 127 (ANSI OFF) / ERROR (ANSI ON) for TINYINT
335369

336370
.. spark:function:: subtract(x, y) -> decimal
337371
@@ -345,9 +379,16 @@ Mathematical Functions
345379
SELECT CAST(99999999999999999999999999999999.99998 as DECIMAL(38, 6)) - CAST(-0.00001 as DECIMAL(38, 5)); -- DECIMAL(38, 6) 99999999999999999999999999999999.999990
346380
SELECT CAST(-99999999999999999999999999999999990.0 as DECIMAL(38, 3)) - CAST(0.00001 as DECIMAL(38, 7)); -- DECIMAL(38, 6) NULL
347381

348-
.. spark:function:: unaryminus(x) -> [same as x]
382+
.. spark:function:: unaryminus(x) -> [same as x] (ANSI compliant)
383+
384+
Returns the negative of ``x``. For integral types, negating the minimum value
385+
returns the same minimum value when ANSI mode is disabled; throws an error otherwise.
386+
387+
Corresponds to Spark's unary operator ``-``. ::
349388

350-
Returns the negative of `x`. Corresponds to Spark's operator ``-``.
389+
SELECT -42; -- -42
390+
SELECT -(-128); -- -128 (ANSI OFF) / ERROR (ANSI ON) for TINYINT
391+
SELECT -(-2147483648); -- -2147483648 (ANSI OFF) / ERROR (ANSI ON) for INTEGER
351392

352393
.. spark:function:: unhex(x) -> varbinary
353394

0 commit comments

Comments
 (0)