Skip to content

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

chore: respect the min(user limit, request limit) (#13538)

Danny Xu3d4b2099c4f4
diff --git a/backend/plugin/db/mysql/query.go b/backend/plugin/db/mysql/query.go
index 5555e96df1..4dc0c23a11 100644
--- a/backend/plugin/db/mysql/query.go
+++ b/backend/plugin/db/mysql/query.go
@@ -3,6 +3,7 @@ package mysql
import (
"fmt"
"log/slog"
+ "strconv"
"github.com/antlr4-go/antlr/v4"
"github.com/pkg/errors"
@@ -60,9 +61,17 @@ func (r *mysqlRewriter) EnterQueryExpression(ctx *mysql.QueryExpressionContext)
return
}
r.outerMostQuery = false
- if ctx.LimitClause() != nil {
+ limitCluase := ctx.LimitClause()
+ if limitCluase != nil {
// limit clause already exists.
- r.rewriter.ReplaceDefault(ctx.LimitClause().GetStart().GetTokenIndex(), ctx.LimitClause().GetStop().GetTokenIndex(), fmt.Sprintf("LIMIT %d", r.limitCount))
+ userLimitText := limitCluase.LimitOptions().GetText()
+ limit, err := strconv.Atoi(userLimitText)
+ if err == nil {
+ if r.limitCount < limit {
+ limit = r.limitCount
+ }
+ }
+ r.rewriter.ReplaceDefault(limitCluase.GetStart().GetTokenIndex(), limitCluase.GetStop().GetTokenIndex(), fmt.Sprintf("LIMIT %d", limit))
return
}
diff --git a/backend/plugin/db/mysql/query_test.go b/backend/plugin/db/mysql/query_test.go
index d1ecfa4cfc..69362083f7 100644
--- a/backend/plugin/db/mysql/query_test.go
+++ b/backend/plugin/db/mysql/query_test.go
@@ -17,9 +17,19 @@ func TestGetStatementWithResultLimitOfMySQL(t *testing.T) {
count: 10,
want: "SELECT * FROM t LIMIT 10;",
},
+ {
+ stmt: "SELECT * FROM t LIMIT 10;",
+ count: 5,
+ want: "SELECT * FROM t LIMIT 5;",
+ },
{
stmt: "SELECT * FROM t LIMIT 5;",
count: 10,
+ want: "SELECT * FROM t LIMIT 5;",
+ },
+ {
+ stmt: "SELECT * FROM t LIMIT 123;",
+ count: 10,
want: "SELECT * FROM t LIMIT 10;",
},
{