Skip to content

Optimize json.path[N].nested.path to json.path[].nested.path[N] - #99802

Merged
Avogar merged 16 commits into
masterfrom
nested-json-array-path-optimization
May 13, 2026
Merged

Avogar merged 16 commits into
masterfrom
nested-json-array-path-optimization

Conversation

@Avogar

@Avogar Avogar commented Mar 17, 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):

Optimize json.path[N].nested.path (which is tupleElement(tupleElement(json.path[N], 'nested'), 'path'), ) to json.path[].nested.path[N] which reads much less data

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.602

@clickhouse-gh

clickhouse-gh Bot commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5ab38d3]

Summary: ❌

job_name test_name status info comment
Code Review FAIL IGNORED

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

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, except the fact that there is no setting to turn this optimization on/off. Which means if we discover a subtle bug, it will be harder to work around.

@alexey-milovidov alexey-milovidov self-assigned this Apr 3, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@alexey-milovidov

Copy link
Copy Markdown
Member

The MSan stress test failure (MemorySanitizer: use-of-uninitialized-value, STID 4179-5154 or 4148-3044) is a known pre-existing issue unrelated to this PR. Fix: #102158

/// Build the new subcolumn name: "col.:`Array(JSON)`.f1.f2...fK".
String new_col_name = ctx.column.name + ".:`Array(JSON)`";
for (const auto & field : field_names)
new_col_name += "." + field;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The rewritten subcolumn name is assembled with raw field segments (new_col_name += "." + field). For quoted JSON keys that contain dots (e.g. ``json.a.b[1].`c.d```), `field` is `c.d`, so this produces `... .c.d` instead of a single escaped segment. As a result, `tryGetColumn` cannot resolve the intended `Array(JSON)` subcolumn and the optimization is skipped for this key shape.

Could you build the path with proper escaping/identifier concatenation for each segment (instead of raw string concatenation), and add an EXPLAIN assertion for the dotted-key case to lock this behavior?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-checking the current head, this is still live: new_col_name is built via raw concatenation (new_col_name += "." + field), so a quoted key segment like `c.d` is turned into .c.d instead of one escaped segment. The tryGetColumn lookup then misses the intended Array(JSON) subcolumn, so the optimization is skipped for this key shape.

Could you build each appended segment with identifier escaping (so dotted keys remain a single path element) and add an EXPLAIN QUERY TREE assertion for the dotted-key case to lock this behavior?

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.

so a quoted key segment like `c.d` is turned into .c.d instead of one escaped segment

What? This is something strange. Why should we escape anything? Segments in JSON path subcolumn should not be escaped

@alexey-milovidov

Copy link
Copy Markdown
Member

The flaky check failure is fixed in #102148, let's update the branch.

@Avogar

Avogar commented Apr 16, 2026

Copy link
Copy Markdown
Member Author

except the fact that there is no setting to turn this optimization on/off. Which means if we discover a subtle bug, it will be harder to work around.

This optimization is guarded by optimize_functions_to_subcolumns setting. Or you think we need a separate setting?

@clickhouse-gh

clickhouse-gh Bot commented May 12, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 91.67% (143/156) · Uncovered code

Full report · Diff report

@Avogar
Avogar added this pull request to the merge queue May 13, 2026
Merged via the queue into master with commit 50adbc2 May 13, 2026
324 of 327 checks passed
@Avogar
Avogar deleted the nested-json-array-path-optimization branch May 13, 2026 10:39
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label May 13, 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.

3 participants