mirror of
https://github.com/MariaDB/server.git
synced 2025-01-19 05:22:25 +01:00
9fc769ff72
result The IN function aggregates result types of all expressions. It uses that type in comparison of left expression and expressions in right part. This approach works in most cases. But let's consider the case when the right part contains both strings and integers. In that case this approach may cause wrong results because all strings which do not start with a digit are evaluated as 0. CASE uses the same approach when a CASE expression is given thus it's also affected. The idea behind this fix is to make IN function to compare expressions with different result types differently. For example a string in the left part will be compared as string with strings specified in right part and will be converted to real for comparison to int or real items in the right part. A new function called collect_cmp_types() is added. It collects different result types for comparison of first item in the provided list with each other item in the list. The Item_func_in class now can refer up to 5 cmp_item objects: 1 for each result type for comparison purposes. cmp_item objects are allocated according to found result types. The comparison of the left expression with any right part expression is now based only on result types of these expressions. The Item_func_case class is modified in the similar way when a CASE expression is specified. Now it can allocate up to 5 cmp_item objects to compare CASE expression with WHEN expressions of different types. The comparison of the CASE expression with any WHEN expression now based only on result types of these expressions.
413 lines
11 KiB
Text
413 lines
11 KiB
Text
drop table if exists t1, t2;
|
||
select 1 in (1,2,3);
|
||
1 in (1,2,3)
|
||
1
|
||
select 10 in (1,2,3);
|
||
10 in (1,2,3)
|
||
0
|
||
select NULL in (1,2,3);
|
||
NULL in (1,2,3)
|
||
NULL
|
||
select 1 in (1,NULL,3);
|
||
1 in (1,NULL,3)
|
||
1
|
||
select 3 in (1,NULL,3);
|
||
3 in (1,NULL,3)
|
||
1
|
||
select 10 in (1,NULL,3);
|
||
10 in (1,NULL,3)
|
||
NULL
|
||
select 1.5 in (1.5,2.5,3.5);
|
||
1.5 in (1.5,2.5,3.5)
|
||
1
|
||
select 10.5 in (1.5,2.5,3.5);
|
||
10.5 in (1.5,2.5,3.5)
|
||
0
|
||
select NULL in (1.5,2.5,3.5);
|
||
NULL in (1.5,2.5,3.5)
|
||
NULL
|
||
select 1.5 in (1.5,NULL,3.5);
|
||
1.5 in (1.5,NULL,3.5)
|
||
1
|
||
select 3.5 in (1.5,NULL,3.5);
|
||
3.5 in (1.5,NULL,3.5)
|
||
1
|
||
select 10.5 in (1.5,NULL,3.5);
|
||
10.5 in (1.5,NULL,3.5)
|
||
NULL
|
||
CREATE TABLE t1 (a int, b int, c int);
|
||
insert into t1 values (1,2,3), (1,NULL,3);
|
||
select 1 in (a,b,c) from t1;
|
||
1 in (a,b,c)
|
||
1
|
||
1
|
||
select 3 in (a,b,c) from t1;
|
||
3 in (a,b,c)
|
||
1
|
||
1
|
||
select 10 in (a,b,c) from t1;
|
||
10 in (a,b,c)
|
||
0
|
||
NULL
|
||
select NULL in (a,b,c) from t1;
|
||
NULL in (a,b,c)
|
||
NULL
|
||
NULL
|
||
drop table t1;
|
||
CREATE TABLE t1 (a float, b float, c float);
|
||
insert into t1 values (1.5,2.5,3.5), (1.5,NULL,3.5);
|
||
select 1.5 in (a,b,c) from t1;
|
||
1.5 in (a,b,c)
|
||
1
|
||
1
|
||
select 3.5 in (a,b,c) from t1;
|
||
3.5 in (a,b,c)
|
||
1
|
||
1
|
||
select 10.5 in (a,b,c) from t1;
|
||
10.5 in (a,b,c)
|
||
0
|
||
NULL
|
||
drop table t1;
|
||
CREATE TABLE t1 (a varchar(10), b varchar(10), c varchar(10));
|
||
insert into t1 values ('A','BC','EFD'), ('A',NULL,'EFD');
|
||
select 'A' in (a,b,c) from t1;
|
||
'A' in (a,b,c)
|
||
1
|
||
1
|
||
select 'EFD' in (a,b,c) from t1;
|
||
'EFD' in (a,b,c)
|
||
1
|
||
1
|
||
select 'XSFGGHF' in (a,b,c) from t1;
|
||
'XSFGGHF' in (a,b,c)
|
||
0
|
||
NULL
|
||
drop table t1;
|
||
CREATE TABLE t1 (field char(1));
|
||
INSERT INTO t1 VALUES ('A'),(NULL);
|
||
SELECT * from t1 WHERE field IN (NULL);
|
||
field
|
||
SELECT * from t1 WHERE field NOT IN (NULL);
|
||
field
|
||
SELECT * from t1 where field = field;
|
||
field
|
||
A
|
||
SELECT * from t1 where field <=> field;
|
||
field
|
||
A
|
||
NULL
|
||
DELETE FROM t1 WHERE field NOT IN (NULL);
|
||
SELECT * FROM t1;
|
||
field
|
||
A
|
||
NULL
|
||
drop table t1;
|
||
create table t1 (id int(10) primary key);
|
||
insert into t1 values (1),(2),(3),(4),(5),(6),(7),(8),(9);
|
||
select * from t1 where id in (2,5,9);
|
||
id
|
||
2
|
||
5
|
||
9
|
||
drop table t1;
|
||
create table t1 (
|
||
a char(1) character set latin1 collate latin1_general_ci,
|
||
b char(1) character set latin1 collate latin1_swedish_ci,
|
||
c char(1) character set latin1 collate latin1_danish_ci
|
||
);
|
||
insert into t1 values ('A','B','C');
|
||
insert into t1 values ('a','c','c');
|
||
select * from t1 where a in (b);
|
||
ERROR HY000: Illegal mix of collations (latin1_general_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '='
|
||
select * from t1 where a in (b,c);
|
||
ERROR HY000: Illegal mix of collations (latin1_general_ci,IMPLICIT), (latin1_swedish_ci,IMPLICIT), (latin1_danish_ci,IMPLICIT) for operation ' IN '
|
||
select * from t1 where 'a' in (a,b,c);
|
||
ERROR HY000: Illegal mix of collations for operation ' IN '
|
||
select * from t1 where 'a' in (a);
|
||
a b c
|
||
A B C
|
||
a c c
|
||
select * from t1 where a in ('a');
|
||
a b c
|
||
A B C
|
||
a c c
|
||
select * from t1 where 'a' collate latin1_general_ci in (a,b,c);
|
||
a b c
|
||
A B C
|
||
a c c
|
||
select * from t1 where 'a' collate latin1_bin in (a,b,c);
|
||
a b c
|
||
a c c
|
||
select * from t1 where 'a' in (a,b,c collate latin1_bin);
|
||
a b c
|
||
a c c
|
||
explain extended select * from t1 where 'a' in (a,b,c collate latin1_bin);
|
||
id select_type table type possible_keys key key_len ref rows filtered Extra
|
||
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
|
||
Warnings:
|
||
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b`,`test`.`t1`.`c` AS `c` from `test`.`t1` where (_latin1'a' in (`test`.`t1`.`a`,`test`.`t1`.`b`,(`test`.`t1`.`c` collate latin1_bin)))
|
||
drop table t1;
|
||
set names utf8;
|
||
create table t1 (a char(10) character set utf8 not null);
|
||
insert into t1 values ('bbbb'),(_koi8r'<27><><EFBFBD><EFBFBD>'),(_latin1'<27><><EFBFBD><EFBFBD>');
|
||
select a from t1 where a in ('bbbb',_koi8r'<27><><EFBFBD><EFBFBD>',_latin1'<27><><EFBFBD><EFBFBD>') order by a;
|
||
a
|
||
ÄÄÄÄ
|
||
bbbb
|
||
цццц
|
||
drop table t1;
|
||
create table t1 (a char(10) character set latin1 not null);
|
||
insert into t1 values ('a'),('b'),('c');
|
||
select a from t1 where a IN ('a','b','c') order by a;
|
||
a
|
||
a
|
||
b
|
||
c
|
||
drop table t1;
|
||
set names latin1;
|
||
select '1.0' in (1,2);
|
||
'1.0' in (1,2)
|
||
1
|
||
select 1 in ('1.0',2);
|
||
1 in ('1.0',2)
|
||
1
|
||
select 1 in (1,'2.0');
|
||
1 in (1,'2.0')
|
||
1
|
||
select 1 in ('1.0',2.0);
|
||
1 in ('1.0',2.0)
|
||
1
|
||
select 1 in (1.0,'2.0');
|
||
1 in (1.0,'2.0')
|
||
1
|
||
select 1 in ('1.1',2);
|
||
1 in ('1.1',2)
|
||
0
|
||
select 1 in ('1.1',2.0);
|
||
1 in ('1.1',2.0)
|
||
0
|
||
create table t1 (a char(2) character set binary);
|
||
insert into t1 values ('aa'), ('bb');
|
||
select * from t1 where a in (NULL, 'aa');
|
||
a
|
||
aa
|
||
drop table t1;
|
||
create table t1 (id int, key(id));
|
||
insert into t1 values (1),(2),(3);
|
||
select count(*) from t1 where id not in (1);
|
||
count(*)
|
||
2
|
||
select count(*) from t1 where id not in (1,2);
|
||
count(*)
|
||
1
|
||
drop table t1;
|
||
CREATE TABLE t1 (a int PRIMARY KEY);
|
||
INSERT INTO t1 VALUES (44), (45), (46);
|
||
SELECT * FROM t1 WHERE a IN (45);
|
||
a
|
||
45
|
||
SELECT * FROM t1 WHERE a NOT IN (0, 45);
|
||
a
|
||
44
|
||
46
|
||
SELECT * FROM t1 WHERE a NOT IN (45);
|
||
a
|
||
44
|
||
46
|
||
CREATE VIEW v1 AS SELECT * FROM t1 WHERE a NOT IN (45);
|
||
SHOW CREATE VIEW v1;
|
||
View Create View
|
||
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1` where (`t1`.`a` <> 45)
|
||
SELECT * FROM v1;
|
||
a
|
||
44
|
||
46
|
||
DROP VIEW v1;
|
||
DROP TABLE t1;
|
||
create table t1 (a int);
|
||
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
||
create table t2 (a int, filler char(200), key(a));
|
||
insert into t2 select C.a*2, 'no' from t1 A, t1 B, t1 C;
|
||
insert into t2 select C.a*2+1, 'yes' from t1 C;
|
||
explain
|
||
select * from t2 where a NOT IN (0, 2,4,6,8,10,12,14,16,18);
|
||
id select_type table type possible_keys key key_len ref rows Extra
|
||
1 SIMPLE t2 range a a 5 NULL 12 Using where
|
||
select * from t2 where a NOT IN (0, 2,4,6,8,10,12,14,16,18);
|
||
a filler
|
||
1 yes
|
||
3 yes
|
||
5 yes
|
||
7 yes
|
||
9 yes
|
||
11 yes
|
||
13 yes
|
||
15 yes
|
||
17 yes
|
||
19 yes
|
||
explain select * from t2 force index(a) where a NOT IN (2,2,2,2,2,2);
|
||
id select_type table type possible_keys key key_len ref rows Extra
|
||
1 SIMPLE t2 range a a 5 NULL 912 Using where
|
||
explain select * from t2 force index(a) where a <> 2;
|
||
id select_type table type possible_keys key key_len ref rows Extra
|
||
1 SIMPLE t2 range a a 5 NULL 912 Using where
|
||
drop table t2;
|
||
create table t2 (a datetime, filler char(200), key(a));
|
||
insert into t2 select '2006-04-25 10:00:00' + interval C.a minute,
|
||
'no' from t1 A, t1 B, t1 C where C.a % 2 = 0;
|
||
insert into t2 select '2006-04-25 10:00:00' + interval C.a*2+1 minute,
|
||
'yes' from t1 C;
|
||
explain
|
||
select * from t2 where a NOT IN (
|
||
'2006-04-25 10:00:00','2006-04-25 10:02:00','2006-04-25 10:04:00',
|
||
'2006-04-25 10:06:00', '2006-04-25 10:08:00');
|
||
id select_type table type possible_keys key key_len ref rows Extra
|
||
1 SIMPLE t2 range a a 9 NULL 18 Using where
|
||
select * from t2 where a NOT IN (
|
||
'2006-04-25 10:00:00','2006-04-25 10:02:00','2006-04-25 10:04:00',
|
||
'2006-04-25 10:06:00', '2006-04-25 10:08:00');
|
||
a filler
|
||
2006-04-25 10:01:00 yes
|
||
2006-04-25 10:03:00 yes
|
||
2006-04-25 10:05:00 yes
|
||
2006-04-25 10:07:00 yes
|
||
2006-04-25 10:09:00 yes
|
||
2006-04-25 10:11:00 yes
|
||
2006-04-25 10:13:00 yes
|
||
2006-04-25 10:15:00 yes
|
||
2006-04-25 10:17:00 yes
|
||
2006-04-25 10:19:00 yes
|
||
drop table t2;
|
||
create table t2 (a varchar(10), filler char(200), key(a));
|
||
insert into t2 select 'foo', 'no' from t1 A, t1 B;
|
||
insert into t2 select 'barbar', 'no' from t1 A, t1 B;
|
||
insert into t2 select 'bazbazbaz', 'no' from t1 A, t1 B;
|
||
insert into t2 values ('fon', '1'), ('fop','1'), ('barbaq','1'),
|
||
('barbas','1'), ('bazbazbay', '1'),('zz','1');
|
||
explain select * from t2 where a not in('foo','barbar', 'bazbazbaz');
|
||
id select_type table type possible_keys key key_len ref rows Extra
|
||
1 SIMPLE t2 range a a 13 NULL 7 Using where
|
||
drop table t2;
|
||
create table t2 (a decimal(10,5), filler char(200), key(a));
|
||
insert into t2 select 345.67890, 'no' from t1 A, t1 B;
|
||
insert into t2 select 43245.34, 'no' from t1 A, t1 B;
|
||
insert into t2 select 64224.56344, 'no' from t1 A, t1 B;
|
||
insert into t2 values (0, '1'), (22334.123,'1'), (33333,'1'),
|
||
(55555,'1'), (77777, '1');
|
||
explain
|
||
select * from t2 where a not in (345.67890, 43245.34, 64224.56344);
|
||
id select_type table type possible_keys key key_len ref rows Extra
|
||
1 SIMPLE t2 range a a 7 NULL 7 Using where
|
||
select * from t2 where a not in (345.67890, 43245.34, 64224.56344);
|
||
a filler
|
||
0.00000 1
|
||
22334.12300 1
|
||
33333.00000 1
|
||
55555.00000 1
|
||
77777.00000 1
|
||
drop table t2;
|
||
create table t2 (a int, key(a), b int);
|
||
insert into t2 values (1,1),(2,2);
|
||
set @cnt= 1;
|
||
set @str="update t2 set b=1 where a not in (";
|
||
select count(*) from (
|
||
select @str:=concat(@str, @cnt:=@cnt+1, ",")
|
||
from t1 A, t1 B, t1 C, t1 D) Z;
|
||
count(*)
|
||
10000
|
||
set @str:=concat(@str, "10000)");
|
||
select substr(@str, 1, 50);
|
||
substr(@str, 1, 50)
|
||
update t2 set b=1 where a not in (2,3,4,5,6,7,8,9,
|
||
prepare s from @str;
|
||
execute s;
|
||
deallocate prepare s;
|
||
set @str=NULL;
|
||
drop table t2;
|
||
drop table t1;
|
||
create table t1 (
|
||
some_id smallint(5) unsigned,
|
||
key (some_id)
|
||
);
|
||
insert into t1 values (1),(2);
|
||
select some_id from t1 where some_id not in(2,-1);
|
||
some_id
|
||
1
|
||
select some_id from t1 where some_id not in(-4,-1,-4);
|
||
some_id
|
||
1
|
||
2
|
||
select some_id from t1 where some_id not in(-4,-1,3423534,2342342);
|
||
some_id
|
||
1
|
||
2
|
||
drop table t1;
|
||
create table t1(f1 char(1));
|
||
insert into t1 values ('a'),('b'),('1');
|
||
select f1 from t1 where f1 in ('a',1);
|
||
f1
|
||
a
|
||
1
|
||
select f1, case f1 when 'a' then '+' when 1 then '-' end from t1;
|
||
f1 case f1 when 'a' then '+' when 1 then '-' end
|
||
a +
|
||
b NULL
|
||
1 -
|
||
create index t1f1_idx on t1(f1);
|
||
select f1 from t1 where f1 in ('a',1);
|
||
f1
|
||
1
|
||
a
|
||
explain select f1 from t1 where f1 in ('a',1);
|
||
id select_type table type possible_keys key key_len ref rows Extra
|
||
1 SIMPLE t1 index NULL t1f1_idx 2 NULL 3 Using where; Using index
|
||
select f1 from t1 where f1 in ('a','b');
|
||
f1
|
||
a
|
||
b
|
||
explain select f1 from t1 where f1 in ('a','b');
|
||
id select_type table type possible_keys key key_len ref rows Extra
|
||
1 SIMPLE t1 index t1f1_idx t1f1_idx 2 NULL 3 Using where; Using index
|
||
select f1 from t1 where f1 in (2,1);
|
||
f1
|
||
1
|
||
explain select f1 from t1 where f1 in (2,1);
|
||
id select_type table type possible_keys key key_len ref rows Extra
|
||
1 SIMPLE t1 index t1f1_idx t1f1_idx 2 NULL 3 Using where; Using index
|
||
create table t2(f2 int, index t2f2(f2));
|
||
insert into t2 values(0),(1),(2);
|
||
select f2 from t2 where f2 in ('a',2);
|
||
f2
|
||
0
|
||
2
|
||
Warnings:
|
||
Warning 1292 Truncated incorrect DOUBLE value: 'a'
|
||
Warning 1292 Truncated incorrect DOUBLE value: 'a'
|
||
Warning 1292 Truncated incorrect DOUBLE value: 'a'
|
||
explain select f2 from t2 where f2 in ('a',2);
|
||
id select_type table type possible_keys key key_len ref rows Extra
|
||
1 SIMPLE t2 index NULL t2f2 5 NULL 3 Using where; Using index
|
||
select f2 from t2 where f2 in ('a','b');
|
||
f2
|
||
0
|
||
Warnings:
|
||
Warning 1292 Truncated incorrect DOUBLE value: 'a'
|
||
Warning 1292 Truncated incorrect DOUBLE value: 'b'
|
||
explain select f2 from t2 where f2 in ('a','b');
|
||
id select_type table type possible_keys key key_len ref rows Extra
|
||
1 SIMPLE t2 index t2f2 t2f2 5 NULL 3 Using where; Using index
|
||
Warnings:
|
||
Warning 1292 Truncated incorrect DOUBLE value: 'a'
|
||
Warning 1292 Truncated incorrect DOUBLE value: 'b'
|
||
select f2 from t2 where f2 in (1,'b');
|
||
f2
|
||
0
|
||
1
|
||
Warnings:
|
||
Warning 1292 Truncated incorrect DOUBLE value: 'b'
|
||
Warning 1292 Truncated incorrect DOUBLE value: 'b'
|
||
explain select f2 from t2 where f2 in (1,'b');
|
||
id select_type table type possible_keys key key_len ref rows Extra
|
||
1 SIMPLE t2 index NULL t2f2 5 NULL 3 Using where; Using index
|
||
drop table t1, t2;
|