github.com/dolthub/go-mysql-server@v0.18.0/enginetest/queries/script_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 "math" 19 "time" 20 21 "github.com/dolthub/vitess/go/sqltypes" 22 querypb "github.com/dolthub/vitess/go/vt/proto/query" 23 "gopkg.in/src-d/go-errors.v1" 24 25 gmstime "github.com/dolthub/go-mysql-server/internal/time" 26 "github.com/dolthub/go-mysql-server/sql" 27 "github.com/dolthub/go-mysql-server/sql/analyzer/analyzererrors" 28 "github.com/dolthub/go-mysql-server/sql/plan" 29 "github.com/dolthub/go-mysql-server/sql/planbuilder" 30 "github.com/dolthub/go-mysql-server/sql/types" 31 ) 32 33 type ScriptTest struct { 34 // Name of the script test 35 Name string 36 // The sql statements to execute as setup, in order. Results are not checked, but statements must not error. 37 SetUpScript []string 38 // The set of assertions to make after setup, in order 39 Assertions []ScriptTestAssertion 40 // For tests that make a single assertion, Query can be set for the single assertion 41 Query string 42 // For tests that make a single assertion, Expected can be set for the single assertion 43 Expected []sql.Row 44 // For tests that make a single assertion, ExpectedErr can be set for the expected error 45 ExpectedErr *errors.Kind 46 // For tests that make a single assertion, ExpectedIndexes can be set for the string representation of indexes that we expect to appear in the query plan 47 ExpectedIndexes []string 48 // For tests that perform join operations, JoinTypes can be set for the type of merge we expect to perform. 49 JoinTypes []plan.JoinType 50 // SkipPrepared is true when we skip a test for prepared statements only 51 SkipPrepared bool 52 } 53 54 type ScriptTestAssertion struct { 55 Query string 56 Expected []sql.Row 57 ExpectedErr *errors.Kind 58 // ExpectedErrStr should be set for tests that expect a specific error string this is not linked to a custom error. 59 // In most cases, errors should be linked to a custom error, however there are exceptions where this is not possible, 60 // such as the use of the SIGNAL statement. 61 ExpectedErrStr string 62 63 // ExpectedWarning contains the expected warning code when a query generates warnings but not errors. 64 ExpectedWarning int 65 66 // ExpectedWarningsCount is used to test the expected number of warnings generated by a query. 67 // The ExpectedWarning field must be set for warning counts to be checked. 68 ExpectedWarningsCount int 69 70 // ExpectedWarningMessageSubstring is used to test the contents of warning messages generated by a 71 // query. The ExpectedWarning field must be set for warning messages to be checked. 72 ExpectedWarningMessageSubstring string 73 74 // ExpectedColumns indicates the Name and Type of the columns expected; no other schema fields are tested. 75 ExpectedColumns sql.Schema 76 77 // The string representation of indexes that we expect to appear in the query plan 78 ExpectedIndexes []string 79 80 // For tests that perform join operations, JoinTypes can be set for the type of merge we expect to perform. 81 JoinTypes []plan.JoinType 82 83 // NewSession instructs the test framework that this assertion requires a new session to be created before the 84 // query is executed. This is generally only needed when a test script is testing functionality that invalidates 85 // a session and prevents additional queries from being executed on the session. 86 NewSession bool 87 88 // SkipResultsCheck is used to skip assertions on expected Rows returned from a query. This should be used 89 // sparingly, such as in cases where you only want to test warning messages. 90 SkipResultsCheck bool 91 92 // Skip is used to completely skip a test, not execute its query at all, and record it as a skipped test 93 // in the test suite results. 94 Skip bool 95 96 // SkipResultCheckOnServerEngine is used when the result of over the wire test does not match the result from the engine test. 97 // It should be fixed in the future. 98 SkipResultCheckOnServerEngine bool 99 100 // Bindings are variable mappings only used for prepared tests 101 Bindings map[string]*querypb.BindVariable 102 103 // CheckIndexedAccess indicates whether we should verify the query plan uses an index 104 CheckIndexedAccess bool 105 } 106 107 // ScriptTests are a set of test scripts to run. 108 // Unlike other engine tests, ScriptTests must be self-contained. No other tables are created outside the definition of 109 // the tests. 110 var ScriptTests = []ScriptTest{ 111 { 112 Name: "filter pushdown through join uppercase name", 113 SetUpScript: []string{ 114 "create table A (A int primary key);", 115 "insert into A values (0),(1)", 116 }, 117 Assertions: []ScriptTestAssertion{ 118 { 119 Query: "select /*+ JOIN_ORDER(A, b) */ * from A join A b where a.A = 1 and b.A = 1", 120 ExpectedIndexes: []string{"primary", "primary"}, 121 }, 122 }, 123 }, 124 { 125 Name: "GMS issue 2349", 126 SetUpScript: []string{ 127 "CREATE TABLE table1 (id int NOT NULL AUTO_INCREMENT primary key, name text)", 128 ` 129 CREATE TABLE table2 ( 130 id int NOT NULL AUTO_INCREMENT, 131 name text, 132 fk int, 133 PRIMARY KEY (id), 134 CONSTRAINT myConstraint FOREIGN KEY (fk) REFERENCES table1 (id) 135 )`, 136 }, 137 Assertions: []ScriptTestAssertion{ 138 { 139 Query: "INSERT INTO table1 (name) VALUES ('tbl1 row 1');", 140 Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 1}}}, 141 }, 142 { 143 Query: "INSERT INTO table1 (name) VALUES ('tbl1 row 2');", 144 Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 2}}}, 145 }, 146 }, 147 }, 148 { 149 Name: "missing indexes", 150 SetUpScript: []string{ 151 ` 152 create table t ( 153 id varchar(500), 154 from_ varchar(500), 155 to_ varchar(500), 156 key (to_, from_), 157 Primary key (id, from_, to_) 158 );`, 159 }, 160 Assertions: []ScriptTestAssertion{ 161 { 162 Query: "select * from t where to_ = 'L1' and from_ = 'L2'", 163 Expected: []sql.Row{}, 164 ExpectedIndexes: []string{"to_from_"}, 165 }, 166 { 167 Query: "select * from t where BIN_TO_UUID(id) = '0' and to_ = 'L1' and from_ = 'L2'", 168 Expected: []sql.Row{}, 169 ExpectedIndexes: []string{"to_from_"}, 170 }, 171 }, 172 }, 173 { 174 Name: "correctness test indexes", 175 SetUpScript: []string{ 176 ` 177 CREATE TABLE tab3 ( 178 pk int NOT NULL, 179 col0 int, 180 col1 float, 181 col2 text, 182 col3 int, 183 col4 float, 184 col5 text, 185 PRIMARY KEY (pk), 186 KEY idx_tab3_0 (col1), 187 UNIQUE KEY idx_tab3_1 (col0), 188 UNIQUE KEY idx_tab3_4 (col3,col4) 189 )`, 190 "insert into tab3 values (1 , 101 , 83.86, 'pgprm', 50 , 58.56, 'nugdy')", 191 }, 192 Assertions: []ScriptTestAssertion{ 193 { 194 Query: "select count(*) from tab3 WHERE (80 < col0 AND (((col0 BETWEEN 87 AND 9 OR (((col0 IS NULL)))))) AND (71.70 <= col1 OR 94 <= col0 AND ((66 > col0) OR (85 = col0 AND ((42.15 >= col1))) OR 30 = col0)));", 195 Expected: []sql.Row{{0}}, 196 }, 197 }, 198 }, 199 { 200 Name: "update exponential parsing", 201 SetUpScript: []string{ 202 "create table a (a int primary key, b double);", 203 "insert into a values (0, 0.0),(1, 1.0)", 204 "update a set b = 5.0E-5 where a = 0", 205 "update a set b = 5.0e-5 where a = 1", 206 }, 207 Assertions: []ScriptTestAssertion{ 208 { 209 Query: "select * from a", 210 Expected: []sql.Row{{0, .00005}, {1, .00005}}, 211 }, 212 }, 213 }, 214 { 215 Name: "set op schema merge", 216 SetUpScript: []string{ 217 "create table `left` (i int primary key, j mediumint, k varchar(20));", 218 "create table `right` (i int primary key, j bigint, k text);", 219 "insert into `left` values (1,2, 'a')", 220 "insert into `right` values (3,4, 'b')", 221 222 "create table t1 (i int);", 223 "insert into t1 values (1), (2), (3);", 224 "create table t2 (i int);", 225 "insert into t2 values (1), (3);", 226 "create table t3 (j int);", 227 "insert into t3 values (1), (3);", 228 }, 229 Assertions: []ScriptTestAssertion{ 230 { 231 Query: "select i, j from `left` union select i, j from `right`", 232 ExpectedColumns: sql.Schema{ 233 { 234 Name: "i", 235 Type: types.Int32, 236 }, 237 { 238 Name: "j", 239 Type: types.Int64, 240 }, 241 }, 242 Expected: []sql.Row{{1, 2}, {3, 4}}, 243 }, 244 { 245 Query: "select i, k from `left` union select i, k from `right`", 246 ExpectedColumns: sql.Schema{ 247 { 248 Name: "i", 249 Type: types.Int32, 250 }, 251 { 252 Name: "k", 253 Type: types.LongText, 254 }, 255 }, 256 Expected: []sql.Row{{1, "a"}, {3, "b"}}, 257 }, 258 { 259 Query: "select i, j, k from `left` union select i, j, k from `right`", 260 ExpectedColumns: sql.Schema{ 261 { 262 Name: "i", 263 Type: types.Int32, 264 }, 265 { 266 Name: "j", 267 Type: types.Int64, 268 }, 269 { 270 Name: "k", 271 Type: types.LongText, 272 }, 273 }, 274 Expected: []sql.Row{{1, 2, "a"}, {3, 4, "b"}}, 275 }, 276 { 277 Query: "select i, k from `left` union select i, j, k from `right`", 278 ExpectedErr: planbuilder.ErrUnionSchemasDifferentLength, 279 }, 280 { 281 Query: "table t1 union table t2 order by i;", 282 ExpectedColumns: sql.Schema{ 283 { 284 Name: "i", 285 Type: types.Int32, 286 }, 287 }, 288 Expected: []sql.Row{ 289 {1}, 290 {2}, 291 {3}, 292 }, 293 }, 294 { 295 Query: "table t1 union table t2 order by i;", 296 ExpectedColumns: sql.Schema{ 297 { 298 Name: "i", 299 Type: types.Int32, 300 }, 301 }, 302 Expected: []sql.Row{ 303 {1}, 304 {2}, 305 {3}, 306 }, 307 }, 308 { 309 Query: "table t3 union table t1 order by j;", 310 ExpectedColumns: sql.Schema{ 311 { 312 Name: "j", 313 Type: types.Int32, 314 }, 315 }, 316 Expected: []sql.Row{ 317 {1}, 318 {2}, 319 {3}, 320 }, 321 }, 322 { 323 Query: "select j as i from t3 union table t1 order by i;", 324 ExpectedColumns: sql.Schema{ 325 { 326 Name: "i", 327 Type: types.Int32, 328 }, 329 }, 330 Expected: []sql.Row{ 331 {1}, 332 {2}, 333 {3}, 334 }, 335 }, 336 { 337 Query: "table t1 union table t2 order by 1;", 338 ExpectedColumns: sql.Schema{ 339 { 340 Name: "i", 341 Type: types.Int32, 342 }, 343 }, 344 Expected: []sql.Row{ 345 {1}, 346 {2}, 347 {3}, 348 }, 349 }, 350 { 351 Query: "table t1 union table t3 order by 1;", 352 ExpectedColumns: sql.Schema{ 353 { 354 Name: "i", 355 Type: types.Int32, 356 }, 357 }, 358 Expected: []sql.Row{ 359 {1}, 360 {2}, 361 {3}, 362 }, 363 }, 364 { 365 // This looks wrong, but it actually matches MySQL 366 Query: "table t1 union select i as j from t2 order by i;", 367 ExpectedColumns: sql.Schema{ 368 { 369 Name: "i", 370 Type: types.Int32, 371 }, 372 }, 373 Expected: []sql.Row{ 374 {1}, 375 {2}, 376 {3}, 377 }, 378 }, 379 { 380 Query: "table t1 union table t3 order by j;", 381 ExpectedErr: sql.ErrColumnNotFound, 382 }, 383 { 384 Query: "table t1 union table t2 order by t1.i;", 385 ExpectedErr: planbuilder.ErrQualifiedOrderBy, 386 }, 387 { 388 Query: "table t1 union table t2 order by t2.i;", 389 ExpectedErr: planbuilder.ErrQualifiedOrderBy, 390 }, 391 { 392 Query: "table t1 union table t3 order by t3.i;", 393 ExpectedErr: planbuilder.ErrQualifiedOrderBy, 394 }, 395 { 396 Query: "table t1 union table t3 order by t3.j;", 397 ExpectedErr: planbuilder.ErrQualifiedOrderBy, 398 }, 399 { 400 Query: "table t1 union table t3 order by t1.j;", 401 ExpectedErr: planbuilder.ErrQualifiedOrderBy, 402 }, 403 }, 404 }, 405 { 406 Name: "intersection and except tests", 407 SetUpScript: []string{ 408 "create table a (m int, n int);", 409 "insert into a values (1,2), (2,3), (3,4);", 410 "create table b (m int, n int);", 411 "insert into b values (1,2), (1,3), (3,4);", 412 "create table c (m int, n int);", 413 "insert into c values (1,3), (1,3), (3,4);", 414 "create table t1 (i int);", 415 "insert into t1 values (1), (2), (3);", 416 "create table t2 (i float);", 417 "insert into t2 values (1.0), (1.99), (3.0);", 418 "create table l (i int);", 419 "insert into l values (1), (1), (1);", 420 "create table r (i int);", 421 "insert into r values (1);", 422 "create table x (i int);", 423 "insert into x values (1), (2), (3);", 424 "create table y (i bigint);", 425 "insert into y values (1), (3);", 426 }, 427 Assertions: []ScriptTestAssertion{ 428 // Intersect tests 429 { 430 Query: "table a intersect table b order by m, n;", 431 Expected: []sql.Row{ 432 {1, 2}, 433 {3, 4}, 434 }, 435 }, 436 { 437 Query: "table a intersect table c order by m, n;", 438 Expected: []sql.Row{ 439 {3, 4}, 440 }, 441 }, 442 { 443 Query: "table c intersect distinct table c order by m, n;", 444 Expected: []sql.Row{ 445 {1, 3}, 446 {3, 4}, 447 }, 448 }, 449 { 450 Query: "table c intersect all table c order by m, n;", 451 Expected: []sql.Row{ 452 {1, 3}, 453 {1, 3}, 454 {3, 4}, 455 }, 456 }, 457 { 458 Query: "table a intersect table b intersect table c;", 459 Expected: []sql.Row{ 460 {3, 4}, 461 }, 462 }, 463 { 464 Query: "(table b order by m limit 1 offset 1) intersect (table c order by m limit 1);", 465 Expected: []sql.Row{ 466 {1, 3}, 467 }, 468 }, 469 { 470 Query: "table x intersect table y order by i;", 471 Expected: []sql.Row{ 472 {1}, 473 {3}, 474 }, 475 }, 476 { 477 Query: "table x intersect table y order by 1;", 478 Expected: []sql.Row{ 479 {1}, 480 {3}, 481 }, 482 }, 483 { 484 // Resulting type is string for some reason 485 Skip: true, 486 Query: "table t1 intersect table t2;", 487 Expected: []sql.Row{ 488 {1}, 489 {3}, 490 }, 491 }, 492 493 // Except tests 494 { 495 Query: "table a except table b order by m, n;", 496 Expected: []sql.Row{ 497 {2, 3}, 498 }, 499 }, 500 { 501 Query: "table a except table c order by m, n;", 502 Expected: []sql.Row{ 503 {1, 2}, 504 {2, 3}, 505 }, 506 }, 507 { 508 Query: "table b except table c order by m, n;", 509 Expected: []sql.Row{ 510 {1, 2}, 511 }, 512 }, 513 { 514 Query: "table c except distinct table a order by m, n;", 515 Expected: []sql.Row{ 516 {1, 3}, 517 }, 518 }, 519 { 520 Query: "table c except all table a order by m, n;", 521 Expected: []sql.Row{ 522 {1, 3}, 523 {1, 3}, 524 }, 525 }, 526 { 527 Query: "(table a order by m limit 1 offset 1) except (table c order by m limit 1);", 528 Expected: []sql.Row{ 529 {2, 3}, 530 }, 531 }, 532 { 533 Query: "table a except table b except table c;", 534 Expected: []sql.Row{ 535 {2, 3}, 536 }, 537 }, 538 { 539 Query: "table x except table y order by i;", 540 Expected: []sql.Row{ 541 {2}, 542 }, 543 }, 544 { 545 Query: "table l except table r;", 546 Expected: []sql.Row{}, 547 }, 548 { 549 Query: "table l except distinct table r;", 550 Expected: []sql.Row{}, 551 }, 552 { 553 Query: "table l except all table r;", 554 Expected: []sql.Row{ 555 {1}, 556 {1}, 557 }, 558 }, 559 560 // Multiple set operation tests 561 { 562 Query: "table a except table b intersect table c order by m;", 563 Expected: []sql.Row{ 564 {1, 2}, 565 {2, 3}, 566 }, 567 }, 568 { 569 Query: "table a intersect table b except table c order by m;", 570 Expected: []sql.Row{ 571 {1, 2}, 572 }, 573 }, 574 { 575 Query: "table a union table a intersect table b except table c order by m;", 576 Expected: []sql.Row{ 577 {1, 2}, 578 {2, 3}, 579 }, 580 }, 581 582 // CTE tests 583 { 584 Query: "with cte as (table a union table a intersect table b except table c order by m) select * from cte", 585 Expected: []sql.Row{ 586 {1, 2}, 587 {2, 3}, 588 }, 589 }, 590 { 591 Query: "with recursive cte(x) as (select 1) select * from cte", 592 Expected: []sql.Row{ 593 {1}, 594 }, 595 }, 596 { 597 Query: "with recursive cte (x,y) as (select 1, 1 intersect select 1, 1 union select x + 1, y + 2 from cte where x < 5) select * from cte;", 598 Expected: []sql.Row{ 599 {1, 1}, 600 {2, 3}, 601 {3, 5}, 602 {4, 7}, 603 {5, 9}, 604 }, 605 }, 606 { 607 Query: "WITH RECURSIVE\n" + 608 " rt (foo) AS (\n" + 609 " SELECT 1 as foo\n" + 610 " UNION ALL\n" + 611 " SELECT foo + 1 as foo FROM rt WHERE foo < 5\n" + 612 " ),\n" + 613 " ladder (depth, foo) AS (\n" + 614 " SELECT 1 as depth, NULL as foo from rt\n" + 615 " UNION ALL\n" + 616 " SELECT ladder.depth + 1 as depth, rt.foo\n" + 617 " FROM ladder JOIN rt WHERE ladder.foo = rt.foo\n" + 618 " )\n" + 619 "SELECT * FROM ladder;", 620 Expected: []sql.Row{ 621 {1, nil}, 622 {1, nil}, 623 {1, nil}, 624 {1, nil}, 625 {1, nil}, 626 }, 627 }, 628 { 629 Query: "with recursive cte (x,y) as (select 1, 1 intersect select 1, 1 intersect select x + 1, y + 2 from cte where x < 5) select * from cte;", 630 ExpectedErr: sql.ErrRecursiveCTEMissingUnion, 631 }, 632 { 633 Query: "with recursive cte (x,y) as (select 1, 1 union select 1, 1 intersect select x + 1, y + 2 from cte where x < 5) select * from cte;", 634 ExpectedErr: sql.ErrRecursiveCTENotUnion, 635 }, 636 }, 637 }, 638 { 639 Name: "create table casing", 640 SetUpScript: []string{ 641 "create table t (lower varchar(20) primary key, UPPER varchar(20), MiXeD varchar(20), un_der varchar(20), `da-sh` varchar(20));", 642 "insert into t values ('a','b','c','d','e')", 643 }, 644 Assertions: []ScriptTestAssertion{ 645 { 646 Query: `select * from t`, 647 ExpectedColumns: sql.Schema{ 648 { 649 Name: "lower", 650 Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 20), 651 }, 652 { 653 Name: "UPPER", 654 Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 20), 655 }, 656 { 657 Name: "MiXeD", 658 Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 20), 659 }, 660 { 661 Name: "un_der", 662 Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 20), 663 }, 664 { 665 Name: "da-sh", 666 Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 20), 667 }, 668 }, 669 Expected: []sql.Row{{"a", "b", "c", "d", "e"}}, 670 }, 671 }, 672 }, 673 { 674 Name: "alter table out of range value error of column type change", 675 SetUpScript: []string{ 676 "create table t (i int primary key, i2 int, key(i2));", 677 "insert into t values (0,-1)", 678 }, 679 Assertions: []ScriptTestAssertion{ 680 { 681 Query: `alter table t modify column i2 int unsigned`, 682 ExpectedErr: sql.ErrValueOutOfRange, 683 }, 684 }, 685 }, 686 { 687 Name: "alter keyless table", 688 SetUpScript: []string{ 689 "create table t (c1 int, c2 varchar(200), c3 enum('one', 'two'));", 690 "insert into t values (1, 'one', NULL);", 691 }, 692 Assertions: []ScriptTestAssertion{ 693 { 694 Query: `alter table t modify column c1 int unsigned`, 695 Expected: []sql.Row{{types.NewOkResult(0)}}, 696 }, 697 { 698 Query: "describe t;", 699 Expected: []sql.Row{ 700 {"c1", "int unsigned", "YES", "", "NULL", ""}, 701 {"c2", "varchar(200)", "YES", "", "NULL", ""}, 702 {"c3", "enum('one','two')", "YES", "", "NULL", ""}, 703 }, 704 }, 705 { 706 Query: `alter table t drop column c1;`, 707 Expected: []sql.Row{{types.NewOkResult(0)}}, 708 }, 709 { 710 Query: "describe t;", 711 Expected: []sql.Row{ 712 {"c2", "varchar(200)", "YES", "", "NULL", ""}, 713 {"c3", "enum('one','two')", "YES", "", "NULL", ""}, 714 }, 715 }, 716 { 717 Query: "alter table t add column new3 int;", 718 Expected: []sql.Row{{types.NewOkResult(0)}}, 719 }, 720 { 721 Query: `insert into t values ('two', 'two', -2);`, 722 Expected: []sql.Row{{types.NewOkResult(1)}}, 723 }, 724 { 725 Query: "describe t;", 726 Expected: []sql.Row{ 727 {"c2", "varchar(200)", "YES", "", "NULL", ""}, 728 {"c3", "enum('one','two')", "YES", "", "NULL", ""}, 729 {"new3", "int", "YES", "", "NULL", ""}, 730 }, 731 }, 732 { 733 Query: "select * from t;", 734 Expected: []sql.Row{{"one", nil, nil}, {"two", "two", -2}}, 735 }, 736 }, 737 }, 738 { 739 Name: "topN stable output", 740 SetUpScript: []string{ 741 "create table xy (x int primary key, y int)", 742 "insert into xy values (1,0),(2,0),(3,0),(4,0)", 743 }, 744 Assertions: []ScriptTestAssertion{ 745 { 746 Query: "select * from xy order by y asc limit 1", 747 Expected: []sql.Row{{1, 0}}, 748 }, 749 { 750 Query: "select * from xy order by y asc limit 1 offset 1", 751 Expected: []sql.Row{{2, 0}}, 752 }, 753 { 754 Query: "select * from xy order by y asc limit 1 offset 2", 755 Expected: []sql.Row{{3, 0}}, 756 }, 757 { 758 Query: "select * from xy order by y asc limit 1 offset 3", 759 Expected: []sql.Row{{4, 0}}, 760 }, 761 { 762 Query: "(select * from xy order by y asc limit 1 offset 1) union (select * from xy order by y asc limit 1 offset 2)", 763 Expected: []sql.Row{{2, 0}, {3, 0}}, 764 }, 765 { 766 Query: "with recursive cte as ((select * from xy order by y asc limit 1 offset 1) union (select * from xy order by y asc limit 1 offset 2)) select * from cte", 767 Expected: []sql.Row{{2, 0}, {3, 0}}, 768 }, 769 }, 770 }, 771 { 772 Name: "enums with default, case-sensitive collation (utf8mb4_0900_bin)", 773 SetUpScript: []string{ 774 "CREATE TABLE enumtest1 (pk int primary key, e enum('abc', 'XYZ'));", 775 "CREATE TABLE enumtest2 (pk int PRIMARY KEY, e enum('x ', 'X ', 'y', 'Y'));", 776 }, 777 Assertions: []ScriptTestAssertion{ 778 { 779 Query: "INSERT INTO enumtest1 VALUES (1, 'abc'), (2, 'abc'), (3, 'XYZ');", 780 Expected: []sql.Row{{types.NewOkResult(3)}}, 781 }, 782 { 783 Query: "SELECT * FROM enumtest1;", 784 Expected: []sql.Row{{1, "abc"}, {2, "abc"}, {3, "XYZ"}}, 785 }, 786 { 787 // enum values must match EXACTLY for case-sensitive collations 788 Query: "INSERT INTO enumtest1 VALUES (10, 'ABC'), (11, 'aBc'), (12, 'xyz');", 789 ExpectedErrStr: "value ABC is not valid for this Enum", 790 }, 791 { 792 Query: "SHOW CREATE TABLE enumtest1;", 793 Expected: []sql.Row{{ 794 "enumtest1", 795 "CREATE TABLE `enumtest1` (\n `pk` int NOT NULL,\n `e` enum('abc','XYZ'),\n PRIMARY KEY (`pk`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 796 }, 797 { 798 // Trailing whitespace should be removed from enum values, except when using the "binary" charset and collation 799 Query: "SHOW CREATE TABLE enumtest2;", 800 Expected: []sql.Row{{ 801 "enumtest2", 802 "CREATE TABLE `enumtest2` (\n `pk` int NOT NULL,\n `e` enum('x','X','y','Y'),\n PRIMARY KEY (`pk`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 803 }, 804 { 805 Query: "DESCRIBE enumtest1;", 806 Expected: []sql.Row{ 807 {"pk", "int", "NO", "PRI", "NULL", ""}, 808 {"e", "enum('abc','XYZ')", "YES", "", "NULL", ""}}, 809 }, 810 { 811 Query: "DESCRIBE enumtest2;", 812 Expected: []sql.Row{ 813 {"pk", "int", "NO", "PRI", "NULL", ""}, 814 {"e", "enum('x','X','y','Y')", "YES", "", "NULL", ""}}, 815 }, 816 { 817 Query: "select data_type, column_type from information_schema.columns where table_name='enumtest1' and column_name='e';", 818 Expected: []sql.Row{{"enum", "enum('abc','XYZ')"}}, 819 }, 820 { 821 Query: "select data_type, column_type from information_schema.columns where table_name='enumtest2' and column_name='e';", 822 Expected: []sql.Row{{"enum", "enum('x','X','y','Y')"}}, 823 }, 824 }, 825 }, 826 { 827 Name: "enums with case-insensitive collation (utf8mb4_0900_ai_ci)", 828 SetUpScript: []string{ 829 "CREATE TABLE enumtest1 (pk int primary key, e enum('abc', 'XYZ') collate utf8mb4_0900_ai_ci);", 830 }, 831 Assertions: []ScriptTestAssertion{ 832 { 833 Query: "INSERT INTO enumtest1 VALUES (1, 'abc'), (2, 'abc'), (3, 'XYZ');", 834 Expected: []sql.Row{{types.NewOkResult(3)}}, 835 }, 836 { 837 Query: "SHOW CREATE TABLE enumtest1;", 838 Expected: []sql.Row{{ 839 "enumtest1", 840 "CREATE TABLE `enumtest1` (\n `pk` int NOT NULL,\n `e` enum('abc','XYZ') COLLATE utf8mb4_0900_ai_ci,\n PRIMARY KEY (`pk`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 841 }, 842 { 843 Query: "DESCRIBE enumtest1;", 844 Expected: []sql.Row{ 845 {"pk", "int", "NO", "PRI", "NULL", ""}, 846 {"e", "enum('abc','XYZ') COLLATE utf8mb4_0900_ai_ci", "YES", "", "NULL", ""}}, 847 }, 848 { 849 Query: "select data_type, column_type from information_schema.columns where table_name='enumtest1' and column_name='e';", 850 Expected: []sql.Row{{"enum", "enum('abc','XYZ')"}}, 851 }, 852 { 853 Query: "CREATE TABLE enumtest2 (pk int PRIMARY KEY, e enum('x ', 'X ', 'y', 'Y'));", 854 Expected: []sql.Row{{types.NewOkResult(0)}}, 855 }, 856 { 857 Query: "INSERT INTO enumtest1 VALUES (10, 'ABC'), (11, 'aBc'), (12, 'xyz');", 858 Expected: []sql.Row{{types.NewOkResult(3)}}, 859 }, 860 }, 861 }, 862 { 863 Name: "failed statements data validation for INSERT, UPDATE", 864 SetUpScript: []string{ 865 "CREATE TABLE test (pk BIGINT PRIMARY KEY, v1 BIGINT, INDEX (v1));", 866 "INSERT INTO test VALUES (1,1), (4,4), (5,5);", 867 }, 868 Assertions: []ScriptTestAssertion{ 869 { 870 Query: "INSERT INTO test VALUES (2,2), (3,3), (1,1);", 871 ExpectedErrStr: "duplicate primary key given: [1]", 872 }, 873 { 874 Query: "SELECT * FROM test;", 875 Expected: []sql.Row{{1, 1}, {4, 4}, {5, 5}}, 876 }, 877 { 878 Query: "UPDATE test SET pk = pk + 1 ORDER BY pk;", 879 ExpectedErrStr: "duplicate primary key given: [5]", 880 }, 881 { 882 Query: "SELECT * FROM test;", 883 Expected: []sql.Row{{1, 1}, {4, 4}, {5, 5}}, 884 }, 885 }, 886 }, 887 { 888 Name: "failed statements data validation for DELETE, REPLACE", 889 SetUpScript: []string{ 890 "CREATE TABLE test (pk BIGINT PRIMARY KEY, v1 BIGINT, INDEX (v1));", 891 "INSERT INTO test VALUES (1,1), (4,4), (5,5);", 892 "CREATE TABLE test2 (pk BIGINT PRIMARY KEY, CONSTRAINT fk_test FOREIGN KEY (pk) REFERENCES test (v1));", 893 "INSERT INTO test2 VALUES (4);", 894 }, 895 Assertions: []ScriptTestAssertion{ 896 { 897 Query: "DELETE FROM test WHERE pk > 0;", 898 ExpectedErr: sql.ErrForeignKeyParentViolation, 899 }, 900 { 901 Query: "SELECT * FROM test;", 902 Expected: []sql.Row{{1, 1}, {4, 4}, {5, 5}}, 903 }, 904 { 905 Query: "SELECT * FROM test2;", 906 Expected: []sql.Row{{4}}, 907 }, 908 { 909 Query: "REPLACE INTO test VALUES (1,7), (4,8), (5,9);", 910 ExpectedErr: sql.ErrForeignKeyParentViolation, 911 }, 912 { 913 Query: "SELECT * FROM test;", 914 Expected: []sql.Row{{1, 1}, {4, 4}, {5, 5}}, 915 }, 916 { 917 Query: "SELECT * FROM test2;", 918 Expected: []sql.Row{{4}}, 919 }, 920 }, 921 }, 922 { 923 Name: "delete with in clause", 924 SetUpScript: []string{ 925 "create table a (x int primary key)", 926 "insert into a values (1), (3), (5)", 927 "delete from a where x in (1, 3)", 928 }, 929 Query: "select x from a order by 1", 930 Expected: []sql.Row{ 931 {5}, 932 }, 933 }, 934 { 935 Name: "sqllogictest evidence/slt_lang_aggfunc.test", 936 SetUpScript: []string{ 937 "CREATE TABLE t1( x INTEGER, y VARCHAR(8) )", 938 "INSERT INTO t1 VALUES(1,'true')", 939 "INSERT INTO t1 VALUES(0,'false')", 940 "INSERT INTO t1 VALUES(NULL,'NULL')", 941 }, 942 Query: "SELECT count(DISTINCT x) FROM t1", 943 Expected: []sql.Row{ 944 {2}, 945 }, 946 }, 947 { 948 Name: "sqllogictest index/commute/10/slt_good_1.test", 949 SetUpScript: []string{ 950 "CREATE TABLE tab0(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT)", 951 "INSERT INTO tab0 VALUES(0,42,58.92,'fnbtk',54,68.41,'xmttf')", 952 "INSERT INTO tab0 VALUES(1,31,46.55,'sksjf',46,53.20,'wiuva')", 953 "INSERT INTO tab0 VALUES(2,30,31.11,'oldqn',41,5.26,'ulaay')", 954 "INSERT INTO tab0 VALUES(3,77,44.90,'pmsir',70,84.14,'vcmyo')", 955 "INSERT INTO tab0 VALUES(4,23,95.26,'qcwxh',32,48.53,'rvtbr')", 956 "INSERT INTO tab0 VALUES(5,43,6.75,'snvwg',3,14.38,'gnfxz')", 957 "INSERT INTO tab0 VALUES(6,47,98.26,'bzzva',60,15.2,'imzeq')", 958 "INSERT INTO tab0 VALUES(7,98,40.9,'lsrpi',78,66.30,'ephwy')", 959 "INSERT INTO tab0 VALUES(8,19,15.16,'ycvjz',55,38.70,'dnkkz')", 960 "INSERT INTO tab0 VALUES(9,7,84.4,'ptovf',17,2.46,'hrxsf')", 961 "CREATE TABLE tab1(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT)", 962 "CREATE INDEX idx_tab1_0 on tab1 (col0)", 963 "CREATE INDEX idx_tab1_1 on tab1 (col1)", 964 "CREATE INDEX idx_tab1_3 on tab1 (col3)", 965 "CREATE INDEX idx_tab1_4 on tab1 (col4)", 966 "INSERT INTO tab1 SELECT * FROM tab0", 967 "CREATE TABLE tab2(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT)", 968 "CREATE UNIQUE INDEX idx_tab2_1 ON tab2 (col4 DESC,col3)", 969 "CREATE UNIQUE INDEX idx_tab2_2 ON tab2 (col3 DESC,col0)", 970 "CREATE UNIQUE INDEX idx_tab2_3 ON tab2 (col3 DESC,col1)", 971 "INSERT INTO tab2 SELECT * FROM tab0", 972 "CREATE TABLE tab3(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT)", 973 "CREATE INDEX idx_tab3_0 ON tab3 (col3 DESC)", 974 "INSERT INTO tab3 SELECT * FROM tab0", 975 "CREATE TABLE tab4(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT)", 976 "CREATE INDEX idx_tab4_0 ON tab4 (col0 DESC)", 977 "CREATE UNIQUE INDEX idx_tab4_2 ON tab4 (col4 DESC,col3)", 978 "CREATE INDEX idx_tab4_3 ON tab4 (col3 DESC)", 979 "INSERT INTO tab4 SELECT * FROM tab0", 980 }, 981 Query: "SELECT pk FROM tab2 WHERE 78 < col0 AND 19 < col3", 982 Expected: []sql.Row{ 983 {7}, 984 }, 985 }, 986 { 987 Name: "3 tables, linear join", 988 SetUpScript: []string{ 989 "create table a (xa int primary key, ya int, za int)", 990 "create table b (xb int primary key, yb int, zb int)", 991 "create table c (xc int primary key, yc int, zc int)", 992 "insert into a values (1,2,3)", 993 "insert into b values (1,2,3)", 994 "insert into c values (1,2,3)", 995 }, 996 Assertions: []ScriptTestAssertion{ 997 { 998 Query: "select ya from a join b on ya - 1= xb join c on xc = zb - 2", 999 Expected: []sql.Row{{2}}, 1000 }, 1001 }, 1002 }, 1003 { 1004 Name: "3 tables, v join", 1005 SetUpScript: []string{ 1006 "create table a (xa int primary key, ya int, za int)", 1007 "create table b (xb int primary key, yb int, zb int)", 1008 "create table c (xc int primary key, yc int, zc int)", 1009 "insert into a values (1,2,3)", 1010 "insert into b values (1,2,3)", 1011 "insert into c values (1,2,3)", 1012 }, 1013 Assertions: []ScriptTestAssertion{ 1014 { 1015 Query: "select za from a join b on ya - 1 = xb join c on xa = xc", 1016 Expected: []sql.Row{{3}}, 1017 }, 1018 }, 1019 }, 1020 { 1021 Name: "3 tables, linear join, indexes on A,C", 1022 SetUpScript: []string{ 1023 "create table a (xa int primary key, ya int, za int)", 1024 "create table b (xb int primary key, yb int, zb int)", 1025 "create table c (xc int primary key, yc int, zc int)", 1026 "insert into a values (1,2,3)", 1027 "insert into b values (1,2,3)", 1028 "insert into c values (1,2,3)", 1029 }, 1030 Assertions: []ScriptTestAssertion{ 1031 { 1032 Query: "select xa from a join b on xa = yb - 1 join c on yb - 1 = xc", 1033 Expected: []sql.Row{{1}}, 1034 }, 1035 }, 1036 }, 1037 { 1038 Name: "4 tables, linear join", 1039 SetUpScript: []string{ 1040 "create table a (xa int primary key, ya int, za int)", 1041 "create table b (xb int primary key, yb int, zb int)", 1042 "create table c (xc int primary key, yc int, zc int)", 1043 "create table d (xd int primary key, yd int, zd int)", 1044 "insert into a values (1,2,3)", 1045 "insert into b values (1,2,3)", 1046 "insert into c values (1,2,3)", 1047 "insert into d values (1,2,3)", 1048 }, 1049 Assertions: []ScriptTestAssertion{ 1050 { 1051 Query: "select xa from a join b on ya - 1 = xb join c on xb = xc join d on xc = xd", 1052 Expected: []sql.Row{{1}}, 1053 }, 1054 }, 1055 }, 1056 { 1057 Name: "4 tables, linear join, index on D", 1058 SetUpScript: []string{ 1059 "create table a (xa int primary key, ya int, za int)", 1060 "create table b (xb int primary key, yb int, zb int)", 1061 "create table c (xc int primary key, yc int, zc int)", 1062 "create table d (xd int primary key, yd int, zd int)", 1063 "insert into a values (1,2,3)", 1064 "insert into b values (1,2,3)", 1065 "insert into c values (1,2,3)", 1066 "insert into d values (1,2,3)", 1067 }, 1068 Assertions: []ScriptTestAssertion{ 1069 { 1070 Query: "select xa from a join b on ya = yb join c on yb = yc join d on yc - 1 = xd", 1071 Expected: []sql.Row{{1}}, 1072 }, 1073 }, 1074 }, 1075 { 1076 Name: "4 tables, left join, indexes on all tables", 1077 SetUpScript: []string{ 1078 "create table a (xa int primary key, ya int, za int)", 1079 "create table b (xb int primary key, yb int, zb int)", 1080 "create table c (xc int primary key, yc int, zc int)", 1081 "create table d (xd int primary key, yd int, zd int)", 1082 "insert into a values (1,2,3)", 1083 "insert into b values (1,2,3)", 1084 "insert into c values (1,2,3)", 1085 "insert into d values (1,2,3)", 1086 }, 1087 Assertions: []ScriptTestAssertion{ 1088 { 1089 Query: "select xa from a left join b on ya = yb left join c on yb = yc left join d on yc - 1 = xd", 1090 Expected: []sql.Row{{1}}, 1091 }, 1092 }, 1093 }, 1094 { 1095 Name: "4 tables, linear join, index on B, D", 1096 SetUpScript: []string{ 1097 "create table a (xa int primary key, ya int, za int)", 1098 "create table b (xb int primary key, yb int, zb int)", 1099 "create table c (xc int primary key, yc int, zc int)", 1100 "create table d (xd int primary key, yd int, zd int)", 1101 "insert into a values (1,2,3)", 1102 "insert into b values (1,2,3)", 1103 "insert into c values (1,2,3)", 1104 "insert into d values (1,2,3)", 1105 }, 1106 Assertions: []ScriptTestAssertion{ 1107 { 1108 Query: "select xa from a join b on ya - 1 = xb join c on yc = za - 1 join d on yc - 1 = xd", 1109 Expected: []sql.Row{{1}}, 1110 }, 1111 }, 1112 }, 1113 { 1114 Name: "4 tables, all joined to A", 1115 SetUpScript: []string{ 1116 "create table a (xa int primary key, ya int, za int)", 1117 "create table b (xb int primary key, yb int, zb int)", 1118 "create table c (xc int primary key, yc int, zc int)", 1119 "create table d (xd int primary key, yd int, zd int)", 1120 "insert into a values (1,2,3)", 1121 "insert into b values (1,2,3)", 1122 "insert into c values (1,2,3)", 1123 "insert into d values (1,2,3)", 1124 }, 1125 Assertions: []ScriptTestAssertion{ 1126 { 1127 Query: "select xa from a join b on xa = xb join c on ya - 1 = xc join d on za - 2 = xd", 1128 Expected: []sql.Row{{1}}, 1129 }, 1130 }, 1131 }, 1132 // { 1133 // Name: "4 tables, all joined to D", 1134 // SetUpScript: []string{ 1135 // "create table a (xa int primary key, ya int, za int)", 1136 // "create table b (xb int primary key, yb int, zb int)", 1137 // "create table c (xc int primary key, yc int, zc int)", 1138 // "create table d (xd int primary key, yd int, zd int)", 1139 // "insert into a values (1,2,3)", 1140 // "insert into b values (1,2,3)", 1141 // "insert into c values (1,2,3)", 1142 // "insert into d values (1,2,3)", 1143 // }, 1144 // Assertions: []ScriptTestAssertion{ 1145 // { 1146 // // gives an error in mysql, a needs an alias 1147 // Query: "select xa from d join a on yd = xa join c on yd = xc join a on xa = yd", 1148 // Expected: []sql.Row{{1}}, 1149 // }, 1150 // }, 1151 // }, 1152 { 1153 Name: "4 tables, all joined to D", 1154 SetUpScript: []string{ 1155 "create table a (xa int primary key, ya int, za int)", 1156 "create table b (xb int primary key, yb int, zb int)", 1157 "create table c (xc int primary key, yc int, zc int)", 1158 "create table d (xd int primary key, yd int, zd int)", 1159 "insert into a values (1,2,3)", 1160 "insert into b values (1,2,3)", 1161 "insert into c values (1,2,3)", 1162 "insert into d values (1,2,3)", 1163 }, 1164 Assertions: []ScriptTestAssertion{ 1165 { 1166 Query: "select xa from d join a on yd - 1 = xa join c on zd - 2 = xc join b on xb = zd - 2", 1167 Expected: []sql.Row{{1}}, 1168 }, 1169 }, 1170 }, 1171 { 1172 Name: "5 tables, complex join conditions", 1173 SetUpScript: []string{ 1174 "create table a (xa int primary key, ya int, za int)", 1175 "create table b (xb int primary key, yb int, zb int)", 1176 "create table c (xc int primary key, yc int, zc int)", 1177 "create table d (xd int primary key, yd int, zd int)", 1178 "create table e (xe int, ye int, ze int, primary key(xe, ye))", 1179 "insert into a values (1,2,3)", 1180 "insert into b values (1,2,3)", 1181 "insert into c values (1,2,3)", 1182 "insert into d values (1,2,3)", 1183 "insert into e values (1,2,3)", 1184 }, 1185 Assertions: []ScriptTestAssertion{ 1186 { 1187 Query: `select xa from a 1188 join b on ya - 1 = xb 1189 join c on xc = za - 2 1190 join d on xd = yb - 1 1191 join e on xe = zb - 2 and ye = yc`, 1192 Expected: []sql.Row{{1}}, 1193 }, 1194 }, 1195 }, 1196 { 1197 Name: "UUIDs used in the wild.", 1198 SetUpScript: []string{ 1199 "SET @uuid = '6ccd780c-baba-1026-9564-5b8c656024db'", 1200 "SET @binuuid = '0011223344556677'", 1201 }, 1202 Assertions: []ScriptTestAssertion{ 1203 { 1204 Query: `SELECT IS_UUID(UUID())`, 1205 Expected: []sql.Row{{int8(1)}}, 1206 }, 1207 { 1208 Query: `SELECT IS_UUID(@uuid)`, 1209 Expected: []sql.Row{{int8(1)}}, 1210 }, 1211 { 1212 Query: `SELECT BIN_TO_UUID(UUID_TO_BIN(@uuid))`, 1213 Expected: []sql.Row{{"6ccd780c-baba-1026-9564-5b8c656024db"}}, 1214 }, 1215 { 1216 Query: `SELECT BIN_TO_UUID(UUID_TO_BIN(@uuid, 1), 1)`, 1217 Expected: []sql.Row{{"6ccd780c-baba-1026-9564-5b8c656024db"}}, 1218 }, 1219 { 1220 Query: `SELECT UUID_TO_BIN(NULL)`, 1221 Expected: []sql.Row{{nil}}, 1222 }, 1223 { 1224 Query: `SELECT HEX(UUID_TO_BIN(@uuid))`, 1225 Expected: []sql.Row{{"6CCD780CBABA102695645B8C656024DB"}}, 1226 }, 1227 { 1228 Query: `SELECT UUID_TO_BIN(123)`, 1229 ExpectedErr: sql.ErrUuidUnableToParse, 1230 }, 1231 { 1232 Query: `SELECT BIN_TO_UUID(123)`, 1233 ExpectedErr: sql.ErrUuidUnableToParse, 1234 }, 1235 { 1236 Query: `SELECT BIN_TO_UUID(X'00112233445566778899aabbccddeeff')`, 1237 Expected: []sql.Row{{"00112233-4455-6677-8899-aabbccddeeff"}}, 1238 }, 1239 { 1240 Query: `SELECT BIN_TO_UUID('0011223344556677')`, 1241 Expected: []sql.Row{{"30303131-3232-3333-3434-353536363737"}}, 1242 }, 1243 { 1244 Query: `SELECT BIN_TO_UUID(@binuuid)`, 1245 Expected: []sql.Row{{"30303131-3232-3333-3434-353536363737"}}, 1246 }, 1247 }, 1248 }, 1249 { 1250 Name: "Test cases on select into statement", 1251 SetUpScript: []string{ 1252 "SELECT * FROM (VALUES ROW(22,44,88)) AS t INTO @x,@y,@z", 1253 "CREATE TABLE tab1 (id int primary key, v1 int)", 1254 "INSERT INTO tab1 VALUES (1, 1), (2, 3), (3, 6)", 1255 "SELECT id FROM tab1 ORDER BY id DESC LIMIT 1 INTO @myVar", 1256 "CREATE TABLE tab2 (i2 int primary key, s text)", 1257 "INSERT INTO tab2 VALUES (1, 'b'), (2, 'm'), (3, 'g')", 1258 "SELECT m.id, t.s FROM tab1 m JOIN tab2 t on m.id = t.i2 ORDER BY t.s DESC LIMIT 1 INTO @myId, @myText", 1259 // TODO: union statement does not handle order by and limit clauses 1260 //"SELECT id FROM tab1 UNION select s FROM tab2 LIMIT 1 INTO @myUnion", 1261 "SELECT id FROM tab1 WHERE id > 3 UNION select s FROM tab2 WHERE s < 'f' INTO @mustSingleVar", 1262 }, 1263 Assertions: []ScriptTestAssertion{ 1264 { 1265 // SELECT INTO has an empty result schema 1266 // https://github.com/dolthub/dolt/issues/6105 1267 Query: `SELECT 1 INTO @abc`, 1268 Expected: []sql.Row{{}}, 1269 ExpectedColumns: nil, 1270 }, 1271 { 1272 Query: `SELECT @abc`, 1273 Expected: []sql.Row{{int8(1)}}, 1274 }, 1275 { 1276 Query: `SELECT @z, @x, @y`, 1277 Expected: []sql.Row{{88, 22, 44}}, 1278 }, 1279 { 1280 Query: `SELECT @myVar, @mustSingleVar`, 1281 Expected: []sql.Row{{3, "b"}}, 1282 }, 1283 { 1284 Query: `SELECT @myId, @myText, @myUnion`, 1285 Expected: []sql.Row{{2, "m", nil}}, 1286 }, 1287 { 1288 Query: `SELECT id FROM tab1 ORDER BY id DESC INTO @myvar`, 1289 ExpectedErr: sql.ErrMoreThanOneRow, 1290 }, 1291 { 1292 Query: `SELECT id INTO DUMPFILE 'baddump.out' FROM tab1 ORDER BY id DESC LIMIT 15`, 1293 ExpectedErr: sql.ErrMoreThanOneRow, 1294 }, 1295 { 1296 Query: `select 1, 2, 3 into @my1, @my2`, 1297 ExpectedErr: sql.ErrColumnNumberDoesNotMatch, 1298 }, 1299 { 1300 Query: `SELECT id, v1 INTO @myFirstVar FROM tab1 ORDER BY id DESC LIMIT 1 INTO @mySecondVar`, 1301 ExpectedErrStr: "Multiple INTO clauses in one query block at position 84 near '@mySecondVar'", 1302 }, 1303 { 1304 Query: `SELECT id FROM tab1 WHERE id > 3 UNION select s INTO @mustSingleVar FROM tab2 WHERE s < 'f' ORDER BY s DESC`, 1305 ExpectedErrStr: "INTO clause is not allowed at position 98 near 'ORDER'", 1306 }, 1307 }, 1308 }, 1309 { 1310 Name: "CrossDB Queries", 1311 SetUpScript: []string{ 1312 "CREATE DATABASE test", 1313 "CREATE TABLE test.x (pk int primary key)", 1314 "insert into test.x values (1),(2),(3)", 1315 "DELETE FROM test.x WHERE pk=2", 1316 "UPDATE test.x set pk=300 where pk=3", 1317 "create table a (xa int primary key, ya int, za int)", 1318 "insert into a values (1,2,3)", 1319 }, 1320 Assertions: []ScriptTestAssertion{ 1321 { 1322 Query: "SELECT pk from test.x", 1323 Expected: []sql.Row{{1}, {300}}, 1324 }, 1325 { 1326 Query: "SELECT * from a", 1327 Expected: []sql.Row{{1, 2, 3}}, 1328 }, 1329 }, 1330 }, 1331 { 1332 // All DECLARE statements are only allowed under BEGIN/END blocks 1333 Name: "Top-level DECLARE statements", 1334 Assertions: []ScriptTestAssertion{ 1335 { 1336 Query: "DECLARE no_such_table CONDITION FOR SQLSTATE '42S02'", 1337 ExpectedErr: sql.ErrSyntaxError, 1338 }, 1339 { 1340 Query: "DECLARE no_such_table CONDITION FOR 1051", 1341 ExpectedErr: sql.ErrSyntaxError, 1342 }, 1343 { 1344 Query: "DECLARE a CHAR(16)", 1345 ExpectedErr: sql.ErrSyntaxError, 1346 }, 1347 { 1348 Query: "DECLARE cur2 CURSOR FOR SELECT i FROM test.t2", 1349 ExpectedErr: sql.ErrSyntaxError, 1350 }, 1351 { 1352 Query: "DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE", 1353 ExpectedErr: sql.ErrSyntaxError, 1354 }, 1355 }, 1356 }, 1357 { 1358 Name: "last_insert_id() behavior", 1359 SetUpScript: []string{ 1360 "create table a (x int primary key auto_increment, y int)", 1361 "create table b (x int primary key)", 1362 }, 1363 Assertions: []ScriptTestAssertion{ 1364 { 1365 Query: "select last_insert_id()", 1366 Expected: []sql.Row{{uint64(0)}}, 1367 }, 1368 { 1369 Query: "insert into a (x,y) values (1,1)", 1370 Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 1}}}, 1371 }, 1372 { 1373 Query: "select last_insert_id()", 1374 Expected: []sql.Row{{uint64(0)}}, 1375 }, 1376 { 1377 Query: "insert into a (y) values (1)", 1378 Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 2}}}, 1379 }, 1380 { 1381 Query: "select last_insert_id()", 1382 Expected: []sql.Row{{uint64(2)}}, 1383 }, 1384 { 1385 Query: "insert into a (y) values (2), (3)", 1386 Expected: []sql.Row{{types.OkResult{RowsAffected: 2, InsertID: 3}}}, 1387 }, 1388 { 1389 // last_insert_id() should return the insert id of the *first* value inserted in the last statement 1390 Query: "select last_insert_id()", 1391 Expected: []sql.Row{{uint64(3)}}, 1392 }, 1393 { 1394 Query: "insert into b (x) values (1), (2)", 1395 Expected: []sql.Row{{types.NewOkResult(2)}}, 1396 }, 1397 { 1398 // The above query doesn't have an auto increment column, so last_insert_id is unchanged 1399 Query: "select last_insert_id()", 1400 Expected: []sql.Row{{uint64(3)}}, 1401 }, 1402 { 1403 Query: "insert into a (x, y) values (-100, 10)", 1404 Expected: []sql.Row{{types.OkResult{ 1405 RowsAffected: 1, 1406 InsertID: uint64(math.MaxUint64 - uint(100-1)), 1407 }}}, 1408 }, 1409 { 1410 // last_insert_id() should not update for manually inserted values 1411 Query: "select last_insert_id()", 1412 Expected: []sql.Row{{uint64(3)}}, 1413 }, 1414 { 1415 Query: "insert into a (x, y) values (100, 10)", 1416 Expected: []sql.Row{{types.OkResult{ 1417 RowsAffected: 1, 1418 InsertID: 100, 1419 }}}, 1420 }, 1421 { 1422 // last_insert_id() should not update for manually inserted values 1423 Query: "select last_insert_id()", 1424 Expected: []sql.Row{{uint64(3)}}, 1425 }, 1426 }, 1427 }, 1428 { 1429 Name: "last_insert_id(expr) behavior", 1430 SetUpScript: []string{ 1431 "create table a (x int primary key auto_increment, y int)", 1432 }, 1433 Assertions: []ScriptTestAssertion{ 1434 { 1435 Query: "insert into a (y) values (1)", 1436 Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 1}}}, 1437 }, 1438 { 1439 Query: "select last_insert_id()", 1440 Expected: []sql.Row{{uint64(1)}}, 1441 }, 1442 { 1443 Query: "insert into a (x, y) values (1, 1) on duplicate key update y = 2, x=last_insert_id(x)", 1444 Expected: []sql.Row{{types.OkResult{RowsAffected: 2, InsertID: 1}}}, 1445 }, 1446 { 1447 Query: "select * from a order by x", 1448 Expected: []sql.Row{{1, 2}}, 1449 }, 1450 { 1451 Query: "select last_insert_id()", 1452 Expected: []sql.Row{{uint64(1)}}, 1453 }, 1454 { 1455 Query: "insert into a (y) values (100)", 1456 Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 2}}}, 1457 }, 1458 { 1459 Query: "select last_insert_id()", 1460 Expected: []sql.Row{{uint64(2)}}, 1461 }, 1462 }, 1463 }, 1464 { 1465 Name: "row_count() behavior", 1466 SetUpScript: []string{ 1467 "create table b (x int primary key)", 1468 "insert into b values (1), (2), (3), (4)", 1469 }, 1470 Assertions: []ScriptTestAssertion{ 1471 { 1472 Query: "select row_count()", 1473 Expected: []sql.Row{{4}}, 1474 }, 1475 { 1476 Query: "replace into b values (1)", 1477 Expected: []sql.Row{{types.NewOkResult(2)}}, 1478 }, 1479 { 1480 Query: "select row_count()", 1481 Expected: []sql.Row{{2}}, 1482 }, 1483 { 1484 Query: "select row_count()", 1485 Expected: []sql.Row{{-1}}, 1486 }, 1487 { 1488 Query: "select count(*) from b", 1489 Expected: []sql.Row{{4}}, 1490 }, 1491 { 1492 Query: "select row_count()", 1493 Expected: []sql.Row{{-1}}, 1494 }, 1495 { 1496 Query: "update b set x = x + 10 where x <> 2", 1497 Expected: []sql.Row{{types.OkResult{ 1498 RowsAffected: 3, 1499 Info: plan.UpdateInfo{ 1500 Matched: 3, 1501 Updated: 3, 1502 }, 1503 }}}, 1504 }, 1505 { 1506 Query: "select row_count()", 1507 Expected: []sql.Row{{3}}, 1508 }, 1509 { 1510 Query: "select row_count()", 1511 Expected: []sql.Row{{-1}}, 1512 }, 1513 { 1514 Query: "delete from b where x <> 2", 1515 Expected: []sql.Row{{types.NewOkResult(3)}}, 1516 }, 1517 { 1518 Query: "select row_count()", 1519 Expected: []sql.Row{{3}}, 1520 }, 1521 { 1522 Query: "select row_count()", 1523 Expected: []sql.Row{{-1}}, 1524 }, 1525 { 1526 Query: "alter table b add column y int null", 1527 Expected: []sql.Row{{types.NewOkResult(0)}}, 1528 }, 1529 { 1530 Query: "select row_count()", 1531 Expected: []sql.Row{{0}}, 1532 }, 1533 { 1534 Query: "select row_count()", 1535 Expected: []sql.Row{{-1}}, 1536 }, 1537 }, 1538 }, 1539 { 1540 Name: "same alias names for result column name and alias table column name", 1541 SetUpScript: []string{ 1542 "CREATE TABLE tab0(col0 INTEGER, col1 INTEGER, col2 INTEGER)", 1543 "INSERT INTO tab0 VALUES(83,0,38)", 1544 "INSERT INTO tab0 VALUES(26,0,79)", 1545 "INSERT INTO tab0 VALUES(43,81,24)", 1546 }, 1547 Assertions: []ScriptTestAssertion{ 1548 { 1549 Query: "SELECT + 13 AS col0 FROM tab0 GROUP BY tab0.col0", 1550 Expected: []sql.Row{{13}, {13}, {13}}, 1551 }, 1552 { 1553 Query: "SELECT 82 col1 FROM tab0 AS cor0 GROUP BY cor0.col1", 1554 Expected: []sql.Row{{82}, {82}}, 1555 }, 1556 { 1557 Query: "SELECT - cor0.col2 * - col2 AS col1 FROM tab0 AS cor0 GROUP BY col2, cor0.col1", 1558 Expected: []sql.Row{{1444}, {6241}, {576}}, 1559 }, 1560 { 1561 Query: "SELECT ALL + 40 col1 FROM tab0 AS cor0 GROUP BY cor0.col1", 1562 Expected: []sql.Row{{40}, {40}}, 1563 }, 1564 { 1565 Query: "SELECT DISTINCT - cor0.col1 col1 FROM tab0 AS cor0 GROUP BY cor0.col1, cor0.col2", 1566 Expected: []sql.Row{{-81}, {0}}, 1567 }, 1568 { 1569 Query: "SELECT DISTINCT ( cor0.col0 ) - col0 AS col2 FROM tab0 AS cor0 GROUP BY cor0.col2, cor0.col0, cor0.col0", 1570 Expected: []sql.Row{{0}}, 1571 }, 1572 }, 1573 }, 1574 { 1575 Name: "found_rows() behavior", 1576 SetUpScript: []string{ 1577 "create table b (x int primary key)", 1578 "insert into b values (1), (2), (3), (4)", 1579 }, 1580 Assertions: []ScriptTestAssertion{ 1581 { 1582 Query: "select * from b where x < 2", 1583 Expected: []sql.Row{{1}}, 1584 }, 1585 { 1586 Query: "select found_rows()", 1587 Expected: []sql.Row{{1}}, 1588 }, 1589 { 1590 Query: "select * from b", 1591 Expected: []sql.Row{{1}, {2}, {3}, {4}}, 1592 }, 1593 { 1594 Query: "select found_rows()", 1595 Expected: []sql.Row{{4}}, 1596 }, 1597 { 1598 Query: "select found_rows()", 1599 Expected: []sql.Row{{1}}, 1600 }, 1601 { 1602 Query: "select * from b order by x limit 3", 1603 Expected: []sql.Row{{1}, {2}, {3}}, 1604 }, 1605 { 1606 Query: "select found_rows()", 1607 Expected: []sql.Row{{3}}, 1608 }, 1609 { 1610 Query: "select found_rows()", 1611 Expected: []sql.Row{{1}}, 1612 }, 1613 { 1614 Query: "select * from b order by x limit 100", 1615 Expected: []sql.Row{{1}, {2}, {3}, {4}}, 1616 }, 1617 { 1618 Query: "select found_rows()", 1619 Expected: []sql.Row{{4}}, 1620 }, 1621 { 1622 Query: "select found_rows()", 1623 Expected: []sql.Row{{1}}, 1624 }, 1625 { 1626 Query: "select sql_calc_found_rows * from b order by x limit 3", 1627 Expected: []sql.Row{{1}, {2}, {3}}, 1628 }, 1629 { 1630 Query: "select found_rows()", 1631 Expected: []sql.Row{{4}}, 1632 }, 1633 { 1634 Query: "select found_rows()", 1635 Expected: []sql.Row{{1}}, 1636 }, 1637 { 1638 Query: "select sql_calc_found_rows * from b where x <= 2 order by x limit 1", 1639 Expected: []sql.Row{{1}}, 1640 }, 1641 { 1642 Query: "select found_rows()", 1643 Expected: []sql.Row{{2}}, 1644 }, 1645 { 1646 Query: "select sql_calc_found_rows * from b where x <= 2 order by x limit 1", 1647 Expected: []sql.Row{{1}}, 1648 }, 1649 { 1650 Query: "insert into b values (10), (11), (12), (13)", 1651 Expected: []sql.Row{{types.NewOkResult(4)}}, 1652 }, 1653 { 1654 Query: "select found_rows()", 1655 Expected: []sql.Row{{2}}, 1656 }, 1657 { 1658 Query: "update b set x = x where x < 40", 1659 Expected: []sql.Row{{types.OkResult{RowsAffected: 0, InsertID: 0, Info: plan.UpdateInfo{Matched: 8}}}}, 1660 }, 1661 { 1662 Query: "select found_rows()", 1663 Expected: []sql.Row{{8}}, 1664 }, 1665 { 1666 Query: "update b set x = x where x > 10", 1667 Expected: []sql.Row{{types.OkResult{RowsAffected: 0, InsertID: 0, Info: plan.UpdateInfo{Matched: 3}}}}, 1668 }, 1669 { 1670 Query: "select found_rows()", 1671 Expected: []sql.Row{{3}}, 1672 }, 1673 }, 1674 }, 1675 { 1676 Name: "INSERT INTO ... SELECT with AUTO_INCREMENT", 1677 SetUpScript: []string{ 1678 "create table ai (pk int primary key auto_increment, c0 int);", 1679 "create table other (pk int primary key);", 1680 "insert into other values (1), (2), (3)", 1681 "insert into ai (c0) select * from other order by other.pk;", 1682 }, 1683 Query: "select * from ai;", 1684 Expected: []sql.Row{ 1685 {1, 1}, 1686 {2, 2}, 1687 {3, 3}, 1688 }, 1689 }, 1690 { 1691 Name: "Indexed Join On Keyless Table", 1692 SetUpScript: []string{ 1693 "create table l (pk int primary key, c0 int, c1 int);", 1694 "create table r (c0 int, c1 int, third int);", 1695 "create index r_c0 on r (c0);", 1696 "create index r_c1 on r (c1);", 1697 "create index r_third on r (third);", 1698 "insert into l values (0, 0, 0), (1, 0, 1), (2, 1, 0), (3, 0, 2), (4, 2, 0), (5, 1, 2), (6, 2, 1), (7, 2, 2);", 1699 "insert into l values (256, 1024, 4096);", 1700 "insert into r values (1, 1, -1), (2, 2, -1), (2, 2, -1);", 1701 "insert into r values (-1, -1, 256);", 1702 }, 1703 Assertions: []ScriptTestAssertion{ 1704 { 1705 Query: "select pk, l.c0, l.c1 from l join r on l.c0 = r.c0 or l.c1 = r.c1 order by 1, 2, 3;", 1706 Expected: []sql.Row{ 1707 {1, 0, 1}, 1708 {2, 1, 0}, 1709 {3, 0, 2}, 1710 {3, 0, 2}, 1711 {4, 2, 0}, 1712 {4, 2, 0}, 1713 {5, 1, 2}, 1714 {5, 1, 2}, 1715 {5, 1, 2}, 1716 {6, 2, 1}, 1717 {6, 2, 1}, 1718 {6, 2, 1}, 1719 {7, 2, 2}, 1720 {7, 2, 2}, 1721 }, 1722 }, 1723 { 1724 Query: "select pk, l.c0, l.c1 from l join r on l.c0 = r.c0 or l.c1 = r.c1 or l.pk = r.third order by 1, 2, 3;", 1725 Expected: []sql.Row{ 1726 {1, 0, 1}, 1727 {2, 1, 0}, 1728 {3, 0, 2}, 1729 {3, 0, 2}, 1730 {4, 2, 0}, 1731 {4, 2, 0}, 1732 {5, 1, 2}, 1733 {5, 1, 2}, 1734 {5, 1, 2}, 1735 {6, 2, 1}, 1736 {6, 2, 1}, 1737 {6, 2, 1}, 1738 {7, 2, 2}, 1739 {7, 2, 2}, 1740 {256, 1024, 4096}, 1741 }, 1742 }, 1743 { 1744 Query: "select pk, l.c0, l.c1 from l join r on l.c0 = r.c0 or l.c1 < 4 and l.c1 = r.c1 or l.c1 >= 4 and l.c1 = r.c1 order by 1, 2, 3;", 1745 Expected: []sql.Row{ 1746 {1, 0, 1}, 1747 {2, 1, 0}, 1748 {3, 0, 2}, 1749 {3, 0, 2}, 1750 {4, 2, 0}, 1751 {4, 2, 0}, 1752 {5, 1, 2}, 1753 {5, 1, 2}, 1754 {5, 1, 2}, 1755 {6, 2, 1}, 1756 {6, 2, 1}, 1757 {6, 2, 1}, 1758 {7, 2, 2}, 1759 {7, 2, 2}, 1760 }, 1761 }, 1762 }, 1763 }, 1764 { 1765 Name: "Group Concat Queries", 1766 SetUpScript: []string{ 1767 "CREATE TABLE x (pk int)", 1768 "INSERT INTO x VALUES (1),(2),(3),(4),(NULL)", 1769 1770 "create table t (o_id int, attribute longtext, value longtext)", 1771 "INSERT INTO t VALUES (2, 'color', 'red'), (2, 'fabric', 'silk')", 1772 "INSERT INTO t VALUES (3, 'color', 'green'), (3, 'shape', 'square')", 1773 1774 "create table nulls(pk int)", 1775 "INSERT INTO nulls VALUES (NULL)", 1776 }, 1777 Assertions: []ScriptTestAssertion{ 1778 { 1779 Query: `SELECT group_concat(pk ORDER BY pk) FROM x;`, 1780 Expected: []sql.Row{{"1,2,3,4"}}, 1781 }, 1782 { 1783 Query: `SELECT group_concat(DISTINCT pk ORDER BY pk) FROM x;`, 1784 Expected: []sql.Row{{"1,2,3,4"}}, 1785 }, 1786 { 1787 Query: `SELECT group_concat(DISTINCT pk ORDER BY pk SEPARATOR '-') FROM x;`, 1788 Expected: []sql.Row{{"1-2-3-4"}}, 1789 }, 1790 { 1791 Query: "SELECT group_concat(`attribute` ORDER BY `attribute`) FROM t group by o_id order by o_id asc", 1792 Expected: []sql.Row{{"color,fabric"}, {"color,shape"}}, 1793 }, 1794 { 1795 Query: "SELECT group_concat(DISTINCT `attribute` ORDER BY value DESC SEPARATOR ';') FROM t group by o_id order by o_id asc", 1796 Expected: []sql.Row{{"fabric;color"}, {"shape;color"}}, 1797 }, 1798 { 1799 Query: "SELECT group_concat(DISTINCT `attribute` ORDER BY `attribute`) FROM t", 1800 Expected: []sql.Row{{"color,fabric,shape"}}, 1801 }, 1802 { 1803 Query: "SELECT group_concat(`attribute` ORDER BY `attribute`) FROM t", 1804 Expected: []sql.Row{{"color,color,fabric,shape"}}, 1805 }, 1806 { 1807 Query: `SELECT group_concat((SELECT 2)) FROM x;`, 1808 Expected: []sql.Row{{"2,2,2,2,2"}}, 1809 }, 1810 { 1811 Query: `SELECT group_concat(DISTINCT (SELECT 2)) FROM x;`, 1812 Expected: []sql.Row{{"2"}}, 1813 }, 1814 { 1815 Query: "SELECT group_concat(DISTINCT `attribute` ORDER BY `attribute` ASC) FROM t", 1816 Expected: []sql.Row{{"color,fabric,shape"}}, 1817 }, 1818 { 1819 Query: "SELECT group_concat(DISTINCT `attribute` ORDER BY `attribute` DESC) FROM t", 1820 Expected: []sql.Row{{"shape,fabric,color"}}, 1821 }, 1822 { 1823 Query: `SELECT group_concat(pk) FROM nulls`, 1824 Expected: []sql.Row{{nil}}, 1825 }, 1826 { 1827 Query: `SELECT group_concat((SELECT * FROM t LIMIT 1)) from t`, 1828 ExpectedErr: sql.ErrInvalidOperandColumns, 1829 }, 1830 { 1831 Query: `SELECT group_concat((SELECT * FROM x)) from t`, 1832 ExpectedErr: sql.ErrExpectedSingleRow, 1833 }, 1834 { 1835 Query: "SELECT group_concat(`attribute`) FROM t where o_id=2 order by attribute", 1836 Expected: []sql.Row{{"color,fabric"}}, 1837 }, 1838 { 1839 Query: "SELECT group_concat(DISTINCT `attribute` ORDER BY value DESC SEPARATOR ';') FROM t group by o_id order by o_id asc", 1840 Expected: []sql.Row{{"fabric;color"}, {"shape;color"}}, 1841 }, 1842 { 1843 Query: "SELECT group_concat(o_id order by o_id) FROM t WHERE `attribute`='color' order by o_id", 1844 Expected: []sql.Row{{"2,3"}}, 1845 }, 1846 { 1847 Query: "SELECT group_concat(attribute separator '') FROM t WHERE o_id=2 ORDER BY attribute", 1848 Expected: []sql.Row{{"colorfabric"}}, 1849 }, 1850 }, 1851 }, 1852 { 1853 Name: "CONVERT USING still converts between incompatible character sets", 1854 SetUpScript: []string{ 1855 "CREATE TABLE test (pk BIGINT PRIMARY KEY, v1 VARCHAR(200)) COLLATE=utf8mb4_0900_ai_ci;", 1856 "INSERT INTO test VALUES (1, '63273াম'), (2, 'GHD30r'), (3, '8জ্রিয277'), (4, NULL);", 1857 }, 1858 Assertions: []ScriptTestAssertion{ 1859 { 1860 Query: "SELECT pk, v1, CONVERT(CONVERT(v1 USING latin1) USING utf8mb4) AS round_trip FROM test WHERE v1 <> CONVERT(CONVERT(v1 USING latin1) USING utf8mb4);", 1861 Expected: []sql.Row{{int64(1), "63273াম", "63273??"}, {int64(3), "8জ্রিয277", "8?????277"}}, 1862 }, 1863 }, 1864 }, 1865 { 1866 Name: "ALTER TABLE, ALTER COLUMN SET , DROP DEFAULT", 1867 SetUpScript: []string{ 1868 "CREATE TABLE test (pk BIGINT PRIMARY KEY, v1 BIGINT NOT NULL DEFAULT 88);", 1869 }, 1870 Assertions: []ScriptTestAssertion{ 1871 { 1872 Query: "INSERT INTO test (pk) VALUES (1);", 1873 Expected: []sql.Row{{types.NewOkResult(1)}}, 1874 }, 1875 { 1876 Query: "SELECT * FROM test;", 1877 Expected: []sql.Row{{1, 88}}, 1878 }, 1879 { 1880 Query: "ALTER TABLE test ALTER v1 SET DEFAULT (CONVERT('42', SIGNED));", 1881 Expected: []sql.Row{{types.NewOkResult(0)}}, 1882 }, 1883 { 1884 Query: "INSERT INTO test (pk) VALUES (2);", 1885 Expected: []sql.Row{{types.NewOkResult(1)}}, 1886 }, 1887 { 1888 Query: "SELECT * FROM test;", 1889 Expected: []sql.Row{{1, 88}, {2, 42}}, 1890 }, 1891 { 1892 Query: "ALTER TABLE test ALTER v2 SET DEFAULT 1;", 1893 ExpectedErr: sql.ErrTableColumnNotFound, 1894 }, 1895 { 1896 Query: "ALTER TABLE test ALTER v1 DROP DEFAULT;", 1897 Expected: []sql.Row{{types.NewOkResult(0)}}, 1898 }, 1899 { 1900 Query: "INSERT INTO test (pk) VALUES (3);", 1901 ExpectedErr: sql.ErrInsertIntoNonNullableDefaultNullColumn, 1902 }, 1903 { 1904 Query: "ALTER TABLE test ALTER v2 DROP DEFAULT;", 1905 ExpectedErr: sql.ErrTableColumnNotFound, 1906 }, 1907 { // Just confirms that the last INSERT didn't do anything 1908 Query: "SELECT * FROM test;", 1909 Expected: []sql.Row{{1, 88}, {2, 42}}, 1910 }, 1911 { 1912 Query: "ALTER TABLE test ALTER v1 SET DEFAULT 100, alter v1 DROP DEFAULT", 1913 Expected: []sql.Row{{types.NewOkResult(0)}}, 1914 }, 1915 { 1916 Query: "INSERT INTO test (pk) VALUES (2);", 1917 ExpectedErr: sql.ErrInsertIntoNonNullableDefaultNullColumn, 1918 }, 1919 { 1920 Query: "ALTER TABLE test ALTER v1 SET DEFAULT 100, alter v1 SET DEFAULT 200", 1921 Expected: []sql.Row{{types.NewOkResult(0)}}, 1922 }, 1923 { 1924 Query: "ALTER TABLE test DROP COLUMN v1, alter v1 SET DEFAULT 5000", 1925 ExpectedErr: sql.ErrTableColumnNotFound, 1926 }, 1927 { 1928 Query: "DESCRIBE test", 1929 Expected: []sql.Row{ 1930 {"pk", "bigint", "NO", "PRI", "NULL", ""}, 1931 {"v1", "bigint", "NO", "", "200", ""}, 1932 }, 1933 }, 1934 }, 1935 }, 1936 { 1937 Name: "Run through some complex queries with DISTINCT and aggregates", 1938 SetUpScript: []string{ 1939 "CREATE TABLE tab1(col0 INTEGER, col1 INTEGER, col2 INTEGER)", 1940 "CREATE TABLE tab2(col0 INTEGER, col1 INTEGER, col2 INTEGER)", 1941 "INSERT INTO tab1 VALUES(51,14,96)", 1942 "INSERT INTO tab1 VALUES(85,5,59)", 1943 "INSERT INTO tab1 VALUES(91,47,68)", 1944 "INSERT INTO tab2 VALUES(64,77,40)", 1945 "INSERT INTO tab2 VALUES(75,67,58)", 1946 "INSERT INTO tab2 VALUES(46,51,23)", 1947 "CREATE TABLE mytable (pk int, v1 int)", 1948 "INSERT INTO mytable VALUES(1,1)", 1949 "INSERT INTO mytable VALUES(1,1)", 1950 "INSERT INTO mytable VALUES(2,2)", 1951 "INSERT INTO mytable VALUES(1,2)", 1952 }, 1953 Assertions: []ScriptTestAssertion{ 1954 { 1955 Query: "SELECT - SUM( DISTINCT - - 71 ) AS col2 FROM tab2 cor0", 1956 Expected: []sql.Row{{float64(-71)}}, 1957 }, 1958 { 1959 Query: "SELECT - SUM ( DISTINCT - - 71 ) AS col2 FROM tab2 cor0", 1960 Expected: []sql.Row{{float64(-71)}}, 1961 }, 1962 { 1963 Query: "SELECT + MAX( DISTINCT ( - col0 ) ) FROM tab1 AS cor0", 1964 Expected: []sql.Row{{-51}}, 1965 }, 1966 { 1967 Query: "SELECT SUM( DISTINCT + col1 ) * - 22 - - ( - COUNT( * ) ) col0 FROM tab1 AS cor0", 1968 Expected: []sql.Row{{float64(-1455)}}, 1969 }, 1970 { 1971 Query: "SELECT MIN (DISTINCT col1) from tab1 GROUP BY col0 ORDER BY col0", 1972 Expected: []sql.Row{{14}, {5}, {47}}, 1973 }, 1974 { 1975 Query: "SELECT SUM (DISTINCT col1) from tab1 GROUP BY col0 ORDER BY col0", 1976 Expected: []sql.Row{{float64(14)}, {float64(5)}, {float64(47)}}, 1977 }, 1978 { 1979 Query: "SELECT pk, SUM(DISTINCT v1), MAX(v1) FROM mytable GROUP BY pk ORDER BY pk", 1980 Expected: []sql.Row{{int64(1), float64(3), int64(2)}, {int64(2), float64(2), int64(2)}}, 1981 }, 1982 { 1983 Query: "SELECT pk, MIN(DISTINCT v1), MAX(DISTINCT v1) FROM mytable GROUP BY pk ORDER BY pk", 1984 Expected: []sql.Row{{int64(1), int64(1), int64(2)}, {int64(2), int64(2), int64(2)}}, 1985 }, 1986 { 1987 Query: "SELECT SUM(DISTINCT pk * v1) from mytable", 1988 Expected: []sql.Row{{float64(7)}}, 1989 }, 1990 { 1991 Query: "SELECT SUM(DISTINCT POWER(v1, 2)) FROM mytable", 1992 Expected: []sql.Row{{float64(5)}}, 1993 }, 1994 { 1995 Query: "SELECT + + 97 FROM tab1 GROUP BY tab1.col1", 1996 Expected: []sql.Row{{97}, {97}, {97}}, 1997 }, 1998 { 1999 Query: "SELECT rand(10) FROM tab1 GROUP BY tab1.col1", 2000 Expected: []sql.Row{{0.5660920659323543}, {0.5660920659323543}, {0.5660920659323543}}, 2001 }, 2002 { 2003 Query: "SELECT ALL - cor0.col0 * + cor0.col0 AS col2 FROM tab1 AS cor0 GROUP BY cor0.col0", 2004 Expected: []sql.Row{{-2601}, {-7225}, {-8281}}, 2005 }, 2006 { 2007 Query: "SELECT cor0.col0 * cor0.col0 + cor0.col0 AS col2 FROM tab1 AS cor0 GROUP BY cor0.col0 order by 1", 2008 Expected: []sql.Row{{2652}, {7310}, {8372}}, 2009 }, 2010 { 2011 Query: "SELECT - floor(cor0.col0) * ceil(cor0.col0) AS col2 FROM tab1 AS cor0 GROUP BY cor0.col0", 2012 Expected: []sql.Row{{-2601}, {-7225}, {-8281}}, 2013 }, 2014 { 2015 Query: "SELECT col0 FROM tab1 AS cor0 GROUP BY cor0.col0", 2016 Expected: []sql.Row{{51}, {85}, {91}}, 2017 }, 2018 { 2019 Query: "SELECT - cor0.col0 FROM tab1 AS cor0 GROUP BY cor0.col0", 2020 Expected: []sql.Row{{-51}, {-85}, {-91}}, 2021 }, 2022 { 2023 Query: "SELECT col0 BETWEEN 2 and 4 from tab1 group by col0", 2024 Expected: []sql.Row{{false}, {false}, {false}}, 2025 }, 2026 { 2027 Query: "SELECT col0, col1 FROM tab1 GROUP by col0;", 2028 ExpectedErr: analyzererrors.ErrValidationGroupBy, 2029 }, 2030 }, 2031 }, 2032 { 2033 Name: "Nested Subquery projections (NTC)", 2034 SetUpScript: []string{ 2035 `CREATE TABLE dcim_site (id char(32) NOT NULL,created date,last_updated datetime,_custom_field_data json NOT NULL,name varchar(100) NOT NULL,_name varchar(100) NOT NULL,slug varchar(100) NOT NULL,facility varchar(50) NOT NULL,asn bigint,time_zone varchar(63) NOT NULL,description varchar(200) NOT NULL,physical_address varchar(200) NOT NULL,shipping_address varchar(200) NOT NULL,latitude decimal(8,6),longitude decimal(9,6),contact_name varchar(50) NOT NULL,contact_phone varchar(20) NOT NULL,contact_email varchar(254) NOT NULL,comments longtext NOT NULL,region_id char(32),status_id char(32),tenant_id char(32),PRIMARY KEY (id),KEY dcim_site_region_id_45210932 (region_id),KEY dcim_site_status_id_e6a50f56 (status_id),KEY dcim_site_tenant_id_15e7df63 (tenant_id),UNIQUE KEY name (name),UNIQUE KEY slug (slug)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;`, 2036 `CREATE TABLE dcim_rackgroup (id char(32) NOT NULL,created date,last_updated datetime,_custom_field_data json NOT NULL,name varchar(100) NOT NULL,slug varchar(100) NOT NULL,description varchar(200) NOT NULL,lft int unsigned NOT NULL,rght int unsigned NOT NULL,tree_id int unsigned NOT NULL,level int unsigned NOT NULL,parent_id char(32),site_id char(32) NOT NULL,PRIMARY KEY (id),KEY dcim_rackgroup_parent_id_cc315105 (parent_id),KEY dcim_rackgroup_site_id_13520e89 (site_id),KEY dcim_rackgroup_slug_3f4582a7 (slug),KEY dcim_rackgroup_tree_id_9c2ad6f4 (tree_id),UNIQUE KEY site_idname (site_id,name),UNIQUE KEY site_idslug (site_id,slug),CONSTRAINT dcim_rackgroup_parent_id_cc315105_fk_dcim_rackgroup_id FOREIGN KEY (parent_id) REFERENCES dcim_rackgroup (id),CONSTRAINT dcim_rackgroup_site_id_13520e89_fk_dcim_site_id FOREIGN KEY (site_id) REFERENCES dcim_site (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;`, 2037 `CREATE TABLE dcim_rack (id char(32) NOT NULL,created date,last_updated datetime,_custom_field_data json NOT NULL,name varchar(100) NOT NULL,_name varchar(100) NOT NULL,facility_id varchar(50),serial varchar(50) NOT NULL,asset_tag varchar(50),type varchar(50) NOT NULL,width smallint unsigned NOT NULL,u_height smallint unsigned NOT NULL,desc_units tinyint NOT NULL,outer_width smallint unsigned,outer_depth smallint unsigned,outer_unit varchar(50) NOT NULL,comments longtext NOT NULL,group_id char(32),role_id char(32),site_id char(32) NOT NULL,status_id char(32),tenant_id char(32),PRIMARY KEY (id),UNIQUE KEY asset_tag (asset_tag),KEY dcim_rack_group_id_44e90ea9 (group_id),KEY dcim_rack_role_id_62d6919e (role_id),KEY dcim_rack_site_id_403c7b3a (site_id),KEY dcim_rack_status_id_ee3dee3e (status_id),KEY dcim_rack_tenant_id_7cdf3725 (tenant_id),UNIQUE KEY group_idfacility_id (group_id,facility_id),UNIQUE KEY group_idname (group_id,name),CONSTRAINT dcim_rack_group_id_44e90ea9_fk_dcim_rackgroup_id FOREIGN KEY (group_id) REFERENCES dcim_rackgroup (id),CONSTRAINT dcim_rack_site_id_403c7b3a_fk_dcim_site_id FOREIGN KEY (site_id) REFERENCES dcim_site (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;`, 2038 `INSERT INTO dcim_site (id, created, last_updated, _custom_field_data, status_id, name, _name, slug, region_id, tenant_id, facility, asn, time_zone, description, physical_address, shipping_address, latitude, longitude, contact_name, contact_phone, contact_email, comments) VALUES ('f0471f313b694d388c8ec39d9590e396', '2021-05-20', '2021-05-20 18:51:46.416695', '{}', NULL, 'Site 1', 'Site 00000001', 'site-1', NULL, NULL, '', NULL, '', '', '', '', NULL, NULL, '', '', '', '')`, 2039 `INSERT INTO dcim_site (id, created, last_updated, _custom_field_data, status_id, name, _name, slug, region_id, tenant_id, facility, asn, time_zone, description, physical_address, shipping_address, latitude, longitude, contact_name, contact_phone, contact_email, comments) VALUES ('442bab8b517149ab87207e8fb5ba1569', '2021-05-20', '2021-05-20 18:51:47.333720', '{}', NULL, 'Site 2', 'Site 00000002', 'site-2', NULL, NULL, '', NULL, '', '', '', '', NULL, NULL, '', '', '', '')`, 2040 `INSERT INTO dcim_rackgroup (id, created, last_updated, _custom_field_data, name, slug, site_id, parent_id, description, lft, rght, tree_id, level) VALUES ('5c107f979f434bf7a7820622f18a5211', '2021-05-20', '2021-05-20 18:51:48.150116', '{}', 'Parent Rack Group 1', 'parent-rack-group-1', 'f0471f313b694d388c8ec39d9590e396', NULL, '', 1, 2, 1, 0)`, 2041 `INSERT INTO dcim_rackgroup (id, created, last_updated, _custom_field_data, name, slug, site_id, parent_id, description, lft, rght, tree_id, level) VALUES ('6707c20336a2406da6a9d394477f7e8c', '2021-05-20', '2021-05-20 18:51:48.969713', '{}', 'Parent Rack Group 2', 'parent-rack-group-2', '442bab8b517149ab87207e8fb5ba1569', NULL, '', 1, 2, 2, 0)`, 2042 `INSERT INTO dcim_rackgroup (id, created, last_updated, _custom_field_data, name, slug, site_id, parent_id, description, lft, rght, tree_id, level) VALUES ('6bc0d9b1affe46918b09911359241db6', '2021-05-20', '2021-05-20 18:51:50.566160', '{}', 'Rack Group 1', 'rack-group-1', 'f0471f313b694d388c8ec39d9590e396', '5c107f979f434bf7a7820622f18a5211', '', 2, 3, 1, 1)`, 2043 `INSERT INTO dcim_rackgroup (id, created, last_updated, _custom_field_data, name, slug, site_id, parent_id, description, lft, rght, tree_id, level) VALUES ('a773cac9dc9842228cdfd8c97a67136e', '2021-05-20', '2021-05-20 18:51:52.126952', '{}', 'Rack Group 2', 'rack-group-2', 'f0471f313b694d388c8ec39d9590e396', '5c107f979f434bf7a7820622f18a5211', '', 4, 5, 1, 1)`, 2044 `INSERT INTO dcim_rackgroup (id, created, last_updated, _custom_field_data, name, slug, site_id, parent_id, description, lft, rght, tree_id, level) VALUES ('a35a843eb181404bb9da2126c6580977', '2021-05-20', '2021-05-20 18:51:53.706000', '{}', 'Rack Group 3', 'rack-group-3', 'f0471f313b694d388c8ec39d9590e396', '5c107f979f434bf7a7820622f18a5211', '', 6, 7, 1, 1)`, 2045 `INSERT INTO dcim_rackgroup (id, created, last_updated, _custom_field_data, name, slug, site_id, parent_id, description, lft, rght, tree_id, level) VALUES ('f09a02c95b064533b823e25374f5962a', '2021-05-20', '2021-05-20 18:52:03.037056', '{}', 'Test Rack Group 4', 'test-rack-group-4', '442bab8b517149ab87207e8fb5ba1569', '6707c20336a2406da6a9d394477f7e8c', '', 2, 3, 2, 1)`, 2046 `INSERT INTO dcim_rackgroup (id, created, last_updated, _custom_field_data, name, slug, site_id, parent_id, description, lft, rght, tree_id, level) VALUES ('ecff5b528c5140d4a58f1b24a1c80ebc', '2021-05-20', '2021-05-20 18:52:05.390373', '{}', 'Test Rack Group 5', 'test-rack-group-5', '442bab8b517149ab87207e8fb5ba1569', '6707c20336a2406da6a9d394477f7e8c', '', 4, 5, 2, 1)`, 2047 `INSERT INTO dcim_rackgroup (id, created, last_updated, _custom_field_data, name, slug, site_id, parent_id, description, lft, rght, tree_id, level) VALUES ('d31b3772910e4418bdd5725d905e2699', '2021-05-20', '2021-05-20 18:52:07.758547', '{}', 'Test Rack Group 6', 'test-rack-group-6', '442bab8b517149ab87207e8fb5ba1569', '6707c20336a2406da6a9d394477f7e8c', '', 6, 7, 2, 1)`, 2048 `INSERT INTO dcim_rack (id,created,last_updated,_custom_field_data,name,_name,facility_id,serial,asset_tag,type,width,u_height,desc_units,outer_width,outer_depth,outer_unit,comments,group_id,role_id,site_id,status_id,tenant_id) VALUES ('abc123', '2021-05-20', '2021-05-20 18:51:48.150116', '{}', "name", "name", "facility", "serial", "assettag", "type", 1, 1, 1, 1, 1, "outer units", "comment", "6bc0d9b1affe46918b09911359241db6", "role", "f0471f313b694d388c8ec39d9590e396", "status", "tenant")`, 2049 }, 2050 Assertions: []ScriptTestAssertion{ 2051 { 2052 Query: `SELECT 2053 (( 2054 SELECT COUNT(*) 2055 FROM dcim_rack 2056 WHERE group_id 2057 IN ( 2058 SELECT m2.id 2059 FROM dcim_rackgroup m2 2060 WHERE m2.tree_id = dcim_rackgroup.tree_id 2061 AND m2.lft BETWEEN dcim_rackgroup.lft 2062 AND dcim_rackgroup.rght 2063 ) 2064 )) AS rack_count, 2065 dcim_rackgroup.id, 2066 dcim_rackgroup._custom_field_data, 2067 dcim_rackgroup.name, 2068 dcim_rackgroup.slug, 2069 dcim_rackgroup.site_id, 2070 dcim_rackgroup.parent_id, 2071 dcim_rackgroup.description, 2072 dcim_rackgroup.lft, 2073 dcim_rackgroup.rght, 2074 dcim_rackgroup.tree_id, 2075 dcim_rackgroup.level 2076 FROM dcim_rackgroup 2077 order by 2 limit 1`, 2078 Expected: []sql.Row{{1, "5c107f979f434bf7a7820622f18a5211", types.JSONDocument{Val: map[string]interface{}{}}, "Parent Rack Group 1", "parent-rack-group-1", "f0471f313b694d388c8ec39d9590e396", nil, "", uint64(1), uint64(2), uint64(1), uint64(0)}}, 2079 }, 2080 }, 2081 }, 2082 { 2083 Name: "CREATE TABLE SELECT Queries", 2084 SetUpScript: []string{ 2085 `CREATE TABLE t1 (pk int PRIMARY KEY, v1 varchar(10))`, 2086 `INSERT INTO t1 VALUES (1,"1"), (2,"2"), (3,"3")`, 2087 `CREATE TABLE t2 AS SELECT * FROM t1`, 2088 //`CREATE TABLE t3(v0 int) AS SELECT pk FROM t1`, // parser problems 2089 `CREATE TABLE t3 AS SELECT pk FROM t1`, 2090 `CREATE TABLE t4 AS SELECT pk, v1 FROM t1`, 2091 `CREATE TABLE t5 SELECT * FROM t1 ORDER BY pk LIMIT 1`, 2092 }, 2093 Assertions: []ScriptTestAssertion{ 2094 { 2095 Query: `SELECT * FROM t2`, 2096 Expected: []sql.Row{{1, "1"}, {2, "2"}, {3, "3"}}, 2097 }, 2098 { 2099 Query: `SELECT * FROM t3`, 2100 Expected: []sql.Row{{1}, {2}, {3}}, 2101 }, 2102 { 2103 Query: `SELECT * FROM t4`, 2104 Expected: []sql.Row{{1, "1"}, {2, "2"}, {3, "3"}}, 2105 }, 2106 { 2107 Query: `SELECT * FROM t5`, 2108 Expected: []sql.Row{{1, "1"}}, 2109 }, 2110 { 2111 Query: `CREATE TABLE test SELECT * FROM t1`, 2112 Expected: []sql.Row{sql.Row{types.OkResult{ 2113 RowsAffected: 3, 2114 InsertID: 0, 2115 Info: nil, 2116 }}}, 2117 }, 2118 }, 2119 }, 2120 { 2121 Name: "unix_timestamp function usage", 2122 SetUpScript: []string{ 2123 // NOTE: session time zone needs to be set as UNIX_TIMESTAMP function depends on it and converts the final result 2124 "SET @@SESSION.time_zone = 'UTC';", 2125 "CREATE TABLE `datetime_table` ( `i` bigint NOT NULL, `date_col` date, `datetime_col` datetime, `timestamp_col` timestamp, `time_col` time(6), PRIMARY KEY (`i`) )", 2126 `insert into datetime_table values 2127 (1, '2019-12-31T12:00:00Z', '2020-01-01T12:00:00Z', '2020-01-02T12:00:00Z', '03:10:0'), 2128 (2, '2020-01-03T12:00:00Z', '2020-01-04T12:00:00Z', '2020-01-05T12:00:00Z', '04:00:44'), 2129 (3, '2020-01-07T00:00:00Z', '2020-01-07T12:00:00Z', '2020-01-07T12:00:01Z', '15:00:00.005000')`, 2130 `create index datetime_table_d on datetime_table (date_col)`, 2131 `create index datetime_table_dt on datetime_table (datetime_col)`, 2132 `create index datetime_table_ts on datetime_table (timestamp_col)`, 2133 }, 2134 Assertions: []ScriptTestAssertion{ 2135 { 2136 Query: "SELECT unix_timestamp(timestamp_col) div 60 * 60 as timestamp_col, avg(i) from datetime_table group by 1 order by unix_timestamp(timestamp_col) div 60 * 60", 2137 Expected: []sql.Row{ 2138 {int64(1577966400), 1.0}, 2139 {int64(1578225600), 2.0}, 2140 {int64(1578398400), 3.0}}, 2141 }, 2142 }, 2143 }, 2144 { 2145 Name: "Issue #499", // https://github.com/dolthub/go-mysql-server/issues/499 2146 SetUpScript: []string{ 2147 "SET @@SESSION.time_zone = 'UTC';", 2148 "CREATE TABLE test (time TIMESTAMP, value DOUBLE);", 2149 `INSERT INTO test VALUES 2150 ("2021-07-04 10:00:00", 1.0), 2151 ("2021-07-03 10:00:00", 2.0), 2152 ("2021-07-02 10:00:00", 3.0), 2153 ("2021-07-01 10:00:00", 4.0);`, 2154 }, 2155 Assertions: []ScriptTestAssertion{ 2156 { 2157 // In the original, reported issue, the order by clause did not qualify the table name 2158 // for `test.time`. When there is ambiguity between a column name and an expression 2159 // alias name in the order by clause, MySQL choose the alias; however, if the reference 2160 // is used in a function call, MySQL instead seems to resolve to the column name. 2161 // Until we determine the exact rule for this behavior, we've qualified the reference 2162 // in the order by clause to ensure it selects the table column and not the alias. 2163 // TODO: Waiting to hear back from MySQL on whether this is intended behavior or not: 2164 // https://bugs.mysql.com/bug.php?id=109020 2165 Query: `SELECT UNIX_TIMESTAMP(time) DIV 60 * 60 AS "time", avg(value) AS "value" 2166 FROM test GROUP BY 1 ORDER BY UNIX_TIMESTAMP(test.time) DIV 60 * 60`, 2167 Expected: []sql.Row{ 2168 {int64(1625133600), 4.0}, 2169 {int64(1625220000), 3.0}, 2170 {int64(1625306400), 2.0}, 2171 {int64(1625392800), 1.0}, 2172 }, 2173 }, 2174 }, 2175 //todo(max): fix arithmatic on bindvar typing 2176 SkipPrepared: true, 2177 }, 2178 { 2179 Name: "WHERE clause considers ENUM/SET types for comparisons", 2180 SetUpScript: []string{ 2181 "CREATE TABLE test (pk BIGINT PRIMARY KEY, v1 ENUM('a', 'b', 'c'), v2 SET('a', 'b', 'c'));", 2182 "INSERT INTO test VALUES (1, 2, 2), (2, 1, 1);", 2183 }, 2184 Assertions: []ScriptTestAssertion{ 2185 { 2186 Query: "SELECT * FROM test;", 2187 Expected: []sql.Row{{1, "b", "b"}, {2, "a", "a"}}, 2188 }, 2189 { 2190 Query: "UPDATE test SET v1 = 3 WHERE v1 = 2;", 2191 Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 0, Info: plan.UpdateInfo{Matched: 1, Updated: 1}}}}, 2192 }, 2193 { 2194 Query: "SELECT * FROM test;", 2195 Expected: []sql.Row{{1, "c", "b"}, {2, "a", "a"}}, 2196 }, 2197 { 2198 Query: "UPDATE test SET v2 = 3 WHERE 2 = v2;", 2199 Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 0, Info: plan.UpdateInfo{Matched: 1, Updated: 1}}}}, 2200 }, 2201 { 2202 Query: "SELECT * FROM test;", 2203 Expected: []sql.Row{{1, "c", "a,b"}, {2, "a", "a"}}, 2204 }, 2205 }, 2206 }, 2207 { 2208 Name: "Slightly more complex example for the Exists Clause", 2209 SetUpScript: []string{ 2210 "create table store(store_id int, item_id int, primary key (store_id, item_id))", 2211 "create table items(item_id int primary key, price int)", 2212 "insert into store values (0, 1), (0,2),(0,3),(1,2),(1,4),(2,1)", 2213 "insert into items values (1, 10), (2, 20), (3, 30),(4,40)", 2214 }, 2215 Assertions: []ScriptTestAssertion{ 2216 { 2217 Query: "SELECT * from store WHERE EXISTS (SELECT price from items where price > 10 and store.item_id = items.item_id)", 2218 Expected: []sql.Row{{0, 2}, {0, 3}, {1, 2}, {1, 4}}, 2219 }, 2220 }, 2221 }, 2222 { 2223 Name: "Simple Update Join test that manipulates two tables", 2224 SetUpScript: []string{ 2225 "CREATE TABLE test (pk int primary key);", 2226 `CREATE TABLE test2 (pk int primary key, val int);`, 2227 `INSERT into test values (0),(1),(2),(3)`, 2228 `INSERT into test2 values (0, 0),(1, 1),(2, 2),(3, 3)`, 2229 `CREATE TABLE test3(k int, val int, primary key (k, val))`, 2230 `INSERT into test3 values (1,2),(1,3),(1,4)`, 2231 }, 2232 Assertions: []ScriptTestAssertion{ 2233 { 2234 Query: `update test2 inner join (select * from test3 order by val) as t3 on test2.pk = t3.k SET test2.val=t3.val`, 2235 Expected: []sql.Row{{types.OkResult{RowsAffected: 1, Info: plan.UpdateInfo{ 2236 Matched: 1, 2237 Updated: 1, 2238 Warnings: 0, 2239 }}}}, 2240 }, 2241 { 2242 Query: "SELECT val FROM test2 where pk = 1", 2243 Expected: []sql.Row{ 2244 {2}, 2245 }, 2246 }, 2247 { 2248 Query: `update test inner join test2 on test.pk = test2.pk SET test.pk=test.pk*10, test2.pk = test2.pk * 4 where test.pk < 10;`, 2249 Expected: []sql.Row{{types.OkResult{RowsAffected: 6, Info: plan.UpdateInfo{ 2250 Matched: 6, // TODO: The answer should be 8 2251 Updated: 6, 2252 Warnings: 0, 2253 }}}}, 2254 }, 2255 { 2256 Query: "SELECT * FROM test", 2257 Expected: []sql.Row{ 2258 {0}, 2259 {10}, 2260 {20}, 2261 {30}, 2262 }, 2263 }, 2264 { 2265 Query: "SELECT * FROM test2", 2266 Expected: []sql.Row{ 2267 {0, 0}, 2268 {4, 2}, 2269 {8, 2}, 2270 {12, 3}, 2271 }, 2272 }, 2273 { 2274 Query: `update test2 inner join (select * from test3 order by val) as t3 on false SET test2.val=t3.val`, 2275 Expected: []sql.Row{{types.OkResult{RowsAffected: 0, Info: plan.UpdateInfo{ 2276 Matched: 0, 2277 Updated: 0, 2278 Warnings: 0, 2279 }}}}, 2280 }, 2281 }, 2282 }, 2283 { 2284 Name: "Partial indexes are used and return the expected result", 2285 SetUpScript: []string{ 2286 "CREATE TABLE test (pk BIGINT PRIMARY KEY, v1 BIGINT, v2 BIGINT, v3 BIGINT, INDEX vx (v3, v2, v1));", 2287 "INSERT INTO test VALUES (1,2,3,4), (2,3,4,5), (3,4,5,6), (4,5,6,7), (5,6,7,8);", 2288 }, 2289 Assertions: []ScriptTestAssertion{ 2290 { 2291 Query: "SELECT * FROM test WHERE v3 = 4;", 2292 Expected: []sql.Row{{1, 2, 3, 4}}, 2293 }, 2294 { 2295 Query: "SELECT * FROM test WHERE v3 = 8 AND v2 = 7;", 2296 Expected: []sql.Row{{5, 6, 7, 8}}, 2297 }, 2298 { 2299 Query: "SELECT * FROM test WHERE v3 >= 6 AND v2 >= 6;", 2300 Expected: []sql.Row{{4, 5, 6, 7}, {5, 6, 7, 8}}, 2301 }, 2302 { 2303 Query: "SELECT * FROM test WHERE v3 = 7 AND v2 >= 6;", 2304 Expected: []sql.Row{{4, 5, 6, 7}}, 2305 }, 2306 }, 2307 }, 2308 { 2309 Name: "Multiple indexes on the same columns in a different order", 2310 SetUpScript: []string{ 2311 "CREATE TABLE test (pk BIGINT PRIMARY KEY, v1 BIGINT, v2 BIGINT, v3 BIGINT, INDEX v123 (v1, v2, v3), INDEX v321 (v3, v2, v1), INDEX v132 (v1, v3, v2));", 2312 "INSERT INTO test VALUES (1,2,3,4), (2,3,4,5), (3,4,5,6), (4,5,6,7), (5,6,7,8);", 2313 }, 2314 Assertions: []ScriptTestAssertion{ 2315 { 2316 Query: "SELECT * FROM test WHERE v1 = 2 AND v2 > 1;", 2317 Expected: []sql.Row{{1, 2, 3, 4}}, 2318 }, 2319 { 2320 Query: "SELECT * FROM test WHERE v2 = 4 AND v3 > 1;", 2321 Expected: []sql.Row{{2, 3, 4, 5}}, 2322 }, 2323 { 2324 Query: "SELECT * FROM test WHERE v3 = 6 AND v1 > 1;", 2325 Expected: []sql.Row{{3, 4, 5, 6}}, 2326 }, 2327 { 2328 Query: "SELECT * FROM test WHERE v1 = 5 AND v3 <= 10 AND v2 >= 1;", 2329 Expected: []sql.Row{{4, 5, 6, 7}}, 2330 }, 2331 }, 2332 }, 2333 { 2334 Name: "Ensure proper DECIMAL support (found by fuzzer)", 2335 SetUpScript: []string{ 2336 "CREATE TABLE `GzaKtwgIya` (`K7t5WY` DECIMAL(64,5), `qBjVrN` VARBINARY(1000), `PvqQtc` SET('c3q6y','kxMqhfkK','XlRI8','dF0N63H','hMPjt0KXRLwCGRr','27fi2s','1FSJ','NcPzIN','Za18lbIgxmZ','on4BKKXykVTbJ','WBfO','RMNG','Sd7','FDzbEO','cLRdLOj1y','syo4','Ul','jfsfDCx6s','yEW3','JyQcWFDl'), `1kv7el` FLOAT, `Y3vfRG` BLOB, `Ijq8CK` TINYTEXT, `tzeStN` MEDIUMINT, `Ak83FQ` BINARY(64), `8Nbp3L` DOUBLE, PRIMARY KEY (`K7t5WY`));", 2337 "REPLACE INTO `GzaKtwgIya` VALUES ('58567047399981325523662211357420045483361289734772861386428.89028','bvo5~Tt8%kMW2nm2!8HghaeulI6!pMadE+j-J2LeU1O1*-#@Lm8Ibh00bTYiA*H1Q8P1_kQq 24Rrd4@HeF%#7#C#U7%mqOMrQ0%!HVrGV1li.XyYa:7#3V^DtAMDTQ9 cY=07T4|DStrwy4.MAQxOG#1d#fcq+7675$y0e96-2@8-WlQ^p|%E!a^TV!Yj2_eqZZys1z:883l5I%zAT:i56K^T!cx#us $60Tb#gH$1#$P.709E#VrH9FbQ5QZK2hZUH!qUa4Xl8*I*0fT~oAha$8jU5AoWs+Uv!~:14Yq%pLXpP9RlZ:Gd1g|*$Qa.9*^K~YlYWVaxwY~_g6zOMpU$YijT+!_*m3=||cMNn#uN0!!OyCg~GTQlJ11+#@Ohqc7b#2|Jp2Aei56GOmq^I=7cQ=sQh~V.D^HzwK5~4E$QzFXfWNVN5J_w2b4dkR~bB~7F%=@R@9qE~e:-_RnoJcOLfBS@0:*hTIP$5ui|5Ea-l+qU4nx98X6rV2bLBxn8am@p~:xLF#T^_9kJVN76q^18=i *FJo.v-xA2GP==^C^Jz3yBF0OY4bIxC59Y#6G=$w:xh71kMxBcYJKf3+$Ci_uWx0P*AfFNne0_1E0Lwv#3J8vm:. 8Wo~F3VT:@w.t@w .JZz$bok9Tls7RGo=~4 Y$~iELr$s@53YuTPM8oqu!x*1%GswpJR=0K#qs00nW-1MqEUc:0wZv#X4qY^pzVDb:!:!yDhjhh+KIT%2%w@+t8c!f~o!%EnwBIr_OyzL6e1$-R8n0nWPU.toODd*|fW3H$9ZLc9!dMS:QfjI0M$nK 8aGvUVP@9kS~W#Y=Q%=37$@pAUkDTXkJo~-DRvCG6phPp*Xji@9|AEODHi+-6p%X4YM5Y3WasPHcZQ8QgTwi9 N=2RQD_MtVU~0J~3SAx*HrMlKvCPTswZq#q_96ny_A@7g!E2jyaxWFJD:C233onBdchW$WdAc.LZdZHYDR^uwZb9B9p-q.BkD1I',608583,'-7.276514330627342e-28','FN3O_E:$ 5S40T7^Vu1g!Ktn^N|4RE!9GnZiW5dG:%SJb5|SNuuI.d2^qnMY.Xn*_fRfk Eo7OhqY8OZ~pA0^ !2P.uN~r@pZ2!A0+4b*%nxO.tm%S6=$CZ9+c1zu-p $b:7:fOkC%@E3951st@2Q93~8hj:ZGeJ6S@nw-TAG+^lad37aB#xN*rD^9TO0|hleA#.Nh28S2PB72L*TxD0$|XE3S5eVVmbI*pkzE~lPecopX1fUyFj#LC+%~pjmab7^ Kdd4B%8I!ohOCQV.oiw++N|#W2=D4:_sK0@~kTTeNA8_+FMKRwro.M0| LdKHf-McKm0Z-R9+H%!9r l6%7UEB50yNH-ld%eW8!f=LKgZLc*TuTP2DA_o0izvzZokNp3ShR+PA7Fk* 1RcSt5KXe+8tLc+WGP','3RvfN2N.Q1tIffE965#2r=u_-4!u:9w!F1p7+mSsO8ckio|ib 1t@~GtgUkJX',1858932,'DJMaQcI=vS-Jk2L#^2N8qZcRpMJ2Ga!30A+@I!+35d-9bwVEVi5-~i.a%!KdoF5h','1.0354401044541863e+255');", 2338 "INSERT INTO `GzaKtwgIya` VALUES ('91198031969464085142628031466155813748261645250257051732159.65596','96Lu=focmodq4otVAUN6TD-F$@k^4443higo=KH!1WBDH9|vpEGdO* 1uF6yWjT4:7G|altXnWSv+d:c8Km8vL!b%-nuB8mAxO9E|a5N5#v@z!ij5ifeIEoZGXrhBJl.m*Rx-@%g~t:y$3Pp3Q7Bd3y$=YG%6yibqXWO9$SS+g=*6QzdSCzuR~@v!:.ATye0A@y~DG=uq!PaZd6wN7.2S Aq868-RN3RM61V#N+Qywqo=%iYV*554@h6GPKZ| pmNwQw=PywuyBhr*MHAOXV+u9_-#imKI-wT4gEcA1~lGg1cfL2IvhkwOXRhrjAx-8+R3#4!Ai J6SYP|YUuuGalJ_N8k_8K^~h!JyiH$0JbGQ4AOxO3-eW=BaopOd8FF1.cfFMK!tXR ^I15g:npOuZZO$Vq3yQ4bl4s$E9:t2^.4f.:I4_@u9_UI1ApBthJZNiv~o#*uhs9K@ufZ1YPJQY-pMj$v-lQ2#%=Uu!iEAO3%vQ^5YITKcWRk~$kd1H#F675r@P5#M%*F_xP3Js7$YuEC4YuQjZ A74tMw:KwQ8dR:k_ Sa85G~42-K3%:jk5G9csC@iW3nY|@-:_dg~5@J!FWF5F+nyBgz4fDpdkdk9^:_.t$A3W-C@^Ax.~o|Rq96_i%HeG*7jBjOGhY-e1k@aD@WW.@GmpGAI|T-84gZFG3BU9@#9lpL|U2YCEA.BEA%sxDZ Kw:n+d$Y!SZw0Iml$Bdtyr:02Np=DZpiI%$N9*U=%Jq#$P5BI60WOTK+UynVx9Dd**5q8y9^v+I|PPa#_2XheV5YQU.ONdQQNJxsiRaEl!*=xv4bTWj1wBH#_-eM3T',490529,'-8.419238802182018e+25','|WD!NpWJOfN+_Au 1y!|XF8l38#%%R5%$TRUEaFt%4ywKQ8 O1LD-3qRDrnHAXboH~0uivbo87f+V%=q9~Mvz1EIxsU!whSmPqtb9r*11346R_@L+H#@@Z9H-Dc6j%.D0o##m@B9o7jO#~N81ACI|f#J3z4dho:jc54Xws$8r%cxuov^1$w_58Fv2*.8qbAW$TF153A:8wwj4YIhkd#^Q7 |g7I0iQG0p+yE64rk!Pu!SA-z=ELtLNOCJBk_4!lV$izn%sB6JwM+uq~ 49I7','v|eUA_h2@%t~bn26ci8Ngjm@Lk*G=l2MhxhceV2V|ka#c',8150267,'nX-=1Q$3riw_jlukGuHmjodT_Y_SM$xRbEt$%$%hlIUF1+GpRp~U6JvRX^: k@n#','7.956726808353253e+267');", 2339 }, 2340 Assertions: []ScriptTestAssertion{ 2341 { 2342 Query: "DELETE FROM `GzaKtwgIya` WHERE `K7t5WY` = '58567047399981325523662211357420045483361289734772861386428.89028';", 2343 Expected: []sql.Row{{types.NewOkResult(1)}}, 2344 }, 2345 { 2346 Query: "SELECT COUNT(*) FROM GzaKtwgIya", 2347 Expected: []sql.Row{{1}}, 2348 }, 2349 }, 2350 }, 2351 { 2352 Name: "Ensure scale is not rounded when inserting to DECIMAL type through float64", 2353 SetUpScript: []string{ 2354 "create table test (number decimal(40,16));", 2355 "insert into test values ('11981.5923291839784651');", 2356 "create table small_test (n decimal(3,2));", 2357 }, 2358 Assertions: []ScriptTestAssertion{ 2359 { 2360 Query: "SELECT COUNT(*) FROM test WHERE number = CONVERT('11981.5923291839784651', DECIMAL)", 2361 Expected: []sql.Row{{1}}, 2362 }, 2363 { 2364 Query: "INSERT INTO test VALUES (11981.5923291839784651);", 2365 Expected: []sql.Row{{types.NewOkResult(1)}}, 2366 }, 2367 { 2368 Query: "SELECT COUNT(*) FROM test WHERE number = CONVERT('11981.5923291839784651', DECIMAL)", 2369 Expected: []sql.Row{{2}}, 2370 }, 2371 { 2372 Query: "INSERT INTO test VALUES (119815923291839784651.11981592329183978465111981592329183978465144);", 2373 Expected: []sql.Row{{types.NewOkResult(1)}}, 2374 }, 2375 { 2376 Query: "SELECT COUNT(*) FROM test WHERE number = CONVERT('119815923291839784651.1198159232918398', DECIMAL)", 2377 Expected: []sql.Row{{1}}, 2378 }, 2379 { 2380 Query: "INSERT INTO test VALUES (1.1981592329183978465111981592329183978465111981592329183978465144);", 2381 Expected: []sql.Row{{types.NewOkResult(1)}}, 2382 }, 2383 { 2384 Query: "SELECT COUNT(*) FROM test WHERE number = CONVERT('1.1981592329183978', DECIMAL)", 2385 Expected: []sql.Row{{1}}, 2386 }, 2387 { 2388 Query: "INSERT INTO test VALUES (1.1981592329183978545111981592329183978465111981592329183978465144);", 2389 Expected: []sql.Row{{types.NewOkResult(1)}}, 2390 }, 2391 { 2392 Query: "SELECT COUNT(*) FROM test WHERE number = CONVERT('1.1981592329183979', DECIMAL)", 2393 Expected: []sql.Row{{1}}, 2394 }, 2395 { 2396 Query: "INSERT INTO small_test VALUES (12.1);", 2397 ExpectedErr: types.ErrConvertToDecimalLimit, 2398 }, 2399 }, 2400 }, 2401 { 2402 Name: "JOIN on non-index-prefix columns do not panic (Dolt Issue #2366)", 2403 SetUpScript: []string{ 2404 "CREATE TABLE `player_season_stat_totals` (`player_id` int NOT NULL, `team_id` int NOT NULL, `season_id` int NOT NULL, `minutes` int, `games_started` int, `games_played` int, `2pm` int, `2pa` int, `3pm` int, `3pa` int, `ftm` int, `fta` int, `ast` int, `stl` int, `blk` int, `tov` int, `pts` int, `orb` int, `drb` int, `trb` int, `pf` int, `season_type_id` int NOT NULL, `league_id` int NOT NULL DEFAULT 0, PRIMARY KEY (`player_id`,`team_id`,`season_id`,`season_type_id`,`league_id`));", 2405 "CREATE TABLE `team_seasons` (`team_id` int NOT NULL, `league_id` int NOT NULL, `season_id` int NOT NULL, `prefix` varchar(100), `nickname` varchar(100), `abbreviation` varchar(100), `city` varchar(100), `state` varchar(100), `country` varchar(100), PRIMARY KEY (`team_id`,`league_id`,`season_id`));", 2406 "INSERT INTO player_season_stat_totals VALUES (1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1);", 2407 "INSERT INTO team_seasons VALUES (1,1,1,'','','','','','');", 2408 }, 2409 Assertions: []ScriptTestAssertion{ 2410 { 2411 Query: "SELECT stats.* FROM player_season_stat_totals stats LEFT JOIN team_seasons ON team_seasons.team_id = stats.team_id AND team_seasons.season_id = stats.season_id;", 2412 Expected: []sql.Row{{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}}, 2413 }, 2414 }, 2415 }, 2416 { 2417 Name: "Show create table with various keys and constraints", 2418 SetUpScript: []string{ 2419 "create table t1(a int primary key, b varchar(10) not null default 'abc')", 2420 "alter table t1 add constraint ck1 check (b like '%abc%')", 2421 "create index t1b on t1(b)", 2422 "create table t2(c int primary key, d varchar(10))", 2423 "alter table t2 add constraint t2du unique (d)", 2424 "alter table t2 add constraint fk1 foreign key (d) references t1 (b)", 2425 "create table t3 (a int, b varchar(100), c datetime(6), primary key (b,a))", 2426 "create table t4 (a int default (floor(1)), b int default (coalesce(a, 10)))", 2427 }, 2428 Assertions: []ScriptTestAssertion{ 2429 { 2430 Query: "show create table t1", 2431 Expected: []sql.Row{ 2432 {"t1", "CREATE TABLE `t1` (\n" + 2433 " `a` int NOT NULL,\n" + 2434 " `b` varchar(10) NOT NULL DEFAULT 'abc',\n" + 2435 " PRIMARY KEY (`a`),\n" + 2436 " KEY `t1b` (`b`),\n" + 2437 " CONSTRAINT `ck1` CHECK (`b` LIKE '%abc%')\n" + 2438 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}, 2439 }, 2440 }, 2441 { 2442 Query: "show create table t2", 2443 Expected: []sql.Row{ 2444 {"t2", "CREATE TABLE `t2` (\n" + 2445 " `c` int NOT NULL,\n" + 2446 " `d` varchar(10),\n" + 2447 " PRIMARY KEY (`c`),\n" + 2448 " UNIQUE KEY `t2du` (`d`),\n" + 2449 " CONSTRAINT `fk1` FOREIGN KEY (`d`) REFERENCES `t1` (`b`)\n" + 2450 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}, 2451 }, 2452 }, 2453 { 2454 Query: "show create table t3", 2455 Expected: []sql.Row{ 2456 {"t3", "CREATE TABLE `t3` (\n" + 2457 " `a` int NOT NULL,\n" + 2458 " `b` varchar(100) NOT NULL,\n" + 2459 " `c` datetime(6),\n" + 2460 " PRIMARY KEY (`b`,`a`)\n" + 2461 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}, 2462 }, 2463 }, 2464 { 2465 Query: "show create table t4", 2466 Expected: []sql.Row{ 2467 {"t4", "CREATE TABLE `t4` (\n" + 2468 " `a` int DEFAULT (floor(1)),\n" + 2469 " `b` int DEFAULT (coalesce(`a`,10))\n" + 2470 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}, 2471 }, 2472 }, 2473 }, 2474 }, 2475 { 2476 Name: "show create table with duplicate primary key", 2477 SetUpScript: []string{ 2478 "create table t (i int primary key)", 2479 "create index notpk on t(i)", 2480 }, 2481 Assertions: []ScriptTestAssertion{ 2482 { 2483 Query: "show create table t", 2484 Expected: []sql.Row{ 2485 {"t", "CREATE TABLE `t` (\n" + 2486 " `i` int NOT NULL,\n" + 2487 " PRIMARY KEY (`i`),\n" + 2488 " KEY `notpk` (`i`)\n" + 2489 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}, 2490 }, 2491 }, 2492 { 2493 Query: "create index `primary` on t(i)", 2494 ExpectedErrStr: "invalid index name 'primary'", 2495 }, 2496 }, 2497 }, 2498 { 2499 Name: "table with defaults, insert with on duplicate key update", 2500 SetUpScript: []string{ 2501 "create table t (a int primary key, b int default 100);", 2502 "insert into t values (1, 1), (2, 2)", 2503 }, 2504 Assertions: []ScriptTestAssertion{ 2505 { 2506 Query: "insert into t values (1, 10) on duplicate key update b = 10", 2507 Expected: []sql.Row{{types.NewOkResult(2)}}, 2508 }, 2509 }, 2510 }, 2511 { 2512 Name: "delete from table with misordered pks", 2513 SetUpScript: []string{ 2514 "create table a (x int, y int, z int, primary key (z,x))", 2515 "insert into a values (0,1,2), (3,4,5)", 2516 }, 2517 Assertions: []ScriptTestAssertion{ 2518 { 2519 Query: "SELECT count(*) FROM a where x = 0", 2520 Expected: []sql.Row{ 2521 {1}, 2522 }, 2523 }, 2524 { 2525 Query: "delete from a where x = 0", 2526 Expected: []sql.Row{{types.NewOkResult(1)}}, 2527 }, 2528 { 2529 Query: "SELECT * FROM a where x = 0", 2530 Expected: []sql.Row{}, 2531 }, 2532 }, 2533 }, 2534 { 2535 Name: "recreate primary key rebuilds secondary indexes", 2536 SetUpScript: []string{ 2537 "create table a (x int, y int, z int, primary key (x,y,z), index idx1 (y))", 2538 "insert into a values (1,2,3), (4,5,6), (7,8,9)", 2539 "alter table a drop primary key", 2540 "alter table a add primary key (y,z,x)", 2541 }, 2542 Assertions: []ScriptTestAssertion{ 2543 { 2544 Query: "delete from a where y = 2", 2545 Expected: []sql.Row{{types.NewOkResult(1)}}, 2546 }, 2547 { 2548 Query: "delete from a where y = 2", 2549 Expected: []sql.Row{{types.NewOkResult(0)}}, 2550 }, 2551 { 2552 Query: "select * from a where y = 2", 2553 Expected: []sql.Row{}, 2554 }, 2555 { 2556 Query: "select * from a where y = 5", 2557 Expected: []sql.Row{{4, 5, 6}}, 2558 }, 2559 }, 2560 }, 2561 { 2562 Name: "Handle hex number to binary conversion", 2563 SetUpScript: []string{ 2564 "CREATE TABLE hex_nums1 (pk BIGINT PRIMARY KEY, v1 INT, v2 BIGINT UNSIGNED, v3 DOUBLE, v4 BINARY(32));", 2565 "INSERT INTO hex_nums1 values (1, 0x7ED0599B, 0x765a8ce4ce74b187, 0xF753AD20B0C4, 0x148aa875c3cdb9af8919493926a3d7c6862fec7f330152f400c0aecb4467508a);", 2566 "CREATE TABLE hex_nums2 (pk BIGINT PRIMARY KEY, v1 VARBINARY(255), v2 BLOB);", 2567 "INSERT INTO hex_nums2 values (1, 0x765a8ce4ce74b187, 0x148aa875c3cdb9af8919493926a3d7c6862fec7f330152f400c0aecb4467508a);", 2568 }, 2569 Assertions: []ScriptTestAssertion{ 2570 { 2571 Query: "SELECT v1, v2, v3, hex(v4) FROM hex_nums1;", 2572 Expected: []sql.Row{{2127583643, uint64(8528283758723641735), float64(271938758947012), "148AA875C3CDB9AF8919493926A3D7C6862FEC7F330152F400C0AECB4467508A"}}, 2573 }, 2574 { 2575 Query: "SELECT hex(v1), hex(v2), hex(v3), hex(v4) FROM hex_nums1;", 2576 Expected: []sql.Row{{"7ED0599B", "765A8CE4CE74B187", "F753AD20B0C4", "148AA875C3CDB9AF8919493926A3D7C6862FEC7F330152F400C0AECB4467508A"}}, 2577 }, 2578 { 2579 Query: "SELECT hex(v1), hex(v2) FROM hex_nums2;", 2580 Expected: []sql.Row{{"765A8CE4CE74B187", "148AA875C3CDB9AF8919493926A3D7C6862FEC7F330152F400C0AECB4467508A"}}, 2581 }, 2582 }, 2583 }, 2584 { 2585 Name: "Multialter DDL with ADD/DROP Primary Key", 2586 SetUpScript: []string{ 2587 "CREATE TABLE t(pk int primary key, v1 int)", 2588 }, 2589 Assertions: []ScriptTestAssertion{ 2590 { 2591 Query: "ALTER TABLE t ADD COLUMN (v2 int), drop primary key, add primary key (v2)", 2592 Expected: []sql.Row{{types.NewOkResult(0)}}, 2593 }, 2594 { 2595 Query: "DESCRIBE t", 2596 Expected: []sql.Row{ 2597 {"pk", "int", "NO", "", "NULL", ""}, 2598 {"v1", "int", "YES", "", "NULL", ""}, 2599 {"v2", "int", "NO", "PRI", "NULL", ""}, 2600 }, 2601 }, 2602 { 2603 Query: "ALTER TABLE t ADD COLUMN (v3 int), drop primary key, add primary key (notacolumn)", 2604 ExpectedErr: sql.ErrKeyColumnDoesNotExist, 2605 }, 2606 { 2607 Query: "DESCRIBE t", 2608 Expected: []sql.Row{ 2609 {"pk", "int", "NO", "", "NULL", ""}, 2610 {"v1", "int", "YES", "", "NULL", ""}, 2611 {"v2", "int", "NO", "PRI", "NULL", ""}, 2612 }, 2613 }, 2614 }, 2615 }, 2616 { 2617 Name: "Multialter DDL with ADD/DROP INDEX", 2618 SetUpScript: []string{ 2619 "CREATE TABLE t(pk int primary key, v1 int)", 2620 }, 2621 Assertions: []ScriptTestAssertion{ 2622 { 2623 Query: "ALTER TABLE t DROP COLUMN v1, ADD INDEX myidx (v1)", 2624 ExpectedErr: sql.ErrKeyColumnDoesNotExist, 2625 }, 2626 { 2627 Query: "DESCRIBE t", 2628 Expected: []sql.Row{ 2629 {"pk", "int", "NO", "PRI", "NULL", ""}, 2630 {"v1", "int", "YES", "", "NULL", ""}, // should not be dropped 2631 }, 2632 }, 2633 { 2634 Query: "ALTER TABLE t ADD COLUMN (v2 int), ADD INDEX myidx (v2)", 2635 Expected: []sql.Row{{types.NewOkResult(0)}}, 2636 }, 2637 { 2638 Query: "DESCRIBE t", 2639 Expected: []sql.Row{ 2640 {"pk", "int", "NO", "PRI", "NULL", ""}, 2641 {"v1", "int", "YES", "", "NULL", ""}, 2642 {"v2", "int", "YES", "MUL", "NULL", ""}, 2643 }, 2644 }, 2645 { 2646 Query: "ALTER TABLE t ADD COLUMN (v3 int), DROP INDEX notanindex", 2647 ExpectedErr: sql.ErrCantDropFieldOrKey, 2648 }, 2649 { 2650 Query: "DESCRIBE t", 2651 Expected: []sql.Row{ 2652 {"pk", "int", "NO", "PRI", "NULL", ""}, 2653 {"v1", "int", "YES", "", "NULL", ""}, 2654 {"v2", "int", "YES", "MUL", "NULL", ""}, 2655 }, 2656 }, 2657 { 2658 Query: "ALTER TABLE t ADD COLUMN (v4 int), ADD INDEX myidx (notacolumn)", 2659 ExpectedErr: sql.ErrKeyColumnDoesNotExist, 2660 }, 2661 { 2662 Query: "DESCRIBE t", 2663 Expected: []sql.Row{ 2664 {"pk", "int", "NO", "PRI", "NULL", ""}, 2665 {"v1", "int", "YES", "", "NULL", ""}, 2666 {"v2", "int", "YES", "MUL", "NULL", ""}, 2667 }, 2668 }, 2669 { 2670 Query: "ALTER TABLE t ADD COLUMN (v4 int), ADD INDEX myidx2 (v4), DROP INDEX notanindex;", 2671 ExpectedErr: sql.ErrCantDropFieldOrKey, 2672 }, 2673 { 2674 Query: "DESCRIBE t", 2675 Expected: []sql.Row{ 2676 {"pk", "int", "NO", "PRI", "NULL", ""}, 2677 {"v1", "int", "YES", "", "NULL", ""}, 2678 {"v2", "int", "YES", "MUL", "NULL", ""}, 2679 }, 2680 }, 2681 { 2682 Query: "ALTER TABLE t ADD COLUMN (v4 int), ADD INDEX myidx2 (v4)", 2683 Expected: []sql.Row{{types.NewOkResult(0)}}, 2684 }, 2685 { 2686 Query: "DESCRIBE t", 2687 Expected: []sql.Row{ 2688 {"pk", "int", "NO", "PRI", "NULL", ""}, 2689 {"v1", "int", "YES", "", "NULL", ""}, 2690 {"v2", "int", "YES", "MUL", "NULL", ""}, 2691 {"v4", "int", "YES", "MUL", "NULL", ""}, 2692 }, 2693 }, 2694 { 2695 Query: "ALTER TABLE t ADD COLUMN (v5 int), RENAME INDEX myidx2 TO myidx3", 2696 Expected: []sql.Row{{types.NewOkResult(0)}}, 2697 }, 2698 { 2699 Query: "ALTER TABLE t DROP INDEX myidx, ADD INDEX v5idx (v5)", 2700 Expected: []sql.Row{{types.NewOkResult(0)}}, 2701 }, 2702 { 2703 Query: "DESCRIBE t", 2704 Expected: []sql.Row{ 2705 {"pk", "int", "NO", "PRI", "NULL", ""}, 2706 {"v1", "int", "YES", "", "NULL", ""}, 2707 {"v2", "int", "YES", "", "NULL", ""}, 2708 {"v4", "int", "YES", "MUL", "NULL", ""}, 2709 {"v5", "int", "YES", "MUL", "NULL", ""}, 2710 }, 2711 }, 2712 }, 2713 }, 2714 { 2715 Name: "ALTER AUTO INCREMENT TABLE ADD column", 2716 SetUpScript: []string{ 2717 "CREATE TABLE test (pk int primary key, uk int UNIQUE KEY auto_increment);", 2718 }, 2719 Assertions: []ScriptTestAssertion{ 2720 { 2721 Query: "alter table test add column j int;", 2722 Expected: []sql.Row{{types.NewOkResult(0)}}, 2723 }, 2724 }, 2725 }, 2726 { 2727 Name: "alter json column default; from scorewarrior: https://github.com/dolthub/dolt/issues/4543", 2728 SetUpScript: []string{ 2729 "CREATE TABLE test (i int default 999, j json);", 2730 }, 2731 Assertions: []ScriptTestAssertion{ 2732 { 2733 Query: "alter table test alter column j set default ('[]');", 2734 Expected: []sql.Row{{types.NewOkResult(0)}}, 2735 }, 2736 { 2737 Query: "show create table test", 2738 Expected: []sql.Row{ 2739 {"test", "CREATE TABLE `test` (\n `i` int DEFAULT '999',\n `j` json DEFAULT ('[]')\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}, 2740 }, 2741 }, 2742 }, 2743 }, 2744 { 2745 Name: "ALTER TABLE MULTI ADD/DROP COLUMN", 2746 SetUpScript: []string{ 2747 "CREATE TABLE test (pk BIGINT PRIMARY KEY, v1 BIGINT NOT NULL DEFAULT 88);", 2748 }, 2749 Assertions: []ScriptTestAssertion{ 2750 { 2751 Query: "INSERT INTO test (pk) VALUES (1);", 2752 Expected: []sql.Row{{types.NewOkResult(1)}}, 2753 }, 2754 { 2755 Query: "ALTER TABLE test DROP COLUMN v1, ADD COLUMN v2 INT NOT NULL DEFAULT 100", 2756 Expected: []sql.Row{{types.NewOkResult(0)}}, 2757 }, 2758 { 2759 Query: "describe test", 2760 Expected: []sql.Row{ 2761 {"pk", "bigint", "NO", "PRI", "NULL", ""}, 2762 {"v2", "int", "NO", "", "100", ""}, 2763 }, 2764 }, 2765 { 2766 Query: "ALTER TABLE TEST MODIFY COLUMN pk BIGINT AUTO_INCREMENT, AUTO_INCREMENT = 100", 2767 Expected: []sql.Row{{types.NewOkResult(0)}}, 2768 }, 2769 { 2770 Query: "INSERT INTO test (v2) values (11)", 2771 Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 100}}}, 2772 }, 2773 { 2774 Query: "SELECT * from test where pk = 100", 2775 Expected: []sql.Row{{100, 11}}, 2776 }, 2777 { 2778 Query: "ALTER TABLE test DROP COLUMN v2, ADD COLUMN v3 int NOT NULL after v2", 2779 ExpectedErr: sql.ErrTableColumnNotFound, 2780 }, 2781 { 2782 Query: "describe test", 2783 Expected: []sql.Row{ 2784 {"pk", "bigint", "NO", "PRI", "NULL", "auto_increment"}, 2785 {"v2", "int", "NO", "", "100", ""}, 2786 }, 2787 }, 2788 { 2789 Query: "ALTER TABLE test DROP COLUMN v2, RENAME COLUMN v2 to v3", 2790 ExpectedErr: sql.ErrTableColumnNotFound, 2791 }, 2792 { 2793 Query: "describe test", 2794 Expected: []sql.Row{ 2795 {"pk", "bigint", "NO", "PRI", "NULL", "auto_increment"}, 2796 {"v2", "int", "NO", "", "100", ""}, 2797 }, 2798 }, 2799 { 2800 Query: "ALTER TABLE test RENAME COLUMN v2 to v3, DROP COLUMN v2", 2801 ExpectedErr: sql.ErrTableColumnNotFound, 2802 }, 2803 { 2804 Query: "describe test", 2805 Expected: []sql.Row{ 2806 {"pk", "bigint", "NO", "PRI", "NULL", "auto_increment"}, 2807 {"v2", "int", "NO", "", "100", ""}, 2808 }, 2809 }, 2810 { 2811 Query: "ALTER TABLE test ADD COLUMN (v3 int NOT NULL), add column (v4 int), drop column v2, add column (v5 int NOT NULL)", 2812 Expected: []sql.Row{{types.NewOkResult(0)}}, 2813 }, 2814 { 2815 Query: "DESCRIBE test", 2816 Expected: []sql.Row{ 2817 {"pk", "bigint", "NO", "PRI", "NULL", "auto_increment"}, 2818 {"v3", "int", "NO", "", "NULL", ""}, 2819 {"v4", "int", "YES", "", "NULL", ""}, 2820 {"v5", "int", "NO", "", "NULL", ""}, 2821 }, 2822 }, 2823 { 2824 Query: "ALTER TABLE test ADD COLUMN (v6 int not null), RENAME COLUMN v5 TO mycol, DROP COLUMN v4, ADD COLUMN (v7 int);", 2825 Expected: []sql.Row{{types.NewOkResult(0)}}, 2826 }, 2827 { 2828 Query: "describe test", 2829 Expected: []sql.Row{ 2830 {"pk", "bigint", "NO", "PRI", "NULL", "auto_increment"}, 2831 {"v3", "int", "NO", "", "NULL", ""}, 2832 {"mycol", "int", "NO", "", "NULL", ""}, 2833 {"v6", "int", "NO", "", "NULL", ""}, 2834 {"v7", "int", "YES", "", "NULL", ""}, 2835 }, 2836 }, 2837 // TODO: Does not include tests with column renames and defaults. 2838 }, 2839 }, 2840 { 2841 // https://github.com/dolthub/dolt/issues/3065 2842 Name: "join index lookups do not handle filters", 2843 SetUpScript: []string{ 2844 "create table a (x int primary key)", 2845 "create table b (y int primary key, x int, index idx_x(x))", 2846 "create table c (z int primary key, x int, y int, index idx_x(x))", 2847 "insert into a values (0),(1),(2),(3)", 2848 "insert into b values (0,1), (1,1), (2,2), (3,2)", 2849 "insert into c values (0,1,0), (1,1,0), (2,2,1), (3,2,1)", 2850 }, 2851 Query: "select a.* from a join b on a.x = b.x join c where c.x = a.x and b.x = 1", 2852 Expected: []sql.Row{ 2853 {1}, 2854 {1}, 2855 {1}, 2856 {1}, 2857 }, 2858 }, 2859 { 2860 Name: "failed conversion shows warning", 2861 Assertions: []ScriptTestAssertion{ 2862 { 2863 Query: "SELECT CONVERT('10000-12-31 23:59:59', DATETIME)", 2864 ExpectedWarning: 1292, 2865 ExpectedWarningsCount: 1, 2866 ExpectedWarningMessageSubstring: "Incorrect datetime value: 10000-12-31 23:59:59", 2867 SkipResultsCheck: true, 2868 }, 2869 { 2870 Query: "SELECT CONVERT('this is not a datetime', DATETIME)", 2871 ExpectedWarning: 1292, 2872 ExpectedWarningsCount: 1, 2873 ExpectedWarningMessageSubstring: "Incorrect datetime value: this is not a datetime", 2874 SkipResultsCheck: true, 2875 }, 2876 { 2877 Query: "SELECT CAST('this is not a datetime' as DATETIME)", 2878 ExpectedWarning: 1292, 2879 ExpectedWarningsCount: 1, 2880 ExpectedWarningMessageSubstring: "Incorrect datetime value: this is not a datetime", 2881 SkipResultsCheck: true, 2882 }, 2883 { 2884 Query: "SELECT CONVERT('this is not a date', DATE)", 2885 ExpectedWarning: 1292, 2886 ExpectedWarningsCount: 1, 2887 ExpectedWarningMessageSubstring: "Incorrect date value: this is not a date", 2888 SkipResultsCheck: true, 2889 }, 2890 { 2891 Query: "SELECT CAST('this is not a date' as DATE)", 2892 ExpectedWarning: 1292, 2893 ExpectedWarningsCount: 1, 2894 ExpectedWarningMessageSubstring: "Incorrect date value: this is not a date", 2895 SkipResultsCheck: true, 2896 }, 2897 }, 2898 }, 2899 { 2900 Name: "Describe with expressions and views work correctly", 2901 SetUpScript: []string{ 2902 "CREATE TABLE t(pk int primary key, val int DEFAULT (pk * 2))", 2903 }, 2904 Assertions: []ScriptTestAssertion{ 2905 { 2906 Query: "DESCRIBE t", 2907 Expected: []sql.Row{ 2908 {"pk", "int", "NO", "PRI", "NULL", ""}, 2909 {"val", "int", "YES", "", "((`pk` * 2))", "DEFAULT_GENERATED"}, 2910 }, 2911 }, 2912 }, 2913 }, 2914 { 2915 Name: "Check support for deprecated BINARY attribute after character set", 2916 SetUpScript: []string{ 2917 "CREATE TABLE test (pk BIGINT PRIMARY KEY, v1 VARCHAR(255) COLLATE utf8mb4_0900_bin);", 2918 }, 2919 Assertions: []ScriptTestAssertion{ 2920 { 2921 Query: "SHOW CREATE TABLE test;", 2922 Expected: []sql.Row{{"test", "CREATE TABLE `test` (\n `pk` bigint NOT NULL,\n `v1` varchar(255),\n PRIMARY KEY (`pk`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 2923 }, 2924 { 2925 Query: "ALTER TABLE test CHANGE v1 v2 VARCHAR(255) CHARACTER SET utf8mb4 BINARY NOT NULL;", 2926 Expected: []sql.Row{{types.NewOkResult(0)}}, 2927 }, 2928 { 2929 Query: "SHOW CREATE TABLE test;", 2930 Expected: []sql.Row{{"test", "CREATE TABLE `test` (\n `pk` bigint NOT NULL,\n `v2` varchar(255) COLLATE utf8mb4_bin NOT NULL,\n PRIMARY KEY (`pk`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 2931 }, 2932 { 2933 Query: "CREATE TABLE test2 (pk BIGINT PRIMARY KEY, v1 VARCHAR(255) CHARACTER SET utf8mb4 BINARY);", 2934 Expected: []sql.Row{{types.NewOkResult(0)}}, 2935 }, 2936 { 2937 Query: "SHOW CREATE TABLE test2;", 2938 Expected: []sql.Row{{"test2", "CREATE TABLE `test2` (\n `pk` bigint NOT NULL,\n `v1` varchar(255) COLLATE utf8mb4_bin,\n PRIMARY KEY (`pk`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 2939 }, 2940 }, 2941 }, 2942 { 2943 Name: "sum() and avg() on DECIMAL type column returns the DECIMAL type result", 2944 SetUpScript: []string{ 2945 "create table decimal_table (id int, val decimal(18,16));", 2946 "insert into decimal_table values (1,-2.5633000000000384);", 2947 "insert into decimal_table values (2,2.5633000000000370);", 2948 "insert into decimal_table values (3,0.0000000000000004);", 2949 }, 2950 Assertions: []ScriptTestAssertion{ 2951 { 2952 Query: "SELECT val FROM decimal_table;", 2953 Expected: []sql.Row{{"-2.5633000000000384"}, {"2.5633000000000370"}, {"0.0000000000000004"}}, 2954 }, 2955 { 2956 Query: "SELECT sum(val) FROM decimal_table;", 2957 Expected: []sql.Row{{"-0.0000000000000010"}}, 2958 }, 2959 { 2960 Query: "SELECT avg(val) FROM decimal_table;", 2961 Expected: []sql.Row{{"-0.00000000000000033333"}}, 2962 }, 2963 }, 2964 }, 2965 { 2966 Name: "sum() and avg() on non-DECIMAL type column returns the DOUBLE type result", 2967 SetUpScript: []string{ 2968 "create table float_table (id int primary key, val1 double, val2 float);", 2969 "insert into float_table values (1,-2.5633000000000384, 2.3);", 2970 "insert into float_table values (2,2.5633000000000370, 2.4);", 2971 "insert into float_table values (3,0.0000000000000004, 5.3);", 2972 }, 2973 Assertions: []ScriptTestAssertion{ 2974 { 2975 Query: "SELECT sum(id), sum(val1), sum(val2) FROM float_table ORDER BY id;", 2976 Expected: []sql.Row{{float64(6), -9.322676295501879e-16, 10.000000238418579}}, 2977 }, 2978 { 2979 Query: "SELECT sum(id), sum(val1), sum(val2) FROM float_table ORDER BY id;", 2980 Expected: []sql.Row{{float64(6), -9.322676295501879e-16, 10.000000238418579}}, 2981 }, 2982 { 2983 Query: "SELECT avg(id), avg(val1), avg(val2) FROM float_table ORDER BY id;", 2984 Expected: []sql.Row{{float64(2), -3.107558765167293e-16, 3.333333412806193}}, 2985 }, 2986 }, 2987 }, 2988 { 2989 Name: "compare DECIMAL type columns with different precision and scale", 2990 SetUpScript: []string{ 2991 "create table t (id int primary key, val1 decimal(2, 1), val2 decimal(3, 1));", 2992 "insert into t values (1, 1.2, 1.1), (2, 1.2, 10.1);", 2993 }, 2994 Assertions: []ScriptTestAssertion{ 2995 { 2996 Query: "select if(val1 < val2, 'YES', 'NO') from t order by id;", 2997 Expected: []sql.Row{{"NO"}, {"YES"}}, 2998 }, 2999 }, 3000 }, 3001 { 3002 Name: "basic test on tables dual and `dual`", 3003 SetUpScript: []string{ 3004 "CREATE TABLE `dual` (id int)", 3005 "INSERT INTO `dual` VALUES (2)", 3006 }, 3007 Assertions: []ScriptTestAssertion{ 3008 { 3009 Query: "SELECT * from `dual`;", 3010 Expected: []sql.Row{{2}}, 3011 }, 3012 { 3013 Query: "SELECT 3 from dual;", 3014 Expected: []sql.Row{{3}}, 3015 }, 3016 { 3017 Query: "SELECT * from dual;", 3018 ExpectedErr: sql.ErrNoTablesUsed, 3019 }, 3020 }, 3021 }, 3022 { 3023 Name: "having clause without groupby clause, all rows implicitly form a single aggregate group", 3024 SetUpScript: []string{ 3025 "create table numbers (val int);", 3026 "insert into numbers values (1), (2), (3);", 3027 "insert into numbers values (2), (4);", 3028 }, 3029 Assertions: []ScriptTestAssertion{ 3030 { 3031 Query: "select val from numbers;", 3032 Expected: []sql.Row{{1}, {2}, {3}, {2}, {4}}, 3033 }, 3034 { 3035 Query: "select val as a from numbers having a = val;", 3036 Expected: []sql.Row{{1}, {2}, {3}, {2}, {4}}, 3037 }, 3038 { 3039 Query: "select val as a from numbers group by val having a = val;", 3040 Expected: []sql.Row{{1}, {2}, {3}, {4}}, 3041 }, 3042 { 3043 Query: "select val as a from numbers as t1 group by t1.val having a = t1.val;", 3044 Expected: []sql.Row{{1}, {2}, {3}, {4}}, 3045 }, 3046 { 3047 Query: "select t1.val as a from numbers as t1 group by 1 having a = t1.val;", 3048 Expected: []sql.Row{{1}, {2}, {3}, {4}}, 3049 }, 3050 { 3051 Query: "select t1.val as a from numbers as t1 having a = t1.val;", 3052 Expected: []sql.Row{{1}, {2}, {3}, {2}, {4}}, 3053 }, 3054 { 3055 Query: "select count(*) from numbers having count(*) = 5;", 3056 Expected: []sql.Row{{5}}, 3057 }, 3058 { 3059 // MySQL returns `Unknown column 'val' in 'having clause'` error for this query, 3060 // but GMS builds GroupBy for any aggregate function. 3061 Skip: true, 3062 Query: "select count(*) from numbers having count(*) > val;", 3063 //ExpectedErrStr: "found HAVING clause with no GROUP BY", // not the exact error we want 3064 }, 3065 { 3066 Query: "select count(*) from numbers group by val having count(*) < val;", 3067 Expected: []sql.Row{{1}, {1}}, 3068 }, 3069 }, 3070 }, 3071 { 3072 Name: "using having and group by clauses in subquery ", 3073 SetUpScript: []string{ 3074 "CREATE TABLE t (i int, t varchar(2));", 3075 "insert into t values (1, 'a'), (1, 'a2'), (2, 'b'), (3, 'c'), (3, 'c2'), (4, 'd'), (5, 'e'), (5, 'e2');", //, (6, 'f'), (7, 'g'), (7, 'g2') 3076 }, 3077 Assertions: []ScriptTestAssertion{ 3078 { 3079 Query: "select i from t group by i having count(1) = 1 order by i asc", 3080 Expected: []sql.Row{{2}, {4}}, 3081 }, 3082 { 3083 Query: "select i from t group by i having count(1) != 1 order by i asc", 3084 Expected: []sql.Row{{1}, {3}, {5}}, 3085 }, 3086 { 3087 Query: "select * from t where i in (select i from t group by i having count(1) = 1) order by i, t asc;", 3088 Expected: []sql.Row{{2, "b"}, {4, "d"}}, 3089 }, 3090 { 3091 Query: "select * from t where i in (select i from t group by i having count(1) != 1) order by i, t asc;", 3092 Expected: []sql.Row{{1, "a"}, {1, "a2"}, {3, "c"}, {3, "c2"}, {5, "e"}, {5, "e2"}}, 3093 }, 3094 { 3095 Query: "select * from t where i in (select i from t where i = 2 group by i having count(1) = 1) order by i, t asc;", 3096 Expected: []sql.Row{{2, "b"}}, 3097 }, 3098 { 3099 Query: "select * from t where i in (select i from t where i = 3 group by i having count(1) != 1) order by i, t asc;", 3100 Expected: []sql.Row{{3, "c"}, {3, "c2"}}, 3101 }, 3102 { 3103 Query: "select * from t where i in (select i from t where i > 2 group by i having count(1) != 1) order by i, t asc;", 3104 Expected: []sql.Row{{3, "c"}, {3, "c2"}, {5, "e"}, {5, "e2"}}, 3105 }, 3106 { 3107 Query: "select * from t where i in (select i from t where i > 2 group by i having count(1) != 1 order by i desc) order by i, t asc;", 3108 Expected: []sql.Row{{3, "c"}, {3, "c2"}, {5, "e"}, {5, "e2"}}, 3109 }, 3110 { 3111 Query: "select * from t where i in (select i from t where i > 2 group by i having count(1) != 1) order by i desc, t asc;", 3112 Expected: []sql.Row{{5, "e"}, {5, "e2"}, {3, "c"}, {3, "c2"}}, 3113 }, 3114 }, 3115 }, 3116 { 3117 Name: "can't create view with same name as existing table", 3118 SetUpScript: []string{ 3119 "create table t (i int);", 3120 }, 3121 Assertions: []ScriptTestAssertion{ 3122 { 3123 Query: "create view t as select 1 from dual", 3124 ExpectedErr: sql.ErrTableAlreadyExists, 3125 }, 3126 }, 3127 }, 3128 { 3129 Name: "can't create table with same name as existing view", 3130 SetUpScript: []string{ 3131 "create view t as select 1 from dual", 3132 }, 3133 Assertions: []ScriptTestAssertion{ 3134 { 3135 Query: "create table t (i int);", 3136 ExpectedErr: sql.ErrTableAlreadyExists, 3137 }, 3138 }, 3139 }, 3140 { 3141 Name: "'/' division operation result in decimal or float", 3142 SetUpScript: []string{ 3143 "create table floats (f float);", 3144 "insert into floats values (1.1), (1.2), (1.3);", 3145 "create table decimals (d decimal(2,1));", 3146 "insert into decimals values (1.0), (2.0), (2.5);", 3147 }, 3148 Assertions: []ScriptTestAssertion{ 3149 { 3150 Query: "select f/2 from floats;", 3151 Expected: []sql.Row{{0.550000011920929}, {0.6000000238418579}, {0.6499999761581421}}, 3152 }, 3153 { 3154 Query: "select 2/f from floats;", 3155 Expected: []sql.Row{{1.8181817787737895}, {1.6666666004392863}, {1.5384615948919735}}, 3156 }, 3157 { 3158 Query: "select d/2 from decimals;", 3159 Expected: []sql.Row{{"0.50000"}, {"1.00000"}, {"1.25000"}}, 3160 }, 3161 { 3162 Query: "select 2/d from decimals;", 3163 Expected: []sql.Row{{"2.0000"}, {"1.0000"}, {"0.8000"}}, 3164 }, 3165 { 3166 Query: "select f/d from floats, decimals;", 3167 Expected: []sql.Row{{1.2999999523162842}, {1.2000000476837158}, {1.100000023841858}, 3168 {0.6499999761581421}, {0.6000000238418579}, {0.550000011920929}, 3169 {0.5199999809265137}, {0.48000001907348633}, {0.4400000095367432}}, 3170 }, 3171 { 3172 Query: "select d/f from floats, decimals;", 3173 Expected: []sql.Row{{0.7692307974459868}, {0.8333333002196431}, {0.9090908893868948}, 3174 {1.5384615948919735}, {1.6666666004392863}, {1.8181817787737895}, 3175 {1.9230769936149668}, {2.083333250549108}, {2.272727223467237}}, 3176 }, 3177 { 3178 Query: `select f/'a' from floats;`, 3179 Expected: []sql.Row{{nil}, {nil}, {nil}}, 3180 }, 3181 }, 3182 }, 3183 { 3184 Name: "'%' mod operation result in decimal or float", 3185 SetUpScript: []string{ 3186 "create table a (pk int primary key, c1 int, c2 double, c3 decimal(5,3));", 3187 "insert into a values (1, 1, 1.111, 1.111), (2, 2, 2.111, 2.111);", 3188 }, 3189 Assertions: []ScriptTestAssertion{ 3190 { 3191 Query: "select c1 % 2, c2 % 2, c3 % 2 from a;", 3192 Expected: []sql.Row{{"1", 1.111, "1.111"}, {"0", 0.11100000000000021, "0.111"}}, 3193 }, 3194 { 3195 Query: "select c1 % 0.5, c2 % 0.5, c3 % 0.5 from a;", 3196 Expected: []sql.Row{{"0.0", 0.11099999999999999, "0.111"}, {"0.0", 0.11100000000000021, "0.111"}}, 3197 }, 3198 { 3199 Query: "select 20 % c1, 20 % c2, 20 % c3 from a;", 3200 Expected: []sql.Row{{"0", 0.002000000000000224, "0.002"}, {"0", 1.0009999999999981, "1.001"}}, 3201 }, 3202 }, 3203 }, 3204 { 3205 Name: "arithmetic bit operations on int, float and decimal types", 3206 SetUpScript: []string{ 3207 "CREATE TABLE num_types (pk int primary key, a int, b float, c decimal(5,3));", 3208 "insert into num_types values (1,1,1.1,1.1), (2,2,1.2,2.2), (3,3,1.6,3.7), (4,4,1.7,4.0);", 3209 }, 3210 Assertions: []ScriptTestAssertion{ 3211 { 3212 Query: "select a & 2.4, a | 2.4, a ^ 2.4 from num_types;", 3213 Expected: []sql.Row{ 3214 {uint64(0), uint64(3), uint64(3)}, 3215 {uint64(2), uint64(2), uint64(0)}, 3216 {uint64(2), uint64(3), uint64(1)}, 3217 {uint64(0), uint64(6), uint64(6)}, 3218 }, 3219 }, 3220 { 3221 Query: "select b & 2.4, b | 2.4, b ^ 2.4 from num_types;", 3222 Expected: []sql.Row{ 3223 {uint64(0), uint64(3), uint64(3)}, 3224 {uint64(0), uint64(3), uint64(3)}, 3225 {uint64(2), uint64(2), uint64(0)}, 3226 {uint64(2), uint64(2), uint64(0)}, 3227 }, 3228 }, 3229 { 3230 Query: "select c & 2.4, c | 2.4, c ^ 2.4 from num_types;", 3231 Expected: []sql.Row{ 3232 {uint64(0), uint64(3), uint64(3)}, 3233 {uint64(2), uint64(2), uint64(0)}, 3234 {uint64(0), uint64(6), uint64(6)}, 3235 {uint64(0), uint64(6), uint64(6)}, 3236 }, 3237 }, 3238 }, 3239 }, 3240 { 3241 Name: "year type behavior", 3242 SetUpScript: []string{ 3243 "create table t (pk int primary key, col1 year);", 3244 }, 3245 Assertions: []ScriptTestAssertion{ 3246 // 1901 - 2155 are interpreted as 1901 - 2155 3247 { 3248 Query: "INSERT INTO t VALUES (1, '1901'), (2, 1901);", 3249 Expected: []sql.Row{{types.NewOkResult(2)}}, 3250 }, 3251 { 3252 Query: "INSERT INTO t VALUES (3, '2000'), (4, 2000);", 3253 Expected: []sql.Row{{types.NewOkResult(2)}}, 3254 }, 3255 { 3256 Query: "INSERT INTO t VALUES (5, '2155'), (6, 2155);", 3257 Expected: []sql.Row{{types.NewOkResult(2)}}, 3258 }, 3259 // 1 - 69 are interpreted as 2001 - 2069 3260 { 3261 Query: "INSERT INTO t VALUES (7, '1'), (8, 1);", 3262 Expected: []sql.Row{{types.NewOkResult(2)}}, 3263 }, 3264 { 3265 Query: "INSERT INTO t VALUES (9, '35'), (10, 35);", 3266 Expected: []sql.Row{{types.NewOkResult(2)}}, 3267 }, 3268 { 3269 Query: "INSERT INTO t VALUES (11, '69'), (12, 69);", 3270 Expected: []sql.Row{{types.NewOkResult(2)}}, 3271 }, 3272 // 70 - 99 are interpreted as 1970 - 1999 3273 { 3274 Query: "INSERT INTO t VALUES (13, '70'), (14, 70);", 3275 Expected: []sql.Row{{types.NewOkResult(2)}}, 3276 }, 3277 { 3278 Query: "INSERT INTO t VALUES (15, '85'), (16, 85);", 3279 Expected: []sql.Row{{types.NewOkResult(2)}}, 3280 }, 3281 { 3282 Query: "INSERT INTO t VALUES (17, '99'), (18, 99);", 3283 Expected: []sql.Row{{types.NewOkResult(2)}}, 3284 }, 3285 // '0', and '00' are interpreted as 2000 3286 { 3287 Query: "INSERT INTO t VALUES (19, '0'), (20, '00');", 3288 Expected: []sql.Row{{types.NewOkResult(2)}}, 3289 }, 3290 // 0 is interpreted as 0000 3291 { 3292 Query: "INSERT INTO t VALUES (21, 0)", 3293 Expected: []sql.Row{{types.NewOkResult(1)}}, 3294 }, 3295 // Assert that returned values are correct. 3296 { 3297 Query: "SELECT * from t order by pk;", 3298 Expected: []sql.Row{ 3299 {1, int16(1901)}, 3300 {2, int16(1901)}, 3301 {3, int16(2000)}, 3302 {4, int16(2000)}, 3303 {5, int16(2155)}, 3304 {6, int16(2155)}, 3305 {7, int16(2001)}, 3306 {8, int16(2001)}, 3307 {9, int16(2035)}, 3308 {10, int16(2035)}, 3309 {11, int16(2069)}, 3310 {12, int16(2069)}, 3311 {13, int16(1970)}, 3312 {14, int16(1970)}, 3313 {15, int16(1985)}, 3314 {16, int16(1985)}, 3315 {17, int16(1999)}, 3316 {18, int16(1999)}, 3317 {19, int16(2000)}, 3318 {20, int16(2000)}, 3319 {21, int16(0)}, 3320 }, 3321 }, 3322 }, 3323 }, 3324 { 3325 Name: "INSERT IGNORE correctly truncates column data", 3326 SetUpScript: []string{ 3327 `CREATE TABLE t ( 3328 pk int primary key, 3329 col1 boolean, 3330 col2 integer, 3331 col3 tinyint, 3332 col4 smallint, 3333 col5 mediumint, 3334 col6 int, 3335 col7 bigint, 3336 col8 decimal, 3337 col9 float, 3338 col10 double, 3339 col11 date, 3340 col12 time, 3341 col13 datetime, 3342 col14 timestamp, 3343 col15 year, 3344 col16 ENUM('first', 'second'), 3345 col17 SET('a', 'b') 3346 );`, 3347 }, 3348 Assertions: []ScriptTestAssertion{ 3349 { 3350 Query: ` 3351 INSERT IGNORE INTO t VALUES ( 3352 1, 'val1', 'val2', 'val3', 'val4', 'val5', 'val6', 'val7', 'val8', 'val9', 'val10', 3353 'val11', 'val12', 'val13', 'val14', 'val15', 'val16', 'val17' 3354 ); 3355 `, 3356 Expected: []sql.Row{{types.NewOkResult(1)}}, 3357 }, 3358 { 3359 SkipResultCheckOnServerEngine: true, // the datetime returned is not non-zero 3360 Query: "SELECT * from t", 3361 Expected: []sql.Row{ 3362 { 3363 1, 3364 0, 3365 0, 3366 0, 3367 0, 3368 0, 3369 0, 3370 0, 3371 "0", 3372 float64(0), 3373 float64(0), 3374 time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), 3375 types.Timespan(0), 3376 time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), 3377 time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), 3378 0, 3379 "first", 3380 "", 3381 }, 3382 }, 3383 }, 3384 }, 3385 }, 3386 { 3387 Name: "scientific notation for floats", 3388 SetUpScript: []string{ 3389 "create table t (b bigint unsigned);", 3390 }, 3391 Assertions: []ScriptTestAssertion{ 3392 { 3393 Query: "insert into t values (5.2443381514267e+18);", 3394 Expected: []sql.Row{{types.NewOkResult(1)}}, 3395 }, 3396 }, 3397 }, 3398 { 3399 Name: "INSERT IGNORE throws an error when json is badly formatted", 3400 SetUpScript: []string{ 3401 "CREATE TABLE t (pk int primary key, col1 json);", 3402 }, 3403 Assertions: []ScriptTestAssertion{ 3404 { 3405 Query: "INSERT IGNORE into t VALUES (1, 'val1');", 3406 ExpectedErr: sql.ErrInvalidJson, 3407 }, 3408 }, 3409 }, 3410 { 3411 Name: "hash lookup for joins works with binary", 3412 SetUpScript: []string{ 3413 "create table uv (u int primary key, v int);", 3414 "create table xy (x int primary key, y int);", 3415 "insert into uv values (0,0), (1,1), (2,2);", 3416 "insert into xy values (0,0), (1,1), (2,2);", 3417 }, 3418 Assertions: []ScriptTestAssertion{ 3419 { 3420 Query: "select uv.u from uv join xy on binary xy.x = binary uv.u;", 3421 Expected: []sql.Row{ 3422 {0}, 3423 {1}, 3424 {2}, 3425 }, 3426 }, 3427 }, 3428 }, 3429 { 3430 Name: "enum columns work as expected in when clauses", 3431 SetUpScript: []string{ 3432 "create table enums (e enum('a'));", 3433 "insert into enums values ('a');", 3434 }, 3435 Assertions: []ScriptTestAssertion{ 3436 { 3437 Query: "select (case e when 'a' then 42 end) from enums", 3438 Expected: []sql.Row{{42}}, 3439 }, 3440 { 3441 Query: "select (case 'a' when e then 42 end) from enums", 3442 Expected: []sql.Row{{42}}, 3443 }, 3444 }, 3445 }, 3446 { 3447 Name: "SET and ENUM properly handle integers using UPDATE and DELETE statements", 3448 SetUpScript: []string{ 3449 "CREATE TABLE setenumtest (pk INT PRIMARY KEY, v1 ENUM('a', 'b', 'c'), v2 SET('a', 'b', 'c'));", 3450 }, 3451 Assertions: []ScriptTestAssertion{ 3452 { 3453 Query: "INSERT INTO setenumtest VALUES (1, 1, 1), (2, 1, 1), (3, 3, 1), (4, 1, 3);", 3454 Expected: []sql.Row{{types.NewOkResult(4)}}, 3455 }, 3456 { 3457 Query: "UPDATE setenumtest SET v1 = 2, v2 = 2 WHERE pk = 2;", 3458 Expected: []sql.Row{{types.OkResult{ 3459 RowsAffected: 1, 3460 Info: plan.UpdateInfo{ 3461 Matched: 1, 3462 Updated: 1, 3463 Warnings: 0, 3464 }, 3465 }}}, 3466 }, 3467 { 3468 Query: "SELECT * FROM setenumtest ORDER BY pk;", 3469 Expected: []sql.Row{ 3470 {1, "a", "a"}, 3471 {2, "b", "b"}, 3472 {3, "c", "a"}, 3473 {4, "a", "a,b"}, 3474 }, 3475 }, 3476 { 3477 Query: "DELETE FROM setenumtest WHERE v1 = 3;", 3478 Expected: []sql.Row{{types.NewOkResult(1)}}, 3479 }, 3480 { 3481 Query: "DELETE FROM setenumtest WHERE v2 = 3;", 3482 Expected: []sql.Row{{types.NewOkResult(1)}}, 3483 }, 3484 { 3485 Query: "SELECT * FROM setenumtest ORDER BY pk;", 3486 Expected: []sql.Row{ 3487 {1, "a", "a"}, 3488 {2, "b", "b"}, 3489 }, 3490 }, 3491 }, 3492 }, 3493 { 3494 Name: "identical expressions over different windows should produce different results", 3495 SetUpScript: []string{ 3496 "CREATE TABLE t(a INT, b INT);", 3497 "INSERT INTO t(a, b) VALUES (1, 1), (1, 2), (1, 3), (2, 4), (2, 5), (2, 6);", 3498 }, 3499 Assertions: []ScriptTestAssertion{ 3500 { 3501 Query: "SELECT SUM(b) OVER (PARTITION BY a ORDER BY b) FROM t ORDER BY 1;", 3502 Expected: []sql.Row{{float64(1)}, {float64(3)}, {float64(4)}, {float64(6)}, {float64(9)}, {float64(15)}}, 3503 }, 3504 { 3505 Query: "SELECT SUM(b) OVER (ORDER BY b) FROM t ORDER BY 1;", 3506 Expected: []sql.Row{{float64(1)}, {float64(3)}, {float64(6)}, {float64(10)}, {float64(15)}, {float64(21)}}, 3507 }, 3508 { 3509 Query: "SELECT SUM(b) OVER (PARTITION BY a ORDER BY b), SUM(b) OVER (ORDER BY b) FROM t ORDER BY 1;", 3510 Expected: []sql.Row{ 3511 {float64(1), float64(1)}, 3512 {float64(3), float64(3)}, 3513 {float64(4), float64(10)}, 3514 {float64(6), float64(6)}, 3515 {float64(9), float64(15)}, 3516 {float64(15), float64(21)}, 3517 }, 3518 }, 3519 }, 3520 }, 3521 { 3522 Name: "windows without ORDER BY should be treated as RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING", 3523 SetUpScript: []string{ 3524 "CREATE TABLE t(a INT, b INT);", 3525 "INSERT INTO t(a, b) VALUES (1, 1), (1, 2), (1, 3), (2, 4), (2, 5), (2, 6);", 3526 }, 3527 Assertions: []ScriptTestAssertion{ 3528 { 3529 Query: "SELECT SUM(b) OVER (PARTITION BY a) FROM t ORDER BY 1;", 3530 Expected: []sql.Row{{float64(6)}, {float64(6)}, {float64(6)}, {float64(15)}, {float64(15)}, {float64(15)}}, 3531 }, 3532 { 3533 Query: "SELECT SUM(b) OVER () FROM t ORDER BY 1;", 3534 Expected: []sql.Row{{float64(21)}, {float64(21)}, {float64(21)}, {float64(21)}, {float64(21)}, {float64(21)}}, 3535 }, 3536 { 3537 Query: "SELECT SUM(b) OVER (PARTITION BY a), SUM(b) OVER () FROM t;", 3538 Expected: []sql.Row{ 3539 {float64(6), float64(21)}, 3540 {float64(6), float64(21)}, 3541 {float64(6), float64(21)}, 3542 {float64(15), float64(21)}, 3543 {float64(15), float64(21)}, 3544 {float64(15), float64(21)}, 3545 }, 3546 }, 3547 }, 3548 }, 3549 { 3550 Name: "decimal literals should be parsed correctly", 3551 SetUpScript: []string{ 3552 "SET @testValue = 809826404100301269648758758005707100;", 3553 }, 3554 Assertions: []ScriptTestAssertion{ 3555 { 3556 Query: "SELECT @testValue;", 3557 Expected: []sql.Row{{"809826404100301269648758758005707100"}}, 3558 }, 3559 }, 3560 }, 3561 { 3562 Name: "division and int division operation on negative, small and big value for decimal type column of table", 3563 SetUpScript: []string{ 3564 "create table t (d decimal(25,10) primary key);", 3565 "insert into t values (-4990), (2), (22336578);", 3566 }, 3567 Assertions: []ScriptTestAssertion{ 3568 { 3569 Query: "select d div 314990 from t order by d;", 3570 Expected: []sql.Row{{0}, {0}, {70}}, 3571 }, 3572 { 3573 Query: "select d / 314990 from t order by d;", 3574 Expected: []sql.Row{{"-0.01584177275469"}, {"0.00000634940792"}, {"70.91202260389219"}}, 3575 }, 3576 }, 3577 }, 3578 { 3579 Name: "drop table if exists on unknown table shows warning", 3580 Assertions: []ScriptTestAssertion{ 3581 { 3582 Query: "DROP TABLE IF EXISTS non_existent_table;", 3583 ExpectedWarning: 1051, 3584 ExpectedWarningsCount: 1, 3585 ExpectedWarningMessageSubstring: "Unknown table 'non_existent_table'", 3586 SkipResultsCheck: true, 3587 }, 3588 }, 3589 }, 3590 { 3591 Name: "find_in_set tests", 3592 SetUpScript: []string{ 3593 "create table set_tbl (i int primary key, s set('a','b','c'));", 3594 "insert into set_tbl values (0, '');", 3595 "insert into set_tbl values (1, 'a');", 3596 "insert into set_tbl values (2, 'b');", 3597 "insert into set_tbl values (3, 'c');", 3598 "insert into set_tbl values (4, 'a,b');", 3599 "insert into set_tbl values (6, 'b,c');", 3600 "insert into set_tbl values (7, 'a,c');", 3601 "insert into set_tbl values (8, 'a,b,c');", 3602 3603 "create table collate_tbl (i int primary key, s varchar(10) collate utf8mb4_0900_ai_ci);", 3604 "insert into collate_tbl values (0, '');", 3605 "insert into collate_tbl values (1, 'a');", 3606 "insert into collate_tbl values (2, 'b');", 3607 "insert into collate_tbl values (3, 'c');", 3608 "insert into collate_tbl values (4, 'a,b');", 3609 "insert into collate_tbl values (6, 'b,c');", 3610 "insert into collate_tbl values (7, 'a,c');", 3611 "insert into collate_tbl values (8, 'a,b,c');", 3612 3613 "create table enum_tbl (i int primary key, s enum('a','b','c'));", 3614 "insert into enum_tbl values (0, 'a'), (1, 'b'), (2, 'c');", 3615 "select i, s, find_in_set('a', s) from enum_tbl;", 3616 }, 3617 Assertions: []ScriptTestAssertion{ 3618 { 3619 Query: "select i, find_in_set('a', s) from set_tbl;", 3620 Expected: []sql.Row{ 3621 {0, 0}, 3622 {1, 1}, 3623 {2, 0}, 3624 {3, 0}, 3625 {4, 1}, 3626 {6, 0}, 3627 {7, 1}, 3628 {8, 1}, 3629 }, 3630 }, 3631 { 3632 Query: "select i, find_in_set('A', s) from collate_tbl;", 3633 Expected: []sql.Row{ 3634 {0, 0}, 3635 {1, 1}, 3636 {2, 0}, 3637 {3, 0}, 3638 {4, 1}, 3639 {6, 0}, 3640 {7, 1}, 3641 {8, 1}, 3642 }, 3643 }, 3644 { 3645 Query: "select i, find_in_set('a', s) from enum_tbl;", 3646 Expected: []sql.Row{ 3647 {0, 1}, 3648 {1, 0}, 3649 {2, 0}, 3650 }, 3651 }, 3652 }, 3653 }, 3654 { 3655 Name: "coalesce tests", 3656 SetUpScript: []string{ 3657 "create table c select coalesce(NULL, 1);", 3658 }, 3659 Assertions: []ScriptTestAssertion{ 3660 { 3661 Query: "select * from c;", 3662 Expected: []sql.Row{ 3663 {1}, 3664 }, 3665 }, 3666 { 3667 Query: "select COLUMN_NAME, DATA_TYPE from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='c';", 3668 Expected: []sql.Row{ 3669 {"coalesce(NULL,1)", "int"}, 3670 }, 3671 }, 3672 }, 3673 }, 3674 { 3675 Name: "Keyless Table with Unique Index", 3676 SetUpScript: []string{ 3677 "create table a (x int, val int unique)", 3678 }, 3679 Assertions: []ScriptTestAssertion{ 3680 { 3681 Query: "INSERT INTO a VALUES (1, 1)", 3682 Expected: []sql.Row{{types.NewOkResult(1)}}, 3683 }, 3684 { 3685 Query: "INSERT INTO a VALUES (1, 1)", 3686 ExpectedErr: sql.ErrUniqueKeyViolation, 3687 }, 3688 }, 3689 }, 3690 { 3691 Name: "renaming views with RENAME TABLE ... TO .. statement", 3692 SetUpScript: []string{ 3693 "create table t1 (id int primary key, v1 int);", 3694 "create view v1 as select * from t1;", 3695 }, 3696 Assertions: []ScriptTestAssertion{ 3697 { 3698 Query: "show tables;", 3699 Expected: []sql.Row{{"t1"}, {"v1"}}, 3700 }, 3701 { 3702 Query: "rename table v1 to view1", 3703 Expected: []sql.Row{{types.OkResult{RowsAffected: 0}}}, 3704 }, 3705 { 3706 Query: "show tables;", 3707 Expected: []sql.Row{{"t1"}, {"view1"}}, 3708 }, 3709 { 3710 Query: "rename table view1 to newViewName, t1 to newTableName", 3711 Expected: []sql.Row{{types.OkResult{RowsAffected: 0}}}, 3712 }, 3713 { 3714 Query: "show tables;", 3715 Expected: []sql.Row{{"newTableName"}, {"newViewName"}}, 3716 }, 3717 }, 3718 }, 3719 { 3720 Name: "renaming views with ALTER TABLE ... RENAME .. statement should fail", 3721 SetUpScript: []string{ 3722 "create table t1 (id int primary key, v1 int);", 3723 "create view v1 as select * from t1;", 3724 }, 3725 Assertions: []ScriptTestAssertion{ 3726 { 3727 Query: "show tables;", 3728 Expected: []sql.Row{{"t1"}, {"v1"}}, 3729 }, 3730 { 3731 Query: "alter table v1 rename to view1", 3732 ExpectedErr: sql.ErrExpectedTableFoundView, 3733 }, 3734 { 3735 Query: "show tables;", 3736 Expected: []sql.Row{{"t1"}, {"v1"}}, 3737 }, 3738 }, 3739 }, 3740 { 3741 Name: "timezone default settings", 3742 Assertions: []ScriptTestAssertion{ 3743 { 3744 // TODO: Skipping this test while we figure out why this change causes the mysql java 3745 // connector integration test to fail. 3746 Skip: true, 3747 // To match MySQL's behavior, this comes from the operating system's timezone setting 3748 // TODO: the "global" shouldn't be necessary here, but GMS goes to session without it 3749 Query: `select @@global.system_time_zone;`, 3750 Expected: []sql.Row{{gmstime.SystemTimezoneOffset()}}, 3751 }, 3752 { 3753 // The default time_zone setting for MySQL is SYSTEM, which means timezone comes from @@system_time_zone 3754 Query: `select @@time_zone;`, 3755 Expected: []sql.Row{{"SYSTEM"}}, 3756 }, 3757 }, 3758 }, 3759 { 3760 Name: "current time functions", 3761 Assertions: []ScriptTestAssertion{ 3762 { 3763 // Smoke test that NOW() and UTC_TIMESTAMP() return non-null values with the SYSTEM time zone 3764 Query: `select @@time_zone, NOW() IS NOT NULL, UTC_TIMESTAMP() IS NOT NULL;`, 3765 Expected: []sql.Row{{"SYSTEM", true, true}}, 3766 }, 3767 { 3768 // CURTIME() returns the same time as NOW() with the SYSTEM timezone 3769 // TODO: TIME(NOW()) would be simpler test logic, but doesn't work correctly here. 3770 Query: `select @@time_zone, NOW() LIKE CONCAT('%', CURTIME(), '%');`, 3771 Expected: []sql.Row{{"SYSTEM", true}}, 3772 }, 3773 { 3774 // Set the timezone set to UTC as an offset 3775 Query: `set @@time_zone='+00:00';`, 3776 Expected: []sql.Row{{}}, 3777 }, 3778 { 3779 // When the session's time zone is set to UTC, NOW() and UTC_TIMESTAMP() should return the same value 3780 Query: `select @@time_zone, NOW(6) = UTC_TIMESTAMP();`, 3781 Expected: []sql.Row{{"+00:00", true}}, 3782 }, 3783 { 3784 // CURTIME() returns the same time as NOW() with UTC's timezone offset 3785 Query: `select @@time_zone, NOW() LIKE CONCAT('%', CURTIME(), '%');`, 3786 Expected: []sql.Row{{"+00:00", true}}, 3787 }, 3788 { 3789 Query: `set @@time_zone='+02:00';`, 3790 Expected: []sql.Row{{}}, 3791 }, 3792 { 3793 // When the session's time zone is set to +2:00, NOW() should report two hours ahead of UTC_TIMESTAMP() 3794 Query: `select @@time_zone, TIMESTAMPDIFF(MINUTE, NOW(6), UTC_TIMESTAMP());`, 3795 Expected: []sql.Row{{"+02:00", -120}}, 3796 }, 3797 { 3798 // CURTIME() returns the same time as NOW() with a +2:00 timezone offset 3799 Query: `select @@time_zone, NOW() LIKE CONCAT('%', CURTIME(), '%');`, 3800 Expected: []sql.Row{{"+02:00", true}}, 3801 }, 3802 }, 3803 }, 3804 { 3805 Name: "timestamp timezone conversion", 3806 SetUpScript: []string{ 3807 "set time_zone='+00:00';", 3808 "create table timezonetest(pk int primary key, dt datetime, ts timestamp);", 3809 "insert into timezonetest values(1, '2020-02-14 12:00:00', '2020-02-14 12:00:00');", 3810 }, 3811 Assertions: []ScriptTestAssertion{ 3812 { 3813 // When reading back the datetime and timestamp values in the same time zone we entered them, 3814 // we should get the exact same results back. 3815 Query: `select * from timezonetest;`, 3816 Expected: []sql.Row{{1, 3817 time.Date(2020, time.February, 14, 12, 0, 0, 0, time.UTC), 3818 time.Date(2020, time.February, 14, 12, 0, 0, 0, time.UTC)}}, 3819 }, 3820 { 3821 Query: `set @@time_zone='-08:00';`, 3822 Expected: []sql.Row{{}}, 3823 }, 3824 { 3825 // TODO: Unskip after adding support for converting timestamp values to/from session time_zone 3826 Skip: true, 3827 // After changing the session's time zone, we should get back a different result for the timestamp 3828 // column, but the same result for the datetime column. 3829 Query: `select * from timezonetest;`, 3830 Expected: []sql.Row{{1, 3831 time.Date(2020, time.February, 14, 12, 0, 0, 0, time.UTC), 3832 time.Date(2020, time.February, 14, 4, 0, 0, 0, time.UTC)}}, 3833 }, 3834 { 3835 Query: `set @@time_zone='+5:00';`, 3836 Expected: []sql.Row{{}}, 3837 }, 3838 { 3839 // Test with explicit timezone in datetime literal 3840 Query: `insert into timezonetest values(3, '2020-02-16 12:00:00 +0800 CST', '2020-02-16 12:00:00 +0800 CST');`, 3841 Expected: []sql.Row{{types.NewOkResult(1)}}, 3842 }, 3843 { 3844 // TODO: Unskip after adding support for converting timestamp values to/from session time_zone 3845 Skip: true, 3846 Query: `select * from timezonetest;`, 3847 Expected: []sql.Row{ 3848 {1, time.Date(2020, time.February, 14, 12, 0, 0, 0, time.UTC), 3849 time.Date(2020, time.February, 14, 17, 0, 0, 0, time.UTC)}, 3850 {3, time.Date(2020, time.February, 16, 9, 0, 0, 0, time.UTC), 3851 time.Date(2020, time.February, 16, 9, 0, 0, 0, time.UTC)}}, 3852 }, 3853 { 3854 Query: `set @@time_zone='+0:00';`, 3855 Expected: []sql.Row{{}}, 3856 }, 3857 { 3858 // TODO: Unskip after adding support for converting timestamp values to/from session time_zone 3859 Skip: true, 3860 Query: `select * from timezonetest;`, 3861 Expected: []sql.Row{ 3862 {1, time.Date(2020, time.February, 14, 12, 0, 0, 0, time.UTC), 3863 time.Date(2020, time.February, 14, 12, 0, 0, 0, time.UTC)}, 3864 {3, time.Date(2020, time.February, 16, 9, 0, 0, 0, time.UTC), 3865 time.Date(2020, time.February, 16, 4, 0, 0, 0, time.UTC)}}, 3866 }, 3867 }, 3868 }, 3869 { 3870 Name: "test index scan over floats", 3871 SetUpScript: []string{ 3872 "CREATE TABLE tab2(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT);", 3873 "CREATE UNIQUE INDEX idx_tab2_0 ON tab2 (col1 DESC,col4 DESC);", 3874 "CREATE INDEX idx_tab2_1 ON tab2 (col1,col0);", 3875 "CREATE INDEX idx_tab2_2 ON tab2 (col4,col0);", 3876 "CREATE INDEX idx_tab2_3 ON tab2 (col3 DESC);", 3877 "INSERT INTO tab2 VALUES(0,344,171.98,'nwowg',833,149.54,'wjiif');", 3878 "INSERT INTO tab2 VALUES(1,353,589.18,'femmh',44,621.85,'qedct');", 3879 }, 3880 Assertions: []ScriptTestAssertion{ 3881 { 3882 Query: "SELECT pk FROM tab2 WHERE ((((((col0 IN (SELECT col3 FROM tab2 WHERE ((col1 = 672.71)) AND col4 IN (SELECT col1 FROM tab2 WHERE ((col4 > 169.88 OR col0 > 939 AND ((col3 > 578))))) AND col0 >= 377) AND col4 >= 817.87 AND (col4 > 597.59)) OR col4 >= 434.59 AND ((col4 < 158.43)))))) AND col0 < 303) OR ((col0 > 549)) AND (col4 BETWEEN 816.92 AND 983.96) OR (col3 BETWEEN 421 AND 96);", 3883 Expected: []sql.Row{}, 3884 }, 3885 }, 3886 }, 3887 { 3888 Name: "empty table update", 3889 SetUpScript: []string{ 3890 "create table t (i int primary key)", 3891 "insert into t values (1), (2), (3)", 3892 }, 3893 Assertions: []ScriptTestAssertion{ 3894 { 3895 Query: "update t set i = 0 where false", 3896 Expected: []sql.Row{{types.OkResult{RowsAffected: 0, InsertID: 0, Info: plan.UpdateInfo{Matched: 0}}}}, 3897 }, 3898 { 3899 Query: "select * from t", 3900 Expected: []sql.Row{ 3901 {1}, 3902 {2}, 3903 {3}, 3904 }, 3905 }, 3906 }, 3907 }, 3908 { 3909 Name: "case insensitive index handling", 3910 SetUpScript: []string{ 3911 "create table table_One (Id int primary key, Val1 int);", 3912 "create table TableTwo (iD int primary key, VAL2 int, vAL3 int);", 3913 }, 3914 Assertions: []ScriptTestAssertion{ 3915 { 3916 Query: "create index idx_one on TABLE_ONE (vAL1);", 3917 Expected: []sql.Row{{types.NewOkResult(0)}}, 3918 }, 3919 { 3920 Query: "show create table TABLE_one;", 3921 Expected: []sql.Row{{"table_One", 3922 "CREATE TABLE `table_One` (\n" + 3923 " `Id` int NOT NULL,\n" + 3924 " `Val1` int,\n" + 3925 " PRIMARY KEY (`Id`),\n" + 3926 " KEY `idx_one` (`Val1`)\n" + 3927 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 3928 }, 3929 { 3930 Query: "show index from TABLE_one;", 3931 Expected: []sql.Row{ 3932 {"table_One", 0, "PRIMARY", 1, "Id", nil, 0, nil, nil, "", "BTREE", "", "", "YES", nil}, 3933 {"table_One", 1, "idx_one", 1, "Val1", nil, 0, nil, nil, "YES", "BTREE", "", "", "YES", nil}, 3934 }, 3935 }, 3936 { 3937 Query: "create index idx_one on TABLEtwo (VAL2, VAL3);", 3938 Expected: []sql.Row{{types.NewOkResult(0)}}, 3939 }, 3940 { 3941 Query: "show create table TABLETWO;", 3942 Expected: []sql.Row{{"TableTwo", "CREATE TABLE `TableTwo` (\n" + 3943 " `iD` int NOT NULL,\n" + 3944 " `VAL2` int,\n" + 3945 " `vAL3` int,\n" + 3946 " PRIMARY KEY (`iD`),\n" + 3947 " KEY `idx_one` (`VAL2`,`vAL3`)\n" + 3948 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 3949 }, 3950 { 3951 Query: "show index from tABLEtwo;", 3952 Expected: []sql.Row{ 3953 {"TableTwo", 0, "PRIMARY", 1, "iD", nil, 0, nil, nil, "", "BTREE", "", "", "YES", nil}, 3954 {"TableTwo", 1, "idx_one", 1, "VAL2", nil, 0, nil, nil, "YES", "BTREE", "", "", "YES", nil}, 3955 {"TableTwo", 1, "idx_one", 2, "vAL3", nil, 0, nil, nil, "YES", "BTREE", "", "", "YES", nil}, 3956 }, 3957 }, 3958 { 3959 Query: "drop index IDX_ONE on TABLE_one;", 3960 Expected: []sql.Row{{types.NewOkResult(0)}}, 3961 }, 3962 { 3963 Query: "drop index IDX_ONE on TABLEtwo;", 3964 Expected: []sql.Row{{types.NewOkResult(0)}}, 3965 }, 3966 { 3967 Query: "show create table TABLE_one;", 3968 Expected: []sql.Row{{"table_One", 3969 "CREATE TABLE `table_One` (\n" + 3970 " `Id` int NOT NULL,\n" + 3971 " `Val1` int,\n" + 3972 " PRIMARY KEY (`Id`)\n" + 3973 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 3974 }, 3975 { 3976 Query: "show create table TABLETWO;", 3977 Expected: []sql.Row{{"TableTwo", "CREATE TABLE `TableTwo` (\n" + 3978 " `iD` int NOT NULL,\n" + 3979 " `VAL2` int,\n" + 3980 " `vAL3` int,\n" + 3981 " PRIMARY KEY (`iD`)\n" + 3982 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 3983 }, 3984 }, 3985 }, 3986 { 3987 Name: "different cases of function name should result in the same outcome", 3988 SetUpScript: []string{ 3989 "create table t (b binary(2) primary key);", 3990 }, 3991 Assertions: []ScriptTestAssertion{ 3992 { 3993 Query: "select hex(*) from t;", 3994 ExpectedErr: sql.ErrStarUnsupported, 3995 }, 3996 { 3997 Query: "select HEX(*) from t;", 3998 ExpectedErr: sql.ErrStarUnsupported, 3999 }, 4000 { 4001 Query: "select HeX(*) from t;", 4002 ExpectedErr: sql.ErrStarUnsupported, 4003 }, 4004 }, 4005 }, 4006 { 4007 Name: "UNIX_TIMESTAMP function usage with session different time zones", 4008 Assertions: []ScriptTestAssertion{ 4009 { 4010 Query: "SET time_zone = '+07:00';", 4011 Expected: []sql.Row{{}}, 4012 }, 4013 { 4014 Query: "SELECT UNIX_TIMESTAMP('2023-09-25 07:02:57');", 4015 Expected: []sql.Row{{float64(1695600177)}}, 4016 }, 4017 { 4018 Query: "SELECT UNIX_TIMESTAMP(CONVERT_TZ('2023-09-25 07:02:57', '+00:00', @@session.time_zone));", 4019 Expected: []sql.Row{{float64(1695625377)}}, 4020 }, 4021 { 4022 Query: "SET time_zone = '+00:00';", 4023 Expected: []sql.Row{{}}, 4024 }, 4025 { 4026 Query: "SELECT UNIX_TIMESTAMP('2023-09-25 07:02:57');", 4027 Expected: []sql.Row{{float64(1695625377)}}, 4028 }, 4029 { 4030 Query: "SET time_zone = '-06:00';", 4031 Expected: []sql.Row{{}}, 4032 }, 4033 { 4034 Query: "SELECT UNIX_TIMESTAMP('2023-09-25 07:02:57');", 4035 Expected: []sql.Row{{float64(1695646977)}}, 4036 }, 4037 }, 4038 }, 4039 { 4040 Name: "Querying existing view that references non-existing table", 4041 SetUpScript: []string{ 4042 "CREATE TABLE a(id int primary key, col1 int);", 4043 "CREATE VIEW b AS SELECT * FROM a;", 4044 "CREATE VIEW f AS SELECT col1 AS npk FROM a;", 4045 "RENAME TABLE a TO d;", 4046 }, 4047 Assertions: []ScriptTestAssertion{ 4048 { 4049 Query: "CREATE VIEW g AS SELECT * FROM nonexistenttable;", 4050 ExpectedErr: sql.ErrTableNotFound, 4051 }, 4052 { 4053 // TODO: ALTER VIEWs are not supported 4054 Skip: true, 4055 Query: "ALTER VIEW b AS SELECT * FROM nonexistenttable;", 4056 ExpectedErr: sql.ErrTableNotFound, 4057 }, 4058 { 4059 Query: "SELECT * FROM b;", 4060 ExpectedErr: sql.ErrInvalidRefInView, 4061 }, 4062 { 4063 Query: "RENAME TABLE d TO a;", 4064 Expected: []sql.Row{{types.NewOkResult(0)}}, 4065 }, 4066 { 4067 Query: "SELECT * FROM b;", 4068 Expected: []sql.Row{}, 4069 }, 4070 { 4071 Query: "ALTER TABLE a RENAME COLUMN col1 TO newcol;", 4072 Expected: []sql.Row{{types.NewOkResult(0)}}, 4073 }, 4074 { 4075 // TODO: View definition should have 'SELECT *' be expanded to each column of the referenced table 4076 Skip: true, 4077 Query: "SELECT * FROM b;", 4078 ExpectedErr: sql.ErrInvalidRefInView, 4079 }, 4080 { 4081 Query: "SELECT * FROM f;", 4082 ExpectedErr: sql.ErrInvalidRefInView, 4083 }, 4084 }, 4085 }, 4086 { 4087 Name: "Multi-db Aliasing", 4088 SetUpScript: []string{ 4089 "create database db1;", 4090 "create table db1.t1 (i int primary key);", 4091 "create table db1.t2 (j int primary key);", 4092 "insert into db1.t1 values (1);", 4093 "insert into db1.t2 values (2);", 4094 4095 "create database db2;", 4096 "create table db2.t1 (i int primary key);", 4097 "create table db2.t2 (j int primary key);", 4098 "insert into db2.t1 values (10);", 4099 "insert into db2.t2 values (20);", 4100 }, 4101 Assertions: []ScriptTestAssertion{ 4102 { 4103 // surprisingly, this works 4104 Query: "select db1.t1.i from db1.t1 where db1.``.i > 0", 4105 Expected: []sql.Row{ 4106 {1}, 4107 }, 4108 }, 4109 { 4110 Query: "select db1.t1.i from db1.t1 where db1.t1.i > 0", 4111 Expected: []sql.Row{ 4112 {1}, 4113 }, 4114 }, 4115 { 4116 Query: "select db1.t1.i from db1.t1 order by db1.t1.i", 4117 Expected: []sql.Row{ 4118 {1}, 4119 }, 4120 }, 4121 { 4122 Query: "select db1.t1.i from db1.t1 group by db1.t1.i", 4123 Expected: []sql.Row{ 4124 {1}, 4125 }, 4126 }, 4127 { 4128 Query: "select db1.t1.i from db1.t1 having db1.t1.i > 0", 4129 Expected: []sql.Row{ 4130 {1}, 4131 }, 4132 }, 4133 { 4134 Query: "select (select db1.t1.i from db1.t1 order by db1.t1.i)", 4135 Expected: []sql.Row{ 4136 {1}, 4137 }, 4138 }, 4139 { 4140 Query: "select i from (select db1.t1.i from db1.t1 order by db1.t1.i) as t", 4141 Expected: []sql.Row{ 4142 {1}, 4143 }, 4144 }, 4145 { 4146 Query: "with cte as (select db1.t1.i from db1.t1 order by db1.t1.i) select * from cte", 4147 Expected: []sql.Row{ 4148 {1}, 4149 }, 4150 }, 4151 { 4152 Query: "select i, j from db1.t1 inner join db2.t2 on 20 * i = j", 4153 Expected: []sql.Row{ 4154 {1, 20}, 4155 }, 4156 }, 4157 { 4158 Query: "select db1.t1.i, db2.t2.j from db1.t1 inner join db2.t2 on 20 * db1.t1.i = db2.t2.j", 4159 Expected: []sql.Row{ 4160 {1, 20}, 4161 }, 4162 }, 4163 { 4164 Query: "select i, j from db1.t1 join db2.t2 order by i, j", 4165 Expected: []sql.Row{ 4166 {1, 20}, 4167 }, 4168 }, 4169 { 4170 Query: "select i, j from db1.t1 join db2.t2 group by i order by j", 4171 Expected: []sql.Row{ 4172 {1, 20}, 4173 }, 4174 }, 4175 { 4176 Query: "select db1.t1.i, db2.t2.j from db1.t1 join db2.t2 group by db1.t1.i order by db2.t2.j", 4177 Expected: []sql.Row{ 4178 {1, 20}, 4179 }, 4180 }, 4181 { 4182 Skip: true, // incorrectly throws Not unique table/alias: t1 4183 Query: "select db1.t1.i, db2.t1.i from db1.t1 join db2.t1 order by db1.t1, db2.t1.i", 4184 Expected: []sql.Row{ 4185 {1, 10}, 4186 }, 4187 }, 4188 { 4189 // Aliasing solves it 4190 Query: "select a.i, b.i from db1.t1 a join db2.t1 b order by a.i, b.i", 4191 Expected: []sql.Row{ 4192 {1, 10}, 4193 }, 4194 }, 4195 }, 4196 }, 4197 { 4198 Name: "order by with index", 4199 SetUpScript: []string{ 4200 "create table t (i int primary key, `100` int);", 4201 "insert into t values (1, 2), (2, 1)", 4202 }, 4203 Assertions: []ScriptTestAssertion{ 4204 { 4205 Query: "select * from t order by `100`", 4206 Expected: []sql.Row{ 4207 {2, 1}, 4208 {1, 2}, 4209 }, 4210 }, 4211 { 4212 Query: "select * from t order by 100", 4213 ExpectedErrStr: "column \"100\" could not be found in any table in scope", 4214 }, 4215 { 4216 Query: "select i as `200`, `100` from t order by `200`", 4217 Expected: []sql.Row{ 4218 {1, 2}, 4219 {2, 1}, 4220 }, 4221 }, 4222 { 4223 Query: "select i as `200` from t order by 200", 4224 ExpectedErrStr: "column \"200\" could not be found in any table in scope", 4225 }, 4226 { 4227 Query: "select * from t order by 0", 4228 ExpectedErrStr: "column \"0\" could not be found in any table in scope", 4229 }, 4230 { 4231 Query: "select * from t order by -999", 4232 Expected: []sql.Row{ 4233 {1, 2}, 4234 {2, 1}, 4235 }, 4236 }, 4237 }, 4238 }, 4239 { 4240 Name: "Point lookups with dropped filters", 4241 SetUpScript: []string{ 4242 `create table t1 ( 4243 id varchar(255), 4244 a varchar(255), 4245 unique key key1 (id, a) 4246 );`, 4247 `create table t2 ( 4248 id varchar(255), 4249 b varchar(255), 4250 unique key key2 (id, b) 4251 );`, 4252 `insert into t1 values 4253 ('id1', 'a1'), 4254 ('id1', 'a2');`, 4255 `insert into t2 values 4256 ('id1', 'b1'), 4257 ('id1', 'b2'), 4258 ('id1', 'b3'), 4259 ('id2', 'b4'), 4260 ('id2', 'b5');`, 4261 }, 4262 Assertions: []ScriptTestAssertion{ 4263 { 4264 Query: ` 4265 select /*+ LOOKUP_JOIN(t1, t3)*/ t1.id, t1.a, t2.b from 4266 t1 4267 inner join 4268 t2 4269 on 4270 t1.id = t2.id and t1.a = t2.b;`, 4271 Expected: []sql.Row{}, 4272 }, 4273 }, 4274 }, 4275 { 4276 Name: "Complex Filter Index Scan", 4277 SetUpScript: []string{ 4278 `CREATE TABLE tab2 ( 4279 pk int NOT NULL, 4280 col0 int, 4281 col1 float, 4282 col2 text, 4283 col3 int, 4284 col4 float, 4285 col5 text, 4286 PRIMARY KEY (pk), 4287 UNIQUE KEY idx_tab2_0 (col3,col4), 4288 UNIQUE KEY idx_tab2_1 (col1,col4), 4289 UNIQUE KEY idx_tab2_2 (col3,col0,col4), 4290 UNIQUE KEY idx_tab2_3 (col1,col3) 4291 );`, 4292 `insert into tab2 values ( 63, 587, 465.59 , 'aggxb', 303 , 763.91, 'tgpqr');`, 4293 }, 4294 Assertions: []ScriptTestAssertion{ 4295 { 4296 Query: "SELECT pk FROM tab2 WHERE col4 IS NULL OR col0 > 560 AND (col3 < 848) OR (col3 > 883) OR (((col4 >= 539.78 AND col3 <= 953))) OR ((col3 IN (258)) OR (col3 IN (583,234,372)) AND col4 >= 488.43)", 4297 Expected: []sql.Row{ 4298 {63}, 4299 }, 4300 }, 4301 }, 4302 }, 4303 { 4304 Name: "Complex Filter Index Scan", 4305 SetUpScript: []string{ 4306 "create table t (pk int primary key, v1 int, v2 int, v3 int, v4 int);", 4307 "create index v_idx on t (v1, v2, v3, v4);", 4308 "insert into t values (0, 26, 24, 91, 0);", 4309 }, 4310 Assertions: []ScriptTestAssertion{ 4311 { 4312 Query: "select * from t where (((v1>25 and v2 between 23 and 54) or (v1<>40 and v3>90)) or (v1<>7 and v4<=78));", 4313 Expected: []sql.Row{ 4314 {0, 26, 24, 91, 0}, 4315 }, 4316 }, 4317 }, 4318 }, 4319 { 4320 Name: "Complex Filter Index Scan", 4321 SetUpScript: []string{ 4322 "create table t (pk integer primary key, col0 integer, col1 float);", 4323 "create index idx on t (col0, col1);", 4324 "insert into t values (0, 22, 1.23);", 4325 }, 4326 Assertions: []ScriptTestAssertion{ 4327 { 4328 Query: "select pk, col0 from t where (col0 in (73,69)) or col0 in (4,12,3,17,70,20) or (col0 in (39) or (col1 < 69.67));", 4329 Expected: []sql.Row{ 4330 {0, 22}, 4331 }, 4332 }, 4333 }, 4334 }, 4335 { 4336 Name: "update columns with default", 4337 SetUpScript: []string{ 4338 "create table t (i int default 10, j varchar(128) default (concat('abc', 'def')));", 4339 "insert into t values (100, 'a'), (200, 'b');", 4340 "create table t2 (i int);", 4341 "insert into t2 values (1), (2), (3);", 4342 }, 4343 Assertions: []ScriptTestAssertion{ 4344 { 4345 Query: "update t set i = default where i = 100;", 4346 Expected: []sql.Row{ 4347 {types.OkResult{RowsAffected: 1, Info: plan.UpdateInfo{Matched: 1, Updated: 1}}}, 4348 }, 4349 }, 4350 { 4351 Query: "select * from t order by i", 4352 Expected: []sql.Row{ 4353 {10, "a"}, 4354 {200, "b"}, 4355 }, 4356 }, 4357 { 4358 Query: "update t set j = default where i = 200;", 4359 Expected: []sql.Row{ 4360 {types.OkResult{RowsAffected: 1, Info: plan.UpdateInfo{Matched: 1, Updated: 1}}}, 4361 }, 4362 }, 4363 { 4364 Query: "select * from t order by i", 4365 Expected: []sql.Row{ 4366 {10, "a"}, 4367 {200, "abcdef"}, 4368 }, 4369 }, 4370 { 4371 Query: "update t set i = default, j = default;", 4372 Expected: []sql.Row{ 4373 {types.OkResult{RowsAffected: 2, Info: plan.UpdateInfo{Matched: 2, Updated: 2}}}, 4374 }, 4375 }, 4376 { 4377 Query: "select * from t order by i", 4378 Expected: []sql.Row{ 4379 {10, "abcdef"}, 4380 {10, "abcdef"}, 4381 }, 4382 }, 4383 { 4384 Query: "update t2 set i = default", 4385 Expected: []sql.Row{ 4386 {types.OkResult{RowsAffected: 3, Info: plan.UpdateInfo{Matched: 3, Updated: 3}}}, 4387 }, 4388 }, 4389 { 4390 Query: "select * from t2", 4391 Expected: []sql.Row{ 4392 {nil}, 4393 {nil}, 4394 {nil}, 4395 }, 4396 }, 4397 }, 4398 }, 4399 { 4400 Name: "int index with float filter", 4401 SetUpScript: []string{ 4402 "create table t0 (i int primary key);", 4403 "insert into t0 values (-1), (0), (1);", 4404 }, 4405 Assertions: []ScriptTestAssertion{ 4406 { 4407 Query: "select * from t0 where i > 0.0 order by i;", 4408 Expected: []sql.Row{ 4409 {1}, 4410 }, 4411 }, 4412 { 4413 Query: "select * from t0 where i > 0.1 order by i;", 4414 Expected: []sql.Row{ 4415 {1}, 4416 }, 4417 }, 4418 { 4419 Query: "select * from t0 where i > 0.5 order by i;", 4420 Expected: []sql.Row{ 4421 {1}, 4422 }, 4423 }, 4424 { 4425 Query: "select * from t0 where i > 0.9 order by i;", 4426 Expected: []sql.Row{ 4427 {1}, 4428 }, 4429 }, 4430 { 4431 Query: "select * from t0 where i > 1.0 order by i;", 4432 Expected: []sql.Row{}, 4433 }, 4434 { 4435 Query: "select * from t0 where i > 1.1 order by i;", 4436 Expected: []sql.Row{}, 4437 }, 4438 4439 { 4440 Query: "select * from t0 where i > -0.0 order by i;", 4441 Expected: []sql.Row{ 4442 {1}, 4443 }, 4444 }, 4445 { 4446 Query: "select * from t0 where i > -0.1 order by i;", 4447 Expected: []sql.Row{ 4448 {0}, 4449 {1}, 4450 }, 4451 }, 4452 { 4453 Query: "select * from t0 where i > -0.5 order by i;", 4454 Expected: []sql.Row{ 4455 {0}, 4456 {1}, 4457 }, 4458 }, 4459 { 4460 Query: "select * from t0 where i > -0.9 order by i;", 4461 Expected: []sql.Row{ 4462 {0}, 4463 {1}, 4464 }, 4465 }, 4466 { 4467 Query: "select * from t0 where i > -1.0 order by i;", 4468 Expected: []sql.Row{ 4469 {0}, 4470 {1}, 4471 }, 4472 }, 4473 { 4474 Query: "select * from t0 where i > -1.1 order by i;", 4475 Expected: []sql.Row{ 4476 {-1}, 4477 {0}, 4478 {1}, 4479 }, 4480 }, 4481 4482 { 4483 Query: "select * from t0 where i >= 0.0 order by i;", 4484 Expected: []sql.Row{ 4485 {0}, 4486 {1}, 4487 }, 4488 }, 4489 { 4490 Query: "select * from t0 where i >= 0.1 order by i;", 4491 Expected: []sql.Row{ 4492 {1}, 4493 }, 4494 }, 4495 { 4496 Query: "select * from t0 where i >= 0.5 order by i;", 4497 Expected: []sql.Row{ 4498 {1}, 4499 }, 4500 }, 4501 { 4502 Query: "select * from t0 where i >= 0.9 order by i;", 4503 Expected: []sql.Row{ 4504 {1}, 4505 }, 4506 }, 4507 { 4508 Query: "select * from t0 where i >= 1.0 order by i;", 4509 Expected: []sql.Row{ 4510 {1}, 4511 }, 4512 }, 4513 { 4514 Query: "select * from t0 where i >= 1.1 order by i;", 4515 Expected: []sql.Row{}, 4516 }, 4517 4518 { 4519 Query: "select * from t0 where i >= -0.0 order by i;", 4520 Expected: []sql.Row{ 4521 {0}, 4522 {1}, 4523 }, 4524 }, 4525 { 4526 Query: "select * from t0 where i >= -0.1 order by i;", 4527 Expected: []sql.Row{ 4528 {0}, 4529 {1}, 4530 }, 4531 }, 4532 { 4533 Query: "select * from t0 where i >= -0.5 order by i;", 4534 Expected: []sql.Row{ 4535 {0}, 4536 {1}, 4537 }, 4538 }, 4539 { 4540 Query: "select * from t0 where i >= -0.9 order by i;", 4541 Expected: []sql.Row{ 4542 {0}, 4543 {1}, 4544 }, 4545 }, 4546 { 4547 Query: "select * from t0 where i >= -1.0 order by i;", 4548 Expected: []sql.Row{ 4549 {-1}, 4550 {0}, 4551 {1}, 4552 }, 4553 }, 4554 { 4555 Query: "select * from t0 where i >= -1.1 order by i;", 4556 Expected: []sql.Row{ 4557 {-1}, 4558 {0}, 4559 {1}, 4560 }, 4561 }, 4562 4563 { 4564 Query: "select * from t0 where i < 0.0 order by i;", 4565 Expected: []sql.Row{ 4566 {-1}, 4567 }, 4568 }, 4569 { 4570 Query: "select * from t0 where i < 0.1 order by i;", 4571 Expected: []sql.Row{ 4572 {-1}, 4573 {0}, 4574 }, 4575 }, 4576 { 4577 Query: "select * from t0 where i < 0.5 order by i;", 4578 Expected: []sql.Row{ 4579 {-1}, 4580 {0}, 4581 }, 4582 }, 4583 { 4584 Query: "select * from t0 where i < 0.9 order by i;", 4585 Expected: []sql.Row{ 4586 {-1}, 4587 {0}, 4588 }, 4589 }, 4590 { 4591 Query: "select * from t0 where i < 1.0 order by i;", 4592 Expected: []sql.Row{ 4593 {-1}, 4594 {0}, 4595 }, 4596 }, 4597 { 4598 Query: "select * from t0 where i < 1.1 order by i;", 4599 Expected: []sql.Row{ 4600 {-1}, 4601 {0}, 4602 {1}, 4603 }, 4604 }, 4605 4606 { 4607 Query: "select * from t0 where i < -0.0 order by i;", 4608 Expected: []sql.Row{ 4609 {-1}, 4610 }, 4611 }, 4612 { 4613 Query: "select * from t0 where i < -0.1 order by i;", 4614 Expected: []sql.Row{ 4615 {-1}, 4616 }, 4617 }, 4618 { 4619 Query: "select * from t0 where i < -0.5 order by i;", 4620 Expected: []sql.Row{ 4621 {-1}, 4622 }, 4623 }, 4624 { 4625 Query: "select * from t0 where i < -0.9 order by i;", 4626 Expected: []sql.Row{ 4627 {-1}, 4628 }, 4629 }, 4630 { 4631 Query: "select * from t0 where i < -1.0 order by i;", 4632 Expected: []sql.Row{}, 4633 }, 4634 { 4635 Query: "select * from t0 where i < -1.1 order by i;", 4636 Expected: []sql.Row{}, 4637 }, 4638 4639 { 4640 Query: "select * from t0 where i <= 0.0 order by i;", 4641 Expected: []sql.Row{ 4642 {-1}, 4643 {0}, 4644 }, 4645 }, 4646 { 4647 Query: "select * from t0 where i <= 0.1 order by i;", 4648 Expected: []sql.Row{ 4649 {-1}, 4650 {0}, 4651 }, 4652 }, 4653 { 4654 Query: "select * from t0 where i <= 0.5 order by i;", 4655 Expected: []sql.Row{ 4656 {-1}, 4657 {0}, 4658 }, 4659 }, 4660 { 4661 Query: "select * from t0 where i <= 0.9 order by i;", 4662 Expected: []sql.Row{ 4663 {-1}, 4664 {0}, 4665 }, 4666 }, 4667 { 4668 Query: "select * from t0 where i <= 1.0 order by i;", 4669 Expected: []sql.Row{ 4670 {-1}, 4671 {0}, 4672 {1}, 4673 }, 4674 }, 4675 { 4676 Query: "select * from t0 where i <= 1.1 order by i;", 4677 Expected: []sql.Row{ 4678 {-1}, 4679 {0}, 4680 {1}, 4681 }, 4682 }, 4683 4684 { 4685 Query: "select * from t0 where i <= -0.0 order by i;", 4686 Expected: []sql.Row{ 4687 {-1}, 4688 {0}, 4689 }, 4690 }, 4691 { 4692 Query: "select * from t0 where i <= -0.1 order by i;", 4693 Expected: []sql.Row{ 4694 {-1}, 4695 }, 4696 }, 4697 { 4698 Query: "select * from t0 where i <= -0.5 order by i;", 4699 Expected: []sql.Row{ 4700 {-1}, 4701 }, 4702 }, 4703 { 4704 Query: "select * from t0 where i <= -0.9 order by i;", 4705 Expected: []sql.Row{ 4706 {-1}, 4707 }, 4708 }, 4709 { 4710 Query: "select * from t0 where i <= -1.0 order by i;", 4711 Expected: []sql.Row{ 4712 {-1}, 4713 }, 4714 }, 4715 { 4716 Query: "select * from t0 where i <= -1.1 order by i;", 4717 Expected: []sql.Row{}, 4718 }, 4719 4720 { 4721 Query: "select * from t0 where i = 0.0 order by i;", 4722 Expected: []sql.Row{ 4723 {0}, 4724 }, 4725 }, 4726 { 4727 Query: "select * from t0 where i = 0.1 order by i;", 4728 Expected: []sql.Row{}, 4729 }, 4730 { 4731 Query: "select * from t0 where i = 0.5 order by i;", 4732 Expected: []sql.Row{}, 4733 }, 4734 { 4735 Query: "select * from t0 where i = 0.9 order by i;", 4736 Expected: []sql.Row{}, 4737 }, 4738 4739 { 4740 Query: "select * from t0 where i = -0.0 order by i;", 4741 Expected: []sql.Row{ 4742 {0}, 4743 }, 4744 }, 4745 { 4746 Query: "select * from t0 where i = -0.1 order by i;", 4747 Expected: []sql.Row{}, 4748 }, 4749 { 4750 Query: "select * from t0 where i = -0.5 order by i;", 4751 Expected: []sql.Row{}, 4752 }, 4753 { 4754 Query: "select * from t0 where i = -0.9 order by i;", 4755 Expected: []sql.Row{}, 4756 }, 4757 { 4758 Query: "select * from t0 where i = -1.0 order by i;", 4759 Expected: []sql.Row{ 4760 {-1}, 4761 }, 4762 }, 4763 4764 { 4765 Query: "select * from t0 where i != 0.0 order by i;", 4766 Expected: []sql.Row{ 4767 {-1}, 4768 {1}, 4769 }, 4770 }, 4771 { 4772 Query: "select * from t0 where i != 0.1 order by i;", 4773 Expected: []sql.Row{ 4774 {-1}, 4775 {0}, 4776 {1}, 4777 }, 4778 }, 4779 { 4780 Query: "select * from t0 where i != 0.5 order by i;", 4781 Expected: []sql.Row{ 4782 {-1}, 4783 {0}, 4784 {1}, 4785 }, 4786 }, 4787 { 4788 Query: "select * from t0 where i != 0.9 order by i;", 4789 Expected: []sql.Row{ 4790 {-1}, 4791 {0}, 4792 {1}, 4793 }, 4794 }, 4795 4796 { 4797 Query: "select * from t0 where i != -0.0 order by i;", 4798 Expected: []sql.Row{ 4799 {-1}, 4800 {1}, 4801 }, 4802 }, 4803 { 4804 Query: "select * from t0 where i != -0.1 order by i;", 4805 Expected: []sql.Row{ 4806 {-1}, 4807 {0}, 4808 {1}, 4809 }, 4810 }, 4811 { 4812 Query: "select * from t0 where i != -0.5 order by i;", 4813 Expected: []sql.Row{ 4814 {-1}, 4815 {0}, 4816 {1}, 4817 }, 4818 }, 4819 { 4820 Query: "select * from t0 where i != -0.9 order by i;", 4821 Expected: []sql.Row{ 4822 {-1}, 4823 {0}, 4824 {1}, 4825 }, 4826 }, 4827 { 4828 Query: "select * from t0 where i != -1.0 order by i;", 4829 Expected: []sql.Row{ 4830 {0}, 4831 {1}, 4832 }, 4833 }, 4834 4835 { 4836 Query: "select * from t0 where i <= 0.0 and i >= 0.0 order by i;", 4837 Expected: []sql.Row{ 4838 {0}, 4839 }, 4840 }, 4841 { 4842 Query: "select * from t0 where i <= 0.1 or i >= 0.1 order by i;", 4843 Expected: []sql.Row{ 4844 {-1}, 4845 {0}, 4846 {1}, 4847 }, 4848 }, 4849 { 4850 Query: "select * from t0 where i > 0.1 and i >= 0.1 order by i;", 4851 Expected: []sql.Row{ 4852 {1}, 4853 }, 4854 }, 4855 { 4856 Query: "select * from t0 where i > 0.1 or i >= 0.1 order by i;", 4857 Expected: []sql.Row{ 4858 {1}, 4859 }, 4860 }, 4861 }, 4862 }, 4863 { 4864 Name: "int secondary index with float filter", 4865 SetUpScript: []string{ 4866 "create table t0 (i int);", 4867 "create index idx on t0(i);", 4868 "insert into t0 values (null), (-1), (0), (1);", 4869 }, 4870 Assertions: []ScriptTestAssertion{ 4871 { 4872 Query: "select * from t0 where i >= 0.0 order by i;", 4873 Expected: []sql.Row{ 4874 {0}, 4875 {1}, 4876 }, 4877 }, 4878 { 4879 Query: "select * from t0 where i <= 0.0 order by i;", 4880 Expected: []sql.Row{ 4881 {-1}, 4882 {0}, 4883 }, 4884 }, 4885 { 4886 // cot(-939932070) = -1.1919623754564008 4887 Query: "SELECT * from t0 where (cot(-939932070) < i);", 4888 Expected: []sql.Row{ 4889 {-1}, 4890 {0}, 4891 {1}, 4892 }, 4893 }, 4894 }, 4895 }, 4896 { 4897 Name: "decimal and float in tuple", 4898 SetUpScript: []string{ 4899 "create table t (d decimal(10, 3), f float);", 4900 "insert into t values (0.8, 0.8);", 4901 }, 4902 Assertions: []ScriptTestAssertion{ 4903 { 4904 Query: "select * from t where (d in (null, 1));", 4905 Expected: []sql.Row{}, 4906 }, 4907 { 4908 Query: "select * from t where (f in (null, 1));", 4909 Expected: []sql.Row{}, 4910 }, 4911 { 4912 // select count to avoid floating point comparison 4913 Query: "select count(*) from t where (d in (null, 0.8));", 4914 Expected: []sql.Row{ 4915 {1}, 4916 }, 4917 }, 4918 { 4919 // This actually matches MySQL behavior 4920 Query: "select * from t where (f in (null, 0.8));", 4921 Expected: []sql.Row{}, 4922 }, 4923 { 4924 // select count to avoid floating point comparison 4925 Query: "select count(*) from t where (f in (null, cast(0.8 as float)));", 4926 Expected: []sql.Row{ 4927 {1}, 4928 }, 4929 }, 4930 }, 4931 }, 4932 { 4933 Name: "floats in tuple are properly hashed", 4934 SetUpScript: []string{ 4935 "create table t (b bool);", 4936 "insert into t values (false);", 4937 "create table t_idx (b bool);", 4938 "create index idx on t_idx(b);", 4939 "insert into t_idx values (false);", 4940 }, 4941 Assertions: []ScriptTestAssertion{ 4942 { 4943 Query: "select * from t where (b in (-''));", 4944 Expected: []sql.Row{ 4945 {0}, 4946 }, 4947 }, 4948 { 4949 Query: "select * from t where (b in (false/'1'));", 4950 Expected: []sql.Row{ 4951 {0}, 4952 }, 4953 }, 4954 { 4955 Query: "select * from t_idx where (b in (-''));", 4956 Expected: []sql.Row{ 4957 {0}, 4958 }, 4959 }, 4960 { 4961 Query: "select * from t_idx where (b in (false/'1'));", 4962 Expected: []sql.Row{ 4963 {0}, 4964 }, 4965 }, 4966 }, 4967 }, 4968 { 4969 Name: "strings in tuple are properly hashed", 4970 SetUpScript: []string{ 4971 "create table t (v varchar(100));", 4972 "insert into t values (false);", 4973 "create table t_idx (v varchar(100));", 4974 "create index idx on t_idx(v);", 4975 "insert into t_idx values (false);", 4976 }, 4977 Assertions: []ScriptTestAssertion{ 4978 { 4979 Query: "select * from t where (v in (-''));", 4980 Expected: []sql.Row{ 4981 {"0"}, 4982 }, 4983 }, 4984 { 4985 Query: "select * from t where (v in (false/'1'));", 4986 Expected: []sql.Row{ 4987 {"0"}, 4988 }, 4989 }, 4990 { 4991 Query: "select * from t_idx where (v in (-''));", 4992 Expected: []sql.Row{ 4993 {"0"}, 4994 }, 4995 }, 4996 { 4997 Query: "select * from t_idx where (v in (false/'1'));", 4998 Expected: []sql.Row{ 4999 {"0"}, 5000 }, 5001 }, 5002 }, 5003 }, 5004 { 5005 Name: "strings vs decimals with trailing 0s in IN exprs", 5006 SetUpScript: []string{ 5007 "create table t (v varchar(100));", 5008 "insert into t values ('0'), ('0.0'), ('123'), ('123.0');", 5009 "create table t_idx (v varchar(100));", 5010 "create index idx on t_idx(v);", 5011 "insert into t_idx values ('0'), ('0.0'), ('123'), ('123.0');", 5012 }, 5013 Assertions: []ScriptTestAssertion{ 5014 { 5015 Skip: true, 5016 Query: "select * from t where (v in (0.0, 123));", 5017 Expected: []sql.Row{ 5018 {"0"}, 5019 {"0.0"}, 5020 {"123"}, 5021 {"123.0"}, 5022 }, 5023 }, 5024 { 5025 Skip: true, 5026 Query: "select * from t_idx where (v in (0.0, 123));", 5027 Expected: []sql.Row{ 5028 {"0"}, 5029 {"0.0"}, 5030 {"123"}, 5031 {"123.0"}, 5032 }, 5033 }, 5034 }, 5035 }, 5036 { 5037 Name: "subquery with range heap join", 5038 SetUpScript: []string{ 5039 "create table a (i int primary key, start int, end int, name varchar(32));", 5040 "insert into a values (1, 603000, 605001, 'test');", 5041 "create table b (i int primary key);", 5042 "insert into b values (600000), (605000), (608000);", 5043 }, 5044 Assertions: []ScriptTestAssertion{ 5045 { 5046 Query: "select a.i from (select 'test' as name) sq join a on sq.name = a.name join b on b.i between a.start and a.end;", 5047 Expected: []sql.Row{ 5048 {1}, 5049 }, 5050 }, 5051 { 5052 Query: "select * from (select 'test' as name, 1 as x, 2 as y, 3 as z) sq join a on sq.name = a.name join b on b.i between a.start and a.end;", 5053 Expected: []sql.Row{ 5054 {"test", 1, 2, 3, 1, 603000, 605001, "test", 605000}, 5055 }, 5056 }, 5057 }, 5058 }, 5059 { 5060 Name: "resolve foreign key on indexed update", 5061 SetUpScript: []string{ 5062 "set foreign_key_checks=0;", 5063 "create table parent (i int primary key);", 5064 "create table child (i int primary key, foreign key (i) references parent(i));", 5065 "set foreign_key_checks=1;", 5066 }, 5067 Assertions: []ScriptTestAssertion{ 5068 { 5069 Query: "update child set i = 1 where i = 1;", 5070 Expected: []sql.Row{ 5071 {types.OkResult{RowsAffected: 0, Info: plan.UpdateInfo{Matched: 0, Updated: 0}}}, 5072 }, 5073 }, 5074 }, 5075 }, 5076 { 5077 Name: "between type conversion", 5078 SetUpScript: []string{ 5079 "create table t0(c0 bool);", 5080 "create table t1(c1 bool);", 5081 "insert into t0 (c0) values (1);", 5082 "insert into t1 (c1) values (false), (true);", 5083 }, 5084 Assertions: []ScriptTestAssertion{ 5085 { 5086 Query: "SELECT t0.c0, t1.c1 FROM t0 LEFT JOIN t1 ON true;", 5087 Expected: []sql.Row{ 5088 {1, 0}, 5089 {1, 1}, 5090 }, 5091 }, 5092 { 5093 Query: "SELECT t0.c0, t1.c1 FROM t0 LEFT JOIN t1 ON ('a' NOT BETWEEN false AND false) WHERE 1 UNION ALL SELECT t0.c0, t1.c1 FROM t0 LEFT JOIN t1 ON ('a' NOT BETWEEN false AND false) WHERE (NOT 1) UNION ALL SELECT t0.c0, t1.c1 FROM t0 LEFT JOIN t1 ON ('a' NOT BETWEEN false AND false) WHERE (1 IS NULL);", 5094 Expected: []sql.Row{ 5095 {1, nil}, 5096 }, 5097 }, 5098 }, 5099 }, 5100 { 5101 Name: "case sensitive subquery column names", 5102 SetUpScript: []string{ 5103 "create table t(ABC int, dEF int);", 5104 "insert into t values (1, 2);", 5105 }, 5106 5107 Assertions: []ScriptTestAssertion{ 5108 { 5109 ExpectedColumns: sql.Schema{ 5110 {Name: "ABC", Type: types.Int32}, 5111 {Name: "dEF", Type: types.Int32}, 5112 }, 5113 Query: "select * from t ", 5114 Expected: []sql.Row{ 5115 {1, 2}, 5116 }, 5117 }, 5118 { 5119 ExpectedColumns: sql.Schema{ 5120 {Name: "ABC", Type: types.Int32}, 5121 {Name: "dEF", Type: types.Int32}, 5122 }, 5123 Query: "select * from (select * from t) sqa", 5124 Expected: []sql.Row{ 5125 {1, 2}, 5126 }, 5127 }, 5128 }, 5129 }, 5130 { 5131 Name: "bool and string", 5132 SetUpScript: []string{ 5133 "CREATE TABLE t0(c0 BOOL, PRIMARY KEY(c0));", 5134 "INSERT INTO t0 (c0) VALUES (true);", 5135 "CREATE TABLE t1(c1 VARCHAR(500));", 5136 "INSERT INTO t1 (c1) VALUES (true);", 5137 }, 5138 5139 Assertions: []ScriptTestAssertion{ 5140 { 5141 Query: "SELECT * FROM t1, t0;", 5142 Expected: []sql.Row{ 5143 {"1", 1}, 5144 }, 5145 }, 5146 { 5147 Query: "SELECT (t1.c1 = t0.c0) FROM t1, t0;", 5148 Expected: []sql.Row{ 5149 {true}, 5150 }, 5151 }, 5152 { 5153 Query: "SELECT * FROM t1, t0 WHERE t1.c1 = t0.c0;", 5154 Expected: []sql.Row{ 5155 {"1", 1}, 5156 }, 5157 }, 5158 }, 5159 }, 5160 { 5161 Name: "bool and int", 5162 SetUpScript: []string{ 5163 "CREATE TABLE t0(c0 INTEGER, PRIMARY KEY(c0));", 5164 "INSERT INTO t0 (c0) VALUES (true);", 5165 "CREATE TABLE t1(c1 VARCHAR(500));", 5166 "INSERT INTO t1 (c1) VALUES (true);", 5167 }, 5168 5169 Assertions: []ScriptTestAssertion{ 5170 { 5171 Query: "SELECT * FROM t1, t0;", 5172 Expected: []sql.Row{ 5173 {"1", 1}, 5174 }, 5175 }, 5176 { 5177 Query: "SELECT (t1.c1 = t0.c0) FROM t1, t0;", 5178 Expected: []sql.Row{ 5179 {true}, 5180 }, 5181 }, 5182 { 5183 Query: "SELECT * FROM t1, t0 WHERE t1.c1 = t0.c0;", 5184 Expected: []sql.Row{ 5185 {"1", 1}, 5186 }, 5187 }, 5188 }, 5189 }, 5190 { 5191 Name: "update with left join with some missing rows", 5192 SetUpScript: []string{ 5193 `create table joinparent ( 5194 id int not null auto_increment, 5195 name varchar(128) not null, 5196 archived int default 0 not null, 5197 archived_at datetime null, 5198 primary key (id) 5199 );`, 5200 `insert into joinparent (name) values 5201 ('first'), 5202 ('second'), 5203 ('third'), 5204 ('fourth'), 5205 ('fifth');`, 5206 `create index joinparent_archived on joinparent (archived, archived_at);`, 5207 `create table joinchild ( 5208 id int not null auto_increment, 5209 name varchar(128) not null, 5210 parent_id int not null, 5211 archived int default 0 not null, 5212 archived_at datetime null, 5213 primary key (id), 5214 constraint joinchild_parent unique (parent_id, id, archived));`, 5215 `insert into joinchild (name, parent_id) values 5216 ('first', 4), 5217 ('second', 3), 5218 ('third', 2);`, 5219 }, 5220 Assertions: []ScriptTestAssertion{ 5221 { 5222 Query: `update joinparent as jp 5223 left join joinchild as jc on jc.parent_id = jp.id 5224 set jp.archived = jp.id, jp.archived_at = now(), 5225 jc.archived = jc.id, jc.archived_at = now() 5226 where jp.id > 0 and jp.name != "never" 5227 order by jp.name 5228 limit 100`, 5229 Expected: []sql.Row{{types.OkResult{RowsAffected: 8, Info: plan.UpdateInfo{Matched: 10, Updated: 8}}}}, 5230 }, 5231 // do without limit to use `plan.Sort` instead of `plan.TopN` 5232 { 5233 Query: `update joinparent as jp 5234 left join joinchild as jc on jc.parent_id = jp.id 5235 set jp.archived = 0, jp.archived_at = null, 5236 jc.archived = 0, jc.archived_at = null 5237 where jp.id > 0 and jp.name != "never" 5238 order by jp.name`, 5239 Expected: []sql.Row{{types.OkResult{RowsAffected: 8, Info: plan.UpdateInfo{Matched: 10, Updated: 8}}}}, 5240 }, 5241 }, 5242 }, 5243 { 5244 Name: "count distinct decimals", 5245 SetUpScript: []string{ 5246 "create table t (i int, j int)", 5247 "insert into t values (1, 11), (11, 1)", 5248 }, 5249 Assertions: []ScriptTestAssertion{ 5250 { 5251 Query: "select count(distinct i, j) from t;", 5252 Expected: []sql.Row{ 5253 {2}, 5254 }, 5255 }, 5256 { 5257 Query: "select count(distinct cast(i as decimal), cast(j as decimal)) from t;", 5258 Expected: []sql.Row{ 5259 {2}, 5260 }, 5261 }, 5262 }, 5263 }, 5264 { 5265 Name: "range query convert int to string zero value", 5266 SetUpScript: []string{ 5267 `CREATE TABLE t0(c0 VARCHAR(500));`, 5268 `INSERT INTO t0(c0) VALUES ('a');`, 5269 `INSERT INTO t0(c0) VALUES ('1');`, 5270 `CREATE TABLE t1(c0 INTEGER, PRIMARY KEY(c0));`, 5271 `INSERT INTO t1(c0) VALUES (0);`, 5272 `INSERT INTO t1(c0) VALUES (1);`, 5273 `INSERT INTO t1(c0) VALUES (2);`, 5274 }, 5275 Assertions: []ScriptTestAssertion{ 5276 { 5277 Query: "SELECT /*+ LOOKUP_JOIN(t0,t1) JOIN_ORDER(t0,t1) */ * FROM t1 INNER JOIN t0 ON ((t0.c0)=(t1.c0));", 5278 Expected: []sql.Row{ 5279 {0, "a"}, 5280 {1, "1"}, 5281 }, 5282 }, 5283 { 5284 Query: "INSERT INTO t0(c0) VALUES ('2abc');", 5285 Expected: []sql.Row{ 5286 {types.OkResult{RowsAffected: 1}}, 5287 }, 5288 }, 5289 { 5290 Skip: true, 5291 Query: "SELECT /*+ LOOKUP_JOIN(t0,t1) JOIN_ORDER(t0,t1) */ * FROM t1 INNER JOIN t0 ON ((t0.c0)=(t1.c0));", 5292 Expected: []sql.Row{ 5293 {0, "a"}, 5294 {1, "1"}, 5295 {2, "2abc"}, 5296 }, 5297 }, 5298 }, 5299 }, 5300 { 5301 Name: "group by having with conflicting aliases test", 5302 SetUpScript: []string{ 5303 "CREATE TABLE tab2(col0 INTEGER, col1 INTEGER, col2 INTEGER);", 5304 "INSERT INTO tab2 VALUES(15,61,87);", 5305 "INSERT INTO tab2 VALUES(91,59,79);", 5306 "INSERT INTO tab2 VALUES(92,41,58);", 5307 }, 5308 Assertions: []ScriptTestAssertion{ 5309 { 5310 Query: `SELECT - col2 AS col0 FROM tab2 GROUP BY col0, col2 HAVING NOT + + col2 <= - col0;`, 5311 Expected: []sql.Row{ 5312 {-87}, 5313 {-79}, 5314 {-58}, 5315 }, 5316 }, 5317 { 5318 Query: `SELECT -col2 AS col0 FROM tab2 GROUP BY col0, col2 HAVING NOT col2 <= - col0;`, 5319 Expected: []sql.Row{ 5320 {-87}, 5321 {-79}, 5322 {-58}, 5323 }, 5324 }, 5325 { 5326 Query: `SELECT -col2 AS col0 FROM tab2 GROUP BY col0, col2 HAVING col2 > -col0;`, 5327 Expected: []sql.Row{ 5328 {-87}, 5329 {-79}, 5330 {-58}, 5331 }, 5332 }, 5333 { 5334 Query: `SELECT 500 * col2 AS col0 FROM tab2 GROUP BY col0, col2 HAVING col2 > -col0;`, 5335 Expected: []sql.Row{ 5336 {43500}, 5337 {39500}, 5338 {29000}, 5339 }, 5340 }, 5341 5342 { 5343 Query: `select col2-100 as col0 from tab2 group by col0 having col0 > 0;`, 5344 Expected: []sql.Row{ 5345 {-13}, 5346 {-21}, 5347 {-42}, 5348 }, 5349 }, 5350 { 5351 Query: `select col2-100 as col0 from tab2 group by 1 having col0 > 0;`, 5352 Expected: []sql.Row{}, 5353 }, 5354 { 5355 Query: `select col0, count(col0) as c from tab2 group by col0 having c > 0;`, 5356 Expected: []sql.Row{ 5357 {15, 1}, 5358 {91, 1}, 5359 {92, 1}, 5360 }, 5361 }, 5362 { 5363 Query: `SELECT col0 as a FROM tab2 GROUP BY a HAVING col0 = a;`, 5364 Expected: []sql.Row{ 5365 {15}, 5366 {91}, 5367 {92}, 5368 }, 5369 }, 5370 { 5371 Query: `SELECT col0 as a FROM tab2 GROUP BY col0 HAVING col0 = a;`, 5372 Expected: []sql.Row{ 5373 {15}, 5374 {91}, 5375 {92}, 5376 }, 5377 }, 5378 { 5379 Query: `SELECT col0 as a FROM tab2 GROUP BY col0, a HAVING col0 = a;`, 5380 Expected: []sql.Row{ 5381 {15}, 5382 {91}, 5383 {92}, 5384 }, 5385 }, 5386 { 5387 Query: `SELECT col0 as a FROM tab2 HAVING col0 = a;`, 5388 Expected: []sql.Row{ 5389 {15}, 5390 {91}, 5391 {92}, 5392 }, 5393 }, 5394 { 5395 Query: `select col0, (select col1 having col0 > 0) as asdf from tab2 where col0 < 1000;`, 5396 Expected: []sql.Row{ 5397 {15, 61}, 5398 {91, 59}, 5399 {92, 41}, 5400 }, 5401 }, 5402 { 5403 Query: `select col0, sum(col1 * col2) as val from tab2 group by col0 having sum(col1 * col2) > 0;`, 5404 Expected: []sql.Row{ 5405 {15, 5307.0}, 5406 {91, 4661.0}, 5407 {92, 2378.0}, 5408 }, 5409 }, 5410 { 5411 Query: `SELECT col0+1 as a FROM tab2 HAVING col0 = a;`, 5412 ExpectedErr: sql.ErrColumnNotFound, 5413 }, 5414 { 5415 Query: `select col2-100 as asdf from tab2 group by 1 having col0 > 0;`, 5416 ExpectedErr: sql.ErrColumnNotFound, 5417 }, 5418 { 5419 Query: `SELECT -col2 AS col0 FROM tab2 HAVING col2 > -col0;`, 5420 ExpectedErr: sql.ErrColumnNotFound, 5421 }, 5422 { 5423 Query: `insert into tab2(col2) select sin(col2) from tab2 group by 1 having col2 > 1;`, 5424 ExpectedErr: sql.ErrColumnNotFound, 5425 }, 5426 }, 5427 }, 5428 { 5429 Name: "dividing has different rounding behavior", 5430 SetUpScript: []string{ 5431 "CREATE TABLE tab0(col0 INTEGER, col1 INTEGER, col2 INTEGER);", 5432 "INSERT INTO tab0 VALUES(97, 1, 99);", 5433 }, 5434 Assertions: []ScriptTestAssertion{ 5435 { 5436 Query: "SELECT col2 IN ( 98 + col0 / 99 ) from tab0;", 5437 Expected: []sql.Row{ 5438 {false}, 5439 }, 5440 }, 5441 { 5442 Query: "SELECT col2 IN ( 98 + 97 / 99 ) from tab0;", 5443 Expected: []sql.Row{ 5444 {false}, 5445 }, 5446 }, 5447 { 5448 Query: "SELECT * FROM tab0 WHERE col2 IN ( 98 + 97 / 99 );", 5449 Expected: []sql.Row{}, 5450 }, 5451 { 5452 Query: "SELECT ALL * FROM tab0 AS cor0 WHERE col2 IN ( 39 + + 89, col0 + + col1 + + ( - ( - col0 ) ) / col2, + ( col0 ) + - 99, + col1, + col2 * - + col2 * - 12 + col1 + - 66 );", 5453 Expected: []sql.Row{}, 5454 }, 5455 }, 5456 }, 5457 { 5458 Name: "complicated range tree", 5459 SetUpScript: []string{ 5460 "create table t1 (a1 int, b1 int, primary key(a1, b1));", 5461 }, 5462 Assertions: []ScriptTestAssertion{ 5463 { 5464 Query: ` 5465 SELECT * 5466 FROM t1 5467 WHERE 5468 a1 in (702, 584, 607, 479, 330, 445, 513, 678, 406, 314, 880, 953, 75, 268) OR 5469 b1 in (213, 55, 992, 922, 619, 972, 654, 130, 88, 141, 679, 761) OR 5470 (a1=145 AND b1=818); 5471 `, 5472 Expected: []sql.Row{}, 5473 }, 5474 }, 5475 }, 5476 { 5477 Name: "many joins with chain of ANDs", 5478 SetUpScript: []string{ 5479 "create table t1 (a1 int primary key, b1 int);", 5480 "create table t2 (a2 int primary key, b2 int);", 5481 "create table t3 (a3 int primary key, b3 int);", 5482 "create table t4 (a4 int primary key, b4 int);", 5483 "create table t5 (a5 int primary key, b5 int);", 5484 "create table t6 (a6 int primary key, b6 int);", 5485 "create table t7 (a7 int primary key, b7 int);", 5486 "create table t8 (a8 int primary key, b8 int);", 5487 "create table t9 (a9 int primary key, b9 int);", 5488 "create table t10 (a10 int primary key, b10 int);", 5489 "insert into t1 values (1, 1);", 5490 "insert into t2 values (1, 1);", 5491 "insert into t3 values (1, 1);", 5492 "insert into t4 values (1, 1);", 5493 "insert into t5 values (1, 1);", 5494 "insert into t6 values (1, 1);", 5495 "insert into t7 values (1, 1);", 5496 "insert into t8 values (1, 1);", 5497 "insert into t9 values (1, 1);", 5498 "insert into t10 values (1, 1);", 5499 "insert into t1 values (2, 2);", 5500 "insert into t2 values (2, 2);", 5501 "insert into t3 values (2, 2);", 5502 "insert into t4 values (2, 2);", 5503 "insert into t5 values (2, 2);", 5504 "insert into t6 values (2, 2);", 5505 "insert into t7 values (2, 2);", 5506 "insert into t8 values (2, 2);", 5507 "insert into t9 values (2, 2);", 5508 "insert into t10 values (2, 2);", 5509 "insert into t1 values (3, 3);", 5510 "insert into t2 values (3, 3);", 5511 "insert into t3 values (3, 3);", 5512 "insert into t4 values (3, 3);", 5513 "insert into t5 values (3, 3);", 5514 "insert into t6 values (3, 3);", 5515 "insert into t7 values (3, 3);", 5516 "insert into t8 values (3, 3);", 5517 "insert into t9 values (3, 3);", 5518 "insert into t10 values (3, 3);", 5519 "insert into t1 values (4, 4);", 5520 "insert into t2 values (4, 4);", 5521 "insert into t3 values (4, 4);", 5522 "insert into t4 values (4, 4);", 5523 "insert into t5 values (4, 4);", 5524 "insert into t6 values (4, 4);", 5525 "insert into t7 values (4, 4);", 5526 "insert into t8 values (4, 4);", 5527 "insert into t9 values (4, 4);", 5528 "insert into t10 values (4, 4);", 5529 }, 5530 Assertions: []ScriptTestAssertion{ 5531 { 5532 Query: ` 5533 select 5534 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 5535 from 5536 t1, t2, t3, t4, t5, t6, t7, t8, t9, t10 5537 where 5538 1 = a3 and 5539 b9 = a3 and 5540 b2 = a9 and 5541 b10 = a2 and 5542 b5 = a10 and 5543 b7 = a5 and 5544 b4 = a7 and 5545 b1 = a4 and 5546 b8 = a1 and 5547 b6 = a8 5548 ; 5549 `, 5550 Expected: []sql.Row{ 5551 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 5552 }, 5553 }, 5554 }, 5555 }, 5556 } 5557 5558 var SpatialScriptTests = []ScriptTest{ 5559 { 5560 Name: "create table using default point value", 5561 SetUpScript: []string{ 5562 "CREATE TABLE test (i int primary key, p point default (point(123.456, 7.89)));", 5563 "insert into test (i) values (0);", 5564 }, 5565 Assertions: []ScriptTestAssertion{ 5566 { 5567 Query: "select st_aswkt(p) from test", 5568 Expected: []sql.Row{{"POINT(123.456 7.89)"}}, 5569 }, 5570 { 5571 Query: "show create table test", 5572 Expected: []sql.Row{{"test", "CREATE TABLE `test` (\n `i` int NOT NULL,\n `p` point DEFAULT (point(123.456,7.89)),\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 5573 }, 5574 { 5575 Query: "describe test", 5576 Expected: []sql.Row{{"i", "int", "NO", "PRI", "NULL", ""}, {"p", "point", "YES", "", "(point(123.456,7.89))", "DEFAULT_GENERATED"}}, 5577 }, 5578 }, 5579 }, 5580 { 5581 Name: "create table using default linestring value", 5582 SetUpScript: []string{ 5583 "CREATE TABLE test (i int primary key, l linestring default (linestring(point(1,2), point(3,4))));", 5584 "insert into test (i) values (0);", 5585 }, 5586 Assertions: []ScriptTestAssertion{ 5587 { 5588 Query: "select st_aswkt(l) from test", 5589 Expected: []sql.Row{{"LINESTRING(1 2,3 4)"}}, 5590 }, 5591 { 5592 Query: "show create table test", 5593 Expected: []sql.Row{{"test", "CREATE TABLE `test` (\n `i` int NOT NULL,\n `l` linestring DEFAULT (linestring(point(1,2),point(3,4))),\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 5594 }, 5595 { 5596 Query: "describe test", 5597 Expected: []sql.Row{{"i", "int", "NO", "PRI", "NULL", ""}, {"l", "linestring", "YES", "", "(linestring(point(1,2),point(3,4)))", "DEFAULT_GENERATED"}}, 5598 }, 5599 }, 5600 }, 5601 { 5602 Name: "create table using default polygon value", 5603 SetUpScript: []string{ 5604 "CREATE TABLE test (i int primary key, p polygon default (polygon(linestring(point(0,0), point(1,1), point(2,2), point(0,0)))));", 5605 "insert into test (i) values (0);", 5606 }, 5607 Assertions: []ScriptTestAssertion{ 5608 { 5609 Query: "select st_aswkt(p) from test", 5610 Expected: []sql.Row{{"POLYGON((0 0,1 1,2 2,0 0))"}}, 5611 }, 5612 { 5613 Query: "show create table test", 5614 Expected: []sql.Row{{"test", "CREATE TABLE `test` (\n `i` int NOT NULL,\n `p` polygon DEFAULT (polygon(linestring(point(0,0),point(1,1),point(2,2),point(0,0)))),\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 5615 }, 5616 { 5617 Query: "describe test", 5618 Expected: []sql.Row{{"i", "int", "NO", "PRI", "NULL", ""}, {"p", "polygon", "YES", "", "(polygon(linestring(point(0,0),point(1,1),point(2,2),point(0,0))))", "DEFAULT_GENERATED"}}, 5619 }, 5620 }, 5621 }, 5622 { 5623 Name: "create geometry table using default point value", 5624 SetUpScript: []string{ 5625 "CREATE TABLE test (i int primary key, g geometry default (point(123.456, 7.89)));", 5626 "insert into test (i) values (0);", 5627 }, 5628 Assertions: []ScriptTestAssertion{ 5629 { 5630 Query: "select st_aswkt(g) from test", 5631 Expected: []sql.Row{{"POINT(123.456 7.89)"}}, 5632 }, 5633 { 5634 Query: "show create table test", 5635 Expected: []sql.Row{{"test", "CREATE TABLE `test` (\n `i` int NOT NULL,\n `g` geometry DEFAULT (point(123.456,7.89)),\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 5636 }, 5637 { 5638 Query: "describe test", 5639 Expected: []sql.Row{{"i", "int", "NO", "PRI", "NULL", ""}, {"g", "geometry", "YES", "", "(point(123.456,7.89))", "DEFAULT_GENERATED"}}, 5640 }, 5641 }, 5642 }, 5643 { 5644 Name: "create geometry table using default linestring value", 5645 SetUpScript: []string{ 5646 "CREATE TABLE test (i int primary key, g geometry default (linestring(point(1,2), point(3,4))));", 5647 "insert into test (i) values (0);", 5648 }, 5649 Assertions: []ScriptTestAssertion{ 5650 { 5651 Query: "select st_aswkt(g) from test", 5652 Expected: []sql.Row{{"LINESTRING(1 2,3 4)"}}, 5653 }, 5654 { 5655 Query: "show create table test", 5656 Expected: []sql.Row{{"test", "CREATE TABLE `test` (\n `i` int NOT NULL,\n `g` geometry DEFAULT (linestring(point(1,2),point(3,4))),\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 5657 }, 5658 { 5659 Query: "describe test", 5660 Expected: []sql.Row{{"i", "int", "NO", "PRI", "NULL", ""}, {"g", "geometry", "YES", "", "(linestring(point(1,2),point(3,4)))", "DEFAULT_GENERATED"}}, 5661 }, 5662 }, 5663 }, 5664 { 5665 Name: "create geometry table using default polygon value", 5666 SetUpScript: []string{ 5667 "CREATE TABLE test (i int primary key, g geometry default (polygon(linestring(point(0,0), point(1,1), point(2,2), point(0,0)))));", 5668 "insert into test (i) values (0);", 5669 }, 5670 Assertions: []ScriptTestAssertion{ 5671 { 5672 Query: "select st_aswkt(g) from test", 5673 Expected: []sql.Row{{"POLYGON((0 0,1 1,2 2,0 0))"}}, 5674 }, 5675 { 5676 Query: "show create table test", 5677 Expected: []sql.Row{{"test", "CREATE TABLE `test` (\n `i` int NOT NULL,\n `g` geometry DEFAULT (polygon(linestring(point(0,0),point(1,1),point(2,2),point(0,0)))),\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 5678 }, 5679 { 5680 Query: "describe test", 5681 Expected: []sql.Row{{"i", "int", "NO", "PRI", "NULL", ""}, {"g", "geometry", "YES", "", "(polygon(linestring(point(0,0),point(1,1),point(2,2),point(0,0))))", "DEFAULT_GENERATED"}}, 5682 }, 5683 }, 5684 }, 5685 { 5686 Name: "create table with NULL default values for geometry types", 5687 SetUpScript: []string{ 5688 "CREATE TABLE null_default (pk int NOT NULL PRIMARY KEY, v1 geometry DEFAULT NULL, v2 linestring DEFAULT NULL, v3 point DEFAULT NULL, v4 polygon DEFAULT NULL)", 5689 "insert into null_default(pk) values (0)", 5690 }, 5691 Assertions: []ScriptTestAssertion{ 5692 { 5693 Query: "select * from null_default", 5694 Expected: []sql.Row{{0, nil, nil, nil, nil}}, 5695 }, 5696 }, 5697 }, 5698 { 5699 Name: "create table using SRID value for geometry type", 5700 SetUpScript: []string{ 5701 "CREATE TABLE tab0 (i int primary key, g geometry srid 4326 default (point(1,1)));", 5702 }, 5703 Assertions: []ScriptTestAssertion{ 5704 { 5705 Query: "show create table tab0", 5706 Expected: []sql.Row{{"tab0", "CREATE TABLE `tab0` (\n `i` int NOT NULL,\n `g` geometry /*!80003 SRID 4326 */ DEFAULT (point(1,1)),\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 5707 }, 5708 { 5709 Query: "INSERT INTO tab0 VALUES (1, ST_GEOMFROMTEXT(ST_ASWKT(POINT(1,2)), 4326))", 5710 Expected: []sql.Row{{types.NewOkResult(1)}}, 5711 }, 5712 { 5713 Query: "select i, ST_ASWKT(g) FROM tab0", 5714 Expected: []sql.Row{{1, "POINT(1 2)"}}, 5715 }, 5716 { 5717 Query: "INSERT INTO tab0 VALUES (2, ST_GEOMFROMTEXT(ST_ASWKT(POINT(2,4))))", 5718 ExpectedErr: sql.ErrNotMatchingSRIDWithColName, 5719 }, 5720 { 5721 Query: "INSERT INTO tab0 VALUES (2, ST_GEOMFROMTEXT(ST_ASWKT(LINESTRING(POINT(1, 6),POINT(4, 3))), 4326))", 5722 Expected: []sql.Row{{types.NewOkResult(1)}}, 5723 }, 5724 { 5725 Query: "select i, ST_ASWKT(g) FROM tab0", 5726 Expected: []sql.Row{{1, "POINT(1 2)"}, {2, "LINESTRING(1 6,4 3)"}}, 5727 }, 5728 }, 5729 }, 5730 { 5731 Name: "create table using SRID value for linestring type", 5732 SetUpScript: []string{ 5733 "CREATE TABLE tab1 (i int primary key, l linestring srid 0);", 5734 }, 5735 Assertions: []ScriptTestAssertion{ 5736 { 5737 Query: "show create table tab1", 5738 Expected: []sql.Row{{"tab1", "CREATE TABLE `tab1` (\n `i` int NOT NULL,\n `l` linestring /*!80003 SRID 0 */,\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 5739 }, 5740 { 5741 Query: "INSERT INTO tab1 VALUES (1, LINESTRING(POINT(0, 0),POINT(2, 2)))", 5742 Expected: []sql.Row{{types.NewOkResult(1)}}, 5743 }, 5744 { 5745 Query: "select i, ST_ASWKT(l) FROM tab1", 5746 Expected: []sql.Row{{1, "LINESTRING(0 0,2 2)"}}, 5747 }, 5748 { 5749 Query: "INSERT INTO tab1 VALUES (2, ST_GEOMFROMTEXT(ST_ASWKT(LINESTRING(POINT(1, 6),POINT(4, 3))), 4326))", 5750 ExpectedErr: sql.ErrNotMatchingSRIDWithColName, 5751 }, 5752 { 5753 Query: "select i, ST_ASWKT(l) FROM tab1", 5754 Expected: []sql.Row{{1, "LINESTRING(0 0,2 2)"}}, 5755 }, 5756 }, 5757 }, 5758 { 5759 Name: "create table using SRID value for point type", 5760 SetUpScript: []string{ 5761 "CREATE TABLE tab2 (i int primary key);", 5762 }, 5763 Assertions: []ScriptTestAssertion{ 5764 { 5765 Query: "ALTER TABLE tab2 ADD COLUMN p POINT NOT NULL SRID 0", 5766 Expected: []sql.Row{{types.NewOkResult(0)}}, 5767 }, 5768 { 5769 Query: "show create table tab2", 5770 Expected: []sql.Row{{"tab2", "CREATE TABLE `tab2` (\n `i` int NOT NULL,\n `p` point NOT NULL /*!80003 SRID 0 */,\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 5771 }, 5772 { 5773 Query: "INSERT INTO tab2 VALUES (1, POINT(2, 2))", 5774 Expected: []sql.Row{{types.NewOkResult(1)}}, 5775 }, 5776 { 5777 Query: "select i, ST_ASWKT(p) FROM tab2", 5778 Expected: []sql.Row{{1, "POINT(2 2)"}}, 5779 }, 5780 { 5781 Query: "INSERT INTO tab2 VALUES (2, ST_GEOMFROMTEXT(ST_ASWKT(POINT(1, 6)), 4326))", 5782 ExpectedErr: sql.ErrNotMatchingSRIDWithColName, 5783 }, 5784 { 5785 Query: "select i, ST_ASWKT(p) FROM tab2", 5786 Expected: []sql.Row{{1, "POINT(2 2)"}}, 5787 }, 5788 { 5789 Query: "ALTER TABLE tab2 CHANGE COLUMN p p POINT NOT NULL", 5790 Expected: []sql.Row{{types.NewOkResult(0)}}, 5791 }, 5792 { 5793 Query: "INSERT INTO tab2 VALUES (2, ST_GEOMFROMTEXT(ST_ASWKT(POINT(1, 6)), 4326))", 5794 Expected: []sql.Row{{types.NewOkResult(1)}}, 5795 }, 5796 { 5797 Query: "select i, ST_ASWKT(p) FROM tab2", 5798 Expected: []sql.Row{{1, "POINT(2 2)"}, {2, "POINT(1 6)"}}, 5799 }, 5800 { 5801 Query: "ALTER TABLE tab2 CHANGE COLUMN p p POINT NOT NULL SRID 4326", 5802 ExpectedErr: sql.ErrNotMatchingSRIDWithColName, 5803 }, 5804 { 5805 Query: "delete from tab2 where i = 1", 5806 Expected: []sql.Row{{types.NewOkResult(1)}}, 5807 }, 5808 { 5809 Query: "ALTER TABLE tab2 CHANGE COLUMN p p POINT NOT NULL SRID 4326", 5810 Expected: []sql.Row{{types.NewOkResult(0)}}, 5811 }, 5812 { 5813 Query: "show create table tab2", 5814 Expected: []sql.Row{{"tab2", "CREATE TABLE `tab2` (\n `i` int NOT NULL,\n `p` point NOT NULL /*!80003 SRID 4326 */,\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 5815 }, 5816 }, 5817 }, 5818 { 5819 Name: "create table using SRID value for polygon type", 5820 SetUpScript: []string{ 5821 "CREATE TABLE tab3 (i int primary key, y polygon NOT NULL);", 5822 }, 5823 Assertions: []ScriptTestAssertion{ 5824 { 5825 Query: "show create table tab3", 5826 Expected: []sql.Row{{"tab3", "CREATE TABLE `tab3` (\n `i` int NOT NULL,\n `y` polygon NOT NULL,\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 5827 }, 5828 { 5829 Query: "INSERT INTO tab3 VALUES (1, polygon(linestring(point(0,0),point(8,0),point(12,9),point(0,9),point(0,0))))", 5830 Expected: []sql.Row{{types.NewOkResult(1)}}, 5831 }, 5832 { 5833 Query: "select i, ST_ASWKT(y) FROM tab3", 5834 Expected: []sql.Row{{1, "POLYGON((0 0,8 0,12 9,0 9,0 0))"}}, 5835 }, 5836 { 5837 Query: "ALTER TABLE tab3 MODIFY COLUMN y POLYGON NOT NULL SRID 0", 5838 Expected: []sql.Row{{types.NewOkResult(0)}}, 5839 }, 5840 { 5841 Query: "ALTER TABLE tab3 MODIFY COLUMN y POLYGON NOT NULL SRID 4326", 5842 ExpectedErr: sql.ErrNotMatchingSRIDWithColName, 5843 }, 5844 { 5845 Query: "select i, ST_ASWKT(y) FROM tab3", 5846 Expected: []sql.Row{{1, "POLYGON((0 0,8 0,12 9,0 9,0 0))"}}, 5847 }, 5848 { 5849 Query: "ALTER TABLE tab3 MODIFY COLUMN y GEOMETRY NULL SRID 0", 5850 Expected: []sql.Row{{types.NewOkResult(0)}}, 5851 }, 5852 { 5853 Query: "select i, ST_ASWKT(y) FROM tab3", 5854 Expected: []sql.Row{{1, "POLYGON((0 0,8 0,12 9,0 9,0 0))"}}, 5855 }, 5856 }, 5857 }, 5858 { 5859 Name: "invalid cases of SRID value", 5860 SetUpScript: []string{ 5861 "CREATE TABLE table1 (i int primary key, p point srid 4326);", 5862 "INSERT INTO table1 VALUES (1, ST_SRID(POINT(1, 5), 4326))", 5863 "CREATE TABLE table2 (i int primary key, g geometry /*!80003 SRID 3857*/);", 5864 }, 5865 Assertions: []ScriptTestAssertion{ 5866 { 5867 Query: "CREATE TABLE table3 (i int primary key, p point srid 1);", 5868 ExpectedErr: sql.ErrNoSRID, 5869 }, 5870 { 5871 Query: "CREATE TABLE table3 (i int primary key, p point srid 3857);", 5872 Expected: []sql.Row{{types.NewOkResult(0)}}, 5873 }, 5874 { 5875 Query: "show create table table2", 5876 Expected: []sql.Row{ 5877 {"table2", "CREATE TABLE `table2` (\n `i` int NOT NULL,\n `g` geometry /*!80003 SRID 3857 */,\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}, 5878 }, 5879 }, 5880 { 5881 Query: "SELECT i, ST_ASWKT(p) FROM table1;", 5882 Expected: []sql.Row{{1, "POINT(5 1)"}}, 5883 }, 5884 { 5885 Query: "INSERT INTO table1 VALUES (2, POINT(2, 5))", 5886 ExpectedErr: sql.ErrNotMatchingSRIDWithColName, 5887 }, 5888 { 5889 Query: "SELECT i, ST_ASWKT(p) FROM table1;", 5890 Expected: []sql.Row{{1, "POINT(5 1)"}}, 5891 }, 5892 { 5893 Query: "ALTER TABLE table1 CHANGE COLUMN p p linestring srid 4326", 5894 ExpectedErr: sql.ErrSpatialTypeConversion, 5895 }, 5896 { 5897 Query: "ALTER TABLE table1 CHANGE COLUMN p p geometry srid 0", 5898 ExpectedErr: sql.ErrNotMatchingSRIDWithColName, 5899 }, 5900 { 5901 Query: "ALTER TABLE table1 CHANGE COLUMN p p geometry srid 4326", 5902 Expected: []sql.Row{{types.NewOkResult(0)}}, 5903 }, 5904 { 5905 Query: "show create table table1", 5906 Expected: []sql.Row{{"table1", "CREATE TABLE `table1` (\n `i` int NOT NULL,\n `p` geometry /*!80003 SRID 4326 */,\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, 5907 }, 5908 { 5909 Query: "INSERT INTO table1 VALUES (2, ST_SRID(LINESTRING(POINT(0, 0),POINT(2, 2)),4326))", 5910 Expected: []sql.Row{{types.NewOkResult(1)}}, 5911 }, 5912 { 5913 Query: "ALTER TABLE table1 CHANGE COLUMN p p point srid 4326", 5914 ExpectedErr: sql.ErrSpatialTypeConversion, 5915 }, 5916 }, 5917 }, 5918 } 5919 5920 var SpatialIndexScriptTests = []ScriptTest{ 5921 { 5922 Name: "create spatial index errors", 5923 SetUpScript: []string{}, 5924 Assertions: []ScriptTestAssertion{ 5925 { 5926 Query: "create table geom(g geometry, SPATIAL INDEX(g))", 5927 ExpectedErr: sql.ErrNullableSpatialIdx, 5928 }, 5929 { 5930 Query: "create table geom(g geometry SRID 4326, SPATIAL INDEX(g))", 5931 ExpectedErr: sql.ErrNullableSpatialIdx, 5932 }, 5933 { 5934 Query: "create table geom(g1 geometry NOT NULL SRID 0, g2 geometry NOT NULL SRID 4326, SPATIAL INDEX(g1, g2))", 5935 ExpectedErr: sql.ErrTooManyKeyParts, 5936 }, 5937 }, 5938 }, 5939 { 5940 Name: "alter table spatial index nullable", 5941 SetUpScript: []string{ 5942 "create table geom(g geometry)", 5943 }, 5944 Assertions: []ScriptTestAssertion{ 5945 { 5946 Query: "alter table geom add spatial index (g)", 5947 ExpectedErr: sql.ErrNullableSpatialIdx, 5948 }, 5949 }, 5950 }, 5951 { 5952 Name: "alter table spatial index with srid nullable", 5953 SetUpScript: []string{ 5954 "create table geom(g geometry SRID 4326)", 5955 }, 5956 Assertions: []ScriptTestAssertion{ 5957 { 5958 Query: "alter table geom add spatial index (g)", 5959 ExpectedErr: sql.ErrNullableSpatialIdx, 5960 }, 5961 }, 5962 }, 5963 { 5964 Name: "show table with spatial indexes", 5965 SetUpScript: []string{ 5966 "create table geom(" + 5967 "p point not null srid 0," + 5968 "l linestring not null srid 0," + 5969 "py polygon not null srid 0," + 5970 "mp multipoint not null srid 0," + 5971 "ml multilinestring not null srid 0," + 5972 "mpy multipolygon not null srid 0," + 5973 "gc geometrycollection not null srid 0," + 5974 "g geometry not null srid 0)", 5975 "alter table geom add spatial index (p)", 5976 "alter table geom add spatial index (l)", 5977 "alter table geom add spatial index (py)", 5978 "alter table geom add spatial index (mp)", 5979 "alter table geom add spatial index (ml)", 5980 "alter table geom add spatial index (mpy)", 5981 "alter table geom add spatial index (gc)", 5982 "alter table geom add spatial index (g)", 5983 }, 5984 Assertions: []ScriptTestAssertion{ 5985 { 5986 Query: "show create table geom", 5987 Expected: []sql.Row{ 5988 { 5989 "geom", 5990 "CREATE TABLE `geom` (\n" + 5991 " `p` point NOT NULL /*!80003 SRID 0 */,\n" + 5992 " `l` linestring NOT NULL /*!80003 SRID 0 */,\n" + 5993 " `py` polygon NOT NULL /*!80003 SRID 0 */,\n" + 5994 " `mp` multipoint NOT NULL /*!80003 SRID 0 */,\n" + 5995 " `ml` multilinestring NOT NULL /*!80003 SRID 0 */,\n" + 5996 " `mpy` multipolygon NOT NULL /*!80003 SRID 0 */,\n" + 5997 " `gc` geometrycollection NOT NULL /*!80003 SRID 0 */,\n" + 5998 " `g` geometry NOT NULL /*!80003 SRID 0 */,\n" + 5999 " SPATIAL KEY `g` (`g`),\n" + 6000 " SPATIAL KEY `gc` (`gc`),\n" + 6001 " SPATIAL KEY `l` (`l`),\n" + 6002 " SPATIAL KEY `ml` (`ml`),\n" + 6003 " SPATIAL KEY `mp` (`mp`),\n" + 6004 " SPATIAL KEY `mpy` (`mpy`),\n" + 6005 " SPATIAL KEY `p` (`p`),\n" + 6006 " SPATIAL KEY `py` (`py`)\n" + 6007 ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin", 6008 }, 6009 }, 6010 }, 6011 }, 6012 }, 6013 { 6014 Name: "add spatial index to non-empty table", 6015 SetUpScript: []string{ 6016 "create table geom_tbl(g geometry not null srid 0)", 6017 "insert into geom_tbl values (point(0,0)), (linestring(point(1,1), point(2,2)))", 6018 }, 6019 Assertions: []ScriptTestAssertion{ 6020 { 6021 Query: "alter table geom_tbl add spatial index (g)", 6022 Expected: []sql.Row{ 6023 {types.NewOkResult(0)}, 6024 }, 6025 }, 6026 { 6027 Query: "show create table geom_tbl", 6028 Expected: []sql.Row{ 6029 {"geom_tbl", "CREATE TABLE `geom_tbl` (\n `g` geometry NOT NULL /*!80003 SRID 0 */,\n SPATIAL KEY `g` (`g`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}, 6030 }, 6031 }, 6032 { 6033 Query: "select count(*) from geom_tbl where st_intersects(g, st_geomfromtext('polygon((0 0,0 10,10 10,10 0,0 0))'))", 6034 Expected: []sql.Row{ 6035 {2}, 6036 }, 6037 }, 6038 }, 6039 }, 6040 { 6041 Name: "add spatial index to non-empty table with primary key", 6042 SetUpScript: []string{ 6043 "create table geom_tbl(i int, j int, g geometry not null srid 0, primary key (i, j))", 6044 "insert into geom_tbl values (1, 10, point(0,0)), (2, 20, linestring(point(1,1), point(2,2)))", 6045 }, 6046 Assertions: []ScriptTestAssertion{ 6047 { 6048 Query: "alter table geom_tbl add spatial index (g)", 6049 Expected: []sql.Row{ 6050 {types.NewOkResult(0)}, 6051 }, 6052 }, 6053 { 6054 Query: "show create table geom_tbl", 6055 Expected: []sql.Row{ 6056 {"geom_tbl", "CREATE TABLE `geom_tbl` (\n `i` int NOT NULL,\n `j` int NOT NULL,\n `g` geometry NOT NULL /*!80003 SRID 0 */,\n PRIMARY KEY (`i`,`j`),\n SPATIAL KEY `g` (`g`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}, 6057 }, 6058 }, 6059 { 6060 Query: "select count(*) from geom_tbl where st_intersects(g, st_geomfromtext('polygon((0 0,0 10,10 10,10 0,0 0))'))", 6061 Expected: []sql.Row{ 6062 {2}, 6063 }, 6064 }, 6065 }, 6066 }, 6067 { 6068 Name: "spatial indexes do not work as foreign keys", 6069 SetUpScript: []string{ 6070 "create table parent (i int primary key, p point not null srid 0, spatial index (p))", 6071 "create table child1 (j int primary key, p point not null srid 0, spatial index (p))", 6072 }, 6073 Assertions: []ScriptTestAssertion{ 6074 { 6075 Query: "alter table child1 add foreign key (p) references parent (p)", 6076 ExpectedErr: sql.ErrForeignKeyMissingReferenceIndex, 6077 }, 6078 { 6079 Query: "create table child2 (p point not null srid 0, spatial index (p), foreign key (p) references parent (p))", 6080 ExpectedErr: sql.ErrForeignKeyMissingReferenceIndex, 6081 }, 6082 }, 6083 }, 6084 } 6085 6086 var PreparedScriptTests = []ScriptTest{ 6087 { 6088 Name: "table_count optimization refreshes result", 6089 SetUpScript: []string{ 6090 "create table a (a int primary key);", 6091 "insert into a values (0), (1), (2);", 6092 }, 6093 Assertions: []ScriptTestAssertion{ 6094 { 6095 Query: "prepare cnt from 'select count(*) from a';", 6096 Expected: []sql.Row{{types.OkResult{Info: plan.PrepareInfo{}}}}, 6097 }, 6098 { 6099 Query: "execute cnt", 6100 Expected: []sql.Row{{3}}, 6101 }, 6102 { 6103 Query: "insert into a values (3), (4)", 6104 Expected: []sql.Row{ 6105 {types.OkResult{RowsAffected: 2}}, 6106 }, 6107 }, 6108 { 6109 Query: "execute cnt", 6110 Expected: []sql.Row{{5}}, 6111 }, 6112 }, 6113 }, 6114 { 6115 Name: "bad prepare", 6116 SetUpScript: []string{}, 6117 Assertions: []ScriptTestAssertion{ 6118 { 6119 Query: "prepare s from 'prepare t from ?'", 6120 ExpectedErrStr: "syntax error at position 17 near ':v1'", 6121 }, 6122 { 6123 Query: "prepare s from 'a very real query'", 6124 ExpectedErrStr: "syntax error at position 2 near 'a'", 6125 }, 6126 { 6127 Query: "deallocate prepare idontexist", 6128 ExpectedErr: sql.ErrUnknownPreparedStatement, 6129 }, 6130 }, 6131 }, 6132 { 6133 Name: "simple select case no bindings", 6134 SetUpScript: []string{}, 6135 Assertions: []ScriptTestAssertion{ 6136 { 6137 Query: "execute s", 6138 ExpectedErr: sql.ErrUnknownPreparedStatement, 6139 }, 6140 { 6141 Query: "prepare s from 'select 1'", 6142 Expected: []sql.Row{ 6143 {types.OkResult{Info: plan.PrepareInfo{}}}, 6144 }, 6145 }, 6146 { 6147 Query: "execute s", 6148 Expected: []sql.Row{ 6149 {1}, 6150 }, 6151 }, 6152 { 6153 Query: "deallocate prepare s", 6154 Expected: []sql.Row{ 6155 {types.OkResult{}}, 6156 }, 6157 }, 6158 { 6159 Query: "execute s", 6160 ExpectedErr: sql.ErrUnknownPreparedStatement, 6161 }, 6162 }, 6163 }, 6164 { 6165 Name: "simple select case one binding", 6166 SetUpScript: []string{ 6167 "set @a = 1", 6168 "set @b = 100", 6169 "set @c = 'abc'", 6170 }, 6171 Assertions: []ScriptTestAssertion{ 6172 { 6173 Query: "prepare s from 'select ?'", 6174 Expected: []sql.Row{ 6175 {types.OkResult{Info: plan.PrepareInfo{}}}, 6176 }, 6177 }, 6178 { 6179 Query: "execute s", 6180 ExpectedErrStr: "bind variable not provided: 'v1'", 6181 }, 6182 { 6183 Query: "execute s using @abc", 6184 Expected: []sql.Row{ 6185 {nil}, 6186 }, 6187 }, 6188 { 6189 Query: "execute s using @a, @b, @c, @abc", 6190 ExpectedErrStr: "invalid arguments. expected: 1, found: 4", 6191 }, 6192 { 6193 Query: "execute s using @a", 6194 Expected: []sql.Row{ 6195 {1}, 6196 }, 6197 }, 6198 { 6199 Query: "execute s using @b", 6200 Expected: []sql.Row{ 6201 {100}, 6202 }, 6203 }, 6204 { 6205 Query: "execute s using @c", 6206 Expected: []sql.Row{ 6207 {"abc"}, 6208 }, 6209 }, 6210 { 6211 Query: "deallocate prepare s", 6212 Expected: []sql.Row{ 6213 {types.OkResult{}}, 6214 }, 6215 }, 6216 { 6217 Query: "execute s using @a", 6218 ExpectedErr: sql.ErrUnknownPreparedStatement, 6219 }, 6220 }, 6221 }, 6222 { 6223 Name: "prepare insert", 6224 SetUpScript: []string{ 6225 "set @a = 123", 6226 "set @b = 'abc'", 6227 "create table t (i int, j varchar(100))", 6228 }, 6229 Assertions: []ScriptTestAssertion{ 6230 { 6231 Query: "prepare s from 'insert into t values (?,?)'", 6232 Expected: []sql.Row{ 6233 {types.OkResult{Info: plan.PrepareInfo{}}}, 6234 }, 6235 }, 6236 { 6237 Query: "execute s using @a", 6238 ExpectedErrStr: "bind variable not provided: 'v2'", 6239 }, 6240 { 6241 SkipResultCheckOnServerEngine: true, // execute depends on prepare stmt for whether to use 'query' or 'exec' from go sql driver. 6242 Query: "execute s using @a, @b", 6243 Expected: []sql.Row{ 6244 {types.OkResult{RowsAffected: 1}}, 6245 }, 6246 }, 6247 { 6248 Query: "select * from t order by i", 6249 Expected: []sql.Row{ 6250 {123, "abc"}, 6251 }, 6252 }, 6253 { 6254 Query: "deallocate prepare s", 6255 Expected: []sql.Row{ 6256 {types.OkResult{}}, 6257 }, 6258 }, 6259 { 6260 Query: "execute s using @a", 6261 ExpectedErr: sql.ErrUnknownPreparedStatement, 6262 }, 6263 }, 6264 }, 6265 { 6266 Name: "prepare using user vars", 6267 SetUpScript: []string{ 6268 "create table t (i int primary key);", 6269 "insert into t values (0), (1), (2);", 6270 "set @num = 123", 6271 "set @bad = 'bad'", 6272 "set @a = 'select * from t order by i'", 6273 "set @b = concat('select 1',' + 1')", 6274 "set @c = 'select 1 from dual limit ?'", 6275 "set @d = 'select @num'", 6276 }, 6277 Assertions: []ScriptTestAssertion{ 6278 { 6279 // non-existent vars is the same as preparing with NULL 6280 Query: "prepare stmt from @asdf", 6281 ExpectedErrStr: "syntax error at position 5 near 'NULL'", 6282 }, 6283 { 6284 Query: "prepare stmt from @num", 6285 ExpectedErrStr: "syntax error at position 4 near '123'", 6286 }, 6287 { 6288 Query: "prepare stmt from @bad", 6289 ExpectedErrStr: "syntax error at position 4 near 'bad'", 6290 }, 6291 { 6292 Query: "prepare stmt from @a", 6293 Expected: []sql.Row{ 6294 {types.OkResult{Info: plan.PrepareInfo{}}}, 6295 }, 6296 }, 6297 { 6298 Query: "execute stmt", 6299 Expected: []sql.Row{ 6300 {0}, 6301 {1}, 6302 {2}, 6303 }, 6304 }, 6305 { 6306 Query: "prepare stmt from @b", 6307 Expected: []sql.Row{ 6308 {types.OkResult{Info: plan.PrepareInfo{}}}, 6309 }, 6310 }, 6311 { 6312 Query: "execute stmt", 6313 Expected: []sql.Row{ 6314 {2}, 6315 }, 6316 }, 6317 { 6318 Query: "prepare stmt from @c", 6319 Expected: []sql.Row{ 6320 {types.OkResult{Info: plan.PrepareInfo{}}}, 6321 }, 6322 }, 6323 { 6324 Query: "execute stmt using @num", 6325 Expected: []sql.Row{ 6326 {1}, 6327 }, 6328 }, 6329 { 6330 Query: "prepare stmt from @d", 6331 Expected: []sql.Row{ 6332 {types.OkResult{Info: plan.PrepareInfo{}}}, 6333 }, 6334 }, 6335 { 6336 Query: "execute stmt", 6337 Expected: []sql.Row{ 6338 {123}, 6339 }, 6340 }, 6341 }, 6342 }, 6343 { 6344 Name: "Complex join query with foreign key constraints", 6345 SetUpScript: []string{ 6346 "CREATE TABLE `users` (`id` int NOT NULL AUTO_INCREMENT, `username` varchar(255) NOT NULL, PRIMARY KEY (`id`));", 6347 "CREATE TABLE `tweet` ( `id` int NOT NULL AUTO_INCREMENT, `user_id` int NOT NULL, `content` text NOT NULL, `timestamp` bigint NOT NULL, PRIMARY KEY (`id`), KEY `tweet_user_id` (`user_id`), CONSTRAINT `0qpfesgd` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`));", 6348 "INSERT INTO `users` (`id`,`username`) VALUES (1,'huey'), (2,'zaizee'), (3,'mickey');", 6349 "INSERT INTO `tweet` (`id`,`user_id`,`content`,`timestamp`) VALUES (1,1,'meow',1647463727), (2,1,'purr',1647463727), (3,2,'hiss',1647463727), (4,3,'woof',1647463727);", 6350 "set @u2 = 'u2';", 6351 "set @u3 = 'u3';", 6352 "set @u4 = 'u4';", 6353 }, 6354 Assertions: []ScriptTestAssertion{ 6355 { 6356 Query: "prepare s from 'SELECT `t1`.`username`, COUNT(`t1`.`id`) AS `ct` FROM ((SELECT `t2`.`id`, `t2`.`content`, `t3`.`username` FROM `tweet` AS `t2` INNER JOIN `users` AS `t3` ON (`t2`.`user_id` = `t3`.`id`) WHERE (`t3`.`username` = ?)) UNION (SELECT `t4`.`id`, `t4`.`content`, `t5`.`username` FROM `tweet` AS `t4` INNER JOIN `users` AS `t5` ON (`t4`.`user_id` = `t5`.`id`) WHERE (`t5`.`username` IN (?, ?)))) AS `t1` GROUP BY `t1`.`username` ORDER BY COUNT(`t1`.`id`) DESC'", 6357 Expected: []sql.Row{ 6358 {types.OkResult{Info: plan.PrepareInfo{}}}, 6359 }, 6360 }, 6361 { 6362 Query: "execute s using @u3, @u2, @u4", 6363 Expected: []sql.Row{}, 6364 }, 6365 }, 6366 }, 6367 { 6368 Name: "Drop column with check constraint, no other columns", 6369 SetUpScript: []string{ 6370 "create table mytable (pk int primary key);", 6371 "ALTER TABLE mytable ADD COLUMN col2 text NOT NULL;", 6372 "ALTER TABLE mytable ADD CONSTRAINT constraint_check CHECK (col2 LIKE '%myregex%');", 6373 }, 6374 Assertions: []ScriptTestAssertion{ 6375 { 6376 Query: "ALTER TABLE mytable DROP COLUMN col2", 6377 Expected: []sql.Row{{types.NewOkResult(0)}}, 6378 }, 6379 }, 6380 }, 6381 { 6382 Name: "Drop column with check constraint, other column referenced first", 6383 SetUpScript: []string{ 6384 "create table mytable (pk int primary key);", 6385 "ALTER TABLE mytable ADD COLUMN col2 text NOT NULL;", 6386 "ALTER TABLE mytable ADD COLUMN col3 text NOT NULL;", 6387 "ALTER TABLE mytable ADD CONSTRAINT constraint_check CHECK (col3 LIKE col2);", 6388 }, 6389 Assertions: []ScriptTestAssertion{ 6390 { 6391 Query: "ALTER TABLE mytable DROP COLUMN col2", 6392 ExpectedErr: sql.ErrCheckConstraintInvalidatedByColumnAlter, 6393 }, 6394 }, 6395 }, 6396 { 6397 Name: "Drop column with check constraint, other column referenced second", 6398 SetUpScript: []string{ 6399 "create table mytable (pk int primary key);", 6400 "ALTER TABLE mytable ADD COLUMN col2 text NOT NULL;", 6401 "ALTER TABLE mytable ADD COLUMN col3 text NOT NULL;", 6402 "ALTER TABLE mytable ADD CONSTRAINT constraint_check CHECK (col2 LIKE col3);", 6403 }, 6404 Assertions: []ScriptTestAssertion{ 6405 { 6406 Query: "ALTER TABLE mytable DROP COLUMN col2", 6407 ExpectedErr: sql.ErrCheckConstraintInvalidatedByColumnAlter, 6408 }, 6409 }, 6410 }, 6411 { 6412 Name: "Drop column with check constraint, multiple constraints", 6413 SetUpScript: []string{ 6414 "create table mytable (pk int primary key);", 6415 "ALTER TABLE mytable ADD COLUMN col2 text NOT NULL;", 6416 "ALTER TABLE mytable ADD COLUMN col3 text NOT NULL;", 6417 "ALTER TABLE mytable ADD CONSTRAINT ok_check CHECK (col2 LIKE '%myregex%');", 6418 "ALTER TABLE mytable ADD CONSTRAINT bad_check CHECK (col2 LIKE col3);", 6419 }, 6420 Assertions: []ScriptTestAssertion{ 6421 { 6422 Query: "ALTER TABLE mytable DROP COLUMN col2", 6423 ExpectedErr: sql.ErrCheckConstraintInvalidatedByColumnAlter, 6424 }, 6425 }, 6426 }, 6427 { 6428 // https://github.com/dolthub/dolthub-issues/issues/489 6429 Name: "Large character data", 6430 SetUpScript: []string{ 6431 "CREATE TABLE `test` (`id` int NOT NULL AUTO_INCREMENT, `data` blob NOT NULL, PRIMARY KEY (`id`))", 6432 }, 6433 Assertions: []ScriptTestAssertion{ 6434 { 6435 Query: `INSERT INTO test (data) values (?)`, 6436 Bindings: map[string]*querypb.BindVariable{ 6437 // Vitess chooses VARBINARY as the bindvar type if the client sends CHAR data 6438 // If we change how Vitess interprets client bindvar types, we should update this test 6439 // Or better yet: have a test harness that uses the server directly 6440 "v1": {Type: querypb.Type_VARBINARY, Value: []byte( 6441 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + 6442 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + 6443 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + 6444 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + 6445 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + 6446 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + 6447 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + 6448 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + 6449 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + 6450 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + 6451 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + 6452 "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + 6453 "")}, 6454 }, 6455 Expected: []sql.Row{{types.OkResult{ 6456 RowsAffected: 1, 6457 InsertID: 1, 6458 }}}, 6459 }, 6460 }, 6461 }, 6462 } 6463 6464 var BrokenScriptTests = []ScriptTest{ 6465 { 6466 Name: "ALTER TABLE MODIFY column with multiple UNIQUE KEYS", 6467 SetUpScript: []string{ 6468 "CREATE table test (pk int primary key, uk1 int, uk2 int, unique(uk1, uk2))", 6469 "ALTER TABLE `test` MODIFY column uk1 int auto_increment", 6470 }, 6471 Assertions: []ScriptTestAssertion{ 6472 { 6473 Query: "describe test", 6474 Expected: []sql.Row{ 6475 {"pk", "int", "NO", "PRI", "NULL", ""}, 6476 {"uk1", "int", "NO", "MUL", "NULL", "auto_increment"}, 6477 {"uk1", "int", "YES", "", "NULL", ""}, 6478 }, 6479 }, 6480 }, 6481 }, 6482 { 6483 Name: "ALTER TABLE MODIFY column with multiple KEYS", 6484 SetUpScript: []string{ 6485 "CREATE table test (pk int primary key, mk1 int, mk2 int, index(mk1, mk2))", 6486 "ALTER TABLE `test` MODIFY column mk1 int auto_increment", 6487 }, 6488 Assertions: []ScriptTestAssertion{ 6489 { 6490 Query: "describe test", 6491 Expected: []sql.Row{ 6492 {"pk", "int", "NO", "PRI", "NULL", ""}, 6493 {"mk1", "int", "NO", "MUL", "NULL", "auto_increment"}, 6494 {"mk1", "int", "YES", "", "NULL", ""}, 6495 }, 6496 }, 6497 }, 6498 }, 6499 { 6500 Name: "ALTER TABLE RENAME on a column when another column has a default dependency on it", 6501 SetUpScript: []string{ 6502 "CREATE TABLE `test` (`pk` bigint NOT NULL,`v2` int NOT NULL DEFAULT '100',`v3` int DEFAULT ((`v2` + 1)),PRIMARY KEY (`pk`));", 6503 }, 6504 Assertions: []ScriptTestAssertion{ 6505 { 6506 Query: "alter table test rename column v2 to mycol", 6507 ExpectedErr: sql.ErrAlterTableNotSupported, // Not the correct error. The point is that this query needs to fail. 6508 }, 6509 }, 6510 }, 6511 // TODO: We should implement unique indexes with GMS 6512 { 6513 Name: "Keyless Table with Unique Index", 6514 SetUpScript: []string{ 6515 "create table a (x int, val int unique)", 6516 }, 6517 Assertions: []ScriptTestAssertion{ 6518 { 6519 Query: "INSERT INTO a VALUES (1, 1)", 6520 Expected: []sql.Row{{types.NewOkResult(1)}}, 6521 }, 6522 { 6523 Query: "INSERT INTO a VALUES (1, 1)", 6524 ExpectedErr: sql.ErrUniqueKeyViolation, 6525 }, 6526 }, 6527 }, 6528 { 6529 Name: "Multialter DDL with ADD/DROP Primary Key", 6530 SetUpScript: []string{ 6531 "CREATE TABLE t(pk int primary key, v1 int)", 6532 }, 6533 Assertions: []ScriptTestAssertion{ 6534 { 6535 Query: "ALTER TABLE t ADD COLUMN (v2 int), drop primary key, add primary key (v2)", 6536 Expected: []sql.Row{{types.NewOkResult(0)}}, 6537 }, 6538 { 6539 Query: "DESCRIBE t", 6540 Expected: []sql.Row{ 6541 {"pk", "int", "NO", "", "NULL", ""}, 6542 {"v1", "int", "YES", "", "NULL", ""}, 6543 {"v2", "int", "NO", "PRI", "NULL", ""}, 6544 }, 6545 }, 6546 { 6547 Query: "ALTER TABLE t ADD COLUMN (v3 int), drop primary key, add primary key (notacolumn)", 6548 ExpectedErr: sql.ErrKeyColumnDoesNotExist, 6549 }, 6550 { 6551 Query: "DESCRIBE t", 6552 Expected: []sql.Row{ 6553 {"pk", "int", "NO", "", "NULL", ""}, 6554 {"v1", "int", "YES", "", "NULL", ""}, 6555 {"v2", "int", "NO", "PRI", "NULL", ""}, 6556 }, 6557 }, 6558 { 6559 // This last modification ends up with a UNIQUE constraint on pk 6560 // This is caused by Table.dropColumnFromSchema, not dropping the pkOrdinal, but this causes other problems specific to GMS 6561 Query: "ALTER TABLE t ADD column `v4` int NOT NULL, ADD column `v5` int NOT NULL, DROP COLUMN `v1`, ADD COLUMN `v6` int NOT NULL, DROP COLUMN `v2`, ADD COLUMN v7 int NOT NULL", 6562 Expected: []sql.Row{{types.NewOkResult(0)}}, 6563 }, 6564 { 6565 Query: "DESCRIBE t", 6566 Expected: []sql.Row{ 6567 {"pk", "int", "NO", "", "NULL", ""}, 6568 {"v4", "int", "NO", "", "NULL", ""}, 6569 {"v5", "int", "NO", "", "NULL", ""}, 6570 {"v6", "int", "NO", "", "NULL", ""}, 6571 {"v7", "int", "NO", "", "NULL", ""}, 6572 }, 6573 }, 6574 }, 6575 }, 6576 { 6577 Name: "REGEXP operator", 6578 SetUpScript: []string{ 6579 "CREATE TABLE IF NOT EXISTS `person` (`id` INTEGER AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` VARCHAR(255) NOT NULL);", 6580 "INSERT INTO `person` (`name`) VALUES ('n1'), ('n2'), ('n3')", 6581 }, 6582 Assertions: []ScriptTestAssertion{ 6583 { 6584 Query: "SELECT `t1`.`id`, `t1`.`name` FROM `person` AS `t1` WHERE (`t1`.`name` REGEXP 'N[1,3]') ORDER BY `t1`.`name`;", 6585 Expected: []sql.Row{{1, "n1"}, {3, "n3"}}, 6586 }, 6587 }, 6588 }, 6589 { 6590 Name: "non-existent procedure in trigger body", 6591 SetUpScript: []string{ 6592 "CREATE TABLE XA(YW VARCHAR(24) NOT NULL, XB VARCHAR(100), XC VARCHAR(2500),\n XD VARCHAR(2500), XE VARCHAR(100), XF VARCHAR(100), XG VARCHAR(100),\n XI VARCHAR(100), XJ VARCHAR(100), XK VARCHAR(100), XL VARCHAR(100),\n XM VARCHAR(1000), XN TEXT, XO TEXT, PRIMARY KEY (YW));", 6593 "CREATE TABLE XP(YW VARCHAR(24) NOT NULL, XQ VARCHAR(100) NOT NULL,\n XR VARCHAR(1000), PRIMARY KEY (YW));", 6594 "CREATE TABLE XS(YW VARCHAR(24) NOT NULL, XT VARCHAR(24) NOT NULL,\n XU VARCHAR(24), XV VARCHAR(100) NOT NULL, XW DOUBLE NOT NULL,\n XX DOUBLE NOT NULL, XY VARCHAR(100), XC VARCHAR(100), XZ VARCHAR(100) NOT NULL,\n YA DOUBLE, YB VARCHAR(24) NOT NULL, YC VARCHAR(1000), XO VARCHAR(1000),\n YD DOUBLE NOT NULL, YE DOUBLE NOT NULL, PRIMARY KEY (YW));", 6595 "CREATE TABLE YF(YW VARCHAR(24) NOT NULL, XB VARCHAR(100) NOT NULL, YG VARCHAR(100),\n YH VARCHAR(100), XO TEXT, PRIMARY KEY (YW));", 6596 "CREATE TABLE yp(YW VARCHAR(24) NOT NULL, XJ VARCHAR(100) NOT NULL, XL VARCHAR(100),\n XT VARCHAR(24) NOT NULL, YI INT NOT NULL, XO VARCHAR(1000), PRIMARY KEY (YW),\n FOREIGN KEY (XT) REFERENCES XP (YW));", 6597 "INSERT INTO XS VALUES ('', '', NULL, 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC', 0, 0,\n NULL, NULL, '', NULL, '', NULL, NULL, 0, 0);", 6598 "INSERT INTO YF VALUES ('', '', NULL, NULL, NULL);", 6599 "INSERT INTO XA VALUES ('', '', '', '', '', 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC',\n '', '', '', '', '', '', '', '');", 6600 }, 6601 Assertions: []ScriptTestAssertion{ 6602 { 6603 Query: "SELECT DISTINCT YM.YW AS YW,\n (SELECT YW FROM YF WHERE YF.XB = YM.XB) AS YF_YW,\n (\n SELECT YW\n FROM yp\n WHERE\n yp.XJ = YM.XJ AND\n (yp.XL = YM.XL OR (yp.XL IS NULL AND YM.XL IS NULL)) AND\n yp.XT = nd.XT\n ) AS YJ,\n XE AS XE,\n XI AS YO,\n XK AS XK,\n XM AS XM,\n CASE\n WHEN YM.XO <> 'Z'\n THEN YM.XO\n ELSE NULL\n END AS XO\n FROM (\n SELECT YW, XB, XC, XE, XF, XI, XJ, XK,\n CASE WHEN XL = 'Z' OR XL = 'Z' THEN NULL ELSE XL END AS XL,\n XM, XO\n FROM XA\n ) YM\n INNER JOIN XS nd\n ON nd.XV = XF\n WHERE\n XB IN (SELECT XB FROM YF) AND\n (XF IS NOT NULL AND XF <> 'Z')\n UNION\n SELECT DISTINCT YL.YW AS YW,\n (\n SELECT YW\n FROM YF\n WHERE YF.XB = YL.XB\n ) AS YF_YW,\n (\n SELECT YW FROM yp\n WHERE\n yp.XJ = YL.XJ AND\n (yp.XL = YL.XL OR (yp.XL IS NULL AND YL.XL IS NULL)) AND\n yp.XT = YN.XT\n ) AS YJ,\n XE AS XE,\n XI AS YO,\n XK AS XK,\n XM AS XM,\n CASE WHEN YL.XO <> 'Z' THEN YL.XO ELSE NULL END AS XO\n FROM (\n SELECT YW, XB, XC, XE, XF, XI, XJ, XK,\n CASE WHEN XL = 'Z' OR XL = 'Z' THEN NULL ELSE XL END AS XL,\n XM, XO\n FROM XA\n ) YL\n INNER JOIN XS YN\n ON YN.XC = YL.XC\n WHERE\n XB IN (SELECT XB FROM YF) AND \n (XF IS NULL OR XF = 'Z');", 6604 Expected: []sql.Row{{"", "", "", "", "", "", "", ""}}, 6605 }, 6606 }, 6607 }, 6608 { 6609 Name: "non-existent procedure in trigger body", 6610 SetUpScript: []string{ 6611 "create table tbl_I (i int primary key);", 6612 }, 6613 Assertions: []ScriptTestAssertion{ 6614 { 6615 Query: "alter table tbl_i add column j int, add check (j < 10);", 6616 Expected: []sql.Row{ 6617 {types.NewOkResult(0)}, 6618 }, 6619 }, 6620 }, 6621 }, 6622 { 6623 Name: "renaming table name that is referenced in existing view", 6624 SetUpScript: []string{ 6625 "create table t1 (id int primary key, v1 int);", 6626 "insert into t1 values (1,1);", 6627 "create view v1 as select * from t1;", 6628 }, 6629 Assertions: []ScriptTestAssertion{ 6630 { 6631 Query: "select * from v1;", 6632 Expected: []sql.Row{{1, 1}}, 6633 }, 6634 { 6635 Query: "rename table t1 to t2;", 6636 Expected: []sql.Row{{types.OkResult{}}}, 6637 }, 6638 { 6639 Query: "show tables;", 6640 Expected: []sql.Row{{"myview"}, {"t2"}, {"v1"}}, 6641 }, 6642 { 6643 Query: "select * from v1;", 6644 ExpectedErrStr: "View 'v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them", 6645 }, 6646 { 6647 Query: "show create view v1;", 6648 Expected: []sql.Row{{"v1", "CREATE VIEW `v1` AS select * from t1", "utf8mb4", "utf8mb4_0900_bin"}}, 6649 ExpectedWarningsCount: 1, 6650 }, 6651 { 6652 Query: "show warnings;", 6653 Expected: []sql.Row{{"Warning", 1356, "View 'v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them"}}, 6654 }, 6655 }, 6656 }, 6657 { 6658 Name: "TIMESTAMP type value should be converted from session TZ to UTC TZ to be stored", 6659 SetUpScript: []string{ 6660 "CREATE TABLE timezone_test (ts TIMESTAMP, dt DATETIME)", 6661 "INSERT INTO timezone_test VALUES ('2023-02-14 08:47', '2023-02-14 08:47');", 6662 }, 6663 Assertions: []ScriptTestAssertion{ 6664 { 6665 Query: "SET SESSION time_zone = '-05:00';", 6666 Expected: []sql.Row{{}}, 6667 }, 6668 { 6669 Query: "SELECT DATE_FORMAT(ts, '%H:%i:%s'), DATE_FORMAT(dt, '%H:%i:%s') from timezone_test;", 6670 Expected: []sql.Row{{"11:47:00", "08:47:00"}}, 6671 }, 6672 { 6673 Query: "SELECT UNIX_TIMESTAMP(ts), UNIX_TIMESTAMP(dt) from timezone_test;", 6674 Expected: []sql.Row{{float64(1676393220), float64(1676382420)}}, 6675 }, 6676 }, 6677 }, 6678 } 6679 6680 var CreateDatabaseScripts = []ScriptTest{ 6681 { 6682 Name: "CREATE DATABASE and create table", 6683 Assertions: []ScriptTestAssertion{ 6684 { 6685 Query: "CREATE DATABASE testdb", 6686 Expected: []sql.Row{{types.OkResult{RowsAffected: 1}}}, 6687 }, 6688 { 6689 Query: "USE testdb", 6690 Expected: []sql.Row{}, 6691 }, 6692 { 6693 Query: "SELECT DATABASE()", 6694 Expected: []sql.Row{{"testdb"}}, 6695 }, 6696 { 6697 Query: "CREATE TABLE test (pk int primary key)", 6698 Expected: []sql.Row{{types.NewOkResult(0)}}, 6699 }, 6700 { 6701 Query: "SHOW TABLES", 6702 Expected: []sql.Row{{"test"}}, 6703 }, 6704 }, 6705 }, 6706 { 6707 Name: "CREATE DATABASE IF NOT EXISTS", 6708 Assertions: []ScriptTestAssertion{ 6709 { 6710 Query: "CREATE DATABASE IF NOT EXISTS testdb2", 6711 Expected: []sql.Row{{types.OkResult{RowsAffected: 1}}}, 6712 }, 6713 { 6714 Query: "USE testdb2", 6715 Expected: []sql.Row{}, 6716 }, 6717 { 6718 Query: "SELECT DATABASE()", 6719 Expected: []sql.Row{{"testdb2"}}, 6720 }, 6721 { 6722 Query: "CREATE TABLE test (pk int primary key)", 6723 Expected: []sql.Row{{types.NewOkResult(0)}}, 6724 }, 6725 { 6726 Query: "SHOW TABLES", 6727 Expected: []sql.Row{{"test"}}, 6728 }, 6729 }, 6730 }, 6731 { 6732 Name: "CREATE SCHEMA", 6733 Assertions: []ScriptTestAssertion{ 6734 { 6735 Query: "CREATE SCHEMA testdb3", 6736 Expected: []sql.Row{{types.OkResult{RowsAffected: 1}}}, 6737 }, 6738 { 6739 Query: "USE testdb3", 6740 Expected: []sql.Row{}, 6741 }, 6742 { 6743 Query: "SELECT DATABASE()", 6744 Expected: []sql.Row{{"testdb3"}}, 6745 }, 6746 { 6747 Query: "CREATE TABLE test (pk int primary key)", 6748 Expected: []sql.Row{{types.NewOkResult(0)}}, 6749 }, 6750 { 6751 Query: "SHOW TABLES", 6752 Expected: []sql.Row{{"test"}}, 6753 }, 6754 }, 6755 }, 6756 { 6757 Name: "CREATE DATABASE error handling", 6758 Assertions: []ScriptTestAssertion{ 6759 { 6760 Query: "CREATE DATABASE newtestdb CHARACTER SET utf8mb4 ENCRYPTION='N'", 6761 Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 0, Info: nil}}}, 6762 }, 6763 { 6764 SkipResultCheckOnServerEngine: true, // tracking issue here, https://github.com/dolthub/dolt/issues/6921. Also for when run with prepares, the warning is added twice 6765 Query: "SHOW WARNINGS /* 1 */", 6766 Expected: []sql.Row{{"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"}}, 6767 }, 6768 { 6769 Query: "CREATE DATABASE newtest1db DEFAULT COLLATE binary ENCRYPTION='Y'", 6770 Expected: []sql.Row{{types.OkResult{RowsAffected: 1, InsertID: 0, Info: nil}}}, 6771 }, 6772 { 6773 SkipResultCheckOnServerEngine: true, // tracking issue here, https://github.com/dolthub/dolt/issues/6921. 6774 // TODO: There should only be one warning (the warnings are not clearing for create database query) AND 'PREPARE' statements should not create warning from its query 6775 Query: "SHOW WARNINGS /* 2 */", 6776 Expected: []sql.Row{{"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"}, {"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"}}, 6777 }, 6778 { 6779 Query: "CREATE DATABASE mydb", 6780 ExpectedErr: sql.ErrDatabaseExists, 6781 }, 6782 { 6783 Query: "CREATE DATABASE IF NOT EXISTS mydb", 6784 Expected: []sql.Row{{types.OkResult{RowsAffected: 1}}}, 6785 }, 6786 { 6787 Query: "SHOW WARNINGS /* 3 */", 6788 Expected: []sql.Row{{"Note", 1007, "Can't create database mydb; database exists "}}, 6789 }, 6790 }, 6791 }, 6792 } 6793 6794 var DropDatabaseScripts = []ScriptTest{ 6795 { 6796 Name: "DROP DATABASE correctly works", 6797 Assertions: []ScriptTestAssertion{ 6798 { 6799 Query: "DROP DATABASE mydb", 6800 Expected: []sql.Row{{types.OkResult{RowsAffected: 1}}}, 6801 }, 6802 { 6803 Query: "SELECT DATABASE()", 6804 Expected: []sql.Row{{nil}}, 6805 }, 6806 { 6807 // TODO: incorrect error returned because the currentdb is not set to empty 6808 Skip: true, 6809 Query: "SHOW TABLES", 6810 ExpectedErr: sql.ErrNoDatabaseSelected, 6811 }, 6812 }, 6813 }, 6814 { 6815 Name: "DROP DATABASE works on newly created databases.", 6816 SetUpScript: []string{ 6817 "CREATE DATABASE testdb", 6818 }, 6819 Assertions: []ScriptTestAssertion{ 6820 { 6821 Query: "USE testdb", 6822 Expected: []sql.Row{}, 6823 }, 6824 { 6825 Query: "DROP DATABASE testdb", 6826 Expected: []sql.Row{{types.OkResult{RowsAffected: 1}}}, 6827 }, 6828 { 6829 Query: "USE testdb", 6830 ExpectedErr: sql.ErrDatabaseNotFound, 6831 }, 6832 }, 6833 }, 6834 { 6835 Name: "DROP DATABASE works on current database and sets current database to empty.", 6836 SetUpScript: []string{ 6837 "CREATE DATABASE testdb", 6838 "USE TESTdb", 6839 }, 6840 Assertions: []ScriptTestAssertion{ 6841 { 6842 Query: "DROP DATABASE TESTDB", 6843 Expected: []sql.Row{{types.OkResult{RowsAffected: 1}}}, 6844 }, 6845 { 6846 Query: "SELECT DATABASE()", 6847 Expected: []sql.Row{{nil}}, 6848 }, 6849 { 6850 Query: "USE testdb", 6851 ExpectedErr: sql.ErrDatabaseNotFound, 6852 }, 6853 }, 6854 }, 6855 { 6856 Name: "DROP SCHEMA works on newly created databases.", 6857 SetUpScript: []string{ 6858 "CREATE SCHEMA testdb", 6859 }, 6860 Assertions: []ScriptTestAssertion{ 6861 { 6862 Query: "DROP SCHEMA TESTDB", 6863 Expected: []sql.Row{{types.OkResult{RowsAffected: 1}}}, 6864 }, 6865 { 6866 Query: "USE testdb", 6867 ExpectedErr: sql.ErrDatabaseNotFound, 6868 }, 6869 }, 6870 }, 6871 { 6872 Name: "DROP DATABASE IF EXISTS correctly works.", 6873 SetUpScript: []string{ 6874 "DROP DATABASE mydb", 6875 "CREATE DATABASE testdb", 6876 }, 6877 Assertions: []ScriptTestAssertion{ 6878 { 6879 Query: "DROP DATABASE IF EXISTS mydb", 6880 Expected: []sql.Row{{types.OkResult{RowsAffected: 0}}}, 6881 }, 6882 { 6883 Query: "SHOW WARNINGS", 6884 Expected: []sql.Row{{"Note", 1008, "Can't drop database mydb; database doesn't exist "}}, 6885 }, 6886 { 6887 Query: "DROP DATABASE IF EXISTS testdb", 6888 Expected: []sql.Row{{types.OkResult{RowsAffected: 1}}}, 6889 }, 6890 { 6891 // TODO: there should not be warning 6892 // https://github.com/dolthub/dolt/issues/6921 6893 Query: "SHOW WARNINGS", 6894 Expected: []sql.Row{{"Note", 1008, "Can't drop database mydb; database doesn't exist "}}, 6895 }, 6896 { 6897 Query: "SELECT DATABASE()", 6898 Expected: []sql.Row{{nil}}, 6899 }, 6900 { 6901 Query: "USE testdb", 6902 ExpectedErr: sql.ErrDatabaseNotFound, 6903 }, 6904 { 6905 Query: "DROP DATABASE IF EXISTS testdb", 6906 Expected: []sql.Row{{types.OkResult{RowsAffected: 0}}}, 6907 }, 6908 { 6909 Query: "SHOW WARNINGS", 6910 Expected: []sql.Row{{"Note", 1008, "Can't drop database testdb; database doesn't exist "}}, 6911 }, 6912 }, 6913 }, 6914 }