Skip to content

Fix DEFAULT expression not applied to explicit null in JSONColumns formats - #127113

Open
alexprabhat99 wants to merge 23 commits into
ClickHouse:masterfrom
alexprabhat99:fix-jsoncolumns-null-as-default-expression
Open

alexprabhat99 wants to merge 23 commits into
ClickHouse:masterfrom
alexprabhat99:fix-jsoncolumns-null-as-default-expression

Conversation

@alexprabhat99

@alexprabhat99 alexprabhat99 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #121905

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix the JSONColumns, JSONCompactColumns and JSONColumnsWithMetadata input formats inserting the type default instead of the column's DEFAULT expression for an explicit null with input_format_null_as_default. Also fix UNKNOWN_IDENTIFIER when a DEFAULT expression uses a column which is not read: in INSERT INTO t (columns) FROM INFILE, and when reading a subset of columns from file, url or s3.


Workflow [PR]
Sync PR [sync-upstream/pr/127113]

@clickhouse-gh clickhouse-gh Bot added comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). external Author is not a member of the ClickHouse organization labels Oct 21, 2026
@NIKTONIKTO717 NIKTONIKTO717 self-assigned this Oct 22, 2026
@NIKTONIKTO717 NIKTONIKTO717 added the can be tested Allows running workflows for external contributors label Oct 22, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [660cdbe]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/can_be_merged.py --review-threads FAIL

AI Review

Summary

The PR fixes explicit-null default tracking in the columnar JSON formats and expands default dependencies for file-like reads and FROM INFILE imports. Reviewed head 660cdbe0. Scalar identifier bindings remain inconsistent with the new scope-aware subcolumn handling, causing incorrect inserted values and a subset-read regression. Changes are required.

Missing context / blind spots
  • ⚠️ Head binaries were not yet published. Runtime repros used the ARM release binary at a5b8bc89 and a master binary; comparison with the latest source confirms the relevant scalar dependency logic is unchanged. Run the focused regressions on the final head to complete runtime validation.
Findings

❌ Blockers

  • src/Interpreters/RequiredSourceColumnsData.cpp:85: Ordinary identifiers do not receive scope-aware alias resolution. With a UInt32 DEFAULT z and b UInt32 DEFAULT arraySum(arrayMap(x -> (toUInt32(2) AS a), [1, 2])) + a, inserting JSONColumns data {"z":[7,9],"a":[5,null],"b":[null,null]} stores b = [11,13] instead of [9,13]. The lambda-local alias hides the outer dependency, so default evaluation recomputes a from z even for the row supplying a = 5. Reversing the operands restores the correct result. This scalar ordering defect already affects JSONEachRow and is now exposed by the explicit-null fix for JSONColumns.

    The same incomplete binding model also adds false dependencies: for {"z":"oops","c":2,"b":null}, selecting c, b from file with schema z String, a UInt32 DEFAULT toUInt32(z), c UInt32, b UInt32 DEFAULT arraySum(arrayMap(x -> (c AS a) + a, [1, 2])) returns (2,8) on master but throws CANNOT_PARSE_TEXT in the PR. The local identifier alias is treated as the physical a. Replacing (c AS a) with (c + 0 AS a) avoids the exception.

    Resolve scalar and compound references through the same scope-aware binding rules for every alias-bearing AST node, and share those results across dependency collection, pruning, and ordering.

Tests
  • ⚠️ Add the mixed present/missing scalar-scope insert and the scalar identifier-alias subset read above. Existing regressions cover dotted aliases but do not establish the invariant for ordinary identifiers.
Final Verdict

Status: ❌ Block. Complete scalar alias binding in default dependency analysis and add the two focused regressions.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 22, 2026
@alexprabhat99

Copy link
Copy Markdown
Contributor Author

Hi @NIKTONIKTO717
Can this be merged? All checks have passed.
Thank you.

@alexprabhat99

Copy link
Copy Markdown
Contributor Author

Hi @NIKTONIKTO717
Just a gentle reminder regarding the PR merge. Could you please take a look whenever you get a chance? Thank you!

