summaryrefslogtreecommitdiffstats
path: root/mysql-test/main/timezone2.test
blob: b504520390333fffaf162ebf38614325a8514f09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# This script tests our own time zone support functions

# Preparing playground
--disable_warnings
drop table if exists t1, t2;
drop function if exists f1;
--enable_warnings


# 
# Let us first check +HH:MM style timezones
#
create table t1 (ts timestamp);

#enable after fix MDEV-27871
--disable_view_protocol

set time_zone='+00:00';
select unix_timestamp(utc_timestamp())-unix_timestamp(current_timestamp());
insert into t1 (ts) values ('2003-03-30 02:30:00');

set time_zone='+10:30';
select unix_timestamp(utc_timestamp())-unix_timestamp(current_timestamp());
insert into t1 (ts) values ('2003-03-30 02:30:00');

set time_zone='-10:00';
select unix_timestamp(utc_timestamp())-unix_timestamp(current_timestamp());
insert into t1 (ts) values ('2003-03-30 02:30:00');

# Here we will get different results
select * from t1;

--enable_view_protocol

drop table t1;


# 
# Let us try DB specified time zones
#
select Name from mysql.time_zone_name where Name in 
  ('UTC','Universal','MET','Europe/Moscow','leap/Europe/Moscow');

create table t1 (i int, ts timestamp);

set time_zone='MET';

# We check common date time value and non existent or ambiguios values
# Normal value without DST
insert into t1 (i, ts) values
  (unix_timestamp('2003-03-01 00:00:00'),'2003-03-01 00:00:00');
# Values around and in spring time-gap
insert into t1 (i, ts) values
  (unix_timestamp('2003-03-30 01:59:59'),'2003-03-30 01:59:59'),
  (unix_timestamp('2003-03-30 02:30:00'),'2003-03-30 02:30:00'),
  (unix_timestamp('2003-03-30 03:00:00'),'2003-03-30 03:00:00');
# Values around and in spring time-gap
insert into t1 (i, ts) values
  (unix_timestamp(20030330015959),20030330015959),
  (unix_timestamp(20030330023000),20030330023000),
  (unix_timestamp(20030330030000),20030330030000);
# Normal value with DST
insert into t1 (i, ts) values
  (unix_timestamp('2003-05-01 00:00:00'),'2003-05-01 00:00:00');
# Ambiguos values (also check for determenism)
insert into t1 (i, ts) values
  (unix_timestamp('2003-10-26 01:00:00'),'2003-10-26 01:00:00'),
  (unix_timestamp('2003-10-26 02:00:00'),'2003-10-26 02:00:00'),
  (unix_timestamp('2003-10-26 02:59:59'),'2003-10-26 02:59:59'),
  (unix_timestamp('2003-10-26 04:00:00'),'2003-10-26 04:00:00'),
  (unix_timestamp('2003-10-26 02:59:59'),'2003-10-26 02:59:59');

set time_zone='UTC';

select * from t1;

truncate table t1;

# Simple check for 'Europe/Moscow' time zone just for showing that it works
set time_zone='Europe/Moscow';
insert into t1 (i, ts) values
  (unix_timestamp('2004-01-01 00:00:00'),'2004-01-01 00:00:00'),
  (unix_timestamp('2004-03-28 02:30:00'),'2004-03-28 02:30:00'),
  (unix_timestamp('2004-08-01 00:00:00'),'2003-08-01 00:00:00'),
  (unix_timestamp('2004-10-31 02:30:00'),'2004-10-31 02:30:00');
select * from t1;
truncate table t1;


#
# Check for time zone with leap seconds
# Values in ts column must be the same but values in i column should
# differ from corresponding values for Europe/Moscow a bit.
#
set time_zone='leap/Europe/Moscow';
insert into t1 (i, ts) values
  (unix_timestamp('2004-01-01 00:00:00'),'2004-01-01 00:00:00'),
  (unix_timestamp('2004-03-28 02:30:00'),'2004-03-28 02:30:00'),
  (unix_timestamp('2004-08-01 00:00:00'),'2003-08-01 00:00:00'),
  (unix_timestamp('2004-10-31 02:30:00'),'2004-10-31 02:30:00');
