Skip to content

Gate numeric 1/0 JSON Bool parsing on allow_special_bool_values - #110835

Merged
Avogar merged 2 commits into
ClickHouse:masterfrom
groeneai:gate-special-bool-values-json-variant
Jul 30, 2026
Merged

Avogar merged 2 commits into
ClickHouse:masterfrom
groeneai:gate-special-bool-values-json-variant

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Numeric 1/0 in JSON Bool parsing now honors the allow_special_bool_values setting. Previously JSON input such as {"v":1} for Variant(Bool, UInt32) was read as Bool even with allow_special_bool_values_inside_variant = 0, because the JSON Bool parsers ignored the setting and Bool has a higher Variant deserialize priority than integer types.

Description

Split out of #109976 per review request.

SerializationBool::deserializeTextJSON and tryDeserializeTextJSON ignored FormatSettings and unconditionally accepted bare 1/0. SerializationVariant gives Bool a higher deserialize priority than the integer types, so SerializationVariant::tryDeserializeTextJSONImpl forwarding allow_special_bool_values_inside_variant (via the nested settings.allow_special_bool_values) into JSON parsing had no effect: {"v":1} for Variant(Bool, UInt32) landed in Bool even with the setting off.

The numeric 1/0 JSON branch is now gated on settings.allow_special_bool_values, mirroring the text/CSV paths. The word form true/false is unchanged, and a plain Bool JSON column still accepts 1/0 (the base FormatSettings.allow_special_bool_values defaults to true and is only overridden internally by Variant/Dynamic inference), so this is backward compatible.

Verified pre-fix {"v":1} for Variant(Bool, UInt32) with the setting off resolved to Bool (wrong) and post-fix resolves to UInt32; with the opt-in it resolves to Bool. Regression test added in 04511_bool_json_special_values_gate.

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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Avogar @CurtizJ — split out of #109976 as requested. Gates the numeric 1/0 JSON Bool branch on allow_special_bool_values so Variant/Dynamic JSON inference stops greedily reading 1/0 as Bool; plain Bool JSON columns keep accepting 1/0.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. clickhouse-local --enable_variant_type=1 --allow_special_bool_values_inside_variant=0 -q "SELECT v, variantType(v) FROM format(JSONEachRow, 'v Variant(Bool, UInt32)', '{\"v\":1}')" returns Bool on base (wrong), UInt32 with the fix.
b Root cause explained? SerializationBool::deserializeTextJSON/tryDeserializeTextJSON ignored FormatSettings and unconditionally accepted bare 1/0. Variant gives Bool a higher deserialize priority than integer types, so allow_special_bool_values_inside_variant (forwarded by SerializationVariant into nested settings.allow_special_bool_values) had no effect on the JSON path -> {"v":1} landed in Bool.
c Fix matches root cause? Yes. Gates the numeric 1/0 JSON branch on settings.allow_special_bool_values, directly restoring the effect of the forwarded setting.
d Test intent preserved / new tests added? New test 04511_bool_json_special_values_gate covers JSON Variant both directions plus the plain-Bool backward-compat path.
e Demonstrated in both directions? Yes. Base binary (29542b5c): OFF -> Bool (wrong). Fixed binary (d7de82fb): OFF -> UInt32, ON -> Bool. Plain Bool JSON still accepts 1/0.
f Fix is general, not a narrow patch? Both JSON entry points (deserializeTextJSON and tryDeserializeTextJSON) gated symmetrically; the text/CSV/quoted paths were already gated (deserializeImpl via tryDeserializeAllVariants).
g Generalizes across inputs? Word form true/false unchanged; numeric 1/0 gated for all consumers of the JSON Bool parser. Plain Bool column keeps 1/0 because base allow_special_bool_values defaults true and is only overridden by Variant/Dynamic inference.
h Backward compatible? Yes. No setting default changes, no format changes. Plain Bool JSON columns keep accepting 1/0; only Variant/Dynamic inference (which sets the flag off) now falls through to a wider type, matching the text/CSV behavior.
i Invariants and contracts preserved? JSON Bool parsing now honors the same allow_special_bool_values invariant already enforced on the text/CSV paths; no change to buffer/position contracts.

Session id: cron:clickhouse-worker-slot-1:20260717-100800

@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Jul 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fec8100]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) FAIL
01666_merge_tree_max_query_limit FAIL cidb IGNORED

AI Review

Summary

This PR fixes the JSONEachRow path where unquoted numeric 1/0 inside Variant(Bool, ...) ignored allow_special_bool_values_inside_variant and were always claimed by Bool. The JSON hunk itself is correct, but the same invariant still leaks through the Quoted carrier, so I do not think the change is ready to merge yet.

Findings

⚠️ Majors

  • [src/DataTypes/Serializations/SerializationBool.cpp:320] Gating numeric 1/0 only in deserializeTextJSON / tryDeserializeTextJSON leaves SerializationBool::deserializeTextQuotedImpl with unconditional case '1' / case '0' branches at lines 412-416. Variant parsing for Values and other quoted formats goes through SerializationVariant::tryDeserializeTextQuoted, so SET allow_special_bool_values_inside_variant = 0; SELECT v, variantType(v) FROM format(Values, 'v Variant(Bool, UInt32)', '(1)'); will still resolve to Bool. The setting therefore remains ineffective for a standard Variant input path. Suggested fix: gate the quoted numeric branch on settings.allow_special_bool_values as well, and add a focused Values regression test next to 04511_bool_json_special_values_gate.
Final Verdict

Changes requested: the JSON carrier is fixed, but Quoted / Values parsing still bypasses the same setting for bare numeric 1 / 0, so Variant type selection is still inconsistent across input formats.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 17, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 879a75f

CI fully finished (Finish Workflow + Mergeable Check pass; 0 checks queued/in-progress). One failure, non-PR-caused, with an owner:

Check / test Reason Owner / fixing PR
Integration tests (amd_msan, 7/8) / test_replicated_database::test_replicated_table_structure_alter flaky (master-wide regression since 2026-07-10) #110030 (external, open) — issue #110036
CH Inc sync — CH Inc sync (private, not actionable)

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@clickhouse-gh

clickhouse-gh Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.30% 86.40% +0.10%
Functions 91.90% 91.90% +0.00%
Branches 78.50% 78.50% +0.00%

Changed lines: Changed C/C++ lines covered: 13/16 (81.25%) · Uncovered code

Full report · Diff report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — fec8100

Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) / 01666_merge_tree_max_query_limit deterministic under parallel replicas (3/3 reruns failed) #112385 (ours, open)

Session id: cron:our-pr-ci-monitor:20260729-043000

@Avogar Avogar self-assigned this Jul 30, 2026
@Avogar
Avogar added this pull request to the merge queue Jul 30, 2026
Merged via the queue into ClickHouse:master with commit 9d8a462 Jul 30, 2026
176 of 178 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants