Repository navigation
Conversation
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.
init-js
force-pushed
the
fix-wide-binary-decimal
branch
from
November 17, 2026 00:33
eb752bf to
8156629
Compare
init-js
marked this pull request as ready for review
November 17, 2026 00:35
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
0x00or0xFFbytes, and the file is still valid. Reading aDECIMAL(38, 0)that holds9in 19 bytes panics.The Arrow reader converts each value with
sign_extend_be::<16>forDecimal128andsign_extend_be::<32>forDecimal256(byte_array.rs#L121-L144). That function asserts the input is at mostNbytes (bit_util.rs#L42-L43), so reading aDECIMAL(38, 0)that holds9in 19 bytes panics:The same happens for
Decimal256past 32 bytes.StatisticsConverterhas the same assumption.from_bytes_to_i32,from_bytes_to_i64,from_bytes_to_i128andfrom_bytes_to_i256(statistics.rs#L41-L66) sign-extend the min and max bytes to the width of the Arrow type, sorow_group_mins,row_group_maxes,data_page_minsanddata_page_maxespanic 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 inStatisticsConverter, becausesign_extend_bereadsb[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_besign-extends likesign_extend_be, but also accepts input longer thanNbytes when the extra leading bytes only repeat the sign. It returnsNonewhenbis empty or the value does not fit inNbytes.ByteArrayReaderconvertsDecimal128andDecimal256through one helper that usestry_sign_extend_be. Values that fit read back unchanged. A value that does not fit returnsBYTE_ARRAY decimal value of {n} bytes is out of range for {data_type}instead of panicking.StatisticsConverter'sfrom_bytes_to_*functions usetry_sign_extend_beand returnOption. A min or max that does not fit the Arrow type becomes null, as anINT64statistic that does not fit aDecimal32already does (statistics.rs#L296-L299, #L1165).The path for values of normal width is unchanged apart from one length comparison.
The
FIXED_LEN_BYTE_ARRAYreader 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::testswriteBYTE_ARRAYdecimal columns and read them back:Decimal128(9 and -9 in 19 bytes,i128::MINin 17 bytes) andDecimal256(5 in 34 bytes,i256::MINin 40 bytes), with nulls;Decimal128orDecimal256, which now return an error.A new test in
tests/arrow_reader/statistics.rswrites sign-extendedBYTE_ARRAYdecimals with page statistics, and checks the row group and data page mins and maxes forDecimal32,Decimal64,Decimal128andDecimal256, including a max that only fits the wider types, and an empty min and max, which become null.All three fail on
mainand pass with this change. A unit test fortry_sign_extend_becovers 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 toStatisticsConverter, drafted the tests and this description, and ran the tests againstmainand 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.