Skip to content

Commit e44f238

Browse files
authored
fix(dolphin): Support more UNIONs for MySQL (#2843)
1 parent 27e47b9 commit e44f238

File tree

16 files changed

+218
-3
lines changed

16 files changed

+218
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ require (
5050
github.com/jackc/pgtype v1.14.0
5151
github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63 // indirect
5252
github.com/pingcap/log v1.1.0 // indirect
53-
github.com/pingcap/tidb/parser v0.0.0-20230815160630-b69fa21942d1
53+
github.com/pingcap/tidb/parser v0.0.0-20231010133155-38cb4f3312be
5454
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
5555
github.com/rogpeppe/go-internal v1.10.0 // indirect
5656
github.com/stoewer/go-strcase v1.2.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ github.com/pingcap/log v1.1.0 h1:ELiPxACz7vdo1qAvvaWJg1NrYFoY6gqAh/+Uo6aXdD8=
142142
github.com/pingcap/log v1.1.0/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
143143
github.com/pingcap/tidb/parser v0.0.0-20230815160630-b69fa21942d1 h1:FvX5fDJ32eblK9f6KJIOVujjtu3FDEfcRGHsEDfJ4kI=
144144
github.com/pingcap/tidb/parser v0.0.0-20230815160630-b69fa21942d1/go.mod h1:pWA6mNa/o7UTDKrg+4H75NdpRgpWRTox/cqQjaQ4ZBU=
145+
github.com/pingcap/tidb/parser v0.0.0-20231010133155-38cb4f3312be h1:4HUBkIZs+4j+tbXGm5/B0yjB66OTz218HDKA6VrhO7U=
146+
github.com/pingcap/tidb/parser v0.0.0-20231010133155-38cb4f3312be/go.mod h1:cwq4bKUlftpWuznB+rqNwbN0xy6/i5SL/nYvEKeJn4s=
145147
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
146148
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
147149
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/sqlc-dev/sqlc/issues/2260

internal/endtoend/testdata/select_union_subquery/mysql/go/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/select_union_subquery/mysql/go/models.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/select_union_subquery/mysql/go/query.sql.go

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- name: TestSubqueryUnion :many
2+
SELECT tmp.* FROM (
3+
SELECT * FROM authors
4+
UNION
5+
SELECT * FROM authors
6+
) tmp LIMIT 5;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE authors (
2+
id int PRIMARY KEY,
3+
name text NOT NULL,
4+
bio text
5+
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: "2"
2+
sql:
3+
- engine: "mysql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"

internal/endtoend/testdata/select_union_subquery/postgresql/go/db.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/select_union_subquery/postgresql/go/models.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/select_union_subquery/postgresql/go/query.sql.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- name: TestSubqueryUnion :many
2+
SELECT tmp.* FROM (
3+
SELECT * FROM authors
4+
UNION
5+
SELECT * FROM authors
6+
) tmp LIMIT 5;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE authors (
2+
id int PRIMARY KEY,
3+
name text NOT NULL,
4+
bio text
5+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "postgresql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "pgx/v5"

internal/engine/dolphin/convert.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,6 @@ func (c *cc) convertInsertStmt(n *pcast.InsertStmt) *ast.InsertStmt {
419419
panic("expected range var")
420420
}
421421

422-
// debug.Dump(n)
423422
insert := &ast.InsertStmt{
424423
Relation: rangeVar,
425424
Cols: c.convertColumnNames(n.Columns),
@@ -1348,7 +1347,7 @@ func (c *cc) convertTableSource(node *pcast.TableSource) ast.Node {
13481347
alias := node.AsName.String()
13491348
switch n := node.Source.(type) {
13501349

1351-
case *pcast.SelectStmt:
1350+
case *pcast.SelectStmt, *pcast.SetOprStmt:
13521351
rs := &ast.RangeSubselect{
13531352
Subquery: c.convert(n),
13541353
}

0 commit comments

Comments
 (0)