Skip to content

Commit

Permalink
fix XiaoMi#221 build pass with test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
martianzhang committed Aug 28, 2019
1 parent d50ad5b commit fb13892
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 31 deletions.
53 changes: 32 additions & 21 deletions ast/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/pingcap/parser/ast"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -1691,17 +1692,39 @@ func MergeAlterTables(sqls ...string) map[string]string {

for _, sql := range sqls {
sql = strings.Trim(sql, common.Config.Delimiter)
stmt, _ := sqlparser.Parse(sql)
stmts, err := TiParse(sql, "", "")
if err != nil {
common.Log.Warn(err.Error())
continue
}
// stmt, _ := sqlparser.Parse(sql)
alterSQL := ""
dbName := ""
tableName := ""
switch n := stmt.(type) {
case *sqlparser.DDL:
// 注意: 表名和库名不区分大小写
tableName = strings.ToLower(n.Table.Name.String())
dbName = strings.ToLower(n.Table.Qualifier.String())
switch n.Action {
case "rename":
for _, stmt := range stmts {
switch n := stmt.(type) {
case *ast.AlterTableStmt:
// 注意: 表名和库名不区分大小写
tableName = n.Table.Name.L
dbName = n.Table.Schema.L
if alterExp.MatchString(sql) {
common.Log.Debug("rename alterExp: ALTER %v %v", tableName, alterExp.ReplaceAllString(sql, ""))
alterSQL = fmt.Sprint(alterExp.ReplaceAllString(sql, ""))
}
case *ast.CreateIndexStmt:
tableName = n.Table.Name.L
dbName = n.Table.Schema.L

buf := createIndexExp.ReplaceAllString(sql, "")
idxName := strings.TrimSpace(indexNameExp.FindString(buf))
buf = string([]byte(buf)[strings.Index(buf, "("):])
common.Log.Error(buf)
common.Log.Debug("alter createIndexExp: ALTER %v ADD INDEX %v %v", tableName, "ADD INDEX", idxName, buf)
alterSQL = fmt.Sprint("ADD INDEX", " "+idxName+" ", buf)
case *ast.RenameTableStmt:
// 注意: 表名和库名不区分大小写
tableName = n.OldTable.Name.L
dbName = n.OldTable.Schema.L
if alterExp.MatchString(sql) {
common.Log.Debug("rename alterExp: ALTER %v %v", tableName, alterExp.ReplaceAllString(sql, ""))
alterSQL = fmt.Sprint(alterExp.ReplaceAllString(sql, ""))
Expand All @@ -1711,22 +1734,10 @@ func MergeAlterTables(sqls ...string) map[string]string {
} else {
common.Log.Warn("rename not match: ALTER %v %v", tableName, sql)
}
case "alter":
if alterExp.MatchString(sql) {
common.Log.Debug("rename alterExp: ALTER %v %v", tableName, alterExp.ReplaceAllString(sql, ""))
alterSQL = fmt.Sprint(alterExp.ReplaceAllString(sql, ""))
} else if createIndexExp.MatchString(sql) {
buf := createIndexExp.ReplaceAllString(sql, "")
idxName := strings.TrimSpace(indexNameExp.FindString(buf))
buf = string([]byte(buf)[strings.Index(buf, "("):])
common.Log.Error(buf)
common.Log.Debug("alter createIndexExp: ALTER %v ADD INDEX %v %v", tableName, "ADD INDEX", idxName, buf)
alterSQL = fmt.Sprint("ADD INDEX", " "+idxName+" ", buf)
}
default:

}
}

if alterSQL != "" && tableName != "" && tableName != "dual" {
if dbName == "" {
alterSQLs["`"+tableName+"`"] = append(alterSQLs["`"+tableName+"`"], alterSQL)
Expand Down
2 changes: 1 addition & 1 deletion ast/rewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ func TestMergeAlterTables(t *testing.T) {

// table name quote in back ticks
"alter table `t3`add index `idx_a`(a)",
"alter table`t3`drop index`idx_b`(b)",
"alter table`t3`drop index`idx_b`",
}

alterSQLs := MergeAlterTables(sqls...)
Expand Down
8 changes: 6 additions & 2 deletions ast/testdata/TestMergeAlterTables.golden
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
`customer` : ALTER TABLE `customer` ADD INDEX part_of_name (name(10)) ;

`db`.`old_table` : ALTER TABLE `db`.`old_table` RENAME new_table ;

`old_table` : ALTER TABLE `old_table` RENAME TO new_table, RENAME AS new_table ;

`sakila`.`t1` : ALTER TABLE `sakila`.`t1` add index `idx_col`(`col`), add UNIQUE index `idx_col`(`col`), add index `idx_ID`(`ID`) ;

`t1` : ALTER TABLE `t1` RENAME INDEX idx_a TO idx_b, RENAME KEY idx_a TO idx_b, MODIFY col1 BIGINT UNSIGNED DEFAULT 1 COMMENT 'my column', CHANGE b a INT NOT NULL ;
`t1` : ALTER TABLE `t1` RENAME COLUMN a TO b, RENAME INDEX idx_a TO idx_b, RENAME KEY idx_a TO idx_b, MODIFY col1 BIGINT UNSIGNED DEFAULT 1 COMMENT 'my column', CHANGE b a INT NOT NULL ;

`t2` : ALTER TABLE `t2` DROP COLUMN c, DROP COLUMN d, ADD COLUMN C int, ADD COLUMN D int FIRST, ADD COLUMN E int AFTER D ;

`t3` : ALTER TABLE `t3` add index `idx_a`(a), drop index`idx_b`(b) ;
`t3` : ALTER TABLE `t3` add index `idx_a`(a), drop index`idx_b` ;

`test_bb` : ALTER TABLE `test_bb` ADD INDEX idx_test_cca (test_cc) ;

15 changes: 9 additions & 6 deletions ast/testdata/TestPrintPrettyStmtNode.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
},
resultSetNode: ast.resultSetNode{},
SelectStmtOpts: &ast.SelectStmtOpts{
Distinct: false,
SQLCache: true,
CalcFoundRows: false,
StraightJoin: false,
Priority: 0,
TableHints: nil,
Distinct: false,
SQLBigResult: false,
SQLBufferResult: false,
SQLCache: true,
SQLSmallResult: false,
CalcFoundRows: false,
StraightJoin: false,
Priority: 0,
TableHints: nil,
},
Distinct: false,
From: (*ast.TableRefsClause)(nil),
Expand Down
3 changes: 3 additions & 0 deletions ast/testdata/TestStmtNode2JSON.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
{
"text": "select 1",
"resultFields": null,
"SQLBigResult": false,
"SQLBufferResult": false,
"SQLCache": true,
"SQLSmallResult": false,
"CalcFoundRows": false,
"StraightJoin": false,
"Priority": 0,
Expand Down
2 changes: 1 addition & 1 deletion ast/testdata/TestTokenizer.golden
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
}
[]ast.Token{
{Type:57348, Val:"select", i:0},
{Type:57593, Val:"sql_calc_found_rows", i:0},
{Type:57588, Val:"sql_calc_found_rows", i:0},
{Type:57396, Val:"col", i:0},
{Type:57353, Val:"from", i:0},
{Type:57396, Val:"tbl", i:0},
Expand Down

0 comments on commit fb13892

Please sign in to comment.