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 |
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); |
|
685.2.4
by Monty Taylor
Fixed case test. |
23 |
select (case 1/0 when "a" then "true" END) + 0.0; |
1
by brian
clean slate |
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 |
#
|
|
47 |
# CASE and argument types/collations aggregation into result |
|
48 |
#
|
|
49 |
CREATE TABLE t1 SELECT |
|
50 |
CASE WHEN 1 THEN 'a' COLLATE utf8_bin ELSE 'a' END AS c1, |
|
685.2.4
by Monty Taylor
Fixed case test. |
51 |
CASE WHEN 1 THEN 'a' ELSE 'a' COLLATE utf8_bin END AS c2, |
52 |
CASE WHEN 1 THEN 'a' ELSE 1 END AS c3, |
|
1
by brian
clean slate |
53 |
CASE WHEN 1 THEN 1 ELSE 'a' END AS c4, |
54 |
CASE WHEN 1 THEN 'a' ELSE 1.0 END AS c5, |
|
55 |
CASE WHEN 1 THEN 1.0 ELSE 'a' END AS c6, |
|
56 |
CASE WHEN 1 THEN 1 ELSE 1.0 END AS c7, |
|
57 |
CASE WHEN 1 THEN 1.0 ELSE 1 END AS c8, |
|
58 |
CASE WHEN 1 THEN 1.0 END AS c9, |
|
59 |
CASE WHEN 1 THEN 0.1e1 else 0.1 END AS c10, |
|
60 |
CASE WHEN 1 THEN 0.1e1 else 1 END AS c11, |
|
61 |
CASE WHEN 1 THEN 0.1e1 else '1' END AS c12 |
|
62 |
;
|
|
63 |
SHOW CREATE TABLE t1; |
|
64 |
DROP TABLE t1; |
|
65 |
||
66 |
--error 1267
|
|
67 |
SELECT CASE |
|
68 |
WHEN 1 |
|
69 |
THEN 'a' COLLATE utf8_bin |
|
685.2.4
by Monty Taylor
Fixed case test. |
70 |
ELSE 'a' COLLATE utf8_swedish_ci |
71 |
END; |
|
1
by brian
clean slate |
72 |
|
73 |
--error 1270
|
|
74 |
SELECT CASE 'a' COLLATE utf8_bin |
|
685.2.4
by Monty Taylor
Fixed case test. |
75 |
WHEN 'a' COLLATE utf8_danish_ci THEN 1 |
76 |
WHEN 'a' COLLATE utf8_swedish_ci THEN 2 |
|
77 |
END; |
|
1
by brian
clean slate |
78 |
|
79 |
SELECT
|
|
80 |
CASE 'a' COLLATE utf8_general_ci WHEN 'A' THEN '1' ELSE 2 END, |
|
685.2.4
by Monty Taylor
Fixed case test. |
81 |
CASE 'a' COLLATE utf8_bin WHEN 'A' THEN '1' ELSE 2 END, |
82 |
CASE 'a' WHEN 'A' COLLATE utf8_swedish_ci THEN '1' ELSE 2 END, |
|
83 |
CASE 'a' WHEN 'A' COLLATE utf8_bin THEN '1' ELSE 2 END |
|
84 |
;
|
|
1
by brian
clean slate |
85 |
|
86 |
#
|
|
87 |
# COALESCE is a CASE abbrevation: |
|
88 |
#
|
|
89 |
# COALESCE(v1,v2) == CASE WHEN v1 IS NOT NULL THEN v1 ELSE v2 END |
|
90 |
#
|
|
91 |
# COALESCE(V1, V2, . . . ,Vn ) = |
|
92 |
# CASE WHEN V1 IS NOT NULL THEN V1 ELSE COALESCE (V2, . . . ,Vn) END |
|
93 |
#
|
|
94 |
# Check COALESCE argument types aggregation |
|
95 |
||
96 |
--error 1267
|
|
97 |
CREATE TABLE t1 SELECT COALESCE('a' COLLATE utf8_swedish_ci,'b' COLLATE utf8_bin); |
|
685.2.4
by Monty Taylor
Fixed case test. |
98 |
CREATE TABLE t1 SELECT |
1
by brian
clean slate |
99 |
COALESCE(1), COALESCE(1.0),COALESCE('a'), |
100 |
COALESCE(1,1.0), COALESCE(1,'1'),COALESCE(1.1,'1'), |
|
101 |
COALESCE('a' COLLATE utf8_bin,'b'); |
|
685.2.4
by Monty Taylor
Fixed case test. |
102 |
explain extended SELECT |
1
by brian
clean slate |
103 |
COALESCE(1), COALESCE(1.0),COALESCE('a'), |
104 |
COALESCE(1,1.0), COALESCE(1,'1'),COALESCE(1.1,'1'), |
|
105 |
COALESCE('a' COLLATE utf8_bin,'b'); |
|
685.2.4
by Monty Taylor
Fixed case test. |
106 |
SHOW CREATE TABLE t1; |
1
by brian
clean slate |
107 |
DROP TABLE t1; |
108 |
||
109 |
# Test for BUG#10151 |
|
110 |
SELECT 'case+union+test' |
|
111 |
UNION
|
|
112 |
SELECT CASE LOWER('1') WHEN LOWER('2') THEN 'BUG' ELSE 'nobug' END; |
|
113 |
||
114 |
SELECT CASE LOWER('1') WHEN LOWER('2') THEN 'BUG' ELSE 'nobug' END; |
|
115 |
||
116 |
SELECT 'case+union+test' |
|
117 |
UNION
|
|
118 |
SELECT CASE '1' WHEN '2' THEN 'BUG' ELSE 'nobug' END; |
|
119 |
||
120 |
#
|
|
121 |
# Bug #17896: problem with MIN(CASE...) |
|
122 |
#
|
|
123 |
||
124 |
create table t1(a float, b int default 3); |
|
125 |
insert into t1 (a) values (2), (11), (8); |
|
126 |
select min(a), min(case when 1=1 then a else NULL end), |
|
127 |
min(case when 1!=1 then NULL else a end) |
|
128 |
from t1 where b=3 group by b; |
|
129 |
drop table t1; |
|
130 |
||
131 |
||
132 |
#
|
|
133 |
# Tests for bug #9939: conversion of the arguments for COALESCE and IFNULL |
|
134 |
#
|
|
135 |
||
136 |
CREATE TABLE t1 (EMPNUM INT); |
|
137 |
INSERT INTO t1 VALUES (0), (2); |
|
138 |
CREATE TABLE t2 (EMPNUM DECIMAL (4, 2)); |
|
139 |
INSERT INTO t2 VALUES (0.0), (9.0); |
|
140 |
||
141 |
SELECT COALESCE(t2.EMPNUM,t1.EMPNUM) AS CEMPNUM, |
|
142 |
t1.EMPNUM AS EMPMUM1, t2.EMPNUM AS EMPNUM2 |
|
143 |
FROM t1 LEFT JOIN t2 ON t1.EMPNUM=t2.EMPNUM; |
|
144 |
||
145 |
SELECT IFNULL(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 |
DROP TABLE t1,t2; |
|
150 |
||
151 |
--echo End of 4.1 tests
|
|
152 |
||
153 |
#
|
|
154 |
# #30782: Truncated BIGINT columns |
|
413.2.2
by Brian Aker
Removed UNSIGNED from parser. |
155 |
#
|
1
by brian
clean slate |
156 |
create table t1 (a int, b bigint); |
413.2.2
by Brian Aker
Removed UNSIGNED from parser. |
157 |
create table t2 (c int); |
1
by brian
clean slate |
158 |
insert into t1 (a, b) values (1,4572794622775114594), (2,18196094287899841997), |
159 |
(3,11120436154190595086); |
|
160 |
insert into t2 (c) values (1), (2), (3); |
|
161 |
select t1.a, (case t1.a when 0 then 0 else t1.b end) d from t1 |
|
162 |
join t2 on t1.a=t2.c order by d; |
|
163 |
select t1.a, (case t1.a when 0 then 0 else t1.b end) d from t1 |
|
164 |
join t2 on t1.a=t2.c where b=11120436154190595086 order by d; |
|
165 |
drop table t1, t2; |
|
166 |
||
167 |
--echo End of 5.0 tests
|
|
168 |