Skip to content

Enforce settings constraints installed by a profile in the same statement - #120376

Merged
Algunenano merged 9 commits into
ClickHouse:masterfrom
Algunenano:enforce-profile-constraints-in-same-statement
Sep 25, 2026
Merged

Algunenano merged 9 commits into
ClickHouse:masterfrom
Algunenano:enforce-profile-constraints-in-same-statement

Conversation

@Algunenano

@Algunenano Algunenano commented Sep 16, 2026 •

Copy link
Copy Markdown
Member

A settings constraint installed by a settings profile was not enforced against the other setting assignments in the same statement. Context::applySettingsChanges walks the change list and replaces the constraint set when it reaches the profile entry, but the whole list has already been checked against the constraints in force before the statement. So SET profile = 'p', max_memory_usage = 1099511627776 overrode the MAX that profile p installs, SET profile = 'p', max_execution_time = 999 overrode its CONST, and a readonly = 1 declared by the profile could be lifted in the same SET, while the same settings applied as two separate statements were correctly rejected. Re-checking after the list is applied does not help: the constraint checker skips a change whose value already equals the current value.

The check now follows the order in which the changes take effect. Context::checkSettingsConstraints and Context::clampToSettingsConstraints enforce each run of changes up to the next profile entry against the constraints in force at that point, on a scratch copy of the context, so a rejected statement leaves the session untouched and the caller applies the changes exactly once. SET name = DEFAULT is checked with the statement's profile applied. A list without a profile entry keeps the previous path. Fixing the primitives covers all of their callers: SET (in every dialect, since they all reuse ParserSetQuery), an outer or nested SETTINGS clause, a WITH subquery, HTTP URL parameters, gRPC, the Prometheus handler, TCP secondary queries, distributed DDL replay, SQL SECURITY DEFINER views and EXECUTE AS.

gRPC and Arrow Flight receive their settings in a map with no order, so they put profile first: its constraints bind the other settings, and those settings override the values it sets. Arrow Flight's SetSessionOptions also checked every option against a snapshot taken before the loop; it now checks against the session context it mutates.

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

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

Fixed settings constraints from a settings profile not being enforced against the other setting assignments in the same statement. SET profile = 'p', <setting> = <value> could override a CONST, MIN/MAX or readonly constraint that profile p installs. The same escape existed through an in-query SETTINGS clause, HTTP URL parameters and Arrow Flight session options.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.678-master (included in 26.10 and later)
  • Backported to: 26.9.3.35, 26.8.11.55, 26.7.16.4, 26.3.36.5

@clickhouse-gh

clickhouse-gh Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e96c55d]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_debug) FAIL
Logical error: Unexpected return type from A. Expected B. Got C. Action: (STID: 1611-45e7) FAIL cidb IGNORED

AI Review

Summary

This PR fixes the ordered SET path and the unordered gRPC/Arrow Flight settings maps by validating profile in a deterministic way, but one HTTP bypass remains: repeated URL parameters that straddle profile are still collapsed into the first occurrence before constraint checking. Because the PR now claims the HTTP path is fixed and rewrites the regression test around that assumption, I would still request changes.

Findings

