~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to tests/t/type_datetime.test

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# testing different DATETIME ranges
 
3
#
 
4
 
 
5
--disable_warnings
 
6
drop table if exists t1;
 
7
--enable_warnings
 
8
 
 
9
create table t1 (t datetime);
 
10
insert into t1 values (101);
 
11
insert into t1 values (691231);
 
12
insert into t1 values (700101);
 
13
insert into t1 values (991231);
 
14
insert into t1 values (10000101);
 
15
insert into t1 values (99991231);
 
16
insert into t1 values (101000000);
 
17
insert into t1 values (691231000000);
 
18
insert into t1 values (700101000000);
 
19
insert into t1 values (991231235959);
 
20
insert into t1 values (10000101000000);
 
21
insert into t1 values (99991231235959);
 
22
--error 1686 # Bad datetime
 
23
insert into t1 values (20030100000000);
 
24
--error 1686 # Bad datetime
 
25
insert into t1 values (20030000000000);
 
26
select * from t1;
 
27
--error 1686 # Bad datetime
 
28
delete from t1 where t > 0;
 
29
# try earliest datetime end point which is actually a friggin datetime, not a 0.
 
30
delete from t1 where t > "0001-01-01 00:00:00";
 
31
alter table t1 engine="default";
 
32
check table t1;
 
33
delete from t1;
 
34
insert into t1 values("000101");
 
35
insert into t1 values("691231");
 
36
insert into t1 values("700101");
 
37
insert into t1 values("991231");
 
38
--error 1686 # Bad datetime
 
39
insert into t1 values("00000101");
 
40
insert into t1 values("00010101");
 
41
insert into t1 values("99991231");
 
42
--error 1686 # Bad datetime
 
43
insert into t1 values("00101000000");
 
44
--error 1686 # Bad datetime
 
45
insert into t1 values("691231000000");
 
46
--error 1686 # Bad datetime
 
47
insert into t1 values("700101000000");
 
48
--error 1686 # Bad datetime
 
49
insert into t1 values("991231235959");
 
50
insert into t1 values("10000101000000");
 
51
insert into t1 values("99991231235959");
 
52
--error 1686 # Bad datetime
 
53
insert into t1 values("20030100000000");
 
54
--error 1686 # Bad datetime
 
55
insert into t1 values("20030000000000");
 
56
 
 
57
# Strange dates
 
58
--error 1686 # Bad datetime
 
59
insert into t1 values ("2003-003-03");
 
60
 
 
61
# Bug #7308: ISO-8601 date format not handled correctly
 
62
--error 1686 # Bad datetime
 
63
insert into t1 values ("20030102T131415");
 
64
insert into t1 values ("2001-01-01T01:01:01");
 
65
--error 1686 # Bad datetime
 
66
insert into t1 values ("2001-1-1T1:01:01");
 
67
select * from t1;
 
68
 
 
69
# Test some wrong dates
 
70
truncate table t1;
 
71
--error 1686 # Bad datetime
 
72
insert into t1 values("2003-0303 12:13:14");
 
73
select * from t1;
 
74
drop table t1;
 
75
 
 
76
#
 
77
# Test of datetime and not null
 
78
#
 
79
 
 
80
CREATE TABLE t1 (a datetime not null);
 
81
insert into t1 values ("2009-02-11 00:00:00");
 
82
select * from t1 where a is null;
 
83
drop table t1;
 
84
 
 
85
#
 
86
# Test with bug when propagating DATETIME to integer and WHERE optimization
 
87
#
 
88
 
 
89
create table t1 (id int, dt datetime);
 
90
insert into t1 values (1,"2001-08-14 00:00:00"),(2,"2001-08-15 00:00:00"),(3,"2001-08-16 00:00:00"),(4,"2003-09-15 01:20:30");
 
91
select * from t1 where dt='2001-08-14 00:00:00' and dt =  if(id=1,'2001-08-14 00:00:00','1999-08-15');
 
92
create index dt on t1 (dt);
 
93
select * from t1 where dt > 20021020;
 
94
select * from t1 ignore index (dt) where dt > 20021020;
 
95
drop table t1;
 
96
 
 
97
#
 
98
# Test of datetime optimization
 
99
#
 
100
 
 
101
CREATE TEMPORARY TABLE `t1` (
 
102
  `date` datetime,
 
103
  `numfacture` int NOT NULL default '0',
 
104
  `expedition` datetime,
 
105
  PRIMARY KEY  (`numfacture`),
 
106
  KEY `date` (`date`),
 
107
  KEY `expedition` (`expedition`)
 
108
) ENGINE=MyISAM;
 
109
 
 
110
INSERT INTO t1 (expedition) VALUES ('0001-01-01 00:00:00');
 
