Skip to content

Commit

Permalink
Merge #86106
Browse files Browse the repository at this point in the history
86106: sql: make round builtin avoid negative zeros r=rafiss a=nnaka2992

fixes  #28404

Previously, the round function could return -0.
In PostgreSQL, the function returns 0 without a negative sign,
so this commit changes the behavior of round to match that.

Release note (sql change): The round(decimal) builtin function
no longer returns negative 0 for any input.

Co-authored-by: nnaka2992 <[email protected]>
  • Loading branch information
craig[bot] and nnaka2992 committed Nov 16, 2022
2 parents 9184a8a + 9debf4b commit 5961b18
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/sql/logictest/testdata/logic_test/builtin_function
Original file line number Diff line number Diff line change
Expand Up @@ -1187,10 +1187,10 @@ SELECT round(123.456::float, -1), round(123.456::float, -2), round(123.456::floa
----
120 100 0

query RRRR
SELECT round(123.456::decimal, -1), round(123.456::decimal, -2), round(123.456::decimal, -3), round(123.456::decimal, -200)
query RRRRR
SELECT round(123.456::decimal, -1), round(123.456::decimal, -2), round(123.456::decimal, -3), round(123.456::decimal, -200), round(-0.1::decimal)
----
1.2E+2 1E+2 0E+3 0E+200
1.2E+2 1E+2 0E+3 0E+200 0

query RRRR
SELECT round('nan'::decimal), round('nan'::decimal, 1), round('nan'::float), round('nan'::float, 1)
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/sem/builtins/math_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,9 @@ func roundDDecimal(d *tree.DDecimal, scale int32) (tree.Datum, error) {
func roundDecimal(x *apd.Decimal, scale int32) (tree.Datum, error) {
dd := &tree.DDecimal{}
_, err := tree.HighPrecisionCtx.Quantize(&dd.Decimal, x, -scale)
if dd.IsZero() {
dd.Negative = false
}
return dd, err
}

Expand Down

0 comments on commit 5961b18

Please sign in to comment.