Skip to content
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

fix: CASE-ELSE clause is not properly parsed when a value is constant #2238

Merged
merged 2 commits into from
Apr 28, 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
2 changes: 1 addition & 1 deletion internal/compiler/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func ParseTableName(node ast.Node) (*ast.TableName, error) {
func ParseTypeName(node ast.Node) (*ast.TypeName, error) {
rel, err := parseRelation(node)
if err != nil {
return nil, fmt.Errorf("parse table name: %w", err)
return nil, fmt.Errorf("parse type name: %w", err)
}
return &ast.TypeName{
Catalog: rel.Catalog,
Expand Down
17 changes: 12 additions & 5 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
if res.Name != nil {
name = *res.Name
}
// TODO: The TypeCase code has been copied from below. Instead, we
// TODO: The TypeCase and A_Const code has been copied from below. Instead, we
// need a recurse function to get the type of a node.
if tc, ok := n.Defresult.(*ast.TypeCast); ok {
if tc.TypeName == nil {
Expand All @@ -175,11 +175,18 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
col.Name = name
cols = append(cols, col)
} else if aconst, ok := n.Defresult.(*ast.A_Const); ok {
tn, err := ParseTypeName(aconst.Val)
if err != nil {
return nil, err
switch aconst.Val.(type) {
case *ast.String:
cols = append(cols, &Column{Name: name, DataType: "text", NotNull: true})
case *ast.Integer:
cols = append(cols, &Column{Name: name, DataType: "int", NotNull: true})
case *ast.Float:
cols = append(cols, &Column{Name: name, DataType: "float", NotNull: true})
case *ast.Boolean:
cols = append(cols, &Column{Name: name, DataType: "bool", NotNull: true})
default:
cols = append(cols, &Column{Name: name, DataType: "any", NotNull: false})
}
cols = append(cols, &Column{Name: name, DataType: dataType(tn), NotNull: true})
} else {
cols = append(cols, &Column{Name: name, DataType: "any", NotNull: false})
}
Expand Down
32 changes: 32 additions & 0 deletions internal/endtoend/testdata/case_text/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/case_text/pgx/v4/go/models.go

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

38 changes: 38 additions & 0 deletions internal/endtoend/testdata/case_text/pgx/v4/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/case_text/pgx/v4/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE foo (id text not null);

-- name: CaseStatementText :many
SELECT CASE
WHEN id = $1 THEN 'foo'
ELSE 'bar'
END is_one
FROM foo;
13 changes: 13 additions & 0 deletions internal/endtoend/testdata/case_text/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/case_text/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/case_text/pgx/v5/go/models.go

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

38 changes: 38 additions & 0 deletions internal/endtoend/testdata/case_text/pgx/v5/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/case_text/pgx/v5/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE foo (id text not null);

-- name: CaseStatementText :many
SELECT CASE
WHEN id = $1 THEN 'foo'
ELSE 'bar'
END is_one
FROM foo;
13 changes: 13 additions & 0 deletions internal/endtoend/testdata/case_text/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/case_text/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/case_text/stdlib/go/models.go

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

41 changes: 41 additions & 0 deletions internal/endtoend/testdata/case_text/stdlib/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/case_text/stdlib/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE foo (id text not null);

-- name: CaseStatementText :many
SELECT CASE
WHEN id = $1 THEN 'foo'
ELSE 'bar'
END is_one
FROM foo;
11 changes: 11 additions & 0 deletions internal/endtoend/testdata/case_text/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"
}
]
}