Skip to content

parser: Generate correct types for SELECT NOT EXISTS #1972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 17 additions & 0 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,23 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
cols = append(cols, &Column{Name: name, DataType: "any", NotNull: false})
}

case *ast.BoolExpr:
name := ""
if res.Name != nil {
name = *res.Name
}
notNull := false
if n.Boolop == ast.BoolExprTypeNot && len(n.Args.Items) == 1 {
sublink, ok := n.Args.Items[0].(*ast.SubLink)
if ok && sublink.SubLinkType == ast.EXISTS_SUBLINK {
notNull = true
if name == "" {
name = "not_exists"
}
}
}
cols = append(cols, &Column{Name: name, DataType: "bool", NotNull: notNull})

case *ast.CaseExpr:
name := ""
if res.Name != nil {
Expand Down
32 changes: 32 additions & 0 deletions internal/endtoend/testdata/select_not_exists/pgx/v4/go/db.go

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

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/select_not_exists/pgx/v4/go/models.go

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.

12 changes: 12 additions & 0 deletions internal/endtoend/testdata/select_not_exists/pgx/v4/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE bar (id serial not null);

-- name: BarNotExists :one
SELECT
NOT EXISTS (
SELECT
1
FROM
bar
where
id = $1
);
13 changes: 13 additions & 0 deletions internal/endtoend/testdata/select_not_exists/pgx/v4/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"sql_package": "pgx/v4",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
32 changes: 32 additions & 0 deletions internal/endtoend/testdata/select_not_exists/pgx/v5/go/db.go

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

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/select_not_exists/pgx/v5/go/models.go

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.

12 changes: 12 additions & 0 deletions internal/endtoend/testdata/select_not_exists/pgx/v5/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE bar (id serial not null);

-- name: BarNotExists :one
SELECT
NOT EXISTS (
SELECT
1
FROM
bar
where
id = $1
);
13 changes: 13 additions & 0 deletions internal/endtoend/testdata/select_not_exists/pgx/v5/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"sql_package": "pgx/v5",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/select_not_exists/stdlib/go/db.go

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

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/select_not_exists/stdlib/go/models.go

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.

12 changes: 12 additions & 0 deletions internal/endtoend/testdata/select_not_exists/stdlib/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE bar (id serial not null);

-- name: BarNotExists :one
SELECT
NOT EXISTS (
SELECT
1
FROM
bar
where
id = $1
);
11 changes: 11 additions & 0 deletions internal/endtoend/testdata/select_not_exists/stdlib/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
8 changes: 8 additions & 0 deletions internal/sql/ast/bool_expr_type.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package ast

// https://github.com/pganalyze/libpg_query/blob/13-latest/protobuf/pg_query.proto#L2783-L2789
const (
_ BoolExprType = iota
BoolExprTypeAnd
BoolExprTypeOr
BoolExprTypeNot
)

type BoolExprType uint

func (n *BoolExprType) Pos() int {
Expand Down