Skip to content

Commit d1d16c9

Browse files
EliSauderCopilot
andcommitted
fix(mssql): embed a table only when its columns are as nullable as the table's
sqlc.embed(b) on the optional side of an outer join folded b's columns into the Book model, whose fields cannot hold NULL, so scanning a row with no match failed. A table is now embedded only when each column is as nullable as the table's, and otherwise its columns stay flat. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8e0928f commit d1d16c9

8 files changed

Lines changed: 120 additions & 2 deletions

File tree

‎internal/compiler/parse_core.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ func embedColumns(raw *ast.RawStmt, cols []*Column, stars []core.StarExpansion,
148148
}
149149

150150
// isWholeTable reports whether cols are every column of one table, in the
151-
// table's order.
151+
// table's order, each as nullable as the table's: the optional side of an
152+
// outer join is not, and the table's model could not hold its NULLs.
152153
func isWholeTable(cat *core.Catalog, cols []*Column) bool {
153154
table := cols[0].Table
154155
if table == nil {
@@ -175,7 +176,7 @@ func isWholeTable(cat *core.Catalog, cols []*Column) bool {
175176
if tc.Hidden {
176177
continue
177178
}
178-
if i >= len(cols) || cols[i].Table == nil || *cols[i].Table != *table || cols[i].OriginalName != tc.Name {
179+
if i >= len(cols) || cols[i].Table == nil || *cols[i].Table != *table || cols[i].OriginalName != tc.Name || cols[i].NotNull != tc.NotNull {
179180
return false
180181
}
181182
i++
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"contexts": ["base"]}

‎internal/endtoend/testdata/mssql_embed_outer_join/mssql/go/db.go‎

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

‎internal/endtoend/testdata/mssql_embed_outer_join/mssql/go/models.go‎

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

‎internal/endtoend/testdata/mssql_embed_outer_join/mssql/go/query.sql.go‎

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: AuthorBooks :many
2+
SELECT sqlc.embed(a), sqlc.embed(b) FROM authors a LEFT JOIN books b ON b.author_id = a.id;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE TABLE authors (id INT NOT NULL, name NVARCHAR(10) NOT NULL);
2+
CREATE TABLE books (id INT NOT NULL, author_id INT NOT NULL, title NVARCHAR(20) NULL);
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: "mssql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "database/sql"

0 commit comments

Comments
 (0)