Skip to content

add materialized subqueries - #7640

Open
nwt wants to merge 1 commit into
mainfrom
materialized
Open

nwt wants to merge 1 commit into
mainfrom
materialized

Conversation

@nwt

@nwt nwt commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator

let q = materialized (...), with t as materialized (...), and values materialized (...) all run their query once with a single null value as input when first evaluated, cache the output, and produce that output for subsequent evaluations.

`let q = materialized (...)`, `with t as materialized (...)`, and
`values materialized (...)` all run their query once with a single null
value as input when first evaluated, cache the output, and produce that
output for subsequent evaluations.
@nwt
nwt requested a review from a team November 11, 2026 04:42
```
materialized ( <query> )
```
A materialized subquery is evaluated just once and receives a single `null`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"is evaluated just once and returns the initial result on each subsequent invocation"

... "a materialized subquery has no access to the expression context in which it is evaluated... the body of the subquery has no parent operator and like all such queries, its input is a single null value"

^^^ I'm not proposing this exact wording... just this direction


To create a query that can operate on its input, declare an [operator](operators.md).

With `materialized`, the query is evaluated once, its output is cached,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave out the "cache" concept as that is mechanism leaking into the semantic definition here

Comment thread compiler/dag/op.go

type MaterializedDef struct {
Kind string `json:"kind" unpack:""`
Name string `json:"name"`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why names? They don't appear in the user facing model. Like functions, it seems like they should be a slice that is ref'd by slot in the slice.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm maybe the names relate to the querydecl name? They can't live in a flat table because they are lexically scoped.

Comment thread compiler/dag/op.go
}
MaterializedRefOp struct {
Kind string `json:"kind" unpack:""`
Name string `json:"name"`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name -> Slot ?
(see other comment)

/ Map
/ SQLTimeExpr
/ Literal
/ MATERIALIZED __ "(" __ expr:SubqueryExpr __ ")" {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Materialized should work for array query too. The PEG rules could be improved by sharing the def of a subquery between expr and let, e.g., create a Materializable rule defined as

OptMaterialized __ "(" __ body:Query __ ")"
/ OptMaterialized __ "[" __ body:Query __ "]"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants