Skip to content

Make DELETE FROM require only the ALTER DELETE privilege - #107491

Merged
tiandiwonder merged 9 commits into
ClickHouse:masterfrom
tiandiwonder:fix-delete-from-row-exists-privilege
Jul 10, 2026
Merged

tiandiwonder merged 9 commits into
ClickHouse:masterfrom
tiandiwonder:fix-delete-from-row-exists-privilege

Conversation

@tiandiwonder

@tiandiwonder tiandiwonder commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #90754

Problem

A user granted only ALTER DELETE cannot run DELETE FROM x WHERE ... on a MergeTree table. It fails with:

Code: 497. DB::Exception: ... Not enough privileges. To execute this query,
it's necessary to have the grant ALTER UPDATE(_row_exists) ON db.x. (ACCESS_DENIED)

The documentation states that DELETE requires only the ALTER DELETE privilege, so the behavior and the documentation disagree.

Root cause

For MergeTree, DELETE FROM is a lightweight delete that is rewritten internally into ALTER TABLE ... UPDATE _row_exists = 0 WHERE ... (InterpreterDeleteQuery). That generated mutation runs through InterpreterAlterQuery, whose getRequiredAccessForCommand UPDATE case requires ALTER_UPDATE on every assigned column — including the hidden _row_exists marker. So the user effectively needs ALTER_UPDATE(_row_exists) in addition to ALTER_DELETE. The lightweight-update delete mode hits the same gap through InterpreterUpdateQuery, which required table-level ALTER_UPDATE.

Fix

Treat an assignment to _row_exists as a delete (governed by ALTER DELETE) only when it is the exact lightweight-delete form _row_exists = 0 on the hidden _row_exists marker. Any other assignment — a non-zero value, or the column on an engine where _row_exists is an ordinary physical column (e.g. Memory) — stays a normal update requiring ALTER UPDATE. The same rule is applied to ALTER ... UPDATE, the UPDATE statement, and the corresponding KILL access checks.

So DELETE FROM (which rewrites to _row_exists = 0) needs only ALTER DELETE, as documented, while a user with only ALTER DELETE still cannot edit the deletion mask (e.g. _row_exists = 1) or mutate ordinary data through a physical _row_exists column.

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

Fixed DELETE FROM (lightweight delete) requiring the ALTER UPDATE privilege in addition to ALTER DELETE. A user granted only ALTER DELETE can now run DELETE FROM, as documented.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

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

For MergeTree, `DELETE FROM` is a lightweight delete that is rewritten
internally into `ALTER TABLE ... UPDATE _row_exists = 0 WHERE ...`. The
generated mutation's access check required ALTER_UPDATE on every assigned
column, including the hidden `_row_exists` marker, so a user effectively
needed ALTER_UPDATE(_row_exists) in addition to ALTER_DELETE. This
contradicts the documentation, which states DELETE requires only ALTER
DELETE.

Treat an assignment to the `_row_exists` lightweight-delete marker as a
delete operation governed by ALTER DELETE rather than ALTER UPDATE, in both
InterpreterAlterQuery::getRequiredAccessForCommand (UPDATE case) and
InterpreterUpdateQuery (covers the lightweight-update delete mode and a
direct `UPDATE ... SET _row_exists = 0`). The heavy-delete path already
checks only ALTER_DELETE.

Closes: ClickHouse#90754

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

clickhouse-gh Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4eea2e4]

Summary: ✅


AI Review

Summary

This PR changes the _row_exists = 0 access check so DELETE FROM can run with only ALTER DELETE. The main privilege regression is addressed, but two access paths are still inconsistent with the DELETE FROM contract, so I would not approve this as-is.

Findings

