Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
上島
soar
提交
2a80b406
S
soar
项目概览
上島
/
soar
与 Fork 源项目一致
Fork自
Xiaomi / soar
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
soar
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2a80b406
编写于
8月 27, 2019
作者:
martianzhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
make SQL more compatible with pingcap/parser
上级
b2a3445f
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
64 addition
and
7 deletion
+64
-7
.travis.yml
.travis.yml
+1
-6
ast/tidb.go
ast/tidb.go
+34
-0
ast/tidb_test.go
ast/tidb_test.go
+28
-0
test/fixture/test_Run_all_test_cases.golden
test/fixture/test_Run_all_test_cases.golden
+1
-1
未找到文件。
.travis.yml
浏览文件 @
2a80b406
...
...
@@ -8,12 +8,7 @@ sudo: required
go_import_path
:
github.com/XiaoMi/soar
addons
:
apt
:
sources
:
-
mysql-5.7-trusty
packages
:
-
mysql-client
dist
:
xenial
services
:
-
docker
...
...
ast/tidb.go
浏览文件 @
2a80b406
...
...
@@ -18,6 +18,8 @@ package ast
import
(
"fmt"
"regexp"
"strings"
"github.com/XiaoMi/soar/common"
...
...
@@ -34,6 +36,7 @@ import (
// TiParse TiDB 语法解析
func
TiParse
(
sql
,
charset
,
collation
string
)
([]
ast
.
StmtNode
,
error
)
{
p
:=
parser
.
New
()
sql
=
removeIncompatibleWords
(
sql
)
stmt
,
warn
,
err
:=
p
.
Parse
(
sql
,
charset
,
collation
)
// TODO: bypass warning info
for
_
,
w
:=
range
warn
{
...
...
@@ -42,6 +45,37 @@ func TiParse(sql, charset, collation string) ([]ast.StmtNode, error) {
return
stmt
,
err
}
// removeIncompatibleWords remove pingcap/parser not support words from schema
func
removeIncompatibleWords
(
sql
string
)
string
{
fields
:=
strings
.
Fields
(
strings
.
TrimSpace
(
sql
))
if
len
(
fields
)
==
0
{
return
sql
}
switch
strings
.
ToLower
(
fields
[
0
])
{
case
"create"
,
"alter"
:
default
:
return
sql
}
// CONSTRAINT col_fk FOREIGN KEY (col) REFERENCES tb (id) ON UPDATE CASCADE
re
:=
regexp
.
MustCompile
(
`(?i) ON UPDATE CASCADE`
)
sql
=
re
.
ReplaceAllString
(
sql
,
""
)
// FULLTEXT KEY col_fk (col) /*!50100 WITH PARSER `ngram` */
// /*!50100 PARTITION BY LIST (col)
re
=
regexp
.
MustCompile
(
`/\*!5`
)
sql
=
re
.
ReplaceAllString
(
sql
,
"/* 5"
)
// col varchar(10) CHARACTER SET gbk DEFAULT NULL
re
=
regexp
.
MustCompile
(
`(?i)CHARACTER SET [a-z_0-9]* `
)
sql
=
re
.
ReplaceAllString
(
sql
,
""
)
// CREATE TEMPORARY TABLE IF NOT EXISTS t_film AS (SELECT * FROM film);
re
=
regexp
.
MustCompile
(
`(?i)CREATE TEMPORARY TABLE`
)
sql
=
re
.
ReplaceAllString
(
sql
,
"CREATE TABLE"
)
return
sql
}
// PrintPrettyStmtNode 打印TiParse语法树
func
PrintPrettyStmtNode
(
sql
,
charset
,
collation
string
)
{
tree
,
err
:=
TiParse
(
sql
,
charset
,
collation
)
...
...
ast/tidb_test.go
浏览文件 @
2a80b406
...
...
@@ -86,3 +86,31 @@ func TestSchemaMetaInfo(t *testing.T) {
}
common
.
Log
.
Debug
(
"Exiting function: %s"
,
common
.
GetFunctionName
())
}
func
TestRemoveIncompatibleWords
(
t
*
testing
.
T
)
{
common
.
Log
.
Debug
(
"Entering function: %s"
,
common
.
GetFunctionName
())
sqls
:=
[][]
string
{
{
`CREATE TEMPORARY TABLE IF NOT EXISTS t_film AS (SELECT * FROM film)`
,
`CREATE CONSTRAINT col_fk FOREIGN KEY (col) REFERENCES tb (id) ON UPDATE CASCADE`
,
"CREATE FULLTEXT KEY col_fk (col) /*!50100 WITH PARSER `ngram` */"
,
`CREATE /*!50100 PARTITION BY LIST (col)`
,
`CREATE col varchar(10) CHARACTER SET gbk DEFAULT NULL`
,
},
{
`CREATE TABLE IF NOT EXISTS t_film AS (SELECT * FROM film)`
,
`CREATE CONSTRAINT col_fk FOREIGN KEY (col) REFERENCES tb (id)`
,
"CREATE FULLTEXT KEY col_fk (col) /* 50100 WITH PARSER `ngram` */"
,
`CREATE /* 50100 PARTITION BY LIST (col)`
,
`CREATE col varchar(10) DEFAULT NULL`
,
},
}
for
k
,
sql
:=
range
sqls
[
0
]
{
sql
=
removeIncompatibleWords
(
sql
)
if
sqls
[
1
][
k
]
!=
sql
{
fmt
.
Println
(
sql
)
t
.
Fatal
(
sql
)
}
}
common
.
Log
.
Debug
(
"Exiting function: %s"
,
common
.
GetFunctionName
())
}
test/fixture/test_Run_all_test_cases.golden
浏览文件 @
2a80b406
...
...
@@ -3861,7 +3861,7 @@ GROUP BY
| id | select\_type | table | partitions | type | possible_keys | key | key\_len | ref | rows | filtered | scalability | Extra |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | SIMPLE | *actor* | NULL | ref | idx\_actor\_last\_name | idx\_actor\_last\_name | 137 | const | 2 |
33.33
% | O(log n) | Using where; Using temporary |
| 1 | SIMPLE | *actor* | NULL | ref | idx\_actor\_last\_name | idx\_actor\_last\_name | 137 | const | 2 |
16.67
% | O(log n) | Using where; Using temporary |
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录