Skip to content

GH-55833: [C++] Return an error when a JSON field changes kind across blocks - #55840

Open
RajaBabu15 wants to merge 3 commits into
apache:mainfrom
RajaBabu15:gh-55833-json-type-change
Open

RajaBabu15 wants to merge 3 commits into
apache:mainfrom
RajaBabu15:gh-55833-json-type-change

Conversation

@RajaBabu15

@RajaBabu15 RajaBabu15 commented Nov 29, 2026 •

Copy link
Copy Markdown

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_json can 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 reports changed 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:

Are these changes tested?

New tests in reader_test.cc compare 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:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

@RajaBabu15

RajaBabu15 commented Nov 29, 2026 •

Copy link
Copy Markdown
Author

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:

  • Arrow CSV loosens the type along a fixed chain that ends in binary
  • arrow-rs schema inference widens int plus float to Float64 and other mixed scalars to Utf8, turns list plus scalar into a list, and rejects object plus scalar
  • Spark JSON inference falls back to string

The 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.

@RajaBabu15
RajaBabu15 marked this pull request as ready for review November 29, 2026 14:13
@RajaBabu15 RajaBabu15 changed the title GH-55833: [C++] Return an error when a JSON list or object changes type GH-55833: [C++] Return an error when a JSON field changes kind across blocks Nov 29, 2026
@RajaBabu15
RajaBabu15 force-pushed the gh-55833-json-type-change branch from 4c723d5 to 5e6f122 Compare November 29, 2026 17:34
@RajaBabu15

Copy link
Copy Markdown
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++][Python] JSON reader segfaults or silently corrupts values when a field changes between array/object and scalar across blocks

1 participant