select * from t1;
truncate table t1;
# Let us test leap jump
insert into t1 (i, ts) values
  (unix_timestamp('1981-07-01 03:59:59'),'1981-07-01 03:59:59'),
  (unix_timestamp('1981-07-01 04:00:00'),'1981-07-01 04:00:00');
select * from t1;
# Additional 60ieth second!
select from_unixtime(362793609);

drop table t1;


# 
# Let us test range for TIMESTAMP
#
create table t1 (ts timestamp);
set time_zone='UTC';
insert into t1 values ('0000-00-00 00:00:00'),('1969-12-31 23:59:59'),
                      ('1970-01-01 00:00:00'),('1970-01-01 00:00:01'),
                      ('2038-01-19 03:14:07'),('2038-01-19 03:14:08');
select * from t1;
truncate table t1;
# MET time zone has range shifted by one hour
set time_zone='MET';
insert into t1 values ('0000-00-00 00:00:00'),('1970-01-01 00:30:00'),
                      ('1970-01-01 01:00:00'),('1970-01-01 01:00:01'),
                      ('2038-01-19 04:14:07'),('2038-01-19 04:14:08');
select * from t1;
truncate table t1;
# same for +01:30 time zone
set time_zone='+01:30';
insert into t1 values ('0000-00-00 00:00:00'),('1970-01-01 01:00:00'),
                      ('1970-01-01 01:30:00'),('1970-01-01 01:30:01'),
                      ('2038-01-19 04:44:07'),('2038-01-19 04:44:08');
select * from t1;

drop table t1;


# 
# Test of show variables
#
show variables like 'time_zone';
set time_zone = default;
show variables like 'time_zone';


# 
# Let us try some invalid time zone specifications
#
--error 1298
set time_zone= '0';
--error 1298
set time_zone= '0:0';
--error 1298
set time_zone= '-20:00';
--error 1298
set time_zone= '+20:00';
--error 1298
set time_zone= 'Some/Unknown/Time/Zone';


# Let us check that aliases for time zones work and they are 
# case-insensitive
select convert_tz(now(),'UTC', 'Universal') = now();
select convert_tz(now(),'utc', 'UTC') = now();


# 
# Let us test CONVERT_TZ function (may be func_time.test is better place).
#
select convert_tz('1917-11-07 12:00:00', 'MET', 'UTC'); 
select convert_tz('1970-01-01 01:00:00', 'MET', 'UTC'); 
select convert_tz('1970-01-01 01:00:01', 'MET', 'UTC'); 
select convert_tz('2003-03-01 00:00:00', 'MET', 'UTC');
select convert_tz('2003-03-30 01:59:59', 'MET', 'UTC');
select convert_tz('2003-03-30 02:30:00', 'MET', 'UTC');
select convert_tz('2003-03-30 03:00:00', 'MET', 'UTC');
select convert_tz('2003-05-01 00:00:00', 'MET', 'UTC');
select convert_tz('2003-10-26 01:00:00', 'MET', 'UTC');
select convert_tz('2003-10-26 02:00:00', 'MET', 'UTC');
select convert_tz('2003-10-26 02:59:59', 'MET', 'UTC');
select convert_tz('2003-10-26 04:00:00', 'MET', 'UTC');
select convert_tz('2038-01-19 04:14:07', 'MET', 'UTC');
select convert_tz('2038-01-19 04:14:08', 'MET', 'UTC');
select convert_tz('2103-01-01 04:00:00', 'MET', 'UTC');

# Let us test variable time zone argument
create table t1 (tz varchar(3));
insert into t1 (tz) values ('MET'), ('UTC');
select tz, convert_tz('2003-12-31 00:00:00',tz,'UTC'), convert_tz('2003-12-31 00:00:00','UTC',tz) from t1 order by tz;
drop table t1;

#enable after fix MDEV-27871
--disable_view_protocol

