Skip to content

Do not match a JSON subcolumn inside a typed path to a skip index - #119321

Merged
alexey-milovidov merged 7 commits into
masterfrom
fix-json-index-typed-path-subcolumn-117938
Oct 3, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
fix-json-index-typed-path-subcolumn-117938

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

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 to CHANGELOG.md):

Fix a JSONAllPaths skip index silently pruning every granule for a filter on a subcolumn inside a declared typed path of a JSON column (for example json.a.b for JSON(a JSON) or JSON(a Tuple(b Int64))). Such a filter no longer uses the index.

Documentation entry for user-facing changes

...

A JSONAllPaths skip index silently returned no rows for a filter on a subcolumn that reaches inside a typed path of a JSON column:

CREATE TABLE t (json JSON(a JSON), INDEX idx JSONAllPaths(json) TYPE bloom_filter GRANULARITY 1)
ENGINE = MergeTree ORDER BY tuple() SETTINGS index_granularity = 2;
INSERT INTO t SELECT '{"a":{"b":42}}' FROM numbers(4);
SELECT count() FROM t WHERE json.a.b = 42;                               -- 0
SELECT count() FROM t WHERE json.a.b = 42 SETTINGS use_skip_indexes = 0; -- 4

JSONAllPaths reports a declared typed path verbatim and never recurses into its contents, so the granule path set here is ['a'], both for a nested JSON typed path and for a structured one such as JSON(a Tuple(b Int64)). The query side derives the path a.b from the subcolumn json.a.b and asks whether any granule contains it; none does, so every granule was pruned, even though the value lives inside the typed path's own column and reading the subcolumn returns it. The assumption behind the match — a path absent from JSONAllPaths means the subcolumn is NULL — holds for a dynamic path but not for the substructure of a typed path.

Refuse the match when the derived path has a strict prefix that is a declared typed path of the JSON type, so such a filter simply does not use the index. The type is taken from the argument of the JSON index function in the index expression, collected per index sample block position and passed to the condition, so it is available regardless of how the column is named inside the index function. This covers every index type sharing the matcher (bloom_filter, tokenbf_v1, ngrambf_v1, text) and every predicate routed through it. The shapes the index is designed for are unaffected: a scalar typed path is reported by its own name, and a dynamic path is reported with all of its components, so both keep using the index — the new test pins that with force_data_skipping_indices.

New test: 05198_json_skip_index_typed_path_subcolumn. Verified in both directions, and a differential run of the skip_index/bloom_filter/text_index/json_path/tokenbf/ngrambf stateless tests (344 tests) shows no new failures.

Closes: #117938


Workflow [PR]
Sync PR [sync-upstream/pr/119321]

Version info

  • Merged into: 26.10.1.1372-master (included in 26.10 and later)

A `JSONAllPaths` skip index silently returned no rows for a filter on a subcolumn that reaches inside a
typed path of a `JSON` column:

    CREATE TABLE t (json JSON(a JSON), INDEX idx JSONAllPaths(json) TYPE bloom_filter GRANULARITY 1)
    ENGINE = MergeTree ORDER BY tuple() SETTINGS index_granularity = 2;
    INSERT INTO t SELECT '{"a":{"b":42}}' FROM numbers(4);
    SELECT count() FROM t WHERE json.a.b = 42;                              -- 0
    SELECT count() FROM t WHERE json.a.b = 42 SETTINGS use_skip_indexes = 0; -- 4

`JSONAllPaths` reports a declared typed path verbatim and never recurses into its contents, so the
granule path set here is `['a']`, both for a nested `JSON` typed path and for a structured one such as
`JSON(a Tuple(b Int64))`. The query side derives the path `a.b` from the subcolumn `json.a.b` and asks
whether any granule contains it; none does, so every granule was pruned, even though the value lives
inside the typed path's own column and reading the subcolumn returns it. The assumption behind the
match - a path absent from `JSONAllPaths` means the subcolumn is `NULL` - holds for a dynamic path but
not for the substructure of a typed path.

