From ba273ca6194ae5abd2473aa85817629a81361c35 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 12 Jul 2023 17:10:23 -0700 Subject: [PATCH] fix(compiler): Validate UNION ... ORDER BY --- internal/compiler/output_columns.go | 5 ++- .../testdata/order_by_union/mysql/go/db.go | 31 ++++++++++++++ .../order_by_union/mysql/go/models.go | 18 +++++++++ .../order_by_union/mysql/go/query.sql.go | 40 +++++++++++++++++++ .../testdata/order_by_union/mysql/query.sql | 14 +++++++ .../testdata/order_by_union/mysql/sqlc.json | 12 ++++++ .../order_by_union/postgresql/go/db.go | 31 ++++++++++++++ .../order_by_union/postgresql/go/models.go | 19 +++++++++ .../order_by_union/postgresql/go/query.sql.go | 40 +++++++++++++++++++ .../order_by_union/postgresql/query.sql | 15 +++++++ .../order_by_union/postgresql/sqlc.json | 12 ++++++ 11 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 internal/endtoend/testdata/order_by_union/mysql/go/db.go create mode 100644 internal/endtoend/testdata/order_by_union/mysql/go/models.go create mode 100644 internal/endtoend/testdata/order_by_union/mysql/go/query.sql.go create mode 100644 internal/endtoend/testdata/order_by_union/mysql/query.sql create mode 100644 internal/endtoend/testdata/order_by_union/mysql/sqlc.json create mode 100644 internal/endtoend/testdata/order_by_union/postgresql/go/db.go create mode 100644 internal/endtoend/testdata/order_by_union/postgresql/go/models.go create mode 100644 internal/endtoend/testdata/order_by_union/postgresql/go/query.sql.go create mode 100644 internal/endtoend/testdata/order_by_union/postgresql/query.sql create mode 100644 internal/endtoend/testdata/order_by_union/postgresql/sqlc.json diff --git a/internal/compiler/output_columns.go b/internal/compiler/output_columns.go index 26a421f025..36265d9052 100644 --- a/internal/compiler/output_columns.go +++ b/internal/compiler/output_columns.go @@ -65,6 +65,7 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er targets = n.ReturningList case *ast.SelectStmt: targets = n.TargetList + isUnion := len(targets.Items) == 0 && n.Larg != nil if n.GroupClause != nil { for _, item := range n.GroupClause.Items { @@ -77,7 +78,7 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er if c.conf.StrictOrderBy != nil { validateOrderBy = *c.conf.StrictOrderBy } - if validateOrderBy { + if !isUnion && validateOrderBy { if n.SortClause != nil { for _, item := range n.SortClause.Items { sb, ok := item.(*ast.SortBy) @@ -110,7 +111,7 @@ func (c *Compiler) outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, er // For UNION queries, targets is empty and we need to look for the // columns in Largs. - if len(targets.Items) == 0 && n.Larg != nil { + if isUnion { return c.outputColumns(qc, n.Larg) } case *ast.CallStmt: diff --git a/internal/endtoend/testdata/order_by_union/mysql/go/db.go b/internal/endtoend/testdata/order_by_union/mysql/go/db.go new file mode 100644 index 0000000000..ec5e95c4ab --- /dev/null +++ b/internal/endtoend/testdata/order_by_union/mysql/go/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.19.0 + +package querytest + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/order_by_union/mysql/go/models.go b/internal/endtoend/testdata/order_by_union/mysql/go/models.go new file mode 100644 index 0000000000..3985bcaca2 --- /dev/null +++ b/internal/endtoend/testdata/order_by_union/mysql/go/models.go @@ -0,0 +1,18 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.19.0 + +package querytest + +import ( + "database/sql" +) + +type Author struct { + Name string + Bio sql.NullString +} + +type Person struct { + FirstName string +} diff --git a/internal/endtoend/testdata/order_by_union/mysql/go/query.sql.go b/internal/endtoend/testdata/order_by_union/mysql/go/query.sql.go new file mode 100644 index 0000000000..df39af738e --- /dev/null +++ b/internal/endtoend/testdata/order_by_union/mysql/go/query.sql.go @@ -0,0 +1,40 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.19.0 +// source: query.sql + +package querytest + +import ( + "context" +) + +const listAuthorsUnion = `-- name: ListAuthorsUnion :many +SELECT name as foo FROM authors +UNION +SELECT first_name as foo FROM people +ORDER BY foo +` + +func (q *Queries) ListAuthorsUnion(ctx context.Context) ([]string, error) { + rows, err := q.db.QueryContext(ctx, listAuthorsUnion) + if err != nil { + return nil, err + } + defer rows.Close() + var items []string + for rows.Next() { + var foo string + if err := rows.Scan(&foo); err != nil { + return nil, err + } + items = append(items, foo) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/order_by_union/mysql/query.sql b/internal/endtoend/testdata/order_by_union/mysql/query.sql new file mode 100644 index 0000000000..e779d457fa --- /dev/null +++ b/internal/endtoend/testdata/order_by_union/mysql/query.sql @@ -0,0 +1,14 @@ +CREATE TABLE authors ( + name text NOT NULL, + bio text +); + +CREATE TABLE people ( + first_name text NOT NULL +); + +-- name: ListAuthorsUnion :many +SELECT name as foo FROM authors +UNION +SELECT first_name as foo FROM people +ORDER BY foo; diff --git a/internal/endtoend/testdata/order_by_union/mysql/sqlc.json b/internal/endtoend/testdata/order_by_union/mysql/sqlc.json new file mode 100644 index 0000000000..534b7e24e9 --- /dev/null +++ b/internal/endtoend/testdata/order_by_union/mysql/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "path": "go", + "engine": "mysql", + "name": "querytest", + "schema": "query.sql", + "queries": "query.sql" + } + ] +} diff --git a/internal/endtoend/testdata/order_by_union/postgresql/go/db.go b/internal/endtoend/testdata/order_by_union/postgresql/go/db.go new file mode 100644 index 0000000000..ec5e95c4ab --- /dev/null +++ b/internal/endtoend/testdata/order_by_union/postgresql/go/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.19.0 + +package querytest + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/order_by_union/postgresql/go/models.go b/internal/endtoend/testdata/order_by_union/postgresql/go/models.go new file mode 100644 index 0000000000..abd3bc7e22 --- /dev/null +++ b/internal/endtoend/testdata/order_by_union/postgresql/go/models.go @@ -0,0 +1,19 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.19.0 + +package querytest + +import ( + "database/sql" +) + +type Author struct { + ID int64 + Name string + Bio sql.NullString +} + +type Person struct { + FirstName string +} diff --git a/internal/endtoend/testdata/order_by_union/postgresql/go/query.sql.go b/internal/endtoend/testdata/order_by_union/postgresql/go/query.sql.go new file mode 100644 index 0000000000..df39af738e --- /dev/null +++ b/internal/endtoend/testdata/order_by_union/postgresql/go/query.sql.go @@ -0,0 +1,40 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.19.0 +// source: query.sql + +package querytest + +import ( + "context" +) + +const listAuthorsUnion = `-- name: ListAuthorsUnion :many +SELECT name as foo FROM authors +UNION +SELECT first_name as foo FROM people +ORDER BY foo +` + +func (q *Queries) ListAuthorsUnion(ctx context.Context) ([]string, error) { + rows, err := q.db.QueryContext(ctx, listAuthorsUnion) + if err != nil { + return nil, err + } + defer rows.Close() + var items []string + for rows.Next() { + var foo string + if err := rows.Scan(&foo); err != nil { + return nil, err + } + items = append(items, foo) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/order_by_union/postgresql/query.sql b/internal/endtoend/testdata/order_by_union/postgresql/query.sql new file mode 100644 index 0000000000..f723929976 --- /dev/null +++ b/internal/endtoend/testdata/order_by_union/postgresql/query.sql @@ -0,0 +1,15 @@ +CREATE TABLE authors ( + id BIGSERIAL PRIMARY KEY, + name text NOT NULL, + bio text +); + +CREATE TABLE people ( + first_name text NOT NULL +); + +-- name: ListAuthorsUnion :many +SELECT name as foo FROM authors +UNION +SELECT first_name as foo FROM people +ORDER BY foo; diff --git a/internal/endtoend/testdata/order_by_union/postgresql/sqlc.json b/internal/endtoend/testdata/order_by_union/postgresql/sqlc.json new file mode 100644 index 0000000000..af57681f66 --- /dev/null +++ b/internal/endtoend/testdata/order_by_union/postgresql/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "path": "go", + "engine": "postgresql", + "name": "querytest", + "schema": "query.sql", + "queries": "query.sql" + } + ] +}