# Parameters to CONVERT_TZ() what should give NULL
select convert_tz('2003-12-31 04:00:00', NULL, 'UTC');
select convert_tz('2003-12-31 04:00:00', 'SomeNotExistingTimeZone', 'UTC');
select convert_tz('2003-12-31 04:00:00', 'MET', 'SomeNotExistingTimeZone');
select convert_tz('2003-12-31 04:00:00', 'MET', NULL);
select convert_tz( NULL, 'MET', 'UTC');

--enable_view_protocol

#
# Test for bug #4508 "CONVERT_TZ() function with new time zone as param
# crashes server." (Was caused by improperly worked mechanism of time zone
# dynamical loading).
#
create table t1 (ts timestamp);
set timestamp=1000000000;
insert into t1 (ts) values (now());
select convert_tz(ts, @@time_zone, 'Japan') from t1;
drop table t1;

#
# Test for bug #7705 "CONVERT_TZ() crashes with subquery/WHERE on index
# column". Queries in which one of time zone arguments of CONVERT_TZ() is
# determined as constant only at val() stage (not at fix_fields() stage),
# should not crash server.
#
select convert_tz('2005-01-14 17:00:00', 'UTC', custTimeZone) from (select 'UTC' as custTimeZone) as tmp;

#
# Test for bug #7899 "CREATE TABLE .. SELECT .. and CONVERT_TZ() function
# does not work well together". The following statement should return only
# one NULL row and not result of full join.
#
create table t1 select convert_tz(NULL, NULL, NULL);
select * from t1;
drop table t1;

# End of 4.1 tests

#
# Test for bug #11081 "Using a CONVERT_TZ function in a stored function
# or trigger fails".
#
SET @old_log_bin_trust_function_creators = @@global.log_bin_trust_function_creators;
SET GLOBAL log_bin_trust_function_creators = 1;

create table t1 (ldt datetime, udt datetime);
create function f1(i datetime) returns datetime
  return convert_tz(i, 'UTC', 'Europe/Moscow');
create trigger t1_bi before insert on t1 for each row
  set new.udt:= convert_tz(new.ldt, 'Europe/Moscow', 'UTC');
# This should work without errors
insert into t1 (ldt) values ('2006-04-19 16:30:00');
select * from t1;
# This should work without errors as well
select ldt, f1(udt) as ldt2 from t1;
drop table t1;
drop function f1;

SET @@global.log_bin_trust_function_creators= @old_log_bin_trust_function_creators;

# End of 5.0 tests


#
# BUG#9953: CONVERT_TZ requires mysql.time_zone_name to be locked
# BUG#19339: CONVERT_TZ(): overly aggressive in locking time_zone_name
# table
#
--disable_service_connection
--disable_warnings
DROP TABLE IF EXISTS t1;
--enable_warnings

CREATE TABLE t1 (t TIMESTAMP);
INSERT INTO t1 VALUES (NULL), (NULL);

LOCK TABLES t1 WRITE;

# The following two queries should not return error that time zone
# tables aren't locked.  We use IS NULL below to supress timestamp
# result.
SELECT CONVERT_TZ(NOW(), 'UTC', 'Europe/Moscow') IS NULL;
UPDATE t1 SET t = CONVERT_TZ(t, 'UTC', 'Europe/Moscow');

UNLOCK TABLES;

DROP TABLE t1;
--enable_service_connection

--echo #
--echo # Bug #55424: convert_tz crashes when fed invalid data
--echo #

CREATE TABLE t1 (a SET('x') NOT NULL);
INSERT INTO t1 VALUES ('');
SELECT CONVERT_TZ(1, a, 1) FROM t1;
SELECT CONVERT_TZ(1, 1, a) FROM t1;
DROP TABLE t1;

--echo End of 5.1 tests


--echo #
--echo # Start of 5.3 tests
--echo #

--echo #
--echo # MDEV-4653 Wrong result for CONVERT_TZ(TIME('00:00:00'),'+00:00','+7:5')
--echo #

SET timestamp=unix_timestamp('2001-02-03 10:20:30');
SELECT CONVERT_TZ(TIME('00:00:00'),'+00:00','+7:5');
SELECT CONVERT_TZ(TIME('2010-01-01 00:00:00'),'+00:00','+7:5');
SET timestamp=DEFAULT;

