You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Patch #4: Wrong results with AND of JSON_CONTAINS and multi-valued indexes
For a multi-valued index on json array field f=[1, 2], this statement
wrongly returned empty result set:
SELECT * FROM f WHERE JSON_CONTAINS(f, 1) AND JSON_CONTAINS(f, 2);
The cause is that get_func_mm_tree() converts two JSON_CONTAINS() to
`f_idx = 1 AND f_idx = 2` which is always false for single value index
but possible for multi-valued index.
Fixed by anding the two key ranges, rather than marking the condition
as always false.
This is a contribution by Yubao Liu.
Change-Id: I535fc6ce8755f4f3b6e8cbd77b4c0ee4aa685cae
Copy file name to clipboardExpand all lines: mysql-test/suite/json/r/array_index.result
+264Lines changed: 264 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -2721,6 +2721,270 @@ EXECUTE stmt USING @a;
2721
2721
f1 f2 i id
2722
2722
bar ["xx", "yy"] 1 xx
2723
2723
bar ["xx", "yy"] 2 yy
2724
+
EXPLAIN SELECT * FROM t1 WHERE 'xx' MEMBER OF (f2) AND 'yy' MEMBER OF (f2);
2725
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
2726
+
1 SIMPLE t1 NULL ref idx2 idx2 203 const 1 100.00 Using where
2727
+
Warnings:
2728
+
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2` from `test`.`t1` where (json'"xx"' member of (cast(`f2` as char(50) array)) and json'"yy"' member of (cast(`f2` as char(50) array)))
2729
+
EXPLAIN
2730
+
SELECT * FROM t1 WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"');
2731
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
2732
+
1 SIMPLE t1 NULL range idx2 idx2 203 NULL 1 100.00 Using where; Using MRR
2733
+
Warnings:
2734
+
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2` from `test`.`t1` where (json_contains(cast(`f2` as char(50) array),json'["xx"]') and json_contains(cast(`f2` as char(50) array),json'["yy"]'))
2735
+
EXPLAIN
2736
+
SELECT *
2737
+
FROM t1
2738
+
WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]');
2739
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
2740
+
1 SIMPLE t1 NULL range idx2 idx2 203 NULL 2 100.00 Using where; Using MRR
2741
+
Warnings:
2742
+
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2` from `test`.`t1` where (json_overlaps(cast(`f2` as char(50) array),json'["xx", "zz"]') and json_overlaps(cast(`f2` as char(50) array),json'["yy", "zz"]'))
2743
+
EXPLAIN SELECT * FROM v1 WHERE 'xx' MEMBER OF (f2) AND 'yy' MEMBER OF (f2);
2744
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
2745
+
1 SIMPLE t1 NULL ref idx2 idx2 203 const 1 100.00 Using where
2746
+
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
2747
+
Warnings:
2748
+
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2`,`ids`.`i` AS `i`,`ids`.`id` AS `id` from `test`.`t1` join json_table(`test`.`t1`.`f2`, '$[*]' columns (`i` for ordinality, `id` varchar(50) character set utf8mb4 path '$')) `ids` where (json'"xx"' member of (cast(`f2` as char(50) array)) and json'"yy"' member of (cast(`f2` as char(50) array)))
2749
+
EXPLAIN
2750
+
SELECT * FROM v1 WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"');
2751
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
2752
+
1 SIMPLE t1 NULL range idx2 idx2 203 NULL 1 100.00 Using where; Using MRR
2753
+
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
2754
+
Warnings:
2755
+
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2`,`ids`.`i` AS `i`,`ids`.`id` AS `id` from `test`.`t1` join json_table(`test`.`t1`.`f2`, '$[*]' columns (`i` for ordinality, `id` varchar(50) character set utf8mb4 path '$')) `ids` where (json_contains(cast(`f2` as char(50) array),json'["xx"]') and json_contains(cast(`f2` as char(50) array),json'["yy"]'))
2756
+
EXPLAIN
2757
+
SELECT *
2758
+
FROM v1
2759
+
WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]');
2760
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
2761
+
1 SIMPLE t1 NULL range idx2 idx2 203 NULL 2 100.00 Using where; Using MRR
2762
+
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
2763
+
Warnings:
2764
+
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2`,`ids`.`i` AS `i`,`ids`.`id` AS `id` from `test`.`t1` join json_table(`test`.`t1`.`f2`, '$[*]' columns (`i` for ordinality, `id` varchar(50) character set utf8mb4 path '$')) `ids` where (json_overlaps(cast(`f2` as char(50) array),json'["xx", "zz"]') and json_overlaps(cast(`f2` as char(50) array),json'["yy", "zz"]'))
2765
+
EXPLAIN
2766
+
SELECT *
2767
+
FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50)
2768
+
PATH '$')) AS ids
2769
+
WHERE 'xx' MEMBER OF (f2) AND 'yy' MEMBER OF (f2);
2770
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
2771
+
1 SIMPLE t1 NULL ref idx2 idx2 203 const 1 100.00 Using where
2772
+
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
2773
+
Warnings:
2774
+
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2`,`ids`.`i` AS `i`,`ids`.`id` AS `id` from `test`.`t1` join json_table(`test`.`t1`.`f2`, '$[*]' columns (`i` for ordinality, `id` varchar(50) character set utf8mb4 path '$')) `ids` where (json'"xx"' member of (cast(`f2` as char(50) array)) and json'"yy"' member of (cast(`f2` as char(50) array)))
2775
+
EXPLAIN
2776
+
SELECT *
2777
+
FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50)
2778
+
PATH '$')) AS ids
2779
+
WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"');
2780
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
2781
+
1 SIMPLE t1 NULL range idx2 idx2 203 NULL 1 100.00 Using where; Using MRR
2782
+
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
2783
+
Warnings:
2784
+
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2`,`ids`.`i` AS `i`,`ids`.`id` AS `id` from `test`.`t1` join json_table(`test`.`t1`.`f2`, '$[*]' columns (`i` for ordinality, `id` varchar(50) character set utf8mb4 path '$')) `ids` where (json_contains(cast(`f2` as char(50) array),json'["xx"]') and json_contains(cast(`f2` as char(50) array),json'["yy"]'))
2785
+
EXPLAIN
2786
+
SELECT *
2787
+
FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50)
2788
+
PATH '$')) AS ids
2789
+
WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]');
2790
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
2791
+
1 SIMPLE t1 NULL range idx2 idx2 203 NULL 2 100.00 Using where; Using MRR
2792
+
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
2793
+
Warnings:
2794
+
Note 1003 /* select#1 */ select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2`,`ids`.`i` AS `i`,`ids`.`id` AS `id` from `test`.`t1` join json_table(`test`.`t1`.`f2`, '$[*]' columns (`i` for ordinality, `id` varchar(50) character set utf8mb4 path '$')) `ids` where (json_overlaps(cast(`f2` as char(50) array),json'["xx", "zz"]') and json_overlaps(cast(`f2` as char(50) array),json'["yy", "zz"]'))
2795
+
EXPLAIN
2796
+
SELECT *
2797
+
FROM t1
2798
+
WHERE 'xx' MEMBER OF (f2) AND 'yy' MEMBER OF(f2) AND f1 = 'bar';
2799
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
2828
+
Warnings:
2829
+
Note 1003 /* select#1 */ select 'bar' AS `f1`,'["xx", "yy"]' AS `f2`,`ids`.`i` AS `i`,`ids`.`id` AS `id` from `test`.`t1` join json_table('["xx", "yy"]', '$[*]' columns (`i` for ordinality, `id` varchar(50) character set utf8mb4 path '$')) `ids` where (json'"xx"' member of (cast('["xx", "yy"]' as char(50) array)) and json'"yy"' member of (cast('["xx", "yy"]' as char(50) array)))
2830
+
EXPLAIN
2831
+
SELECT *
2832
+
FROM v1
2833
+
WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"') AND f1 = 'bar';
2834
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
2837
+
Warnings:
2838
+
Note 1003 /* select#1 */ select 'bar' AS `f1`,'["xx", "yy"]' AS `f2`,`ids`.`i` AS `i`,`ids`.`id` AS `id` from `test`.`t1` join json_table('["xx", "yy"]', '$[*]' columns (`i` for ordinality, `id` varchar(50) character set utf8mb4 path '$')) `ids` where (json_contains(cast('["xx", "yy"]' as char(50) array),json'["xx"]') and json_contains(cast('["xx", "yy"]' as char(50) array),json'["yy"]'))
2839
+
EXPLAIN
2840
+
SELECT *
2841
+
FROM v1
2842
+
WHERE json_overlaps(f2, '["xx", "zz"]') AND
2843
+
json_overlaps(f2, '["yy", "zz"]') AND
2844
+
f1 = 'bar';
2845
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
2848
+
Warnings:
2849
+
Note 1003 /* select#1 */ select 'bar' AS `f1`,'["xx", "yy"]' AS `f2`,`ids`.`i` AS `i`,`ids`.`id` AS `id` from `test`.`t1` join json_table('["xx", "yy"]', '$[*]' columns (`i` for ordinality, `id` varchar(50) character set utf8mb4 path '$')) `ids` where (json_overlaps(cast('["xx", "yy"]' as char(50) array),json'["xx", "zz"]') and json_overlaps(cast('["xx", "yy"]' as char(50) array),json'["yy", "zz"]'))
2850
+
EXPLAIN
2851
+
SELECT *
2852
+
FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50)
2853
+
PATH '$')) AS ids
2854
+
WHERE 'xx' MEMBER OF (f2) AND 'yy' MEMBER OF (f2) AND f1 = 'bar';
2855
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
2858
+
Warnings:
2859
+
Note 1003 /* select#1 */ select 'bar' AS `f1`,'["xx", "yy"]' AS `f2`,`ids`.`i` AS `i`,`ids`.`id` AS `id` from `test`.`t1` join json_table('["xx", "yy"]', '$[*]' columns (`i` for ordinality, `id` varchar(50) character set utf8mb4 path '$')) `ids` where (json'"xx"' member of (cast('["xx", "yy"]' as char(50) array)) and json'"yy"' member of (cast('["xx", "yy"]' as char(50) array)))
2860
+
EXPLAIN
2861
+
SELECT *
2862
+
FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50)
2863
+
PATH '$')) AS ids
2864
+
WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"') AND f1 = 'bar';
2865
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
2868
+
Warnings:
2869
+
Note 1003 /* select#1 */ select 'bar' AS `f1`,'["xx", "yy"]' AS `f2`,`ids`.`i` AS `i`,`ids`.`id` AS `id` from `test`.`t1` join json_table('["xx", "yy"]', '$[*]' columns (`i` for ordinality, `id` varchar(50) character set utf8mb4 path '$')) `ids` where (json_contains(cast('["xx", "yy"]' as char(50) array),json'["xx"]') and json_contains(cast('["xx", "yy"]' as char(50) array),json'["yy"]'))
2870
+
EXPLAIN
2871
+
SELECT *
2872
+
FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50)
2873
+
PATH '$')) AS ids
2874
+
WHERE json_overlaps(f2, '["xx", "zz"]') AND
2875
+
json_overlaps(f2, '["yy", "zz"]') AND
2876
+
f1 = 'bar';
2877
+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE ids NULL ALL NULL NULL NULL NULL 2 100.00 Table function: json_table; Using temporary
2880
+
Warnings:
2881
+
Note 1003 /* select#1 */ select 'bar' AS `f1`,'["xx", "yy"]' AS `f2`,`ids`.`i` AS `i`,`ids`.`id` AS `id` from `test`.`t1` join json_table('["xx", "yy"]', '$[*]' columns (`i` for ordinality, `id` varchar(50) character set utf8mb4 path '$')) `ids` where (json_overlaps(cast('["xx", "yy"]' as char(50) array),json'["xx", "zz"]') and json_overlaps(cast('["xx", "yy"]' as char(50) array),json'["yy", "zz"]'))
2882
+
SELECT * FROM t1 WHERE 'xx' MEMBER OF (f2) AND 'yy' MEMBER OF (f2);
2883
+
f1 f2
2884
+
bar ["xx", "yy"]
2885
+
SELECT * FROM t1 WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"');
2886
+
f1 f2
2887
+
bar ["xx", "yy"]
2888
+
SELECT *
2889
+
FROM t1
2890
+
WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]');
2891
+
f1 f2
2892
+
bar ["xx", "yy"]
2893
+
SELECT * FROM v1 WHERE 'xx' MEMBER OF (f2) AND 'yy' MEMBER OF (f2);
2894
+
f1 f2 i id
2895
+
bar ["xx", "yy"] 1 xx
2896
+
bar ["xx", "yy"] 2 yy
2897
+
SELECT * FROM v1 WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"');
2898
+
f1 f2 i id
2899
+
bar ["xx", "yy"] 1 xx
2900
+
bar ["xx", "yy"] 2 yy
2901
+
SELECT *
2902
+
FROM v1
2903
+
WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]');
2904
+
f1 f2 i id
2905
+
bar ["xx", "yy"] 1 xx
2906
+
bar ["xx", "yy"] 2 yy
2907
+
SELECT *
2908
+
FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50)
2909
+
PATH '$')) AS ids
2910
+
WHERE 'xx' MEMBER OF (f2) AND 'yy' MEMBER OF (f2);
2911
+
f1 f2 i id
2912
+
bar ["xx", "yy"] 1 xx
2913
+
bar ["xx", "yy"] 2 yy
2914
+
SELECT *
2915
+
FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50)
2916
+
PATH '$')) AS ids
2917
+
WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"');
2918
+
f1 f2 i id
2919
+
bar ["xx", "yy"] 1 xx
2920
+
bar ["xx", "yy"] 2 yy
2921
+
SELECT *
2922
+
FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50)
2923
+
PATH '$')) AS ids
2924
+
WHERE json_overlaps(f2, '["xx", "zz"]') AND json_overlaps(f2, '["yy", "zz"]');
2925
+
f1 f2 i id
2926
+
bar ["xx", "yy"] 1 xx
2927
+
bar ["xx", "yy"] 2 yy
2928
+
SELECT *
2929
+
FROM t1
2930
+
WHERE 'xx' MEMBER OF (f2) AND 'yy' MEMBER OF (f2) AND f1 = 'bar';
2931
+
f1 f2
2932
+
bar ["xx", "yy"]
2933
+
SELECT *
2934
+
FROM t1
2935
+
WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"') AND f1 = 'bar';
2936
+
f1 f2
2937
+
bar ["xx", "yy"]
2938
+
SELECT *
2939
+
FROM t1
2940
+
WHERE json_overlaps(f2, '["xx", "zz"]') AND
2941
+
json_overlaps(f2, '["yy", "zz"]') AND
2942
+
f1 = 'bar';
2943
+
f1 f2
2944
+
bar ["xx", "yy"]
2945
+
SELECT *
2946
+
FROM v1
2947
+
WHERE 'xx' MEMBER OF (f2) AND 'yy' MEMBER OF (f2) AND f1 = 'bar';
2948
+
f1 f2 i id
2949
+
bar ["xx", "yy"] 1 xx
2950
+
bar ["xx", "yy"] 2 yy
2951
+
SELECT *
2952
+
FROM v1
2953
+
WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"') AND f1 = 'bar';
2954
+
f1 f2 i id
2955
+
bar ["xx", "yy"] 1 xx
2956
+
bar ["xx", "yy"] 2 yy
2957
+
SELECT *
2958
+
FROM v1
2959
+
WHERE json_overlaps(f2, '["xx", "zz"]') AND
2960
+
json_overlaps(f2, '["yy", "zz"]') AND
2961
+
f1 = 'bar';
2962
+
f1 f2 i id
2963
+
bar ["xx", "yy"] 1 xx
2964
+
bar ["xx", "yy"] 2 yy
2965
+
SELECT *
2966
+
FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50)
2967
+
PATH '$')) AS ids
2968
+
WHERE 'xx' MEMBER OF (f2) AND 'yy' MEMBER OF(f2) AND f1 = 'bar';
2969
+
f1 f2 i id
2970
+
bar ["xx", "yy"] 1 xx
2971
+
bar ["xx", "yy"] 2 yy
2972
+
SELECT *
2973
+
FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50)
2974
+
PATH '$')) AS ids
2975
+
WHERE json_contains(f2, '"xx"') AND json_contains(f2, '"yy"') AND f1 = 'bar';
2976
+
f1 f2 i id
2977
+
bar ["xx", "yy"] 1 xx
2978
+
bar ["xx", "yy"] 2 yy
2979
+
SELECT *
2980
+
FROM t1, JSON_TABLE(f2, '$[*]' COLUMNS(i FOR ORDINALITY, id VARCHAR(50)
2981
+
PATH '$')) AS ids
2982
+
WHERE json_overlaps(f2, '["xx", "zz"]') AND
2983
+
json_overlaps(f2, '["yy", "zz"]') AND
2984
+
f1 = 'bar';
2985
+
f1 f2 i id
2986
+
bar ["xx", "yy"] 1 xx
2987
+
bar ["xx", "yy"] 2 yy
2724
2988
EXPLAIN SELECT * FROM t1 WHERE 'xx' MEMBER OF (f2) OR 'zz' MEMBER OF (f2);
2725
2989
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
2726
2990
1 SIMPLE t1 NULL range idx2 idx2 203 NULL 2 100.00 Using where; Using MRR
0 commit comments