Skip to content
Merged
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
2 changes: 1 addition & 1 deletion examples/booktest/postgresql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestBooks(t *testing.T) {
t.Fatal(err)
}
for _, ab := range res {
t.Logf("Book %d: '%s', Author: '%s', ISBN: '%s' Tags: '%v'\n", ab.BookID, ab.Title, ab.Name, ab.Isbn, ab.Tags)
t.Logf("Book %d: '%s', Author: '%s', ISBN: '%s' Tags: '%v'\n", ab.BookID, ab.Title, ab.Name.String, ab.Isbn, ab.Tags)
}

// TODO: call say_hello(varchar)
Expand Down
3 changes: 2 additions & 1 deletion examples/booktest/postgresql/query.sql.go

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
Expand Up @@ -23,7 +23,7 @@ WHERE tags && ?::varchar[]
data class BooksByTagsRow (
val bookId: Int,
val title: String,
val name: String,
val name: String?,
val isbn: String,
val tags: List<String>
)
Expand Down
2 changes: 1 addition & 1 deletion examples/python/src/booktest/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class BooksByTagsRow:
book_id: int
title: str
name: str
name: Optional[str]
isbn: str
tags: List[str]

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/jackc/pgx/v4 v4.11.0
github.com/jinzhu/inflection v1.0.0
github.com/kr/pretty v0.2.1 // indirect
github.com/lib/pq v1.10.0
github.com/lib/pq v1.10.1
github.com/pganalyze/pg_query_go/v2 v2.0.2
github.com/pingcap/parser v0.0.0-20201024025010-3b2fb4b41d73
github.com/spf13/cobra v1.1.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.0 h1:Zx5DJFEYQXio93kgXnQ09fXNiUKsqv4OUEu2UtGcB1E=
github.com/lib/pq v1.10.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lib/pq v1.10.1 h1:6VXZrLU0jHBYyAqrSPa+MgPfnSvTPuMgK+k0o5kVFWo=
github.com/lib/pq v1.10.1/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
Expand Down
48 changes: 48 additions & 0 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,57 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
}
}

if n, ok := node.(*ast.SelectStmt); ok {
for _, col := range cols {
if !col.NotNull || col.Table == nil {
continue
}
for _, f := range n.FromClause.Items {
if res := isTableRequired(f, col.Table.Name, tableRequired); res != tableNotFound {
col.NotNull = res == tableRequired
break
}
}
}
}

return cols, nil
}

const (
tableNotFound = iota
tableRequired
tableOptional
)

func isTableRequired(n ast.Node, tableName string, prior int) int {
switch n := n.(type) {
case *ast.RangeVar:
if *n.Relname == tableName {
return prior
}
case *ast.JoinExpr:
helper := func(l, r int) int {
if res := isTableRequired(n.Larg, tableName, l); res != tableNotFound {
return res
}
if res := isTableRequired(n.Rarg, tableName, r); res != tableNotFound {
return res
}
return tableNotFound
}
switch n.Jointype {
case ast.JoinTypeLeft:
return helper(tableRequired, tableOptional)
case ast.JoinTypeRight:
return helper(tableOptional, tableRequired)
case ast.JoinTypeFull:
return helper(tableOptional, tableOptional)
}
}
return tableNotFound
}

// Compute the output columns for a statement.
//
// Return an error if column references are ambiguous
Expand Down
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/join_full/mysql/go/db.go

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

16 changes: 16 additions & 0 deletions internal/endtoend/testdata/join_full/mysql/go/models.go

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

45 changes: 45 additions & 0 deletions internal/endtoend/testdata/join_full/mysql/go/query.sql.go

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

8 changes: 8 additions & 0 deletions internal/endtoend/testdata/join_full/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE foo (id serial not null, bar_id int references bar(id));
CREATE TABLE bar (id serial not null);

-- name: FullJoin :many
SELECT f.id, f.bar_id, b.id
FROM foo f
FULL OUTER JOIN bar b ON b.id = f.bar_id
WHERE f.id = $1;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/join_full/mysql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/join_full/postgresql/go/db.go

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

16 changes: 16 additions & 0 deletions internal/endtoend/testdata/join_full/postgresql/go/models.go

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

45 changes: 45 additions & 0 deletions internal/endtoend/testdata/join_full/postgresql/go/query.sql.go

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

8 changes: 8 additions & 0 deletions internal/endtoend/testdata/join_full/postgresql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE foo (id serial not null, bar_id int references bar(id));
CREATE TABLE bar (id serial not null);

-- name: FullJoin :many
SELECT f.id, f.bar_id, b.id
FROM foo f
FULL OUTER JOIN bar b ON b.id = f.bar_id
WHERE f.id = $1;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/join_full/postgresql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/join_left/mysql/go/db.go

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

Loading