--echo #
--echo # MDEV-5506 safe_mutex: Trying to lock unitialized mutex at safemalloc.c on server shutdown after SELECT with CONVERT_TZ
--echo #

#enable after fix MDEV-27871
--disable_view_protocol

SELECT CONVERT_TZ('2001-10-08 00:00:00', MAKE_SET(0,'+01:00'), '+00:00' ); 

--enable_view_protocol

--echo #
--echo # End of 5.3 tests
--echo #

--echo #
--echo # Start of 10.1 tests
--echo #

--echo #
--echo # MDEV-11895 NO_ZERO_DATE affects timestamp values without any warnings
--echo #

SET sql_mode = '';
CREATE TABLE t1 (a TIMESTAMP NULL) ENGINE = MyISAM;
CREATE TABLE t2 (a TIMESTAMP NULL) ENGINE = MyISAM;
CREATE TABLE t3 (a TIMESTAMP NULL) ENGINE = MyISAM;

SET @@session.time_zone = 'UTC';
INSERT INTO t1 VALUES ('2011-10-29 23:00:00');
INSERT INTO t1 VALUES ('2011-10-29 23:00:01');
INSERT INTO t1 VALUES ('2011-10-29 23:59:59');

SET @@session.time_zone = 'Europe/Moscow';
SET sql_mode='NO_ZERO_DATE';
INSERT INTO t2 SELECT * FROM t1;
SET sql_mode='';
INSERT INTO t3 SELECT * FROM t1;
SELECT UNIX_TIMESTAMP(a), a FROM t2;
SELECT UNIX_TIMESTAMP(a), a FROM t3;
DROP TABLE t1, t2, t3;

--echo #
--echo # End of 10.1 tests
--echo #

--echo #
--echo # Start of 10.4 tests
--echo #

--echo #
--echo # MDEV-17203 Move fractional second truncation from Item_xxx_typecast::get_date() to Time and Datetime constructors
--echo # (an addition for the test for MDEV-4653)

SET timestamp=unix_timestamp('2001-02-03 10:20:30');
SET old_mode=ZERO_DATE_TIME_CAST;
SELECT CONVERT_TZ(TIME('00:00:00'),'+00:00','+7:5');
SELECT CONVERT_TZ(TIME('2010-01-01 00:00:00'),'+00:00','+7:5');
SET old_mode=DEFAULT;
SET timestamp=DEFAULT;

--echo #
--echo # MDEV-13995 MAX(timestamp) returns a wrong result near DST change
--echo #

# MAX()
SET time_zone='+00:00';
CREATE TABLE t1 (a TIMESTAMP);
INSERT INTO t1 VALUES (FROM_UNIXTIME(1288477526)      /*summer time in Moscow*/);
INSERT INTO t1 VALUES (FROM_UNIXTIME(1288477526+3599) /*winter time in Moscow*/);
SET time_zone='Europe/Moscow';
SELECT a, UNIX_TIMESTAMP(a) FROM t1;
SELECT UNIX_TIMESTAMP(MAX(a)) AS a FROM t1;
CREATE TABLE t2 (a TIMESTAMP);
INSERT INTO t2 SELECT MAX(a) AS a FROM t1;
SELECT a, UNIX_TIMESTAMP(a) FROM t2;
DROP TABLE t2;
DROP TABLE t1;


# Comparison
SET time_zone='+00:00';
CREATE TABLE t1 (a TIMESTAMP);
CREATE TABLE t2 (a TIMESTAMP);
INSERT INTO t1 VALUES (FROM_UNIXTIME(1288477526)      /*summer time in Moscow*/);
INSERT INTO t2 VALUES (FROM_UNIXTIME(1288477526+3599) /*winter time in Moscow*/);
SET time_zone='Europe/Moscow';
SELECT UNIX_TIMESTAMP(t1.a), UNIX_TIMESTAMP(t2.a) FROM t1,t2;
SELECT * FROM t1,t2 WHERE t1.a < t2.a;
DROP TABLE t1,t2;


