Skip to content

Implement ALTER TABLE ... MODIFY CONSTRAINT - #108768

Merged
alexey-milovidov merged 7 commits into
masterfrom
alter-modify-constraint
Jul 1, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
alter-modify-constraint

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jun 28, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • New Feature

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

Support ALTER TABLE ... MODIFY CONSTRAINT [IF EXISTS] name CHECK expr to change the expression of an existing constraint in place.

Description

Previously only ADD CONSTRAINT and DROP CONSTRAINT were supported. Changing a constraint's expression required dropping and re-adding it, e.g.

ALTER TABLE t DROP CONSTRAINT length, ADD CONSTRAINT length CHECK length(content) < 50 * 1024 * 1024;

This works but is awkward, and it moves the constraint to the end of the table definition.

This PR adds:

ALTER TABLE [db].name [ON CLUSTER cluster] MODIFY CONSTRAINT [IF EXISTS] name CHECK expr;

which replaces the declaration of an existing constraint in place, preserving its position in the table definition. It also works for ASSUME constraints.

Semantics, consistent with the existing constraint commands:

  • It is a metadata-only operation (no mutation, no merge) and is applied immediately.
  • It does not validate existing data — the new constraint is only checked on subsequent INSERTs (and on parts touched by later mutations), exactly like ADD CONSTRAINT.
  • On replicated tables the change is broadcast via (Zoo)Keeper like any other metadata change.
  • If the constraint does not exist, the query throws BAD_ARGUMENTS, unless IF EXISTS is given.

A dedicated ALTER MODIFY CONSTRAINT privilege is added under the ALTER CONSTRAINT group (alongside ALTER ADD CONSTRAINT / ALTER DROP CONSTRAINT).

Documentation entry for user-facing changes

docs/en/sql-reference/statements/alter/constraint.md and grant.md are updated.

Tests

tests/queries/0_stateless/03999_alter_modify_constraint.sql covers: modifying a CHECK constraint (enforcement changes, position preserved), switching CHECK → ASSUME, MODIFY of a missing constraint (error), and MODIFY ... IF EXISTS of a missing constraint (no-op).

Version info

  • Merged into: 26.7.1.330 (included in 26.7 and later)

Previously only ADD CONSTRAINT and DROP CONSTRAINT were supported, so
changing a constraint's expression required dropping and re-adding it
(which also moved the constraint to the end of the table definition).

This adds `ALTER TABLE ... MODIFY CONSTRAINT [IF EXISTS] name CHECK expr`,
which replaces the declaration of an existing constraint in place,
preserving its position. Like ADD/DROP CONSTRAINT it is a metadata-only
operation and does not validate existing data. A dedicated
ALTER MODIFY CONSTRAINT privilege (under the ALTER CONSTRAINT group) is
added as well.

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

clickhouse-gh Bot commented Jun 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [238ef82]

Summary: ✅


AI Review

Summary

This PR adds ALTER TABLE ... MODIFY CONSTRAINT with a dedicated privilege, documentation, and focused stateless tests. The current code wires the new alter through parsing, formatting, access checks, metadata application, replicated metadata propagation, and constraint graph rebuilding consistently with existing ADD/DROP CONSTRAINT behavior. The prior bot-authored inline findings have been addressed in the current code.

Final Verdict

✅ No blocking or major code-review findings.

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Jun 28, 2026
Comment thread src/Access/Common/AccessType.h Resolved
alexey-milovidov and others added 6 commits June 29, 2026 00:12
`ComparisonGraph::normalizeAtom` (the AST overload) flips `less`/`lessOrEquals`
into `greater`/`greaterOrEquals` by swapping the two arguments, but it accessed
`children[0]` and `children[1]` without checking the argument count. The AST
reaching `ConstraintsDescription::buildGraph` is not analyzed, so a constraint
expression such as `CHECK less(a)` (a unary `less`) caused an out-of-bounds read:
`Assertion 'this->m_holder.m_size > n' failed` in debug builds and a segfault in
release builds. The query-tree overload is protected by function resolution and
its own `chassert(arguments.size() == 2)`; the AST path had no such guard.

Only binary comparisons can be normalized, so anything else is left unchanged and
ignored later, where `arguments.size() == 2` is already checked.

Found by the AST fuzzer on the new `ALTER ... MODIFY CONSTRAINT` test:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108768&sha=686df51dd6d5f41a3043194c6418c5059a53c793&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29
#108768

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`ParserConstraintDeclaration` accepts both `CHECK` and `ASSUME`, and
`ALTER ... MODIFY CONSTRAINT` can switch a constraint from one kind to the other,
but the docs only showed the `CHECK` form. Show `{CHECK|ASSUME}` for both
`ADD CONSTRAINT` and `MODIFY CONSTRAINT`, and note that `MODIFY CONSTRAINT` can
also change the constraint kind.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…TRAINT

Adding the `ALTER MODIFY CONSTRAINT` access type shifts the `Enum16` values of
`system.privileges.privilege`, `system.privileges.parent_group` and
`system.grants.access_type`. Regenerate the autogenerated enum listings in the
system-table docs so they match the server (this also picks up `YTSAURUS` and
`ARROW FLIGHT`, which had drifted out of the docs earlier).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Verify that a user holding only `ALTER ADD CONSTRAINT` / `ALTER DROP CONSTRAINT`
cannot run `ALTER ... MODIFY CONSTRAINT` (it returns `ACCESS_DENIED`), while a
user with the dedicated `ALTER MODIFY CONSTRAINT` privilege or the parent
`ALTER CONSTRAINT` group can.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The tests `05020_constraint_comparison_wrong_arity` and
`05021_grant_alter_modify_constraint` left a numbering gap larger than
100 after merging `master` (highest existing number was `04488`), which
failed the `test_numbers_check` style check (`Gap (4415, 5020) > 100`).
Renumber them to `04489` and `04490`.

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

