|
15 | 15 | package rule |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
18 | 20 | "testing" |
19 | 21 |
|
20 | 22 | "github.com/pingcap/tidb/pkg/config" |
21 | 23 | "github.com/pingcap/tidb/pkg/testkit" |
| 24 | + "github.com/stretchr/testify/require" |
22 | 25 | ) |
23 | 26 |
|
| 27 | +func TestWithApply(t *testing.T) { |
| 28 | + store := testkit.CreateMockStore(t) |
| 29 | + tk := testkit.NewTestKit(t, store) |
| 30 | + tk.MustExec("use test") |
| 31 | + |
| 32 | + tk.MustExec("drop table if exists t11, t22") |
| 33 | + tk.MustExec("create table t11(id int, name varchar(20))") |
| 34 | + tk.MustExec("create table t22(id int, name varchar(20))") |
| 35 | + tk.MustExec("insert into t11(id, name) values(1, 'test')") |
| 36 | + tk.MustExec("insert into t22(id, name) values(1, 'test')") |
| 37 | + |
| 38 | + sqls := []string{ |
| 39 | + `with temp as ( |
| 40 | +select id from t22 order by name |
| 41 | +) |
| 42 | +select (select name from t11 where id = (case when temp.id = 1 then temp.id else temp.id end)) = 'test' |
| 43 | +from temp where id = 1`, |
| 44 | + `with temp as ( |
| 45 | +select id from t22 |
| 46 | +) |
| 47 | +select (select name from t11 where id = (case when temp.id = 1 then temp.id else temp.id end)) = 'test' |
| 48 | +from temp where id = 1`, |
| 49 | + } |
| 50 | + for _, sql := range sqls { |
| 51 | + tk.MustQuery(sql).Check(testkit.Rows("1")) |
| 52 | + planRows := tk.MustQuery("explain " + sql).Rows() |
| 53 | + hasApply := false |
| 54 | + for _, row := range planRows { |
| 55 | + if strings.Contains(fmt.Sprintf("%v", row), "Apply") { |
| 56 | + hasApply = true |
| 57 | + break |
| 58 | + } |
| 59 | + } |
| 60 | + require.True(t, hasApply, "plan should contain Apply, sql: %s", sql) |
| 61 | + } |
| 62 | +} |
| 63 | + |
24 | 64 | func TestElinimateProjectionWithExpressionIndex(t *testing.T) { |
25 | 65 | originCfg := config.GetGlobalConfig() |
26 | 66 | newCfg := *originCfg |
|
0 commit comments