# SP variable comparison
DELIMITER $$;
BEGIN NOT ATOMIC
  DECLARE a,b TIMESTAMP;
  SET time_zone='+00:00';
  SET a=FROM_UNIXTIME(1288477526);
  SET b=FROM_UNIXTIME(1288481125);
  SELECT a < b;
  SET time_zone='Europe/Moscow';
  SELECT a < b;
END;
$$
DELIMITER ;$$


# SP function comparison
DELIMITER $$;
CREATE OR REPLACE FUNCTION f1(uts INT) RETURNS TIMESTAMP
BEGIN
  DECLARE ts TIMESTAMP;
  DECLARE tz VARCHAR(64) DEFAULT @@time_zone;
  SET time_zone='+00:00';
  SET ts=FROM_UNIXTIME(uts);
  SET time_zone=tz;
  RETURN ts;
END;
$$
DELIMITER ;$$
SET time_zone='+00:00';
SELECT f1(1288477526) < f1(1288481125);
SET time_zone='Europe/Moscow';
SELECT f1(1288477526) < f1(1288481125);
DROP FUNCTION f1;


# LEAST()
CREATE TABLE t1 (a TIMESTAMP,b TIMESTAMP);
SET time_zone='+00:00';
INSERT INTO t1 VALUES (FROM_UNIXTIME(1288477526) /*summer time in Mowcow*/,
                       FROM_UNIXTIME(1288481125) /*winter time in Moscow*/);
SELECT *, LEAST(a,b) FROM t1;
SET time_zone='Europe/Moscow';
SELECT *, LEAST(a,b) FROM t1;
SELECT UNIX_TIMESTAMP(a), UNIX_TIMESTAMP(b), UNIX_TIMESTAMP(LEAST(a,b)) FROM t1;
DROP TABLE t1;


# BETWEEN
CREATE TABLE t1 (a TIMESTAMP,b TIMESTAMP,c TIMESTAMP);
SET time_zone='+00:00';
INSERT INTO t1 VALUES (
  FROM_UNIXTIME(1288477526) /*summer time in Moscow*/,
  FROM_UNIXTIME(1288481125) /*winter time in Moscow*/,
  FROM_UNIXTIME(1288481126) /*winter time in Moscow*/);
SELECT b BETWEEN a AND c FROM t1;
SET time_zone='Europe/Moscow';
SELECT b BETWEEN a AND c FROM t1;
DROP TABLE t1;


# ORDER BY
SET time_zone='+00:00';
CREATE TABLE t1 (a TIMESTAMP);
INSERT INTO t1 VALUES (FROM_UNIXTIME(1288477526) /*summer time in Mowcow*/);
INSERT INTO t1 VALUES (FROM_UNIXTIME(1288481125) /*winter time in Moscow*/);
SELECT a, UNIX_TIMESTAMP(a) FROM t1 ORDER BY a;
SELECT COALESCE(a) AS a, UNIX_TIMESTAMP(a) FROM t1 ORDER BY a;
SET time_zone='Europe/Moscow';
SELECT a, UNIX_TIMESTAMP(a) FROM t1 ORDER BY a;
SELECT COALESCE(a) AS a, UNIX_TIMESTAMP(a) FROM t1 ORDER BY a;
DROP TABLE t1;


# GROUP BY
SET time_zone='+00:00';
CREATE TABLE t1 (a TIMESTAMP);
INSERT INTO t1 VALUES (FROM_UNIXTIME(1288477526) /*summer time in Mowcow*/);
INSERT INTO t1 VALUES (FROM_UNIXTIME(1288481126) /*winter time in Moscow*/);
SET time_zone='Europe/Moscow';
SELECT a, UNIX_TIMESTAMP(a) FROM t1 GROUP BY a;
DROP TABLE t1;


# CASE
SET time_zone='+00:00';
CREATE TABLE t1 (a TIMESTAMP, b TIMESTAMP);
INSERT INTO t1 VALUES (FROM_UNIXTIME(1288477526),FROM_UNIXTIME(1288481126));
SELECT UNIX_TIMESTAMP(a),UNIX_TIMESTAMP(b),CASE a WHEN b THEN 'eq' ELSE 'ne' END AS x FROM t1;
SET time_zone='Europe/Moscow';
SELECT UNIX_TIMESTAMP(a),UNIX_TIMESTAMP(b),CASE a WHEN b THEN 'eq' ELSE 'ne' END AS x FROM t1;
DROP TABLE t1;


