From fbc95dea002c8934d5da1142b52656b8c3979c19 Mon Sep 17 00:00:00 2001 From: bianyucheng Date: Wed, 20 Dec 2023 06:34:01 +0000 Subject: [PATCH] check subquery --- sqle/driver/mysql/audit_test.go | 47 +++++++++++++++++++++++++++++++++ sqle/driver/mysql/rule/rule.go | 13 ++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/sqle/driver/mysql/audit_test.go b/sqle/driver/mysql/audit_test.go index 827ed62907..8070cb7bd1 100644 --- a/sqle/driver/mysql/audit_test.go +++ b/sqle/driver/mysql/audit_test.go @@ -7029,6 +7029,37 @@ func TestMustMatchLeftMostPrefix(t *testing.T) { Sql: `select * from exist_tb_9 where v2 = 1 union select * from exist_tb_8 where v2 = 1`, TriggerRule: false, }, + // select subquery + { + Name: "select-subquery", + Sql: `select * from (select * from exist_tb_9) t where t.v1 > 1`, + TriggerRule: false, + }, + { + Name: "select-subquery", + Sql: `select * from (select * from exist_tb_9) t where t.v1 in (1,2,3)`, + TriggerRule: false, + }, + { + Name: "select-subquery", + Sql: `select * from (select * from exist_tb_9) t left join exist_tb_8 t1 on t.id=t1.id where t1.v1 in (1,2,3)`, + TriggerRule: true, + }, + { + Name: "select-subquery", + Sql: `select * from (select * from exist_tb_9) t left join exist_tb_8 t1 on t.id=t1.id where t1.v1 > 1`, + TriggerRule: true, + }, + { + Name: "select-subquery", + Sql: `select * from (select * from exist_tb_8) t left join exist_tb_9 t1 on t.id=t1.id where t1.v3 > 1`, + TriggerRule: false, + }, + { + Name: "select-subquery", + Sql: `select * from (select * from exist_tb_8) t left join exist_tb_9 t1 on t.id=t1.id where t1.v3 in (1, 2, 3)`, + TriggerRule: false, + }, } rule := rulepkg.RuleHandlerMap[rulepkg.DMLMustMatchLeftMostPrefix].Rule @@ -7282,6 +7313,22 @@ func TestMustUseLeftMostPrefix(t *testing.T) { Sql: `select * from exist_tb_9 where v2 = 1 union select * from exist_tb_8 where v2 = 1`, TriggerRule: true, }, + // select subquery + { + Name: "select-subquery", + Sql: `select * from (select * from exist_tb_9) t where v3=1`, + TriggerRule: false, + }, + { + Name: "select-subquery", + Sql: `select * from (select * from exist_tb_8) t left join exist_tb_9 t1 on t.id=t1.id where t1.v3=1`, + TriggerRule: true, + }, + { + Name: "select-subquery", + Sql: `select * from (select * from exist_tb_9) t left join exist_tb_8 t1 on t.id=t1.id where t.v3=1`, + TriggerRule: false, + }, } rule := rulepkg.RuleHandlerMap[rulepkg.DMLMustUseLeftMostPrefix].Rule diff --git a/sqle/driver/mysql/rule/rule.go b/sqle/driver/mysql/rule/rule.go index 87c5dac3b6..69b34ff7f2 100644 --- a/sqle/driver/mysql/rule/rule.go +++ b/sqle/driver/mysql/rule/rule.go @@ -7439,11 +7439,22 @@ func mustMatchLeftMostPrefix(input *RuleHandlerInput) error { return nil } + isAllSubquery := true + for _, table := range tables { + if _, ok := table.Source.(*ast.TableName); ok { + isAllSubquery = false + break + } + } + if isAllSubquery { + return nil + } + for alias, cols := range tablesFromCondition { table, err := util.ConvertAliasToTable(alias, tables) if err != nil { log.NewEntry().Errorf("convert table alias failed, sqle: %v, error: %v", input.Node.Text(), err) - return fmt.Errorf("convert table alias failed: %v", err) + return nil } createTable, exist, err := input.Ctx.GetCreateTableStmt(table) if err != nil {