8000 fix(parquet): read a sign-extended BYTE_ARRAY decimal without panic by init-js · Pull Request #2 · init-js/arrow-rs · GitHub
[go: up one dir, main page]

Skip to content

fix(parquet): read a sign-extended BYTE_ARRAY decimal without panic - #2

Open
init-js wants to merge 1 commit into
mainfrom
fix-wide-binary-decimal
Open

init-js wants to merge 1 commit into
mainfrom
fix-wide-binary-decimal

Conversation

@init-js
@init-js init-js commented Nov 16, 2026 •
Copy link
Copy Markdown
Owner

Which issue does this PR close?

Issue not opened yet.

Rationale for this change

The spec does not limit the size of the backing array for DECIMAL. (LogicalTypes.md#L241-L242). A writer can therefore sign-extend a value with redundant leading 0x00 or 0xFF bytes, and the file is still valid. Reading a DECIMAL(38, 0) that holds 9 in 19 bytes panics.

The Arrow reader converts each value with sign_extend_be::<16> for Decimal128 and sign_extend_be::<32> for Decimal256 (byte_array.rs#L121-L144). That function asserts the input is at most N bytes (bit_util.rs#L42-L43), so reading a DECIMAL(38, 0) that holds 9 in 19 bytes panics:

Array too large, expected less than 16

The same happens for Decimal256 past 32 bytes.

StatisticsConverter has the same assumption. from_bytes_to_i32, from_bytes_to_i64, from_bytes_to_i128 and from_bytes_to_i256 (statistics.rs#L41-L66) sign-extend the min and max bytes to the width of the Arrow type, so row_group_mins, row_group_maxes, data_page_mins and data_page_maxes panic on such a file for every Arrow decimal type.

A value that genuinely needs more bytes than the Arrow type has, such as 2^127 in a DECIMAL(38, 0), also panics today. It cannot be represented, so reading it should be an error, and its statistics should be unknown. An empty min or max also panics in StatisticsConverter, because sign_extend_be reads b[0]; a 0-byte value is not a valid two's complement integer, so its statistics should be unknown too.

What changes are included in this PR?

  • bit_util::try_sign_extend_be sign-extends like sign_extend_be, but also accepts input longer than N bytes when the extra leading bytes only repeat the sign. It returns None when b is empty or the value does not fit in N bytes.
  • ByteArrayReader converts Decimal128 and Decimal256 through one helper that uses try_sign_extend_be. Values that fit read back unchanged. A value that does not fit returns BYTE_ARRAY decimal value of {n} bytes is out of range for {data_type} instead of panicking.
  • StatisticsConverter's from_bytes_to_* functions use try_sign_extend_be and return Option. A min or max that does not fit the Arrow type becomes null, as an 8000 INT64 statistic that does not fit a Decimal32 already does (statistics.rs#L296-L299, #L1165).

The path for values of normal width is unchanged apart from one length comparison.

The FIXED_LEN_BYTE_ARRAY reader does not hit this panic: the column width selects an Arrow type that holds it, and a narrower schema hint is rejected with an error.

Are these changes tested?

Yes. New tests in arrow::array_reader::byte_array::tests write BYTE_ARRAY decimal columns and read them back:

  • sign-extended values for Decimal128 (9 and -9 in 19 bytes, i128::MIN in 17 bytes) and Decimal256 (5 in 34 bytes, i256::MIN in 40 bytes), with nulls;
  • values that do not fit Decimal128 or Decimal256, which now return an error.

A new test in tests/arrow_reader/statistics.rs writes sign-extended BYTE_ARRAY decimals with page statistics, and checks the row group and data page mins and maxes for Decimal32, Decimal64, Decimal128 and Decimal256, including a max that only fits the wider types, and an empty min and max, which become null.

All three fail on main and pass with this change. A unit test for try_sign_extend_be covers the boundary cases, where the dropped bytes look like sign bytes but do not match the sign of the kept bytes.

Are there any significant user-facing changes?

No API change.

AI usage disclosure

The fix started from a patch I wrote and use internally. An AI assistant (Claude) adapted it to current main, extended it to StatisticsConverter, drafted the tests and this description, and ran the tests against main and this branch. I reviewed the changes and chose the behaviour for values that do not fit: a read error in the reader, and a null statistic.

A BYTE_ARRAY decimal is a big-endian two's complement integer of any
length, and a writer may sign-extend it past the fewest bytes it needs.
The Arrow reader and StatisticsConverter converted each value with
sign_extend_be, which panics on more bytes than the Arrow decimal type
holds, so reading a DECIMAL(38, 0) holding 9 in 19 bytes, or its
statistics, panicked. An empty min or max also panicked.

Both now drop leading bytes that only repeat the sign, so such a value
converts unchanged. A value that does not fit the Arrow type is a read
error in the reader, and a null min or max in StatisticsConverter, as
for INT64 statistics that do not fit a Decimal32. An empty min or max
is null too.
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.

1 participant

0