Refuse the match when the derived path has a strict prefix that is a declared typed path of the `JSON`
type, so such a filter simply does not use the index. The type is taken from the argument of the JSON
index function in the index expression, collected per index sample block position and passed to the
condition, so it is available regardless of how the column is named inside the index function. This
covers every index type sharing the matcher (`bloom_filter`, `tokenbf_v1`, `ngrambf_v1`, `text`) and
every predicate routed through it. The shapes the index is designed for are unaffected: a scalar typed
path is reported by its own name, and a dynamic path is reported with all of its components, so both
keep using the index.

Closes: #117938
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ccf28d2]

Summary: ✅


AI Review

Summary

This PR fixes a real wrong-result case where JSONAllPaths-backed skip and text indexes could match a subcolumn that reaches inside a declared typed path of a JSON column and incorrectly prune all granules. The current patch now threads the JSON argument type metadata through every affected matcher, refuses those unsafe descendant matches, preserves exact typed-path matches for overlapping declarations, and adds focused stateless coverage for the bloom_filter, tokenbf_v1, and text paths. I do not see remaining correctness issues in the current code.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit ccf28d2.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.50% 79.50% +0.00%

Changed lines: Changed C/C++ lines covered: 92/97 (94.85%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 10, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-skip-index Skipping indexes (minmax, set, bloom) and their evaluation. label Sep 10, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

The red stateless jobs are all the same unrelated test: 05141_time_series_select_join_settings fails with join_algorithm = 'grace_hash' is external from the first block and needs a spill threshold, but neither max_bytes_before_external_join nor max_bytes_ratio_before_external_join resolved to a non-zero value, and the test sets both of those to 0 on purpose. That check came from the spilling-hash-join unification, which is already reverted on master (12981f21057), so it only affects merge commits built before the revert. Nothing here touches joins.

@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ccf28d22e with master ca1dbe542 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

34 translation units recompiled, 348 s compile time in total, 34 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +20.0 s (+6%) in total before that adjustment.

Job report

alexey-milovidov and others added 2 commits September 18, 2026 04:11
…d-path-subcolumn-117938

Both sides added a trailing constructor parameter to the bloom filter, text bloom filter and text index conditions (`columns_shadowing_map_subcolumns_` on master, `json_argument_types_` in this branch); keep both.
… path

Typed paths of a `JSON` type may overlap: `JSON(a Array(JSON), a.b Int64)` declares
both `a` and `a.b`, and the subcolumn `json.a.b` reads the typed path `a.b` itself,
which `JSONAllPaths` reports verbatim. `pathIsInsideTypedPath` returned true on the
shorter typed prefix `a`, so such a filter stopped using the skip index. A path that
is a declared typed path is now exempt from the strict-prefix check; the test pins it
with `force_data_skipping_indices`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
alexey-milovidov and others added 2 commits October 2, 2026 06:34
# Conflicts:
#	src/Storages/MergeTree/MergeTreeIndexConditionText.cpp
#	src/Storages/MergeTree/MergeTreeIndexConditionText.h
… `JSON` index argument

`collectJSONIndexArgumentTypes` only recognized a bare `DataTypeObject`, so for an index
`JSONAllPaths(json)` over `Nullable(JSON(a JSON))` the typed paths were not known and a
subcolumn inside a typed path (`json.a.b`) was still matched to the index, pruning every granule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov and others added 2 commits October 2, 2026 14:43
# Conflicts:
#	src/Storages/MergeTree/MergeTreeIndexBloomFilter.cpp
#	src/Storages/MergeTree/MergeTreeIndexBloomFilter.h
…mn test

`MergeTreeIndexConditionText` has its own matchers for `JSONAllValues` and
`JSONAllPaths`, which the fix also guards. Add regressions for both with
`tokenizer = 'array'` over `JSON(a JSON)`: without the fix a filter on
`json.a.b` prunes every granule and returns 0 rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Oct 3, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 3, 2026
Merged via the queue into master with commit d5d854b Oct 3, 2026
150 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-json-index-typed-path-subcolumn-117938 branch October 3, 2026 02:15
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 3, 2026
leshikus pushed a commit to leshikus/ClickHouse that referenced this pull request Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-skip-index Skipping indexes (minmax, set, bloom) and their evaluation. 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.

A JSONAllPaths skip index silently prunes all granules for a subcolumn under a typed JSON path with substructure

2 participants