Skip to content

Stop rounding numeric[] values without a precision to 3 decimal places - #966

Merged
sfc-gh-agedemenli merged 2 commits into
mainfrom
agedemenli/numeric-array-typmod
Nov 30, 2026
Merged

sfc-gh-agedemenli merged 2 commits into
mainfrom
agedemenli/numeric-array-typmod

Conversation

@sfc-gh-agedemenli

@sfc-gh-agedemenli sfc-gh-agedemenli commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

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[] as DECIMAL(18,3)[]. #910 fixed this for single values, but arrays were still sent that way: numeric[] parameters, array constants, and IN lists 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 where n is 1.5, because 1.5001 was rounded to 1.500.

The fix

  • A 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.
  • In n = ANY (array), including a folded IN list, the array takes the scale of n when n has more than 9 decimal places, so neither side is rounded.
  • A numeric(p,s)[] relabeled as numeric[] keeps its own precision and scale.

The numeric[] limitation is removed from docs/data-types.md.

Tests

test_unbounded_numeric.py no longer skips the IN list case without pushdown, and adds n IN (1.5001, 7.07), n = ANY of an array constant with a value at scale 20, and a numeric(10,2) column compared with an array value that has more decimal places. The test fails without the change.

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-agedemenli sfc-gh-agedemenli changed the title Send unconstrained numeric[] values to DuckDB with a precision and scale Stop rounding numeric[] values without a precision to 3 decimal places Nov 27, 2026

@sfc-gh-mslot sfc-gh-mslot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked on a local build: COALESCE, CASE and GREATEST keep 1e-20 now and the = ANY cases still match. Thanks!

Comment thread docs/data-types.md
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[]`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 "

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the new cases cover it.

@sfc-gh-mslot sfc-gh-mslot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@sfc-gh-agedemenli
sfc-gh-agedemenli force-pushed the agedemenli/numeric-array-typmod branch from 64783ed to 69c2d9d Compare November 30, 2026 10:50
@sfc-gh-agedemenli
sfc-gh-agedemenli merged commit 555f8f0 into main Nov 30, 2026
90 checks passed
@sfc-gh-agedemenli
sfc-gh-agedemenli deleted the agedemenli/numeric-array-typmod branch November 30, 2026 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants