Repository navigation
Conversation
`JSONValues(json, 'path1', 'path2', ...)` returns an `Array(String)` of
values at the specified paths, in argument order. Absent or null paths are
omitted from the row's array.
This complements `JSONAllValues` (which returns all paths) and enables
focused text indexes covering only the paths you care about:
INDEX idx JSONValues(json, 'type.name', 'player.name')
TYPE text(tokenizer = 'ngrams') GRANULARITY 100000000
The two free helper functions (`serializeValueIntoResult`,
`emitSharedDataValue`) are shared between both functions in the same file.
Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
`MergeTreeIndexConditionText` now recognises indexes built on `JSONValues(col, 'path1', 'path2', ...)` when filtering on subcolumns of `col`. A query like `WHERE json.player.name LIKE '%x%'` will use the index if `player.name` is one of the listed paths. The matching logic lives in `tryMatchNodeToJSONValuesIndex` / `tryMatchColumnToJSONValuesIndex` (added to `MergeTreeIndexJSONSubcolumnHelper`), which parses the index expression string to extract the JSON column name and path list, then checks the queried subcolumn path against that list. Paths not listed in the index (e.g. `json.score`) correctly fall through without triggering the index. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
|
Workflow [PR], commit [71b296b] AI ReviewSummaryThis PR adds PR Metadata
Findings❌ Blockers
Tests
Final Verdict
|
…e positive in example data
|
Maybe it's better to pass paths as an array? |
…typed path omission - Change `JSONValues(json, 'p1', 'p2', ...)` to `JSONValues(json, ['p1', 'p2', ...])` so the paths are a single constant `Array(String)` argument, consistent with how other ClickHouse functions accept array-valued parameters - Fix typed paths being emitted unconditionally: add `isDefaultAt` check so typed paths that were absent in the original JSON (stored as the type default) are omitted from the result, consistent with dynamic and shared-data paths - Update `parseJSONValuesColumnName` in `MergeTreeIndexJSONSubcolumnHelper` to parse the new array literal format `JSONValues(col, ['path1', 'path2', ...])` instead of the old variadic format `JSONValues(col, 'path1', 'path2', ...)` - Update test and documentation examples to use array syntax Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…and add regression test
For schema-declared non-nullable typed paths, `ColumnObject` stores the type
default (0 for UInt32, "" for String, false for Bool) for both absent rows and
rows explicitly set to that default value. There is no null map, so the two
cases are indistinguishable at read time.
To avoid emitting spurious placeholder values for absent paths, both cases are
omitted (via the existing `isDefaultAt` check). This is now documented in:
- the class-level comment on `FunctionJSONValues`
- the in-code comment on the typed-path branch
- the `FunctionDocumentation` description
A regression test is added that inserts `{"a":0}`, `{"a":1}`, and `{}` into
a `JSON(a UInt32)` column and asserts that `JSONValues(json, ['a'])` returns
`[]`, `['1']`, `[]` respectively — confirming the documented contract.
Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Update the `tryMatchNodeToJSONValuesIndex` doc comment from the old variadic form `JSONValues(col, 'path1', 'path2', ...)` to the current array form `JSONValues(col, ['path1', 'path2', ...])`. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
yeh good idea. Have updated |
|
This was fixed by #105146. Let's update the branch. |
LLVM Coverage Report
Changed lines: 92.44% (220/238) | lost baseline coverage: 3 line(s) · Uncovered code |
…h defaults `JSONValues` omits typed-path default values from the index (via `isDefaultAt`). When comparing a subcolumn against its type default (e.g. `data.a = 0` for `JSON(a UInt32)`), `FUNCTION_EQUALS` in `mayBeTrueOnGranule` calls `hasAllQueryTokensOrEmpty`, which does not find the token `"0"` and incorrectly skips the granule — a false negative. The fix splits the `JSONAllValues` and `JSONValues` match paths in `traverseFunctionNode`. For `JSONValues` with `equals`, it calls `isJSONPathFilterSafe` (already used by the bloom-filter text index) to detect when the comparison value equals the typed default. In that case the function returns `false`, leaving the RPN element as `FUNCTION_UNKNOWN` (always may-be-true). For non-default values the index is still used normally. `JSONAllValues` is unaffected: it always indexes typed-path defaults, so no guard is needed there. A regression test is added to `04218_json_values` that inserts rows with `a=0`, `a=1`, and absent `a` into a `JSON(a UInt32)` column with a `JSONValues` text index (granularity 1) and asserts that `WHERE data.a = 0` returns both the explicit-zero and absent rows. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
| if (rest[0] != '\'') | ||
| break; | ||
| rest.remove_prefix(1); | ||
| auto end = rest.find('\''); |
There was a problem hiding this comment.
parseJSONValuesColumnName splits each array element at the first ' (rest.find('\'')), so it does not handle SQL-escaped quotes inside string literals.
For example, JSONValues(data, ['a''b']) is parsed here as two paths (a and b) instead of one path (a'b). That violates the contract that we match only explicitly listed paths, and can make tryMatchNodeToJSONValuesIndex treat an unlisted subcolumn as indexed, which risks incorrect granule skipping.
Please parse the array literal with proper SQL string escaping (or reuse an existing SQL literal parser) before path matching.
| || hasIndexForMapElementValue(node) | ||
| || tryMatchNodeToJSONIndex(node, header, "JSONAllValues"); | ||
| || tryMatchNodeToJSONIndex(node, header, "JSONAllValues") | ||
| || tryMatchNodeToJSONValuesIndex(node, header); |
There was a problem hiding this comment.
This IN path now treats JSONValues as indexable via has_index, but unlike the equals branch above it never applies isJSONPathFilterSafe.
JSONValues omits typed defaults, so IN against a default value is still unsafe for non-nullable typed paths. A concrete case is JSON(a FixedString(3)) with tokenizer='ngrams': data.a IN (toFixedString('', 3)) can tokenize to non-empty ngrams, while absent/default rows have no corresponding tokens in the JSONValues index, so matching granules can be skipped.
Please add the same default-value safety check for IN/globalIn set elements before enabling JSONValues-based skipping.
Changelog category (leave one):
Changelog entry:
Add
JSONValues(json, 'path1', 'path2', ...)function that returns anArray(String)of values at the specified paths from a JSON column, in argument order. Absent or null paths are omitted. Supports text indexes for efficient filtering on specific subcolumns.Why is this needed?
When working with a JSON column, there are two existing options for building a text index:
Separate indexes per subcolumn — works but creates multiple index structures and per-path maintenance overhead:
JSONAllValues(json)— a single index covering all paths. This is simpler but the index is larger (covers every path in every row) and produces more false positives at granule-skip time when you only care about a few specific paths.There is no way to get a single index covering only a chosen subset of paths - the ClickHouse docs AI says the following:
So this function would solve the problem. We would use it like this:
Queries on the listed subcolumns use the index; queries on unlisted paths (e.g.,
json.score) would ignore it.What changed
src/Functions/JSONAllValues.cpp— addedFunctionJSONValues. Shared helpers (serializeValueIntoResult,emitSharedDataValue) are extracted as free functions used by bothJSONAllValuesandJSONValues. Per-path lookup uses typed/dynamic column maps (O(1)) andColumnObject::findPathLowerBoundInSharedDatafor binary search in shared data.src/Storages/MergeTree/MergeTreeIndexJSONSubcolumnHelper— addedtryMatchNodeToJSONValuesIndexwhich parsesJSONValues(col, 'path1', 'path2', ...)index expression names and checks whether the queried subcolumn path is one of the listed paths.src/Storages/MergeTree/MergeTreeIndexConditionText.cpp— wiredtryMatchNodeToJSONValuesIndexinto the two call sites that currently check forJSONAllValues.Documentation entry for user-facing changes
Motivation: Users building text indexes on JSON columns currently have no way to target specific paths with a single index.
JSONValuesenables a smaller, more precise index that avoids false positives from unrelated paths.Parameters:
json— a column of typeJSONpath1, path2, ...— one or more constant string path names (dot-separated, e.g.'type.name')Example:
Extra notes:
I did also try to create an index like this:
And it created the index (at least as far as
system.partstold me!) but then didn't use it at query time.