Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: use TiDB parser before MySQL parser (#6835)

rebeliceece848554054
diff --git a/backend/plugin/advisor/mysql/parser.go b/backend/plugin/advisor/mysql/parser.go
index 791fafa9fc..7ff5db46e4 100644
--- a/backend/plugin/advisor/mysql/parser.go
+++ b/backend/plugin/advisor/mysql/parser.go
@@ -4,8 +4,6 @@ import (
tidbparser "github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
- mysqlparser "github.com/bytebase/mysql-parser"
-
"github.com/bytebase/bytebase/backend/plugin/advisor"
parser "github.com/bytebase/bytebase/backend/plugin/parser/sql"
)
@@ -22,69 +20,72 @@ func newParser() *tidbparser.Parser {
}
func parseStatement(statement string, charset string, collation string) ([]ast.StmtNode, []advisor.Advice) {
- treeList, err := parser.ParseMySQL(statement)
+ list, err := parser.SplitMySQL(statement)
if err != nil {
- if syntaxErr, ok := err.(*parser.SyntaxError); ok {
- return nil, []advisor.Advice{
- {
- Status: advisor.Error,
- Code: advisor.StatementSyntaxError,
- Title: advisor.SyntaxErrorTitle,
- Content: syntaxErr.Message,
- Line: syntaxErr.Line,
- },
- }
- }
return nil, []advisor.Advice{
{
Status: advisor.Warn,
Code: advisor.Internal,
- Title: "Parse error",
+ Title: "Syntax error",
Content: err.Error(),
Line: 1,
},
}
}
- if len(treeList) == 0 {
- return nil, nil
- }
+ p := newParser()
var returnNodes []ast.StmtNode
var adviceList []advisor.Advice
- p := newParser()
- for _, item := range treeList {
- tree := item.Tree
- tokens := item.Tokens
- for _, child := range tree.GetChildren() {
- if child == nil {
- continue
- }
-
- if query, ok := child.(mysqlparser.IQueryContext); ok {
- text := tokens.GetTextFromRuleContext(query)
- lastLine := query.GetStop().GetLine() + item.BaseLine
- if nodes, _, err := p.Parse(text, charset, collation); err == nil {
- if len(nodes) != 1 {
- continue
+ for _, item := range list {
+ nodes, _, err := p.Parse(item.Text, charset, collation)
+ if err != nil {
+ // TiDB parser doesn't fully support MySQL syntax, so we need to use MySQL parser to parse the statement.
+ // But MySQL parser has some performance issue, so we only use it to parse the statement after TiDB parser failed.
+ if _, err := parser.ParseMySQL(item.Text); err != nil {
+ if syntaxErr, ok := err.(*parser.SyntaxError); ok {
+ return nil, []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: advisor.StatementSyntaxError,
+ Title: advisor.SyntaxErrorTitle,
+ Content: syntaxErr.Message,
+ Line: syntaxErr.Line,
+ },
}
- node := nodes[0]
- node.SetText(nil, text)
- node.SetOriginTextPosition(lastLine)
- if n, ok := node.(*ast.CreateTableStmt); ok {
- if err := parser.SetLineForMySQLCreateTableStmt(n); err != nil {
- return nil, append(adviceList, advisor.Advice{
- Status: advisor.Error,
- Code: advisor.Internal,
- Title: "Set line error",
- Content: err.Error(),
- Line: lastLine,
- })
- }
- }
- returnNodes = append(returnNodes, node)
}
+ return nil, []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.Internal,
+ Title: "Parse error",
+ Content: err.Error(),
+ Line: 1,
+ },
+ }
+ }
+ // If MySQL parser can parse the statement, but TiDB parser can't, we just ignore the statement.
+ continue
+ }
+
+ if len(nodes) != 1 {
+ continue
+ }
+
+ node := nodes[0]
+ node.SetText(nil, item.Text)
+ node.SetOriginTextPosition(item.LastLine)
+ if n, ok := node.(*ast.CreateTableStmt); ok {
+ if err := parser.SetLineForMySQLCreateTableStmt(n); err != nil {
+ return nil, append(adviceList, advisor.Advice{
+ Status: advisor.Error,
+ Code: advisor.Internal,
+ Title: "Set line error",
+ Content: err.Error(),
+ Line: item.LastLine,
+ })
}
}
+ returnNodes = append(returnNodes, node)
}
return returnNodes, adviceList
diff --git a/backend/plugin/advisor/mysql/test/column_disallow_change.yaml b/backend/plugin/advisor/mysql/test/column_disallow_change.yaml
index b25f8ce70a..48b6696a3c 100644
--- a/backend/plugin/advisor/mysql/test/column_disallow_change.yaml
+++ b/backend/plugin/advisor/mysql/test/column_disallow_change.yaml
@@ -16,7 +16,8 @@
code: 406
title: column.disallow-change
content: |-
- "ALTER TABLE t CHANGE COLUMN b a int
+ "
+ ALTER TABLE t CHANGE COLUMN b a int
;" contains CHANGE COLUMN statement
line: 3
details: ""
diff --git a/backend/plugin/advisor/mysql/test/column_disallow_changing_order.yaml b/backend/plugin/advisor/mysql/test/column_disallow_changing_order.yaml
index 8d7fd61c6e..d600fbc3ca 100644
--- a/backend/plugin/advisor/mysql/test/column_disallow_changing_order.yaml
+++ b/backend/plugin/advisor/mysql/test/column_disallow_changing_order.yaml
@@ -16,7 +16,8 @@
code: 407
title: column.disallow-changing-order
content: |-
- "ALTER TABLE t MODIFY COLUMN a int FIRST
+ "
+ ALTER TABLE t MODIFY COLUMN a int FIRST
;" changes column order
line: 3
details: ""
@@ -28,7 +29,8 @@
code: 407
title: column.disallow-changing-order
content: |-
- "ALTER TABLE t CHANGE COLUMN a1 a int FIRST
+ "
+ ALTER TABLE t CHANGE COLUMN a1 a int FIRST
;" changes column order
line: 3
details: ""
@@ -40,7 +42,8 @@
code: 407
title: column.disallow-changing-order
content: |-
- "ALTER TABLE t MODIFY COLUMN a int AFTER b
+ "
+ ALTER TABLE t MODIFY COLUMN a int AFTER b
;" changes column order
line: 3
details: ""
@@ -52,7 +55,8 @@
code: 407
title: column.disallow-changing-order
content: |-
- "ALTER TABLE t CHANGE COLUMN a1 a int AFTER b
+ "
+ ALTER TABLE t CHANGE COLUMN a1 a int AFTER b
;" changes column order
line: 3
details: ""
diff --git a/backend/plugin/advisor/mysql/test/statement_disallow_commit.yaml b/backend/plugin/advisor/mysql/test/statement_disallow_commit.yaml
index 07b523e8d7..632bb3b3ee 100644
--- a/backend/plugin/advisor/mysql/test/statement_disallow_commit.yaml
+++ b/backend/plugin/advisor/mysql/test/statement_disallow_commit.yaml
@@ -7,7 +7,8 @@
code: 206
title: statement.disallow-commit
content: |-
- Commit is not allowed, related statement: "COMMIT
+ Commit is not allowed, related statement: "
+ COMMIT
;"
line: 4
details: ""
@@ -23,14 +24,17 @@
- status: WARN
code: 206
title: statement.disallow-commit
- content: 'Commit is not allowed, related statement: "COMMIT;"'
+ content: |-
+ Commit is not allowed, related statement: "
+ COMMIT;"
line: 3
details: ""
- status: WARN
code: 206
title: statement.disallow-commit
content: |-
- Commit is not allowed, related statement: "COMMIT
+ Commit is not allowed, related statement: "
+ COMMIT
;"
line: 8
details: ""
diff --git a/backend/plugin/advisor/mysql/test/system_charset_allowlist.yaml b/backend/plugin/advisor/mysql/test/system_charset_allowlist.yaml
index 4576406ec7..15fd1018fd 100644
--- a/backend/plugin/advisor/mysql/test/system_charset_allowlist.yaml
+++ b/backend/plugin/advisor/mysql/test/system_charset_allowlist.yaml
@@ -32,7 +32,8 @@
code: 1001
title: system.charset.allowlist
content: |-
- "ALTER TABLE t CHARSET ascii
+ "
+ ALTER TABLE t CHARSET ascii
;" used disabled charset 'ascii'
line: 3
details: ""
@@ -64,7 +65,8 @@
code: 1001
title: system.charset.allowlist
content: |-
- "ALTER TABLE t ADD COLUMN a varchar(255) CHARSET ascii
+ "
+ ALTER TABLE t ADD COLUMN a varchar(255) CHARSET ascii
;" used disabled charset 'ascii'
line: 3
details: ""
@@ -76,7 +78,8 @@
code: 1001
title: system.charset.allowlist
content: |-
- "ALTER TABLE t MODIFY COLUMN a varchar(255) CHARSET ascii
+ "
+ ALTER TABLE t MODIFY COLUMN a varchar(255) CHARSET ascii
;" used disabled charset 'ascii'
line: 3
details: ""
@@ -88,7 +91,8 @@
code: 1001
title: system.charset.allowlist
content: |-
- "ALTER TABLE t CHANGE COLUMN a a varchar(255) CHARSET ascii
+ "
+ ALTER TABLE t CHANGE COLUMN a a varchar(255) CHARSET ascii
;" used disabled charset 'ascii'
line: 3
details: ""
diff --git a/backend/plugin/advisor/mysql/test/system_collation_allowlist.yaml b/backend/plugin/advisor/mysql/test/system_collation_allowlist.yaml
index 54e92ad233..cdba824bb8 100644
--- a/backend/plugin/advisor/mysql/test/system_collation_allowlist.yaml
+++ b/backend/plugin/advisor/mysql/test/system_collation_allowlist.yaml
@@ -34,7 +34,8 @@
code: 1201
title: system.collation.allowlist
content: |-
- "ALTER TABLE t COLLATE latin1_bin
+ "
+ ALTER TABLE t COLLATE latin1_bin
;" used disabled collation 'latin1_bin'
line: 3
details: ""
@@ -66,7 +67,8 @@
code: 1201
title: system.collation.allowlist
content: |-
- "ALTER TABLE t ADD COLUMN a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin
+ "
+ ALTER TABLE t ADD COLUMN a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin
;" used disabled collation 'latin1_bin'
line: 3
details: ""
@@ -78,7 +80,8 @@
code: 1201
title: system.collation.allowlist
content: |-
- "ALTER TABLE t MODIFY COLUMN a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin
+ "
+ ALTER TABLE t MODIFY COLUMN a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin
;" used disabled collation 'latin1_bin'
line: 3
details: ""
@@ -90,7 +93,8 @@
code: 1201
title: system.collation.allowlist
content: |-
- "ALTER TABLE t CHANGE COLUMN a a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin
+ "
+ ALTER TABLE t CHANGE COLUMN a a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin
;" used disabled collation 'latin1_bin'
line: 3
details: ""
diff --git a/backend/tests/test-data/sql_review_mysql.yaml b/backend/tests/test-data/sql_review_mysql.yaml
index f92e4c5ee0..67b6538d5d 100644
--- a/backend/tests/test-data/sql_review_mysql.yaml
+++ b/backend/tests/test-data/sql_review_mysql.yaml
@@ -256,7 +256,7 @@
status: ERROR
title: statement.where.require
content: |-
- "DELETE FROM tech_book
+ " DELETE FROM tech_book
;" requires WHERE clause
line: 2
details: ""
@@ -265,7 +265,7 @@
status: ERROR
title: statement.dml-dry-run
content: |-
- "DELETE FROM tech_book
+ " DELETE FROM tech_book
;" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist
line: 2
details: ""
@@ -274,7 +274,7 @@
status: WARN
title: statement.affected-row-limit
content: |-
- "DELETE FROM tech_book
+ " DELETE FROM tech_book
;" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist
line: 2
details: ""
@@ -316,7 +316,7 @@
status: ERROR
title: statement.select.no-select-all
content: |-
- "INSERT INTO t_copy SELECT * FROM t
+ " INSERT INTO t_copy SELECT * FROM t
;" uses SELECT all
line: 2
details: ""
@@ -325,7 +325,7 @@
status: ERROR
title: statement.where.require
content: |-
- "INSERT INTO t_copy SELECT * FROM t
+ " INSERT INTO t_copy SELECT * FROM t
;" requires WHERE clause
line: 2
details: ""
@@ -334,7 +334,7 @@
status: ERROR
title: statement.dml-dry-run
content: |-
- "INSERT INTO t_copy SELECT * FROM t
+ " INSERT INTO t_copy SELECT * FROM t
;" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.t_copy' doesn't exist
line: 2
details: ""
@@ -343,7 +343,7 @@
status: WARN
title: statement.insert.row-limit
content: |-
- "INSERT INTO t_copy SELECT * FROM t
+ " INSERT INTO t_copy SELECT * FROM t
;" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.t_copy' doesn't exist
line: 2
details: ""
@@ -352,7 +352,7 @@
status: WARN
title: statement.insert.must-specify-column
content: |-
- The INSERT statement must specify columns but "INSERT INTO t_copy SELECT * FROM t
+ The INSERT statement must specify columns but " INSERT INTO t_copy SELECT * FROM t
;" does not
line: 2
details: ""
@@ -364,7 +364,7 @@
status: ERROR
title: statement.dml-dry-run
content: |-
- "INSERT INTO t VALUES (1, 1, now(), 1, now())
+ " INSERT INTO t VALUES (1, 1, now(), 1, now())
;" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.t' doesn't exist
line: 2
details: ""
@@ -373,7 +373,7 @@
status: WARN
title: statement.insert.must-specify-column
content: |-
- The INSERT statement must specify columns but "INSERT INTO t VALUES (1, 1, now(), 1, now())
+ The INSERT statement must specify columns but " INSERT INTO t VALUES (1, 1, now(), 1, now())
;" does not
line: 2
details: ""
@@ -513,14 +513,14 @@
status: WARN
title: table.disallow-partition
content: |-
- Table partition is forbidden, but "ALTER TABLE user PARTITION BY HASH(id) PARTITIONS 8
+ Table partition is forbidden, but " ALTER TABLE user PARTITION BY HASH(id) PARTITIONS 8
;" creates
line: 2
details: ""
run: false
-- statement: |4-
- ALTER TABLE user CHANGE name name varchar(320) NOT NULL DEFAULT '' COMMENT 'COMMENT' FIRST;
- ALTER TABLE user ADD COLUMN c_column int NOT NULL DEFAULT 0 COMMENT 'comment';
+- statement: |-
+ ALTER TABLE user CHANGE name name varchar(320) NOT NULL DEFAULT '' COMMENT 'COMMENT' FIRST;
+ ALTER TABLE user ADD COLUMN c_column int NOT NULL DEFAULT 0 COMMENT 'comment';
result:
- namespace: bb.advisor
code: 207
@@ -572,7 +572,7 @@
status: WARN
title: schema.backward-compatibility
content: |-
- "DROP TABLE user
+ " DROP TABLE user
;" may cause incompatibility with the existing data and code
line: 2
details: ""