111
SELECT * FROM t1 WHERE expedition='0001-01-01 00:00:00';
 
112
INSERT INTO t1 (numfacture,expedition) VALUES ('1212','0001-01-01 00:00:00');
 
113
SELECT * FROM t1 WHERE expedition='0001-01-01 00:00:00';
 
114
EXPLAIN SELECT * FROM t1 WHERE expedition='0001-01-01 00:00:00';
 
115
drop table t1;
 
116
create table t1 (a datetime not null, b datetime not null);
 
117
insert into t1 values (now(), now());
 
118
insert into t1 values (now(), now());
 
119
select * from t1 where a is null or b is null;
 
120
drop table t1;
 
121
 
 
122
#
 
123
# Let us check if we properly treat wrong datetimes and produce proper
 
124
# warnings (for both strings and numbers)
 
125
#
 
126
create table t1 (t datetime);
 
127
--error 1686 # Bad datetime
 
128
insert into t1 values (20030102030460),(20030102036301),(20030102240401),
 
129
                      (20030132030401),(20031302030401),(100001202030401);
 
130
select * from t1;
 
131
delete from t1;
 
132
--error 1686 # Bad datetime
 
133
insert into t1 values
 
134
  ("2003-01-02 03:04:60"),("2003-01-02 03:63:01"),("2003-01-02 24:04:01"),
 
135
  ("2003-01-32 03:04:01"),("2003-13-02 03:04:01"), ("10000-12-02 03:04:00");
 
136
select * from t1;
 
137
delete from t1;
 
138
--error 1686 # Bad datetime
 
139
insert into t1 values ("0000-00-00 00:00:00 some trailer"),("2003-01-01 00:00:00 some trailer");
 
140
select * from t1 order by t;
 
141
drop table t1;
 
142
 
 
143
#
 
144
# Test for bug #7297 "Two digit year should be interpreted correctly even
 
145
# with zero month and day"
 
146
#
 
147
create table t1 (dt datetime);
 
148
# These dates should be treated as dates in 21st century
 
149
insert into t1 values ("12-01-01"), ("01-01-01 01:00:00");
 
150
# Zero dates are still special :/
 
151
--error 1686 # Yeah special uh no its wrong
 
152
insert into t1 values ("00-00-00"), ("00-00-00 00:00:00");
 
153
select * from t1;
 
154
drop table t1;
 
155
 
 
156
#
 
157
# Bug #16546 DATETIME+0 not always coerced the same way 
 
158
#
 
159
select cast('2006-12-05 22:10:10' as datetime) + 0;
 
160
 
 
161
 
 
162
# End of 4.1 tests
 
163
 
 
164
#
 
165
# Bug#21475: Wrongly applied constant propagation leads to a false comparison.
 
166
#
 
167
CREATE TABLE t1(a DATETIME NOT NULL);
 
168
INSERT INTO t1 VALUES ('20060606155555');
 
169
SELECT a FROM t1 WHERE a=(SELECT MAX(a) FROM t1) AND (a="20060606155555");
 
170
DROP TABLE t1;
 
171
 
 
172
 
 
173
#
 
174
# Bug 19491 (CAST DATE AS DECIMAL returns incorrect result
 
175
#
 
176
SELECT CAST(CAST('2006-08-10' AS DATE) AS DECIMAL(20,6));
 
177
SELECT CAST(CAST('2006-08-10 10:11:12' AS DATETIME) AS DECIMAL(20,6));
 
178
SELECT CAST(CAST('2006-08-10 10:11:12' AS DATETIME) + INTERVAL 14 MICROSECOND AS DECIMAL(20,6));
 
179
 
 
180
 
 
181
#
 
182
# Test of storing datetime into date fields
 
183
#
 
184
 
 
185
create table t1 (da date default '1962-03-03 23:33:34', dt datetime default '1962-03-03');
 
186
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
 
187
show create table t1;
 
188
insert into t1 values ();
 
189
insert into t1 values ('2007-03-23 13:49:38','2007-03-23 13:49:38');
 
190
insert into t1 values ('2007-03-23 13:49:38','2007-03-23 13:49:38');
 
191
insert into t1 set dt='2007-03-23 13:49:38',da=dt;
 
192
# Test error handling
 
193
--error 1686 # Bad date
 
194
insert into t1 values ('2007-03-32','2007-03-23 13:49:38');
 
195
select * from t1;
 
196
drop table t1;
 
197
--error 1686 # Bad datetime
 
198
create table t1 (da date default '1962-03-32 23:33:34', dt datetime default '1962-03-03');
 
199
 
 
200
 
 
201
#
 
202
# Bug#16377: Wrong DATE/DATETIME comparison in BETWEEN function.
 
203
#
 
204
create table t1 (f1 date, f2 datetime, f3 timestamp);
 
