Repository navigation
Implement ALTER TABLE ... MODIFY CONSTRAINT - #108768
Conversation
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]>
|
Workflow [PR], commit [238ef82] Summary: ✅
AI ReviewSummaryThis PR adds Final Verdict✅ No blocking or major code-review findings. |
`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]>
LLVM Coverage Report
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 |
|
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 (
I re-ran the two failed jobs on the sync PR. No code change is needed. |
|
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 (
I re-ran the two failed jobs on the sync PR. No code change is needed. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Very straightforward and clean.
| @@ -0,0 +1,46 @@ | |||
| #!/usr/bin/env bash | |||
|
|
|||
| CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) | |||
There was a problem hiding this comment.
@alexey-milovidov is this going to be fine if there are existing mutations using the previous constraint definition?
`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]>
…straint Implement ALTER TABLE ... MODIFY CONSTRAINT
`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]>
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]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Support
ALTER TABLE ... MODIFY CONSTRAINT [IF EXISTS] name CHECK exprto change the expression of an existing constraint in place.Description
Previously only
ADD CONSTRAINTandDROP CONSTRAINTwere supported. Changing a constraint's expression required dropping and re-adding it, e.g.This works but is awkward, and it moves the constraint to the end of the table definition.
This PR adds:
which replaces the declaration of an existing constraint in place, preserving its position in the table definition. It also works for
ASSUMEconstraints.Semantics, consistent with the existing constraint commands:
INSERTs (and on parts touched by later mutations), exactly likeADD CONSTRAINT.BAD_ARGUMENTS, unlessIF EXISTSis given.A dedicated
ALTER MODIFY CONSTRAINTprivilege is added under theALTER CONSTRAINTgroup (alongsideALTER ADD CONSTRAINT/ALTER DROP CONSTRAINT).Documentation entry for user-facing changes
docs/en/sql-reference/statements/alter/constraint.mdandgrant.mdare updated.Tests
tests/queries/0_stateless/03999_alter_modify_constraint.sqlcovers: modifying a CHECK constraint (enforcement changes, position preserved), switching CHECK → ASSUME,MODIFYof a missing constraint (error), andMODIFY ... IF EXISTSof a missing constraint (no-op).Version info
26.7.1.330(included in26.7and later)