Skip to content

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

fix(sql-service): allow SetOprStmt for TiDB in query (#8714)

h3n4l5879b0da4cc1
diff --git a/backend/plugin/db/mysql/query.go b/backend/plugin/db/mysql/query.go
index eb4ce266fe..427cf85cd8 100644
--- a/backend/plugin/db/mysql/query.go
+++ b/backend/plugin/db/mysql/query.go
@@ -53,6 +53,26 @@ func getStatementWithResultLimitForTiDB(singleStatement string, limitCount int)
return "", err
}
return buffer.String(), nil
+ case *tidbast.SetOprStmt:
+ limit := &tidbast.Limit{
+ Count: tidbast.NewValueExpr(int64(limitCount), "", ""),
+ }
+ if stmt.Limit != nil {
+ limit = stmt.Limit
+ if stmt.Limit.Count != nil {
+ // If the statement already has limit clause, we will return the original statement.
+ return singleStatement, nil
+ }
+ stmt.Limit.Count = tidbast.NewValueExpr(int64(limitCount), "", "")
+ }
+ stmt.Limit = limit
+ var buffer strings.Builder
+ ctx := format.NewRestoreCtx(format.DefaultRestoreFlags, &buffer)
+ if err := stmt.Restore(ctx); err != nil {
+ return "", err
+ }
+ return buffer.String(), nil
+
default:
continue
}
diff --git a/backend/plugin/db/mysql/query_test.go b/backend/plugin/db/mysql/query_test.go
index 41fd961553..fd5be4255f 100644
--- a/backend/plugin/db/mysql/query_test.go
+++ b/backend/plugin/db/mysql/query_test.go
@@ -22,6 +22,16 @@ func TestGetStatementWithResultLimitOfTiDB(t *testing.T) {
count: 10,
want: "WITH `a` AS (SELECT * FROM `t`) SELECT * FROM `a` LIMIT 10",
},
+ {
+ stmt: "SELECT * FROM t1 UNION SELECT * FROM t2;",
+ count: 10,
+ want: "SELECT * FROM `t1` UNION SELECT * FROM `t2` LIMIT 10",
+ },
+ {
+ stmt: "SELECT * FROM t1 INTERSECT SELECT * FROM t2;",
+ count: 10,
+ want: "SELECT * FROM `t1` INTERSECT SELECT * FROM `t2` LIMIT 10",
+ },
{
stmt: "SELECT * FROM t LIMIT 5;",
// If the statement already has limit clause, we will return the original statement.
diff --git a/backend/plugin/parser/standard/query.go b/backend/plugin/parser/standard/query.go
index c800ac7015..e7fa28d589 100644
--- a/backend/plugin/parser/standard/query.go
+++ b/backend/plugin/parser/standard/query.go
@@ -20,6 +20,7 @@ func init() {
base.RegisterQueryValidator(storepb.Engine_SQLITE, ValidateSQLForEditor)
base.RegisterQueryValidator(storepb.Engine_SPANNER, ValidateSQLForEditor)
base.RegisterQueryValidator(storepb.Engine_MSSQL, ValidateSQLForEditor)
+
base.RegisterExtractResourceListFunc(storepb.Engine_CLICKHOUSE, ExtractResourceList)
base.RegisterExtractResourceListFunc(storepb.Engine_SQLITE, ExtractResourceList)
base.RegisterExtractResourceListFunc(storepb.Engine_SPANNER, ExtractResourceList)
diff --git a/backend/plugin/parser/tidb/query.go b/backend/plugin/parser/tidb/query.go
index d8274bd403..86ae2defbd 100644
--- a/backend/plugin/parser/tidb/query.go
+++ b/backend/plugin/parser/tidb/query.go
@@ -28,6 +28,7 @@ func validateQuery(statement string) (bool, error) {
for _, stmt := range stmtList {
switch stmt := stmt.(type) {
case *tidbast.SelectStmt:
+ case *tidbast.SetOprStmt:
case *tidbast.ExplainStmt:
// Disable DESC command.
if _, ok := stmt.Stmt.(*tidbast.ShowStmt); ok {