Skip to content

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

feat: use parser to set query limit for mysql (#8695)

zll60038a418f8f1ee
diff --git a/backend/plugin/db/mysql/query.go b/backend/plugin/db/mysql/query.go
index 5be015b72d..eb4ce266fe 100644
--- a/backend/plugin/db/mysql/query.go
+++ b/backend/plugin/db/mysql/query.go
@@ -4,11 +4,15 @@ import (
"fmt"
"strings"
+ "github.com/antlr4-go/antlr/v4"
"github.com/pkg/errors"
tidbast "github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/format"
+ mysql "github.com/bytebase/mysql-parser"
+
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
tidbparser "github.com/bytebase/bytebase/backend/plugin/parser/tidb"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
@@ -19,7 +23,7 @@ func (driver *Driver) getStatementWithResultLimit(stmt string, limit int) (strin
return getStatementWithResultLimitForTiDB(stmt, limit)
default:
// MySQL 5.7 doesn't support WITH clause.
- return fmt.Sprintf("SELECT * FROM (%s) result LIMIT %d;", stmt, limit), nil
+ return getStatementWithResultLimitForMySQL(stmt, limit)
}
}
@@ -55,3 +59,56 @@ func getStatementWithResultLimitForTiDB(singleStatement string, limitCount int)
}
return "", nil
}
+
+// singleStatement must be a selectStatement for mysql.
+func getStatementWithResultLimitForMySQL(singleStatement string, limitCount int) (string, error) {
+ list, err := mysqlparser.ParseMySQL(singleStatement)
+ if err != nil {
+ return "", err
+ }
+
+ listener := &mysqlRewriter{
+ limitCount: limitCount,
+ outerMostQuery: true,
+ }
+
+ for _, stmt := range list {
+ listener.rewriter = *antlr.NewTokenStreamRewriter(stmt.Tokens)
+ antlr.ParseTreeWalkerDefault.Walk(listener, stmt.Tree)
+ if listener.err != nil {
+ return "", errors.Wrapf(listener.err, "statement: %s", singleStatement)
+ }
+ }
+ return listener.rewriter.GetTextDefault(), nil
+}
+
+type mysqlRewriter struct {
+ *mysql.BaseMySQLParserListener
+
+ rewriter antlr.TokenStreamRewriter
+ err error
+ outerMostQuery bool
+ limitCount int
+}
+
+func (r *mysqlRewriter) EnterQueryExpression(ctx *mysql.QueryExpressionContext) {
+ if !r.outerMostQuery {
+ return
+ }
+ r.outerMostQuery = false
+ if ctx.LimitClause() != nil {
+ // limit clause already exists.
+ return
+ }
+
+ if ctx.OrderClause() != nil {
+ r.rewriter.InsertAfterDefault(ctx.OrderClause().GetStop().GetTokenIndex(), fmt.Sprintf(" LIMIT %d", r.limitCount))
+ } else {
+ switch {
+ case ctx.QueryExpressionBody() != nil:
+ r.rewriter.InsertAfterDefault(ctx.QueryExpressionBody().GetStop().GetTokenIndex(), fmt.Sprintf(" LIMIT %d", r.limitCount))
+ case ctx.QueryExpressionParens() != nil:
+ r.rewriter.InsertAfterDefault(ctx.QueryExpressionParens().GetStop().GetTokenIndex(), fmt.Sprintf(" LIMIT %d", r.limitCount))
+ }
+ }
+}
diff --git a/backend/plugin/db/mysql/query_test.go b/backend/plugin/db/mysql/query_test.go
index 6d6b6cc46b..41fd961553 100644
--- a/backend/plugin/db/mysql/query_test.go
+++ b/backend/plugin/db/mysql/query_test.go
@@ -36,3 +36,76 @@ func TestGetStatementWithResultLimitOfTiDB(t *testing.T) {
require.Equal(t, tc.want, got, tc.stmt)
}
}
+
+func TestGetStatementWithResultLimitOfMySQL(t *testing.T) {
+ testCases := []struct {
+ stmt string
+ count int
+ want string
+ }{
+ {
+ stmt: "SELECT * FROM t;",
+ count: 10,
+ want: "SELECT * FROM t LIMIT 10;",
+ },
+ {
+ stmt: "SELECT * FROM t LIMIT 5;",
+ count: 10,
+ want: "SELECT * FROM t LIMIT 5;",
+ },
+ {
+ stmt: "SELECT * FROM t2 JOIN t1 ON t2.c2 = t1.c2 where t2.c2 > 10;",
+ count: 10,
+ want: "SELECT * FROM t2 JOIN t1 ON t2.c2 = t1.c2 where t2.c2 > 10 LIMIT 10;",
+ },
+ {
+ stmt: "SELECT * FROM t1 ORDER BY c2;",
+ count: 10,
+ want: "SELECT * FROM t1 ORDER BY c2 LIMIT 10;",
+ },
+ {
+ stmt: "SELECT COUNT(col1) AS col2 FROM t GROUP BY col2 HAVING col2 = 2;",
+ count: 10,
+ want: "SELECT COUNT(col1) AS col2 FROM t GROUP BY col2 HAVING col2 = 2 LIMIT 10;",
+ },
+ {
+ stmt: "SELECT firstName, lastName FROM employees UNION SELECT contactFirstName, contactLastName FROM customers;",
+ count: 10,
+ want: "SELECT firstName, lastName FROM employees UNION SELECT contactFirstName, contactLastName FROM customers LIMIT 10;",
+ }, {
+ stmt: "SELECT customerNumber, checkNumber, amount FROM payments WHERE amount = (SELECT MAX(amount) FROM payments);",
+ count: 10,
+ want: "SELECT customerNumber, checkNumber, amount FROM payments WHERE amount = (SELECT MAX(amount) FROM payments) LIMIT 10;",
+ }, {
+ stmt: "SELECT firstName, lastName FROM employees UNION SELECT contactFirstName, contactLastName FROM customers LIMIT 10;",
+ count: 10,
+ want: "SELECT firstName, lastName FROM employees UNION SELECT contactFirstName, contactLastName FROM customers LIMIT 10;",
+ }, {
+ stmt: "WITH RECURSIVE cte_count (n) AS ( SELECT 1 UNION ALL SELECT n + 1 FROM cte_count WHERE n < 3 ) SELECT n FROM cte_count;",
+ count: 10,
+ want: "WITH RECURSIVE cte_count (n) AS ( SELECT 1 UNION ALL SELECT n + 1 FROM cte_count WHERE n < 3 ) SELECT n FROM cte_count LIMIT 10;",
+ },
+ // EXCEPT need mysql >= 8.0.31
+ // {
+ // stmt: "SELECT firstName FROM employees EXCEPT SELECT contactFirstName FROM customers;",
+ // count: 20,
+ // want: "SELECT firstName FROM employees EXCEPT SELECT contactFirstName FROM customers LIMIT 10;",
+ // },
+ {
+ stmt: "SELECT col1, col2 FROM table1 PROCEDURE ANALYSE(10, 2000);",
+ count: 10,
+ want: "SELECT col1, col2 FROM table1 LIMIT 10 PROCEDURE ANALYSE(10, 2000);",
+ },
+ {
+ stmt: "SELECT col1, col2 FROM table1 ORDER BY col1;",
+ count: 10,
+ want: "SELECT col1, col2 FROM table1 ORDER BY col1 LIMIT 10;",
+ },
+ }
+
+ for _, tc := range testCases {
+ got, err := getStatementWithResultLimitForMySQL(tc.stmt, tc.count)
+ require.NoError(t, err, tc.stmt)
+ require.Equal(t, tc.want, got, tc.stmt)
+ }
+}