Skip to content

Fix LOGICAL_ERROR "Table has no columns." when Merge matches a dangling Alias - #104790

Merged
azat merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-104740-merge-alias-missing-target
May 19, 2026
Merged

azat merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-104740-merge-alias-missing-target

Conversation

@groeneai

@groeneai groeneai commented May 13, 2026 •

Copy link
Copy Markdown
Collaborator

A Merge table whose regexp matches an Alias storage whose target table
has been dropped aborts the query with an internal LOGICAL_ERROR
"Table has no columns.". This shows up in CI as
STID 3593-560a / 3593-5990
and was flagged on #100752
("Enable snappy compression in HTTP interface") and
#102690
("Fix inserting into Time data type during JSON parsing") — both
unrelated to StorageMerge/StorageAlias.

Root cause

  • StorageAlias::getInMemoryMetadataPtr in src/Storages/StorageAlias.h
    returns the alias's own (empty) metadata when its target is missing.
    This is intentional so that metadata-only operations such as
    system.tables/system.columns queries on a dangling alias work
    (exercised by the existing 03636_storage_alias_basic test).
  • ReadFromMerge::createChildrenPlans in src/Storages/StorageMerge.cpp
    iterates the selected children, calls getInMemoryMetadataPtr on each,
    and throws LOGICAL_ERROR "Table has no columns." when the snapshot
    has no columns — assuming the only legitimate empty case is a
    parameterized view.

In CI the path is reached two ways:

  • Stress tests: ignore_drop_queries_probability leaks the
    alias_with_missing_target table from 03636_storage_alias_basic
    into a later test whose Merge(currentDatabase(), 'a') regexp
    matches it.
  • Regular workloads: TOCTOU race when the target is dropped between
    StorageMerge::getSelectedTables (which only locks the alias storage,
    not its target) and the metadata snapshot read in
    createChildrenPlans.

Fix

Detect the Alias case in the empty-metadata branch and throw
UNKNOWN_TABLE with an exception that names both the dangling alias
and the matching Merge table. This mirrors what
StorageMerge::isRemote already surfaces during analyzer resolution
via StorageAlias::isRemote → getTargetTable.

A new regression test 04236_merge_with_alias_missing_target_104740
locks down the user-visible contract: a SELECT through a Merge
whose regexp matches a dangling alias must throw UNKNOWN_TABLE,
never LOGICAL_ERROR.

Closes #104740.

CI evidence: 30-day CIDB shows 6 hits across arm_msan/arm_release/amd_tsan
stress runs on master and 4 distinct unrelated PRs.

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Documentation entry for user-facing changes

  • Documentation is unchanged.

Version info

  • Merged into: 26.5.1.789

…ngling `Alias`

When the regexp of a `Merge` table matches an `Alias` storage whose target
table has been dropped, the query aborted with the internal
`LOGICAL_ERROR` "Table has no columns." raised in
`ReadFromMerge::createChildrenPlans`.

Root cause:
  * `StorageAlias::getInMemoryMetadataPtr` (`src/Storages/StorageAlias.h`)
    returns the alias's own empty metadata when its target is missing
    (`tryGetTargetTable` is null). This is necessary for metadata-only
    operations such as `system.tables`/`system.columns` queries on a
    dangling alias, exercised by the existing
    `03636_storage_alias_basic` test.
  * `ReadFromMerge::createChildrenPlans` (`src/Storages/StorageMerge.cpp`)
    iterates the selected children, calls `getInMemoryMetadataPtr` on
    each, and bails out with `LOGICAL_ERROR` "Table has no columns." if
    the snapshot has no columns — assuming the only legitimate empty
    case is a parameterized view.

In stress tests the path is reached via `ignore_drop_queries_probability`
leaking `alias_with_missing_target` from `03636_storage_alias_basic` into
later tests, where a `Merge(currentDatabase(), 'a')` regexp matches the
dangling alias. In regular workloads the same path is reachable via a
TOCTOU race: the target table is dropped between `getSelectedTables`
(which locks the alias storage, not its target) and the metadata
snapshot read in `createChildrenPlans`.

Fix: detect the `Alias` case in the empty-metadata branch and throw a
user-facing `UNKNOWN_TABLE` exception that names both the dangling alias
and the matching `Merge` table. This mirrors the analyzer-resolution
path, where `StorageMerge::isRemote` already surfaces `UNKNOWN_TABLE`
via `StorageAlias::isRemote` → `getTargetTable`.

