Repository navigation
Stop rounding numeric[] values without a precision to 3 decimal places - #966
Conversation
A numeric[] without a precision, such as a parameter or a constant array, which a custom plan also folds an IN list into, was sent as numeric[], which DuckDB reads as DECIMAL(18,3)[]. Its elements were rounded to 3 decimal places, so without full query pushdown n IN (1.5001, 7.07) matched a row where n is 1.5. Such arrays now get the precision and scale of unbounded numeric columns, as single values do, and in n = ANY (array) the larger scale of n, so that neither side is rounded. A numeric(p,s)[] relabeled without a typmod keeps its own. Co-authored-by: Cursor <cursoragent@cursor.com> Signed-off-by: Ahmet Gedemenli <ahmet.gedemenli@snowflake.com>
sfc-gh-mslot
left a comment
There was a problem hiding this comment.
This fixes the IN list and = ANY($1) cases, I checked them on a local build in both plan and pushdown modes. One thing to decide on: an unconstrained numeric[] next to a numeric(38,20)[] column in COALESCE, CASE or GREATEST now rounds the column (see inline). Folding the array case into RaiseUnboundedNumericScale seems to fix that and drops the separate function.
| RaiseUnboundedNumericScale(lcons(linitial(arrayOpExpr->args), | ||
| list_copy(((ArrayExpr *) arrayArg)->elements))); | ||
| else | ||
| RaiseUnboundedNumericArrayScale(linitial(arrayOpExpr->args), arrayArg); |
There was a problem hiding this comment.
With na numeric(38,20)[] holding {1e-20}, coalesce(na, $1), coalesce(na, '{1}'::numeric[]), CASE and greatest(na, $1) now return {0.000000000}. Before this they were exact, since (18,3)[] left room for 20 decimals, so it is the same thing #910 fixed for scalars. Letting RaiseUnboundedNumericScale and IsUnboundedNumericDefaultCast accept numeric[] (the typmod is the element's), and calling it with list_make2(scalar, array) here, fixed those for me and kept the = ANY cases right, so RaiseUnboundedNumericArrayScale could go.
There was a problem hiding this comment.
Checked on a local build: COALESCE, CASE and GREATEST keep 1e-20 now and the = ANY cases still match. Thanks!
| Queries on lake tables compute with DuckDB decimals, which hold at most 38 digits. A `numeric` | ||
| without a precision in a query, such as a query parameter or a cast to `numeric`, is sent as | ||
| `numeric(38,9)` by default, so it is rounded to 9 decimal places. | ||
| `numeric(38,9)` by default, so it is rounded to 9 decimal places. The elements of a `numeric[]` |
There was a problem hiding this comment.
A text cast to numeric[] still goes out as ::numeric[]: n = ANY(($1::text)::numeric[]) fails with "can't be cast to DECIMAL(18,3)[]", and n = ANY(text_array_col::numeric[]) matches the 1.5 row for 1.5001. The CoerceViaIO branch only checks NUMERICOID, so that one is probably easy, the ArrayCoerceExpr case maybe just a line here.
There was a problem hiding this comment.
Fixed text_array_col::numeric[]: the array cast now gets the same treatment as other casts to numeric. ($1::text)::numeric[] isn't about the typmod though. It fails for any array type: ($1::text)::int[] gives "can't be cast to INTEGER[]" too, since we push down text-to-array casts and DuckDB can't parse '{...}'. I'll open a separate issue for that.
There was a problem hiding this comment.
Makes sense, I checked ($1::text)::numeric[] too and it is the {...} parsing, not the typmod. Thanks for filing #975.
|
|
||
| for table in ["heap", "lake"]: | ||
| run_command( | ||
| f"PREPARE {name}_{table}(numeric) AS " |
There was a problem hiding this comment.
All statements are prepared with (numeric), so the Param branch for arrays is not driven. Maybe add n = ANY($1) prepared with numeric[], and a numeric(38,20)[] column with coalesce(na, $1)?
There was a problem hiding this comment.
Thanks, the new cases cover it.
sfc-gh-mslot
left a comment
There was a problem hiding this comment.
The second commit fixes the array COALESCE/CASE/GREATEST and text[] cast cases. I re-ran my checks on a local build and everything this PR touches matches heap now. Looks good to me. The second commit is missing a Signed-off-by, so DCO is still failing.
An unconstrained numeric[] next to a numeric(38,20)[] column in COALESCE, CASE or GREATEST rounded the column to 9 decimal places, as numeric(38,9) did for scalars before. RaiseUnboundedNumericScale now takes numeric[] too, whose type modifier is that of its elements, and also handles = ANY, which replaces RaiseUnboundedNumericArrayScale. A cast of text[] to numeric[] was sent as ::numeric[], so DuckDB rounded the values to 3 decimal places. It is now cast like other casts to numeric. Tests prepare numeric[] parameters and cover a numeric(38,20)[] column. Co-authored-by: Cursor <cursoragent@cursor.com> Signed-off-by: Ahmet Gedemenli <ahmet.gedemenli@snowflake.com> Co-authored-by: Cursor <cursoragent@cursor.com>
64783ed to
69c2d9d
Compare
A follow-up to #910, as suggested in its review by @sfc-gh-mslot : a
numeric[]value without a precision was still rounded to 3 decimal places.The problem
DuckDB reads
numeric[]asDECIMAL(18,3)[]. #910 fixed this for single values, but arrays were still sent that way:numeric[]parameters, array constants, andINlists that PostgreSQL folds into an array constant in a custom plan. Without full query pushdown, for example,WHERE n IN (1.5001, 7.07)also matched a row wherenis 1.5, because 1.5001 was rounded to 1.500.The fix
numeric[]without a precision now gets the precision and scale of unbounded numeric columns,numeric(38,9)by default, as single values do since Send unconstrained numeric values to DuckDB with a precision and scale #910.n = ANY (array), including a foldedINlist, the array takes the scale ofnwhennhas more than 9 decimal places, so neither side is rounded.numeric(p,s)[]relabeled asnumeric[]keeps its own precision and scale.The
numeric[]limitation is removed fromdocs/data-types.md.Tests
test_unbounded_numeric.pyno longer skips theINlist case without pushdown, and addsn IN (1.5001, 7.07),n = ANYof an array constant with a value at scale 20, and anumeric(10,2)column compared with an array value that has more decimal places. The test fails without the change.