2 # Test of cast function
5 select CAST(1-2 AS UNSIGNED);
6 select CAST(CAST(1-2 AS UNSIGNED) AS SIGNED INTEGER);
7 select CAST('10 ' as unsigned integer);
8 select cast(-5 as unsigned) | 1, cast(-5 as unsigned) & -1;
9 select cast(-5 as unsigned) -1, cast(-5 as unsigned) + 1;
10 select ~5, cast(~5 as signed);
11 explain extended select ~5, cast(~5 as signed);
12 select cast(5 as unsigned) -6.0;
13 select cast(NULL as signed), cast(1/0 as signed);
15 # Bug #28250: Run-Time Check Failure #3 - The variable 'value' is being used
18 # The following line causes Run-Time Check Failure on
19 # binaries built with Visual C++ 2005
20 select cast(NULL as unsigned), cast(1/0 as unsigned);
21 select cast("A" as binary) = "a", cast(BINARY "a" as CHAR) = "A";
22 select cast("2001-1-1" as DATE), cast("2001-1-1" as DATETIME);
23 select cast("1:2:3" as TIME);
24 select CONVERT("2004-01-22 21:45:33",DATE);
29 # The following cast creates warnings
31 select CONVERT(DATE "2004-01-22 21:45:33" USING latin1);
32 select CONVERT(DATE "2004-01-22 21:45:33",CHAR);
33 select CONVERT(DATE "2004-01-22 21:45:33",CHAR(4));
34 select CONVERT(DATE "2004-01-22 21:45:33",BINARY(4));
35 select CAST(DATE "2004-01-22 21:45:33" AS BINARY(4));
36 select CAST(0xb3 as signed);
37 select CAST(0x8fffffffffffffff as signed);
38 select CAST(0xffffffffffffffff as unsigned);
39 select CAST(0xfffffffffffffffe as signed);
40 select cast('-10a' as signed integer);
41 select cast('a10' as unsigned integer);
43 select 10.0+cast('a' as decimal);
47 select cast('18446744073709551616' as unsigned);
48 select cast('18446744073709551616' as signed);
49 select cast('9223372036854775809' as signed);
50 select cast('-1' as unsigned);
51 select cast('abc' as signed);
52 select cast('1a' as signed);
53 select cast('' as signed);
56 # Character set conversion
59 select cast(_latin1'test' as char character set latin2);
60 select cast(_koi8r'ÔÅÓÔ' as char character set cp1251);
61 create table t1 select cast(_koi8r'ÔÅÓÔ' as char character set cp1251) as t;
66 # CAST to CHAR with/without length
69 cast(_latin1'ab' AS char) as c1,
70 cast(_latin1'a ' AS char) as c2,
71 cast(_latin1'abc' AS char(2)) as c3,
72 cast(_latin1'a ' AS char(2)) as c4,
73 hex(cast(_latin1'a' AS char(2))) as c5;
74 select cast(1000 as CHAR(3));
76 create table t1 select
77 cast(_latin1'ab' AS char) as c1,
78 cast(_latin1'a ' AS char) as c2,
79 cast(_latin1'abc' AS char(2)) as c3,
80 cast(_latin1'a ' AS char(2)) as c4,
81 cast(_latin1'a' AS char(2)) as c5;
82 select c1,c2,c3,c4,hex(c5) from t1;
87 # CAST to NCHAR with/without length
90 cast(_koi8r'ÆÇ' AS nchar) as c1,
91 cast(_koi8r'Æ ' AS nchar) as c2,
92 cast(_koi8r'ÆÇÈ' AS nchar(2)) as c3,
93 cast(_koi8r'Æ ' AS nchar(2)) as c4,
94 cast(_koi8r'Æ' AS nchar(2)) as c5;
96 create table t1 select
97 cast(_koi8r'ÆÇ' AS nchar) as c1,
98 cast(_koi8r'Æ ' AS nchar) as c2,
99 cast(_koi8r'ÆÇÈ' AS nchar(2)) as c3,
100 cast(_koi8r'Æ ' AS nchar(2)) as c4,
101 cast(_koi8r'Æ' AS nchar(2)) as c5;
103 show create table t1;
108 # CAST from BINARY to non-BINARY and from non-BINARY to BINARY
110 create table t1 (a binary(4), b char(4) character set koi8r);
111 insert into t1 values (_binary'ÔÅÓÔ',_binary'ÔÅÓÔ');
112 select a,b,cast(a as char character set cp1251),cast(b as binary) from t1;
114 select a,b,cast(a as char character set cp1251),cast(b as binary) from t1;
116 select a,b,cast(a as char character set cp1251),cast(b as binary) from t1;
121 # The following should be fixed in 4.1
124 select cast("2001-1-1" as date) = "2001-01-01";
125 select cast("2001-1-1" as datetime) = "2001-01-01 00:00:00";
126 select cast("1:2:3" as TIME) = "1:02:03";
127 select cast(NULL as DATE);
128 select cast(NULL as BINARY);
131 # Bug #5228 ORDER BY CAST(enumcol) sorts incorrectly under certain conditions
133 CREATE TABLE t1 (a enum ('aac','aab','aaa') not null);
134 INSERT INTO t1 VALUES ('aaa'),('aab'),('aac');
135 # these two should be in enum order
136 SELECT a, CAST(a AS CHAR) FROM t1 ORDER BY CAST(a AS UNSIGNED) ;
137 SELECT a, CAST(a AS CHAR(3)) FROM t1 ORDER BY CAST(a AS CHAR(2)), a;
138 # these two should be in alphabetic order
139 SELECT a, CAST(a AS UNSIGNED) FROM t1 ORDER BY CAST(a AS CHAR) ;
140 SELECT a, CAST(a AS CHAR(2)) FROM t1 ORDER BY CAST(a AS CHAR(3)), a;
144 # Test for bug #6914 "Problems using time()/date() output in expressions".
145 # When we are casting datetime value to DATE/TIME we should throw away
146 # time/date parts (correspondingly).
148 select date_add(cast('2004-12-30 12:00:00' as date), interval 0 hour);
149 select timediff(cast('2004-12-30 12:00:00' as time), '12:00:00');
150 # Still we should not throw away "days" part of time value
151 select timediff(cast('1 12:00:00' as time), '12:00:00');
154 # Bug #7036: Casting from string to unsigned would cap value of result at
155 # maximum signed value instead of maximum unsigned value
157 select cast(18446744073709551615 as unsigned);
158 select cast(18446744073709551615 as signed);
159 select cast('18446744073709551615' as unsigned);
160 select cast('18446744073709551615' as signed);
161 select cast('9223372036854775807' as signed);
163 select cast(concat('184467440','73709551615') as unsigned);
164 select cast(concat('184467440','73709551615') as signed);
166 select cast(repeat('1',20) as unsigned);
167 select cast(repeat('1',20) as signed);
170 # Bug #13344: cast of large decimal to signed int not handled correctly
172 select cast(1.0e+300 as signed int);
175 # Bugs: #15098: CAST(column double TO signed int), wrong result
177 CREATE TABLE t1 (f1 double);
178 INSERT INTO t1 SET f1 = -1.0e+30 ;
179 INSERT INTO t1 SET f1 = +1.0e+30 ;
180 # Expected result is +-1e+30, but Windows returns +-1e+030.
181 --replace_result 1e+030 1e+30
182 SELECT f1 AS double_val, CAST(f1 AS SIGNED INT) AS cast_val FROM t1;
186 # Bug #23938: cast(NULL as DATE)
189 select isnull(date(NULL)), isnull(cast(NULL as DATE));
192 # Bug#23656: Wrong result of CAST from DATE to int
194 SELECT CAST(cast('01-01-01' as date) AS UNSIGNED);
195 SELECT CAST(cast('01-01-01' as date) AS SIGNED);
197 --echo End of 4.1 tests
200 #decimal-related additions
201 select cast('1.2' as decimal(3,2));
202 select 1e18 * cast('1.2' as decimal(3,2));
203 select cast(cast('1.2' as decimal(3,2)) as signed);
205 select cast(@v1 as decimal(22, 2));
206 select cast(-1e18 as decimal(22,2));
208 create table t1(s1 time);
209 insert into t1 values ('11:11:11');
210 select cast(s1 as decimal(7,2)) from t1;
214 # Test for bug #11283: field conversion from varchar, and text types to decimal
217 CREATE TABLE t1 (v varchar(10), tt tinytext, t text,
218 mt mediumtext, lt longtext);
219 INSERT INTO t1 VALUES ('1.01', '2.02', '3.03', '4.04', '5.05');
221 SELECT CAST(v AS DECIMAL), CAST(tt AS DECIMAL), CAST(t AS DECIMAL),
222 CAST(mt AS DECIMAL), CAST(lt AS DECIMAL) from t1;
227 # Bug #10237 (CAST(NULL DECIMAL) crashes server)
229 select cast(NULL as decimal(6)) as t1;
233 # Bug #17903: cast to char results in binary
236 select hex(cast('a' as char(2) binary));
237 select hex(cast('a' as binary(2)));
238 select hex(cast('a' as char(2) binary));
241 # Bug#29898: Item_date_typecast::val_int doesn't reset the null_value flag.
243 CREATE TABLE t1 (d1 datetime);
244 INSERT INTO t1(d1) VALUES ('2007-07-19 08:30:00'), (NULL),
245 ('2007-07-19 08:34:00'), (NULL), ('2007-07-19 08:36:00');
246 SELECT cast(date(d1) as signed) FROM t1;
250 # Bug #31990: MINUTE() and SECOND() return bogus results when used on a DATE
253 # Show that HH:MM:SS of a DATE are 0, and that it's the same for columns
254 # and typecasts (NULL in, NULL out).
255 CREATE TABLE t1 (f1 DATE);
256 INSERT INTO t1 VALUES ('2007-07-19'), (NULL);
260 SELECT HOUR(CAST('2007-07-19' AS DATE)),
261 MINUTE(CAST('2007-07-19' AS DATE)),
262 SECOND(CAST('2007-07-19' AS DATE));
263 SELECT HOUR(CAST(NULL AS DATE)),
264 MINUTE(CAST(NULL AS DATE)),
265 SECOND(CAST(NULL AS DATE));
271 --echo End of 5.0 tests
274 --echo # Bug #44766: valgrind error when using convert() in a subquery
277 CREATE TABLE t1(a tinyint);
278 INSERT INTO t1 VALUES (127);
281 SELECT CONVERT(t2.a USING UTF8) FROM t1, t1 t2 LIMIT 1
286 --echo # Bug #11765023: 57934: DOS POSSIBLE SINCE BINARY CASTING
287 --echo # DOESN'T ADHERE TO MAX_ALLOWED_PACKET
289 SET @@GLOBAL.max_allowed_packet=2048;
290 # reconnect to make the new max packet size take effect
291 --connect (newconn, localhost, root,,)
293 SELECT CONVERT('a', BINARY(2049));
294 SELECT CONVERT('a', CHAR(2049));
298 SET @@GLOBAL.max_allowed_packet=default;
301 --echo # Bug#13519724 63793: CRASH IN DTCOLLATION::SET(DTCOLLATION &SET)
304 CREATE TABLE t1 (a VARCHAR(50));
307 WHERE CAST(a as BINARY)=x'62736D697468'
308 AND CAST(a AS BINARY)=x'65736D697468';
313 --echo # Bug#13581962 HIGH MEMORY USAGE ATTEMPT, THEN CRASH WITH
314 --echo # LONGTEXT, UNION, USER VARIABLE
315 --echo # Bug#14096619 UNABLE TO RESTORE DATABASE DUMP
318 CREATE TABLE t1 AS SELECT CONCAT(CAST(REPEAT('9', 1000) AS SIGNED)),
319 CONCAT(CAST(REPEAT('9', 1000) AS UNSIGNED));
320 SHOW CREATE TABLE t1;
323 --echo # End of test for Bug#13581962, Bug#14096619
325 --echo End of 5.1 tests