Repository navigation
Fix case-insensitive JSONExtract functions on JSON Object type columns - #102039
antaljanosbenjamin wants to merge 63 commits into
Conversation
JSONExtractStringCaseInsensitive and all other CaseInsensitive variants silently returned empty/default values when used with JSON Object type columns, because runForObjectColumn was called without propagating the case_insensitive template parameter from the Executor class. The fix adds case-insensitive path resolution inside runForObjectColumn: when the function is case-insensitive, the user-provided path is matched against typed paths, dynamic paths, and shared data paths using character-by-character comparison before the subcolumn lookup. Closes #102018
|
Workflow [PR], commit [50d3eaa] Summary: ❌
|
|
I found some bigger issues with |
|
The |
|
The |
…e-json-extract-functions-with-json-type
|
This was fixed by #105146. Let's update the branch. |
|
The remaining CI failure on @groeneai, please investigate the flaky |
|
@alexey-milovidov — fix PR opened at #105194. Confirmed the diagnosis exactly: locally, running the inner SELECT with The fix wraps the inner query in |
|
Please do not merge this PR as is. It fixes some things, but not properly. See this comment. |
The test asserted a deterministic row-by-row output from: SELECT val, avg(toUInt32(val)) FROM t_group_by_lowcardinality GROUP BY val LIMIT 10 SETTINGS max_threads = 1, max_rows_to_group_by = 100, group_by_overflow_mode = 'any'; With `group_by_overflow_mode = 'any'`, only the first `max_rows_to_group_by` distinct keys make it into the hash table — and which keys those are depends on the order in which rows arrive at the aggregator. `LIMIT 10` without `ORDER BY` then selects ten of those keys in hash-iteration order. The `max_threads = 1` clause does not help under `ParallelReplicas` (where the table is sharded across replicas and partial aggregations are merged) or under S3 storage (where reads can be split into independent ranges), so the resulting row set varies between runs. Originally tracked in ClickHouse#36069 (2022). The `-- Tags: no-random-settings` workaround masked the issue for random-settings randomization but the underlying non-determinism returned once `ParallelReplicas` was enabled by default in some CI configurations. Concretely the test now fails ~10–20 times per day on master under `Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel)` — @alexey-milovidov flagged this on ClickHouse#102039. The test is a crash-regression test for "Avoid crash in case of GROUP BY LowCardinality(Nullable(String)) column and group_by_overflow_mode='any'" (PR ClickHouse#29637 / ClickHouse#56057). The query is preserved verbatim, including all the problematic settings; only the assertion is changed to `SELECT count() FROM (...) `, which is always 10. If the underlying crash ever returns, the test will detect it; the specific row values were never deterministic to begin with. Verified locally that the old reference fails under `max_threads = 4` (extra null bucket appears, last row drops — exact diff matches the CI report). The new assertion returns `{"count()":10}` for all parallelism levels. Closes ClickHouse#36069
|
🕵 Merged the latest |
|
This one is not caused by this PR. The numbers come from CIDB (
The test measures the fixed cost per block at |
# Conflicts: # src/Functions/FunctionsJSON.cpp
…itive `JSON` path matching With `json_type_escape_dots_in_keys` the requested key `.` is stored as `%2E`, and the case-insensitive matcher folded that escape sequence too, so it matched an unrelated literal key `%2e`. Compare the escape sequence `%2E` as a unit: on either side it only matches `%2E` on the other. Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts: # src/Formats/FormatSettings.h
|
@antaljanosbenjamin one call shape is still broken on this head ( CREATE TABLE t (id UInt32, j JSON) ENGINE = MergeTree ORDER BY id;
INSERT INTO t VALUES (1, '{"t": {"n": 5, "m": 6}, "a": [1, 2]}');
SELECT JSONExtract(j, 't', 'Tuple(n UInt8, m UInt8)') FROM t; -- (0,0), toString(j) gives (5,6)
SELECT JSONExtract(j, 't', 'Nullable(Tuple(n UInt8, m UInt8))') FROM t; -- NULL
SELECT JSONExtract(j, 'a', 'Array(Int64)') FROM t; -- Code: 43 ... cannot be inside Nullable
SELECT JSONExtract(j, 't', 'Map(String, UInt8)') FROM t; -- Code: 43
Which do you prefer: (1) this PR also sends |
…Extract*` - Write a narrower floating-point value (`Float32`, `BFloat16`) promoted to `Float64` in the internal round-trip serialization, because the text is parsed back as `Float64`: the shortest representation of `Float32` `1234567808` is `1234567800`. - Choose the subcolumn route only when the result type can be produced by `accurateCastOrNull`; `Array`, `Map`, and tuples containing them go through the parser. - When the requested path descends into a typed path holding a whole object (`JSON(Obj JSON)`, `JSON(Obj Tuple(Key Int64))`), read it from the JSON text, both for the case-insensitive and the case-sensitive functions; `runForObjectColumn` returns `nullptr` for such paths. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failures: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=102039&sha=a93170bb4c6a88471d2eddb19414a11a072800fc&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29 ( |
|
Both failures are already fixed on master by merged PRs of mine. Neither fix was in the build tested at
|
…ts constant in `JSONExtract*` - A `Tuple` result type is always produced by the parser: the stored value is an object, which cannot be cast to a `Tuple`, so `JSONExtract(j, 't', 'Tuple(n UInt8, m UInt8)')` silently returned `(0,0)`. - Every path that descends into a typed path is read from the JSON text, whatever the type of the typed path is (`Map`, `Dynamic`, ... in addition to `JSON` and `Tuple`). - With non-constant keys, a group of rows that cannot be read from subcolumns goes through the parser alone; the other groups keep the results of the subcolumn route, so the result of a row no longer depends on the keys of the other rows of the block. - `runForObjectColumn` no longer expands a constant `JSON` object to one copy per row: the value is extracted once from its single row and the result is constant. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…lts from `JSON` columns The test, added on master after this branch was created, pinned the default `(0,0)` that `JSONExtract` returned for a `Tuple` result from an untyped path of a `JSON` column. That default came from an unsupported cast of the stored value, and this pull request intentionally replaces it: a `Tuple` result is now produced from the JSON text of the stored value, the same as for the equivalent JSON string. So `json_extract_named_tuples_as_objects` applies to such values as well; the description of the setting is updated accordingly. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
🕵 @groeneai, option (1) is done here: in 7e3ac49 The same commit addresses the four review threads (constant object kept constant, parser fallback local to a key group, any typed parent). 50d3eaa updates The only test failure on the previous head, |
|
Verified on a debug build of Two result types still differ from CREATE TABLE t (id UInt32, j JSON) ENGINE = MergeTree ORDER BY id;
INSERT INTO t VALUES (1, '{"s": "x"}');
SELECT JSONExtract(j, 's', 'LowCardinality(String)') FROM t; -- Code: 43 ... not supported for accurateCastOrNull, toString(j) gives 'x'
SELECT JSONExtract(j, 'e', 'Enum8(''a'' = 1, ''b'' = 2)') FROM t; -- Code: 691 Unexpected value 0 in enum, toString(j) gives 'a'
Should this PR also cover these two, or should I send a follow-up once it is merged? |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The
JSONExtract*functions now handle several previously broken call shapes on columns of theJSONdata type: case-insensitive functions such asJSONExtractStringCaseInsensitivematch keys case-insensitively instead of silently returning a default value;JSONLength,JSONTypeand the structural extractors (JSONExtractKeys,JSONExtractKeysAndValues,JSONExtractKeysAndValuesRaw,JSONExtractArrayRaw) no longer return a default value or fail withILLEGAL_TYPE_OF_ARGUMENT; and the root form without a path, an integer index, and a non-constant key work as well. Other documented differences betweenJSONcolumns and JSON strings remain.Documentation entry for user-facing changes
The
JSONExtract*functions accept a value of theJSONdata type as their first argument, and they take a fast path for it: instead of parsing a JSON document, the value stored at the requested path is read as a subcolumn and cast to the function's result type. That fast path is what the value-extracting functions need, and only they, which is where the reported misbehavior came from.runForObjectColumnwas called without propagating thecase_insensitivetemplate parameter of theExecutor, so the subcolumn lookup was case-sensitive and the function silently returned a default value. The requested path is now matched against typed paths, dynamic paths and shared-data paths without regard to case. Because two keys that differ only in case can both be present, and each row may carry either of them, the path is resolved per row, preferring a candidate that carries a real value over a typed path that only holds its default (which is indistinguishable from an absent key, see JSONExtractRaw returns empty string for typed JSON paths when value equals type default (0 for UInt32, '' for String) #101721).JSONExtractRaw(json_column)) has no subcolumn to read and used to fall into an empty-path branch that returned the default value. Each row is now serialized back to JSON text — with the caller'sFormatSettings, so that a setting such asjson_type_escape_dots_in_keysround-trips — and handled by the ordinary string implementation.JSONcolumn:JSONLength(j, 'a')returned0andJSONType(j, 'a')returnedNullfor a value that is a real object or array, and the structural extractors failed withType Array(...) is not supported for accurateCastOrNull because it cannot be inside Nullable. They now go through the JSON text of the row as well.JSONExtractRaw(j, 'a', 1)orJSONKey(j, 1), and a non-constant key — used to throwsupports only constant string path arguments. They take the same route, so the restriction is gone.Array,MapandTuple, as inJSONExtract(j, 't', 'Tuple(n UInt8, m UInt8)')— used to throwILLEGAL_TYPE_OF_ARGUMENTor to silently return the default tuple. They are produced from the JSON text as well, sojson_extract_named_tuples_as_objectsapplies to them as it does to a JSON string. The same holds for a path that descends into a typed path (JSON(Obj Map(String, Int64)),JSON(Obj Dynamic), ...), whose inner keys are not stored paths of the column.These changes make the repaired call shapes behave consistently with JSON strings where the
JSONdata type retains the necessary information. Other documented differences remain, including droppednullkeys, the type storage order of object keys, and nativeJSONconversion semantics.Every case is covered in
04092_json_extract_case_insensitive_object_columnby asserting theJSON-column result against the same call on the equivalent JSON string.Closes #102018
Related: #102079
Workflow [PR]
Sync PR [sync-upstream/pr/102039]