Skip to content

Support DECIMAL values wider than 38 digits - #1021

Merged
staticlibs merged 10 commits into
duckdb:mainfrom
staticlibs:wide-decimal
Dec 2, 2026
Merged

staticlibs merged 10 commits into
duckdb:mainfrom
staticlibs:wide-decimal

Conversation

@staticlibs

Copy link
Copy Markdown
Member

This PR is a continuation of #1005

h4nsmuller and others added 10 commits December 2, 2026 10:23
DuckDB stores the unscaled value of a DECIMAL wider than 38 digits as a
BIGNUM blob. Such decimals appear in a default session (for example as
the result of DECIMAL(38,2) + DECIMAL(38,2)), the driver did not handle
them:

- a BigDecimal parameter with more than 38 digits was bound as NULL
  (setBigDecimal, setObject, single value appender)
- fetching a wide DECIMAL column failed with an internal error, the
  result set and parameter metadata reported it as an unknown type
- DuckDBValue#getBigDecimal returned zero for values beyond 128 bits
- UDF vectors and the appender rejected the wide storage type
- DuckDBLogicalType#decimal rejected widths above 38

The values are now passed between JNI and Java in the BIGNUM form and
are converted to and from BigDecimal exactly in DuckDBBignum.

DatabaseMetaData#getTypeInfo now reports the max_decimal_width setting
as the DECIMAL precision (it was 0).
The engine applies max(max_decimal_width, 38) as the DECIMAL width limit
and the setting has no lower bound, so with a setting below 38
getTypeInfo reported a PRECISION and a MAXIMUM_SCALE lower than the
widths that the session accepts.
The wide DECIMAL support changed the public type_size, width and scale
fields and the public constructor from short to int, that breaks the
code that was compiled against the previous versions of the driver.

The fields and the constructor are short again. A width or a scale that
does not fit a short is clamped there to Short.MAX_VALUE, the full
values are available from the new getWidth() and getScale() methods
that the driver itself now uses.
The width and the scale of a DECIMAL are 32-bit unsigned in the engine.
The JDBC result and parameter metadata parsed them with
Integer.parseInt, so with max_decimal_width above INT32_MAX a query over
a DECIMAL(3000000000,0) column failed with NumberFormatException while
the result set metadata or the vectors were created, the result could
not be read at all.

Such values are now clamped to Integer.MAX_VALUE, the same way as it is
done in the duckdb_decimal_width and duckdb_decimal_scale bindings.
A BigDecimal parameter with more than 38 digits was cast to
DECIMAL(precision, scale) without the client context, so the
max_decimal_width setting of the session was not applied to it. With
max_decimal_width = 38, that is meant to preserve the types that existed
before the wide decimals, a 50 digit parameter was bound as
DECIMAL(50,3), and a very large BigDecimal was materialised as a DECIMAL
of any width.

A parameter is now refused like the engine refuses a DECIMAL literal
that is wider than the setting, the check is done before the value is
converted to a string. The setting is read when the parameter is bound,
values below 38 are the same as 38.

The values of the single value appender are not checked, they are cast
to the type of an existing column.
get_wide_decimal read the scale of a DECIMAL with its own copy of the
logic of get_decimal_param. The helper is now declared in bindings.hpp
and is used in both translation units.
duckdb_get_decimal reached the BIGNUM storage of a wide DECIMAL only
after duckdb_v2_value_get_decimal failed. For a decimal whose unscaled
value does not fit in 128 bits that call throws and catches an exception
with a formatted message, so every DuckDBValue#getBigDecimal call on
such a value paid for it. The logical type and the scale were then read
once more.

The width of the DECIMAL is now checked first: a value that is wider
than 38 digits is read directly with value_get_blob, value_get_decimal
is only called for the fixed-size decimals. A NULL value is detected
with value_is_null instead of a failed value_get_blob call.
make_vec_byte_arrays filled the array of every row with
GetByteArrayElements, memcpy and ReleaseByteArrayElements, that pins the
new array or copies it out and back. The function is now also used to
fetch every wide DECIMAL column. It did not check the result of
NewByteArray, a failed allocation was followed by a null dereference.

The arrays are now filled with SetByteArrayRegion, that is a single
copy, and the allocations are checked. make_jbyteArray, that is used to
read the wide DECIMAL values in the C API bindings, is changed the same
way.
The BIGNUM blob of a wide DECIMAL is decoded in DuckDBBignum, that
throws an IllegalArgumentException for a blob that is malformed or
truncated. This unchecked exception was thrown from the JDBC getters
(getBigDecimal, getObject, getString and the numeric getters), the code
that handles SQLException did not see it. It was also thrown from
DuckDBReadableVector#getBigDecimal in the callbacks of the user defined
functions.

The JDBC getters now throw an SQLException with the new
VECTOR_DECIMAL_INVALID error code, DuckDBReadableVector throws a
FunctionException like it does for the other errors.
The driver had its own Java codec for the BIGNUM storage format (the
header layout and the complement of the negative values), that is used
for the unscaled values of the wide decimals. The C API v2 declares
this format opaque and provides duckdb_v2_bignum_decode and
duckdb_v2_bignum_encode to translate it, a change of the format in the
engine would silently corrupt the values of the driver.

The storage bytes are now only interpreted by these two functions on the
native side. The integers are passed between the native side and Java
as the big-endian two's-complement bytes of a BigInteger:

- the wide DECIMAL vectors of a JDBC result are decoded when the vector
  is converted, without additional JNI calls for the values
- DuckDBValue#getBigDecimal decodes the value on the native side
- the vectors of the user defined functions and the appender use the new
  duckdb_bignum_decode and duckdb_bignum_encode bindings, that report
  invalid input as an SQLException

The duckdb_get_bignum binding, that was a stub that returned null, is
implemented with the same conversion.
@staticlibs
staticlibs merged commit 895f0b4 into duckdb:main Dec 2, 2026
17 checks passed
@staticlibs
staticlibs deleted the wide-decimal branch December 2, 2026 11:00
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