❌ Blockers

  • [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/120376#discussion_r4085481216] [src/Server/HTTPHandler.cpp:371-392] HTTP still lets a repeated setting bypass a later profile constraint. HTMLForm::readQuery stores every occurrence with add, and NameValueCollection keeps equal keys in one contiguous block at the first occurrence (src/Server/HTTP/HTMLForm.cpp:138-184, src/Common/tests/gtest_poco_list_map.cpp:23-39). The loop here then feeds that grouped block into SettingsChanges::setSetting, which overwrites the first max_memory_usage entry in place instead of preserving the later position. A request such as ?max_memory_usage=7&profile=p&max_memory_usage=1099511627776 therefore becomes effectively max_memory_usage=1099511627776, profile=p, so the over-limit value is checked against the pre-profile constraints and survives a later MAX/readonly profile. The updated regression only covers single-occurrence parameters (tests/queries/0_stateless/05220_settings_constraints_profile_in_http_params.sh:21-26), so this escape is now untested. Suggested fix: either preserve setting occurrences in raw request order for the settings pipeline, or reject repeated setting names whenever the request also carries profile, and restore the repeated-parameter regression.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required action: close the remaining HTTP repeated-parameter bypass (or reject that request shape explicitly) and add the focused regression back.

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Sep 16, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-settings Settings & configuration infrastructure (server/user/query settings, profiles). label Sep 16, 2026
Comment thread src/Interpreters/Context.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing e96c55d86 with master 845c70990 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 8ec0771f5; 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

77 object files changed (+50.52 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Interpreters/Context.cpp.o 2.84 MiB 2.88 MiB +45.16 KiB (+1.55%)

716 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

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

Job report

Algunenano and others added 2 commits September 23, 2026 11:48
Checking every change against the final profile let a later, looser `profile` lift the constraint of an earlier one in the same `SET`. Go back to segment by segment checking, and move `profile` first only in HTTP and gRPC, whose input has no order. Fix the gRPC tests, whose `display_text` has no error name.

Related: ClickHouse#120376 (comment)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Moving `profile` first changed which assignment wins, e.g. `run_query_in_background=1&profile=p` let the explicit value override the profile (`04666_run_query_in_background_http`). Apply the list in its own order, and check each change against the constraints both before and after the profile, since these sources have no order.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Comment thread src/Interpreters/Context.cpp Resolved
The check against the constraints before the profile erased changes equal to the current value, so `?profile=p&max_result_rows=0` lost the explicit value and the profile won. Check that pass on a `const` view.

Related: ClickHouse#120376 (comment)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Comment thread src/Server/GRPCServer.cpp Resolved
Comment thread src/Server/HTTPHandler.cpp Resolved
…ces put profile first

gRPC now puts `profile` first when building the list from the protobuf map, so the other settings are bound by it and override its values deterministically. `checkSettingsConstraintsInAnyOrder` is removed and HTTP keeps master's handling: Poco groups a repeated URL parameter with its first occurrence before the handler runs, so there is no later position to restore. Tests are batched into few client calls.

Related: ClickHouse#120376 (comment)

Co-Authored-By: Claude Fable 5.1 <[email protected]>

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

Looks good

@Algunenano

Copy link
Copy Markdown
Member Author

🕵 The AST fuzzer (amd_debug) failure on this run is unrelated to this PR.

It's a pre-existing bug: a lambda argument named the same as an INTERPOLATE synthetic column gets resolved to that column by the analyzer but to the lambda argument by the planner, so analysis and execution disagree on the type:

Logical error: 'Unexpected return type from moduloLegacy. Expected UInt64. Got UInt8.'

Already tracked upstream in #121254 (same STID 1611-45e7), with an open fix at #121230. The same signature also fired on master's own AST fuzzer runs on 2026-09-24 and 2026-09-25, confirming it's unrelated to this change.

@Algunenano
Algunenano added this pull request to the merge queue Sep 25, 2026
Merged via the queue into ClickHouse:master with commit 585f97e Sep 25, 2026
170 of 172 checks passed
@Algunenano
Algunenano deleted the enforce-profile-constraints-in-same-statement branch September 25, 2026 15:15
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 25, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 25, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 25, 2026
Backport #120376 to 26.9: Enforce settings constraints installed by a profile in the same statement
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 25, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 25, 2026
Backport #120376 to 26.8: Enforce settings constraints installed by a profile in the same statement
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 28, 2026
…LECT ... SETTINGS` does

The core settings of a BACKUP/RESTORE clause went through the non-const
`checkSettingsConstraints` overload, which drops every change equal to the value
the setting already has. So for `SETTINGS max_execution_time = 0.000001,
max_execution_time = 0` the trailing item was dropped and the first one applied,
while `SELECT ... SETTINGS` keeps both and ends at 0. That is also the shape an
ON CLUSTER BACKUP/RESTORE forwards to the other hosts for a core reset (the
override, then the declared default), and each host applies it through this
same branch. `execute` and `executeForCurrentContext` already use the const
overload for this reason.

The resets also skipped the profile-aware check from ClickHouse#120376, so
`BACKUP ... SETTINGS profile = 'p', max_execution_time = DEFAULT` was accepted
when `p` makes the setting constant, while `SET` and `SELECT ... SETTINGS` reject
it. Pass the clause's changes to the three-argument overload, as the other two
paths do.

Both are measured over HTTP. The native client applies the clause on its side
first (a `profile` item fails there with THERE_IS_NO_PROFILE), and the
duplicate-item case does not reproduce through it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-ch-test-poll added a commit that referenced this pull request Sep 28, 2026
Cherry pick #120376 to 26.7: Enforce settings constraints installed by a profile in the same statement
robot-clickhouse added a commit that referenced this pull request Sep 28, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Sep 28, 2026
Cherry pick #120376 to 26.3: Enforce settings constraints installed by a profile in the same statement
robot-clickhouse added a commit that referenced this pull request Sep 28, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 28, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 28, 2026
Backport #120376 to 26.7: Enforce settings constraints installed by a profile in the same statement
Algunenano added a commit that referenced this pull request Sep 28, 2026
Backport #120376 to 26.3: Enforce settings constraints installed by a profile in the same statement
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 2, 2026
…alled by a profile in the same statement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-settings Settings & configuration infrastructure (server/user/query settings, profiles). pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

5 participants