@NIKTONIKTO717

Copy link
Copy Markdown
Member

I'm sorry @alexprabhat99 , I managed to do a code review but not to publish it 🤦. Would you mind please to take a look? Thanks!

Comment thread src/Client/ClientBase.cpp Resolved
Comment thread src/Client/ClientBase.cpp Resolved

@ch-samuel-reviewer-1 ch-samuel-reviewer-1 left a comment

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.

Thank you @alexprabhat99 for addressing the comments, and sorry again for the delay on my side! The fixes look good to me: on master an explicit null in JSONColumns, JSONCompactColumns and JSONColumnsWithMetadata is inserted as 0 instead of a * 2, and both SELECT b FROM file(..., Parquet, 'a UInt32, b UInt32 DEFAULT a * 2') and INSERT INTO t (b) FROM INFILE ... FORMAT JSONEachRow throw UNKNOWN_IDENTIFIER. With this PR, all of them take the DEFAULT.

I agree with both findings of the AI review, so I think these still need to be addressed:

  • The lambda capture in substituteNotInsertedColumns. With the AI review's table (a UInt32 DEFAULT z * 2, b UInt32 DEFAULT arraySum(arrayMap(z -> a + z, [1, 2]))), INSERT INTO tl (z, b) FROM INFILE of {"z":10,"b":null} inserts b = 9, while the same row as inline data or in VALUES gives 43. On master this FROM INFILE throws UNKNOWN_IDENTIFIER, so with this PR the error becomes a silently wrong value.
  • With input_format_defaults_for_omitted_fields = 0, SELECT b FROM file(..., JSONColumns, 'a UInt32, b UInt32 DEFAULT a * 2') of {"a":["oops"],"b":[3]} now parses a and fails with CANNOT_PARSE_INPUT_ASSERTION_FAILED, while master returns 3.

Could you please add both cases to the tests as well?

Could you please also update the changelog entry? The PR now fixes more than JSONColumns: INSERT INTO t (columns) FROM INFILE where a DEFAULT uses a column that isn't inserted (in any format, JSONEachRow included), and reading a subset of columns from file/url/s3 where a DEFAULT of a requested column uses a column that isn't read. For example:

Fix the JSONColumns, JSONCompactColumns and JSONColumnsWithMetadata input formats inserting the type default instead of the column's DEFAULT expression for an explicit null with input_format_null_as_default. Also fix UNKNOWN_IDENTIFIER when a DEFAULT expression uses a column which is not read: in INSERT INTO t (columns) FROM INFILE, and when reading a subset of columns from file, url or s3.

This review was checked, modified and approved by human (@NIKTONIKTO717).

