Skip to content

Reject ATTACH TABLE with storage clauses but no ENGINE - #104800

Merged
Algunenano merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-104791-attach-table-settings-silent-ignore
May 15, 2026
Merged

Algunenano merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-104791-attach-table-settings-silent-ignore

Conversation

@groeneai

@groeneai groeneai commented May 13, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #104791. Reported by @clickgapai during automated review of merged PR #73120; @zlareb1 flagged it for fix.

Motivation

ATTACH TABLE name SETTINGS k = v; (and the same with ORDER BY, PARTITION BY, etc.) without an explicit ENGINE would be parsed and then silently routed through the short-ATTACH path that reads the table definition from stored metadata, overwriting the user's input. Users saw no error and assumed their settings took effect.

Reproducer (from the issue):

SET default_table_engine = 'MergeTree';
CREATE TABLE t (id UInt32) ORDER BY id;
DETACH TABLE t;
ATTACH TABLE t SETTINGS max_part_loading_threads = 16;

SELECT engine_full FROM system.tables WHERE name = 't';
-- Expected: SETTINGS contains max_part_loading_threads = 16
-- Actual:   only the original index_granularity = 8192

Root cause

ParserCreateQuery.cpp:697 allows SETTINGS without ENGINE for tables in order to support default_table_engine on CREATE. For ATTACH the same syntax produced an ASTStorage with settings but no engine, which made the short-ATTACH detection at InterpreterCreateQuery.cpp:1571 (!create.storage->engine) match and discard the user's clauses while reading the full stored definition. The other non-engine storage clauses (PARTITION BY, PRIMARY KEY, ORDER BY, SAMPLE BY, TTL, UNIQUE KEY) have the same shape and the same bug.

Fix

Reject the ambiguous case before entering the short-ATTACH branch. When the user provided storage clauses without an ENGINE and no columns list, throw BAD_ARGUMENTS and point them at the short form (ATTACH TABLE t;) or ALTER TABLE for changing settings after attach.

The check fires before ON CLUSTER forwarding and before Replicated-database query enqueue, so distributed ATTACH ... SETTINGS is rejected at the initiator instead of silently dropping on each replica.

Tests

Added tests/queries/0_stateless/04237_attach_table_settings_without_engine_error.sql covering:

  • ATTACH TABLE t SETTINGS k = v; — BAD_ARGUMENTS
  • ATTACH TABLE t ORDER BY id; — BAD_ARGUMENTS
  • ATTACH TABLE t PARTITION BY id; — BAD_ARGUMENTS
  • ATTACH TABLE t; — still works (positive control)

Verified locally with the debug build that the bug reproduces without the fix and is rejected with BAD_ARGUMENTS with the fix.

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):

ATTACH TABLE name <storage clauses>; without an ENGINE and without a columns list (e.g. ATTACH TABLE t SETTINGS max_part_loading_threads = 16; or ATTACH TABLE t ORDER BY id;) used to silently ignore the user-provided storage clauses and re-attach the table with its stored definition. Now this is rejected with BAD_ARGUMENTS so users can notice and use the short form ATTACH TABLE t; or ALTER TABLE ... MODIFY SETTING instead.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.668

`ATTACH TABLE name SETTINGS k = v;` (and the same with `ORDER BY`,
`PARTITION BY`, etc.) without an explicit `ENGINE` would be parsed and
then silently routed through the short-`ATTACH` path that reads the table
definition from stored metadata, overwriting the user's input. Users saw
no error and assumed their settings took effect.

`ParserCreateQuery.cpp:697` allows `SETTINGS` without `ENGINE` to support
`default_table_engine` on `CREATE`. For `ATTACH` the same syntax produced
an `ASTStorage` with settings but no engine, which made the short-`ATTACH`
detection at `InterpreterCreateQuery.cpp:1571` (`!create.storage->engine`)
match and drop the user's clauses. The other non-engine storage clauses
(`PARTITION BY`, `PRIMARY KEY`, `ORDER BY`, `SAMPLE BY`, `TTL`, `UNIQUE KEY`)
have the same shape and the same bug.