❌ [src/Interpreters/InterpreterAlterQuery.cpp:608, src/Interpreters/InterpreterUpdateQuery.cpp:101] The new ALTER DELETE shortcut reclassifies raw _row_exists = 0 mutations as delete-shaped access, but the execution paths behind ALTER TABLE ... UPDATE _row_exists = 0 and UPDATE ... SET _row_exists = 0 still skip the lightweight-delete guards that InterpreterDeleteQuery enforces at src/Interpreters/InterpreterDeleteQuery.cpp:134-147: enable_lightweight_delete and the projection lightweight_mutation_projection_mode = 'throw' check. After this patch, a user with only ALTER DELETE can still create a delete mask even when DELETE FROM itself is disabled, and on projection tables the raw marker-assignment forms still reach the mutation path that drops projections under THROW at src/Storages/MergeTree/MutateTask.cpp:3510-3517 instead of rejecting the query. Suggested fix: keep the _row_exists = 0 privilege shortcut behind the same guards as DELETE FROM, or reject the raw marker-assignment form whenever those guards would fail.

⚠️ [src/Interpreters/InterpreterKillQueryQuery.cpp:295] The local KILL MUTATION path now mirrors the _row_exists = 0 / ALTER DELETE split, but KILL MUTATION ... ON CLUSTER still returns through getRequiredAccessForDDLOnCluster and unconditionally requires ALTER_UPDATE | ALTER_DELETE | ... at src/Interpreters/InterpreterKillQueryQuery.cpp:470-477. That leaves the privilege contract inconsistent across supported entrypoints: the same delete-shaped mutation can be killed locally with only ALTER DELETE, but not via the cluster path. Suggested fix: align the cluster pre-check with the per-command mutation access rules, or deliberately keep the broader contract everywhere instead of partially special-casing only the local path.

Tests

⚠️ Add a focused regression that ALTER TABLE ... UPDATE _row_exists = 0 with only ALTER DELETE still fails when enable_lightweight_delete = 0.

⚠️ Add the same coverage for both ALTER TABLE ... UPDATE _row_exists = 0 and UPDATE ... SET _row_exists = 0 on a MergeTree table with projections and lightweight_mutation_projection_mode = 'throw', so the raw marker-assignment forms cannot bypass the DELETE FROM guard.

Final Verdict

Status: ❌ Block
Minimum required actions: restore the DELETE FROM lightweight-delete guards for raw _row_exists = 0 mutations, and make the KILL MUTATION privilege story consistent for the cluster entrypoint.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 15, 2026
@tiandiwonder tiandiwonder added the can be tested Allows running workflows for external contributors label Jun 15, 2026
The privilege check treated every assignment to the _row_exists lightweight-
delete marker as ALTER DELETE. But only _row_exists = 0 is a delete (what
DELETE FROM rewrites to); the mutation path accepts arbitrary expressions for
this hidden column, so a user with only ALTER DELETE could run
ALTER TABLE t UPDATE _row_exists = 1 (or the same via the UPDATE statement) to
edit or resurrect the deletion mask without ALTER UPDATE.

Add a shared helper isLightweightDeleteAssignment (true only for
_row_exists = 0) in MergeTreeVirtualColumns, and use it in both
InterpreterAlterQuery (ALTER ... UPDATE) and InterpreterUpdateQuery: _row_exists
= 0 requires ALTER DELETE, any other _row_exists assignment is treated as a
column update requiring ALTER UPDATE. Extend the privilege test to assert
_row_exists = 1 needs ALTER UPDATE while _row_exists = 0 needs ALTER DELETE.
…le comment

Add UPDATE tab_lw SET _row_exists = 0/1 checks (InterpreterUpdateQuery path) for
both the ALTER DELETE-only and ALTER UPDATE-only users, mirroring the ALTER
TABLE checks, so the value-gated split is regression-covered on both paths.

Update the stale note in 04077_lwd_alter_delete_privilege.sh: only
_row_exists = 0 is governed by ALTER DELETE; any other assignment needs ALTER
UPDATE.
The _row_exists = 0 -> ALTER DELETE shortcut keyed only on the column name and
literal, but _row_exists is the lightweight-delete marker only on engines that
expose it as a virtual column. On an engine where _row_exists is an ordinary
physical column (e.g. Memory), ALTER TABLE t UPDATE _row_exists = 0 would have
been treated as a delete, letting an ALTER DELETE-only user mutate real data.

