Repository navigation
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 ClickHouse#103617
|
Pre-PR validation gate (per task workflow): a) Deterministic repro? Yes. Single SQL b) Root cause explained? Yes.
c) Fix matches root cause? Yes. The fix adds d) Test intent preserved? / New tests added? Yes. New e) Demonstrated in both directions? Yes.
f) Fix is general, not a narrow patch? Yes.
Session id: cron:clickhouse-ci-task-worker:20260427-214500 |
|
cc @Avogar @CurtizJ — could you review? This fixes silent data loss when |
|
Workflow [PR], commit [c474a1f] Summary: ❌
AI ReviewSummaryThis PR fixes real silent data loss in ClickHouse Rules
Final Verdict
|
| { | ||
| auto strict_settings = insert_settings; | ||
| strict_settings.no_int_truncation_from_double = true; | ||
| for (size_t i : order) |
There was a problem hiding this comment.
VariantNode now runs the strict first pass across all variants, including String. That changes behavior beyond the stated int-vs-float fix: for example, JSONExtract('{"x": 3.14}', 'x', 'Variant(String, Int64)') now picks String in the strict pass (Int64 rejects non-integral), while previously it picked Int64=3 in the single legacy pass.
Concrete trace on this path:
- strict pass sets
no_int_truncation_from_double=true; Int64branch fails at the new non-integral check;Stringbranch succeeds viajsonElementToString;- function returns before the legacy pass.
If this broader behavior change is intentional, please add an explicit test and document it in PR/changelog. If not, the strict pass should avoid letting String/other non-numeric fallbacks preempt the legacy numeric behavior.
There was a problem hiding this comment.
Thanks — that's a real behavior change beyond Variant(IntT, FloatT) and you're right that it deserves explicit coverage.
This is intentional. The bug being fixed is silent data loss, and for Variant(String, IntT) + a fractional JSON DOUBLE, capturing the value as String="3.14" is strictly better than the previous Int64=3 (which silently dropped the fractional part). Restricting the strict pass to numeric variants only would preserve the truncation for Variant(String, IntT) shapes — that's exactly the failure mode @fm4v reported in #103617.
Concrete reasoning:
| Variant shape | Behavior with strict-only-on-numeric | Behavior with strict-on-all (current PR) |
|---|---|---|
Variant(String, Int64, Float64) + 3.14 |
Float64=3.14 ✅ |
Float64=3.14 ✅ |
Variant(String, Int64) + 3.14 |
Int64=3 ❌ (still silent data loss) |
String="3.14" ✅ |
Variant(String, Int64) + 3 |
Int64=3 ✅ |
Int64=3 ✅ (integer literal still picks Int64) |
Variant(Int64, Int32) + 3.14 |
Int64=3 (legacy fallback) |
Int64=3 (legacy fallback) |
Users who explicitly want integer truncation must remove String from the variant — Variant(IntT, IntT) + 3.14 continues to truncate via the legacy lenient pass (test 10).
I added four explicit regression cases in commit c474a1f and updated the PR description with a new "Intentional broader scope" section + behavior-matrix entries:
- Test 16:
Variant(String, Int64)+3.14→String="3.14"(your exact case) - Test 17:
Variant(String, Int32)+3.14→String="3.14"(narrower integer) - Test 18:
Variant(String, Int64)+3→Int64=3(integer literal unchanged) - Test 19:
Variant(String, Int32, Int64, Int128)+3.14→String="3.14"(multiple integer widths still preempted byString)
50/50 passes locally with tests/clickhouse-test --test-runs 50 04141_json_extract_variant_no_int_truncation.
PR ClickHouse#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.
LLVM Coverage Report
Changed lines: 90.48% (38/42) · Uncovered code |
95b90bb
Cherry pick #103620 to 26.4: Fix JSONExtract into Variant silently truncating fractional JSON numbers
…ating fractional JSON numbers
Backport #103620 to 26.4: Fix JSONExtract into Variant silently truncating fractional JSON numbers
…ntly truncating fractional JSON numbers
…3620 Backport ClickHouse#103620 to 26.4: Fix JSONExtract into Variant silently truncating fractional JSON numbers
JSONExtractinto aVariantthat contained both an integer and a floating-point member silently truncated fractional JSON numbers: the integer member won and the fractional part was lost. Reported and root-caused by @fm4v in #103617.The same code path also applies to
STRINGJSON elements that parse as non-integral floats (e.g."3.14"), which previously truncated toInt64=3as well.Root cause (lines from master
7df6513e4d29)SerializationVariant::getVariantsDeserializeTextOrderorders variants by priority — integers > big integers > decimals > floats > strings (SerializationVariant.cpp:899-958).VariantNode::insertResultToColumniterates variants in that order and accepts the first member whose extractor returns true (JSONExtractTree.cpp:1514).tryGetNumericValueFromJSONElementcallsaccurate::convertNumeric<Float64, NumberType, false>(JSONExtractTree.cpp:141). Withstrict=false, only the overflow check fires —3.14 -> 3returnstrueand theInt64member wins.convertNumericcall.Fix
Add
JSONExtractInsertSettings::no_int_truncation_from_double(defaultfalse, preserves legacy behavior). When set,tryGetNumericValueFromJSONElementrejects DOUBLE -> integer and STRING-as-fractional-float -> integer conversions whose round-trip is lossy.VariantNode::insertResultToColumnnow performs a strict first pass with this flag enabled so aFloat/Decimalmember 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.14still returns3for backward compatibility). Direct integer extraction (JSONExtract(json, 'Int64'),JSONExtractInt, etc.) is unchanged — the fix is scoped to theVariantpath.This mirrors the established pattern used in
DynamicNode::insertResultToColumnfortry_existing_variants_in_dynamic_first— try strictly first, fall back to lenient.Also fixes a pre-existing copy-paste error in the error message:
"cannot read Map value"->"cannot read Variant value".Intentional broader scope (raised in review)
The strict first pass runs across all variant members, not only numeric ones. This means a
Variantthat contains bothStringand an integer member (without aFloat/Decimal) also benefits from the fix: a fractional JSON DOUBLE now lands inStringinstead of being silently truncated into the integer member.JSONExtract('{"x": 3.14}', 'x', 'Variant(String, Int64)')Int64=3(silent data loss)String="3.14"(lossless)JSONExtract('{"x": 3.14}', 'x', 'Variant(String, Int32)')Int32=3String="3.14"JSONExtract('{"x": 3.14}', 'x', 'Variant(String, Int32, Int64, Int128)')Int128=3String="3.14"This is intentional: the bug being fixed is silent data loss, and capturing the value as
Stringis strictly better than dropping the fractional part. Users who explicitly want integer truncation must removeStringfrom the variant —Variant(Int64, Int32)+3.14still returns3via the legacy lenient pass. Integer JSON literals (3) continue to pick the integer member even whenStringis present, because the strict pass accepts an exactly representable value before reachingString.Tests 16-19 in the regression suite codify this behavior.
Behavior matrix
JSONExtract('{"x": 3.14}', 'x', 'Variant(Int64, Float64)')Int64=3Float64=3.14JSONExtract('{"x": 3.14}', 'x', 'Variant(Float64, Int64)')Int64=3Float64=3.14JSONExtract('{"x": "3.14"}', 'x', 'Variant(Int64, Float64)')Int64=3Float64=3.14JSONExtract('{"x": -2.5}', 'x', 'Variant(Int64, Float64)')Int64=-2Float64=-2.5JSONExtract('{"x": 1.5}', 'x', 'Variant(Int64, Decimal(10, 4))')Int64=1Decimal=1.5JSONExtract('{"x": 3.14}', 'x', 'Variant(String, Int64)')Int64=3(silent data loss)String="3.14"(lossless)JSONExtract('{"x": 3}', 'x', 'Variant(String, Int64)')Int64=3Int64=3(unchanged)JSONExtract('{"x": 3}', 'x', 'Variant(Int64, Float64)')Int64=3Int64=3(unchanged)JSONExtract('{"x": 3.14}', 'x', 'Variant(Int64, Int32)')Int32=3Int32=3(legacy fallback)JSONExtract('{"x": 3.14}', 'x', 'Int64')33(unchanged)JSONExtractInt('{"x": 3.14}', 'x')33(unchanged)Tests
New regression test
tests/queries/0_stateless/04141_json_extract_variant_no_int_truncation.sqlcovering 19 cases: declaration order independence, negative fractional, small fractional, integer literals, overflow, string-as-fractional, decimal members, fall-through to truncation when only integer variants exist, direct extraction backward compat, big int variants, bool, the original reproducer, and the fourVariant(String, IntT)cases that codify the broader-scope behavior described above.Existing test
04121_json_extract_types_edgeshad its.referenceupdated for theVariant(String, Int64, Float64)+3.14case (3->3.14). The issue explicitly called out this test as codifying the bug; @fm4v's PR #103618 also rewrites that case (usingVariant(String, Float64)to sidestep the bug). Both approaches converge on3.14as the correct output, so this update is forward-compatible with #103618.Closes #103617
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
JSONExtractinto aVarianttype silently truncating fractional JSON numbers. Previously,JSONExtract('{"x": 3.14}', 'x', 'Variant(Int64, Float64)')returnedInt64=3andJSONExtract('{"x": 3.14}', 'x', 'Variant(String, Int64)')returnedInt64=3, dropping the fractional part. The fractional value is now preserved losslessly: aFloat64/Decimalmember claims it when present, otherwise aStringmember captures the original JSON.Varianttypes containing only integer members (e.g.Variant(Int64, Int32)) and direct integer extraction (JSONExtract(json, 'Int64'),JSONExtractInt, etc.) are unchanged.Documentation entry for user-facing changes
Version info
26.5.1.57226.4.3.14