github.com/dolthub/go-mysql-server@v0.18.0/enginetest/queries/variable_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 20 "github.com/dolthub/go-mysql-server/sql" 21 "github.com/dolthub/go-mysql-server/sql/types" 22 ) 23 24 var VariableQueries = []ScriptTest{ 25 { 26 Name: "use string name for foreign_key checks", 27 SetUpScript: []string{}, 28 Query: "select @@GLOBAL.unknown", 29 ExpectedErr: sql.ErrUnknownSystemVariable, 30 }, 31 { 32 Name: "use string name for foreign_key checks", 33 SetUpScript: []string{}, 34 Query: "set @@foreign_key_checks = off;", 35 Expected: []sql.Row{{}}, 36 }, 37 { 38 Name: "set system variables", 39 SetUpScript: []string{ 40 "set @@auto_increment_increment = 100, sql_select_limit = 1", 41 }, 42 Query: "SELECT @@auto_increment_increment, @@sql_select_limit", 43 Expected: []sql.Row{ 44 {100, 1}, 45 }, 46 }, 47 { 48 Name: "select join_complexity_limit", 49 Query: "SELECT @@join_complexity_limit", 50 Expected: []sql.Row{ 51 {uint64(12)}, 52 }, 53 }, 54 { 55 Name: "set join_complexity_limit", 56 SetUpScript: []string{ 57 "set @@join_complexity_limit = 2", 58 }, 59 Query: "SELECT @@join_complexity_limit", 60 Expected: []sql.Row{ 61 {uint64(2)}, 62 }, 63 }, 64 { 65 Name: "variable scope is included in returned column name when explicitly provided", 66 Assertions: []ScriptTestAssertion{ 67 { 68 Query: "select @@max_allowed_packet;", 69 Expected: []sql.Row{{1073741824}}, 70 ExpectedColumns: sql.Schema{ 71 { 72 Name: "@@max_allowed_packet", 73 Type: types.Uint64, 74 }, 75 }, 76 }, 77 { 78 Query: "select @@session.max_allowed_packet;", 79 Expected: []sql.Row{{1073741824}}, 80 ExpectedColumns: sql.Schema{ 81 { 82 Name: "@@session.max_allowed_packet", 83 Type: types.Uint64, 84 }, 85 }, 86 }, 87 { 88 Query: "select @@global.max_allowed_packet;", 89 Expected: []sql.Row{{1073741824}}, 90 ExpectedColumns: sql.Schema{ 91 { 92 Name: "@@global.max_allowed_packet", 93 Type: types.Uint64, 94 }, 95 }, 96 }, 97 { 98 Query: "select @@GLoBAL.max_allowed_packet;", 99 Expected: []sql.Row{{1073741824}}, 100 ExpectedColumns: sql.Schema{ 101 { 102 Name: "@@GLoBAL.max_allowed_packet", 103 Type: types.Uint64, 104 }, 105 }, 106 }, 107 }, 108 }, 109 { 110 Name: "@@server_id", 111 Assertions: []ScriptTestAssertion{ 112 { 113 Query: "select @@server_id;", 114 Expected: []sql.Row{{uint32(0)}}, 115 }, 116 { 117 Query: "set @@server_id=123;", 118 Expected: []sql.Row{{}}, 119 }, 120 { 121 Query: "set @@GLOBAL.server_id=123;", 122 Expected: []sql.Row{{}}, 123 }, 124 { 125 Query: "set @@GLOBAL.server_id=0;", 126 Expected: []sql.Row{{}}, 127 }, 128 }, 129 }, 130 { 131 Name: "set system variables and user variables", 132 SetUpScript: []string{ 133 "SET @myvar = @@autocommit", 134 "SET autocommit = @myvar", 135 "SET @myvar2 = @myvar - 1, @myvar3 = @@autocommit - 1", 136 }, 137 Assertions: []ScriptTestAssertion{ 138 { 139 Query: "select @myvar, @@autocommit, @myvar2, @myvar3", 140 Expected: []sql.Row{ 141 {1, 1, 0, 0}, 142 }, 143 }, 144 }, 145 }, 146 { 147 Name: "set system variables mixed case", 148 SetUpScript: []string{ 149 "set @@auto_increment_INCREMENT = 100, sql_select_LIMIT = 1", 150 }, 151 Query: "SELECT @@auto_increment_increment, @@sql_select_limit", 152 Expected: []sql.Row{ 153 {100, 1}, 154 }, 155 }, 156 { 157 Name: "set system variable defaults", 158 SetUpScript: []string{ 159 "set @@auto_increment_increment = 100, sql_select_limit = 1", 160 "set @@auto_increment_increment = default, sql_select_limit = default", 161 }, 162 Query: "SELECT @@auto_increment_increment, @@sql_select_limit", 163 Expected: []sql.Row{ 164 {1, math.MaxInt32}, 165 }, 166 }, 167 { 168 Name: "set system variable ON / OFF", 169 SetUpScript: []string{ 170 "set @@autocommit = ON, sql_mode = \"\"", 171 }, 172 Query: "SELECT @@autocommit, @@session.sql_mode", 173 Expected: []sql.Row{ 174 {1, ""}, 175 }, 176 }, 177 { 178 Name: "set system variable ON / OFF", 179 SetUpScript: []string{ 180 "set @@autocommit = ON, session sql_mode = \"\"", 181 }, 182 Query: "SELECT @@autocommit, @@session.sql_mode", 183 Expected: []sql.Row{ 184 {1, ""}, 185 }, 186 }, 187 { 188 Name: "set system variable sql_mode to ANSI for session", 189 SetUpScript: []string{ 190 "set SESSION sql_mode = 'ANSI'", 191 }, 192 Query: "SELECT @@session.sql_mode", 193 Expected: []sql.Row{ 194 {"ANSI"}, 195 }, 196 }, 197 { 198 Name: "set system variable true / false quoted", 199 SetUpScript: []string{ 200 `set @@autocommit = "true", default_table_encryption = "false"`, 201 }, 202 Query: "SELECT @@autocommit, @@session.default_table_encryption", 203 Expected: []sql.Row{ 204 {1, 0}, 205 }, 206 }, 207 { 208 Name: "set system variable true / false", 209 SetUpScript: []string{ 210 `set @@autocommit = true, default_table_encryption = false`, 211 }, 212 Query: "SELECT @@autocommit, @@session.default_table_encryption", 213 Expected: []sql.Row{ 214 {1, 0}, 215 }, 216 }, 217 { 218 Name: "set system variable with expressions", 219 SetUpScript: []string{ 220 `set lc_messages = '123', @@auto_increment_increment = 1`, 221 `set lc_messages = concat(@@lc_messages, '456'), @@auto_increment_increment = @@auto_increment_increment + 3`, 222 }, 223 Query: "SELECT @@lc_messages, @@auto_increment_increment", 224 Expected: []sql.Row{ 225 {"123456", 4}, 226 }, 227 }, 228 { 229 Name: "set system variable to another system variable", 230 SetUpScript: []string{ 231 `set @@auto_increment_increment = 123`, 232 `set @@sql_select_limit = @@auto_increment_increment`, 233 }, 234 Query: "SELECT @@sql_select_limit", 235 Expected: []sql.Row{ 236 {123}, 237 }, 238 }, 239 { 240 Name: "set names", 241 SetUpScript: []string{ 242 `set names utf8mb4`, 243 }, 244 Query: "SELECT @@character_set_client, @@character_set_connection, @@character_set_results", 245 Expected: []sql.Row{ 246 {"utf8mb4", "utf8mb4", "utf8mb4"}, 247 }, 248 }, 249 // TODO: we should validate the character set here 250 { 251 Name: "set names quoted", 252 SetUpScript: []string{ 253 `set NAMES "utf8mb3"`, 254 }, 255 Query: "SELECT @@character_set_client, @@character_set_connection, @@character_set_results", 256 Expected: []sql.Row{ 257 {"utf8mb3", "utf8mb3", "utf8mb3"}, 258 }, 259 }, 260 { 261 Name: "set character set", 262 SetUpScript: []string{ 263 `set character set utf8`, 264 }, 265 Query: "SELECT @@character_set_client, @@character_set_connection, @@character_set_results", 266 Expected: []sql.Row{ 267 {"utf8", "utf8mb4", "utf8"}, 268 }, 269 }, 270 { 271 Name: "set charset", 272 SetUpScript: []string{ 273 `set charset utf8`, 274 }, 275 Query: "SELECT @@character_set_client, @@character_set_connection, @@character_set_results", 276 Expected: []sql.Row{ 277 {"utf8", "utf8mb4", "utf8"}, 278 }, 279 }, 280 { 281 Name: "set charset quoted", 282 SetUpScript: []string{ 283 `set charset 'utf8'`, 284 }, 285 Query: "SELECT @@character_set_client, @@character_set_connection, @@character_set_results", 286 Expected: []sql.Row{ 287 {"utf8", "utf8mb4", "utf8"}, 288 }, 289 }, 290 { 291 Name: "set multiple variables including 'names'", 292 SetUpScript: []string{ 293 "set SESSION sql_mode = 'ANSI'", 294 `SET sql_mode=(SELECT CONCAT(@@sql_mode, ',PIPES_AS_CONCAT,NO_ENGINE_SUBSTITUTION')), time_zone='+00:00', NAMES utf8mb3 COLLATE utf8mb3_bin;`, 295 }, 296 Query: "SELECT @@sql_mode, @@time_zone, @@character_set_client, @@character_set_connection, @@character_set_results", 297 Expected: []sql.Row{ 298 {"NO_ENGINE_SUBSTITUTION,PIPES_AS_CONCAT,ANSI", "+00:00", "utf8mb3", "utf8mb3", "utf8mb3"}, 299 }, 300 }, 301 { 302 Name: "set multiple variables including 'charset'", 303 SetUpScript: []string{ 304 `SET sql_mode=ALLOW_INVALID_DATES, time_zone='+00:00', CHARSET 'utf8'`, 305 }, 306 Query: "SELECT @@sql_mode, @@time_zone, @@character_set_client, @@character_set_connection, @@character_set_results", 307 Expected: []sql.Row{ 308 {"ALLOW_INVALID_DATES", "+00:00", "utf8", "utf8mb4", "utf8"}, 309 }, 310 }, 311 { 312 Name: "set system variable to bareword", 313 SetUpScript: []string{ 314 `set @@sql_mode = ALLOW_INVALID_DATES`, 315 }, 316 Query: "SELECT @@sql_mode", 317 Expected: []sql.Row{ 318 {"ALLOW_INVALID_DATES"}, 319 }, 320 }, 321 { 322 Name: "set system variable to bareword, unqualified", 323 SetUpScript: []string{ 324 `set sql_mode = ALLOW_INVALID_DATES`, 325 }, 326 Query: "SELECT @@sql_mode", 327 Expected: []sql.Row{ 328 {"ALLOW_INVALID_DATES"}, 329 }, 330 }, 331 { 332 Name: "set sql_mode variable from mysqldump", 333 SetUpScript: []string{ 334 `SET sql_mode = 'STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_ENGINE_SUBSTITUTION'`, 335 }, 336 Query: "SELECT @@sql_mode", 337 Expected: []sql.Row{ 338 {"ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,STRICT_ALL_TABLES,STRICT_TRANS_TABLES,TRADITIONAL"}, 339 }, 340 }, 341 { 342 Name: "show variables renders enums after set", 343 SetUpScript: []string{ 344 `set @@sql_mode='ONLY_FULL_GROUP_BY';`, 345 }, 346 Assertions: []ScriptTestAssertion{ 347 { 348 Query: `SHOW VARIABLES LIKE '%sql_mode%'`, 349 Expected: []sql.Row{ 350 {"sql_mode", "ONLY_FULL_GROUP_BY"}, 351 }, 352 }, 353 }, 354 }, 355 // User variables 356 { 357 Name: "set user var", 358 SetUpScript: []string{ 359 `set @myvar = "hello"`, 360 }, 361 Query: "SELECT @myvar", 362 Expected: []sql.Row{ 363 {"hello"}, 364 }, 365 }, 366 { 367 Name: "set user var, integer type", 368 SetUpScript: []string{ 369 `set @myvar = 123`, 370 }, 371 Query: "SELECT @myvar", 372 Expected: []sql.Row{ 373 {123}, 374 }, 375 }, 376 { 377 Name: "set user var, floating point", 378 SetUpScript: []string{ 379 `set @myvar = 123.4`, 380 }, 381 Query: "SELECT @myvar", 382 Expected: []sql.Row{ 383 {"123.4"}, 384 }, 385 }, 386 { 387 Name: "set user var and sys var in same statement", 388 SetUpScript: []string{ 389 `set @myvar = 123.4, @@auto_increment_increment = 1234`, 390 }, 391 Query: "SELECT @myvar, @@auto_increment_increment", 392 Expected: []sql.Row{ 393 {"123.4", 1234}, 394 }, 395 }, 396 { 397 Name: "set sys var to user var", 398 SetUpScript: []string{ 399 `set @myvar = 1234`, 400 `set auto_increment_increment = @myvar`, 401 }, 402 Query: "SELECT @myvar, @@auto_increment_increment", 403 Expected: []sql.Row{ 404 {1234, 1234}, 405 }, 406 }, 407 { 408 Name: "local is session", 409 SetUpScript: []string{ 410 `set @@LOCAL.cte_max_recursion_depth = 1234`, 411 }, 412 Query: "SELECT @@SESSION.cte_max_recursion_depth", 413 Expected: []sql.Row{ 414 {1234}, 415 }, 416 }, 417 { 418 Name: "user and system var with same name", 419 SetUpScript: []string{ 420 `set @cte_max_recursion_depth = 55`, 421 `set cte_max_recursion_depth = 77`, 422 }, 423 Query: "SELECT @cte_max_recursion_depth, @@cte_max_recursion_depth", 424 Expected: []sql.Row{ 425 {55, 77}, 426 }, 427 }, 428 { 429 Name: "uninitialized user vars", 430 Assertions: []ScriptTestAssertion{ 431 { 432 Query: "SELECT @doesNotExist;", 433 Expected: []sql.Row{{nil}}, 434 }, 435 { 436 Query: "SELECT @doesNotExist is NULL;", 437 Expected: []sql.Row{{true}}, 438 }, 439 { 440 Query: "SELECT @doesNotExist='';", 441 Expected: []sql.Row{{nil}}, 442 }, 443 { 444 Query: "SELECT @doesNotExist < 123;", 445 Expected: []sql.Row{{nil}}, 446 }, 447 }, 448 }, 449 450 { 451 Name: "eval string user var", 452 SetUpScript: []string{ 453 "set @stringVar = 'abc'", 454 }, 455 Assertions: []ScriptTestAssertion{ 456 { 457 Query: "SELECT @stringVar='abc'", 458 Expected: []sql.Row{{true}}, 459 }, 460 { 461 Query: "SELECT @stringVar='abcd';", 462 Expected: []sql.Row{{false}}, 463 }, 464 { 465 Query: "SELECT @stringVar=123;", 466 Expected: []sql.Row{{false}}, 467 }, 468 { 469 Query: "SELECT @stringVar is null;", 470 Expected: []sql.Row{{false}}, 471 }, 472 }, 473 }, 474 { 475 Name: "set transaction", 476 Assertions: []ScriptTestAssertion{ 477 { 478 Query: "set transaction isolation level serializable, read only", 479 Expected: []sql.Row{{}}, 480 }, 481 { 482 Query: "select @@transaction_isolation, @@transaction_read_only", 483 Expected: []sql.Row{{"SERIALIZABLE", 1}}, 484 }, 485 { 486 Query: "set transaction read write, isolation level read uncommitted", 487 Expected: []sql.Row{{}}, 488 }, 489 { 490 Query: "select @@transaction_isolation, @@transaction_read_only", 491 Expected: []sql.Row{{"READ-UNCOMMITTED", 0}}, 492 }, 493 { 494 Query: "set transaction isolation level read committed", 495 Expected: []sql.Row{{}}, 496 }, 497 { 498 Query: "select @@transaction_isolation", 499 Expected: []sql.Row{{"READ-COMMITTED"}}, 500 }, 501 { 502 Query: "set transaction isolation level repeatable read", 503 Expected: []sql.Row{{}}, 504 }, 505 { 506 Query: "select @@transaction_isolation", 507 Expected: []sql.Row{{"REPEATABLE-READ"}}, 508 }, 509 { 510 Query: "set session transaction isolation level serializable, read only", 511 Expected: []sql.Row{{}}, 512 }, 513 { 514 Query: "select @@transaction_isolation, @@transaction_read_only", 515 Expected: []sql.Row{{"SERIALIZABLE", 1}}, 516 }, 517 { 518 Query: "set global transaction read write, isolation level read uncommitted", 519 Expected: []sql.Row{{}}, 520 }, 521 { 522 Query: "select @@transaction_isolation, @@transaction_read_only", 523 Expected: []sql.Row{{"SERIALIZABLE", 1}}, 524 }, 525 { 526 Query: "select @@global.transaction_isolation, @@global.transaction_read_only", 527 Expected: []sql.Row{{"READ-UNCOMMITTED", 0}}, 528 }, 529 }, 530 }, 531 //TODO: do not override tables with user-var-like names...but why would you do this?? 532 //{ 533 // Name: "user var table name no conflict", 534 // SetUpScript: []string{ 535 // "create table test (pk bigint primary key, `@v1` bigint)", 536 // `insert into test values (1, 123)`, 537 // `set @v1 = 1234`, 538 // }, 539 // Query: "SELECT @v1, `@v1` from test", 540 // Expected: []sql.Row{ 541 // {1234, 123}, 542 // }, 543 //}, 544 } 545 546 var VariableErrorTests = []QueryErrorTest{ 547 { 548 Query: "set @@does_not_exist = 100", 549 ExpectedErr: sql.ErrUnknownSystemVariable, 550 }, 551 { 552 Query: "set @myvar = bareword", 553 ExpectedErr: sql.ErrColumnNotFound, 554 }, 555 { 556 Query: "set @@sql_mode = true", 557 ExpectedErr: sql.ErrInvalidSystemVariableValue, 558 }, 559 { 560 Query: `set @@sql_mode = "NOT_AN_OPTION"`, 561 ExpectedErr: sql.ErrInvalidSetValue, 562 }, 563 { 564 Query: `set global core_file = true`, 565 ExpectedErr: sql.ErrSystemVariableReadOnly, 566 }, 567 { 568 Query: `set global require_row_format = on`, 569 ExpectedErr: sql.ErrSystemVariableSessionOnly, 570 }, 571 { 572 Query: `set session default_password_lifetime = 5`, 573 ExpectedErr: sql.ErrSystemVariableGlobalOnly, 574 }, 575 { 576 Query: `set @custom_var = default`, 577 ExpectedErr: sql.ErrUserVariableNoDefault, 578 }, 579 { 580 Query: `set session @@bulk_insert_buffer_size = 5`, 581 ExpectedErr: sql.ErrSyntaxError, 582 }, 583 { 584 Query: `set global @@bulk_insert_buffer_size = 5`, 585 ExpectedErr: sql.ErrSyntaxError, 586 }, 587 { 588 Query: `set session @@session.bulk_insert_buffer_size = 5`, 589 ExpectedErr: sql.ErrSyntaxError, 590 }, 591 { 592 Query: `set session @@global.bulk_insert_buffer_size = 5`, 593 ExpectedErr: sql.ErrSyntaxError, 594 }, 595 { 596 Query: `set global @@session.bulk_insert_buffer_size = 5`, 597 ExpectedErr: sql.ErrSyntaxError, 598 }, 599 { 600 Query: `set global @@global.bulk_insert_buffer_size = 5`, 601 ExpectedErr: sql.ErrSyntaxError, 602 }, 603 { 604 Query: `set session @myvar = 5`, 605 ExpectedErr: sql.ErrSyntaxError, 606 }, 607 { 608 Query: `set global @myvar = 5`, 609 ExpectedErr: sql.ErrSyntaxError, 610 }, 611 { 612 Query: `set @@session.@@bulk_insert_buffer_size = 5`, 613 ExpectedErr: sql.ErrSyntaxError, 614 }, 615 { 616 Query: `set @@global.@@bulk_insert_buffer_size = 5`, 617 ExpectedErr: sql.ErrSyntaxError, 618 }, 619 { 620 Query: `set @@session.@bulk_insert_buffer_size = 5`, 621 ExpectedErr: sql.ErrSyntaxError, 622 }, 623 { 624 Query: `set @@global.@bulk_insert_buffer_size = 5`, 625 ExpectedErr: sql.ErrSyntaxError, 626 }, 627 { 628 Query: `set @@session.@myvar = 5`, 629 ExpectedErr: sql.ErrSyntaxError, 630 }, 631 { 632 Query: `set @@global.@myvar = 5`, 633 ExpectedErr: sql.ErrSyntaxError, 634 }, 635 }