Repository navigation
Conversation
…wed-by-physical-column
A column may legally be named `j.some.path` while the table also holds a JSON column `j`. `tryMatchJSONSubcolumnToIndex` decided "this name is a path of that index's JSON column" purely from the rendered string: it scanned the index expression names for `JSONAllPaths(X)` / `JSONAllValues(X)` and, if the candidate name started with `X` followed by a dot, declared the remainder to be a path of `X`. Nothing checked that the name actually resolves to a subcolumn reached through `X`, so a predicate on the physical column was answered from the JSON path set, the path was absent, and the granule holding the matching row was pruned. Wrong results, silently, on every index type that uses this helper (text, tokenbf_v1, ngrambf_v1, sparse_grams, bloom_filter) and every predicate route. The string is irreducibly ambiguous: subcolumn names are flat and the wrapper substreams contribute nothing to a rendered name, so `x.j.p` is the same string whether it means Tuple element `j.p` or Tuple element `j` then JSON path `p`. Only the serialization substream path separates them. Resolve both the index's JSON column and the candidate name through the table metadata - the same resolver the analyzer uses, so index analysis and the read agree by construction - and accept the name only when it resolves to a subcolumn of the same storage column whose substream path extends the JSON column's by JSON path steps alone. The path is then read off that resolution instead of the name, which also fixes subcolumns of a path (`j.a.size0`, `j.a.:`String`.null`) whose textual remainder is not a path at all. Resolution is two-stage, in the resolver's own order (exact column and static subcolumn lookups first, a dynamic root only after), and its dynamic stage walks the declared columns rather than the name's dot splits: a name can embed a folded constant, and index analysis runs before any data is read and observes no cancellation, so its cost must stay bounded by the schema. The check runs per candidate inside the selection loop, not after it: one index may carry several JSON columns, and rejecting the shortest textual match must not discard a longer candidate that is a genuine ancestor. Co-Authored-By: Claude Opus 5 <[email protected]>
…ly the selected index column A JSON skip index is matched against a filter column name in two steps, and both used to trust the rendered string. The path a name denotes now comes from the substream path rather than from the name, so a subcolumn OF a path no longer contributes its own name to the searched path. Which substream tails may follow the path is a property of what the two index contents hold, not of the object serialization, so the rule moves out of SerializationObject and becomes a closed allow-list of value-preserving descents: an element that reads a stored value under the path is allowed, an element that reads a property derived from it is not. A size is never among the values JSONAllValues holds, and a null map is 1 exactly where the path is absent, which is the opposite of what the path set models; both are refused, both returned wrong results before. The list is closed rather than a list of exclusions so that an unclassified substream type refuses by default. Only the index column the textual scan selects is validated, and a longer entry that also owns the name is not tried in its place: probing that one asks it for the path the name denotes inside it, and JSONAllPaths emits only the first element of a path that continues into a nested object, which prunes the granule holding the match. Keeping master's selection also makes the change monotone, since the index column probed is always the one that was probed before. resolveName walks the schema once instead of restarting the scan per dot-prefix. Index analysis runs before any data is read and observes no cancellation, so its cost must stay bounded by the schema rather than by a name that can embed a folded constant. Co-Authored-By: Claude Opus 5 <[email protected]>
05190 covers a physical column whose name shadows a JSON path and its subcolumns, a name claimed inside a Tuple element, the derived-property tails that must not be answered from either index, and the structural descents under a typed composite path whose searched path this change corrects. The control arms assert the plan as well as the count, because a count alone also passes when the index is absent from the plan, which is what a too-broad guard causes. Arms 1-3 are one per index condition class rather than one per index type, since tokenbf_v1 and sparse_grams share a condition class with ngrambf_v1. 04780 gains a fifth arm for the one shape that reaches metadata resolution through a dot-dense name, a genuine JSON subcolumn reference. Its control is the same query on a table with no JSONAllPaths index, so parsing and analysis are identical and only the matching differs; a control of equal character length but a single path element measures the parser's per-part cost, which is 1.57x and unrelated. Co-Authored-By: Claude Opus 5 <[email protected]>
Reading the null map of an absent JSON path yields 0 under the `advanced` shared data serialization and 1 under `map`. The test runner randomizes that setting, so the arm asserting that a null map tail is not answered from the path set failed in 10 of 50 runs while measuring the reader rather than the rule it exists for. Pinning it on that one table keeps the arm deterministic; the other arms are unaffected by the setting, so nothing else is pinned. Co-Authored-By: Claude Opus 5 <[email protected]>
The path is read off the rendered subcolumn name again, as it was before. What this change keeps is the pair of refusals: a name is matched to a `JSONAllPaths` or `JSONAllValues` index only when the table metadata resolves it to a subcolumn of the same storage column as the index's JSON column, reached from that column by JSON path steps, and whose remaining tail still reads the value stored at that path. Otherwise the index is not consulted for that predicate, which costs pruning and nothing else. Correcting the path as well does fix a subcolumn inside a declared typed path, but ClickHouse#119321 fixes that family by refusing it, and recognises it by finding a declared typed path in the prefix of the derived path. A corrected path has no such prefix, so a tree containing both changes would lose that refusal in either merge order. Leaving the derivation alone keeps the two changes independent: this one can only decline a match that would otherwise be made, and asks for the same path when it does match. The test arms that asserted the corrected paths go with it. Co-Authored-By: Claude Opus 5 <[email protected]>
…wed-by-physical-column
Four conflicts, all in the same place and all additive: master now passes a
`NameSet columns_shadowing_map_subcolumns_` to `MergeTreeIndexConditionBloomFilter`,
`MergeTreeConditionBloomFilterText` and `MergeTreeIndexConditionText`, and this branch
passes them a `StorageMetadataPtr metadata_snapshot_`. Both are kept, in the same order in
all three classes: `metadata_snapshot_` directly after `context_`, the `NameSet` last.
Files: MergeTreeIndexBloomFilter.{h,cpp}, MergeTreeIndexBloomFilterText.cpp,
MergeTreeIndexText.cpp. MergeTreeIndexJSONSubcolumnHelper.{h,cpp} is untouched by the merge.
The 2000-dot arm of `04780` has a one-sided oracle: it fires only when the resolved arm allocates more than 150% of its control, so a change that stopped matching the long name at all would lower the measurement and pass. That arm exists to guard against an uncancellable planning walk, so it now also asserts that the long name is still matched and its granules still pruned. The assertion is identical before and after this change by design: it is a liveness guard, not a fix detector. The three index condition classes now take two parameters derived from the same metadata snapshot. `columns_shadowing_map_subcolumns` is a precomputed set answering whether a name is a column that shadows a `Map` key subcolumn; the snapshot answers on demand whether a name resolves to a path of this `JSON` column. Different data, different question, and neither reads the other's. `05190` gains two arms. `isNotNull` reaches the matcher from two condition classes no other arm covers, and only with `optimize_functions_to_subcolumns` off, because the rewrite turns the function into a null-map read before index analysis sees it; its claimant is `Nullable` because the route early-outs on a non-Nullable argument. A typed path under `JSONAllValues` is the one typed-path shape whose granules can be pruned, since `JSONAllPaths` emits a non-Nullable typed path for every row and the `JSONAllPaths` typed-path arms can therefore only assert index eligibility. The dynamic stage of `resolveName` walks the declared columns rather than the name's dot splits, which is what `ColumnsDescription::tryGetColumn` does once dynamic subcolumns are enabled. Both reach the same root, the shortest declared prefix that owns the remainder, but a name can embed a folded constant, so splitting it costs O(length^2) allocated bytes, and index analysis runs before any data is read and observes no cancellation.
Issue 119592 reports this defect with two shapes the file did not carry. Face 1 indexes a JSON column whose own name contains a dot (`j.a` JSON beside a physical `j.a.b` String), which additionally exercises the scan's handling of a dotted index-column name; both names are declared, so the resolver answers at its first stage and the ownership check refuses on `name_in_storage`. Face 2 declares two JSON columns where the shorter one owns the name (`j` and `j.a`, index on `JSONAllPaths(j.a)`): the resolver sorts candidate roots shortest-first and binds `j.a.b` to `j`, so the index's column is not the name's owner and the check refuses again. The reporter's own `toTypeName(j.a.b)` of `Dynamic` is what that binding looks like from SQL, and it holds on both binaries. Measured on the unfixed binary before the arms were written: each indexed count is 0 while its `use_skip_indexes = 0` oracle is 1, so both shapes are this defect and not a coincidence of the fixture. Arm 22 is the first arm in the file whose answer depends on the root order at all, because every other shape resolves with at most one candidate root: the physical-column arms answer at the resolver's first stage, and the arms with two JSON-typed names declare them inside a single top-level Tuple, which the root loop does not descend into. Reversing the sort predicate reddens arm 22 and nothing else, so the order is asserted rather than assumed. Both arms measure a refusal, so per the file's own convention they assert the count only and each carries its unindexed oracle line.
Internal second-model review: adjudication log (click to expand)Three review rounds before this PR was opened: pre-publication review by an independent model
Severity: ❌ blocker / One correction to the issue's own census: the first shape listed there as unaffected, Also noticed and deliberately not raised, both identical before and after this change and so not Session id: cron:clickhouse-review-self-slot-58:20260912-154000 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20260912-145600 |
|
Workflow [PR], commit [0b7a53b] Summary: ✅
AI ReviewSummaryThis PR hardens JSON skip-index matching by resolving candidate subcolumns through Findings
Final Verdict |
|
cc @ahmadov @shankar-iyer, could you review this? A |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes12 object files changed (+55.77 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units42 translation units recompiled, 391 s compile time in total, 42 of them have a recent master baseline. |
…wed-by-physical-column # Conflicts: # src/Storages/MergeTree/MergeTreeIndexConditionText.cpp
Comment-only change: the comment-stripped token stream of every touched file is byte-identical to the previous commit. The removed text is motivation and design history, which the pull request description already carries: why a rendered subcolumn name cannot say whose name it is, why the probed path is still read off the rendered name, why a longer owning index column is not re-selected, and the per-index soundness argument for refusing derived properties. What stays is what a reader of the code needs at each spot.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
| /// Only the selected entry is validated: a longer owner would be probed for a nested path `JSONAllPaths` never emits. | ||
| auto resolved_json_column = resolveName(columns, String(matched_json_column)); | ||
| auto resolved_name = resolveName(columns, column_name); | ||
| if (!resolved_json_column || !resolved_name) | ||
| return std::nullopt; | ||
|
|
||
| if (!isJSONPathOfColumn(*resolved_json_column, *resolved_name)) | ||
| return std::nullopt; |
There was a problem hiding this comment.
This stops after disproving the shortest textual candidate, but a multi-column JSONAllPaths index can still have a longer indexed JSON column that really owns the name and is safe to probe.
Concrete shape:
x Tuple(j JSON, `j.p` JSON(a UInt64)),
INDEX idx (JSONAllPaths(x.j), JSONAllPaths(x.`j.p`)) TYPE bloom_filterFor x.j.p.a = 42, resolveName correctly shows that x.j does not own the filter column, but the real owner is the second indexed column x.j.p``. Returning here drops that second candidate entirely, so the query loses skip-index pruning even though probing JSONAllPaths(x.j.p`)` with path `a` is sound.
The "longer owner would be probed for a nested path" argument only holds for descendants like a.b. Once the owner switches to x.j.p``, the suffix becomes just a, which `JSONAllPaths` does emit. Can we validate candidates inside the scan loop and continue after a non-owner, recomputing the suffix relative to each candidate?
# Conflicts: # src/Storages/MergeTree/MergeTreeIndexText.cpp
Closes: #119592
Related: #119321
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results when a
JSONAllPathsorJSONAllValuesskip index was consulted for a name that is not a path of itsJSONcolumn. Dots are legal in column names, so a physical column named`j.some.path`can sit beside aJSONcolumnj; index analysis read it as a path ofjand skipped the granule holding the matching row. ATupleelement or dynamic path claiming that name, and a subcolumn reading a derived property such as.size0or.null, had the same effect.Description
A name shaped
j.<rest>was taken to be a path of the index'sJSONcolumn on a string prefix split alone, nothing checked against the table's columns. Rendered subcolumn names are flat and cannot carry that question. At defaults:Both names are now resolved through
ColumnsDescription, the resolver the analyzer uses; the match is refused unless the candidate is the same storage column, reached by JSON path steps and then only by descents that still read the value stored there. A length or a null map is not such a descent:JSONAllValuesnever holds a length, and a null map is 1 where the path is absent.That also answers the issue's second face: with two
JSONcolumns the shorter owns the name, the storage columns differ, and the index is not consulted. The five index types able to carry such an index share three condition classes and one matcher; each of the five was measured 0 before and 1 after. Genuine paths keep their index and prune exactly as before.The new parameter is not a second
columns_shadowing_map_subcolumns(Mapkey shadowing, not path ownership). My #119591 adds a same-namedmetadata_snapshotmember to these three classes, so whichever lands second folds one member.Complements #119321 (merged), which fixes the declared-typed-path family (#117938): this change only refuses matches, never adds one and never changes the path probed.
Workflow [PR]
Sync PR [sync-upstream/pr/119641]