1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
|
CREATE TABLE t1 (int_col INTEGER, string_col VARCHAR(255));
INSERT INTO t1 (int_col, string_col) VALUES (-1, "foo"), (1, "bar");
CREATE INDEX int_func_index ON t1 ((ABS(int_col)));
CREATE INDEX string_func_index ON t1 ((SUBSTRING(string_col, 1, 2)));
ANALYZE TABLE t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
# We should see the functional index syntax in SHOW CREATE TABLE
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`int_col` int DEFAULT NULL,
`string_col` varchar(255) DEFAULT NULL,
KEY `int_func_index` ((abs(`int_col`))),
KEY `string_func_index` ((substr(`string_col`,1,2)))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# INFORMATION_SCHEMA.STATISTICS should show the expression and no column
# name
SELECT * FROM INFORMATION_SCHEMA.STATISTICS
WHERE INDEX_NAME IN ('int_func_index', 'string_func_index');
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME NON_UNIQUE INDEX_SCHEMA INDEX_NAME SEQ_IN_INDEX COLUMN_NAME COLLATION CARDINALITY SUB_PART PACKED NULLABLE INDEX_TYPE COMMENT INDEX_COMMENT IS_VISIBLE EXPRESSION
def test t1 1 test int_func_index 1 NULL A 1 NULL NULL YES BTREE YES abs(`int_col`)
def test t1 1 test string_func_index 1 NULL A 2 NULL NULL YES BTREE YES substr(`string_col`,1,2)
# INFORMATION_SCHEMA.COLUMNS should not show the hidden generated
# columns.
SELECT COUNT(*) AS should_be_2 FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = "t1";
should_be_2
2
# INFORMATION_SCHEMA.INNODB_COLUMNS should not show the hidden generated
# columns.
SELECT COUNT(*) AS should_be_2 FROM INFORMATION_SCHEMA.INNODB_COLUMNS c
JOIN INFORMATION_SCHEMA.INNODB_TABLES t ON (c.TABLE_ID = t.TABLE_ID)
WHERE t.NAME = "test/t1";
should_be_2
2
# The optimizer should be able to make use of the functional index.
# Also, the query printed as a warning from EXPLAIN should display the
# expression and not the name of the hidden generated column.
EXPLAIN SELECT * FROM t1 WHERE SUBSTRING(string_col, 1, 2) = "fo";
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref string_func_index string_func_index 11 const 1 100.00 NULL
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`int_col` AS `int_col`,`test`.`t1`.`string_col` AS `string_col` from `test`.`t1` where (substr(`string_col`,1,2) = 'fo')
EXPLAIN SELECT * FROM t1 WHERE ABS(int_col) = 1;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref int_func_index int_func_index 5 const 2 100.00 NULL
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`int_col` AS `int_col`,`test`.`t1`.`string_col` AS `string_col` from `test`.`t1` where (abs(`int_col`) = 1)
# Creating multiple nameless functional indexes should automatically
# generate new names that doesn't collide.
CREATE TABLE t2 (
col1 INT,
INDEX ((col1 * 2)),
INDEX ((col1 * 4)),
INDEX ((col1 * 6)));
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`col1` int DEFAULT NULL,
KEY `functional_index` (((`col1` * 2))),
KEY `functional_index_2` (((`col1` * 4))),
KEY `functional_index_3` (((`col1` * 6)))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
DROP TABLE t2;
# Adding a multi-column index with both a functional part and a
# non-functional part should work just fine.
CREATE INDEX combined_index ON t1 ((int_col + int_col), string_col);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`int_col` int DEFAULT NULL,
`string_col` varchar(255) DEFAULT NULL,
KEY `int_func_index` ((abs(`int_col`))),
KEY `string_func_index` ((substr(`string_col`,1,2))),
KEY `combined_index` (((`int_col` + `int_col`)),`string_col`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# ...and, the optimizer should be able to use this multi-column index
EXPLAIN SELECT * FROM t1 WHERE int_col + int_col = 2 AND string_col = "bar";
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref combined_index combined_index 1032 const,const 1 100.00 NULL
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`int_col` AS `int_col`,`test`.`t1`.`string_col` AS `string_col` from `test`.`t1` where ((`test`.`t1`.`string_col` = 'bar') and ((`int_col` + `int_col`) = 2))
# Test that if we have a string column with the same name as a function,
# we will pick the function. To pick the column, skip the enclosing
# parentheses around the expression.
ALTER TABLE t1 ADD COLUMN rand VARCHAR(255);
CREATE INDEX name_collision ON t1 ((rand(2)));
ERROR HY000: Expression of functional index 'name_collision' contains a disallowed function.
CREATE INDEX name_collision ON t1 ((`rand`(2)));
ERROR HY000: Expression of functional index 'name_collision' contains a disallowed function.
DROP TABLE t1;
# Check that in case of errors when saving data to the generated column,
# the functional index name is printed and not the auto generated name
# of the generated column.
CREATE TABLE t1 (f1 JSON, f2 VARCHAR(255));
CREATE INDEX my_functional_index ON t1 ((CAST(f1 AS DECIMAL(2, 1))));
CREATE INDEX my_functional_index_2 ON t1 ((CAST(f2 AS CHAR(1))));
CREATE INDEX idx1 ON t1 ((CAST(f2 AS JSON)));
ERROR 42000: Cannot create a functional index on a function that returns a JSON or GEOMETRY value.
INSERT INTO t1 (f1) VALUES (CAST(1000 AS JSON));
ERROR 22003: Value is out of range for functional index 'my_functional_index' at row 1
INSERT INTO t1 (f2) VALUES ("lorem ipsum");
ERROR 01000: Data truncated for functional index 'my_functional_index_2' at row 1
# In non-strict mode, a warning should be printed.
SET @@sql_mode='';
INSERT INTO t1 (f1) VALUES (CAST(1000 AS JSON));
Warnings:
Warning 3752 Value is out of range for functional index 'my_functional_index' at row 1
INSERT INTO t1 (f2) VALUES ("lorem ipsum");
Warnings:
Warning 3751 Data truncated for functional index 'my_functional_index_2' at row 1
DROP TABLE t1;
SET @@sql_mode=DEFAULT;
CREATE TABLE t1 (t1_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
CREATE INDEX idx1 ON t1 ((t1_id + t1_id));
ERROR HY000: Functional index 'idx1' cannot refer to an auto-increment column.
DROP TABLE t1;
# Check that "SHOW KEYS" prints out the expression for generated columns
# (Column_name should be NULL, and Expression should contain the
# expression).
CREATE TABLE t1 (col1 INT, col2 INT);
CREATE INDEX idx1 ON t1 (col1, (col1 + col2));
SHOW KEYS FROM t1;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression
t1 1 idx1 1 col1 A 0 NULL NULL YES BTREE YES NULL
t1 1 idx1 2 NULL A 0 NULL NULL YES BTREE YES (`col1` + `col2`)
# Create a functional index with many functional key parts. Bug#27168185.
CREATE INDEX idx2
ON t1 ((col1 + 1), (col1 + 2), (col1 + 3), (col1 + 4), (col1 + 5));
# Check that if we try to add a column with the same name as a hidden
# column, we get an internal error. The column name of the generated
# column is `!hidden!index_name!key_part!counter`.
ALTER TABLE t1 ADD COLUMN `!hidden!idx1!1!0` INT NOT NULL;
ERROR HY000: Internal error: The column name '!hidden!idx1!1!0' is already in use by a hidden column
ALTER TABLE t1 ADD COLUMN `!hidden!idx3!0!0` INT NOT NULL;
CREATE INDEX idx3 ON t1 ((col1-col2));
ALTER TABLE t1 ADD COLUMN `!hidden!idx3!0!1` INT NOT NULL;
ERROR HY000: Internal error: The column name '!hidden!idx3!0!1' is already in use by a hidden column
DROP TABLE t1;
# Creating a functional index on already existing columns using
# ALTER TABLE should work just fine.
CREATE TABLE t1 (col1 INT, col2 INT);
ALTER TABLE t1 ADD INDEX ((ABS(col1))), ADD INDEX ((ABS(col2)));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`col1` int DEFAULT NULL,
`col2` int DEFAULT NULL,
KEY `functional_index` ((abs(`col1`))),
KEY `functional_index_2` ((abs(`col2`)))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
# Adding a functional index to a column that is created in the same
# ALTER TABLE statement should work.
ALTER TABLE t1 ADD COLUMN col3 INT, ADD INDEX ((col1 - col3));
DROP TABLE t1;
# Adding a functional index using CREATE TABLE should work
CREATE TABLE t1 (col1 INT, INDEX ((ABS(col1))));
DROP TABLE t1;
# Print out functional indexes on a temporary table. Note that
# "Column_name" should be NULL, and Expression should contain the
# expression for the functional index.
CREATE TEMPORARY TABLE t1(a INT);
CREATE INDEX idx ON t1 ((ABS(a)));
SHOW KEYS FROM t1;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression
t1 1 idx 1 NULL A 0 NULL NULL YES BTREE YES abs(`a`)
DROP TABLE t1;
CREATE TABLE t1 (col1 INT, INDEX ((CONCAT(''))));
ERROR HY000: The used storage engine cannot index the expression 'concat(_utf8mb4'')'.
# Check that a functional index cannot be a part of the primary key
CREATE TABLE t1 (col1 INT, PRIMARY KEY ((ABS(col1))));
ERROR HY000: The primary key cannot be a functional index
CREATE TABLE t1 (col1 INT, PRIMARY KEY (col1, (ABS(col1))));
ERROR HY000: The primary key cannot be a functional index
CREATE TABLE t1 (col1 INT, col2 INT, PRIMARY KEY (col1, (ABS(col1)), col2));
ERROR HY000: The primary key cannot be a functional index
CREATE TABLE t1 (col1 INT, col2 INT);
ALTER TABLE t1 ADD PRIMARY KEY ((ABS(col1)));
ERROR HY000: The primary key cannot be a functional index
ALTER TABLE t1 ADD PRIMARY KEY (col2, (ABS(col1)));
ERROR HY000: The primary key cannot be a functional index
ALTER TABLE t1 ADD PRIMARY KEY (col1, col2, (ABS(col1)));
ERROR HY000: The primary key cannot be a functional index
DROP TABLE t1;
# Check that descending functional index works
CREATE TABLE t1 (col1 INT, INDEX ((ABS(col1)) DESC));
EXPLAIN SELECT col1 FROM t1 WHERE ABS(col1) < 1 ORDER BY ABS(col1) DESC;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL range functional_index functional_index 5 NULL 1 100.00 Using where
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`col1` AS `col1` from `test`.`t1` where (abs(`col1`) < 1) order by abs(`test`.`t1`.`col1`) desc
DROP TABLE t1;
# Test integer index over a JSON key
CREATE TABLE t1(f1 JSON, INDEX idx1 ((CAST(f1->"$.id" AS UNSIGNED))));
INSERT INTO t1 VALUES(CAST('{"id":1}' AS JSON)), (CAST('{"id":2}' AS JSON)),
(CAST('{"id":3}' AS JSON)), (CAST('{"id":4}' AS JSON)),
(CAST('{"id":5}' AS JSON)), (CAST('{"id":6}' AS JSON)),
(CAST('{"id":7}' AS JSON)), (CAST('{"id":8}' AS JSON)),
(CAST('{"id":9}' AS JSON)), (CAST('{"id":10}' AS JSON));
ANALYZE TABLE t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
SELECT * FROM t1 WHERE f1->"$.id"= 5;
f1
{"id": 5}
EXPLAIN SELECT * FROM t1 WHERE f1->"$.id"= 5;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref idx1 idx1 9 const 1 100.00 NULL
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where (cast(json_extract(`f1`,_utf8mb4'$.id') as unsigned) = 5)
SELECT * FROM t1 WHERE f1->"$.id" IN (1,2,3);
f1
{"id": 1}
{"id": 2}
{"id": 3}
EXPLAIN SELECT * FROM t1 WHERE f1->"$.id" IN (1,2,3);
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL range idx1 idx1 9 NULL 3 100.00 Using where
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where (cast(json_extract(`f1`,_utf8mb4'$.id') as unsigned) in (1,2,3))
DROP TABLE t1;
Test string index over a JSON key
CREATE TABLE t1(f1 JSON, INDEX idx1 ((CAST(f1->>"$.id" AS CHAR(10)))));
INSERT INTO t1 VALUES
(CAST('{"id":"a"}' AS JSON)), (CAST('{"id":"b"}' AS JSON)),
(CAST('{"id":"v"}' AS JSON)), (CAST('{"id":"c"}' AS JSON)),
(CAST('{"id":"x"}' AS JSON)), (CAST('{"id":"\'z"}' AS JSON)),
(JSON_OBJECT("id",JSON_QUOTE("n"))), (CAST('{"id":"w"}' AS JSON)),
(CAST('{"id":"m"}' AS JSON)), (CAST('{"id":"q"}' AS JSON));
ANALYZE TABLE t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
SELECT * FROM t1 WHERE CAST(f1->>"$.id" AS CHAR(10)) = "\"n\"";
f1
{"id": "\"n\""}
EXPLAIN SELECT * FROM t1 WHERE CAST(f1->>"$.id" AS CHAR(10)) = "\"n\"";
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref idx1 idx1 43 const 1 100.00 NULL
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where (cast(json_unquote(json_extract(`f1`,_utf8mb4'$.id')) as char(10) charset utf8mb4) = '"n"')
Warnings:
SELECT * FROM t1 WHERE CAST(f1->>"$.id" AS CHAR(10)) IN ("'z", "\"n\"","a");
f1
{"id": "'z"}
{"id": "\"n\""}
{"id": "a"}
EXPLAIN SELECT * FROM t1 WHERE CAST(f1->>"$.id" AS CHAR(10)) IN ("'z", "\"n\"","a");
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL range idx1 idx1 43 NULL 3 100.00 Using where
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where (cast(json_unquote(json_extract(`f1`,_utf8mb4'$.id')) as char(10) charset utf8mb4) in ('\'z','"n"','a'))
Warnings:
DROP TABLE t1;
CREATE TABLE t1(f1 JSON, INDEX idx1 ((CAST(f1->>"$.name" AS CHAR(30)) COLLATE utf8mb4_bin)));
INSERT INTO t1 VALUES
('{"name": "james"}'),
('{"name": "JAMES"}'),
('{"name": "Peter"}'),
('{"name": "parker"}');
ANALYZE TABLE t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
# Should not return any rows, and should use the index
SELECT * FROM t1 WHERE f1->>"$.name" = "James";
f1
EXPLAIN SELECT * FROM t1 WHERE f1->>"$.name" = "James";
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref idx1 idx1 123 const 1 100.00 NULL
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where ((cast(json_unquote(json_extract(`f1`,_utf8mb4'$.name')) as char(30) charset utf8mb4) collate utf8mb4_bin) = 'James')
# Should return one row, and should use the index
SELECT * FROM t1 WHERE f1->>"$.name" = "james";
f1
{"name": "james"}
EXPLAIN SELECT * FROM t1 WHERE f1->>"$.name" = "james";
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref idx1 idx1 123 const 1 100.00 NULL
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where ((cast(json_unquote(json_extract(`f1`,_utf8mb4'$.name')) as char(30) charset utf8mb4) collate utf8mb4_bin) = 'james')
# Should return one row, and should use the index
SELECT * FROM t1 WHERE CAST(f1->>"$.name" AS CHAR(30)) COLLATE utf8mb4_bin = "james";
f1
{"name": "james"}
EXPLAIN SELECT * FROM t1 WHERE CAST(f1->>"$.name" AS CHAR(30)) COLLATE utf8mb4_bin = "james";
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref idx1 idx1 123 const 1 100.00 NULL
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where ((cast(json_unquote(json_extract(`f1`,_utf8mb4'$.name')) as char(30) charset utf8mb4) collate utf8mb4_bin) = 'james')
DROP TABLE t1;
# Test that doing an UPDATE on a hidden generated column returns "column
# not found".
CREATE TABLE t1 (col1 INT, INDEX idx1 ((col1 + col1)));
# Update the hidden generated column.
UPDATE t1 SET `!hidden!idx1!0!0` = 123;
ERROR 42S22: Unknown column '!hidden!idx1!0!0' in 'field list'
DROP TABLE t1;
#
# Bug#28037245 VIRTUAL COLUMN IS ALLOWED IN THE WHERE CLAUSE FOR SQL
# QUERY
#
CREATE TABLE t3 (c1 INT);
CREATE INDEX int_func_index ON t3 ((ABS(c1)));
SELECT * FROM t3 WHERE `!hidden!int_func_index!0!0`=1;
ERROR 42S22: Unknown column '!hidden!int_func_index!0!0' in 'where clause'
DROP TABLE t3;
#
# Bug#28037375 ALTER TABLE DROP OF A NORMAL COLUMN RAISES ERROR
#
CREATE TABLE t6 (c1 INT, c2 INT);
CREATE INDEX int_func_index ON t6 ((ABS(c1)));
ALTER TABLE t6 DROP COLUMN c2;
DROP TABLE t6;
# Check that a functional index on a function that returns geometry data
# isn't allowed.
CREATE TABLE t1(x VARCHAR(100), KEY ((ST_GeomFromText(x))));
ERROR HY000: Spatial functional index is not supported.
# See that we don't print out the names of the hidden generated columns,
# but rather the expression that they represent.
CREATE TABLE t1(x VARCHAR(30), INDEX idx ((CAST(x->>'$.name' AS CHAR(30)))));
INSERT INTO t1 VALUES ('{"name":"knut"}');
ANALYZE TABLE t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
EXPLAIN FORMAT=JSON SELECT * FROM t1 WHERE CAST(x->>'$.name' AS CHAR(30)) = 'knut';
EXPLAIN
{
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "0.35"
},
"table": {
"table_name": "t1",
"access_type": "ref",
"possible_keys": [
"idx"
],
"key": "idx",
"used_key_parts": [
"cast(json_unquote(json_extract(`x`,_utf8mb4'$.name')) as char(30) charset utf8mb4)"
],
"key_length": "123",
"ref": [
"const"
],
"rows_examined_per_scan": 1,
"rows_produced_per_join": 1,
"filtered": "100.00",
"cost_info": {
"read_cost": "0.25",
"eval_cost": "0.10",
"prefix_cost": "0.35",
"data_read_per_join": "248"
},
"used_columns": [
"x",
"cast(json_unquote(json_extract(`x`,_utf8mb4'$.name')) as char(30) charset utf8mb4)"
]
}
}
}
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`x` AS `x` from `test`.`t1` where (cast(json_unquote(json_extract(`x`,_utf8mb4'$.name')) as char(30) charset utf8mb4) = 'knut')
DROP TABLE t1;
# Check that we cannot drop the last visible column when we have hidden
# generated columns.
CREATE TABLE t(x INT, KEY((1+1)));
ALTER TABLE t DROP COLUMN x;
ERROR HY000: A table must have at least one visible column.
DROP TABLE t;
# See that we print out the correct error message in case of duplicate
# index names.
CREATE TABLE t (x INT);
CREATE INDEX idx ON t ((x+1));
CREATE INDEX idx ON t ((x+2));
ERROR 42000: Duplicate key name 'idx'
DROP TABLE t;
#
# Bug#28206859 SERVER CRASHED WHEN NORMAL INDEX WAS CREATED USING NEW
# FUNCTIONAL INDEX SYNTAX
#
CREATE TABLE t2(a INT, b INT, INDEX id2 ((a)));
ERROR HY000: Functional index on a column is not supported. Consider using a regular index instead.
#
# Bug#28206912 ASSERTION `TABLE_LIST->TABLE' FAILED.
#
CREATE TABLE t2(a INT, b INT, INDEX id2 ((a+b+c)));
ERROR 42S22: Unknown column 'c' in 'functional index'
#
# Bug#28216475 SIG 11 IN ITEM_FIELD::REPLACE_FIELD_PROCESSOR DURING
# CREATE TABLE
#
CREATE TABLE IF NOT EXISTS table470 ( pk INTEGER AUTO_INCREMENT, a1
VARCHAR(3) NOT NULL, b1 DATETIME NOT NULL, c1 TEXT NOT NULL, d1 TEXT NULL,
PRIMARY KEY (a1), KEY ((VALUES(d1))));
ERROR HY000: Expression of functional index 'functional_index' contains a disallowed function.
CREATE TABLE table44 ( pk INTEGER AUTO_INCREMENT, a1 TEXT NOT NULL, b1
DATETIME NOT NULL, c1 TIME NOT NULL, d1 BLOB NOT NULL, PRIMARY KEY (a1), KEY
((VALUES(d1))));
ERROR HY000: Expression of functional index 'functional_index' contains a disallowed function.
#
# Bug #28222789 SIG 11 IN BITMAP<64U>::INTERSECT DURING CREATE INDEX
#
CREATE TABLE t1 (
pk int(11) NOT NULL AUTO_INCREMENT,
col_int_nokey int(11) DEFAULT NULL,
col_int_key int(11) DEFAULT NULL,
col_date_key date DEFAULT NULL,
col_date_nokey date DEFAULT NULL,
col_time_key time DEFAULT NULL,
col_time_nokey time DEFAULT NULL,
col_datetime_key datetime DEFAULT NULL,
col_datetime_nokey datetime DEFAULT NULL,
col_varchar_key varchar(1) DEFAULT NULL,
col_varchar_nokey varchar(1) DEFAULT NULL,
PRIMARY KEY (pk),
KEY col_int_key (col_int_key),
KEY col_date_key (col_date_key),
KEY col_time_key (col_time_key),
KEY col_datetime_key (col_datetime_key),
KEY col_varchar_key (col_varchar_key,col_int_key),
KEY ind25 ((dayofmonth(col_time_nokey))),
KEY ind211 ((cast(col_date_nokey as date))),
KEY ind602 ((is_uuid(col_time_nokey)))
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_0900_ai_ci;
Warnings:
Warning 1681 Integer display width is deprecated and will be removed in a future release.
Warning 1681 Integer display width is deprecated and will be removed in a future release.
Warning 1681 Integer display width is deprecated and will be removed in a future release.
CREATE INDEX ind320 ON t1 ((pk >= col_time_nokey LIKE
ST_GeomFromGeoJSON(col_varchar_key) ));
ERROR HY000: Incorrect arguments to like
DROP TABLE t1;
#
# Bug#28218591 WRONG ERROR MESSAGE WHILE CREATING FUNCTIONAL INDEX
#
CREATE TABLE t(a INT, b INT, INDEX c((null)));
ERROR HY000: The used storage engine cannot index the expression 'NULL'.
# Blank functional index name should give a "Incorrect index name" error
CREATE TABLE t1(x INT, KEY `` ((x + 1)));
ERROR 42000: Incorrect index name ''
# Check that we don't use the functional index in case of a collation
# mismatch
CREATE TABLE t(x VARCHAR(10), KEY k ((CAST(CONCAT(x,x) AS BINARY))));
INSERT INTO t VALUES ('x');
ANALYZE TABLE t;
Table Op Msg_type Msg_text
test.t analyze status OK
EXPLAIN SELECT * FROM t WHERE CONCAT(x,x) = 'XX';
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t NULL ALL NULL NULL NULL NULL 1 100.00 Using where
Warnings:
Note 1003 /* select#1 */ select `test`.`t`.`x` AS `x` from `test`.`t` where (concat(`test`.`t`.`x`,`test`.`t`.`x`) = 'XX')
DROP TABLE t;
#
# Bug#28244585 ERROR WHILE CREATING TABLE WITH FUNCTIONAL INDEXES INSIDE
# PROCEDURES.
CREATE PROCEDURE p1() BEGIN
CREATE TABLE t(a INT,b INT,UNIQUE INDEX i((a+b)));
END//
CALL p1();
CALL p1();
ERROR 42S01: Table 't' already exists
DROP TABLE t;
CALL p1();
DROP TABLE t;
DROP PROCEDURE p1;
# Code coverage tests
CREATE TABLE t1 (
col1 FLOAT
, col2 TIMESTAMP
, col3 YEAR
, INDEX ((ABS(col1)))
, INDEX ((ADDDATE(col2, INTERVAL 2 DAY)))
, INDEX ((ABS(col3)))
);
DROP TABLE t1;
CREATE TABLE t1 (
col4 TEXT NOT NULL
, INDEX ((ST_AsBinary(col4)))
);
ERROR HY000: Cannot create a functional index on an expression that returns a BLOB or TEXT. Please consider using CAST.
# See that we get a reasonable error message when trying to remove a
# column that is a part of a functional index.
CREATE TABLE t1 (col1 INT, col2 INT, col3 INT, INDEX idx ((col1 + col2 + col3)));
ALTER TABLE t1 DROP COLUMN col2;
ERROR HY000: Column 'col2' has a functional index dependency and cannot be dropped or renamed.
ALTER TABLE t1 DROP COLUMN col3;
ERROR HY000: Column 'col3' has a functional index dependency and cannot be dropped or renamed.
ALTER TABLE t1 DROP INDEX idx;
ALTER TABLE t1 DROP COLUMN col3;
DROP TABLE t1;
# Check that INFORMATION_SCHEMA.KEY_COLUMN_USAGE doesn't reveal any
# functional indexes, since this view provides information about columns
# and a functional index does not represent a column per se.
CREATE TABLE t (
col1 INT
, UNIQUE INDEX regular_index (col1)
, UNIQUE INDEX functional_index ((ABS(col1))));
SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE table_name = "t";
CONSTRAINT_NAME
regular_index
DROP TABLE t;
#
# Bug#28526493 ASSERTION WHEN CREATING A FUNCTIONAL INDEX ON A SUBQUERY
#
CREATE TABLE t1 (x INT);
CREATE TABLE t2 (y INT, KEY (((SELECT * FROM t1))));
ERROR HY000: Expression of functional index 'functional_index' contains a disallowed function.
CREATE TABLE t2 (y INT, KEY (((SELECT x FROM t1))));
ERROR HY000: Expression of functional index 'functional_index' contains a disallowed function.
CREATE TABLE t2 (y INT, KEY (((SELECT 1 FROM t1))));
ERROR HY000: Expression of functional index 'functional_index' contains a disallowed function.
DROP TABLE t1;
#
# Bug#28643252 ASSERT FAILURE WHEN FUNCTIONAL INDEX EXPRESSION IS A LIST
#
CREATE TABLE t (j JSON, KEY k (((j,j))));
ERROR HY000: Expression of functional index 'k' cannot refer to a row value.
#
# Bug#29360763 WL#1075: FUNCTIONAL INDEX: INCORRECT ERROR MESSAGE REVEALS
# HIDDEN COLUMN NAME
#
CREATE TABLE t1 (
j1 JSON,
j3 JSON,
KEY my_idx ((CAST(j1->'$[0]' as SIGNED))),
KEY my_idx_char ((CAST(j3->'$[0]' as CHAR(10))))
);
ALTER TABLE t1 RENAME KEY my_idx_char TO my_idx;
ERROR 42000: Duplicate key name 'my_idx'
DROP TABLE t1;
#
# Bug#29317684 REPLICATION IS SENSITIVE TO ORDER OF HIDDEN COLUMNS FOR
# FUNCTIONAL INDEXES
#
CREATE TABLE t1 (col1 INT, INDEX ((col1 + col1)));
SELECT COLUMN_NAME, ORDINAL_POSITION FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = "t1";
COLUMN_NAME ORDINAL_POSITION
col1 1
ALTER TABLE t1 ADD COLUMN col2 INT;
SELECT COLUMN_NAME, ORDINAL_POSITION FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = "t1";
COLUMN_NAME ORDINAL_POSITION
col1 1
col2 2
DROP TABLE t1;
# See that INSTANT ADD COLUMN still works with this bugfix.
CREATE TABLE t1 (col1 INT, col2 INT AS (col1 + col1), INDEX (col2));
ALTER TABLE t1 ADD COLUMN new_col INT AFTER col1, ALGORITHM=INSTANT;
DROP TABLE t1;
#
# Bug#29934661 FUNCTIONAL INDEX SEEMS TO MALFUNCTION WITH UNSIGNED COLUMN
#
# The hidden column that is added should have a data type that can store
# the absolute value of an unsigned data type.
CREATE TABLE t0(c0 INT UNSIGNED, INDEX idx ((ABS(c0))));
INSERT INTO t0 (c0) VALUES (4294967294);
DROP TABLE t0;
#
# Bug#29723353: ADDING FUNCTIONAL INDEX ON JSON EXPRESSION CHANGES RESULT
#
CREATE TABLE t(j JSON);
INSERT INTO t VALUES ('{"x":"abc"}'), ('{"x":"ABC"}');
SELECT * FROM t WHERE j->>'$.x' = 'abc';
j
{"x": "abc"}
SELECT * FROM t WHERE j->'$.x' = 'abc';
j
{"x": "abc"}
CREATE INDEX idx ON t((CAST(j->>'$.x' AS CHAR(100))));
SELECT * FROM t WHERE j->>'$.x' = 'abc';
j
{"x": "abc"}
SELECT * FROM t WHERE j->'$.x' = 'abc';
j
{"x": "abc"}
ALTER TABLE t DROP INDEX idx;
CREATE INDEX idx ON t((CAST(j->'$.x' AS CHAR(100))));
SELECT * FROM t WHERE j->>'$.x' = 'abc';
j
{"x": "abc"}
SELECT * FROM t WHERE j->'$.x' = 'abc';
j
{"x": "abc"}
DROP TABLE t;
##
## Bug #31017765 CREATE TABLE ... SELECT FAILS IF THERE IS A FUNCTIONAL INDEX
##
##
CREATE TEMPORARY TABLE issue_functional_key_part (
sold_on DATETIME NOT NULL DEFAULT NOW(),
INDEX sold_on_date ((DATE(sold_on)))
)
SELECT NOW() `sold_on`;
#
# Bug#30838749: PERFORMANCE DROP WHEN CREATING A FUNCTIONAL INDEX
#
CREATE TABLE t1(x INT, KEY k ((x+1)));
CREATE TABLE t2(j JSON);
LOAD DATA INFILE 'MYSQLTEST_VARDIR/tmp/bug30838749' INTO TABLE t2
LINES STARTING BY 'EXPLAIN' TERMINATED BY 'EOL';
SELECT j->'$**.used_columns' FROM t2;
j->'$**.used_columns'
[["x"]]
DROP TABLE t1, t2;
#
# Bug#32287186: FAILED TO CREATE FUNCTION INDEX,
# COLUMN NAME IS UPPERCASE CHARACTER
#
# Verify that normal index creation is case-insensitive on
# column name
CREATE TABLE t1 (id INT, name VARCHAR(50), INDEX (NAME));
CREATE TABLE t2 (id INT, name VARCHAR(50), INDEX (name));
CREATE TABLE t3 (id INT, NAME VARCHAR(50), INDEX (name));
CREATE TABLE t4 (id INT, NAME VARCHAR(50), INDEX (NAME));
# Now verify that functional index creation is case-insensitive
# on column name as well
CREATE TABLE t5 (id INT, name VARCHAR(50),
INDEX ((SUBSTR(name, 1, 2))));
CREATE TABLE t6 (id INT, name VARCHAR(50),
INDEX ((SUBSTR(NAME, 1, 2))));
CREATE TABLE t7 (id INT, NAME VARCHAR(50),
INDEX ((SUBSTR(name, 1, 2))));
CREATE TABLE t8 (id INT, NAME VARCHAR(50),
INDEX ((SUBSTR(NAME, 1, 2))));
# Cleanup
DROP TABLE t1;
DROP TABLE t2;
DROP TABLE t3;
DROP TABLE t4;
DROP TABLE t5;
DROP TABLE t6;
DROP TABLE t7;
DROP TABLE t8;
#
# Bug#33851055: Correlated subquery does not use functional index
#
CREATE TABLE t (
id INT DEFAULT NULL,
KEY functional_index ((id * id))
);
CREATE TABLE t2 (
id INT NOT NULL AUTO_INCREMENT,
val INT,
PRIMARY KEY (id)
);
INSERT INTO t(id) VALUES (1), (1), (2), (-1), (-2);
INSERT INTO t2(val) VALUES (1), (2), (3), (4);
ANALYZE TABLE t,t2;
Table Op Msg_type Msg_text
test.t analyze status OK
test.t2 analyze status OK
EXPLAIN SELECT id, val, (SELECT id FROM t WHERE (id * id) = val LIMIT 1) AS tid FROM t2;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 NULL ALL NULL NULL NULL NULL 4 100.00 NULL
2 DEPENDENT SUBQUERY t NULL ref functional_index functional_index 9 test.t2.val 2 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t2.val' of SELECT #2 was resolved in SELECT #1
Note 1003 /* select#1 */ select `test`.`t2`.`id` AS `id`,`test`.`t2`.`val` AS `val`,(/* select#2 */ select `test`.`t`.`id` from `test`.`t` where ((`id` * `id`) = `test`.`t2`.`val`) limit 1) AS `tid` from `test`.`t2`
SELECT id, val, (SELECT id FROM t WHERE (id * id) = val LIMIT 1) AS tid FROM t2;
id val tid
1 1 1
2 2 NULL
3 3 NULL
4 4 2
EXPLAIN SELECT id, val, (SELECT id FROM t WHERE (id * id) BETWEEN val and val + 2 LIMIT 1) AS tid FROM t2;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 NULL ALL NULL NULL NULL NULL 4 100.00 NULL
2 DEPENDENT SUBQUERY t NULL ALL functional_index NULL NULL NULL 5 20.00 Range checked for each record (index map: 0x1)
Warnings:
Note 1276 Field or reference 'test.t2.val' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.t2.val' of SELECT #2 was resolved in SELECT #1
Note 1003 /* select#1 */ select `test`.`t2`.`id` AS `id`,`test`.`t2`.`val` AS `val`,(/* select#2 */ select `test`.`t`.`id` from `test`.`t` where ((`id` * `id`) between `test`.`t2`.`val` and (`test`.`t2`.`val` + 2)) limit 1) AS `tid` from `test`.`t2`
SELECT id, val, (SELECT id FROM t WHERE (id * id) BETWEEN val and val + 2 LIMIT 1) AS tid FROM t2;
id val tid
1 1 1
2 2 2
3 3 2
4 4 2
EXPLAIN SELECT id, val, (SELECT id FROM t WHERE (id * id) IN (val) LIMIT 1) AS tid FROM t2;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 NULL ALL NULL NULL NULL NULL 4 100.00 NULL
2 DEPENDENT SUBQUERY t NULL ref functional_index functional_index 9 test.t2.val 2 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t2.val' of SELECT #2 was resolved in SELECT #1
Note 1003 /* select#1 */ select `test`.`t2`.`id` AS `id`,`test`.`t2`.`val` AS `val`,(/* select#2 */ select `test`.`t`.`id` from `test`.`t` where ((`id` * `id`) = `test`.`t2`.`val`) limit 1) AS `tid` from `test`.`t2`
SELECT id, val, (SELECT id FROM t WHERE (id * id) IN (val) LIMIT 1) AS tid FROM t2;
id val tid
1 1 1
2 2 NULL
3 3 NULL
4 4 2
EXPLAIN SELECT id, val, (SELECT id FROM t WHERE (id * id) = val + (SELECT 1 FROM t WHERE id > 1) LIMIT 1) AS tid FROM t2;
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 NULL ALL NULL NULL NULL NULL 4 100.00 NULL
2 DEPENDENT SUBQUERY t NULL ref functional_index functional_index 9 func 2 100.00 Using where
3 SUBQUERY t NULL ALL NULL NULL NULL NULL 5 33.33 Using where
Warnings:
Note 1276 Field or reference 'test.t2.val' of SELECT #2 was resolved in SELECT #1
Note 1003 /* select#1 */ select `test`.`t2`.`id` AS `id`,`test`.`t2`.`val` AS `val`,(/* select#2 */ select `test`.`t`.`id` from `test`.`t` where ((`id` * `id`) = (`test`.`t2`.`val` + (/* select#3 */ select 1 from `test`.`t` where (`test`.`t`.`id` > 1)))) limit 1) AS `tid` from `test`.`t2`
SELECT id, val, (SELECT id FROM t WHERE (id * id) = val + (SELECT 1 FROM t WHERE id > 1) LIMIT 1) AS tid FROM t2;
id val tid
1 1 NULL
2 2 NULL
3 3 2
4 4 NULL
DROP TABLE t, t2;
#
# Bug#35352161: Mysql dies when a functional index on FIND_IN_SET is added
#
# Create the table without INDEXes first.
CREATE TABLE t1 (
id INT NOT NULL AUTO_INCREMENT,
roles SET('Admin', 'Moderator', 'Editor') NOT NULL,
PRIMARY KEY (id)
);
# Fill the table.
INSERT INTO t1 VALUES (NULL, "Admin");
INSERT INTO t1 VALUES (NULL, "Moderator");
INSERT INTO t1 VALUES (NULL, "Editor");
INSERT INTO t1 VALUES (NULL, "Editor,Admin");
# Show operation without index.
SHOW INDEXES FROM t1 WHERE key_name="i1";
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression
EXPLAIN FORMAT=tree SELECT id FROM t1 WHERE FIND_IN_SET('Admin', roles)>0;
EXPLAIN
-> Filter: (find_in_set('Admin',t1.roles) > 0) (rows=4)
-> Table scan on t1 (rows=4)
SELECT id FROM t1 WHERE FIND_IN_SET('Admin', roles)>0;
id
1
4
# ADD function INDEX: If Bug#35352161 is present, this will fail.
ALTER TABLE t1 ADD INDEX i1 ((FIND_IN_SET('Admin', roles)));
# Show operation using functional index i1.
# (Prove index exists, we're using it, and it gives correct results.)
SHOW INDEXES FROM t1 WHERE key_name="i1";
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression
t1 1 i1 1 NULL A 2 NULL NULL BTREE YES find_in_set(_utf8mb4\'Admin\',`roles`)
EXPLAIN FORMAT=tree SELECT id FROM t1 WHERE FIND_IN_SET('Admin', roles)>0;
EXPLAIN
-> Filter: (find_in_set(_utf8mb4'Admin',roles) > 0) (rows=2)
-> Index range scan on t1 using i1 over (0 < find_in_set(_utf8mb4'Admin',`roles`)) (rows=2)
SELECT id FROM t1 WHERE FIND_IN_SET('Admin', roles)>0;
id
1
4
# Clean up.
DROP TABLE t1;
# Create table including index.
# If Bug#35352161 is present, the INDEX part will fail.
CREATE TABLE t1 (
id INT NOT NULL AUTO_INCREMENT,
roles SET('Admin', 'Moderator', 'Editor') NOT NULL,
PRIMARY KEY (id),
INDEX i1((FIND_IN_SET('Admin', roles)))
);
# Clean up.
DROP TABLE t1;
#
# Bug#35497623: SHOW INDEXES with generated column may access wrong key-part
#
# SHOW INDEXES on regular table:
CREATE TABLE t1 (c1 INT);
CREATE TABLE t2
(UNIQUE i3 TYPE BTREE (c1, (c1 REGEXP c1))) TABLE t1;
SHOW INDEXES IN t2;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression
t2 0 i3 1 c1 A 0 NULL NULL YES BTREE YES NULL
t2 0 i3 2 NULL A 0 NULL NULL YES BTREE YES regexp_like(`c1`,`c1`)
/* regular table */
DROP TABLE t2;
DROP TABLE t1;
# SHOW INDEXES on temporary table (different code path!):
CREATE TABLE t1 (c1 INT);
CREATE TEMPORARY TABLE t2
(UNIQUE i3 TYPE BTREE (c1, (c1 REGEXP c1))) TABLE t1;
SHOW INDEXES IN t2;
Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type Comment Index_comment Visible Expression
t2 0 i3 1 c1 A 0 NULL NULL YES BTREE YES NULL
t2 0 i3 2 NULL A 0 NULL NULL YES BTREE YES regexp_like(`c1`,`c1`)
/* temporary table */
DROP TABLE t2;
DROP TABLE t1;
|