~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to mysql-test/t/case.test

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

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