Skip to content

[Variant] Preserve fixed-point JSON numbers as decimals - #11177

Closed
osipovartem wants to merge 9 commits into
apache:mainfrom
Embucket:upstream-variant-json-decimal
Closed

osipovartem wants to merge 9 commits into
apache:mainfrom
Embucket:upstream-variant-json-decimal

Conversation

@osipovartem

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

What changes are included in this PR?

JsonToVariant::append_json now parses JSON directly into VariantBuilderExt while retaining exact numeric lexemes. The parser:

  • preserves the existing smallest-width integer encoding for values fitting in i64;
  • encodes fixed-point values and wider integers as the smallest fitting Decimal4, Decimal8, or Decimal16;
  • keeps exponent notation and values outside the Variant decimal range as Double;
  • applies the same rules recursively in arrays and objects;
  • borrows unescaped strings directly from the input and only allocates when JSON escapes must be decoded;
  • bounds nesting depth and does not commit a root value until the full input has been validated.

This deliberately avoids enabling serde_json's workspace-wide arbitrary_precision feature and removes the intermediate serde_json::Value tree from the string conversion path.

The first commit adds a defaulted fallible VariantBuilderExt::try_append_value method. This lets strict object builders report duplicate fields as ArrowError instead of panicking. It also fixes duplicate validation to check before mutating an object's field mapping, so the same builder remains usable after an error.

The existing append_json(&serde_json::Value, ...) API remains available. Its documentation now explains that non-integer numbers use Double because the original fixed-point lexeme is no longer available.

Are these changes tested?

Yes. This enables the existing Decimal4/8/16 tests and adds coverage for nested decimals, precision/scale boundaries, malformed JSON, Unicode escapes and surrogate pairs, duplicate fields, recursion limits, error rollback, and the documented difference between the string and Value APIs.

Local validation:

  • cargo +1.95.0 test -p parquet-variant --lib (175 passed)
  • cargo +1.95.0 test -p parquet-variant-json (76 unit tests and 6 doctests passed)
  • cargo +1.95.0 test -p parquet-variant-compute --lib (367 passed)
  • cargo +1.95.0 clippy -p parquet-variant-json --all-targets -- -D warnings
  • cargo +1.89.0 check -p parquet-variant-json --all-targets

A Criterion benchmark compares the direct parser with the previous Value-tree path. On this machine the direct path was faster for all covered inputs:

Input Direct Value tree Improvement
Integers 534 ns 644 ns 17%
Mixed object 711 ns 793 ns 10%
Fixed-point decimals 437 ns 474 ns 8%
Unescaped strings 394 ns 484 ns 19%
Escaped strings 527 ns 561 ns 6%
Nested object/list 850 ns 1,062 ns 20%

The decimal baseline encodes non-integer numbers as Double, while the direct path performs the new exact decimal conversion.

Are there any user-facing changes?

Yes. Parsing fixed-point JSON text into Variant now preserves exact decimal semantics and precision instead of converting those values to Double.

@github-actions github-actions Bot added the parquet-variant parquet-variant* crates label Sep 22, 2026
@osipovartem
osipovartem force-pushed the upstream-variant-json-decimal branch from 24a3aca to a936e5b Compare September 22, 2026 23:37
@osipovartem
osipovartem force-pushed the upstream-variant-json-decimal branch from a936e5b to d6db05a Compare September 23, 2026 08:42
@sdf-jkl

sdf-jkl commented Oct 5, 2026

Copy link
Copy Markdown
Member

Hi @osipovartem 👋 thanks for the PR

Could you please split this work into separate smaller PRs to make the review process easier?

I'd propose adding separate PRs for:

@osipovartem

Copy link
Copy Markdown
Contributor Author

Thanks, agreed. I split the fallible builder work into #11401, including regression coverage and an independent review. I will separate direct JSON parsing from fixed-point decimal conversion, move benchmarks to their own PR, and check the existing arrow-json decoder APIs before finalizing the parser change. I will update/close this combined PR as the replacements are ready.

@osipovartem

Copy link
Copy Markdown
Contributor Author

Quick correction to my earlier reply: #11401 overlapped with #11106, so I closed it. I extracted the distinct object-field rollback fix into #11403. I won't ask for review of this combined PR as-is; the remaining direct JSON parser, decimal conversion, and benchmarks still need separate PRs. I'll check the existing arrow-json decoder APIs while extracting those pieces, then close #11177 once its replacements are ready. Thanks for the clear split suggestion.

@osipovartem

osipovartem commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Split complete: the independent upstream PRs are #11106 (fallible builder), #11403 (object rollback), #11409 (direct JSON parser, draft), #11415 (exact decimal conversion, draft), and #11411 (benchmarks). The decimal draft is temporarily stacked on the parser branch; I will rebase it once #11409 lands so its diff contains only decimal changes. No Embucket PRs are needed for review. I am closing this combined draft in favor of those focused Apache PRs. Thanks for the split suggestion.

@osipovartem osipovartem closed this Oct 6, 2026
github-merge-queue Bot pushed a commit that referenced this pull request Oct 8, 2026
…11411)

## Which issue does this PR relate to?

Benchmark requested during review of #11177 (related to #11169). It also
provides a direct kernel baseline for #11409.

## What changes are included?

Extend the existing `parquet-variant-compute` Criterion benchmark for
`json_to_variant` with six incremental JSON shapes: Int32, fixed-point
decimals, escaped strings, a 1,024-element array, an 8 KiB string, and a
100 KiB duplicate-key object. Existing short-string and nested-structure
cases remain unchanged.

Each input is prebuilt outside the timed loop into up to 8,000 rows and
approximately 1 MiB of JSON. The new cases use the existing
`bench_function` naming convention and report bytes per document plus
row count in their IDs. No separate serde/builder measurements, library
behavior, or public API changes.

## Validation

- `cargo +1.95.0 bench -p parquet-variant-compute --bench
variant_kernels --offline -- "batch_json_string_to_variant
(int32|fixed_point_decimals|escaped_strings|large_array_1024|large_string_8k|duplicate_key_100k)"
--quick` (all six new cases ran)
- `cargo +1.99.0 clippy -p parquet-variant-compute --bench
variant_kernels --offline -- -D warnings`
- `cargo +1.95.0 fmt --all -- --check`
- Independent read-only review: approved, no blocking findings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parquet-variant parquet-variant* crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Variant] Preserve fixed-point JSON numbers as decimals

2 participants