Repository navigation
Gate numeric 1/0 JSON Bool parsing on allow_special_bool_values - #110835
Conversation
SerializationBool::deserializeTextJSON and tryDeserializeTextJSON ignored
FormatSettings and unconditionally accepted bare 1/0. Variant gives Bool a
higher deserialize priority than the integer types, so JSON input like
{"v":1} for Variant(Bool, UInt32) landed in Bool even when
allow_special_bool_values_inside_variant (forwarded by SerializationVariant
into the nested settings.allow_special_bool_values) was off.
Gate the numeric 1/0 JSON branch on settings.allow_special_bool_values,
mirroring the text/CSV paths. Word form true/false is unchanged, and a plain
Bool JSON column still accepts 1/0 because the base FormatSettings value
defaults to true and is only overridden internally by Variant inference.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-1:20260717-100800 |
|
Workflow [PR], commit [fec8100] Summary: ❌
AI ReviewSummaryThis PR fixes the Findings
Final VerdictChanges requested: the JSON carrier is fixed, but |
CI finish ledger — 879a75fCI fully finished (Finish Workflow + Mergeable Check pass; 0 checks queued/in-progress). One failure, non-PR-caused, with an owner:
Session id: cron:our-pr-ci-monitor:20260717-170000 |
| return false; | ||
| } | ||
| else if (first_char == '1' || first_char == '0') | ||
| else if ((first_char == '1' || first_char == '0') && settings.allow_special_bool_values) |
There was a problem hiding this comment.
Variant only goes through this JSON branch for JSONEachRow, but the same allow_special_bool_values_inside_variant invariant still leaks through the Quoted carrier. SerializationBool::deserializeTextQuotedImpl has unconditional case '1' / case '0' branches at lines 412-416, so SET allow_special_bool_values_inside_variant = 0; SELECT v, variantType(v) FROM format(Values, 'v Variant(Bool, UInt32)', '(1)'); will still choose Bool. Since Values and other quoted formats call SerializationVariant::tryDeserializeTextQuoted, the setting remains ineffective for a standard Variant input path unless the quoted parser gates numeric 1 / 0 the same way and gets a matching regression test.
There was a problem hiding this comment.
The reproducer as written does not fire today, but the invariant concern is correct and it becomes live the moment #109976 merges. Details, all measured on origin/master (whose deserializeTextQuotedImpl is byte-identical to this branch's -- md5 044fd00111d9 on both; this PR touches only the two JSON entry points).
Today the reproducer returns UInt32, not Bool:
SET allow_special_bool_values_inside_variant = 0;
SELECT v, variantType(v) FROM format(Values, 'v Variant(Bool, UInt32)', '(1)');
-- 1 UInt32
It returns UInt32 with the setting = 1 as well, so the gate is not what decides this case. The reason is not the gate but that case '1' / case '0' (:409 / :412 on master, :412 / :415 here) never advance istr.position(), unlike the t / f branches (assertStringCaseInsensitive, :400) and the quote branch (++istr.position(), :419). SerializationVariant::tryDeserializeVariantFromField requires variant_buf.eof() (SerializationVariant.cpp:1140) and the format-level callers assert the delimiter, so the unconsumed digit always fails the end-of-value check. Those two branches are currently unreachable-as-accepting:
SELECT v FROM format(CustomSeparated, 'v Bool', '1\n') SETTINGS format_custom_escaping_rule = 'Quoted';
-- Code: 27 ... text "1<LINE FEED>" is not like Bool
0 behaves the same, and true through the identical shape parses fine, which isolates the numeric branches.
Meanwhile the quoted carrier does honour the setting on the path that is reachable, via case '\'' (:418) delegating to deserializeImpl, gated at :201:
-- allow_special_bool_values_inside_variant = 0
SELECT v, variantType(v) FROM format(Values, 'v Variant(Bool, String)', '(''yes'')'); -- yes String
-- = 1
SELECT v, variantType(v) FROM format(Values, 'v Variant(Bool, String)', '(''yes'')'); -- true Bool
Where the concern is right: #109976 adds ++istr.position() to both numeric branches and does not gate them. That makes them reachable-as-accepting while still ungated, which is precisely the leak described. Its own reference update to 03158_dynamic_type_from_variant shows it, and that test sets no bool setting, so allow_special_bool_values_inside_variant is at its default 0:
-- origin/master -> after #109976
0 Variant(Bool,...,UInt32) -> false Variant(Bool,...,UInt32)
1 Variant(Bool,...,UInt32) -> true Variant(Bool,...,UInt32)
0 UInt32 -> false Bool
1 UInt32 -> true Bool
So the correct target is #109976, not this PR: the gate has to land on those two branches in the same change that makes them accept, otherwise the position fix reopens the hole for the quoted carrier. Gating them here would be a behavioural no-op today (they never accept) and would leave #109976 free to reintroduce the leak, so folding it in would not actually pin the contract. I have raised it on #109976, which is where the accepting behaviour is introduced and where the reference diff already records the flip.
This PR stays scoped to the JSON carrier, which is why it was split out of #109976 in the first place. No change pushed for this comment.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 13/16 (81.25%) · Uncovered code |
CI finish ledger — fec8100Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260729-043000 |
9d8a462
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Numeric
1/0in JSONBoolparsing now honors theallow_special_bool_valuessetting. Previously JSON input such as{"v":1}forVariant(Bool, UInt32)was read asBooleven withallow_special_bool_values_inside_variant = 0, because the JSONBoolparsers ignored the setting andBoolhas a higherVariantdeserialize priority than integer types.Description
Split out of #109976 per review request.
SerializationBool::deserializeTextJSONandtryDeserializeTextJSONignoredFormatSettingsand unconditionally accepted bare1/0.SerializationVariantgivesBoola higher deserialize priority than the integer types, soSerializationVariant::tryDeserializeTextJSONImplforwardingallow_special_bool_values_inside_variant(via the nestedsettings.allow_special_bool_values) into JSON parsing had no effect:{"v":1}forVariant(Bool, UInt32)landed inBooleven with the setting off.The numeric
1/0JSON branch is now gated onsettings.allow_special_bool_values, mirroring the text/CSV paths. The word formtrue/falseis unchanged, and a plainBoolJSON column still accepts1/0(the baseFormatSettings.allow_special_bool_valuesdefaults to true and is only overridden internally byVariant/Dynamicinference), so this is backward compatible.Verified pre-fix
{"v":1}forVariant(Bool, UInt32)with the setting off resolved toBool(wrong) and post-fix resolves toUInt32; with the opt-in it resolves toBool. Regression test added in04511_bool_json_special_values_gate.