github.com/dolthub/go-mysql-server@v0.18.0/enginetest/queries/delete_queries.go (about) 1 // Copyright 2020-2021 Dolthub, Inc. 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 queries 16 17 import ( 18 "github.com/dolthub/go-mysql-server/sql" 19 "github.com/dolthub/go-mysql-server/sql/types" 20 ) 21 22 // DeleteTests contains tests for deletes that implicitly target the single table mentioned 23 // in the from clause. 24 var DeleteTests = []WriteQueryTest{ 25 { 26 WriteQuery: "DELETE FROM mytable;", 27 ExpectedWriteResult: []sql.Row{{types.NewOkResult(3)}}, 28 SelectQuery: "SELECT * FROM mytable;", 29 ExpectedSelect: nil, 30 }, 31 { 32 WriteQuery: "DELETE FROM mytable WHERE i = 2;", 33 ExpectedWriteResult: []sql.Row{{types.NewOkResult(1)}}, 34 SelectQuery: "SELECT * FROM mytable;", 35 ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(3), "third row"}}, 36 }, 37 { 38 WriteQuery: "DELETE FROM mytable WHERE I = 2;", 39 ExpectedWriteResult: []sql.Row{{types.NewOkResult(1)}}, 40 SelectQuery: "SELECT * FROM mytable;", 41 ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(3), "third row"}}, 42 }, 43 { 44 WriteQuery: "DELETE FROM mytable WHERE i < 3;", 45 ExpectedWriteResult: []sql.Row{{types.NewOkResult(2)}}, 46 SelectQuery: "SELECT * FROM mytable;", 47 ExpectedSelect: []sql.Row{{int64(3), "third row"}}, 48 }, 49 { 50 WriteQuery: "DELETE FROM mytable WHERE i > 1;", 51 ExpectedWriteResult: []sql.Row{{types.NewOkResult(2)}}, 52 SelectQuery: "SELECT * FROM mytable;", 53 ExpectedSelect: []sql.Row{{int64(1), "first row"}}, 54 }, 55 { 56 WriteQuery: "DELETE FROM mytable WHERE i <= 2;", 57 ExpectedWriteResult: []sql.Row{{types.NewOkResult(2)}}, 58 SelectQuery: "SELECT * FROM mytable;", 59 ExpectedSelect: []sql.Row{{int64(3), "third row"}}, 60 }, 61 { 62 WriteQuery: "DELETE FROM mytable WHERE i >= 2;", 63 ExpectedWriteResult: []sql.Row{{types.NewOkResult(2)}}, 64 SelectQuery: "SELECT * FROM mytable;", 65 ExpectedSelect: []sql.Row{{int64(1), "first row"}}, 66 }, 67 { 68 WriteQuery: "DELETE FROM mytable WHERE s = 'first row';", 69 ExpectedWriteResult: []sql.Row{{types.NewOkResult(1)}}, 70 SelectQuery: "SELECT * FROM mytable;", 71 ExpectedSelect: []sql.Row{{int64(2), "second row"}, {int64(3), "third row"}}, 72 }, 73 { 74 WriteQuery: "DELETE FROM mytable WHERE s <> 'dne';", 75 ExpectedWriteResult: []sql.Row{{types.NewOkResult(3)}}, 76 SelectQuery: "SELECT * FROM mytable;", 77 ExpectedSelect: nil, 78 }, 79 { 80 WriteQuery: "DELETE FROM mytable WHERE i in (2,3);", 81 ExpectedWriteResult: []sql.Row{{types.NewOkResult(2)}}, 82 SelectQuery: "SELECT * FROM mytable;", 83 ExpectedSelect: []sql.Row{{int64(1), "first row"}}, 84 }, 85 { 86 WriteQuery: "DELETE FROM mytable WHERE s LIKE '%row';", 87 ExpectedWriteResult: []sql.Row{{types.NewOkResult(3)}}, 88 SelectQuery: "SELECT * FROM mytable;", 89 ExpectedSelect: nil, 90 }, 91 { 92 WriteQuery: "DELETE FROM mytable WHERE s = 'dne';", 93 ExpectedWriteResult: []sql.Row{{types.NewOkResult(0)}}, 94 SelectQuery: "SELECT * FROM mytable;", 95 ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(2), "second row"}, {int64(3), "third row"}}, 96 }, 97 { 98 WriteQuery: "DELETE FROM mytable ORDER BY i ASC LIMIT 2;", 99 ExpectedWriteResult: []sql.Row{{types.NewOkResult(2)}}, 100 SelectQuery: "SELECT * FROM mytable;", 101 ExpectedSelect: []sql.Row{{int64(3), "third row"}}, 102 }, 103 { 104 WriteQuery: "DELETE FROM mytable ORDER BY i DESC LIMIT 1;", 105 ExpectedWriteResult: []sql.Row{{types.NewOkResult(1)}}, 106 SelectQuery: "SELECT * FROM mytable;", 107 ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(2), "second row"}}, 108 }, 109 { 110 WriteQuery: "DELETE FROM mytable ORDER BY i DESC LIMIT 1 OFFSET 1;", 111 ExpectedWriteResult: []sql.Row{{types.NewOkResult(1)}}, 112 SelectQuery: "SELECT * FROM mytable;", 113 ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(3), "third row"}}, 114 }, 115 { 116 WriteQuery: "DELETE FROM mytable WHERE (i,s) = (1, 'first row');", 117 ExpectedWriteResult: []sql.Row{{types.NewOkResult(1)}}, 118 SelectQuery: "SELECT * FROM mytable;", 119 ExpectedSelect: []sql.Row{{int64(2), "second row"}, {int64(3), "third row"}}, 120 }, 121 { 122 WriteQuery: `DELETE FROM tabletest where 's' = 'something'`, 123 ExpectedWriteResult: []sql.Row{{types.OkResult{RowsAffected: 0}}}, 124 SelectQuery: "SELECT * FROM mytable;", 125 ExpectedSelect: []sql.Row{{int64(1), "first row"}, {int64(2), "second row"}, {int64(3), "third row"}}, 126 }, 127 { 128 WriteQuery: "with t (n) as (select (1) from dual) delete from mytable where i in (select n from t)", 129 ExpectedWriteResult: []sql.Row{{types.OkResult{RowsAffected: 1}}}, 130 SelectQuery: "select * from mytable order by i", 131 ExpectedSelect: []sql.Row{ 132 sql.NewRow(2, "second row"), 133 sql.NewRow(3, "third row"), 134 }, 135 }, 136 { 137 WriteQuery: "with recursive t (n) as (select (1) from dual union all select n + 1 from t where n < 2) delete from mytable where i in (select n from t)", 138 ExpectedWriteResult: []sql.Row{{types.OkResult{RowsAffected: 2}}}, 139 SelectQuery: "select * from mytable order by i", 140 ExpectedSelect: []sql.Row{ 141 sql.NewRow(3, "third row"), 142 }, 143 }, 144 } 145 146 // DeleteJoinTests contains tests for deletes that explicitly list the table from which 147 // to delete, and whose source may contain joined table relations. 148 var DeleteJoinTests = []WriteQueryTest{ 149 { 150 WriteQuery: "DELETE mytable FROM mytable join tabletest where mytable.i=tabletest.i;", 151 ExpectedWriteResult: []sql.Row{{types.NewOkResult(3)}}, 152 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 153 ExpectedSelect: []sql.Row{{0, 3}}, 154 }, 155 { 156 WriteQuery: "DELETE MYTABLE FROM mytAble join tAbletest where mytable.i=tabletest.i;", 157 ExpectedWriteResult: []sql.Row{{types.NewOkResult(3)}}, 158 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 159 ExpectedSelect: []sql.Row{{0, 3}}, 160 }, 161 { 162 WriteQuery: "DELETE tabletest FROM mytable join tabletest where mytable.i=tabletest.i;", 163 ExpectedWriteResult: []sql.Row{{types.NewOkResult(3)}}, 164 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 165 ExpectedSelect: []sql.Row{{3, 0}}, 166 }, 167 { 168 WriteQuery: "DELETE t1 FROM mytable as t1 join tabletest where t1.i=tabletest.i;", 169 ExpectedWriteResult: []sql.Row{{types.NewOkResult(3)}}, 170 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 171 ExpectedSelect: []sql.Row{{0, 3}}, 172 }, 173 { 174 WriteQuery: "DELETE mytable, tabletest FROM mytable join tabletest where mytable.i=tabletest.i;", 175 ExpectedWriteResult: []sql.Row{{types.NewOkResult(3)}}, 176 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 177 ExpectedSelect: []sql.Row{{0, 0}}, 178 }, 179 { 180 WriteQuery: "DELETE MYTABLE, TABLETEST FROM mytable join tabletest where mytable.i=tabletest.i;", 181 ExpectedWriteResult: []sql.Row{{types.NewOkResult(3)}}, 182 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 183 ExpectedSelect: []sql.Row{{0, 0}}, 184 }, 185 { 186 WriteQuery: "DELETE mytable FROM mytable;", 187 ExpectedWriteResult: []sql.Row{{types.NewOkResult(3)}}, 188 SelectQuery: "SELECT count(*) FROM mytable;", 189 ExpectedSelect: []sql.Row{{0}}, 190 }, 191 { 192 WriteQuery: "DELETE mytable FROM mytable WHERE i > 9999;", 193 ExpectedWriteResult: []sql.Row{{types.NewOkResult(0)}}, 194 SelectQuery: "SELECT count(*) FROM mytable;", 195 ExpectedSelect: []sql.Row{{3}}, 196 }, 197 { 198 WriteQuery: "DELETE FROM mytable USING mytable inner join tabletest on mytable.i=tabletest.i;", 199 ExpectedWriteResult: []sql.Row{{types.NewOkResult(3)}}, 200 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 201 ExpectedSelect: []sql.Row{{0, 3}}, 202 }, 203 { 204 WriteQuery: "DELETE FROM tabletest USING mytable inner join tabletest on mytable.i=tabletest.i;", 205 ExpectedWriteResult: []sql.Row{{types.NewOkResult(3)}}, 206 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 207 ExpectedSelect: []sql.Row{{3, 0}}, 208 }, 209 { 210 WriteQuery: "DELETE FROM mytable, tabletest USING mytable inner join tabletest on mytable.i=tabletest.i;", 211 ExpectedWriteResult: []sql.Row{{types.NewOkResult(3)}}, 212 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 213 ExpectedSelect: []sql.Row{{0, 0}}, 214 }, 215 { 216 WriteQuery: "DELETE mytable FROM mytable join tabletest where mytable.i=tabletest.i and mytable.i = 2;", 217 ExpectedWriteResult: []sql.Row{{types.NewOkResult(1)}}, 218 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 219 ExpectedSelect: []sql.Row{{2, 3}}, 220 }, 221 { 222 WriteQuery: "DELETE mytable, tabletest FROM mytable join tabletest where mytable.i=tabletest.i and mytable.i = 2;", 223 ExpectedWriteResult: []sql.Row{{types.NewOkResult(1)}}, 224 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 225 ExpectedSelect: []sql.Row{{2, 2}}, 226 }, 227 { 228 WriteQuery: "DELETE tabletest, mytable FROM mytable join tabletest where mytable.i=tabletest.i and mytable.i = 2;", 229 ExpectedWriteResult: []sql.Row{{types.NewOkResult(1)}}, 230 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 231 ExpectedSelect: []sql.Row{{2, 2}}, 232 }, 233 { 234 WriteQuery: "DELETE mytable FROM mytable join (select 1 as i union all select 2 as i) dt where mytable.i=dt.i;", 235 ExpectedWriteResult: []sql.Row{{types.NewOkResult(2)}}, 236 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 237 ExpectedSelect: []sql.Row{{1, 3}}, 238 }, 239 { 240 WriteQuery: "with t (n) as (select (1) from dual) delete mytable from mytable join tabletest where mytable.i=tabletest.i and mytable.i in (select n from t)", 241 ExpectedWriteResult: []sql.Row{{types.OkResult{RowsAffected: 1}}}, 242 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 243 ExpectedSelect: []sql.Row{{2, 3}}, 244 }, 245 { 246 WriteQuery: "with t (n) as (select (1) from dual) delete mytable, tabletest from mytable join tabletest where mytable.i=tabletest.i and mytable.i in (select n from t)", 247 ExpectedWriteResult: []sql.Row{{types.OkResult{RowsAffected: 1}}}, 248 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 249 ExpectedSelect: []sql.Row{{2, 2}}, 250 }, 251 { 252 // Single target table, join with table function 253 WriteQuery: "DELETE mytable FROM mytable join tabletest on mytable.i=tabletest.i join JSON_TABLE('[{\"x\": 1},{\"x\": 2}]', '$[*]' COLUMNS (x INT PATH '$.x')) as jt on jt.x=mytable.i;", 254 ExpectedWriteResult: []sql.Row{{types.NewOkResult(2)}}, 255 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 256 ExpectedSelect: []sql.Row{{1, 3}}, 257 }, 258 { 259 // Multiple target tables, join with table function 260 WriteQuery: "DELETE mytable, tabletest FROM mytable join tabletest on mytable.i=tabletest.i join JSON_TABLE('[{\"x\": 1},{\"x\": 2}]', '$[*]' COLUMNS (x INT PATH '$.x')) as jt on jt.x=mytable.i;", 261 ExpectedWriteResult: []sql.Row{{types.NewOkResult(2)}}, 262 SelectQuery: "SELECT (select count(*) FROM mytable), (SELECT count(*) from tabletest);", 263 ExpectedSelect: []sql.Row{{1, 1}}, 264 }, 265 } 266 267 var SpatialDeleteTests = []WriteQueryTest{ 268 { 269 WriteQuery: "DELETE FROM point_table;", 270 ExpectedWriteResult: []sql.Row{{types.NewOkResult(1)}}, 271 SelectQuery: "SELECT * FROM point_table;", 272 ExpectedSelect: nil, 273 }, 274 { 275 WriteQuery: "DELETE FROM line_table;", 276 ExpectedWriteResult: []sql.Row{{types.NewOkResult(2)}}, 277 SelectQuery: "SELECT * FROM line_table;", 278 ExpectedSelect: nil, 279 }, 280 { 281 WriteQuery: "DELETE FROM polygon_table;", 282 ExpectedWriteResult: []sql.Row{{types.NewOkResult(2)}}, 283 SelectQuery: "SELECT * FROM polygon_table;", 284 ExpectedSelect: nil, 285 }, 286 } 287 288 var DeleteErrorTests = []ScriptTest{ 289 { 290 Name: "DELETE FROM error cases", 291 Assertions: []ScriptTestAssertion{ 292 { 293 // unknown table 294 Query: "DELETE FROM invalidtable WHERE x < 1;", 295 ExpectedErrStr: "table not found: invalidtable", 296 }, 297 { 298 // invalid column 299 Query: "DELETE FROM mytable WHERE z = 'dne';", 300 ExpectedErrStr: "column \"z\" could not be found in any table in scope", 301 }, 302 { 303 // missing binding 304 Query: "DELETE FROM mytable WHERE i = ?;", 305 ExpectedErrStr: "unbound variable \"v1\" in query", 306 }, 307 { 308 // negative limit 309 Query: "DELETE FROM mytable LIMIT -1;", 310 ExpectedErrStr: "syntax error at position 28 near 'LIMIT'", 311 }, 312 { 313 // negative offset 314 Query: "DELETE FROM mytable LIMIT 1 OFFSET -1;", 315 ExpectedErrStr: "syntax error at position 37 near 'OFFSET'", 316 }, 317 { 318 // missing keyword from 319 Query: "DELETE mytable WHERE i = 1;", 320 ExpectedErrStr: "syntax error at position 21 near 'WHERE'", 321 }, 322 { 323 // targets subquery alias 324 Query: "DELETE FROM (SELECT * FROM mytable) mytable WHERE i = 1;", 325 ExpectedErrStr: "syntax error at position 14 near 'FROM'", 326 }, 327 }, 328 }, 329 { 330 Name: "DELETE FROM JOIN error cases", 331 Assertions: []ScriptTestAssertion{ 332 { 333 // targeting tables in multiple databases 334 Query: "DELETE mydb.mytable, test.other FROM mydb.mytable inner join test.other on mydb.mytable.i=test.other.pk;", 335 ExpectedErrStr: "multiple databases specified as delete from targets", 336 }, 337 { 338 // unknown table in delete join 339 Query: "DELETE unknowntable FROM mytable WHERE i < 1;", 340 ExpectedErrStr: "table not found: unknowntable", 341 }, 342 { 343 // invalid table in delete join 344 Query: "DELETE tabletest FROM mytable WHERE i < 1;", 345 ExpectedErrStr: "table \"tabletest\" not found in DELETE FROM sources", 346 }, 347 { 348 // repeated table in delete join 349 Query: "DELETE mytable, mytable FROM mytable WHERE i < 1;", 350 ExpectedErrStr: "duplicate tables specified as delete from targets", 351 }, 352 { 353 // targets join with no explicit target tables 354 Query: "DELETE FROM mytable one, mytable two WHERE one.i = 1;", 355 ExpectedErrStr: "syntax error at position 24 near 'one'", 356 }, 357 { 358 // targets table function alias 359 Query: "DELETE jt FROM mytable join tabletest on mytable.i=tabletest.i join JSON_TABLE('[{\"x\": 1},{\"x\": 2}]', '$[*]' COLUMNS (x INT PATH '$.x')) as jt on jt.x=mytable.i;", 360 ExpectedErrStr: "target table jt of the DELETE is not updatable", 361 }, 362 { 363 // targets valid table and table function alias 364 Query: "DELETE mytable, jt FROM mytable join tabletest on mytable.i=tabletest.i join JSON_TABLE('[{\"x\": 1},{\"x\": 2}]', '$[*]' COLUMNS (x INT PATH '$.x')) as jt on jt.x=mytable.i;", 365 ExpectedErrStr: "target table jt of the DELETE is not updatable", 366 }, 367 }, 368 }, 369 }