Gate the shortcut on the column not being a physical column of the target
table: getRequiredAccess resolves the storage and passes that down to
getRequiredAccessForCommand; InterpreterUpdateQuery resolves the table the same
way. A null storage (non-local ON CLUSTER target, ALTER DATABASE) fails closed
to ALTER UPDATE. Add a Memory-table regression case with a physical _row_exists
column.
getRequiredAccessForCommand gained a fourth argument, but InterpreterKillQueryQuery
calls it (KILL MUTATION and KILL ... MOVE PARTITION) and was not updated - a build
break. Extract the storage-aware marker check into a shared static
InterpreterAlterQuery::isRowExistsLightweightDeleteMarker and use it at both KILL
sites (each already has the resolved storage), in getRequiredAccess, and in
InterpreterUpdateQuery.
@tiandiwonder
tiandiwonder requested a review from CurtizJ June 22, 2026 12:40

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

LGTM, but please fix one AI review comment.

@CurtizJ CurtizJ self-assigned this Jun 23, 2026
…tual marker

Address review comment (clickhouse-gh, endorsed by CurtizJ): the previous
`!hasPhysical(_row_exists)` check was too broad. Because `getRequiredAccess`
computes the marker state once on the pre-ALTER table, a user with `ALTER ADD
COLUMN` + `ALTER DELETE` (but no `ALTER UPDATE`) could run `ALTER TABLE t ADD
COLUMN _row_exists UInt8, UPDATE _row_exists = 0` and edit a real physical column
with only `ALTER DELETE`. Detect the lightweight-delete marker via
`isVirtualColumn(RowExistsColumn::name)` so the `ALTER DELETE` shortcut applies
only to storages that actually register `_row_exists` as the virtual marker (the
MergeTree family), not to e.g. `Memory`.

