Skip to content

tp: let semantic analysis see CTEs - #7643

Merged
LalitMaganti merged 2 commits into
mainfrom
dev/lalitm/analysis-cte-scopes
Oct 1, 2026
Merged

LalitMaganti merged 2 commits into
mainfrom
dev/lalitm/analysis-cte-scopes

Conversation

@LalitMaganti

@LalitMaganti LalitMaganti commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

Semantic analysis skipped the definitions of a WITH clause, so a query reading a CTE could not be described, and a pipeline could not read SQL which uses one:

FROM (
  WITH nodes AS (SELECT id, parent_id, self FROM tree)
  SELECT * FROM nodes
)
|> TREE ACCUMULATE UP SUM(self) AS total

Now each CTE is in scope for the rest of its WITH clause and the query it belongs to, hiding any relation of the same name there, as in SQLite:

  • a CTE's list of column names renames its columns, as a view's does;
  • a view still sees only what its names meant where it was defined;
  • a recursive CTE reads itself as a relation of unknown shape.

What is analyzed is also read where it is in its statement: AnalyzeQuery(query) and AnalyzeRelation(at, name) see the CTEs in scope at that node. The analyzer finds them by following its own walk down to the node (statements, WITH clauses, compound selects, FROM clauses and pipeline sources), defining only the CTEs it passes. So once pipelines can be subqueries, the SQL they read can name a CTE of the statement they are written in:

WITH t AS (SELECT id, parent_id, self FROM tree)
SELECT * FROM (FROM t |> TREE ACCUMULATE UP SUM(self) AS total)

@LalitMaganti
LalitMaganti added this pull request to stack #7645 September 27, 2026 20:08
@LalitMaganti
LalitMaganti removed this pull request from stack #7645 September 27, 2026 20:08
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from c024449 to 8ca182d Compare September 27, 2026 20:43
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from 8ca182d to 9c25c8f Compare September 27, 2026 21:13
@github-actions

github-actions Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

🎨 Perfetto UI Builds & Tests

@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from 9c25c8f to 3124216 Compare September 27, 2026 22:55
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch 2 times, most recently from 1673785 to 7ff5acd Compare September 27, 2026 23:45
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from 7ff5acd to 9e2c303 Compare September 28, 2026 03:53
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from 9e2c303 to 502bb9c Compare September 28, 2026 04:21
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from 502bb9c to 9380a1c Compare September 28, 2026 05:28
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from 9380a1c to eeb1d9a Compare September 28, 2026 06:06
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch 2 times, most recently from 07466a2 to d735e3f Compare September 29, 2026 15:34
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from d735e3f to 0df32ce Compare September 29, 2026 15:45
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from c6a734a to c7d39cf Compare September 29, 2026 17:13
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from c7d39cf to 2cddcca Compare September 29, 2026 17:36
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from 2cddcca to 8cf3966 Compare September 29, 2026 17:59
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from 8cf3966 to 0602c38 Compare September 29, 2026 18:20
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from 0602c38 to 89d15cb Compare September 29, 2026 19:04
@LalitMaganti
LalitMaganti marked this pull request as ready for review September 29, 2026 19:18
@LalitMaganti
LalitMaganti requested a review from a team as a code owner September 29, 2026 19:18
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from 89d15cb to f1330bb Compare September 29, 2026 19:44
Comment thread src/perfetto_sql/analysis/relation.cc Outdated
Comment thread src/perfetto_sql/analysis/relation.cc Outdated
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from f1330bb to 4503200 Compare October 1, 2026 00:17
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch 2 times, most recently from f5b8ed1 to 120223e Compare October 1, 2026 00:41
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch 2 times, most recently from 5c833f0 to c199ff4 Compare October 1, 2026 00:51
No behaviour change. A pipeline source and each intersection operand
now go through one step, CompileRelation, which gives the plan node and
columns of what the source reads, and the names in scope while a
pipeline is compiled live together in one Scope. Also renames
MacroRewriteBuilder to RewriteTree, as syntaqlite calls these rewrites.
Semantic analysis skipped the definitions of a WITH clause, so a query
reading a CTE could not be described: a pipeline could not read SQL
which uses one. Each CTE is now in scope for the rest of its WITH
clause and the query it belongs to, hiding any relation of the same
name there, as in SQLite. A view still sees only what its name meant
where it was defined, and a recursive CTE reads itself as a relation
of unknown shape.

What is analyzed is also read where it is in its statement: a query,
or a relation named at some node, sees the CTEs in scope there. So the
SQL a pipeline reads can name a CTE of the statement the pipeline is
written in, once pipelines can be written inside one.
@LalitMaganti
LalitMaganti force-pushed the dev/lalitm/analysis-cte-scopes branch from c199ff4 to e5a8795 Compare October 1, 2026 01:29
Base automatically changed from dev/lalitm/pipeline-compiler-scope to main October 1, 2026 01:30
@LalitMaganti
LalitMaganti merged commit 4ccba28 into main Oct 1, 2026
22 of 23 checks passed
@LalitMaganti
LalitMaganti deleted the dev/lalitm/analysis-cte-scopes branch October 1, 2026 01:30
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