~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
# Testing of CASE
2
#
3
4
--disable_warnings
5
drop table if exists t1, t2;
6
--enable_warnings
7
8
select CASE "b" when "a" then 1 when "b" then 2 END;
9
select CASE "c" when "a" then 1 when "b" then 2 END;
10
select CASE "c" when "a" then 1 when "b" then 2 ELSE 3 END;
11
select CASE BINARY "b" when "a" then 1 when "B" then 2 WHEN "b" then "ok" END;
12
select CASE "b" when "a" then 1 when binary "B" then 2 WHEN "b" then "ok" END;
13
select CASE concat("a","b") when concat("ab","") then "a" when "b" then "b" end;
14
select CASE when 1=0 then "true" else "false" END;
15
select CASE 1 when 1 then "one" WHEN 2 then "two" ELSE "more" END;
16
explain extended select CASE 1 when 1 then "one" WHEN 2 then "two" ELSE "more" END;
17
select CASE 2.0 when 1 then "one" WHEN 2.0 then "two" ELSE "more" END;
18
select (CASE "two" when "one" then "1" WHEN "two" then "2" END);
685.2.4 by Monty Taylor
Fixed case test.
19
select (CASE "two" when "one" then 1.00 WHEN "two" then 2.00 END) +0.0;
1 by brian
clean slate
20
--error ER_DIVISION_BY_ZERO
1812.4.2 by Brian Aker
Fix issue with divide by zero not being an error.
21
select case 1/0 when "a" then "true" else "false" END;
1 by brian
clean slate
22
--error ER_DIVISION_BY_ZERO
1812.4.2 by Brian Aker
Fix issue with divide by zero not being an error.
23
select case 1/0 when "a" then "true" END;
1 by brian
clean slate
24
--error ER_DIVISION_BY_ZERO
1812.4.2 by Brian Aker
Fix issue with divide by zero not being an error.
25
select (case 1/0 when "a" then "true" END);
685.2.4 by Monty Taylor
Fixed case test.
26
--error ER_DIVISION_BY_ZERO
1812.4.2 by Brian Aker
Fix issue with divide by zero not being an error.
27
select (case 1/0 when "a" then "true" END) + 0.0;
1 by brian
clean slate
28
select case when 1>0 then "TRUE" else "FALSE" END;
29
select case when 1<0 then "TRUE" else "FALSE" END;
30
31
#
32
# Test bug when using GROUP BY on CASE
33
#
34
create table t1 (a int);
35
insert into t1 values(1),(2),(3),(4);
36
select case a when 1 then 2 when 2 then 3 else 0 end as fcase, count(*) from t1 group by fcase;
37
explain extended select case a when 1 then 2 when 2 then 3 else 0 end as fcase, count(*) from t1 group by fcase;
38
select case a when 1 then "one" when 2 then "two" else "nothing" end as fcase, count(*) from t1 group by fcase;
39
drop table t1;
40
41
#
42
# Test MAX(CASE ... ) that can return null
43
#
44
45
create table t1 (row int not null, col int not null, val varchar(255) not null);
46
insert into t1 values (1,1,'orange'),(1,2,'large'),(2,1,'yellow'),(2,2,'medium'),(3,1,'green'),(3,2,'small');
47
select max(case col when 1 then val else null end) as color from t1 group by row;
48
drop table t1;
49
50
#
51
# CASE and argument types/collations aggregation into result 
52
#
53
CREATE TABLE t1 SELECT 
54
 CASE WHEN 1 THEN 'a' COLLATE utf8_bin ELSE 'a' END AS c1,
685.2.4 by Monty Taylor
Fixed case test.
55
 CASE WHEN 1 THEN 'a' ELSE 'a' COLLATE utf8_bin END AS c2,
56
 CASE WHEN 1 THEN 'a' ELSE  1  END AS c3,
1 by brian
clean slate
57
 CASE WHEN 1 THEN  1  ELSE 'a' END AS c4,
58
 CASE WHEN 1 THEN 'a' ELSE 1.0 END AS c5,
59
 CASE WHEN 1 THEN 1.0 ELSE 'a' END AS c6,
60
 CASE WHEN 1 THEN  1  ELSE 1.0 END AS c7,
61
 CASE WHEN 1 THEN 1.0 ELSE  1  END AS c8,
62
 CASE WHEN 1 THEN 1.0 END AS c9,
63
 CASE WHEN 1 THEN 0.1e1 else 0.1 END AS c10,
64
 CASE WHEN 1 THEN 0.1e1 else 1 END AS c11,
65
 CASE WHEN 1 THEN 0.1e1 else '1' END AS c12
66
;
67
942.3.1 by Vladimir Kolesnikov
test generalizations
68
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
69
SHOW CREATE TABLE t1;
1 by brian
clean slate
70
DROP TABLE t1;
71
72
--error ER_CANT_AGGREGATE_2COLLATIONS
1731.3.1 by Lee Bieber
change tests to use enum values instead of error numbers
73
SELECT CASE 
1 by brian
clean slate
74
  WHEN 1 
75
  THEN 'a' COLLATE utf8_bin 
685.2.4 by Monty Taylor
Fixed case test.
76
  ELSE 'a' COLLATE utf8_swedish_ci
77
  END;
1 by brian
clean slate
78
79
--error ER_CANT_AGGREGATE_3COLLATIONS
1731.3.1 by Lee Bieber
change tests to use enum values instead of error numbers
80
SELECT CASE 'a' COLLATE utf8_bin
685.2.4 by Monty Taylor
Fixed case test.
81
  WHEN 'a' COLLATE utf8_danish_ci  THEN 1
