Skip to content

Support ON CLUSTER for SET DEFAULT ROLE - #118429

Merged
alexey-milovidov merged 8 commits into
masterfrom
set-default-role-on-cluster
Oct 3, 2026
Merged

alexey-milovidov merged 8 commits into
masterfrom
set-default-role-on-cluster

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

SET DEFAULT ROLE took no ON CLUSTER clause:

SET DEFAULT ROLE r_test TO u_test ON CLUSTER test_cluster;

Code: 62. DB::Exception: Syntax error: failed at position 35 (ON): ON CLUSTER test_cluster.
Expected one of: token, At, Comma, EXCEPT, ParallelWithClause, PARALLEL WITH, end of query. (SYNTAX_ERROR)

even though CREATE ROLE ... ON CLUSTER, CREATE USER ... ON CLUSTER and GRANT ON CLUSTER ... all did, so the one statement in that sequence that could not be distributed was the one setting the default roles.

ASTSetRoleQuery now implements ASTQueryWithOnCluster and the clause is accepted in either position, matching ParserGrantQuery:

SET DEFAULT ROLE role1 TO user ON CLUSTER my_cluster
SET DEFAULT ROLE ON CLUSTER my_cluster role1 TO user

Details:

  • CURRENT_USER is resolved on the initiator before the query is distributed. The AST text is what reaches every host and a DDL worker there runs as its own user, so an unresolved tag would set the default roles of the wrong user. This mirrors what InterpreterGrantQuery does for grantees.
  • The access checks (checkCanAdministerDefaultRoles and ALTER USER on the grantees) run on the initiator before distributing, as they do for the local path.
  • ASTSetRoleQuery is added to isAccessControlQuery in removeOnClusterClauseIfNeeded, so ignore_on_cluster_for_replicated_access_entities_queries drops the redundant clause on a ReplicatedAccessStorage the same way it does for GRANT and CREATE USER.
  • SET ROLE only changes the current session. It takes no ON CLUSTER clause and still fails to parse with one; the parser only looks for the clause in the SET DEFAULT ROLE branch, and the interpreter refuses a cluster on any other kind, in case an AST is built by other means.
  • SET ROLE is also refused when the current context has no user. The DDL worker executes a distributed query in such a context unless distributed_ddl_use_initial_user_and_roles is set, and Context::setCurrentRoles reaches ContextAccess::getUser there, which treats a missing user as a logical error and aborts. The AST fuzzer found this by mutating the queued SET DEFAULT ROLE ... TO ... into a SET ROLE and running it on the worker; a proper exception replaces the abort.

ALTER USER u ON CLUSTER c DEFAULT ROLE r remains the equivalent long form and is unchanged.

Closes: #31058

Changelog category (leave one):

  • Improvement

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

SET DEFAULT ROLE now accepts an ON CLUSTER clause, in either position: SET DEFAULT ROLE role1 TO user ON CLUSTER my_cluster and SET DEFAULT ROLE ON CLUSTER my_cluster role1 TO user. CURRENT_USER is resolved on the initiator before the query is distributed. SET ROLE only affects the current session and still takes no ON CLUSTER clause.


Workflow [PR]
Sync PR [sync-upstream/pr/118429]

Version info

  • Merged into: 26.10.1.1358-master (included in 26.10 and later)

    SET DEFAULT ROLE r TO u ON CLUSTER c

    Code: 62. DB::Exception: Syntax error: failed at position 35 (ON): ON CLUSTER c.
    (SYNTAX_ERROR)

while `CREATE ROLE ... ON CLUSTER`, `CREATE USER ... ON CLUSTER` and
`GRANT ON CLUSTER ...` all worked, so setting a user's default roles fleet-wide meant
falling back to `ALTER USER u ON CLUSTER c DEFAULT ROLE r`.

`ASTSetRoleQuery` now implements `ASTQueryWithOnCluster`, and the clause is accepted in
either position, as `GRANT` does:

    SET DEFAULT ROLE r TO u ON CLUSTER c
    SET DEFAULT ROLE ON CLUSTER c r TO u

`CURRENT_USER` is resolved on the initiator before the query is distributed - the AST
text is what reaches every host, and a DDL worker there runs as its own user.

`SET ROLE` only changes the current session, so it takes no `ON CLUSTER` clause and
still fails to parse with one.

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

clickhouse-gh Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [55b82a1]

Summary: ✅


AI Review

Summary