# IN
SET time_zone='+00:00';
CREATE TABLE t1 (a TIMESTAMP, b TIMESTAMP,c TIMESTAMP);
INSERT INTO t1 VALUES (FROM_UNIXTIME(1288477526),FROM_UNIXTIME(1288481126),FROM_UNIXTIME(1288481127));
SELECT UNIX_TIMESTAMP(a),UNIX_TIMESTAMP(b),a IN (b,c) AS x FROM t1;
SET time_zone='Europe/Moscow';
SELECT UNIX_TIMESTAMP(a),UNIX_TIMESTAMP(b),a IN (b,c) AS x FROM t1;
DROP TABLE t1;

# Comparison and IN in combination with a subquery (with one row)

SET time_zone='+00:00';
CREATE TABLE t1 (a TIMESTAMP, b TIMESTAMP);
INSERT INTO t1 VALUES (FROM_UNIXTIME(1288477526),FROM_UNIXTIME(1288481126));
SELECT * FROM t1 WHERE a = (SELECT MAX(b) FROM t1);
SELECT * FROM t1 WHERE a = (SELECT MIN(b) FROM t1);
SELECT * FROM t1 WHERE a IN ((SELECT MAX(b) FROM t1), (SELECT MIN(b) FROM t1));

SET time_zone='Europe/Moscow';
SELECT * FROM t1 WHERE a = (SELECT MAX(b) FROM t1);
SELECT * FROM t1 WHERE a = (SELECT MIN(b) FROM t1);
SELECT * FROM t1 WHERE a IN ((SELECT MAX(b) FROM t1), (SELECT MIN(b) FROM t1));
DROP TABLE t1;

# Comparison and IN in combinarion with a subquery (with multiple rows)
SET time_zone='+00:00';
CREATE TABLE t1 (a TIMESTAMP, b TIMESTAMP);
INSERT INTO t1 VALUES (FROM_UNIXTIME(1100000000),FROM_UNIXTIME(1200000000));
INSERT INTO t1 VALUES (FROM_UNIXTIME(1100000001),FROM_UNIXTIME(1200000001));
INSERT INTO t1 VALUES (FROM_UNIXTIME(1288477526),FROM_UNIXTIME(1288481126));
INSERT INTO t1 VALUES (FROM_UNIXTIME(1300000000),FROM_UNIXTIME(1400000000));
INSERT INTO t1 VALUES (FROM_UNIXTIME(1300000001),FROM_UNIXTIME(1400000001));
SELECT * FROM t1 WHERE a = (SELECT MAX(b) FROM t1);
SELECT * FROM t1 WHERE a = (SELECT MIN(b) FROM t1);
SELECT * FROM t1 WHERE a IN ((SELECT MAX(b) FROM t1), (SELECT MIN(b) FROM t1));

SET time_zone='Europe/Moscow';
SELECT * FROM t1 WHERE a = (SELECT MAX(b) FROM t1);
SELECT * FROM t1 WHERE a = (SELECT MIN(b) FROM t1);
SELECT * FROM t1 WHERE a IN ((SELECT MAX(b) FROM t1), (SELECT MIN(b) FROM t1));
DROP TABLE t1;

# NULLIF

SET time_zone='Europe/Moscow';

CREATE TABLE t1 (a TIMESTAMP);
CREATE TABLE t2 (a TIMESTAMP);
SET timestamp=1288479599 /*summer time in Mowcow*/;
INSERT INTO t1 VALUES (CURRENT_TIMESTAMP);
SET timestamp=1288479599+3600 /*winter time in Mowcow*/;
INSERT INTO t2 VALUES (CURRENT_TIMESTAMP);
SELECT t1.a, UNIX_TIMESTAMP(t1.a), t2.a, UNIX_TIMESTAMP(t2.a) FROM t1, t2;
SELECT NULLIF(t1.a, t2.a) FROM t1,t2;
DROP TABLE t1, t2;
SET time_zone=DEFAULT;
SET timestamp=DEFAULT;


