← Back to Pull requests

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

Open
RajaBabu15 opened this pull request with 241 lines diff across 3 files
View on GitHub
RajaBabu15 RajaBabu15 commented on Nov 29, 2026 13:57 UTC
Author

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:

  • The message has no column path; the builders do not know it.

  • A list or object column whose null-only block is built first still fails with NotImplemented. That is GH-20253.
  • 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
  • GitHub Issue: Closes #55833
  • Diff: 241 lines (+238 / -3) across 3 files Reviewer: Reranko05 Review duration: 1672m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.39) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.39 · 241 lines diff across 3 files
    0.39
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.94 · 3 substantive comment(s)
    0.94
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    91%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.328
    Real PR Conversation Timeline
    5 items
    RajaBabu15 RajaBabu15 commented on Nov 29, 2026 14:12 UTC
    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 commented on Nov 29, 2026 17:41 UTC
    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.

    pitrou pitrou commented on Nov 30, 2026 09:44 UTC
    Contributor

    @taepper @Reranko05 Would you like to review this?

    Reranko05 Reranko05 commented on Nov 30, 2026 11:11 UTC
    Reviewer

    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.

    Reviewers
    Assignees
    RajaBabu15 RajaBabu15
    Labels
    risk: low confidence: 91.2% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 38.8% (MEDIUM) · Higher = larger blast radius
    38.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 91.2% · Lower = rushed or shallow review
    91.2%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 3.4% · Safe to merge
    3.4%
    Repository