Repository navigation
Fix merge function failing with columns not present in all merged tables - #99833
Conversation
…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]>
|
Workflow [PR], commit [112264a] Summary: ✅ AI ReviewSummaryThis PR fixes Missing context
ClickHouse Rules
Final Verdict
|
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]>
…missing-columns-remote
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]>
…missing-columns-remote
…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]>
…missing-columns-remote
…missing-columns-remote
…missing-columns-remote
|
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)); |
There was a problem hiding this comment.
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.
…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]>
…missing-columns-remote
…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]>
LLVM Coverage Report
Changed lines: 90.48% (19/21) · Uncovered code |
|
Hi — this PR may need backporting to 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 |
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]>
The
merge()table function threwUNKNOWN_IDENTIFIERwhen 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 atFetchColumnsstage, 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
replaceColumnsmechanism.Closes #86393
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
merge()table function failing withUNKNOWN_IDENTIFIERerror when querying columns not present in all underlying distributed/remote tables.Documentation entry for user-facing changes
Version info
26.4.1.776