~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) | 0;
19
select (CASE "two" when "one" then 1.00 WHEN "two" then 2.00 END) +0.0;
20
select case 1/0 when "a" then "true" else "false" END;
21
select case 1/0 when "a" then "true" END;
22
select (case 1/0 when "a" then "true" END) | 0;
23
select (case 1/0 when "a" then "true" END) + 0.0;
24
select case when 1>0 then "TRUE" else "FALSE" END;
25
select case when 1<0 then "TRUE" else "FALSE" END;
26
27
#
28
# Test bug when using GROUP BY on CASE
29
#
30
create table t1 (a int);
31
insert into t1 values(1),(2),(3),(4);
32
select case a when 1 then 2 when 2 then 3 else 0 end as fcase, count(*) from t1 group by fcase;
33
explain extended select case a when 1 then 2 when 2 then 3 else 0 end as fcase, count(*) from t1 group by fcase;
34
select case a when 1 then "one" when 2 then "two" else "nothing" end as fcase, count(*) from t1 group by fcase;
35
drop table t1;
36
37
#
38
# Test MAX(CASE ... ) that can return null
39
#
40
41
create table t1 (row int not null, col int not null, val varchar(255) not null);
42
insert into t1 values (1,1,'orange'),(1,2,'large'),(2,1,'yellow'),(2,2,'medium'),(3,1,'green'),(3,2,'small');
43
select max(case col when 1 then val else null end) as color from t1 group by row;
44
drop table t1;
45
46
SET NAMES latin1;
47
48
#
49
# CASE and argument types/collations aggregation into result 
50
#
51
CREATE TABLE t1 SELECT 
52
 CASE WHEN 1 THEN _latin1'a' COLLATE latin1_danish_ci ELSE _latin1'a' END AS c1,
53
 CASE WHEN 1 THEN _latin1'a' ELSE _latin1'a' COLLATE latin1_danish_ci END AS c2,
54
 CASE WHEN 1 THEN 'a' ELSE  1  END AS c3,
55
 CASE WHEN 1 THEN  1  ELSE 'a' END AS c4,
56
 CASE WHEN 1 THEN 'a' ELSE 1.0 END AS c5,
57
 CASE WHEN 1 THEN 1.0 ELSE 'a' END AS c6,
58
 CASE WHEN 1 THEN  1  ELSE 1.0 END AS c7,
59
 CASE WHEN 1 THEN 1.0 ELSE  1  END AS c8,
60
 CASE WHEN 1 THEN 1.0 END AS c9,
61
 CASE WHEN 1 THEN 0.1e1 else 0.1 END AS c10,
62
 CASE WHEN 1 THEN 0.1e1 else 1 END AS c11,
63
 CASE WHEN 1 THEN 0.1e1 else '1' END AS c12
64
;
65
SHOW CREATE TABLE t1;
66
DROP TABLE t1;
67
68
--error 1267
69
SELECT CASE 
70
  WHEN 1 
71
  THEN _latin1'a' COLLATE latin1_danish_ci 
72
  ELSE _latin1'a' COLLATE latin1_swedish_ci
73
  END;
74
75
--error 1270
76
SELECT CASE _latin1'a' COLLATE latin1_general_ci
77
  WHEN _latin1'a' COLLATE latin1_danish_ci  THEN 1
78
  WHEN _latin1'a' COLLATE latin1_swedish_ci THEN 2
79
  END;
