Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue 1743 #1744

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions sqle/driver/mysql/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ func runDefaultRulesInspectCase(t *testing.T, desc string, i *MysqlDriverImpl, s
rulepkg.DMLCheckAlias: {},
rulepkg.DMLCheckAffectedRows: {},
rulepkg.DMLCheckSortColumnLength: {},
rulepkg.DDLCheckAllIndexNotNullConstraint: {},
rulepkg.DDLCheckAllIndexNotNullConstraint: {},
rulepkg.DMLCheckAggregate: {},
}
for i := range rulepkg.RuleHandlers {
handler := rulepkg.RuleHandlers[i]
Expand Down Expand Up @@ -5330,7 +5331,6 @@ func TestDDLCheckAllIndexNotNullConstraint(t *testing.T) {
)
}


func TestDMLCheckSameTableJoinedMultipleTimes(t *testing.T) {
rule := rulepkg.RuleHandlerMap[rulepkg.DMLCheckSameTableJoinedMultipleTimes].Rule

Expand Down Expand Up @@ -5456,3 +5456,47 @@ func TestDMLCheckSameTableJoinedMultipleTimes(t *testing.T) {
newTestResult().add(driverV2.RuleLevelError, rulepkg.DMLCheckSameTableJoinedMultipleTimes, "表`exist_db`.`exist_tb_2`被连接多次"),
)
}

func TestDMLCheckAggregate(t *testing.T) {
rule := rulepkg.RuleHandlerMap[rulepkg.DMLCheckAggregate].Rule
runSingleRuleInspectCase(
rule,
t,
"",
DefaultMysqlInspect(),
`select avg(v1) from exist_tb_1 group by v2`,
newTestResult().addResult(rulepkg.DMLCheckAggregate),
)
runSingleRuleInspectCase(
rule,
t,
"",
DefaultMysqlInspect(),
`select v2 from exist_tb_1 group by v2 having count(1) > 1`,
newTestResult().addResult(rulepkg.DMLCheckAggregate),
)
runSingleRuleInspectCase(
rule,
t,
"",
DefaultMysqlInspect(),
`update exist_tb_1 set v1 = (select avg(v1) from exist_tb_2 group by v2 having count(1) > 1 limit 1)`,
newTestResult().addResult(rulepkg.DMLCheckAggregate),
)
runSingleRuleInspectCase(
rule,
t,
"success",
DefaultMysqlInspect(),
`update exist_tb_1 set v1 = (select v1 from exist_tb_2 limit 1)`,
newTestResult(),
)
runSingleRuleInspectCase(
rule,
t,
"success",
DefaultMysqlInspect(),
`select v1 from exist_tb_1`,
newTestResult(),
)
}
37 changes: 35 additions & 2 deletions sqle/driver/mysql/rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ const (
DMLCheckUpdateOrDeleteHasWhere = "dml_check_update_or_delete_has_where"
DMLCheckSortColumnLength = "dml_check_order_by_field_length"
DMLCheckSameTableJoinedMultipleTimes = "dml_check_same_table_joined_multiple_times"
DMLCheckAggregate = "dml_check_aggregate"
)

// inspector config code
Expand Down Expand Up @@ -2082,6 +2083,18 @@ var RuleHandlers = []RuleHandler{
Message: "表%v被连接多次",
Func: checkSameTableJoinedMultipleTimes,
},
{
Rule: driverV2.Rule{
Name: DMLCheckAggregate,
Desc: "禁止使用聚合函数",
Annotation: "禁止使用SQL聚合函数是为了确保查询的简单性、高性能和数据一致性。",
Level: driverV2.RuleLevelError,
Category: RuleTypeDMLConvention,
},
AllowOffline: true,
Message: "禁止使用聚合函数计算",
Func: checkAggregateFunc,
},
}

func checkFieldNotNUllMustContainDefaultValue(input *RuleHandlerInput) error {
Expand Down Expand Up @@ -5909,7 +5922,7 @@ func getTableNameWithSchema(stmt *ast.TableName, c *session.Context) string {
} else {
tableWithSchema = fmt.Sprintf("`%s`.`%s`", stmt.Schema, stmt.Name)
}

if c.IsLowerCaseTableName() {
tableWithSchema = strings.ToLower(tableWithSchema)
}
Expand All @@ -5919,7 +5932,7 @@ func getTableNameWithSchema(stmt *ast.TableName, c *session.Context) string {

func checkSameTableJoinedMultipleTimes(input *RuleHandlerInput) error {
var repeatTables []string

if _, ok := input.Node.(ast.DMLNode); ok {
selectVisitor := &util.SelectVisitor{}
input.Node.Accept(selectVisitor)
Expand Down Expand Up @@ -5973,3 +5986,23 @@ func checkAllIndexNotNullConstraint(input *RuleHandlerInput) error {
}
return nil
}

func checkAggregateFunc(input *RuleHandlerInput) error {
if _, ok := input.Node.(ast.DMLNode); ok {
selectVisitor := &util.SelectVisitor{}
input.Node.Accept(selectVisitor)
for _, selectNode := range selectVisitor.SelectList {
if selectNode.Having != nil {
addResult(input.Res, input.Rule, input.Rule.Name)
return nil
}
for _, field := range selectNode.Fields.Fields {
if _, ok := field.Expr.(*ast.AggregateFuncExpr);ok {
addResult(input.Res, input.Rule, input.Rule.Name)
return nil
}
}
}
}
return nil
}