This PR teaches SET DEFAULT ROLE to accept ON CLUSTER, wires it into the ASTQueryWithOnCluster / distributed DDL path, resolves role names on the initiator, and keeps the TO CURRENT_USER semantics aligned on DDL workers when distributed_ddl_use_initial_user_and_roles = 1. I reviewed the current diff, the updated code, the prior discussion threads, and the added stateless/integration coverage; the previously reported contract violations appear fixed, and I did not find any remaining correctness, privilege, or failure-mode issues that warrant a new review comment.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 55b82a1.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.50% 79.50% +0.00%

Changed lines: Changed C/C++ lines covered: 89/93 (95.70%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Sep 6, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. label Sep 6, 2026
The AST fuzzer aborted with `No user in current context, it's a bug`. Making
`ASTSetRoleQuery` distributable exposed a path that did not exist before: the DDL worker
executes a distributed query in a context with no user, unless
`distributed_ddl_use_initial_user_and_roles` is set, and the server-side fuzzer mutated
the queued `SET DEFAULT ROLE ... TO ...` into the session-local `SET ROLE`, which reached
`Context::setCurrentRoles` there. `ContextAccess::getUser` treats a missing user as a
logical error and aborts:

    DDLWorker: Executing query: SET DEFAULT ROLE `05107_role_a` TO `05107_user`
    executeQuery: (internal) SET ROLE `05107_role_a` (stage: Complete)
    <Fatal> : Logical error: 'No user in current context, it's a bug'.

Two guards:

  - Refuse a cluster on any kind other than `SET DEFAULT ROLE`. Only that one changes the
    user; `SET ROLE` and `SET ROLE DEFAULT` change the current session, for which a
    cluster is meaningless. The parser only accepts the clause for `SET DEFAULT ROLE`, so
    this covers an AST built by other means.
  - Refuse `SET ROLE` when the current context has no user, with a proper exception
    instead of an abort.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The AST fuzzer failure was real and caused by this PR. Diagnosis from the artifacts:

DDLWorker: Executing query: SET DEFAULT ROLE `05107_role_a` TO `05107_user`
executeQuery: (internal) SET ROLE `05107_role_a` (stage: Complete)
<Fatal> : Logical error: 'No user in current context, it's a bug'.

with executeASTFuzzerQueries in the stack between DDLWorker::processTask and InterpreterSetRoleQuery::setRole. Making ASTSetRoleQuery distributable opened a path that did not exist before: the DDL worker executes a distributed query in a context with no user (distributed_ddl_use_initial_user_and_roles defaults to 0), and the server-side fuzzer mutated the queued SET DEFAULT ROLE ... TO ... into the session-local SET ROLE, which reached Context::setCurrentRoles there. ContextAccess::getUser treats a missing user as a logical error and aborts.

Two guards added:

  • Refuse a cluster on any kind other than SET DEFAULT ROLE. Only that one changes the user; SET ROLE/SET ROLE DEFAULT change the current session, for which a cluster is meaningless. The parser only accepts the clause for SET DEFAULT ROLE, so this covers an AST built by other means.
  • Refuse SET ROLE when the current context has no user, with a proper exception instead of an abort.

Verified against a local build:

  • SET DEFAULT ROLE ... ON CLUSTER still works in both clause positions (['v_ra'] then ['v_rb']) — i.e. the distributed path still runs on the DDL worker, and now without aborting.
  • Plain SET ROLE / SET ROLE DEFAULT / SET ROLE NONE still work for a real user (['sr_role'], then []), so the guard does not block legitimate use.
  • SET ROLE ... ON CLUSTER and SET ROLE DEFAULT ... ON CLUSTER are still SYNTAX_ERROR; the second is now pinned in the test.
  • No Logical error in the server log, and system.errors reports 0 LOGICAL_ERROR.

@clickhouse-gh

clickhouse-gh Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 55b82a1fe with master 918d3af67 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 49a47ff77; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

8 object files changed (+50.20 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/Access/InterpreterSetRoleQuery.cpp.o 55.36 KiB 92.02 KiB +36.65 KiB (+66.20%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

14 translation units recompiled, 62 s compile time in total, 14 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.09 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +9.2 s (+17%) in total before that adjustment.

Job report

alexey-milovidov and others added 2 commits September 11, 2026 09:40
…distributing

Addresses the review of `SET DEFAULT ROLE ... ON CLUSTER`:

1. The role names are now resolved into a `RolesOrUsersSet` before the cluster branch, so an unknown
role is rejected on the initiator instead of being queued into the DDL log and failing on every host.
This is the contract of both the local `SET DEFAULT ROLE` path and `ALTER USER ... DEFAULT ROLE ...
ON CLUSTER`, which builds `default_roles_from_query` before calling `executeDDLQueryOnCluster` too.
Pinned in `05107_set_default_role_on_cluster`.

2. `replaceCurrentUserTag` moved into the cluster branch, after the access check. `collectRequiredGrants`
collects nothing for the `CURRENT_USER` tag and `ALTER USER` on the named user otherwise, so resolving
the tag before the check made `SET DEFAULT ROLE ... TO CURRENT_USER` require `ALTER USER` on oneself,
which plain `SET DEFAULT ROLE` never did. The local path does not need the tag resolved at all -
`RolesOrUsersSet` resolves it from the context's user id.

3. New test `05182_set_default_role_on_cluster_current_user.sh`: a non-`default` user with only its role
and `CLUSTER` granted runs `SET DEFAULT ROLE <role> TO CURRENT_USER ON CLUSTER test_shard_localhost`,
and the test asserts that this user - and no other - gets the default role. Verified as a real control:
with `replaceCurrentUserTag` commented out, the DDL worker reaches `RolesOrUsersSet` with no user in
its context and the server aborts on `optional operator* called on a disengaged value`.

4. That abort also poisons the DDL queue: the entry is retried on every restart, so the server no longer
starts. Guard the local path against a `CURRENT_USER` tag in a context with no user and throw
`BAD_ARGUMENTS` instead. Verified on the crash-looping data directory from item 3 - the server starts
and reports `SET DEFAULT ROLE TO CURRENT_USER requires a user in the current context` for the queued entry.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Pushed the review fixes (role names resolved on the initiator before the query is distributed, CURRENT_USER resolved only on the cluster path and only after the access check, plus a new 05182_set_default_role_on_cluster_current_user.sh control test) and merged master (the branch was 2673 commits behind).

Both public CI reds on the previous head are unrelated to this PR:

@groeneai, investigate these two failures and provide fixes in separate PRs:

If a fix is already in progress, link it here. I could not find an open issue for either.

CH Inc sync was tests failed on the previous head, with the single red being Stress test (arm_asan_ubsan, SharedCatalog, meta in keeper) in the private sync PR; the report itself is not publicly readable. The new head re-runs the whole sync from scratch on top of current master.

@groeneai

Copy link
Copy Markdown
Collaborator

Both failures already have owners, and neither needs a new pull request from me.

00084_external_aggregation (Stateless tests (amd_debug, parallel))

Code: 241, Query memory limit exceeded while spilling. Fixed by #118474, merged 2026-09-08 13:14Z (4ee25bab45a5), backported to 26.8 in #118803; #117326 (09-04) covered a narrower part of the same drain path.

The head in your report predates it: compare/4ee25bab...b92a5ca7 gives behind_by: 854, ahead_by: 2. Your new head daf4bcccc7ee gives behind_by: 0, so it carries the fix.

In checks, on true master (pull_request_number = 0 AND head_ref = 'master'), the last failure is 2026-09-08, and both 09-08 master rows sit on trees that are ancestors of the fix merge (behind_by: 27 and behind_by: 141, ahead_by: 0 for both). 09-09, 09-10 and 09-11: none. The daily rate you measured is the window before the fix landed.

One row survives fleet-wide since then, and it is the headroom margin rather than the drain residue: #115014 on 09-11, Stateless tests (arm_asan_ubsan, azure, parallel), would use 572.57 MiB ... maximum: 572.20 MiB while executing SourceFromNativeStream, 0 of 3 reruns.

Stress test (arm_tsan) data race

The open issue is #118784, filed under STID 3097-4413. It does not mention 4806-4fd5, which is the same defect reported from the read side, so a search on the STID in your report does not reach it.

Two fixes are open:

Neither has merged, so the race is still live: 14 rows across 13 pull requests between 09-05 and 09-08, none since, which I read as amd_tsan/arm_tsan stress volume rather than as a fix.

alexey-milovidov and others added 2 commits September 23, 2026 10:33
…served initial user

The initiator replaces the `CURRENT_USER` tag with its user name before queueing
the DDL entry. With `distributed_ddl_use_initial_user_and_roles` (and entry format
version 8) the DDL worker runs the query as that user, and `collectRequiredGrants`
then demanded `ALTER USER` on the user's own name, so a query the initiator
accepted failed on every host. In a distributed query executed with a user, the
user's own name no longer requires a grant; other names still do.

Added an integration test, since a stateless test cannot enable the server
setting. Without the fix it fails with `Not enough privileges ... ALTER USER ON u`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (the branch was 6717 commits behind) and pushed the fix for the AI Review blocker, the TO CURRENT_USER path under distributed_ddl_use_initial_user_and_roles (details in the thread). New integration test test_set_default_role_on_cluster_initial_user fails without the fix and passes with it.

@groeneai, thanks. The new head carries #118474, so 00084_external_aggregation is covered. The only red on the previous head was Upgrade check (amd_release) / Virtual row has different type (STID 1637-309b). This PR doesn't touch read-in-order, and #119195 ("Fix stale virtual row conversion ...", merged 2026-09-12) is now pulled in by the merge. I'll check the fresh run.

alexey-milovidov and others added 2 commits October 2, 2026 10:42
… merging master

`ASTRolesOrUsersSet::names` is now an `ASTUserNamesWithHost` node instead of
`Strings`, so drop the current user's name from its children instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master and adapted InterpreterSetRoleQuery to the new ASTUserNamesWithHost type of ASTRolesOrUsersSet::names (the merge did not compile otherwise). 05107_set_default_role_on_cluster and 05182_set_default_role_on_cluster_current_user pass locally.

The previous red checks are unrelated to this PR: Stress test (arm_release) hit GroupingAggregatedTransform pushed bucket 0 twice, which is tracked in #115986, and Performance Comparison (arm_release, master_head, 2/6) reports queries that do not touch access control.

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

Good.

@alexey-milovidov alexey-milovidov self-assigned this Oct 2, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 2, 2026
Merged via the queue into master with commit e907c7b Oct 3, 2026
150 checks passed
@alexey-milovidov
alexey-milovidov deleted the set-default-role-on-cluster branch October 3, 2026 00:01
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 3, 2026
alexey-milovidov pushed a commit that referenced this pull request Oct 13, 2026
Resolve the cherry-pick of #118021 to 26.9.

The conflict in `InterpreterSetRoleQuery.cpp` comes from #118429, which
is not on 26.9 and has no backport label: on master `setRole` also
refuses a context without a user, next to the new `readonly` check.
Only the `readonly` check is added.

The new integration test merges cleanly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov pushed a commit that referenced this pull request Oct 13, 2026
Resolve the cherry-pick of #118021 to 26.8.

The conflict in `InterpreterSetRoleQuery.cpp` comes from #118429, which
is not on 26.8 and has no backport label: on master `setRole` also
refuses a context without a user, next to the new `readonly` check.
Only the `readonly` check is added.

`ParserCreateHandlerQuery.cpp` has no `getDocumentation` on 26.8, so its
documentation change is dropped.

The new integration test merges cleanly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov added a commit that referenced this pull request Oct 13, 2026
Resolve conflicts for the 26.7 backport of #118021:
- `InterpreterSetRoleQuery.cpp`: keep only the `readonly` check; the
  missing-user check comes from #118429, which is not on 26.7.
- `SQLDefinedHandlerFromAST.cpp`, `ParserCreateHandlerQuery.cpp`,
  `04824_handler_safe_method_session_state.sh`: removed, 26.7 has no
  SQL-defined handlers.

Resolution matches the one proposed by @groeneai in
#125310 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov pushed a commit that referenced this pull request Oct 13, 2026
Resolve the cherry-pick of #118021 to 26.3.

The conflict in `InterpreterSetRoleQuery.cpp` comes from #118429, which
is not on 26.3 and has no backport label: on master `setRole` also
refuses a context without a user, next to the new `readonly` check.
Only the `readonly` check is added.

`docs/concepts/features/interfaces/http.mdx` and the SQL-defined handler
files (`SQLDefinedHandlerFromAST.cpp`, `ParserCreateHandlerQuery.cpp`,
`04824_handler_safe_method_session_state.sh`) do not exist on 26.3, so
their comment and documentation changes are dropped. The server setting
row is added to 26.3's `_server_settings_outside_source.md`.

The new integration test merges cleanly.

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

comp-rbac Authorization: roles, grants, quotas, row-level security, access checks. pr-improvement Pull request with some product improvements 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.

SET DEFAULT ROLE support for ON CLUSTER

3 participants