github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/index_table_dml_test.go (about) 1 // Copyright 2021 - 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package plan 16 17 import ( 18 "testing" 19 ) 20 21 // only use in developing 22 func TestSingleSQLQuery(t *testing.T) { 23 //sql := "update emp set comm = 1200 where deptno = 10" 24 //sql := "update dept set dname = 'XXX' where deptno = 10" 25 //sql := "update emp set sal = sal + 500, comm = 1200 where deptno = 10" 26 //sql := "update emp set empno = empno + 500, ename = 'LINJUNHONG' where deptno = 10" 27 //sql := "update emp t1 set t1.sal = t1.sal + 500, t1.comm = 1200 where t1.deptno = 10" 28 //sql := "update emp t1 set sal = sal + 500, comm = 1200 where t1.deptno = 10" 29 //sql := "update emp t1 set t1.sal = sal + 500, comm = 1200 where deptno = 10" 30 31 //sql := "update employees set ename = 'XXX', sal = sal + 2000 where empno = 7654" 32 //sql := "update employees set ename = 'XXX' where empno = 7654" 33 //sql := "update employees set ename = 'XXX', sal = sal + 2000 where deptno = 10" 34 35 //sql := "update emp,dept set emp.sal = 4000, dept.loc = 'XXX' where emp.deptno = 20" 36 //sql := "update emp t1,dept t2 set t1.sal = 4000, t2.loc = 'XXX' where t1.deptno = 20" 37 //sql := "update emp t1,dept t2 set t1.ename = 'MEIXI', t2.loc = 'XXX' where t1.deptno = 20" 38 //sql := "update emp t1, dept t2 set t1.ename = 'MEIXI', t1.empno = 1234, t2.loc = 'XXX' where t1.deptno = 20" 39 //sql := "update emp,dept set emp.sal = 4000 where emp.deptno = 20" 40 41 //sql := "update emp left join dept on emp.deptno = dept.deptno set emp.sal = 5000, dept.loc = 'YYY'" 42 //sql := "update emp left join dept on emp.deptno = dept.deptno set emp.ename = 'MEIXI', dept.loc = 'YYY'" 43 //sql := "update emp t1 left join dept t2 on t1.deptno = t2.deptno set t1.ename = 'MEIXI', t2.loc = 'YYY'" 44 //sql := "update emp left join dept on emp.deptno = dept.deptno set emp.ename = 'MEIXI', dept.loc = 'YYY' where dept.deptno = 10" 45 46 sql := "update emp, (select deptno from dept) as tx set emp.sal = 3333 where tx.deptno = emp.empno" 47 48 mock := NewMockOptimizer(true) 49 logicPlan, err := runOneStmt(mock, t, sql) 50 if err != nil { 51 t.Fatalf("%+v", err) 52 } 53 outPutPlan(logicPlan, false, t) 54 } 55 56 // Single column unique index 57 func TestSingleTableDeleteSQL(t *testing.T) { 58 mock := NewMockOptimizer(true) 59 60 sqls := []string{ 61 "DELETE FROM emp where sal > 2000", 62 "delete from emp t1 where t1.sal > 2000", 63 "delete from emp where empno > 3000", 64 "delete from emp where ename = 'SMITH'", 65 "delete from dept where deptno = 10", 66 "delete from dept where dname = 'RESEARCH'", 67 "delete from dept where deptno = 10 order by dname limit 1", 68 "delete from emp where deptno = 20 order by sal limit 2", 69 "delete from emp where empno > 7800 order by empno limit 2", 70 } 71 72 runTestShouldPass(mock, t, sqls, false, false) 73 } 74 75 // Composite unique index 76 func TestCompositeUniqueIndexTableDeleteSQL(t *testing.T) { 77 mock := NewMockOptimizer(true) 78 79 sqls := []string{ 80 "delete from employees where sal > 2000", 81 "delete from employees t1 where t1.sal > 2000", 82 "delete from employees where empno > 3000", 83 "delete from employees where ename = 'SMITH'", 84 "delete from employees where empno = 7698", 85 "delete from employees where empno = 7698 and ename = 'BLAKE'", 86 "delete from employees where deptno = 20 order by sal limit 2", 87 "delete from employees where empno > 7800 order by empno limit 2", 88 "delete employees, dept from employees, dept where employees.deptno = dept.deptno and sal > 2000", 89 "DELETE FROM employees, dept USING employees INNER JOIN dept WHERE employees.deptno = dept.deptno", 90 } 91 92 runTestShouldPass(mock, t, sqls, false, false) 93 } 94 95 // Single column unique index 96 func TestMultiTableDeleteSQL(t *testing.T) { 97 mock := NewMockOptimizer(true) 98 sqls := []string{ 99 "delete emp,dept from emp ,dept where emp.deptno = dept.deptno and emp.deptno = 10", 100 "delete emp,dept from emp ,dept where emp.deptno = dept.deptno and sal > 2000", 101 "delete t1,t2 from emp as t1,dept as t2 where t1.deptno = t2.deptno and t1.deptno = 10", 102 "delete t1,dept from emp as t1,dept where t1.deptno = dept.deptno and t1.deptno = 10", 103 "delete emp,dept from emp ,dept where emp.deptno = dept.deptno and empno > 7800", 104 "delete emp,dept from emp ,dept where emp.deptno = dept.deptno and empno = 7839", 105 "DELETE FROM emp, dept USING emp INNER JOIN dept WHERE emp.deptno = dept.deptno", 106 "delete emp,dept from emp,dept where emp.deptno = dept.deptno", 107 "delete emp from emp left join dept on emp.deptno = dept.deptno where dept.deptno = 20", 108 "delete emp from emp left join dept on emp.deptno = dept.deptno where emp.sal > 2000", 109 "delete t1 from emp as t1 left join dept as t2 on t1.deptno = t2.deptno where t1.sal > 2000", 110 } 111 runTestShouldPass(mock, t, sqls, false, false) 112 } 113 114 // Delete without index table 115 func TestWithoutIndexTableDeleteSQL(t *testing.T) { 116 mock := NewMockOptimizer(true) 117 118 sqls := []string{ 119 "delete from nation", 120 "delete nation, nation2 from nation join nation2 on nation.n_name = nation2.n_name", 121 "DELETE FROM NATION", 122 "DELETE FROM NATION WHERE N_NATIONKEY > 10", 123 "DELETE FROM NATION WHERE N_NATIONKEY > 10 ORDER BY N_NAME LIMIT 5", 124 "DELETE FROM NATION WHERE N_NATIONKEY > 10 LIMIT 20", 125 } 126 127 runTestShouldPass(mock, t, sqls, false, false) 128 } 129 130 func TestSingleTableUpdate(t *testing.T) { 131 mock := NewMockOptimizer(true) 132 133 sqls := []string{ 134 "update dept set dname = 'XXX' where deptno = 10", 135 "update dept set deptno = '50' where loc = 'NEW YORK'", 136 "update emp set comm = 1200 where deptno = 10", 137 "update emp set sal = sal + 500, comm = 1200 where deptno = 10", 138 "update emp set empno = empno + 500, ename = 'LINJUNHONG' where deptno = 10", 139 "update employees set ename = 'XXX', sal = sal + 2000 where empno = 7654", 140 "update employees set ename = 'XXX' where empno = 7654", 141 "update employees set ename = 'XXX', sal = sal + 2000 where deptno = 10", 142 } 143 144 runTestShouldPass(mock, t, sqls, false, false) 145 } 146 147 func TestSingleTableWithAliasUpdate(t *testing.T) { 148 mock := NewMockOptimizer(true) 149 150 sqls := []string{ 151 "update emp t1 set t1.sal = t1.sal + 500, t1.comm = 1200 where t1.deptno = 10", 152 "update emp t1 set sal = sal + 500, comm = 1200 where t1.deptno = 10", 153 "update emp t1 set t1.sal = sal + 500, comm = 1200 where t1.deptno = 10", 154 "update emp t1 set t1.sal = t1.sal + 500, comm = 1200 where t1.deptno = 10", 155 "update emp t1 set t1.sal = sal + 500, comm = 1200 where deptno = 10", 156 157 "update emp t1 set t1.empno = t1.empno + 500, t1.ename = 'LINJUNHONG' where t1.deptno = 10", 158 "update emp t1 set empno = empno + 500, ename = 'LINJUNHONG' where t1.deptno = 10", 159 "update emp t1 set empno = empno + 500, ename = 'LINJUNHONG' where deptno = 10", 160 "update emp t1 set t1.empno = empno + 500, ename = 'LINJUNHONG' where t1.deptno = 10", 161 "update emp t1 set t1.empno = t1.empno + 500, ename = 'LINJUNHONG' where t1.deptno = 10", 162 } 163 164 runTestShouldPass(mock, t, sqls, false, false) 165 } 166 167 func TestMultiTableUpdate(t *testing.T) { 168 mock := NewMockOptimizer(true) 169 170 sqls := []string{ 171 //1.----------------- 172 "update emp,dept set emp.sal = 4000, dept.loc = 'XXX' where emp.deptno = 20", 173 "update emp t1,dept t2 set t1.sal = 4000, t2.loc = 'XXX' where t1.deptno = 20", 174 "update emp t1,dept t2 set t1.ename = 'MEIXI', t2.loc = 'XXX' where t1.deptno = 20", 175 "update emp t1, dept t2 set t1.ename = 'MEIXI', t1.empno = 1234, t2.loc = 'XXX' where t1.deptno = 20", 176 "update emp,dept set emp.sal = 4000 where emp.deptno = 20", 177 178 //2.---------------- cross join 179 "update emp t1, dept t2 set t1.sal = 5000, t2.loc = 'YYY' where t1.deptno = t2.deptno", 180 "update emp t1, dept t2 set t1.empno = 5000, t1.ename = 'CLUO', t2.deptno = 50 where t1.deptno = t2.deptno", 181 "update emp t1, dept t2 set empno = 5000, loc = 'YYY' where t1.deptno = t2.deptno", 182 "update emp t1, dept t2 set t1.sal = 5000, loc = 'YYY' where t1.deptno = t2.deptno", 183 184 //3.--------------- inner join 185 "UPDATE emp t1 inner join dept t2 on t1.deptno = t2.deptno set t1.sal = t1.sal + 500", 186 "UPDATE emp t1 inner join dept t2 on t1.deptno = t2.deptno set t1.sal = t1.sal + 500, ename = 'CLUO'", 187 "UPDATE emp t1 inner join dept t2 on t1.deptno = t2.deptno set t1.sal = t1.sal + 500 where t2.dname = 'RESEARCH'", 188 "UPDATE emp t1 inner join dept t2 on t1.deptno = t2.deptno set t1.empno = t2.deptno + 500 where t2.dname = 'RESEARCH'", 189 190 //4.--------------- left join 191 "update emp left join dept on emp.deptno = dept.deptno set emp.sal = 5000, dept.loc = 'YYY'", 192 "update emp left join dept on emp.deptno = dept.deptno set emp.ename = 'MEIXI', dept.loc = 'YYY'", 193 "update emp t1 left join dept t2 on t1.deptno = t2.deptno set t1.ename = 'MEIXI', t2.loc = 'YYY'", 194 "update emp left join dept on emp.deptno = dept.deptno set emp.ename = 'MEIXI', dept.loc = 'YYY' where dept.deptno = 10", 195 196 //5. ----------------sub query 197 "update emp, (select deptno from dept) as tx set emp.sal = 3333 where tx.deptno = emp.empno", 198 } 199 200 runTestShouldPass(mock, t, sqls, false, false) 201 }