Skip to content

Speed up JSON schema inference by ~2.8x#9494

Open
Rafferty97 wants to merge 6 commits intoapache:mainfrom
Rafferty97:json-schema
Open

Speed up JSON schema inference by ~2.8x#9494
Rafferty97 wants to merge 6 commits intoapache:mainfrom
Rafferty97:json-schema

Conversation

@Rafferty97
Copy link
Contributor

@Rafferty97 Rafferty97 commented Feb 28, 2026

Which issue does this PR close?

This PR fixes #9484, and also sets the groundwork for implementing #9482. It also delivers an approximate 2.8x speed to JSON schema inference.

I have refactored the code that infers the schema of JSON sources, specifically:

  • Simplify the type inference logic, removing special cases
  • Schema inference now consumes TapeDecoder, eliminating the need to materialise rows into serde_json::Values first
  • Use arena allocation for efficiency
  • Remove scalar-to-array coersion, as the actual JSON reader doesn't support it
  • Move ValueIter into its own module

Rationale for this change

While working on #9482, I saw a need and opportunity to refactor the schema inference code for JSON schemas. I also discovered the bug detailed in #9484.

These changes not only make the code more readible and predictable by eliminating a lot of special case handling, but make it trivial to create a new inference function for "single field" JSON reading.

They have also provided a significant performance boost to the schema inference functions. I added a simple benchmark for infer_json_schema, which yielded the following results on my machine, reflecting an approx. 2.8x speed up:

Before changes:
infer_json_schema/1000 time: [1.4443 ms 1.4616 ms 1.4793 ms]
thrpt: [85.336 MiB/s 86.366 MiB/s 87.401 MiB/s]

After changes:
infer_json_schema/1000 time: [517.79 µs 519.10 µs 520.54 µs]
thrpt: [242.51 MiB/s 243.18 MiB/s 243.80 MiB/s]
change:
time: [−64.919% −64.485% −64.043%] (p = 0.00 < 0.05)
thrpt: [+178.11% +181.57% +185.06%]

What changes are included in this PR?

At a glance:

  • An overhaul of arrow-json/src/reader/schema.rs
  • Removed mixed_arrays.json as it's no longer valid, and replaced mixed_arrays.json.gz with arrays.json.gz
  • Added a dependency on Bumpalo for arena allocation

Because this is a somewhat sizeable PR, I've done my best to break into a logical sequence of commits to hopefully assist with the review.

Are these changes tested?

Yes, the changes pass all existing unit tests - except for one intentionally removed due to the change in behaviour related to #9484 (removing scalar-to-array promotion).

I have also added an additional benchmark for the schema inference performance.

Are there any user-facing changes?

There are no API changes, except for the addition of the record_count method on ValueIter.

However, the error messages returned by infer_json_schema and its cousins will significantly change, with most of them condensed to a single "Expected {expected}, found {got}" template.

Finally, some files that used to generate a valid schema will now return errors. However, this is desirable because those files would have failed to be read by the actual JSON reader anyway - due to the lack of support for scalar-to-array promotion in the JSON reader. (See #9484)

@github-actions github-actions bot added the arrow Changes to the arrow crate label Feb 28, 2026
@Rafferty97 Rafferty97 changed the title Refactor and improve performance of JSON schema inference Speed up JSON schema inference by ~2.8x Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow Changes to the arrow crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON reader doesn't support scalar-to-list promotion, even though schema inference does

1 participant