Skip to content

Improve JSON subcolumn resolution with type hints on typed paths - #99179

Merged
Avogar merged 13 commits into
masterfrom
json-array-object-subcolumn-magic
May 12, 2026
Merged

Avogar merged 13 commits into
masterfrom
json-array-object-subcolumn-magic

Conversation

@Avogar

@Avogar Avogar commented Mar 10, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Allow json.path[] syntax sugar and explicit type hints to work correctly on typed JSON paths.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Note

Medium Risk
Touches JSON DataTypeObject subcolumn parsing and SerializationObject::enumerateStreams behavior, which can affect subcolumn discovery/resolution across queries and serialization paths. Added logic is localized but involves nuanced string parsing (typed-path prefix matching and JSON-parameter normalization).

Overview
Improves JSON subcolumn resolution for typed JSON paths so that nested access and [] sugar behave consistently with dynamic paths, and redundant explicit type hints (e.g. .:`Array(JSON)`) on typed paths are stripped when they match the declared typed-path type (after normalizing away JSON(...)` parameters).

This refactors subcolumn parsing to split requests by the longest typed-path prefix before falling back to parsing .:`Type`markers, and addsremoveJSONTypeParameters()` to compare type hints robustly even when JSON parameters include back-quoted strings.

Updates SerializationObject::enumerateStreams to only enumerate typed/dynamic/shared streams when enumerate_dynamic_streams is enabled, forcing JSON subcolumn requests to route through DataTypeObject::getDynamicSubcolumnData for the new hint-stripping behavior. Adds stateless query tests covering typed-vs-dynamic parity, nested subcolumns on typed paths, mismatched hints, and JSON-parameter edge cases.

Written by Cursor Bugbot for commit a699e56. This will update automatically on new commits. Configure here.

Version info

  • Merged into: 26.5.1.578

Avogar added 2 commits March 10, 2026 13:45
When a JSON column declares a typed path like json JSON(a Array(JSON)), the [] syntax sugar transforms json.a[].x into json.a.:Array(JSON).x. The type hint is redundant for typed paths and caused incorrect resolution. Detect and strip such redundant hints in getDynamicSubcolumnData by comparing type names after normalizing JSON parameters. Move typed paths enumeration inside the enumerate_dynamic_streams guard so all subcolumn requests go through this path.
@clickhouse-gh

clickhouse-gh Bot commented Mar 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [168c537]

Summary: ❌

job_name test_name status info comment
Docker server image failure
clickhouse/clickhouse-server:head-amd64 failure cidb
docker library image test (clickhouse/clickhouse-server:head-amd64) failure cidb

AI Review

Summary

This PR improves JSON typed-path subcolumn resolution (especially [] sugar and explicit type hints) and adds broad stateless coverage. I found one blocker still present: the new type-hint matching logic strips all JSON(...) parameters before comparison, so semantically different JSON type hints can be treated as equal and silently ignored.

Findings

❌ Blockers

  • [src/DataTypes/DataTypeObject.cpp:680] removeJSONTypeParameters removes all JSON(...) parameters before checking whether an explicit type hint matches a typed path. This makes incompatible hints compare equal (for example Array(JSON(x UInt64)) vs Array(JSON(y String))), so mismatched explicit hints can be stripped and resolved through the typed-path branch with incorrect semantics.
    Suggested fix: compare parsed types structurally, or only normalize non-semantic JSON parameters (such as max_dynamic_paths / max_dynamic_types) while keeping schema-defining parameters (x UInt64, y String, SKIP ...) in the comparison.
Tests
  • ⚠️ Add a negative test where typed path is Array(JSON(x UInt64)) but query uses explicit hint .:\Array(JSON(y String))`and then accesses.x`; this should not be treated as a matching hint and must preserve mismatch behavior.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ⚠️ Explicit hint semantics can change due to over-broad normalization of JSON(...) parameters.
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ⚠️ Incorrect hint stripping can surface as silent query-result semantic drift for typed JSON paths.
Compilation time ✅
No large/binary files ✅
Performance & Safety

The issue is primarily semantic correctness rather than raw performance: it can route subcolumn resolution through a branch that assumes the hint matched, even when the hint encoded a different JSON schema.

Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required action: tighten type-hint equality so only truly equivalent JSON types are stripped, and add the negative coverage above.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Mar 10, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Comment thread src/DataTypes/DataTypeObject.cpp Resolved
Comment thread src/DataTypes/DataTypeObject.cpp Resolved
Comment thread src/DataTypes/DataTypeObject.cpp Resolved
Avogar added a commit that referenced this pull request Apr 2, 2026
…Data`

In `IDataType::getSubcolumnData`, the `!res` guard on the exact-name branch
prevented an exact match from overriding a `res` already set by the prefix
(dynamic subcolumn) branch. Because subcolumns are iterated in sorted order,
a Tuple element `a` (JSON) would match as a prefix of `a.b` and set `res`,
then the exact match on element `a.b` (UInt32) would be skipped, causing a
server exception via `ColumnDynamic::setVariantType`.

Fix: track `res_from_exact_match` separately. Exact matches always override
any prior prefix match; prefix matches are only taken when no exact match has
been found yet.

Fixes #101271
Extracted from #99179

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 90.90% 90.90% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 95.59% (65/68) · Uncovered code

Full report · Diff report

@Avogar

Avogar commented Apr 17, 2026

Copy link
Copy Markdown
Member Author

❌ Blockers
[src/DataTypes/DataTypeObject.cpp:680] removeJSONTypeParameters removes all JSON(...) parameters before checking whether an explicit type hint matches a typed path. This makes incompatible hints compare equal (for example Array(JSON(x UInt64)) vs Array(JSON(y String))), so mismatched explicit hints can be stripped and resolved through the typed-path branch with incorrect semantics.
Suggested fix: compare parsed types structurally, or only normalize non-semantic JSON parameters (such as max_dynamic_paths / max_dynamic_types) while keeping schema-defining parameters (x UInt64, y String, SKIP ...) in the comparison.

It's not the blocker. It's expected. We need json.path[] to read path path with any type hint Array(JSON(...))

@thevar1able thevar1able self-assigned this Apr 23, 2026
@Avogar
Avogar added this pull request to the merge queue May 12, 2026
Merged via the queue into master with commit 04b22c4 May 12, 2026
159 of 161 checks passed
@Avogar
Avogar deleted the json-array-object-subcolumn-magic branch May 12, 2026 17:57
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @Avogar @thevar1able — while reviewing this PR I found the following:

Happy to discuss — close anything that's wrong or already addressed.

@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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