80
81
SELECT 
82
CASE _latin1'a' COLLATE latin1_general_ci  WHEN _latin1'A' THEN '1' ELSE 2 END,
83
CASE _latin1'a' COLLATE latin1_bin         WHEN _latin1'A' THEN '1' ELSE 2 END,
84
CASE _latin1'a' WHEN _latin1'A' COLLATE latin1_swedish_ci THEN '1' ELSE 2 END,
85
CASE _latin1'a' WHEN _latin1'A' COLLATE latin1_bin        THEN '1' ELSE 2 END
86
;
87
88
#
89
# COALESCE is a CASE abbrevation:
90
#
91
# COALESCE(v1,v2) == CASE WHEN v1 IS NOT NULL THEN v1 ELSE v2 END
92
#
93
# COALESCE(V1, V2, . . . ,Vn ) =  
94
#     CASE WHEN V1 IS NOT NULL THEN V1 ELSE COALESCE (V2, . . . ,Vn) END
95
#
96
# Check COALESCE argument types aggregation
97
98
--error 1267
99
CREATE TABLE t1 SELECT COALESCE(_latin1'a',_latin2'a');
100
--error 1267
101
CREATE TABLE t1 SELECT COALESCE('a' COLLATE latin1_swedish_ci,'b' COLLATE latin1_bin);
102
CREATE TABLE t1 SELECT 
103
 COALESCE(1), COALESCE(1.0),COALESCE('a'),
104
 COALESCE(1,1.0), COALESCE(1,'1'),COALESCE(1.1,'1'),
105
 COALESCE('a' COLLATE latin1_bin,'b');
106
explain extended SELECT 
107
 COALESCE(1), COALESCE(1.0),COALESCE('a'),
108
 COALESCE(1,1.0), COALESCE(1,'1'),COALESCE(1.1,'1'),
109
 COALESCE('a' COLLATE latin1_bin,'b');
110
SHOW CREATE TABLE t1;
111
DROP TABLE t1;
112
113
# Test for BUG#10151
114
SELECT 'case+union+test'
115
UNION 
116
SELECT CASE LOWER('1') WHEN LOWER('2') THEN 'BUG' ELSE 'nobug' END;
117
118
SELECT CASE LOWER('1') WHEN LOWER('2') THEN 'BUG' ELSE 'nobug' END;
119
120
SELECT 'case+union+test'
121
UNION 
122
SELECT CASE '1' WHEN '2' THEN 'BUG' ELSE 'nobug' END;
123
124
#
125
# Bug #17896: problem with MIN(CASE...)
126
#
127
128
create table t1(a float, b int default 3);
129
insert into t1 (a) values (2), (11), (8);
130
select min(a), min(case when 1=1 then a else NULL end),
131
  min(case when 1!=1 then NULL else a end) 
132
from t1 where b=3 group by b;
133
drop table t1;
134
135
136
#
137
# Tests for bug #9939: conversion of the arguments for COALESCE and IFNULL
138
#
139
140
CREATE TABLE t1 (EMPNUM INT);
141
INSERT INTO t1 VALUES (0), (2);
142
CREATE TABLE t2 (EMPNUM DECIMAL (4, 2));
143
INSERT INTO t2 VALUES (0.0), (9.0);
144
145
SELECT COALESCE(t2.EMPNUM,t1.EMPNUM) AS CEMPNUM,
146
               t1.EMPNUM AS EMPMUM1, t2.EMPNUM AS EMPNUM2
147
  FROM t1 LEFT JOIN t2 ON t1.EMPNUM=t2.EMPNUM;
148
149
SELECT IFNULL(t2.EMPNUM,t1.EMPNUM) AS CEMPNUM,
150
               t1.EMPNUM AS EMPMUM1, t2.EMPNUM AS EMPNUM2
151
  FROM t1 LEFT JOIN t2 ON t1.EMPNUM=t2.EMPNUM;
152
153
DROP TABLE t1,t2;
154
155
--echo End of 4.1 tests
156
157
#
158
# #30782: Truncated UNSIGNED BIGINT columns 
159
#
160
create table t1 (a int, b bigint unsigned);
161
create table t2 (c int);
162
insert into t1 (a, b) values (1,4572794622775114594), (2,18196094287899841997),
163
  (3,11120436154190595086);
164
insert into t2 (c) values (1), (2), (3);
165
select t1.a, (case t1.a when 0 then 0 else t1.b end) d from t1 
166
  join t2 on t1.a=t2.c order by d;
167
select t1.a, (case t1.a when 0 then 0 else t1.b end) d from t1 
168
  join t2 on t1.a=t2.c where b=11120436154190595086 order by d;
169
drop table t1, t2;
170
171
--echo End of 5.0 tests
172