-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-42045][SQL] ANSI SQL mode: Round/Bround should return an error…
… on integer overflow ### What changes were proposed in this pull request? In ANSI SQL mode, Round/Bround should return an error on integer overflow. Note this PR is for integer only. Once it is merge, I will create one follow-up PR for all the rest integral types: byte, short, and long. Also, the function ceil and floor accepts decimal type input, so there is no need to change them. ### Why are the changes needed? In ANSI SQL mode, integer overflow should cause error instead of returning an unreasonable result. For example, `round(2147483647, -1)` should return error instead of returning `-2147483646` ### Does this PR introduce _any_ user-facing change? Yes, in ANSI SQL mode, SQL function Round and Bround will return an error on integer overflow ### How was this patch tested? UT Closes #39546 from gengliangwang/fixRound. Authored-by: Gengliang Wang <[email protected]> Signed-off-by: Gengliang Wang <[email protected]>
- Loading branch information
1 parent
785f1bb
commit 4272112
Showing
8 changed files
with
381 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--IMPORT math.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
-- Round with integer input | ||
SELECT round(525, 1); | ||
SELECT round(525, 0); | ||
SELECT round(525, -1); | ||
SELECT round(525, -2); | ||
SELECT round(525, -3); | ||
SELECT round(2147483647, -1); | ||
SELECT round(-2147483647, -1); | ||
|
||
-- BRound with integer input | ||
SELECT bround(525, 1); | ||
SELECT bround(525, 0); | ||
SELECT bround(525, -1); | ||
SELECT bround(525, -2); | ||
SELECT bround(525, -3); | ||
SELECT bround(2147483647, -1); | ||
SELECT bround(-2147483647, -1); |
175 changes: 175 additions & 0 deletions
175
sql/core/src/test/resources/sql-tests/results/ansi/math.sql.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
-- Automatically generated by SQLQueryTestSuite | ||
-- !query | ||
SELECT round(525, 1) | ||
-- !query schema | ||
struct<round(525, 1):int> | ||
-- !query output | ||
525 | ||
|
||
|
||
-- !query | ||
SELECT round(525, 0) | ||
-- !query schema | ||
struct<round(525, 0):int> | ||
-- !query output | ||
525 | ||
|
||
|
||
-- !query | ||
SELECT round(525, -1) | ||
-- !query schema | ||
struct<round(525, -1):int> | ||
-- !query output | ||
530 | ||
|
||
|
||
-- !query | ||
SELECT round(525, -2) | ||
-- !query schema | ||
struct<round(525, -2):int> | ||
-- !query output | ||
500 | ||
|
||
|
||
-- !query | ||
SELECT round(525, -3) | ||
-- !query schema | ||
struct<round(525, -3):int> | ||
-- !query output | ||
1000 | ||
|
||
|
||
-- !query | ||
SELECT round(2147483647, -1) | ||
-- !query schema | ||
struct<> | ||
-- !query output | ||
org.apache.spark.SparkArithmeticException | ||
{ | ||
"errorClass" : "ARITHMETIC_OVERFLOW", | ||
"sqlState" : "22003", | ||
"messageParameters" : { | ||
"alternative" : "", | ||
"config" : "\"spark.sql.ansi.enabled\"", | ||
"message" : "Overflow" | ||
}, | ||
"queryContext" : [ { | ||
"objectType" : "", | ||
"objectName" : "", | ||
"startIndex" : 8, | ||
"stopIndex" : 28, | ||
"fragment" : "round(2147483647, -1)" | ||
} ] | ||
} | ||
|
||
|
||
-- !query | ||
SELECT round(-2147483647, -1) | ||
-- !query schema | ||
struct<> | ||
-- !query output | ||
org.apache.spark.SparkArithmeticException | ||
{ | ||
"errorClass" : "ARITHMETIC_OVERFLOW", | ||
"sqlState" : "22003", | ||
"messageParameters" : { | ||
"alternative" : "", | ||
"config" : "\"spark.sql.ansi.enabled\"", | ||
"message" : "Overflow" | ||
}, | ||
"queryContext" : [ { | ||
"objectType" : "", | ||
"objectName" : "", | ||
"startIndex" : 8, | ||
"stopIndex" : 29, | ||
"fragment" : "round(-2147483647, -1)" | ||
} ] | ||
} | ||
|
||
|
||
-- !query | ||
SELECT bround(525, 1) | ||
-- !query schema | ||
struct<bround(525, 1):int> | ||
-- !query output | ||
525 | ||
|
||
|
||
-- !query | ||
SELECT bround(525, 0) | ||
-- !query schema | ||
struct<bround(525, 0):int> | ||
-- !query output | ||
525 | ||
|
||
|
||
-- !query | ||
SELECT bround(525, -1) | ||
-- !query schema | ||
struct<bround(525, -1):int> | ||
-- !query output | ||
520 | ||
|
||
|
||
-- !query | ||
SELECT bround(525, -2) | ||
-- !query schema | ||
struct<bround(525, -2):int> | ||
-- !query output | ||
500 | ||
|
||
|
||
-- !query | ||
SELECT bround(525, -3) | ||
-- !query schema | ||
struct<bround(525, -3):int> | ||
-- !query output | ||
1000 | ||
|
||
|
||
-- !query | ||
SELECT bround(2147483647, -1) | ||
-- !query schema | ||
struct<> | ||
-- !query output | ||
org.apache.spark.SparkArithmeticException | ||
{ | ||
"errorClass" : "ARITHMETIC_OVERFLOW", | ||
"sqlState" : "22003", | ||
"messageParameters" : { | ||
"alternative" : "", | ||
"config" : "\"spark.sql.ansi.enabled\"", | ||
"message" : "Overflow" | ||
}, | ||
"queryContext" : [ { | ||
"objectType" : "", | ||
"objectName" : "", | ||
"startIndex" : 8, | ||
"stopIndex" : 29, | ||
"fragment" : "bround(2147483647, -1)" | ||
} ] | ||
} | ||
|
||
|
||
-- !query | ||
SELECT bround(-2147483647, -1) | ||
-- !query schema | ||
struct<> | ||
-- !query output | ||
org.apache.spark.SparkArithmeticException | ||
{ | ||
"errorClass" : "ARITHMETIC_OVERFLOW", | ||
"sqlState" : "22003", | ||
"messageParameters" : { | ||
"alternative" : "", | ||
"config" : "\"spark.sql.ansi.enabled\"", | ||
"message" : "Overflow" | ||
}, | ||
"queryContext" : [ { | ||
"objectType" : "", | ||
"objectName" : "", | ||
"startIndex" : 8, | ||
"stopIndex" : 30, | ||
"fragment" : "bround(-2147483647, -1)" | ||
} ] | ||
} |
Oops, something went wrong.