Repository navigation
Cherry pick #103620 to 26.4: Fix JSONExtract into Variant silently truncating fractional JSON numbers - #104742
Merged
robot-ch-test-poll3 merged 4 commits intoMay 12, 2026
Conversation
`JSONExtract` into a `Variant` that contained both an integer and a floating-point
member silently truncated fractional JSON numbers: the integer member won and the
fractional part was lost. For example,
JSONExtract('{"x": 3.14}', 'x', 'Variant(String, Int64, Float64)')
returned `Int64=3` instead of `Float64=3.14`.
Root cause: the JSON DOUBLE -> integer branch in `tryGetNumericValueFromJSONElement`
called `accurate::convertNumeric<Float64, NumberType, false>` with `strict=false`,
which only checks overflow rather than equality. `VariantNode::insertResultToColumn`
iterates variants in priority order and accepts the first member whose extractor
returns true, so the integer member always claimed the value. The same path
truncated `STRING` JSON values whose contents parse as a non-integral float
(e.g. `"3.14"`).
Fix: add `JSONExtractInsertSettings::no_int_truncation_from_double` (default false,
preserves legacy behavior). When set, `tryGetNumericValueFromJSONElement` rejects
DOUBLE -> integer and STRING-as-fractional-float -> integer conversions whose
round-trip is lossy. `VariantNode::insertResultToColumn` now performs a strict
first pass with this flag enabled so a Float/Decimal member can claim the value,
and falls back to the legacy lenient pass only when the strict pass found no
match (e.g. `Variant(Int64, Int32)` + `3.14`). Direct integer extraction
(`JSONExtract(json, 'Int64')`, `JSONExtractInt`) is unchanged.
Also fixes a pre-existing copy-paste error in the Variant error message
("cannot read Map value" -> "cannot read Variant value").
Closes #103617
PR #103620 review by clickhouse-gh[bot] flagged that the strict first pass in `VariantNode::insertResultToColumn` runs across all variants, not only numeric ones, so `Variant(String, IntT)` + a fractional JSON DOUBLE now picks `String` instead of silently truncating to `IntT`. This is intentional: the bug being fixed is *silent data loss*, and capturing the value as `String` is strictly better than dropping the fractional part. Restricting the strict pass to numeric variants would preserve the bug for `Variant(String, IntT)` shapes, which is exactly the failure mode the issue reporter (@fm4v) described. Add four explicit cases (16-19) to the regression test: 16. Variant(String, Int64) + 3.14 -> String="3.14" (bot's exact case) 17. Variant(String, Int32) + 3.14 -> String="3.14" 18. Variant(String, Int64) + 3 -> Int64=3 (integer literal unchanged) 19. Variant(String, Int32, Int64, Int128) + 3.14 -> String="3.14" 50/50 passes locally with `--test-runs 50` randomization. PR description updated with a new "Intentional broader scope" section and behavior-matrix entries documenting the change.
…ant-truncation Fix JSONExtract into Variant silently truncating fractional JSON numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #103620
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job