Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
LATERAL subquery support
  • Loading branch information
WillAbides committed Nov 26, 2025
commit 2d7b302a85a2da586681dadd514f836360be4ec5
28 changes: 27 additions & 1 deletion internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,24 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
tables = append(tables, table)

case *ast.RangeSubselect:
cols, err := c.outputColumns(qc, n.Subquery)
lateralQC := qc
if n.Lateral && len(tables) > 0 {
// LATERAL allows the subquery to reference columns from preceding FROM items.
lateralTables := make(map[string]*Table, len(tables))
for _, table := range tables {
if table.Rel != nil && table.Rel.Name != "" {
lateralTables[table.Rel.Name] = table
}
}
lateralQC = &QueryCatalog{
catalog: qc.catalog,
ctes: qc.ctes,
embeds: qc.embeds,
lateralTables: lateralTables,
}
}

cols, err := c.outputColumns(lateralQC, n.Subquery)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -637,6 +654,15 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
return nil, fmt.Errorf("sourceTable: unsupported list item type: %T", n)
}
}

// Add LATERAL outer tables to the tables list.
// LATERAL subqueries can reference these outer tables for column resolution.
if qc != nil {
for _, lateralTable := range qc.lateralTables {
tables = append(tables, lateralTable)
}
}

return tables, nil
}

Expand Down
2 changes: 2 additions & 0 deletions internal/compiler/query_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type QueryCatalog struct {
catalog *catalog.Catalog
ctes map[string]*Table
embeds rewrite.EmbedSet

lateralTables map[string]*Table
}

func (comp *Compiler) buildQueryCatalog(c *catalog.Catalog, node ast.Node, embeds rewrite.EmbedSet) (*QueryCatalog, error) {
Expand Down
3 changes: 3 additions & 0 deletions internal/endtoend/testdata/lateral_view_column_ref/issue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/sqlc-dev/sqlc/issues/4182

LATERAL subquery column references from outer query fail with "column does not exist".

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- name: GetFooLateral :many
SELECT val, result FROM foo_lateral;

-- name: GetFooLateralDirect :many
SELECT f.id, f.val, sub.result
FROM foo f
CROSS JOIN LATERAL (
SELECT f.val || '-direct' AS result
) sub;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE foo (
id integer PRIMARY KEY,
val text NOT NULL
);

-- Reproduces issue #4182: LATERAL subquery referencing outer column
CREATE VIEW foo_lateral AS
SELECT t.val, sub.result
FROM foo t
CROSS JOIN LATERAL (
SELECT t.val AS result
FROM foo LIMIT 1
) sub;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "2"
sql:
- engine: "postgresql"
schema: "schema.sql"
queries: "query.sql"
gen:
go:
package: "querytest"
out: "go"
sql_package: "pgx/v5"
Loading