clickhouse-gh Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.40% 85.40% +0.00%
Functions 92.60% 92.60% +0.00%
Branches 77.60% 77.60% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 77/78 (98.72%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 7 line(s) · Uncovered code

Full report · Diff report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The only remaining red check is CH Inc sync (private sync PR #62547). The public CI is fully green (151 successful jobs), all review threads are resolved, and the AI review reports no blocking findings.

The sync PR failed on two unrelated, pre-existing flaky tests. Neither touches anything this PR changes (ParserAlterQuery, AccessType, AlterCommands, ComparisonGraph):

  1. Integration tests (amd_asan_ubsan, db disk, old analyzer, 5/7) → test_refreshable_mv_no_multi_read::test_refreshable_mv_attach_without_multi_read: AssertionError: assert 'Disabled' in 'Scheduled'. Pre-existing refreshable-materialized-view status race (last stabilization attempt cdabef4635c, 2026-06-25); this PR does not touch refreshable materialized views.
  2. Stress test (arm_asan_ubsan, SharedCatalog, meta in keeper) → Hung check: the hung query is SELECT formatQuery($$SELECT … (deep-nested literal, elapsed ~1280s, already cancelled), auto-marked flaky by CI matching Hung check failed, possible deadlock found #107941.

I re-ran the two failed jobs on the sync PR. No code change is needed.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The only remaining red is CH Inc sync (private sync PR #62547). Public CI is fully green (150 successful jobs, 0 failures), all review threads are resolved, the AI review reports no blocking findings, and the LLVM coverage job covers 77/78 changed lines.

The sync failed again on the same two unrelated, pre-existing flaky tests. Neither touches anything this PR changes (ParserAlterQuery, AccessType, AlterCommands, ComparisonGraph):

  1. Integration tests (amd_asan_ubsan, db disk, old analyzer) → test_refreshable_mv_no_multi_read::test_refreshable_mv_attach_without_multi_read: AssertionError: assert 'Disabled' in 'Scheduled'. This is a known refreshable-materialized-view status race. It is actively being fixed in Stop refreshable MV gracefully when the Keeper feature-flag check races on attach #108890 (Stop refreshable MV gracefully when the Keeper feature-flag check races on attach), which touches RefreshTask.cpp and this very test; the test currently flakes there too (public CI, 2026-06-30). This PR does not touch refreshable materialized views.
  2. Stress test (arm_asan_ubsan, SharedCatalog, meta in keeper) → Hung check: the chronic deep-nested literal formatQuery hang, auto-marked flaky by CI matching Hung check failed, possible deadlock found #107941.

I re-ran the two failed jobs on the sync PR. No code change is needed.

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

Very straightforward and clean.

@alexey-milovidov alexey-milovidov self-assigned this Jul 1, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 1, 2026
Merged via the queue into master with commit b670b2d Jul 1, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the alter-modify-constraint branch July 1, 2026 06:31
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 1, 2026
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)

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.

@alexey-milovidov is this going to be fine if there are existing mutations using the previous constraint definition?

zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
`ComparisonGraph::normalizeAtom` (the AST overload) flips `less`/`lessOrEquals`
into `greater`/`greaterOrEquals` by swapping the two arguments, but it accessed
`children[0]` and `children[1]` without checking the argument count. The AST
reaching `ConstraintsDescription::buildGraph` is not analyzed, so a constraint
expression such as `CHECK less(a)` (a unary `less`) caused an out-of-bounds read:
`Assertion 'this->m_holder.m_size > n' failed` in debug builds and a segfault in
release builds. The query-tree overload is protected by function resolution and
its own `chassert(arguments.size() == 2)`; the AST path had no such guard.

Only binary comparisons can be normalized, so anything else is left unchanged and
ignored later, where `arguments.size() == 2` is already checked.

Found by the AST fuzzer on the new `ALTER ... MODIFY CONSTRAINT` test:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108768&sha=686df51dd6d5f41a3043194c6418c5059a53c793&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29
ClickHouse#108768

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…straint

Implement ALTER TABLE ... MODIFY CONSTRAINT
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
`ComparisonGraph::normalizeAtom` (the AST overload) flips `less`/`lessOrEquals`
into `greater`/`greaterOrEquals` by swapping the two arguments, but it accessed
`children[0]` and `children[1]` without checking the argument count. The AST
reaching `ConstraintsDescription::buildGraph` is not analyzed, so a constraint
expression such as `CHECK less(a)` (a unary `less`) caused an out-of-bounds read:
`Assertion 'this->m_holder.m_size > n' failed` in debug builds and a segfault in
release builds. The query-tree overload is protected by function resolution and
its own `chassert(arguments.size() == 2)`; the AST path had no such guard.

Only binary comparisons can be normalized, so anything else is left unchanged and
ignored later, where `arguments.size() == 2` is already checked.

Found by the AST fuzzer on the new `ALTER ... MODIFY CONSTRAINT` test:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108768&sha=686df51dd6d5f41a3043194c6418c5059a53c793&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29
ClickHouse#108768

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit 01e999e)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
The cherry-picked test exercised `ALTER TABLE ... MODIFY CONSTRAINT`, which was introduced together with the fix in ClickHouse#108768 and does not exist on this branch. Replace that case with a `CREATE TABLE` reproducer (`CHECK less(a)`), and cover `ASSUME less(a)` and a zero-argument `less()` as well.

Also rename it to `04489_constraint_comparison_wrong_arity`, the name it has on master.

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-feature Pull request with new product feature 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.

3 participants