A new regression test `04236_merge_with_alias_missing_target_104740`
locks down the user-visible contract: a `SELECT` through a `Merge`
whose regexp matches a dangling alias must throw `UNKNOWN_TABLE`, never
`LOGICAL_ERROR`.

Closes ClickHouse#104740 (STID 3593-560a / 3593-5990).
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (per TASK.md)

# Question Answer
a Deterministic repro? Partial. Local single-threaded repro deterministically reaches StorageMerge::isRemote via the analyzer's resolveQueryJoinTreeNode, which already throws UNKNOWN_TABLE via StorageAlias::isRemote → getTargetTable. The LOGICAL_ERROR branch in createChildrenPlans is only reached in CI when a stress test or TOCTOU race lets the alias's target survive analyzer resolution but disappear before plan optimization. The new test (04236_…) locks down the user-visible contract that both paths throw UNKNOWN_TABLE.
b Root cause explained? Yes. StorageAlias::getInMemoryMetadataPtr falls back to IStorage's default (empty metadata) when tryGetTargetTable returns null — this is needed for system.tables/system.columns queries on a dangling alias (exercised in 03636_storage_alias_basic). ReadFromMerge::createChildrenPlans then sees the empty getColumns() and throws LOGICAL_ERROR "Table has no columns." in its parameterized-view-or-bug branch.
c Fix matches root cause? Yes. The fix adds the third legitimate case ("dangling Alias") to the empty-columns branch, with a clear UNKNOWN_TABLE exception that names both the alias and the Merge table that matched it. The existing LOGICAL_ERROR is preserved as a genuine assertion for any other empty-metadata producer.
d Test intent preserved? / new test added? Yes. New test 04236_merge_with_alias_missing_target_104740 added — asserts serverError UNKNOWN_TABLE for both a bare SELECT and the JOIN pattern from 03549_analyzer_fix_filter_removal. Existing tests preserved: 03636_storage_alias_basic (metadata-only ops on dangling alias), 03549_analyzer_fix_filter_removal, 01925_test_storage_merge_aliases, 01214_test_storage_merge_aliases_with_where all still pass.
e Both directions demonstrated? Yes for the contract. The new test passes 50/50 with full randomization (tests/clickhouse-test --test-runs 50 04236_…). The race-condition path that produced the original LOGICAL_ERROR cannot be deterministically triggered single-threaded; CI's stress tests will validate that the LOGICAL_ERROR disappears.
f Fix is general / no symptom-only patch? Yes. Grep for "Table has no columns" in src/ returns one site (the one fixed). Other getInMemoryMetadataPtr call-sites in StorageMerge (traverseTablesUntilImpl line 232, the order_optimizer loop at line 646) tolerate empty metadata gracefully — they don't crash with a dangling alias. The fix targets the only assertion site that fired in CI.

Directive: @alexey-milovidov's comment on #100752 (2026-05-13T03:12:00Z).

Session: cron:clickhouse-ci-task-worker:20260513-051500.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov @azat — could you review this? Fixes the chronic STID 3593-560a / 3593-5990 LOGICAL_ERROR "Table has no columns." by turning the assertion into a user-facing UNKNOWN_TABLE for the dangling-Alias case in ReadFromMerge::createChildrenPlans. Closes #104740, per the directive on #100752.

@Algunenano Algunenano added the can be tested Allows running workflows for external contributors label May 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5b5c895]

Summary: ✅


AI Review

Summary

This PR changes ReadFromMerge::createChildrenPlans to map the dangling-Alias empty-metadata case to UNKNOWN_TABLE instead of an internal LOGICAL_ERROR, and adds a focused stateless regression test that covers both plain read and join planning paths. I did not find a remaining contract violation in the touched paths of the current diff.

Missing context / blind spots
  • ⚠️ I could not validate full CI signal from the PR URL due token restrictions in this environment; direct S3 report lookup for this SHA returned an empty test list, so this review is based on code + test diff inspection.
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label May 13, 2026
@Avogar Avogar self-assigned this May 13, 2026
Comment thread src/Storages/StorageMerge.cpp Resolved
@groeneai

Copy link
Copy Markdown
Collaborator Author

Standing down — @azat's PR #104919 supersedes this one.

Quick summary for future readers:

Aspect This PR #104790 @azat's PR #104919
Approach Catch empty-metadata in ReadFromMerge::createChildrenPlans and convert to UNKNOWN_TABLE Make StorageAlias::getInMemoryMetadataPtr throw on missing target; introduce tryGetInMemoryMetadataPtrForSystemTable() for the introspection paths
Files changed 2 10
Regression test reused 04236_merge_with_alias_missing_target_104740 Same

