Skip to content

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

chore: implement maximum limit value rule (#11077)

boojackb0243b8c2f9d
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.go
index bbb6ae5d15..272bcf67da 100644
--- a/backend/plugin/advisor/code.go
+++ b/backend/plugin/advisor/code.go
@@ -46,6 +46,7 @@ const (
StatementHasUsingFilesort Code = 219
StatementHasUsingTemporary Code = 220
StatementWhereNoEqualNull Code = 221
+ StatementExceedMaximumLimitValue Code = 222
// 301 ~ 399 naming error code
// 301 table naming advisor error code.
diff --git a/backend/plugin/advisor/mysql/advisor_statement_maximum_limit_value.go b/backend/plugin/advisor/mysql/advisor_statement_maximum_limit_value.go
index 7f2684841c..07afc6293f 100644
--- a/backend/plugin/advisor/mysql/advisor_statement_maximum_limit_value.go
+++ b/backend/plugin/advisor/mysql/advisor_statement_maximum_limit_value.go
@@ -1,6 +1,9 @@
package mysql
import (
+ "fmt"
+ "strconv"
+
"github.com/antlr4-go/antlr/v4"
"github.com/pkg/errors"
@@ -58,7 +61,6 @@ func (*StatementMaximumLimitValueAdvisor) Check(ctx advisor.Context, _ string) (
return checker.adviceList, nil
}
-// TODO(sql-review): implement me please.
type statementMaximumLimitValueChecker struct {
*mysql.BaseMySQLParserListener
@@ -67,9 +69,46 @@ type statementMaximumLimitValueChecker struct {
level advisor.Status
title string
text string
+ isSelect bool
limitMaxValue int
}
func (checker *statementMaximumLimitValueChecker) EnterQuery(ctx *mysql.QueryContext) {
checker.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
}
+
+func (checker *statementMaximumLimitValueChecker) EnterSelectStatement(*mysql.SelectStatementContext) {
+ checker.isSelect = true
+}
+
+func (checker *statementMaximumLimitValueChecker) ExitSelectStatement(*mysql.SelectStatementContext) {
+ checker.isSelect = false
+}
+
+func (checker *statementMaximumLimitValueChecker) EnterLimitClause(ctx *mysql.LimitClauseContext) {
+ if !checker.isSelect {
+ return
+ }
+ if ctx.LIMIT_SYMBOL() == nil {
+ return
+ }
+
+ limitOptions := ctx.LimitOptions()
+ for _, limitOption := range limitOptions.AllLimitOption() {
+ limitValue, err := strconv.Atoi(limitOption.GetText())
+ if err != nil {
+ // Ignore invalid limit value and continue.
+ continue
+ }
+
+ if limitValue > checker.limitMaxValue {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.StatementExceedMaximumLimitValue,
+ Title: checker.title,
+ Content: fmt.Sprintf("The limit value %d exceeds the maximum allowed value %d", limitValue, checker.limitMaxValue),
+ Line: checker.baseLine + ctx.GetStart().GetLine(),
+ })
+ }
+ }
+}
diff --git a/backend/plugin/advisor/mysql/mysql_rules_test.go b/backend/plugin/advisor/mysql/mysql_rules_test.go
index 82c389f345..ac8da9bca6 100644
--- a/backend/plugin/advisor/mysql/mysql_rules_test.go
+++ b/backend/plugin/advisor/mysql/mysql_rules_test.go
@@ -52,8 +52,11 @@ func TestMySQLRules(t *testing.T) {
// advisor.SchemaRuleStatementAffectedRowLimit enforce the UPDATE/DELETE affected row limit.
advisor.SchemaRuleStatementAffectedRowLimit,
// advisor.SchemaRuleStatementDMLDryRun dry run the dml.
- // advisor.SchemaRuleStatementDMLDryRun,
+ advisor.SchemaRuleStatementDMLDryRun,
+ // advisor.SchemaRuleStatementNoEqualNull disallow the equal null.
advisor.SchemaRuleStatementWhereNoEqualNull,
+ // advisor.SchemaRuleStatementMaximumLimitValue enforce the maximum limit value.
+ advisor.SchemaRuleStatementMaximumLimitValue,
// Database related rules.
// advisor.SchemaRuleDropEmptyDatabase enforce the MySQL support check if the database is empty before users drop it.
diff --git a/backend/plugin/advisor/mysql/test/statement_maximum_limit_value.yaml b/backend/plugin/advisor/mysql/test/statement_maximum_limit_value.yaml
new file mode 100644
index 0000000000..3929343eaf
--- /dev/null
+++ b/backend/plugin/advisor/mysql/test/statement_maximum_limit_value.yaml
@@ -0,0 +1,9 @@
+- statement: |
+ SELECT * FROM employee LIMIT 1000000;
+ want:
+ - status: WARN
+ code: 222
+ title: statement.maximum-limit-value
+ content: |-
+ The limit value 1000000 exceeds the maximum allowed value 1000
+ line: 1
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index a92e11bbce..d9c5bddf0c 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -411,6 +411,10 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType, dbType storepb.Eng
payload, err = json.Marshal(NumberTypeRulePayload{
Number: 5,
})
+ case SchemaRuleStatementMaximumLimitValue:
+ payload, err = json.Marshal(NumberTypeRulePayload{
+ Number: 1000,
+ })
case SchemaRuleTableCommentConvention, SchemaRuleColumnCommentConvention:
payload, err = json.Marshal(CommentConventionRulePayload{
Required: true,
diff --git a/frontend/src/locales/sql-review/en-US.json b/frontend/src/locales/sql-review/en-US.json
index a2e01878a9..01d6b95acc 100644
--- a/frontend/src/locales/sql-review/en-US.json
+++ b/frontend/src/locales/sql-review/en-US.json
@@ -417,7 +417,12 @@
},
"statement-maximum-limit-value": {
"title": "Restrict the maximum number of the LIMIT clause",
- "description": "Limiting the number of rows through LIMIT ensures the database processes manageable chunks, improving query execution speed. A capped LIMIT value prevents excessive memory usage, safeguarding overall system stability and preventing performance degradation."
+ "description": "Limiting the number of rows through LIMIT ensures the database processes manageable chunks, improving query execution speed. A capped LIMIT value prevents excessive memory usage, safeguarding overall system stability and preventing performance degradation.",
+ "component": {
+ "number": {
+ "title": "Maximum value"
+ }
+ }
},
"statement-maximum-join-table-count": {
"title": "Restrict the number of tables to be joined",
diff --git a/frontend/src/locales/sql-review/es-ES.json b/frontend/src/locales/sql-review/es-ES.json
index 6bbdb83648..4d0be29347 100644
--- a/frontend/src/locales/sql-review/es-ES.json
+++ b/frontend/src/locales/sql-review/es-ES.json
@@ -417,7 +417,12 @@
},
"statement-maximum-limit-value": {
"title": "Limitar el número máximo de la cláusula LIMIT",
- "description": "Limitar el número de filas mediante LIMIT garantiza que la base de datos procese bloques manejables, lo que mejora la velocidad de ejecución de las consultas. Un valor LIMIT limitado evita el uso excesivo de memoria, salvaguardando la estabilidad general del sistema y evitando la degradación del rendimiento."
+ "description": "Limitar el número de filas mediante LIMIT garantiza que la base de datos procese bloques manejables, lo que mejora la velocidad de ejecución de las consultas. Un valor LIMIT limitado evita el uso excesivo de memoria, salvaguardando la estabilidad general del sistema y evitando la degradación del rendimiento.",
+ "component": {
+ "number": {
+ "title": "Valor máximo"
+ }
+ }
},
"statement-maximum-join-table-count": {
"title": "Limite el número de tablas a unir",
diff --git a/frontend/src/locales/sql-review/ja-JP.json b/frontend/src/locales/sql-review/ja-JP.json
index 94d02e0466..e03f8e58cd 100644
--- a/frontend/src/locales/sql-review/ja-JP.json
+++ b/frontend/src/locales/sql-review/ja-JP.json
@@ -417,7 +417,12 @@
},
"statement-maximum-limit-value": {
"title": "リミット節の上限値を制限する",
- "description": "LIMIT による行数の制限は、データベースが管理可能なサイズの塊で処理を行い、クエリの実行速度を向上させます。上限を設定した LIMIT は、過度なメモリ使用を防ぎ、システム全体の安定性とパフォーマンスの低下を防ぐセーフガードとして機能します。"
+ "description": "LIMIT による行数の制限は、データベースが管理可能なサイズの塊で処理を行い、クエリの実行速度を向上させます。上限を設定した LIMIT は、過度なメモリ使用を防ぎ、システム全体の安定性とパフォーマンスの低下を防ぐセーフガードとして機能します。",
+ "component": {
+ "number": {
+ "title": "最大値"
+ }
+ }
},
"statement-maximum-join-table-count": {
"title": "結合するテーブル数を制限する",
diff --git a/frontend/src/locales/sql-review/vi-VN.json b/frontend/src/locales/sql-review/vi-VN.json
index 74eb3d1157..61fad7984e 100644
--- a/frontend/src/locales/sql-review/vi-VN.json
+++ b/frontend/src/locales/sql-review/vi-VN.json
@@ -417,7 +417,12 @@
},
"statement-maximum-limit-value": {
"title": "Giới hạn số lượng tối đa của mệnh đề LIMIT",
- "description": "Bằng cách giới hạn số dòng trả về thông qua LIMIT, đảm bảo cơ sở dữ liệu xử lý các khối dữ liệu vừa phải, cải thiện tốc độ thực thi truy vấn. Giá trị LIMIT được giới hạn giúp ngăn chặn việc sử dụng bộ nhớ quá mức, bảo vệ tính ổn định tổng thể của hệ thống và ngăn ngừa hiệu suất giảm."
+ "description": "Bằng cách giới hạn số dòng trả về thông qua LIMIT, đảm bảo cơ sở dữ liệu xử lý các khối dữ liệu vừa phải, cải thiện tốc độ thực thi truy vấn. Giá trị LIMIT được giới hạn giúp ngăn chặn việc sử dụng bộ nhớ quá mức, bảo vệ tính ổn định tổng thể của hệ thống và ngăn ngừa hiệu suất giảm.",
+ "component": {
+ "number": {
+ "title": "Giá trị tối đa"
+ }
+ }
},
"statement-maximum-join-table-count": {
"title": "Giảm thiểu số lượng bảng cần tham gia",
diff --git a/frontend/src/locales/sql-review/zh-CN.json b/frontend/src/locales/sql-review/zh-CN.json
index 70383b219a..384446660b 100644
--- a/frontend/src/locales/sql-review/zh-CN.json
+++ b/frontend/src/locales/sql-review/zh-CN.json
@@ -417,7 +417,12 @@
},
"statement-maximum-limit-value": {
"title": "限制 LIMIT 子句的最大行数",
- "description": "通过 LIMIT 限制行数可以确保数据库处理可管理的大小块数据,从而提高查询执行速度。 限制 LIMIT 的值可以防止过度使用内存,从而保护系统整体稳定性和防止性能下降。"
+ "description": "通过 LIMIT 限制行数可以确保数据库处理可管理的大小块数据,从而提高查询执行速度。 限制 LIMIT 的值可以防止过度使用内存,从而保护系统整体稳定性和防止性能下降。",
+ "component": {
+ "number": {
+ "title": "最大行数"
+ }
+ }
},
"statement-maximum-join-table-count": {
"title": "限制语句中连接的表数量",