205
insert into t1 values('2001-01-01','2001-01-01 01:01:01','2001-01-01 01:01:01');
 
206
insert into t1 values('2001-02-05','2001-02-05 00:00:00','2001-02-05 01:01:01');
 
207
insert into t1 values('2001-03-10','2001-03-09 01:01:01','2001-03-10 01:01:01');
 
208
insert into t1 values('2001-04-15','2001-04-15 00:00:00','2001-04-15 00:00:00');
 
209
insert into t1 values('2001-05-20','2001-05-20 01:01:01','2001-05-20 01:01:01');
 
210
select f2 from t1 where f2 between '2001-2-5' and '01-04-14';
 
211
select f1, f2, f3 from t1 where f1 between f2 and f3;
 
212
select f1, f2, f3 from t1 where cast(f1 as datetime) between f2 and
 
213
  cast(f3 as date);
 
214
select f2 from t1 where '2001-04-10 12:34:56' between f2 and '01-05-01';
 
215
select f2, f3 from t1 where '01-03-10' between f2 and f3;
 
216
select f2 from t1 where DATE(f2) between "2001-4-15" AND "01-4-15";
 
217
drop table t1;
 
218
 
 
219
#
 
220
# Bug#28133: Wrong DATE/DATETIME comparison in IN() function.
 
221
#
 
222
create table t1 (f1 date);
 
223
insert into t1 values('01-01-01'),('01-01-02'),('01-01-03');
 
224
select * from t1 where f1 in ('01-01-01','2001-01-02','2001-01-03 00:00:00');
 
225
create table t2(f2 datetime);
 
226
insert into t2 values('01-01-01 00:00:00'),('01-02-03 12:34:56'),('02-04-06 11:22:33');
 
227
select * from t2 where f2 in ('01-01-01','01-02-03 12:34:56','01-02-03');
 
228
select * from t1,t2 where '01-01-02' in (f1, cast(f2 as date));
 
229
select * from t1,t2 where '01-01-01' in (f1, '01-02-03');
 
230
select * from t1,t2 where if(1,'01-02-03 12:34:56','') in (f1, f2);
 
231
create table t3(f3 varchar(20));
 
232
insert into t3 select * from t2;
 
233
select * from t2,t3 where f2 in (f3,'03-04-05');
 
234
select f1,f2,f3 from t1,t2,t3 where (f1,'1') in ((f2,'1'),(f3,'1'));
 
235
select f1 from t1 where ('1',f1) in (('1','01-01-01'),('1','2001-1-1 0:0:0'),('1','02-02-02'));
 
236
drop table t1,t2,t3;
 
237
 
 
238
#
 
239
# Bug#27759: Wrong DATE/DATETIME comparison in LEAST()/GREATEST() functions.
 
240
#
 
241
select least(cast('01-01-01' as date), '01-01-02');
 
242
select greatest(cast('01-01-01' as date), '01-01-02');
 
243
select least(cast('01-01-01' as date), '01-01-02') + 0;
 
244
select greatest(cast('01-01-01' as date), '01-01-02') + 0;
 
245
select least(cast('01-01-01' as datetime), '01-01-02') + 0;
 
246
select cast(least(cast('01-01-01' as datetime), '01-01-02') as decimal(16,2));
 
247
#
 
248
#
 
249
# Bug#28208: Wrong result of a non-const STRING function with a const
 
250
#            DATETIME function.
 
251
#
 
252
create table t1 (f1 date);
 
253
insert into t1 values (curdate());
 
254
select left(f1,10) = curdate() from t1;
 
255
drop table t1;
 
256
 
 
257
#
 
258
# Bug#28261: Wrong DATETIME comparison result when the GET_USER_VAR function
 
259
#            is involved.
 
260
#
 
261
create table t1(f1 date);
 
262
insert into t1 values('01-01-01'),('02-02-02'),('01-01-01'),('02-02-02');
 
263
set @bug28261='';
 
264
select if(@bug28261 = f1, '', @bug28261:= f1) from t1;
 
265
select if(@bug28261 = f1, '', @bug28261:= f1) from t1;
 
266
select if(@bug28261 = f1, '', @bug28261:= f1) from t1;
 
267
drop table t1;
 
268
 
 
269
#
 
270
# Bug#28778: Wrong result of BETWEEN when comparing a DATETIME field with an
 
271
#            integer constants.
 
272
#
 
273
create table t1(f1 datetime);
 
274
insert into t1 values('2001-01-01'),('2002-02-02');
 
275
select * from t1 where f1 between 20020101 and 20070101000000;
 
276
--error 1686 # 2002010 is not a datetime.
 
277
select * from t1 where f1 between 2002010 and 20070101000000;
 
278
--error 1686 # 2007010100000 is not a datetime.
 
