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

check subquery #2189

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions sqle/driver/mysql/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion sqle/driver/mysql/rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading