Skip to content

Commit 0842233

Browse files
committed
fix(compiler): reject sqlc.embed on the nullable side of an outer join
An embedded struct is the model of its table, whose columns may not be nullable. When the table is on the nullable side of a LEFT, RIGHT or FULL JOIN the query returns NULLs the struct cannot hold, so the generated code compiled but failed at runtime with "converting NULL to string is unsupported". Report an error at generate time instead. The join walk is the one that already makes plain columns nullable (isTableRequired); embeds skipped it because their column carries no table. Only the top-level FROM clause is checked, as for plain columns. The error is returned together with the columns. A database analyzer discards inference errors and replaces the columns when none come back, which would have turned the embed into flat columns. Updates #2997
1 parent 3ad8b20 commit 0842233

28 files changed

Lines changed: 389 additions & 1 deletion

File tree

‎internal/compiler/output_columns.go‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/sqlc-dev/sqlc/internal/sql/astutils"
1010
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
1111
"github.com/sqlc-dev/sqlc/internal/sql/lang"
12+
"github.com/sqlc-dev/sqlc/internal/sql/preprocess"
1213
"github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
1314
)
1415

@@ -53,6 +54,8 @@ func hasStarRef(cf *ast.ColumnRef) bool {
5354
// Return an error if column references are ambiguous
5455
// Return an error if column references don't exist
5556
func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
57+
var embedErr error
58+
5659
tables, err := c.sourceTables(qc, node)
5760
if err != nil {
5861
return nil, err
@@ -267,6 +270,11 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
267270

268271
// add a column with a reference to an embedded table
269272
if embed, ok := qc.embeds.Find(n.Location, res.Location); ok {
273+
// Keep the column and report the error with the rest, so an
274+
// analyzer that discards inference errors still sees the embed.
275+
if err := checkEmbedRequired(node, embed, res.Location); err != nil && embedErr == nil {
276+
embedErr = err
277+
}
270278
cols = append(cols, &Column{
271279
Name: embed.Table.Name,
272280
EmbedTable: embed.Table,
@@ -416,7 +424,7 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er
416424
}
417425
}
418426

419-
return cols, nil
427+
return cols, embedErr
420428
}
421429

422430
const (
@@ -467,6 +475,31 @@ func isTableRequired(n ast.Node, col *Column, prior int) int {
467475
return tableNotFound
468476
}
469477

478+
// An embedded struct is the model of its table, whose columns may not be
479+
// nullable, so it cannot hold the NULLs the optional side of an outer join
480+
// produces.
481+
func checkEmbedRequired(node ast.Node, embed *preprocess.Embed, location int) error {
482+
n, ok := node.(*ast.SelectStmt)
483+
if !ok {
484+
return nil
485+
}
486+
col := &Column{Table: embed.Table, TableAlias: embed.Param()}
487+
for _, f := range n.FromClause.Items {
488+
res := isTableRequired(f, col, tableRequired)
489+
if res != tableNotFound {
490+
if res == tableOptional {
491+
return &sqlerr.Error{
492+
Code: "0A000",
493+
Message: fmt.Sprintf("sqlc.embed(%s) on the nullable side of an outer join is not supported", embed.Param()),
494+
Location: location,
495+
}
496+
}
497+
break
498+
}
499+
}
500+
return nil
501+
}
502+
470503
type tableVisitor struct {
471504
list ast.List
472505
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"contexts": ["base"]
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- name: LeftJoin :many
2+
SELECT sqlc.embed(posts), sqlc.embed(users) FROM posts
3+
LEFT JOIN users ON posts.user_id = users.id;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE users (
2+
id integer PRIMARY KEY,
3+
name text NOT NULL
4+
);
5+
6+
CREATE TABLE posts (
7+
id integer PRIMARY KEY,
8+
user_id integer
9+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "postgresql",
7+
"name": "querytest",
8+
"schema": "schema.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# package querytest
2+
query.sql:2:27: sqlc.embed(users) on the nullable side of an outer join is not supported
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"contexts": ["base"]
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- name: LeftJoin :many
2+
SELECT sqlc.embed(p), sqlc.embed(u) FROM posts AS p
3+
LEFT JOIN users AS u ON p.user_id = u.id;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE users (
2+
id integer PRIMARY KEY,
3+
name text NOT NULL
4+
);
5+
6+
CREATE TABLE posts (
7+
id integer PRIMARY KEY,
8+
user_id integer
9+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "sqlite",
7+
"name": "querytest",
8+
"schema": "schema.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)