GH-55833: [C++] Return an error when a JSON field changes kind across blocks #55840
Notes from reading the code and related work, as context for this PR.
The tests hold one rule: a read split into blocks gives the same result as the same rows in one block. For type inference that means merging types must not depend on block order or grouping, null changes nothing, and a conflict stays a conflict.
Inside one block BlockParser enforces this. Its comment in parser.h says a change of JSON kind in a column is an error. Across blocks the chunked builders assume the kind has not changed. checked_cast is a dynamic_cast in debug and a static_cast in release, which is why a debug build stops on a failed check or std::bad_cast, while release crashes or returns wrong values.
One case goes further than the issue title. Numbers and strings reach the converters as the same array type, so on main (6f040e8) a string followed by a number in the next block is read as a string, while one block rejects it. A check on the array type cannot catch this. The parser already tags each struct field and list item with its JSON kind (json_kind), and Kind::ForType gives the kind a builder expects. The branch now uses that tag.
How other readers merge two different types:
Float64 and other mixed scalars to Utf8, turns list plus scalar into a list, and rejects object plus scalarThe same builders had earlier crash fixes around null and missing lists (ARROW-12065, ARROW-13871). A list or object after only nulls still fails, which is GH-20253.
Update: the builders now check the JSON kind between blocks. The PR description lists what is and is not covered.
@pitrou, could you review this when you have time? It fixes GH-55833: when a JSON field changed kind between blocks, read_json crashed or returned wrong values. It now raises an error, as a single-block read already does. The CI runs are waiting for approval.
@taepper @Reranko05 Would you like to review this?
I think the changes are more extensive than necessary to address the reported issue. Could you simplify the implementation? Adding runtime type checks before the unsafe casts in the chunked builders and converters should be sufficient for the reported cases. The regression tests could also be simplified by following the existing test patterns in reader_test.cc.
Reranko05
RajaBabu15
Rationale for this change
The JSON reader parses each block separately, and the parser checks that a field keeps one JSON kind only within a block. When a field changes kind in a later block,
read_jsoncan crash, or return wrong values with no error. With default options this can happen on any file just over 1 MiB. A single-block read of the same data reportschanged from X to Y.What changes are included in this PR?
The parser already tags each field with the JSON kind it saw. The chunked builders now compare that tag between blocks and return
JSON parse error: Column mixes <kind> and <kind> values across blocks. With two kinds the message is the same whichever block is processed first. Promotion within one kind still works: int64 to double, timestamp to string. The list and struct builders and the number and string converters check the array type before casting it.Not changed here:
NotImplemented. That is GH-20253.Are these changes tested?
New tests in
reader_test.cccompare a read split into one row per block with a single-block read, with and without threads, at the top level and inside lists and objects, and check that valid input still reads.Are there any user-facing changes?
Input that used to crash now raises an error. A field that mixes numbers and strings across blocks used to read as string, int64 or double; it now raises an error, as a single-block read already did. Other valid files read as before.
This PR contains a "Critical Fix". It fixes a crash and incorrect data produced from JSON input.
Was AI used for this PR?
In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.
PR code and description written by:
Reviewed before submission by: