Skip to content

Fix multi-valued index #360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions mysql-test/r/hash_join.result
Original file line number Diff line number Diff line change
Expand Up @@ -812,11 +812,11 @@ test.t2 analyze status OK
EXPLAIN FORMAT=tree SELECT t1.col_int_key AS field1, t2.col_int AS field2 FROM t2
JOIN t1 ON 1 WHERE (CAST("1" AS JSON) MEMBER OF( t1.col_json->'$[*]'));
EXPLAIN
-> Inner hash join (no condition) (cost=*** rows=5)
-> Inner hash join (no condition) (cost=*** rows=10)
-> Table scan on t2 (cost=*** rows=5)
-> Hash
-> Filter: json'"1"' member of (cast(json_extract(col_json,_utf8mb4'$[*]') as char(40) array)) (cost=*** rows=1)
-> Index lookup on t1 using mv_idx (cast(json_extract(col_json,_utf8mb4'$[*]') as char(40) array)=json'"1"') (cost=*** rows=1)
-> Filter: json'"1"' member of (cast(json_extract(col_json,_utf8mb4'$[*]') as char(40) array)) (cost=*** rows=2)
-> Index lookup on t1 using mv_idx (cast(json_extract(col_json,_utf8mb4'$[*]') as char(40) array)=json'"1"') (cost=*** rows=2)

SELECT t1.col_int_key AS field1, t2.col_int AS field2 FROM t2
JOIN t1 ON 1 WHERE (CAST("1" AS JSON) MEMBER OF( t1.col_json->'$[*]'));
Expand Down
26 changes: 13 additions & 13 deletions mysql-test/suite/json/r/array_index.result
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ id f1
18 [8, 4, 3, 5]
explain select * from t1 where 5 member of (f1->"$[*]");
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref i1 i1 9 const 1 100.00 Using where
1 SIMPLE t1 NULL ref i1 i1 9 const 5 100.00 Using where
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`id` AS `id`,`test`.`t1`.`f1` AS `f1` from `test`.`t1` where json'5' member of (cast(json_extract(`f1`,_utf8mb4'$[*]') as unsigned array))
select * from t1 where f1->"$[0]" member of ('[1,3,9]');
Expand Down Expand Up @@ -679,7 +679,7 @@ id f1
8 [8, 5]
explain select * from t1 where 5 member of (f1->"$");
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref i1 i1 9 const 1 100.00 Using where
1 SIMPLE t1 NULL ref i1 i1 9 const 2 100.00 Using where
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`id` AS `id`,`test`.`t1`.`f1` AS `f1` from `test`.`t1` where json'5' member of (cast(json_extract(`f1`,_utf8mb4'$') as signed array))
select * from t1 force index(i1) where 99 member of (f1->"$[*]");
Expand Down Expand Up @@ -727,7 +727,7 @@ f1
["gfd", "qwe"]
explain select * from t1 where "qwe" member of (f1->"$[*]");
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref i1 i1 43 const 1 100.00 Using where
1 SIMPLE t1 NULL ref i1 i1 43 const 2 100.00 Using where
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where json'"qwe"' member of (cast(json_extract(`f1`,_utf8mb4'$[*]') as char(10) array))
select * from t1 force index(i1) where "bnm" member of (f1->"$[*]");
Expand Down Expand Up @@ -775,7 +775,7 @@ f1
["gfd", "qwe"]
explain select * from t1 where "qwe" member of (f1->"$[*]");
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref i1 i1 13 const 1 100.00 Using where
1 SIMPLE t1 NULL ref i1 i1 13 const 2 100.00 Using where
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where json'"base64:type15:cXdl"' member of (cast(json_extract(`f1`,_utf8mb4'$[*]') as binary(10) array))
select * from t1 force index(i1) where "bnm" member of (f1->"$[*]");
Expand Down Expand Up @@ -840,7 +840,7 @@ id f1
8 [-1.330, 5, 1.3300]
explain select * from t1 where 1.33 member of (f1->"$");
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref i1 i1 6 const 1 100.00 Using where
1 SIMPLE t1 NULL ref i1 i1 6 const 2 100.00 Using where
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`id` AS `id`,`test`.`t1`.`f1` AS `f1` from `test`.`t1` where json'1.3300' member of (cast(json_extract(`f1`,_utf8mb4'$') as decimal(10, 4) array))
select * from t1 force index(i1) where 99 member of (f1->"$[*]");
Expand Down Expand Up @@ -908,7 +908,7 @@ id f1
7 ["01-02-03", "22.11.17"]
explain select * from t1 where cast('01-02-03' as date) member of (f1->"$");
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref i1 i1 4 const 1 100.00 Using where
1 SIMPLE t1 NULL ref i1 i1 4 const 4 100.00 Using where
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`id` AS `id`,`test`.`t1`.`f1` AS `f1` from `test`.`t1` where json'"2001-02-03"' member of (cast(json_extract(`f1`,_utf8mb4'$') as date array))
select * from t1 force index(i1) where cast('01-01-12' as date) member of (f1->"$");
Expand Down Expand Up @@ -981,7 +981,7 @@ id f1
7 ["01:02:03", "22:11:17"]
explain select * from t1 where cast('01:02:03' as time) member of (f1->"$");
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref i1 i1 4 const 1 100.00 Using where
1 SIMPLE t1 NULL ref i1 i1 4 const 4 100.00 Using where
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`id` AS `id`,`test`.`t1`.`f1` AS `f1` from `test`.`t1` where json'"01:02:03.000000"' member of (cast(json_extract(`f1`,_utf8mb4'$') as time array))
select * from t1 force index(i1) where
Expand Down Expand Up @@ -1056,7 +1056,7 @@ id f1
explain select * from t1 where cast('01-01-01 01:02:03' as datetime) member
of (f1->"$");
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref i1 i1 6 const 1 100.00 Using where
1 SIMPLE t1 NULL ref i1 i1 6 const 4 100.00 Using where
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`id` AS `id`,`test`.`t1`.`f1` AS `f1` from `test`.`t1` where json'"2001-01-01 01:02:03.000000"' member of (cast(json_extract(`f1`,_utf8mb4'$') as datetime array))
select * from t1 force index(i1) where
Expand Down Expand Up @@ -1955,7 +1955,7 @@ test.t2 analyze status OK
EXPLAIN SELECT t1.col_int_key AS field1, t2.col_int AS field2 FROM t2 LEFT
JOIN t1 ON 1 WHERE (CAST("1" AS JSON) MEMBER OF( t1.col_jsonn->'$[*]'));
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref mv_idx mv_idx 163 const 1 100.00 Using where
1 SIMPLE t1 NULL ref mv_idx mv_idx 163 const 2 100.00 Using where
1 SIMPLE t2 NULL ALL NULL NULL NULL NULL 5 100.00 Using join buffer (hash join)
Warnings:
Note 1003 /* select#1 */ select `test`.`t1`.`col_int_key` AS `field1`,`test`.`t2`.`col_int` AS `field2` from `test`.`t2` join `test`.`t1` where json'"1"' member of (cast(json_extract(`col_jsonn`,_utf8mb4'$[*]') as char(40) array))
Expand Down Expand Up @@ -2416,8 +2416,8 @@ vc j
[2,3,4] [2, 3, 4]
EXPLAIN FORMAT=TREE SELECT * FROM t WHERE 3 MEMBER OF (j);
EXPLAIN
-> Filter: json'3' member of (cast(j as unsigned array)) (cost=0.35 rows=1)
-> Index lookup on t using j_idx (cast(j as unsigned array)=json'3') (cost=0.35 rows=1)
-> Filter: json'3' member of (cast(j as unsigned array)) (cost=0.80 rows=3)
-> Index lookup on t using j_idx (cast(j as unsigned array)=json'3') (cost=0.80 rows=3)

