Skip to content

Fix merge function failing with columns not present in all merged tables - #99833

Merged
alexey-milovidov merged 18 commits into
masterfrom
fix-merge-function-missing-columns-remote
Apr 10, 2026
Merged

alexey-milovidov merged 18 commits into
masterfrom
fix-merge-function-missing-columns-remote

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 18, 2026 •

Copy link
Copy Markdown
Member

The merge() table function threw UNKNOWN_IDENTIFIER when querying columns that exist in some but not all underlying tables — but only for distributed/remote tables. Local MergeTree tables worked fine because they only received column read requests at FetchColumns stage, with missing columns added as defaults later.

For distributed/remote tables, the processing stage is above FetchColumns, so the full query (including references to missing columns) gets sent to the child table. The child table's analyzer then fails to resolve columns that don't exist in its schema.

The fix replaces references to missing columns in the query tree with constant default values (matching the column's type default) before the query is sent to the child table, using the existing replaceColumns mechanism.

Closes #86393

Changelog category (leave one):

  • Improvement

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

Fix merge() table function failing with UNKNOWN_IDENTIFIER error when querying columns not present in all underlying distributed/remote tables.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.776

…ables

When the `merge()` table function operates over tables with different
schemas (e.g., distributed/remote tables), columns that exist in some
but not all underlying tables caused an `UNKNOWN_IDENTIFIER` error.

The issue occurred because when the processing stage is above
`FetchColumns` (typical for distributed/remote tables), the full query
including references to missing columns was sent to child tables for
processing. Local `MergeTree` tables worked fine because they only
received column read requests, with missing columns added as defaults
later by `convertAndFilterSourceStream`.

The fix replaces references to missing columns in the query tree with
constant default values (matching the column's type default) before
the query is sent to the child table. The existing `replaceColumns`
mechanism already used for virtual column substitution handles this.

Closes #86393

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [112264a]

Summary: ✅


AI Review

Summary

This PR fixes merge() over heterogeneous remote/distributed tables by substituting missing referenced columns with type-default constants before child-query execution, aligning remote behavior with local MergeTree behavior. I reviewed the changed C++ path and added tests; I did not find new correctness, safety, or performance blockers beyond already-addressed discussion points.

Missing context
  • ⚠️ Full CI signal is not available yet in this run (Style check/Code Review still in progress; Fast test/Build (arm_tidy) not finished), so final merge confidence still depends on CI completion.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Mar 18, 2026
Comment thread src/Storages/StorageMerge.cpp Resolved
alexey-milovidov and others added 4 commits March 18, 2026 09:44
The fix is in the new analyzer code path only, so the test must
explicitly enable it to avoid failure in old analyzer CI jobs.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…ution in `merge`

The default-substitution loop for missing columns incorrectly replaced
`_table` with an empty string for remote/distributed child storages.
These virtual columns are handled separately by the Merge engine
(added as plan steps after the child query executes) and should not
be substituted with default values.

Added a regression test for `_table` through remote `merge()`.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The `_table` virtual column is not properly handled for `remote()` table
wrappers in the analyzer path. This is a pre-existing issue: commit
55968ae intentionally commented out the `_table` replacement in the
query tree to allow nested Merge tables to resolve `_table` to leaf
table names. This makes `_table` unavailable when `merge()` wraps
`remote()` tables over non-Merge storages.

Remove this test case since it tests a separate issue from the core fix
(handling missing physical columns in different schemas).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Comment thread src/Storages/StorageMerge.cpp Resolved
alexey-milovidov and others added 8 commits March 28, 2026 23:50
…itution

The merge-side `tryGetColumn` call was missing `withSubcolumns`, so subcolumns
(e.g. Tuple element access like `t.1`) that exist in the merged schema but not
in a child table were not found and therefore not substituted with defaults.
This could propagate unresolved identifiers to child queries and reproduce
`UNKNOWN_IDENTIFIER` for heterogeneous subcolumn layouts in the remote/distributed
path.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…bles in `merge`

Tests that Tuple subcolumn access (e.g. `t.1`) works correctly when one table
in a `merge()` has the Tuple column and the other does not, for both local
and remote table wrappers.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

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.

continue;

column_name_to_node.emplace(column_name,
std::make_shared<ConstantNode>(merge_column->type->getDefault(), merge_column->type));

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.

This will use the data type's default value. Is it going to work correctly when a table has custom default expressions? Write a test.

alexey-milovidov and others added 4 commits April 7, 2026 14:53
…ing columns

The test verifies that when `merge()` is used over tables with different
schemas and one table has a column with a custom DEFAULT expression that
the other table lacks, the type's zero default is consistently used for
both local and remote/distributed paths.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…e-function-missing-columns-remote

# Conflicts:
#	src/Storages/StorageMerge.cpp
…` to avoid prefix collision with master

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.00% -0.10%
Functions 90.90% 90.90% +0.00%
Branches 76.60% 76.50% -0.10%

Changed lines: 90.48% (19/21) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

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.

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Apr 10, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Apr 10, 2026
Merged via the queue into master with commit 8d58309 Apr 10, 2026
163 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-merge-function-missing-columns-remote branch April 10, 2026 04:36
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 10, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this PR may need backporting to 26.3 (LTS), 26.2, 26.1, 25.8 (LTS), but no backport label was found.

Affected code: ReadFromMerge::getModifiedQueryInfo in StorageMerge.cpp — missing column handling for remote tables above FetchColumns stage — core code present in all supported branches.

Why: The fix addresses a user-reported error (UNKNOWN_IDENTIFIER) in merge() with remote tables having different schemas. The affected function getModifiedQueryInfo in StorageMerge.cpp predates all supported branches (introduced in v21.x per git log). All supported branches have the analyzer enabled by default and would exhibit this bug.

If this should be backported, consider adding pr-must-backport or a version-specific label (e.g. v26.3-must-backport). Ignore this if backporting is not applicable.

alexey-milovidov pushed a commit to amosbird/ClickHouse that referenced this pull request Oct 15, 2026
When a table behind `merge()` / `Merge` lacks a column, `ReadFromMerge::getModifiedQueryInfo`
replaces the column in that table's query tree with the default of the `Merge` type. It was a
bare `ConstantNode` with no source expression, a node the analyzer never produces from SQL.

A table read above `FetchColumns` (a `Distributed` table, a `remote()` wrapper, or a local table
when another table forces a later stage) re-analyzes the query printed from that tree, and a bare
literal does not survive the round trip:

- `Map` prints as `map()` and comes back as `Map(Nothing, Nothing)`: `Code: 8` on a remote child
  (`max([]_Map(String, UInt64))` vs `max([]_Map(Nothing, Nothing))`), `Code: 43` from `mapKeys([])`,
  `Code: 70` on a local child (`AggregateFunction(any, Map(Nothing, Nothing))`).
- `Decimal` always prints as `_CAST(...)`, while the initiator names the bare constant without the
  cast: `Code: 8` / `Code: 10` (`min('0'_Decimal(10, 2))` vs `min(_CAST('0'_Decimal(10, 2), ...))`).
- An integer default in `GROUP BY` / `ORDER BY` / `LIMIT BY` is read back as a positional argument:
  `Code: 36 Positional argument number 0 is out of bounds`.

Build the default the way the analyzer resolves `_CAST(<default>, '<type>')`, with
`createCastFunction` + `foldConstantCast`: the folded constant keeps `_CAST` as its source
expression, so it is printed as `_CAST(...)`, re-analyzed to the same type, never taken as a
position, and named the same on the initiator and on every receiver. Types whose default already
printed with a cast are unchanged; for the others only the constant's name changes.

Related: ClickHouse#99833, ClickHouse#86393.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

merge function fails with columns not present in all merged tables

3 participants