Now we reject `ATTACH TABLE` with `create.storage && !create.storage->engine
&& !create.columns_list` with `BAD_ARGUMENTS`, pointing users at the
short-`ATTACH` form (`ATTACH TABLE t;`) or `ALTER TABLE` for changing
settings after attach. The check fires before `ON CLUSTER` forwarding and
before Replicated-database query enqueue, so distributed `ATTACH ... SETTINGS`
is rejected at the initiator instead of silently dropping on each replica.

Closes ClickHouse#104791.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (see TASK.md):

  • a) Deterministic repro? Yes. With session-set default_table_engine = 'MergeTree', ATTACH TABLE t SETTINGS max_part_loading_threads = 16; deterministically reproduces the bug (settings dropped, no error) on master. Confirmed by git stash + rebuild on master HEAD.
  • b) Root cause explained? Yes. Parser at ParserCreateQuery.cpp:697 permits SETTINGS without ENGINE to support default_table_engine on CREATE. The interpreter's short-ATTACH check at InterpreterCreateQuery.cpp:1571 (!create.storage->engine) then matches the resulting AST, enters the short-ATTACH branch, reads the full table definition from stored metadata via getCreateTableQuery, and discards create.storage and create.columns_list. The same shape applies to ORDER BY, PARTITION BY, PRIMARY KEY, SAMPLE BY, TTL, UNIQUE KEY (all live on the same ASTStorage with engine == nullptr).
  • c) Fix matches root cause? Yes. The check is at the root cause — the short-ATTACH branch silently dropping user input. We reject the ambiguous case (create.storage && !create.storage->engine && !create.columns_list) before the short-ATTACH branch, before ON CLUSTER forwarding, and before Replicated-database query enqueue. No symptom widening.
  • d) Test intent preserved / new tests added? Test-only addition: tests/queries/0_stateless/04237_attach_table_settings_without_engine_error.sql covers SETTINGS, ORDER BY, PARTITION BY without ENGINE (all BAD_ARGUMENTS) plus a short-ATTACH positive control. No existing tests modified.
  • e) Both directions demonstrated? Yes.
    • Without the fix (master + git stash of the .cpp change): the original reproducer succeeds and engine_full shows only index_granularity = 8192, no max_part_loading_threads = 16.
    • With the fix: ATTACH TABLE t_104791 SETTINGS max_part_loading_threads = 16; → Code: 36. BAD_ARGUMENTS: ATTACH TABLE without ENGINE cannot specify storage clauses .... The short-ATTACH positive control still passes.
    • 04237_attach_table_settings_without_engine_error passes 20/20 with --test-runs 20 and random settings. Adjacent ATTACH-related tests (01601_detach_permanently, 01516_create_table_primary_key, 01249_bad_arguments_for_bloom_filter, 01528_clickhouse_local_prepare_parts, 03167_attach_as_replicated_errors) all pass on top of the fix.
  • f) Fix is general? Yes. The check is on create.storage->engine and create.columns_list, so it covers every non-engine storage clause (SETTINGS, ORDER BY, PARTITION BY, PRIMARY KEY, SAMPLE BY, TTL, UNIQUE KEY). It also fires for distributed/ON CLUSTER ATTACH ... SETTINGS at the initiator instead of letting each replica silently drop the user input.

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

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Algunenano @tavplubix — could you review this? It tightens InterpreterCreateQuery::createTable to reject ATTACH TABLE name <storage clauses>; (no ENGINE, no columns) instead of silently dropping the user-provided clauses via the short-ATTACH path. The parser hook that allowed SETTINGS without ENGINE for default_table_engine (PR #73120) is unchanged — only the ATTACH side is rejected with BAD_ARGUMENTS. Closes #104791.

@Algunenano Algunenano self-assigned this May 13, 2026
@nikitamikhaylov nikitamikhaylov 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 [b49ae90]

Summary: ✅


AI Review

Summary

This PR rejects ATTACH TABLE ... <storage clauses> without ENGINE and without a columns list before the short-ATTACH metadata path can silently drop user input, and it adds a focused stateless regression test. I found no unresolved correctness, safety, performance, or evidence gaps in the current diff.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 13, 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.60% 76.50% -0.10%

Changed lines: 100.00% (9/9) · Uncovered code

Full report · Diff report

@Algunenano Algunenano 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.

I think this is ok. I don't expect this syntax exists in the wild, so although it's technically a breaking change it's fine

@Algunenano
Algunenano added this pull request to the merge queue May 15, 2026
Merged via the queue into ClickHouse:master with commit e40f4fc May 15, 2026
166 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 15, 2026
pull Bot pushed a commit to Mu-L/ClickHouse that referenced this pull request May 19, 2026
Apply the edit-changelog skill workflow to the autogenerated 26.5 section:
add the release header, drop the NO CL ENTRY and NOT FOR CHANGELOG /
INSIGNIFICANT sections, rescue user-visible entries into Bug Fix,
Performance Improvement and Improvement, reconcile reverted PRs against
the rest of the changelog (drop entries for ClickHouse#102801, ClickHouse#103547, ClickHouse#103858,
ClickHouse#104800), record the isPrime / isProbablePrime revert-of-revert and
follow-up PRs on the original entry, prune Build/Testing to user-affecting
items, and apply a focused round of per-entry rewrites (proper-noun
capitalization, backticked identifiers, common typos).

Date, presentation URL and video URL in the release header are left as
FIXME placeholders.
pull Bot pushed a commit to Spencerx/ClickHouse that referenced this pull request May 22, 2026
Re-land of ClickHouse#104800 with a narrower guard that does not break
`ATTACH TABLE t SETTINGS log_comment = 'foo';`. PR ClickHouse#104800 was reverted in
ClickHouse#105032 because it rejected that pattern, which internal CI relies on
(reported by @Algunenano on ClickHouse#104791).

`InterpreterSetQuery::applySettingsFromQuery` runs in `executeQueryImpl`
before `InterpreterCreateQuery::createTable` and hoists session settings
out of `create.storage->settings` into the context, resetting
`storage->settings` to `nullptr` when nothing engine-specific is left.
The reverted guard fired only on the existence of an `ASTStorage` without
an `engine`, so by the time it ran the storage object had already been
emptied — rejecting the legitimate `log_comment` shape.

The narrower guard checks the individual non-engine fields
(`partition_by`, `primary_key`, `order_by`, `sample_by`, `ttl_table`,
`unique_key`, `settings`) so it only rejects when there is something the
short-`ATTACH` path would actually drop:

  ATTACH TABLE t;                                  OK (short ATTACH)
  ATTACH TABLE t SETTINGS log_comment = 'foo';     OK (session bind)
  ATTACH TABLE t SETTINGS index_granularity = 100; BAD_ARGUMENTS
  ATTACH TABLE t SETTINGS max_part_loading_threads = 16; BAD_ARGUMENTS
  ATTACH TABLE t ORDER BY id;                      BAD_ARGUMENTS
  ATTACH TABLE t PARTITION BY id;                  BAD_ARGUMENTS

Regression test `04247_attach_table_storage_clauses_without_engine_error`
covers all six cases.

Closes ClickHouse#104791.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
pull Bot pushed a commit to Spencerx/ClickHouse that referenced this pull request May 22, 2026
…1-narrower-attach-table-storage-guard

Reject ATTACH TABLE with non-engine storage clauses (narrower guard, re-land of ClickHouse#104800)
DavidHe-2008 pushed a commit to DavidHe-2008/ClickHouse that referenced this pull request Jun 1, 2026
…1-narrower-attach-table-storage-guard

Reject ATTACH TABLE with non-engine storage clauses (narrower guard, re-land of ClickHouse#104800)
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-bugfix Pull request with bugfix, not backported by default 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.

ATTACH TABLE ... SETTINGS silently ignores settings when ENGINE not specified

4 participants