SELECT * FROM t WHERE 3 MEMBER OF (j);
vc j
Expand Down Expand Up @@ -2446,8 +2446,8 @@ vc j
[2,3,4] [2, 3, 4]
EXPLAIN FORMAT=TREE SELECT * FROM t WHERE 3 MEMBER OF (vc);
EXPLAIN
-> Filter: json'3' member of (cast(vc as unsigned array)) (cost=0.35 rows=1)
-> Index lookup on t using vc_idx (cast(vc as unsigned array)=json'3') (cost=0.35 rows=1)
-> Filter: json'3' member of (cast(vc as unsigned array)) (cost=0.80 rows=3)
-> Index lookup on t using vc_idx (cast(vc as unsigned array)=json'3') (cost=0.80 rows=3)

SELECT * FROM t WHERE 3 MEMBER OF (vc);
vc j
Expand Down
152 changes: 152 additions & 0 deletions mysql-test/suite/json/r/multi-valued-index-in-and.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
DROP TABLE IF EXISTS t1;
DROP VIEW IF EXISTS v1;
CREATE TABLE t1 (
f1 VARCHAR(50) NOT NULL PRIMARY KEY,
f2 JSON NOT NULL,
INDEX idx2 ( (CAST(f2 AS CHAR(50) ARRAY)) )
);
CREATE VIEW v1 AS
SELECT * FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50) PATH '$')) AS ids;
INSERT INTO t1 VALUES ('foo', '["aa", "bb"]');
INSERT INTO t1 VALUES ('bar', '["xx", "yy"]');
ANALYZE TABLE t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
SELECT * FROM t1;
f1 f2
bar ["xx", "yy"]
foo ["aa", "bb"]
EXPLAIN SELECT * FROM t1 WHERE 'xx' member of (f2) AND 'yy' member of(f2);
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref idx2 idx2 203 const 1 100.00 Using where
EXPLAIN SELECT * FROM t1 WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"');
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL range idx2 idx2 203 NULL 1 100.00 Using where
EXPLAIN SELECT * FROM t1 WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]');
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL range idx2 idx2 203 NULL 2 100.00 Using where
EXPLAIN SELECT * FROM v1 WHERE 'xx' member of (f2) AND 'yy' member of(f2);
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref idx2 idx2 203 const 1 100.00 Using where
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
EXPLAIN SELECT * FROM v1 WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"');
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL range idx2 idx2 203 NULL 1 100.00 Using where
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
EXPLAIN SELECT * FROM v1 WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]');
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL range idx2 idx2 203 NULL 2 100.00 Using where
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
EXPLAIN SELECT * FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50) PATH '$')) AS ids WHERE 'xx' member of (f2) AND 'yy' member of(f2);
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ref idx2 idx2 203 const 1 100.00 Using where
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
EXPLAIN SELECT * FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50) PATH '$')) AS ids WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"');
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL range idx2 idx2 203 NULL 1 100.00 Using where
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
EXPLAIN SELECT * FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50) PATH '$')) AS ids WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]');
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL range idx2 idx2 203 NULL 2 100.00 Using where
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
EXPLAIN SELECT * FROM t1 WHERE 'xx' member of (f2) AND 'yy' member of(f2) AND f1 = 'bar';
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL const PRIMARY,idx2 PRIMARY 202 const 1 100.00 NULL
EXPLAIN SELECT * FROM t1 WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"') AND f1 = 'bar';
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL const PRIMARY,idx2 PRIMARY 202 const 1 100.00 NULL
EXPLAIN SELECT * FROM t1 WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]') AND f1 = 'bar';
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL const PRIMARY,idx2 PRIMARY 202 const 1 100.00 NULL
EXPLAIN SELECT * FROM v1 WHERE 'xx' member of (f2) AND 'yy' member of(f2) AND f1 = 'bar';
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL const PRIMARY,idx2 PRIMARY 202 const 1 100.00 NULL
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
EXPLAIN SELECT * FROM v1 WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"') AND f1 = 'bar';
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL const PRIMARY,idx2 PRIMARY 202 const 1 100.00 NULL
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
EXPLAIN SELECT * FROM v1 WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]') AND f1 = 'bar';
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL const PRIMARY,idx2 PRIMARY 202 const 1 100.00 NULL
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
EXPLAIN SELECT * FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50) PATH '$')) AS ids WHERE 'xx' member of (f2) AND 'yy' member of(f2) AND f1 = 'bar';
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL const PRIMARY,idx2 PRIMARY 202 const 1 100.00 NULL
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
EXPLAIN SELECT * FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50) PATH '$')) AS ids WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"') AND f1 = 'bar';
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL const PRIMARY,idx2 PRIMARY 202 const 1 100.00 NULL
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
EXPLAIN SELECT * FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50) PATH '$')) AS ids WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]') AND f1 = 'bar';
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL const PRIMARY,idx2 PRIMARY 202 const 1 100.00 NULL
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
SELECT * FROM t1 WHERE 'xx' member of (f2) AND 'yy' member of(f2);
f1 f2
bar ["xx", "yy"]
SELECT * FROM t1 WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"');
f1 f2
bar ["xx", "yy"]
SELECT * FROM t1 WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]');
f1 f2
bar ["xx", "yy"]
SELECT * FROM v1 WHERE 'xx' member of (f2) AND 'yy' member of(f2);
f1 f2 i id
bar ["xx", "yy"] 1 xx
bar ["xx", "yy"] 2 yy
SELECT * FROM v1 WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"');
f1 f2 i id
bar ["xx", "yy"] 1 xx
bar ["xx", "yy"] 2 yy
SELECT * FROM v1 WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]');
f1 f2 i id
bar ["xx", "yy"] 1 xx
bar ["xx", "yy"] 2 yy
SELECT * FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50) PATH '$')) AS ids WHERE 'xx' member of (f2) AND 'yy' member of(f2);
f1 f2 i id
bar ["xx", "yy"] 1 xx
bar ["xx", "yy"] 2 yy
SELECT * FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50) PATH '$')) AS ids WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"');
f1 f2 i id
bar ["xx", "yy"] 1 xx
bar ["xx", "yy"] 2 yy
SELECT * FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50) PATH '$')) AS ids WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]');
f1 f2 i id
bar ["xx", "yy"] 1 xx
bar ["xx", "yy"] 2 yy
SELECT * FROM t1 WHERE 'xx' member of (f2) AND 'yy' member of(f2) AND f1 = 'bar';
f1 f2
bar ["xx", "yy"]
SELECT * FROM t1 WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"') AND f1 = 'bar';
f1 f2
bar ["xx", "yy"]
SELECT * FROM t1 WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]') AND f1 = 'bar';
f1 f2
bar ["xx", "yy"]
SELECT * FROM v1 WHERE 'xx' member of (f2) AND 'yy' member of(f2) AND f1 = 'bar';
f1 f2 i id
bar ["xx", "yy"] 1 xx
bar ["xx", "yy"] 2 yy
SELECT * FROM v1 WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"') AND f1 = 'bar';
f1 f2 i id
bar ["xx", "yy"] 1 xx
bar ["xx", "yy"] 2 yy
SELECT * FROM v1 WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]') AND f1 = 'bar';
f1 f2 i id
bar ["xx", "yy"] 1 xx
bar ["xx", "yy"] 2 yy
SELECT * FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50) PATH '$')) AS ids WHERE 'xx' member of (f2) AND 'yy' member of(f2) AND f1 = 'bar';
f1 f2 i id
bar ["xx", "yy"] 1 xx
bar ["xx", "yy"] 2 yy
SELECT * FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50) PATH '$')) AS ids WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"') AND f1 = 'bar';
f1 f2 i id
bar ["xx", "yy"] 1 xx
bar ["xx", "yy"] 2 yy
SELECT * FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50) PATH '$')) AS ids WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]') AND f1 = 'bar';
f1 f2 i id
bar ["xx", "yy"] 1 xx
bar ["xx", "yy"] 2 yy
DROP TABLE t1;
DROP VIEW v1;
Loading