279
select * from t1 where f1 between 20020101 and 2007010100000;
 
280
drop table t1;
 
281
 
 
282
--echo #
 
283
--echo # Bug#27216: functions with parameters of different date types may
 
284
--echo #            return wrong type of the result.
 
285
--echo #
 
286
create table t1 (f1 date, f2 datetime, f3 varchar(20));
 
287
create table t2 as select coalesce(f1,f1) as f4 from t1;
 
288
desc t2;
 
289
create table t3 as select coalesce(f1,f2) as f4 from t1;
 
290
desc t3;
 
291
create table t4 as select coalesce(f2,f2) as f4 from t1;
 
292
desc t4;
 
293
create table t5 as select coalesce(f1,f3) as f4 from t1;
 
294
desc t5;
 
295
create table t6 as select coalesce(f2,f3) as f4 from t1;
 
296
desc t6;
 
297
create table t7 as select coalesce(makedate(1997,1),f2) as f4 from t1;
 
298
desc t7;
 
299
create table t8 as select coalesce(cast('01-01-01' as datetime),f2) as f4
 
300
  from t1;
 
301
desc t8;
 
302
create table t9 as select case when 1 then cast('01-01-01' as date)
 
303
  when 0 then cast('01-01-01' as date) end as f4 from t1;
 
304
desc t9;
 
305
create table t10 as select case when 1 then cast('01-01-01' as datetime)
 
306
  when 0 then cast('01-01-01' as datetime) end as f4 from t1;
 
307
desc t10;
 
308
create table t11 as select if(1, cast('01-01-01' as datetime),
 
309
  cast('01-01-01' as date)) as f4 from t1;
 
310
desc t11;
 
311
create table t12 as select least(cast('01-01-01' as datetime),
 
312
  cast('01-01-01' as date)) as f4 from t1;
 
313
desc t12;
 
314
create table t13 as select ifnull(cast('01-01-01' as datetime),
 
315
  cast('01-01-01' as date)) as f4 from t1;
 
316
desc t13;
 
317
drop tables t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13;
 
318
--echo ###################################################################
 
319
 
 
320
#
 
321
# Bug #31249: problem with convert(..., datetime)
 
322
#
 
323
create table t1 (a int);
 
324
insert into t1 values (), (), ();
 
325
select sum(a) from t1 group by convert(a, datetime);
 
326
drop table t1;
 
327
 
 
328
#
 
329
# Bug #32694: NOT NULL table field in a subquery produces invalid results
 
330
#
 
331
create table t1 (id int not null, cur_date datetime not null);
 
332
create table t2 (id int not null, cur_date date not null);
 
333
insert into t1 (id, cur_date) values (1, '2007-04-25 18:30:22');
 
334
insert into t2 (id, cur_date) values (1, '2007-04-25');
 
335
 
 
336
explain extended
 
337
select * from t2
 
338
where id in (select id from t2 as x1 where (t2.cur_date is null));
 
339
select * from t2
 
340
where id in (select id from t2 as x1 where (t2.cur_date is null));
 
341
 
 
342
insert into t1 (id, cur_date) values (2, '2007-04-26 18:30:22');
 
343
insert into t2 (id, cur_date) values (2, '2007-04-26');
 
344
 
 
345
explain extended
 
346
select * from t1
 
347
where id in (select id from t1 as x1 where (t1.cur_date is null));
 
348
select * from t1
 
349
where id in (select id from t1 as x1 where (t1.cur_date is null));
 
350
 
 
351
explain extended
 
352
select * from t2
 
353
where id in (select id from t2 as x1 where (t2.cur_date is null));
 
354
select * from t2
 
355
where id in (select id from t2 as x1 where (t2.cur_date is null));
 
356
drop table t1,t2;
 
357
 
 
358
--echo End of 5.0 tests
 
359
#
 
360
# Test of storing datetime into date fields
 
361
#
 
362
 
 
363
create table t1 (da date default '1962-03-03 23:33:34', dt datetime default '1962-03-03');
 
364
--replace_regex /ENGINE=[a-zA-Z]+/ENGINE=DEFAULT/
 
365
show create table t1;
 
366
insert into t1 values ();
 
367
insert into t1 values ('2007-03-23 13:49:38','2007-03-23 13:49:38');
 
368
insert into t1 values ('2007-03-23 13:49:38','2007-03-23 13:49:38');
 
369
insert into t1 set dt='2007-03-23 13:49:38',da=dt;
 
370
# Test error handling
 
371
--error 1686 # Bad date
 
372
insert into t1 values ('2007-03-32','2007-03-23 13:49:38');
 
373
select * from t1;
 
374
drop table t1;
 
375
--error 1686 # Bad date
 
376
create table t1 (da date default '1962-03-32 23:33:34', dt datetime default '1962-03-03');