@azat's approach fixes the contract at the source (alias storage refuses to hand out empty metadata) and isolates the "tolerate dangling target" behavior to the four system-table call sites that actually need it. That's cleaner than guarding at one consumer (our approach).

Closing this PR in favor of #104919. Thanks for the alternative @azat!

Session: cron:clickhouse-ci-task-worker:20260514-091500

@groeneai

Copy link
Copy Markdown
Collaborator Author

Superseded by #104919 (see above).

Comment thread src/Storages/StorageMerge.cpp Resolved
@azat
azat enabled auto-merge May 18, 2026 20:21
@clickhouse-gh

clickhouse-gh Bot commented May 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.20% 84.10% -0.10%
Functions 91.40% 91.40% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 33.33% (3/9) · Uncovered code

Full report · Diff report

@groeneai

Copy link
Copy Markdown
Collaborator Author

Thanks for the review and approval, @azat — and thanks for applying the comment-wording suggestion directly (commit 5b5c895). The new comment is much cleaner.

For context to anyone following this thread: I had earlier stood down in favor of #104919, but it looks like this PR's approach was kept after all. CI is currently rebuilding on the updated commit; will monitor for green and ping again if anything else surfaces.

@azat
azat added this pull request to the merge queue May 19, 2026
Merged via the queue into ClickHouse:master with commit 3a00bfd May 19, 2026
166 checks passed
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — could this be a backport candidate for 26.4, 26.3 (LTS), 26.2? No backport label was found, and the affected code appears to exist in those branches based on a file/function presence check.

Affected code: ReadFromMerge::createChildrenPlans empty metadata check for Alias storage — introduced in 25.11.

Why this might apply: Fix for LOGICAL_ERROR that can occur in stress tests and TOCTOU race conditions. StorageAlias was introduced in 25.11, so the fix should be backported to all branches where the feature exists (26.2, 26.3, 26.4). Not needed for 25.8 since StorageAlias doesn't exist there.

Other supported branches (25.8) predate 25.11 and do not contain this code.

Caveat: I verified that the modified code exists in those branches, but not whether the bug is actually reachable there — a new caller in master may be what makes this newly observable. If older branches can't reach the affected path, please ignore this; otherwise consider pr-must-backport or a version label like v26.4-must-backport.

pull Bot pushed a commit to AnotherGenZ/ClickHouse that referenced this pull request Aug 24, 2026
…no columns

ReadFromMerge::createChildrenPlans classifies a matched table whose metadata
snapshot has no columns. It recognised a parameterized view and an Alias with a
missing target, and treated everything else as an internal invariant violation.

An Alias whose target is a live parameterized view falls through both checks. A
parameterized view registers no columns by design (StorageView.cpp), and
StorageAlias forwards its target's metadata, so the alias presents that empty
column list. But StorageAlias does not override isView(), so it inherits the
IStorage default of false and the parameterized-view check short-circuits; the
target exists, so the missing-target check is false too. The remaining branch
raised LOGICAL_ERROR, which aborts the server in debug and sanitizer builds.
Two CI runs reached it on 2026-08-21 through merge('^'), one on master and one
on a pull request, with identical stacks.

Two changes at that site. The parameterized-view diagnosis now looks one hop
through an Alias, so such a table reports STORAGE_REQUIRES_PARAMETER, the same
error the view itself reports. The remaining case reports UNSUPPORTED_METHOD
naming the table and the Merge table, which is what fixes the class rather than
this one carrier: any matched table that cannot supply columns now produces a
proper error instead of an assertion, longer Alias chains included. This mirrors
the change made at the planner's getSmallestColumn site in ClickHouse#115716.

The empty column list is not itself a bad state, so there is nothing to correct
upstream of this point; the defect was that plan construction treated a
user-reachable condition as an internal one. Both existing messages are left
byte-identical. Making StorageAlias forward isView() was rejected as far too
broad for this, since that predicate also gates DDL validation, system.tables,
replication and materialized-view logic, and the header records a deliberate
split between predicates that resolve the target and predicates that fail
closed.

No setting, serialization format or new feature is involved, and the block
always threw, so no query that previously succeeded changes behaviour.

The regression test covers the observed carrier through both a Merge table and
the merge() table function, with FINAL and SAMPLE, a two-hop Alias chain, the
two already-classified carriers, and two positive arms that must still return
rows. It aborts an unpatched server, so it cannot pass vacuously.

Related: ClickHouse#104740
Related: ClickHouse#104790
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 pr-ci 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.

Logical error: 'Table has no columns.' (STID: 3593-5990)

6 participants