82
  WHEN 'a' COLLATE utf8_swedish_ci THEN 2
83
  END;
1 by brian
clean slate
84
85
SELECT 
86
CASE 'a' COLLATE utf8_general_ci  WHEN 'A' THEN '1' ELSE 2 END,
685.2.4 by Monty Taylor
Fixed case test.
87
CASE 'a' COLLATE utf8_bin         WHEN 'A' THEN '1' ELSE 2 END,
88
CASE 'a' WHEN 'A' COLLATE utf8_swedish_ci THEN '1' ELSE 2 END,
89
CASE 'a' WHEN 'A' COLLATE utf8_bin        THEN '1' ELSE 2 END
90
;
1 by brian
clean slate
91
92
#
93
# COALESCE is a CASE abbrevation:
94
#
95
# COALESCE(v1,v2) == CASE WHEN v1 IS NOT NULL THEN v1 ELSE v2 END
96
#
97
# COALESCE(V1, V2, . . . ,Vn ) =  
98
#     CASE WHEN V1 IS NOT NULL THEN V1 ELSE COALESCE (V2, . . . ,Vn) END
99
#
100
# Check COALESCE argument types aggregation
101
102
--error ER_CANT_AGGREGATE_2COLLATIONS
1731.3.1 by Lee Bieber
change tests to use enum values instead of error numbers
103
CREATE TABLE t1 SELECT COALESCE('a' COLLATE utf8_swedish_ci,'b' COLLATE utf8_bin);
685.2.4 by Monty Taylor
Fixed case test.
104
CREATE TABLE t1 SELECT 
1 by brian
clean slate
105
 COALESCE(1), COALESCE(1.0),COALESCE('a'),
106
 COALESCE(1,1.0), COALESCE(1,'1'),COALESCE(1.1,'1'),
107
 COALESCE('a' COLLATE utf8_bin,'b');
685.2.4 by Monty Taylor
Fixed case test.
108
explain extended SELECT 
1 by brian
clean slate
109
 COALESCE(1), COALESCE(1.0),COALESCE('a'),
110
 COALESCE(1,1.0), COALESCE(1,'1'),COALESCE(1.1,'1'),
111
 COALESCE('a' COLLATE utf8_bin,'b');
685.2.4 by Monty Taylor
Fixed case test.
112
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
942.3.1 by Vladimir Kolesnikov
test generalizations
113
SHOW CREATE TABLE t1;
1 by brian
clean slate
114
DROP TABLE t1;
115
116
# Test for BUG#10151
117
SELECT 'case+union+test'
118
UNION 
119
SELECT CASE LOWER('1') WHEN LOWER('2') THEN 'BUG' ELSE 'nobug' END;
120
121
SELECT CASE LOWER('1') WHEN LOWER('2') THEN 'BUG' ELSE 'nobug' END;
122
123
SELECT 'case+union+test'
124
UNION 
125
SELECT CASE '1' WHEN '2' THEN 'BUG' ELSE 'nobug' END;
126
127
#
128
# Bug #17896: problem with MIN(CASE...)
129
#
130
131
create table t1(a float, b int default 3);
132
insert into t1 (a) values (2), (11), (8);
133
select min(a), min(case when 1=1 then a else NULL end),
134
  min(case when 1!=1 then NULL else a end) 
135
from t1 where b=3 group by b;
136
drop table t1;
137
138
139
#
140
# Tests for bug #9939: conversion of the arguments for COALESCE and IFNULL
141
#
142
143
CREATE TABLE t1 (EMPNUM INT);
144
INSERT INTO t1 VALUES (0), (2);
145
CREATE TABLE t2 (EMPNUM DECIMAL (4, 2));
146
INSERT INTO t2 VALUES (0.0), (9.0);
147
148
SELECT COALESCE(t2.EMPNUM,t1.EMPNUM) AS CEMPNUM,
149
               t1.EMPNUM AS EMPMUM1, t2.EMPNUM AS EMPNUM2
150
  FROM t1 LEFT JOIN t2 ON t1.EMPNUM=t2.EMPNUM;
151
152
SELECT IFNULL(t2.EMPNUM,t1.EMPNUM) AS CEMPNUM,
153
               t1.EMPNUM AS EMPMUM1, t2.EMPNUM AS EMPNUM2
154
  FROM t1 LEFT JOIN t2 ON t1.EMPNUM=t2.EMPNUM;
155
156
DROP TABLE t1,t2;
157
158
--echo End of 4.1 tests
159
160
#
161
# #30782: Truncated BIGINT columns 
413.2.2 by Brian Aker
Removed UNSIGNED from parser.
162
#
1 by brian
clean slate
163
create table t1 (a int, b bigint unsigned);
2008.2.4 by Brian Aker
Merge in additional fixes for sign, plus alter table, plus TIME on
164
create table t2 (c int);
1 by brian
clean slate
165
insert into t1 (a, b) values (1,4572794622775114594), (2,18196094287899841997),
166
  (3,11120436154190595086);
167
insert into t2 (c) values (1), (2), (3);
168
select t1.a, (case t1.a when 0 then 0 else t1.b end) d from t1 
169
  join t2 on t1.a=t2.c order by d;
170
select t1.a, (case t1.a when 0 then 0 else t1.b end) d from t1 
171
  join t2 on t1.a=t2.c where b=11120436154190595086 order by d;
172
drop table t1, t2;
173
174
--echo End of 5.0 tests
175