Skip to content

Execute Alter Commands in Order of Declaration - #100288

Merged
Michicosun merged 36 commits into
masterfrom
force_alter_commands_order
Mar 29, 2026
Merged

Michicosun merged 36 commits into
masterfrom
force_alter_commands_order

Conversation

@Michicosun

@Michicosun Michicosun commented Mar 21, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

This patch fixes the execution of the alter command by forbidding command reordering.

Better fix for #96022

Version info

  • Merged into: 26.4.1.361

@clickhouse-gh

clickhouse-gh Bot commented Mar 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3bc6891]

Summary: ✅


AI Review

Summary

This PR refactors ALTER TABLE execution to preserve declaration order by parsing command segments and running them sequentially. The main implementation direction is reasonable, but there are test/comment quality issues that should be corrected to avoid misleading future maintenance and over-claiming coverage.

Missing context
  • ⚠️ No CI report/logs were provided in this task context, so runtime validation was limited to static diff/code inspection.
Findings

💡 Nits

  • [tests/queries/0_stateless/03400_alter_update_with_rename.sql:7] The file header says the fix rejects UPDATE + RENAME on the same column, but the test now executes that pattern successfully. Update the comment to match real behavior.
  • [src/Interpreters/MutationsInterpreter.cpp:985] Typo in comment: materalize → materialize.
  • [tests/queries/0_stateless/03978_alter_table_execute_expire_snapshots_parse.sql:50] The comment claims coverage for combining EXECUTE with alters and mutations, but only alter+execute is exercised. Add a mutation+execute case or narrow the comment.
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 ✅
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Fix the stale test/behavior comments and typo noted above.
    • Align test comments with actual covered scenarios (especially EXECUTE + mutation coverage claim).

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Mar 21, 2026
Comment thread src/Storages/PartitionCommands.cpp Resolved
Comment thread src/Storages/MutationCommands.cpp Resolved
Comment thread src/Storages/MutationCommands.cpp Resolved
Comment thread src/Storages/MutationCommands.h Resolved
Comment thread src/Storages/PartitionCommands.h Resolved
Comment thread src/Storages/AlterCommands.cpp Resolved
Comment thread src/Storages/MutationCommands.cpp Resolved
Comment thread src/Storages/MutationCommands.cpp Resolved
return;

if (segments.size() != 1)
throw Exception(ErrorCodes::QUERY_IS_PROHIBITED,

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.

This refactor changes the error code for replicated-database mixed ALTER segments from NOT_IMPLEMENTED (old behavior) to QUERY_IS_PROHIBITED.

That is externally visible and can break clients/tests that key off stable error codes. Unless this semantic change is intentional and documented, please keep NOT_IMPLEMENTED here to preserve backward compatibility.

@@ -6,31 +6,16 @@
-- succeed together or fail together.
-- The fix rejects UPDATE + RENAME on the same column early to ensure atomicity.

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.

The test comment at the top still says this fix "rejects UPDATE + RENAME on the same column early" and enforces atomicity, but the updated query/reference below now allow that pattern and validate partial execution order (1\t1, renamed column c1).

Please update the test description to match the current expected behavior, otherwise this test is misleading for future maintainers.

@clickhouse-gh

clickhouse-gh Bot commented Mar 28, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 24.60% 24.60% +0.00%
Branches 76.70% 76.70% +0.00%

Changed lines: 75.11% (169/225) · Uncovered code

Full report · Diff report

@Michicosun
Michicosun added this pull request to the merge queue Mar 29, 2026
Merged via the queue into master with commit c12959f Mar 29, 2026
149 checks passed
@Michicosun
Michicosun deleted the force_alter_commands_order branch March 29, 2026 10:59
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 29, 2026
Krishnachaitanyakc added a commit to Krishnachaitanyakc/ClickHouse that referenced this pull request Apr 10, 2026
After the refactor in PR ClickHouse#100288, the `allow_statistics` enforcement
was moved to `MutationsInterpreter::prepare()`. However, `ADD STATISTICS`
and `DROP STATISTICS` are routed as `AlterCommands`, not `MutationCommands`,
so they never reached the new check.

Re-add the check in `AlterCommands::validate()` so that all three
ALTER STATISTICS commands respect `allow_statistics=0`.

Closes ClickHouse#101082
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @Michicosun @azat — this PR appears to contain a backward incompatible change but is categorized as Not for changelog (changelog entry is not required).

Why: PR changes the execution order of mixed ALTER commands from 'always alter before mutation regardless of declaration order' to 'declaration order'. Existing user queries like ALTER TABLE t MATERIALIZE TTL, MODIFY TTL ts + INTERVAL 30 DAY worked before this PR (the deleted comment in InterpreterAlterQuery.cpp explicitly documented this support: 'Check it after alter finished, so we can add TTL and materialize TTL in the same ALTER query') but now throw INCORRECT_QUERY. This is observable backward-incompatible behavior change.

Suggested category: Backward Incompatible Change

Could you check if the changelog category should be updated? This helps users know what to expect when upgrading. Ignore this if the change is intentionally non-breaking.

@clickgapai

Copy link
Copy Markdown
Contributor

Hi @Michicosun @azat — while reviewing this PR I found the following:

Could you review the test PR and add the can be tested label if it looks good?
Happy to discuss — close anything that's wrong or already addressed.

@azat

azat commented May 13, 2026

Copy link
Copy Markdown
Member

@clickgapai

This should fail ALTER TABLE t MATERIALIZE TTL, MODIFY TTL ts + INTERVAL 30 DAY

This should not ALTER TABLE t MODIFY TTL ts + INTERVAL 30 DAY, MATERIALIZE TTL, and this is exactly how it works right now

(For table create table t (ts DateTime))

@clickgapai

Copy link
Copy Markdown
Contributor

@azat You're absolutely right — I just verified this on the running server:

CREATE TABLE t (ts DateTime('UTC'), data String) ENGINE = MergeTree ORDER BY ts;

-- This fails (error 80: Cannot MATERIALIZE TTL as there is no TTL set)
ALTER TABLE t MATERIALIZE TTL, MODIFY TTL ts + INTERVAL 30 DAY;

-- This works correctly
ALTER TABLE t MODIFY TTL ts + INTERVAL 30 DAY, MATERIALIZE TTL;

The "declaration order" semantics from this PR are actually the correct behavior — MATERIALIZE TTL, MODIFY TTL should fail because you can't materialize a TTL that doesn't exist yet. Users must write commands in the logical execution order.

My original backward-compatibility concern was based on a misunderstanding — I was thinking the old behavior silently reordered commands to "make them work," but that would have masked logical errors. The PR's behavior (execute in declaration order) gives users correct, predictable semantics.

Withdrawing that concern. Thanks for the correction.

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

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog 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.

4 participants