Skip to content

Commit 2d7b302

Browse files
committed
LATERAL subquery support
1 parent b807fe9 commit 2d7b302

File tree

9 files changed

+179
-1
lines changed

9 files changed

+179
-1
lines changed

internal/compiler/output_columns.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,24 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
592592
tables = append(tables, table)
593593

594594
case *ast.RangeSubselect:
595-
cols, err := c.outputColumns(qc, n.Subquery)
595+
lateralQC := qc
596+
if n.Lateral && len(tables) > 0 {
597+
// LATERAL allows the subquery to reference columns from preceding FROM items.
598+
lateralTables := make(map[string]*Table, len(tables))
599+
for _, table := range tables {
600+
if table.Rel != nil && table.Rel.Name != "" {
601+
lateralTables[table.Rel.Name] = table
602+
}
603+
}
604+
lateralQC = &QueryCatalog{
605+
catalog: qc.catalog,
606+
ctes: qc.ctes,
607+
embeds: qc.embeds,
608+
lateralTables: lateralTables,
609+
}
610+
}
611+
612+
cols, err := c.outputColumns(lateralQC, n.Subquery)
596613
if err != nil {
597614
return nil, err
598615
}
@@ -637,6 +654,15 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
637654
return nil, fmt.Errorf("sourceTable: unsupported list item type: %T", n)
638655
}
639656
}
657+
658+
// Add LATERAL outer tables to the tables list.
659+
// LATERAL subqueries can reference these outer tables for column resolution.
660+
if qc != nil {
661+
for _, lateralTable := range qc.lateralTables {
662+
tables = append(tables, lateralTable)
663+
}
664+
}
665+
640666
return tables, nil
641667
}
642668

internal/compiler/query_catalog.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type QueryCatalog struct {
1212
catalog *catalog.Catalog
1313
ctes map[string]*Table
1414
embeds rewrite.EmbedSet
15+
16+
lateralTables map[string]*Table
1517
}
1618

1719
func (comp *Compiler) buildQueryCatalog(c *catalog.Catalog, node ast.Node, embeds rewrite.EmbedSet) (*QueryCatalog, error) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/sqlc-dev/sqlc/issues/4182
2+
3+
LATERAL subquery column references from outer query fail with "column does not exist".

internal/endtoend/testdata/lateral_view_column_ref/postgresql/pgx/go/db.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/lateral_view_column_ref/postgresql/pgx/go/models.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/lateral_view_column_ref/postgresql/pgx/go/query.sql.go

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- name: GetFooLateral :many
2+
SELECT val, result FROM foo_lateral;
3+
4+
-- name: GetFooLateralDirect :many
5+
SELECT f.id, f.val, sub.result
6+
FROM foo f
7+
CROSS JOIN LATERAL (
8+
SELECT f.val || '-direct' AS result
9+
) sub;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE TABLE foo (
2+
id integer PRIMARY KEY,
3+
val text NOT NULL
4+
);
5+
6+
-- Reproduces issue #4182: LATERAL subquery referencing outer column
7+
CREATE VIEW foo_lateral AS
8+
SELECT t.val, sub.result
9+
FROM foo t
10+
CROSS JOIN LATERAL (
11+
SELECT t.val AS result
12+
FROM foo LIMIT 1
13+
) sub;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "postgresql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "pgx/v5"

0 commit comments

Comments
 (0)