Repository navigation
Reject CREATE ROW POLICY and ALTER ROW POLICY whose filter cannot be applied to an existing table - #126643
Reject CREATE ROW POLICY and ALTER ROW POLICY whose filter cannot be applied to an existing table#126643groeneai wants to merge 5 commits into
CREATE ROW POLICY and ALTER ROW POLICY whose filter cannot be applied to an existing table#126643Conversation
CREATE/ALTER ROW POLICY checked the USING filter only syntactically. The filter is first resolved against a table when the table is read, so a policy using a column the table does not have was stored, and every read of the table by the policy's users failed with UNKNOWN_IDENTIFIER (for ON db.*, every table of the database lacking the column). The policy DDL now analyzes each new filter with buildFilterQueryTree, the helper reads use, against every existing table it is for, in the creator's session, and rejects aggregate, window and GROUPING functions and a result type that cannot be used as a filter, as a read would. - Filters that read another table or a dictionary (subquery, IN with a name that is not a column of the table, IN with a table function, dictGet/joinGet and the other functions that resolve a dictionary or table while being analyzed) are not checked: those names resolve against the reader's current database. They are classified after SQL UDF calls are expanded, so a UDF parameter is judged by its argument. - Only tables whose columns the creator may see (SHOW COLUMNS or SELECT at table level, through the whole Alias chain) are checked, decided by name before a table is waited for or opened, so success, failure and the analyzer's hints disclose nothing else. - Only in a user session: an ON CLUSTER host checks as the creator only when it runs the statement as the initiator's user. - A not yet created lazy or table-function proxy is checked against a StorageDummy of its declared columns when the filter names only stored columns (the dummy differs from the real storage only for other names); otherwise against the real table, as its first read would. - Names the statement leaves untouched (IF NOT EXISTS of an existing policy, ALTER IF EXISTS of a missing one) are not checked. - Persisted, restored and replicated policies are not checked and keep loading. 04368 now creates its projection-only policy before the table; 05228 uses USING 0, which still empties the projection when the policy is wrongly applied to the projection calculation. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A data lake table object keeps the columns it saw at its last read, and a read refreshes them from the latest snapshot before resolving anything (IdentifierResolver, DESCRIBE). The policy DDL analyzed the filter against the cached columns, so a column added to the table through another table object was unknown to the check while every read resolved it, and a valid policy was rejected with UNKNOWN_IDENTIFIER. The check now takes the share lock, refreshes external dynamic metadata and then takes the metadata snapshot, in the DESCRIBE order. The lock is taken with tryLockForShare, so a table dropped or restarting meanwhile is skipped under ON db.* (as system.columns does) instead of failing the statement. 05407 gains the window function and GROUPING cases and a negative case for the declared-columns check of a not yet loaded lazy table; 05408 covers the data lake case. The docs paragraph now says that the server running the statement checks it and that tables of external databases are not checked. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y check An `Alias` whose table was dropped has no columns, but its updateExternalDynamicMetadataIfExists resolves the target table and throws UNKNOWN_TABLE. Since the previous commit refreshed metadata before the empty-columns skip, an `ON db.*` policy over a database holding such an alias, or a policy on the alias itself, failed where master accepts it. The check now returns for a storage whose cached metadata has no columns before refreshing. 05407 gains the dangling `Alias` case and an `ON db.*` case that fails only on the last table in name order, so a walk that stops after the first table is caught. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The check decides which names to analyze from one existence lookup, but CREATE built a policy for every name and ALTER IF EXISTS looked the names up again, so a concurrent DROP or CREATE of the same policy could store a filter that was never checked. CREATE now builds only the checked names and ALTER IF EXISTS updates only them; single-session behaviour is unchanged for every statement shape. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (4 rounds: 5 agreed and fixed, 5 declined with evidence, 2 noted)
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261019-050000 |
|
cc @pufit @vitlibar, could you review this and add the |
|
Workflow [PR], commit [62e37a9] Summary: ❌
AI ReviewSummary
Final VerdictStatus: ✅ Approve. |
…jects
A filter that names an ALIAS column runs the alias expression, so an ALIAS
that calls `dictGet` with a name stored unqualified (for example a computed
`dictGet(concat('d', ''), ...)`) resolves the dictionary against the reader's
current database, exactly like a `dictGet` written in the filter itself. The
check skipped only the latter, so `CREATE ROW POLICY p ON db.t USING tenant = 'a'`
run from another database was rejected with `Dictionary (d) not found` while
reads in `db` work.
`readsOtherObjects` now follows referenced ALIAS columns, through ALIAS chains,
through a subcolumn of an ALIAS (`tup.val`), and also when the column is
qualified with the table or with the database and the table. Any other
qualifier does not name the column, so `wrong.tenant` is still analyzed and
rejected. The documentation also lists parameterized views as not checked:
their catalog entry has no columns. Stored ALIAS expressions already have SQL UDF calls
expanded (`InterpreterCreateQuery` expands column defaults, `InterpreterAlterQuery`
the whole ALTER), so they need no expansion here.
The test also covers a SQL UDF whose body calls `dictGet`, which is skipped only
because the filter is expanded before it is classified.
Reported by the AI review on ClickHouse#126643.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes4 object files changed (+76.73 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 units8 translation units recompiled, 16 s compile time in total, 8 of them have a recent master baseline. |
CI finish ledger - 62e37a9Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261021-113005 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
CREATE ROW POLICYandALTER ROW POLICYnow analyze theUSINGcondition against the existing tables the policy is for (every existing table of the database forON db.*) and fail if it cannot be applied to one of them, for example because it uses a column the table does not have, instead of storing a policy that makes every read of that table fail.Description
Related: #126169 (requested in #126169 (comment))
Related: #121944 (the
ALTER TABLEside)CREATE/ALTER ROW POLICYchecked the filter only syntactically; it is first resolved against a table when the table is read. SoCREATE ROW POLICY p ON db.t USING nosuchcol = 1 TO ALLwas accepted, and every later read ofdb.tby its users failed withUNKNOWN_IDENTIFIER. AnON db.*policy broke every table lacking the column (as a leaked02703policy did in theUpgrade checkof #126078).The policy DDL now resolves each new filter with the read-time helper (
buildFilterQueryTree) against every existing table it is for, in the creator's session (agreed in #126169 (comment)), and, as a read would, rejects aggregate and window functions and a result type unusable as a filter. The analyzer's error is kept, naming the policy and the table.Not checked: filters that read another table or a dictionary (a subquery, a table on the right of
IN,dictGet,joinGet, ..., also via anALIAScolumn), resolved against the reader's current database; tables created later; parameterized views; tables of databases with an external engine (MySQL,PostgreSQL, ...); tables whose columns the creator cannot see (nothing is disclosed);ON CLUSTERhosts that do not run the statement as the initiator's user. Persisted policies are not checked and keep loading after restart, restore, upgrade.Behaviour change: a policy created before
ALTER TABLE ADD COLUMNof the column it uses now fails; add the column first.Validation: new tests
05407and05408(a data lake schema changed outside the table object), the row policy integration tests and all stateless tests that create a row policy; two that create an unresolvable filter on purpose are adjusted.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/126643]