Repository navigation
Conversation
QA LLM Review1. MEDIUM -- Constant folding hides nested SQL functions from the pin
A materialized view selecting DetailsThe outer function delegates directly to 2. MEDIUM -- Pins omit SQL functions embedded in imported index keys
Give a table with a DetailsThe optimizer copies the index key into the import at 3. MEDIUM -- The registry skips nonconstant views that fold into pinned results
A materialized view selecting Details
|
0537fa1 to
c52b23a
Compare
QA LLM Review1. MEDIUM -- Metric sinks can pin builtin relations through replacement materialized views
A metric sink can read a pending replacement materialized view whose gauge value is DetailsThe metric-sink planner accepts the replacement as long as its columns match the sink schema ( |
This PR extends the builtin registry to cover SQL-defined builtin functions and constant builtin views (views that read no collection, such as the time zone views generated from chrono-tz). Their SQL text goes in the source half of the snapshot, so a change needs a person to decide whether pinned plans must pick it up. Builtins in unstable schemas like mz_internal are excluded because persistent user objects can't depend on them. A new adapter test fails if a stable function's body ever reaches an unstable one in a way a pin could capture. Pins now record which builtins they inlined, in a new inlined_builtins field. A later LIR_VERSION migration can then replan only the pins a change affects, not every pin. The way to create a pin is now PinnedDataflow::pin, which takes a PinningContext, and CatalogState is that context. Callers can't skip the inlining information by accident.
Pinning now rejects an item that depends, directly or through user views, on any builtin relation, including a builtin view that reads no collection. Such a view leaves no import behind once inlined, so only the catalog walk can tell. Constant builtin views are therefore no longer recorded in the builtin registry, and `InlinedBuiltinKind` keeps only `Func`. Recording them can be added later without invalidating existing pins. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Catalog items now keep the ids planning resolves inside the bodies of the SQL-defined functions they call, transitively, in a new in-memory field next to `resolved_ids`. They stay out of `uses()`: a builtin body's references are not user-visible dependencies. Pinning reads them, recording nested SQL-defined builtins and rejecting builtin relations and unstable builtins reached through a body, even when constant folding removed the call. It also records the builtins inlined into the keys of imported indexes, which a reader copies into its plan. This replaces the test that required SQL bodies inlining builtins to read a collection, which constant folding defeated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Planning cannot read a pending replacement's collection, so it inlines the replacement's definition, as it does a user view's. The pinning walk now does the same, so a metric sink over a replacement that reads a builtin relation is refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… test order-independent Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…and imports Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
b46c056 to
937f58f
Compare
ggevay
left a comment
There was a problem hiding this comment.
Thanks for the rework, this looks good to me. Minor comments inline.
| // Constant folding can remove the read, though, so refuse on the | ||
| // reference, which keeps the rule that nothing depending on a | ||
| // builtin relation is pinned. | ||
| if referenced.is_relation() { |
There was a problem hiding this comment.
Could pin_rejects_builtin_relations also cover a builtin relation that is read only inside a function body, such as mz_roles in a materialized view over pg_catalog.pg_get_userbyid(NULL::oid)?
Two checks reject that view today: this one, and the walk's uses(), which includes what the unoptimized plan reads inside function and cast bodies. With either one disabled, the tests and 23 extra cases give the same results. With both disabled, the view is pinned and the tests still pass.
Keeping both seems right to me: this branch doesn't depend on what uses() contains, and uses() may change (SQL-439).
Separately, the doc of function_body_references says that uses() leaves these ids out. That holds for the functions and types that a body names, not for the relations that it reads.
| let stmt = mz_sql::parse::parse(create_sql)?.into_element().ast; | ||
| let (stmt, resolved_ids) = mz_sql::names::resolve(catalog, stmt)?; | ||
| let (plan, _sql_impl_ids) = | ||
| let (plan, function_body_ids) = |
There was a problem hiding this comment.
Two comments in src/sql/src/plan/statement.rs (lines 484 and 553) still say that these ids are only for the restrict_to_user_objects RBAC check. Pinning now uses them too.
| // Mirrors `DataflowBuilder::import_into_dataflow`, which inlines | ||
| // user views and pending replacements, whose collections cannot | ||
| // be read yet, and imports everything else. |
There was a problem hiding this comment.
Could this comment say what the walk follows, and how that differs from import_into_dataflow? "Mirrors" reads as if both inlined the same items. They differ in three ways:
import_into_dataflowimports an indexed user view through its index. The walk goes into the view's definition anyway.import_into_dataflowalso inlines builtin views. The walk stops at a builtin view and rejects the pin.import_into_dataflowreads a view's locally optimized expression. The walk followsuses(), so it also sees reads that optimization removes.
We have many dependency traversals with slightly different rules (SQL-439), so it helps when each one states its own.
Motivation
PR 3 of SQL-923.
Description
The builtin registry tracks scalar functions, aggregate and table functions (#40912), and now SQL-defined functions. Each pin records the SQL-defined builtins it may have inlined, so a later change to one can be traced to the pins it affects.
To know what a pin inlined, catalog items now keep the ids planning resolves inside the bodies of the SQL-defined functions they call, transitively. Planning already computed these for an RBAC check and discarded them. They are now kept in memory next to
resolved_ids, outsideuses(), since a builtin body's references are not user-visible dependencies. Pinning walks the export, the user views and pending replacements planning inlines into it, and these function bodies. It also records what the keys of imported indexes inline.Pinning rejects any object that depends on a builtin relation, directly, through user views, or through a function body. That includes builtin views that read no collection, such as
mz_timezone_names, which leave no import behind once inlined, and reads that constant folding removes. Recording such views instead could come later without invalidating existing pins.Pinning also rejects any object that reaches an unstable builtin, including through a function body. (Even with
unsafe_enable_unstable_dependencies, since pinning such an object will always be a footgun.) In practice this makes objects calling thehas_*_privilegefamily unpinnable.Verification
New tests of the rebuilt registry and of pinning: recorded SQL-defined functions through views, nested calls captured from function bodies and refused when folding hides them, functions inlined into imported index keys, pending replacements, and the refusals for builtin relations and unstable builtins.