--echo #
--echo # MDEV-17979 Assertion `0' failed in Item::val_native upon SELECT with timestamp, NULLIF, GROUP BY
--echo #

SET time_zone='+00:00';
CREATE TABLE t1 (a INT, ts TIMESTAMP) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1, FROM_UNIXTIME(1288481126) /*winter time in Moscow*/);
SET time_zone='Europe/Moscow';
CREATE TABLE t2 AS SELECT ts, COALESCE(ts) AS cts FROM t1 GROUP BY cts;
SELECT ts, cts, UNIX_TIMESTAMP(ts) AS uts, UNIX_TIMESTAMP(cts) AS ucts FROM t2;
DROP TABLE t1,t2;
SET time_zone=DEFAULT;

--echo #
--echo # MDEV-19961 MIN(timestamp_column) returns a wrong result in a GROUP BY query
--echo #

SET time_zone='Europe/Moscow';
CREATE OR REPLACE TABLE t1 (i INT, d TIMESTAMP NOT NULL DEFAULT NOW());
SET timestamp=1288477526  /* this is summer time */ ;
INSERT INTO t1 VALUES (3,NULL);
SET timestamp=1288477526+3599 /* this is winter time*/ ;
INSERT INTO t1 VALUES (3,NULL);
SELECT i, d, UNIX_TIMESTAMP(d) FROM t1 ORDER BY d;
SELECT i, MIN(d) FROM t1 GROUP BY i;
SELECT i, MAX(d) FROM t1 GROUP BY i;
DROP TABLE t1;
SET timestamp=DEFAULT;
SET time_zone=DEFAULT;

--echo #
--echo # MDEV-20397 Support TIMESTAMP, DATETIME, TIME in ROUND() and TRUNCATE()
--echo #

SET time_zone='Europe/Moscow';
CREATE TABLE t1 (i INT, d TIMESTAMP(6) NOT NULL DEFAULT NOW());
SET timestamp=1288479599.999999  /* this is the last second in summer time */ ;
INSERT INTO t1 VALUES (1,NULL);
SET timestamp=1288479600.000000 /* this is the first second in winter time */ ;
INSERT INTO t1 VALUES (2,NULL);
SELECT i, d, UNIX_TIMESTAMP(d) FROM t1 ORDER BY d;

CREATE TABLE t2 (i INT, d TIMESTAMP, expected_unix_timestamp INT UNSIGNED);
INSERT INTO t2 SELECT i, ROUND(d) AS d, ROUND(UNIX_TIMESTAMP(d)) FROM t1;

--echo # UNIX_TIMESTAMP(d) and expected_unix_timestamp should return the same value.
--echo # Currently they do not, because ROUND(timestamp) is performed as DATETIME.
--echo # We should fix this eventually.

SELECT i, d, UNIX_TIMESTAMP(d), expected_unix_timestamp FROM t2 ORDER BY i;
DROP TABLE t2;

DROP TABLE t1;
SET timestamp=DEFAULT;
SET time_zone=DEFAULT;


--echo #
--echo # End of 10.4 tests
--echo #

--echo #
--echo # MDEV-27101 Subquery using the ALL keyword on TIMESTAMP columns produces a wrong result
--echo #

SET time_zone='Europe/Moscow';
CREATE TABLE t1 (a TIMESTAMP NULL);
SET timestamp=1288477526;      /* this is summer time, earlier */
INSERT INTO t1 VALUES (NOW());
SET timestamp=1288477526+3599; /* this is winter time, later */
INSERT INTO t1 VALUES (NOW());
SELECT a, UNIX_TIMESTAMP(a) FROM t1 ORDER BY a;
SELECT a, UNIX_TIMESTAMP(a) FROM t1 WHERE a <= ALL (SELECT * FROM t1);
SELECT a, UNIX_TIMESTAMP(a) FROM t1 WHERE a >= ALL (SELECT * FROM t1);
DROP TABLE t1;