Skip to content

Commit 6fdb7e1

Browse files
zelchZephaniah E. Loss-Cutler-Hull
and
Zephaniah E. Loss-Cutler-Hull
authored
Add support for mysql CTEs. (#1188)
* Update to most current github.com/pingcap/parser This is needed as CTE support is fairly new for the parser. * Handle multiple renames for mysql. pcast.RenameTableStmt was modified in the current parser to only have the list of renames, instead of both a single set of data for renames and the list. As such we need to turn that into an ast.List of ast.RenameTableStmt. * Handle ast.List in Catalog.Update. The mysql code now uses an ast.List for all rename statements, however Catalog.Update ignores ast.List items instead of walking through them, this results in the renames getting lost. This change causes us to go through each list, assuming that they may contain statements. * Add functions to handle With statements for mysql. These will be used by the different types that have With objects. * Handle With for various types. This should give us full support for CTEs (recursive and not) for mysql. All types that parser has With entries for, and which we currently support, are handled here. * Add mysql CTE test cases. This just copies the stdlib test cases to mysql, sets the engine, and switches to mysql style parameters. Otherwise, no changes to the tests or the expected test output. Co-authored-by: Zephaniah E. Loss-Cutler-Hull <warp@aehallh.com>
1 parent 846c629 commit 6fdb7e1

File tree

19 files changed

+445
-4
lines changed

19 files changed

+445
-4
lines changed

go.mod

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,33 @@ go 1.16
44

55
require (
66
github.com/antlr/antlr4 v0.0.0-20200209180723-1177c0b58d07
7+
github.com/cznic/golex v0.0.0-20181122101858-9c343928389c // indirect
8+
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
9+
github.com/cznic/parser v0.0.0-20160622100904-31edd927e5b1 // indirect
10+
github.com/cznic/sortutil v0.0.0-20181122101858-f5f958428db8 // indirect
11+
github.com/cznic/strutil v0.0.0-20171016134553-529a34b1c186 // indirect
12+
github.com/cznic/y v0.0.0-20170802143616-045f81c6662a // indirect
713
github.com/davecgh/go-spew v1.1.1
814
github.com/go-sql-driver/mysql v1.6.0
915
github.com/google/go-cmp v0.5.6
1016
github.com/jackc/pgx/v4 v4.13.0
1117
github.com/jinzhu/inflection v1.0.0
18+
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
1219
github.com/kr/pretty v0.2.1 // indirect
1320
github.com/lib/pq v1.10.3
1421
github.com/pganalyze/pg_query_go/v2 v2.0.5
15-
github.com/pingcap/parser v0.0.0-20201024025010-3b2fb4b41d73
22+
github.com/pingcap/log v0.0.0-20210906054005-afc726e70354 // indirect
23+
github.com/pingcap/parser v0.0.0-20210914110036-002913dd28ec
24+
github.com/pingcap/tipb v0.0.0-20210830034902-3d2699ad59b9 // indirect
25+
github.com/pkg/errors v0.9.1 // indirect
26+
github.com/sirupsen/logrus v1.8.1 // indirect
1627
github.com/spf13/cobra v1.2.1
1728
github.com/spf13/pflag v1.0.5
29+
go.uber.org/zap v1.19.1 // indirect
30+
golang.org/x/net v0.0.0-20210913180222-943fd674d43e // indirect
31+
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 // indirect
32+
golang.org/x/text v0.3.7 // indirect
33+
google.golang.org/genproto v0.0.0-20210909211513-a8c4777a87af // indirect
1834
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
1935
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
2036
)

go.sum

Lines changed: 52 additions & 0 deletions
Large diffs are not rendered by default.

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

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

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

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

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

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE bar (ready bool not null);
2+
3+
-- name: CTECount :many
4+
WITH all_count AS (
5+
SELECT count(*) FROM bar
6+
), ready_count AS (
7+
SELECT count(*) FROM bar WHERE ready = true
8+
)
9+
SELECT all_count.count, ready_count.count
10+
FROM all_count, ready_count;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "mysql",
6+
"path": "go",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

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

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

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

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

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

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TABLE bar (ready bool not null);
2+
3+
-- name: CTEFilter :many
4+
WITH filter_count AS (
5+
SELECT count(*) FROM bar WHERE ready = ?
6+
)
7+
SELECT filter_count.count
8+
FROM filter_count;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "mysql",
6+
"path": "go",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

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

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

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

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

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

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE TABLE bar (id INT NOT NULL, parent_id INT);
2+
3+
-- name: CTERecursive :many
4+
WITH RECURSIVE cte AS (
5+
SELECT b.* FROM bar AS b
6+
WHERE b.id = ?
7+
UNION ALL
8+
SELECT b.*
9+
FROM bar AS b, cte AS c
10+
WHERE b.parent_id = c.id
11+
) SELECT * FROM cte;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "mysql",
6+
"path": "go",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)