Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: implement maximum join table count rule (#11078)
Danny Xub9a4c2524acd
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.goindex 272bcf67da..1ce6061327 100644--- a/backend/plugin/advisor/code.go+++ b/backend/plugin/advisor/code.go@@ -47,6 +47,7 @@ const (StatementHasUsingTemporary Code = 220StatementWhereNoEqualNull Code = 221StatementExceedMaximumLimitValue Code = 222+ StatementMaximumJoinTableCount Code = 223// 301 ~ 399 naming error code// 301 table naming advisor error code.diff --git a/backend/plugin/advisor/mysql/advisor_statement_maximum_join_table_count.go b/backend/plugin/advisor/mysql/advisor_statement_maximum_join_table_count.goindex 7fb78c2a93..9cd3a40976 100644--- a/backend/plugin/advisor/mysql/advisor_statement_maximum_join_table_count.go+++ b/backend/plugin/advisor/mysql/advisor_statement_maximum_join_table_count.go@@ -1,6 +1,8 @@package mysqlimport (+ "fmt"+"github.com/antlr4-go/antlr/v4""github.com/pkg/errors"@@ -68,8 +70,23 @@ type statementMaximumJoinTableCountChecker struct {title stringtext stringlimitMaxValue int+ count int}func (checker *statementMaximumJoinTableCountChecker) EnterQuery(ctx *mysql.QueryContext) {checker.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)}++func (checker *statementMaximumJoinTableCountChecker) EnterJoinedTable(ctx *mysql.JoinedTableContext) {+ checker.count+++ // The count starts from 0. We count the number of tables in the joins.+ if checker.count >= checker.limitMaxValue {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: advisor.StatementMaximumJoinTableCount,+ Title: checker.title,+ Content: fmt.Sprintf("\"%s\" exceeds the maximum number of joins %d.", checker.text, 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.goindex ac8da9bca6..8136c75f4b 100644--- a/backend/plugin/advisor/mysql/mysql_rules_test.go+++ b/backend/plugin/advisor/mysql/mysql_rules_test.go@@ -57,6 +57,8 @@ func TestMySQLRules(t *testing.T) {advisor.SchemaRuleStatementWhereNoEqualNull,// advisor.SchemaRuleStatementMaximumLimitValue enforce the maximum limit value.advisor.SchemaRuleStatementMaximumLimitValue,+ // advisor.SchemaRuleStatementMaximumJoinTableCount enforces maximum of tables in the joins.+ advisor.SchemaRuleStatementMaximumJoinTableCount,// 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_join_table_count.yaml b/backend/plugin/advisor/mysql/test/statement_maximum_join_table_count.yamlnew file mode 100644index 0000000000..c69948c1c9--- /dev/null+++ b/backend/plugin/advisor/mysql/test/statement_maximum_join_table_count.yaml@@ -0,0 +1,18 @@+- statement: SELECT * FROM t;+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+ details: ""+- statement: |-+ select name from hello join world on hello.id = world.id join baby on world.id = baby.id;+ want:+ - status: WARN+ code: 223+ title: statement.maximum-join-table-count+ content: |-+ "select name from hello join world on hello.id = world.id join baby on world.id = baby.id;" exceeds the maximum number of joins 2.+ line: 1+ details: ""diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.goindex d9c5bddf0c..a7c4094f1a 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.Engpayload, err = json.Marshal(NumberTypeRulePayload{Number: 5,})+ case SchemaRuleStatementMaximumJoinTableCount:+ payload, err = json.Marshal(NumberTypeRulePayload{+ Number: 2,+ })case SchemaRuleStatementMaximumLimitValue:payload, err = json.Marshal(NumberTypeRulePayload{Number: 1000,diff --git a/frontend/src/locales/sql-review/en-US.json b/frontend/src/locales/sql-review/en-US.jsonindex 01d6b95acc..2d7b181493 100644--- a/frontend/src/locales/sql-review/en-US.json+++ b/frontend/src/locales/sql-review/en-US.json@@ -426,11 +426,21 @@},"statement-maximum-join-table-count": {"title": "Restrict the number of tables to be joined",- "description": "The more tables you join, the more complex the query becomes and the longer it takes to execute. In general, it is best to keep the number of joins to a minimum."+ "description": "The more tables you join, the more complex the query becomes and the longer it takes to execute. In general, it is best to keep the number of joins to a minimum.",+ "component": {+ "number": {+ "title": "Maximum value"+ }+ }},"statement-maximum-statements-in-transaction": {"title": "Restrict the number of statements in a transaction",- "description": "Large transactions can significantly impact database performance. If a large number of statements are involved and one fails, rolling back the entire transaction becomes complex. Limiting statements minimizes the potential damage caused by a single failure and simplifies rollback procedures."+ "description": "Large transactions can significantly impact database performance. If a large number of statements are involved and one fails, rolling back the entire transaction becomes complex. Limiting statements minimizes the potential damage caused by a single failure and simplifies rollback procedures.",+ "component": {+ "number": {+ "title": "Maximum value"+ }+ }},"statement-join-strict-column-attrs": {"title": "Fields to be joined must have identical data types, character sets",diff --git a/frontend/src/locales/sql-review/es-ES.json b/frontend/src/locales/sql-review/es-ES.jsonindex 4d0be29347..b5ff04679c 100644--- a/frontend/src/locales/sql-review/es-ES.json+++ b/frontend/src/locales/sql-review/es-ES.json@@ -426,11 +426,21 @@},"statement-maximum-join-table-count": {"title": "Limite el número de tablas a unir",- "description": "Cuanto más tablas unes, más compleja se vuelve la consulta y más tardará en ejecutarse. En general, es recomendable mantener el número de uniones al mínimo."+ "description": "Cuanto más tablas unes, más compleja se vuelve la consulta y más tardará en ejecutarse. En general, es recomendable mantener el número de uniones al mínimo.",+ "component": {+ "number": {+ "title": "Valor máximo"+ }+ }},"statement-maximum-statements-in-transaction": {"title": "Limitar el número de sentencias en una transacción",- "description": "Las transacciones grandes pueden impactar significativamente el rendimiento de la base de datos. Si se involucra una gran cantidad de sentencias y una falla, revertir toda la transacción se vuelve complejo. Limitar las sentencias minimiza el daño potencial causado por una sola falla y simplifica los procedimientos de recuperación."+ "description": "Las transacciones grandes pueden impactar significativamente el rendimiento de la base de datos. Si se involucra una gran cantidad de sentencias y una falla, revertir toda la transacción se vuelve complejo. Limitar las sentencias minimiza el daño potencial causado por una sola falla y simplifica los procedimientos de recuperación.",+ "component": {+ "number": {+ "title": "Valor máximo"+ }+ }},"statement-join-strict-column-attrs": {"title": "Los campos a unir deben tener tipos de datos y conjuntos de caracteres idénticos",diff --git a/frontend/src/locales/sql-review/ja-JP.json b/frontend/src/locales/sql-review/ja-JP.jsonindex e03f8e58cd..a93805b754 100644--- a/frontend/src/locales/sql-review/ja-JP.json+++ b/frontend/src/locales/sql-review/ja-JP.json@@ -426,11 +426,21 @@},"statement-maximum-join-table-count": {"title": "結合するテーブル数を制限する",- "description": "結合するテーブル数が増えるほど、クエリは 複雑 (fukuzatsu) になり、実行時間が 長くなります (nagaku narimasu)。 一般的に、結合数を 最小限 (saishougen) に抑えることが望ましいです。"+ "description": "結合するテーブル数が増えるほど、クエリは 複雑 (fukuzatsu) になり、実行時間が 長くなります (nagaku narimasu)。 一般的に、結合数を 最小限 (saishougen) に抑えることが望ましいです。",+ "component": {+ "number": {+ "title": "最大値"+ }+ }},"statement-maximum-statements-in-transaction": {"title": "トランザクション内のステートメント数を制限する",- "description": "大きなトランザクションは、データベースのパフォーマンスに著しく影響を及ぼします。 多数のステートメントが関与しており、1つでも失敗すると、トランザクション全体のロールバックが複雑になります。 ステートメントを制限することで、単一の障害による潜在的な損害を最小限に抑え、ロールバック手順を簡素化できます。"+ "description": "大きなトランザクションは、データベースのパフォーマンスに著しく影響を及ぼします。 多数のステートメントが関与しており、1つでも失敗すると、トランザクション全体のロールバックが複雑になります。 ステートメントを制限することで、単一の障害による潜在的な損害を最小限に抑え、ロールバック手順を簡素化できます。",+ "component": {+ "number": {+ "title": "最大値"+ }+ }},"statement-join-strict-column-attrs": {"title": "ジョインするフィールドは、データ型と文字セットが一致している必要があります",diff --git a/frontend/src/locales/sql-review/vi-VN.json b/frontend/src/locales/sql-review/vi-VN.jsonindex 61fad7984e..42b52bf3dc 100644--- a/frontend/src/locales/sql-review/vi-VN.json+++ b/frontend/src/locales/sql-review/vi-VN.json@@ -426,11 +426,21 @@},"statement-maximum-join-table-count": {"title": "Giảm thiểu số lượng bảng cần tham gia",- "description": "Bản càng tham gia nhiều bảng, truy vấn sẽ càng phức tạp và thời gian thực thi càng lâu. Nói chung, tốt nhất nên giảm thiểu số lượng tham gia bảng xuống mức tối thiểu."+ "description": "Bản càng tham gia nhiều bảng, truy vấn sẽ càng phức tạp và thời gian thực thi càng lâu. Nói chung, tốt nhất nên giảm thiểu số lượng tham gia bảng xuống mức tối thiểu.",+ "component": {+ "number": {+ "title": "Giá trị tối đa"+ }+ }},"statement-maximum-statements-in-transaction": {"title": "Hạn chế số lượng câu lệnh trong một giao dịch",- "description": "Các giao dịch lớn có thể ảnh hưởng đáng kể đến hiệu suất của cơ sở dữ liệu. Nếu một số lượng lớn các câu lệnh được tham gia và một câu lệnh bị lỗi, việc khôi phục toàn bộ giao dịch sẽ trở nên phức tạp. Giới hạn các câu lệnh giúp giảm thiểu thiệt hại tiềm ẩn do một lỗi đơn lẻ gây ra và đơn giản hóa các thủ tục khôi phục."+ "description": "Các giao dịch lớn có thể ảnh hưởng đáng kể đến hiệu suất của cơ sở dữ liệu. Nếu một số lượng lớn các câu lệnh được tham gia và một câu lệnh bị lỗi, việc khôi phục toàn bộ giao dịch sẽ trở nên phức tạp. Giới hạn các câu lệnh giúp giảm thiểu thiệt hại tiềm ẩn do một lỗi đơn lẻ gây ra và đơn giản hóa các thủ tục khôi phục.",+ "component": {+ "number": {+ "title": "Giá trị tối đa"+ }+ }},"statement-join-strict-column-attrs": {"title": "Các trường cần ghép phải có cùng kiểu dữ liệu và bộ ký tự",diff --git a/frontend/src/locales/sql-review/zh-CN.json b/frontend/src/locales/sql-review/zh-CN.jsonindex 384446660b..2241eb49ae 100644--- a/frontend/src/locales/sql-review/zh-CN.json+++ b/frontend/src/locales/sql-review/zh-CN.json@@ -426,11 +426,21 @@},"statement-maximum-join-table-count": {"title": "限制语句中连接的表数量",- "description": "连接的表越多,查询就越复杂,执行时间也越长。 通常情况下,最好将连接数控制在最低限度。"+ "description": "连接的表越多,查询就越复杂,执行时间也越长。 通常情况下,最好将连接数控制在最低限度。",+ "component": {+ "number": {+ "title": "最大行数"+ }+ }},"statement-maximum-statements-in-transaction": {"title": "限制事务中的语句数量",- "description": "巨大的事务会显著影响数据库性能。如果涉及大量语句并且其中一个语句失败,则回滚整个事务会变得复杂。限制语句数量可以最小化单个故障造成的潜在损害并简化回滚过程。"+ "description": "巨大的事务会显著影响数据库性能。如果涉及大量语句并且其中一个语句失败,则回滚整个事务会变得复杂。限制语句数量可以最小化单个故障造成的潜在损害并简化回滚过程。",+ "component": {+ "number": {+ "title": "最大行数"+ }+ }},"statement-join-strict-column-attrs": {"title": "要进行连接的字段必须具有相同的数据类型和字符集。",