Skip to content

Cherry pick #103620 to 26.4: Fix JSONExtract into Variant silently truncating fractional JSON numbers - #104742

Merged
robot-ch-test-poll3 merged 4 commits into
backport/26.4/103620from
cherrypick/26.4/103620
May 12, 2026
Merged

robot-ch-test-poll3 merged 4 commits into
backport/26.4/103620from
cherrypick/26.4/103620

Conversation

@robot-ch-test-poll3

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 4 commits April 27, 2026 22:21
`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
@robot-ch-test-poll3 robot-ch-test-poll3 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels May 12, 2026
@robot-ch-test-poll3
robot-ch-test-poll3 merged commit e3e0a92 into backport/26.4/103620 May 12, 2026
@robot-ch-test-poll3
robot-ch-test-poll3 deleted the cherrypick/26.4/103620 branch May 12, 2026 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants