From 296db5d14262e0dfabd66ac03b925d7d902c6ec3 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Thu, 12 Oct 2023 16:20:46 -0700 Subject: [PATCH] fix(dolphin): Support more UNIONs for MySQL --- go.mod | 2 +- go.sum | 2 + .../testdata/select_union_subquery/issue.md | 1 + .../select_union_subquery/mysql/go/db.go | 31 ++++++++++++++ .../select_union_subquery/mysql/go/models.go | 15 +++++++ .../mysql/go/query.sql.go | 41 +++++++++++++++++++ .../select_union_subquery/mysql/query.sql | 6 +++ .../select_union_subquery/mysql/schema.sql | 5 +++ .../select_union_subquery/mysql/sqlc.yaml | 9 ++++ .../select_union_subquery/postgresql/go/db.go | 32 +++++++++++++++ .../postgresql/go/models.go | 15 +++++++ .../postgresql/go/query.sql.go | 38 +++++++++++++++++ .../postgresql/query.sql | 6 +++ .../postgresql/schema.sql | 5 +++ .../postgresql/sqlc.yaml | 10 +++++ internal/engine/dolphin/convert.go | 3 +- 16 files changed, 218 insertions(+), 3 deletions(-) create mode 100644 internal/endtoend/testdata/select_union_subquery/issue.md create mode 100644 internal/endtoend/testdata/select_union_subquery/mysql/go/db.go create mode 100644 internal/endtoend/testdata/select_union_subquery/mysql/go/models.go create mode 100644 internal/endtoend/testdata/select_union_subquery/mysql/go/query.sql.go create mode 100644 internal/endtoend/testdata/select_union_subquery/mysql/query.sql create mode 100644 internal/endtoend/testdata/select_union_subquery/mysql/schema.sql create mode 100644 internal/endtoend/testdata/select_union_subquery/mysql/sqlc.yaml create mode 100644 internal/endtoend/testdata/select_union_subquery/postgresql/go/db.go create mode 100644 internal/endtoend/testdata/select_union_subquery/postgresql/go/models.go create mode 100644 internal/endtoend/testdata/select_union_subquery/postgresql/go/query.sql.go create mode 100644 internal/endtoend/testdata/select_union_subquery/postgresql/query.sql create mode 100644 internal/endtoend/testdata/select_union_subquery/postgresql/schema.sql create mode 100644 internal/endtoend/testdata/select_union_subquery/postgresql/sqlc.yaml diff --git a/go.mod b/go.mod index a040a4edb8..05edd4d2d9 100644 --- a/go.mod +++ b/go.mod @@ -50,7 +50,7 @@ require ( github.com/jackc/pgtype v1.14.0 github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63 // indirect github.com/pingcap/log v1.1.0 // indirect - github.com/pingcap/tidb/parser v0.0.0-20230815160630-b69fa21942d1 + github.com/pingcap/tidb/parser v0.0.0-20231010133155-38cb4f3312be github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/rogpeppe/go-internal v1.10.0 // indirect github.com/stoewer/go-strcase v1.2.0 // indirect diff --git a/go.sum b/go.sum index e8f9fb10c2..f780baa8dd 100644 --- a/go.sum +++ b/go.sum @@ -142,6 +142,8 @@ github.com/pingcap/log v1.1.0 h1:ELiPxACz7vdo1qAvvaWJg1NrYFoY6gqAh/+Uo6aXdD8= github.com/pingcap/log v1.1.0/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4= github.com/pingcap/tidb/parser v0.0.0-20230815160630-b69fa21942d1 h1:FvX5fDJ32eblK9f6KJIOVujjtu3FDEfcRGHsEDfJ4kI= github.com/pingcap/tidb/parser v0.0.0-20230815160630-b69fa21942d1/go.mod h1:pWA6mNa/o7UTDKrg+4H75NdpRgpWRTox/cqQjaQ4ZBU= +github.com/pingcap/tidb/parser v0.0.0-20231010133155-38cb4f3312be h1:4HUBkIZs+4j+tbXGm5/B0yjB66OTz218HDKA6VrhO7U= +github.com/pingcap/tidb/parser v0.0.0-20231010133155-38cb4f3312be/go.mod h1:cwq4bKUlftpWuznB+rqNwbN0xy6/i5SL/nYvEKeJn4s= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/internal/endtoend/testdata/select_union_subquery/issue.md b/internal/endtoend/testdata/select_union_subquery/issue.md new file mode 100644 index 0000000000..138725aa27 --- /dev/null +++ b/internal/endtoend/testdata/select_union_subquery/issue.md @@ -0,0 +1 @@ +https://github.com/sqlc-dev/sqlc/issues/2260 diff --git a/internal/endtoend/testdata/select_union_subquery/mysql/go/db.go b/internal/endtoend/testdata/select_union_subquery/mysql/go/db.go new file mode 100644 index 0000000000..a457fb76b2 --- /dev/null +++ b/internal/endtoend/testdata/select_union_subquery/mysql/go/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.22.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/select_union_subquery/mysql/go/models.go b/internal/endtoend/testdata/select_union_subquery/mysql/go/models.go new file mode 100644 index 0000000000..f5cf23b517 --- /dev/null +++ b/internal/endtoend/testdata/select_union_subquery/mysql/go/models.go @@ -0,0 +1,15 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.22.0 + +package querytest + +import ( + "database/sql" +) + +type Author struct { + ID int32 + Name string + Bio sql.NullString +} diff --git a/internal/endtoend/testdata/select_union_subquery/mysql/go/query.sql.go b/internal/endtoend/testdata/select_union_subquery/mysql/go/query.sql.go new file mode 100644 index 0000000000..198565e2a0 --- /dev/null +++ b/internal/endtoend/testdata/select_union_subquery/mysql/go/query.sql.go @@ -0,0 +1,41 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.22.0 +// source: query.sql + +package querytest + +import ( + "context" +) + +const testSubqueryUnion = `-- name: TestSubqueryUnion :many +SELECT tmp.id, tmp.name, tmp.bio FROM ( + SELECT id, name, bio FROM authors + UNION + SELECT id, name, bio FROM authors +) tmp LIMIT 5 +` + +func (q *Queries) TestSubqueryUnion(ctx context.Context) ([]Author, error) { + rows, err := q.db.QueryContext(ctx, testSubqueryUnion) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Author + for rows.Next() { + var i Author + if err := rows.Scan(&i.ID, &i.Name, &i.Bio); err != nil { + return nil, err + } + items = append(items, i) + } + 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/select_union_subquery/mysql/query.sql b/internal/endtoend/testdata/select_union_subquery/mysql/query.sql new file mode 100644 index 0000000000..6eaabdcf49 --- /dev/null +++ b/internal/endtoend/testdata/select_union_subquery/mysql/query.sql @@ -0,0 +1,6 @@ +-- name: TestSubqueryUnion :many +SELECT tmp.* FROM ( + SELECT * FROM authors + UNION + SELECT * FROM authors +) tmp LIMIT 5; diff --git a/internal/endtoend/testdata/select_union_subquery/mysql/schema.sql b/internal/endtoend/testdata/select_union_subquery/mysql/schema.sql new file mode 100644 index 0000000000..7793468f4e --- /dev/null +++ b/internal/endtoend/testdata/select_union_subquery/mysql/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE authors ( + id int PRIMARY KEY, + name text NOT NULL, + bio text +); diff --git a/internal/endtoend/testdata/select_union_subquery/mysql/sqlc.yaml b/internal/endtoend/testdata/select_union_subquery/mysql/sqlc.yaml new file mode 100644 index 0000000000..b843ef55e3 --- /dev/null +++ b/internal/endtoend/testdata/select_union_subquery/mysql/sqlc.yaml @@ -0,0 +1,9 @@ +version: "2" +sql: + - engine: "mysql" + schema: "schema.sql" + queries: "query.sql" + gen: + go: + package: "querytest" + out: "go" diff --git a/internal/endtoend/testdata/select_union_subquery/postgresql/go/db.go b/internal/endtoend/testdata/select_union_subquery/postgresql/go/db.go new file mode 100644 index 0000000000..8a010ccc48 --- /dev/null +++ b/internal/endtoend/testdata/select_union_subquery/postgresql/go/db.go @@ -0,0 +1,32 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.22.0 + +package querytest + +import ( + "context" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" +) + +type DBTX interface { + Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) + Query(context.Context, string, ...interface{}) (pgx.Rows, error) + QueryRow(context.Context, string, ...interface{}) pgx.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx pgx.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/select_union_subquery/postgresql/go/models.go b/internal/endtoend/testdata/select_union_subquery/postgresql/go/models.go new file mode 100644 index 0000000000..4338b41c91 --- /dev/null +++ b/internal/endtoend/testdata/select_union_subquery/postgresql/go/models.go @@ -0,0 +1,15 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.22.0 + +package querytest + +import ( + "github.com/jackc/pgx/v5/pgtype" +) + +type Author struct { + ID int32 + Name string + Bio pgtype.Text +} diff --git a/internal/endtoend/testdata/select_union_subquery/postgresql/go/query.sql.go b/internal/endtoend/testdata/select_union_subquery/postgresql/go/query.sql.go new file mode 100644 index 0000000000..5e3335c0d6 --- /dev/null +++ b/internal/endtoend/testdata/select_union_subquery/postgresql/go/query.sql.go @@ -0,0 +1,38 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.22.0 +// source: query.sql + +package querytest + +import ( + "context" +) + +const testSubqueryUnion = `-- name: TestSubqueryUnion :many +SELECT tmp.id, tmp.name, tmp.bio FROM ( + SELECT id, name, bio FROM authors + UNION + SELECT id, name, bio FROM authors +) tmp LIMIT 5 +` + +func (q *Queries) TestSubqueryUnion(ctx context.Context) ([]Author, error) { + rows, err := q.db.Query(ctx, testSubqueryUnion) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Author + for rows.Next() { + var i Author + if err := rows.Scan(&i.ID, &i.Name, &i.Bio); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/select_union_subquery/postgresql/query.sql b/internal/endtoend/testdata/select_union_subquery/postgresql/query.sql new file mode 100644 index 0000000000..6eaabdcf49 --- /dev/null +++ b/internal/endtoend/testdata/select_union_subquery/postgresql/query.sql @@ -0,0 +1,6 @@ +-- name: TestSubqueryUnion :many +SELECT tmp.* FROM ( + SELECT * FROM authors + UNION + SELECT * FROM authors +) tmp LIMIT 5; diff --git a/internal/endtoend/testdata/select_union_subquery/postgresql/schema.sql b/internal/endtoend/testdata/select_union_subquery/postgresql/schema.sql new file mode 100644 index 0000000000..7793468f4e --- /dev/null +++ b/internal/endtoend/testdata/select_union_subquery/postgresql/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE authors ( + id int PRIMARY KEY, + name text NOT NULL, + bio text +); diff --git a/internal/endtoend/testdata/select_union_subquery/postgresql/sqlc.yaml b/internal/endtoend/testdata/select_union_subquery/postgresql/sqlc.yaml new file mode 100644 index 0000000000..5dc63e3f91 --- /dev/null +++ b/internal/endtoend/testdata/select_union_subquery/postgresql/sqlc.yaml @@ -0,0 +1,10 @@ +version: "2" +sql: + - engine: "postgresql" + schema: "schema.sql" + queries: "query.sql" + gen: + go: + package: "querytest" + out: "go" + sql_package: "pgx/v5" diff --git a/internal/engine/dolphin/convert.go b/internal/engine/dolphin/convert.go index 5cca536976..b16e0a2a39 100644 --- a/internal/engine/dolphin/convert.go +++ b/internal/engine/dolphin/convert.go @@ -419,7 +419,6 @@ func (c *cc) convertInsertStmt(n *pcast.InsertStmt) *ast.InsertStmt { panic("expected range var") } - // debug.Dump(n) insert := &ast.InsertStmt{ Relation: rangeVar, Cols: c.convertColumnNames(n.Columns), @@ -1348,7 +1347,7 @@ func (c *cc) convertTableSource(node *pcast.TableSource) ast.Node { alias := node.AsName.String() switch n := node.Source.(type) { - case *pcast.SelectStmt: + case *pcast.SelectStmt, *pcast.SetOprStmt: rs := &ast.RangeSubselect{ Subquery: c.convert(n), }