…OM INFILE` defaults, and skip reading the columns of `DEFAULT` expressions without `input_format_defaults_for_omitted_fields`
Comment thread src/Client/ClientBase.cpp Resolved
Comment thread src/Client/ClientBase.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 660cdbe0f with master 5c48ad1cc (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

41 object files changed (+207.31 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Client/ClientBase.cpp.o 1.46 MiB 1.52 MiB +51.86 KiB (+3.46%)
src/CMakeFiles/dbms.dir/Interpreters/RequiredSourceColumnsVisitor.cpp.o 82.70 KiB 111.45 KiB +28.75 KiB (+34.77%)
src/CMakeFiles/dbms.dir/Processors/Transforms/AddingDefaultsTransform.cpp.o 305.46 KiB 330.26 KiB +24.80 KiB (+8.12%)
src/CMakeFiles/dbms.dir/Storages/prepareReadingFromFormat.cpp.o 347.43 KiB 369.25 KiB +21.82 KiB (+6.28%)

711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

155 translation units recompiled, 1763 s compile time in total, 155 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×0.93 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost -135.8 s (-7%) in total before that adjustment.

Job report

… defaults, and compute the defaults of columns read only for a `DEFAULT` only when that `DEFAULT` is computed
Comment thread src/Client/ClientBase.cpp Resolved

@ch-samuel-reviewer-1 ch-samuel-reviewer-1 left a comment

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.

Thank you @alexprabhat99 for addressing the comments! Both cases from my previous review I consider fixed now. Thanks also for adding the tests and for updating the changelog entry.

Once the open threads of the AI review are addressed, I'll be happy to take another look.

This review was checked, modified and approved by human (@NIKTONIKTO717).

Comment thread src/Client/ClientBase.cpp Resolved
…ake subcolumns of lambda arguments for columns read by defaults
Comment thread src/Client/ClientBase.cpp Resolved
…take subcolumns of aliases for columns read by defaults

@ch-samuel-reviewer-1 ch-samuel-reviewer-1 left a comment

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.

Thank you @alexprabhat99 for addressing the comments! I left one more comment, which will be marked as resolved just for the sake of having it mentioned, since we consider this trade-off.

This review was checked, modified and approved by human (@NIKTONIKTO717).

@alexprabhat99

Copy link
Copy Markdown
Contributor Author

Hi @NIKTONIKTO717
Can this be merged? All checks have passed.
Thank you.

@NIKTONIKTO717

Copy link
Copy Markdown
Member

Hi @NIKTONIKTO717 Can this be merged? All checks have passed. Thank you.

It is very likely to be approved soon. I'm going through it one more time. Thank you @alexprabhat99; I don't think any action on your part will be necessary.

@alexprabhat99

Copy link
Copy Markdown
Contributor Author

@NIKTONIKTO717 The latest commit is causing the tests to fail. All checks were passing before this commit.

…ull-as-default-expression

# Conflicts:
#	src/Storages/prepareReadingFromFormat.h
…ull-as-default-expression

# Conflicts:
#	src/Interpreters/RequiredSourceColumnsData.h
Comment thread src/Client/ClientBase.cpp Resolved
if (array_join_columns.contains(name) || array_join_columns.contains(table_name))
continue;

if (mask_subcolumns_of_aliases && isSubcolumnOfAliases(name, pr.second.appears))

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.

[Blocker] Apply scope-aware dependency resolution to ordinary identifiers too

The new scope map is consulted only for compound identifiers. Plain identifiers still use the flat complex_aliases set, so a lambda-local alias can hide a real column reference outside that lambda. This leaves the dependency order incorrect for an input whose explicit nulls this PR now defaults:

CREATE TABLE t
(
    z UInt32,
    a UInt32 DEFAULT z,
    b UInt32 DEFAULT
        arraySum(arrayMap(x -> (toUInt32(2) AS a), [1, 2])) + a
) ENGINE = Memory;

INSERT INTO t FORMAT JSONColumns
{"z":[7,9],"a":[5,null],"b":[null,null]}

SELECT z, a, b FROM t ORDER BY z;

The PR inserts (7, 5, 11) and (9, 9, 13); the first b must be 9. Because the lambda alias is visited first, the outer a is omitted from the dependency graph. Both defaults are evaluated together, and b uses z instead of the supplied a = 5. Moving the outer a before the lambda changes the result to the correct value. The scalar case already affects JSONEachRow; this PR also exposes it through JSONColumns.

There is an inverse failure for aliases on identifier nodes: with x.jsonl containing {"z":"oops","c":2,"b":null}, this valid subset read returns (2, 8) on master but throws CANNOT_PARSE_TEXT in the PR:

SELECT c, b FROM file('x.jsonl', JSONEachRow,
    'z String, a UInt32 DEFAULT toUInt32(z), c UInt32,
     b UInt32 DEFAULT arraySum(arrayMap(x -> (c AS a) + a, [1, 2]))');

Here the local scalar alias is mistaken for the physical a, whose unrelated default is evaluated. Recording (c AS a) in alias_scopes does not fix scalar lookups. Reproduced with the ARM release binary at a5b8bc89; the relevant scalar lookup logic remains unchanged at 660cdbe0.

Please use shared scope-aware resolution for both scalar and compound identifiers, including every alias-bearing node, in dependency collection, pruning, and ordering. Add both focused regressions instead of another subcolumn-only exception.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSONColumns formats insert the type default instead of the DEFAULT expression for an explicit null under input_format_null_as_default

3 participants