Add a regression case in `04328_delete_from_alter_delete_privilege`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
for (const ASTPtr & assignment_ast : command.update_assignments->children)
{
const auto & assignment = assignment_ast->as<const ASTAssignment &>();
if (row_exists_is_lightweight_marker && isLightweightDeleteAssignment(assignment))

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 shortcut now makes the raw _row_exists = 0 mutation reachable with only ALTER DELETE, but the execution path behind it still skips the DELETE FROM safety gates. InterpreterDeleteQuery is the only place that checks enable_lightweight_delete and rejects projections when lightweight_mutation_projection_mode = THROW; direct ALTER TABLE ... UPDATE _row_exists = 0 and the corresponding UPDATE statement go straight through MutationsInterpreter, which only checks supportsLightweightDelete().

As a result, after this patch an ALTER DELETE-only user can still create a lightweight-delete mask even when lightweight deletes are disabled or projections are configured to THROW, for example via SET enable_lightweight_delete = 0; ALTER TABLE t UPDATE _row_exists = 0 WHERE .... Before this change they still needed ALTER UPDATE for that raw marker assignment. Please keep the _row_exists = 0 shortcut behind the same guards as DELETE FROM, or reject the raw marker-assignment form whenever those guards would fail.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pre-existing behavior, not introduced here: ALTER TABLE ... UPDATE _row_exists = 0 has been accepted and executed via InterpreterAlterQuery → MutationsInterpreter since lightweight deletes were introduced, and it has never passed through the enable_lightweight_delete / lightweight_mutation_projection_mode = THROW checks, which live only in InterpreterDeleteQuery. This PR changes only which privilege that form requires; execution paths and gating are untouched, and the projection handling itself is enforced deeper in MutateTask for any _row_exists mutation regardless of entry path — gating the raw ALTER form on those settings would be a behavior change best done as a separate follow-up.

# Conflicts:
#	src/Storages/MergeTree/MergeTreeVirtualColumns.cpp
@@ -293,7 +293,8 @@ BlockIO InterpreterKillQueryQuery::execute()
getContext()->getSettingsRef()[Setting::max_parser_depth],
getContext()->getSettingsRef()[Setting::max_parser_backtracks]);
required_access_rights = InterpreterAlterQuery::getRequiredAccessForCommand(

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.

KILL MUTATION is only consistent for the local path now. If the query has ON CLUSTER, execute returns before this per-command check and getRequiredAccessForDDLOnCluster still unconditionally asks for ALTER_UPDATE | ALTER_DELETE | ... for every mutation.

That means a user with only ALTER DELETE can kill the _row_exists = 0 delete-shaped mutation locally, but the same KILL MUTATION ... ON CLUSTER is still rejected on the initiator with ACCESS_DENIED. Please align the cluster pre-check with the per-command access rules, or the privilege contract this PR is fixing stays inconsistent across local vs cluster KILL MUTATION.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The local-vs-cluster asymmetry is real but pre-existing for every mutation shape — the coarse ON CLUSTER pre-check (all ALTER bits) predates this PR and is deliberately fail-close because the initiator cannot resolve the mutation's command shape before the DDL fans out; this PR only touches the local per-command check. Aligning the cluster pre-check is a fair follow-up, but out of scope for this privilege fix.

@clickhouse-gh

clickhouse-gh Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.80% 85.80% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 78.00% 78.00% +0.00%

Changed lines: Changed C/C++ lines covered: 73/82 (89.02%) · Uncovered code

Full report · Diff report

@tiandiwonder
tiandiwonder added this pull request to the merge queue Jul 10, 2026
Merged via the queue into ClickHouse:master with commit a943f71 Jul 10, 2026
344 of 345 checks passed
@tiandiwonder
tiandiwonder deleted the fix-delete-from-row-exists-privilege branch July 10, 2026 06:52
@clickgapai

Copy link
Copy Markdown
Contributor

Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.

TL;DR

Make DELETE FROM require only the ALTER DELETE privilege

ClickGap verified: reproduced it with the script below on 26.6; tested 7 version(s) — 6 affected, 1 clean (see Affects); long-standing (not a recent regression); introducing PR not yet pinned.

Reproduction

-- Setup (run as an admin with access_management enabled):
CREATE TABLE tab (id UInt32, val UInt32) ENGINE = MergeTree ORDER BY id;
INSERT INTO tab SELECT number, number FROM numbers(10);
CREATE USER u_del IDENTIFIED WITH plaintext_password BY 'pw';
GRANT SELECT, ALTER DELETE ON default.tab TO u_del;

-- Then, as user u_del (granted only SELECT + ALTER DELETE):
DELETE FROM tab WHERE id = 1;
-- Observed pre-fix: Code: 497. ACCESS_DENIED — 'it's necessary to have the grant
--   ALTER UPDATE(_row_exists) ON default.tab', even though only ALTER DELETE is documented.
-- Fixed: the DELETE succeeds with only ALTER DELETE.

▶ Run on ClickHouse Fiddle

Bisect

  • Reproduces on every release line tested down to v23.3.22.3 (the oldest with a downloadable binary for this repro). The introducing change predates v23.3.22.3, so it can't be pinned from release binaries — this is a long-standing issue, not a recent regression. A maintainer with full git history can git blame the relevant file/line to find the exact change.

Affects

  • Verified against master build e209e84c77d1 (aarch64)
  • ✓ Fix is on master — this PR's change fixes the bug on master. Branches below still have the bug and need the backport.
  • Reproduces on: 26.6, 26.5, 26.4, 26.3, 26.2, 26.1
  • Does NOT reproduce on: master
  • Backport needed: 26.6, 26.5, 26.4, 26.3, 26.2, 26.1

Suggested next step

The fix has landed on master; the affected release branches above still need the backport.

Component: comp-rbac
Severity: P2

cc fix PR author: @tiandiwonder
cc fix PR reviewer: @CurtizJ


ClickGap · Finding: phase_d_pr107491

Triage metadata

  • Primary component: comp-rbac
  • Secondary components: comp-mutations
  • Component owners (comp-rbac): @vitlibar @pufit

zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…m-row-exists-privilege

Make DELETE FROM require only the ALTER DELETE privilege
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.

Potential issue: DELETE FROM statement requires ALTER UPDATE privilege, which is inconsistent with the documentation

4 participants