Repository navigation
Add AI FUNCTIONS privilege group - #127057
Conversation
Per-function privileges (`aiGenerate`, `aiEmbed`, ...) grouped under `AI FUNCTIONS`, checked when the function is created. Widen `AccessType` to `uint16_t`. Closes: #121411 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…AI functions Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
|
Workflow [PR], commit [d1c77cd] Summary: ✅
AI ReviewSummaryThis PR introduces per-function AI privileges and rejects AI expressions in background operations. Request changes: a definer view loses the new validation marker and allows an unsupported mutation to be persisted. The current head also retains the previously discussed alias-dependency and compatibility failures below. Those author-dismissed findings are recorded here without further inline replies. The column-TTL/default issue is fixed in the current head. Findings
Tests
Final VerdictStatus: |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
The changes here are needed because the docs regenerator only runs nightly on master, and so links to autogenerated sections wouldn't work in this PR, and fail the Docs check.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes270 object files changed (+135.24 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units1540 translation units recompiled, 10369 s compile time in total, 1539 of them have a recent master baseline. Translation units without a recent master baseline:
|
…he defining user Keys, TTL, indices and projections are built with the global context, so function access checks in them passed for any user. Create each function with the query context when a table is defined. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Manerone
left a comment
There was a problem hiding this comment.
From my point of view the PR is good. I left some comments that Claude pointed out.
- Distributed sharding key is open. It's built with the server's full-access context, so cityHash64(aiGenerate(...)) is never checked, and every INSERT then calls the AI provider.
| if (is_fresh_definition && !is_ddl_replay && !is_stored_definition) | ||
| { | ||
| checkAccessToFunctions(*args.storage_def, args.getLocalContext()); | ||
| if (args.query.columns_list) | ||
| checkAccessToFunctions(*args.query.columns_list, args.getLocalContext()); | ||
| } |
There was a problem hiding this comment.
Claude complained about this, I think it is also fine to handle this in another PR:
Doesn't check the user on Replicated databases or ON CLUSTER. There, CREATE TABLE runs under a server context with no user unless distributed_ddl_use_initial_user_and_roles is set, and it defaults to false. So the check passes for everyone. ALTERs that aren't mutations (MODIFY TTL, ADD INDEX, etc.) have the same gap. Fix: run the check in InterpreterCreateQuery/InterpreterAlterQuery with the user's context, before the query is handed off to replicas.
There was a problem hiding this comment.
…ions` CI enables `table_engines_require_grant`, so the denials came from the missing engine grant. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| static FunctionPtr create(ContextPtr context_) { return std::make_shared<FunctionAiGenerate>(context_); } | ||
| static FunctionPtr create(ContextPtr context_) | ||
| { | ||
| context_->checkAccess(AccessType::aiGenerate); |
There was a problem hiding this comment.
create is also reached by the analyzer's speculative arity lookup, before it knows whether the identifier names this built-in. In src/Analyzer/Resolve/resolveFunction.cpp, tryGetRegisteredFunctionArity calls FunctionFactory::tryGet at line 141, and the bare-function rewrite calls that helper at line 2251 before checking column/lambda aliases at lines 2280–2286. Consequently, a user without the new grant cannot run this ordinary arithmetic query:
WITH (x -> x + 1) AS aiGenerate
SELECT arrayMap(aiGenerate, [1]);It should return [2], but the speculative lookup creates the built-in and throws ACCESS_DENIED, although the query never calls an AI function. The same problem applies to the other newly guarded names.
Please make the probe preserve alias precedence without constructing a privileged built-in first, while retaining the check when an actual AI call is resolved. Add a negative-privilege regression case for this alias-only query alongside the existing denial cases.
There was a problem hiding this comment.
Confirmed. The introspection functions are affected the same way, and the query fails closed, so it is tracked separately: #128160
@Manerone Good catch, but it's also not specific to this PR so I think it should be fixed in a separate one, I added a comment on https://github.com/ClickHouse/clickhouse-private/issues/83375. |
…y context Replaces the AST walk, which missed `TTL ... GROUP BY ... SET` assignments. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Keep 26.8's `validate_mutation_query` gate, `checkAlterIsPossible` and grant parser (no generated docs on 26.8). Use `isRecoveryFromStoredMetadata` and the ZooKeeper transaction for the fresh-definition check. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #127057 to 26.8: Add AI FUNCTIONS privilege group
Keep 26.9's `validate_mutation_query` gate and generated `grant.mdx`; drop master-only neighbours of the new members and checks. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #127057 to 26.9: Add AI FUNCTIONS privilege group
Keep 26.10's generated `grant.mdx`; drop master-only neighbours of the new lines. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #127057 to 26.10: Add AI FUNCTIONS privilege group
Backport #127057 to 26.8: Add AI FUNCTIONS privilege group
Backport #127057 to 26.9: Add AI FUNCTIONS privilege group
Backport #127057 to 26.10: Add AI FUNCTIONS privilege group
…e ALTER's session `AlterCommands::apply` re-derived the sorting, primary and partition keys and the `minmax_count` projection in the context of the session that issued the `ALTER`, while `CREATE`, a reload of the table and the part writers derive them in the global context. A session setting that changes key analysis therefore installed in-memory keys that the table does not really have. With `transform_null_in = 1`, `in` is rewritten to `nullIn`, which renames the key column and, for a `Nullable` argument, changes its type from `Nullable(UInt8)` to `UInt8`. After such an `ALTER` (even a comment-only one): - an INSERT into a table with a Nullable key under `IN` serialized the writer's `ColumnNullable` with the stored `UInt8` serialization: `Bad cast from type DB::ColumnNullable to DB::ColumnVector<char8_t>` in debug builds, a primary index of garbage bytes in release builds; - every merge failed with `NOT_FOUND_COLUMN_IN_BLOCK: Not found column in(x, ...)`; - a partition key wrote `partition.dat` with the session type, so after a restart the parts were detached as `broken-on-start`. Derive these keys in the global context, as ClickHouse#119799 already does for skip indices in the same function, and carry `isAnalyzingBackgroundExpressions` into it so that AI functions stay rejected in keys (ClickHouse#127057). The sampling key, projections and column TTLs keep the session context: the sampling key is re-analysed in the query context at read time, and projections and TTLs are separate derivations. Found by the targeted AST fuzzer on PR ClickHouse#130066 (STID 1499-5685). Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
|
||
| Calling an AI function requires two privileges: | ||
|
|
||
| - the function's privilege: either the per-function privilege (e.g. `aiGenerate`) or the `AI FUNCTIONS` group, see [`GRANT`](/reference/statements/grant#ai-functions); |
There was a problem hiding this comment.
Per-function privileges seem honestly too fine-granular. The purpose of the privilege (group) is to prevent unintended costly remote LLM calls. All AI functions do such calls. I doubt that DBAs would need more flexibility than just saying "Allow all remote LLM calls or forbid them.".
Could we please have just a single privilege AI FUNCTIONS? If someone asks for more fine-granular configurability, we can add that later.
There was a problem hiding this comment.
This is already possible with the current change, a user can just grant the AI FUNCTIONS privilege. The fine-grained privileges are included "for free" so I don't see any reason to remove them. This is how other privileges are implemented as well.
| /// Set for CREATE queries a Replicated database replays from a definition it already stored. | ||
| /// Such a definition describes existing state, so validation that may reject a new one must not run. | ||
| bool is_recovery_from_stored_metadata = false; | ||
| /// Set while analyzing expressions that are executed later in the background, without a user: |
There was a problem hiding this comment.
Could we check if the information whether we are in a background operation by already existing means instead of introducing another state variable merely for the AI functions to context?
Closes: #121411
Calling an AI function previously required only
NAMED COLLECTIONaccess to the selected credentials. This adds an explicit entitlement to use AI functionality, independent of credential access.Each AI function gets its own
GLOBALprivilege (aiClassify,aiEmbed,aiExtract,aiFilter,aiGenerate,aiRedact,aiSimilarity,aiTranslate), grouped underAI FUNCTIONS. The check runs in each function'screate, i.e. at query analysis time: before any outbound request, also for zero-row queries, and with the usual effective-user semantics forSQL SECURITY DEFINER/INVOKERviews and SQL UDFs.Functions used in expressions stored in a
MergeTreetable definition (keys, TTL, skip indexes, projections) are now checked against the privileges of the user creating the table. This applies to all function privileges, not only AI ones. Definitions loaded from stored metadata are not checked.AI functions are not executed with a context without a user, which is the case for merges, mutations and
Bufferflushes. They are rejected, even when the user has the privilege, in newly definedMergeTreekeys, TTL expressions, skip indexes and projections, in theDEFAULT/MATERIALIZEDexpression of a column with a TTL or of a column added or modified byALTER, and in mutations. A background operation that fails on an AI function adds an entry tosystem.warnings.DEFAULTandMATERIALIZEDcolumns defined atCREATEwithout a TTL are evaluated by the insert and keep working. To fill a column of existing rows, use a lightweightUPDATEorINSERT ... SELECTinstead ofALTER TABLE ... UPDATE.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added the
AI FUNCTIONSprivilege group and per-function privileges (aiGenerate,aiEmbed, etc.) that control who can call AI functions, e.g.GRANT AI FUNCTIONS ON *.* TO roleorGRANT aiGenerate, aiClassify ON *.* TO role. Calling an AI function now requires its privilege in addition toNAMED COLLECTIONaccess to the selected credentials. To keep existing users working, runGRANT AI FUNCTIONS ON *.* TO <user_or_role>. Functions used in the keys, TTL, skip indexes and projections of a newMergeTreetable now require the privileges of the user creating the table. AI functions are rejected in newly definedMergeTreekeys, TTL expressions, skip indexes and projections, inDEFAULTandMATERIALIZEDexpressions computed in the background, and in mutations, even when the user has the AI privilege; use a lightweightUPDATEorINSERT ... SELECTinstead ofALTER TABLE ... UPDATEto fill a column with AI function results.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/127057]
Version info
26.11.1.1614-master(included in26.11and later)