aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/tests/postgresql/cases/interval.out
blob: 26f1353402c962d3f71cfe5bdb30b28261c58578 (plain) (blame)
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
--
-- INTERVAL
--
SET DATESTYLE = 'ISO';
-- check acceptance of "time zone style"
SELECT INTERVAL '01:00' AS "One hour";
 One hour 
----------
 01:00:00
(1 row)

SELECT INTERVAL '+02:00' AS "Two hours";
 Two hours 
-----------
 02:00:00
(1 row)

SELECT INTERVAL '-08:00' AS "Eight hours";
 Eight hours 
-------------
 -08:00:00
(1 row)

SELECT INTERVAL '-1 +02:03' AS "22 hours ago...";
  22 hours ago...  
-------------------
 -1 days +02:03:00
(1 row)

SELECT INTERVAL '-1 days +02:03' AS "22 hours ago...";
  22 hours ago...  
-------------------
 -1 days +02:03:00
(1 row)

SELECT INTERVAL '1.5 weeks' AS "Ten days twelve hours";
 Ten days twelve hours 
-----------------------
 10 days 12:00:00
(1 row)

SELECT INTERVAL '1.5 months' AS "One month 15 days";
 One month 15 days 
-------------------
 1 mon 15 days
(1 row)

SELECT INTERVAL '10 years -11 month -12 days +13:14' AS "9 years...";
            9 years...            
----------------------------------
 9 years 1 mon -12 days +13:14:00
(1 row)

CREATE TABLE INTERVAL_TBL (f1 interval);
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 1 minute');
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 5 hour');
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 10 day');
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 34 year');
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 3 months');
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 14 seconds ago');
INSERT INTO INTERVAL_TBL (f1) VALUES ('1 day 2 hours 3 minutes 4 seconds');
INSERT INTO INTERVAL_TBL (f1) VALUES ('6 years');
INSERT INTO INTERVAL_TBL (f1) VALUES ('5 months');
INSERT INTO INTERVAL_TBL (f1) VALUES ('5 months 12 hours');
-- badly formatted interval
INSERT INTO INTERVAL_TBL (f1) VALUES ('badly formatted interval');
ERROR:  invalid input syntax for type interval: "badly formatted interval"
LINE 1: INSERT INTO INTERVAL_TBL (f1) VALUES ('badly formatted inter...
                                              ^
INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 30 eons ago');
ERROR:  invalid input syntax for type interval: "@ 30 eons ago"
LINE 1: INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 30 eons ago');
                                              ^
-- test interval operators
SELECT * FROM INTERVAL_TBL;
       f1        
-----------------
 00:01:00
 05:00:00
 10 days
 34 years
 3 mons
 -00:00:14
 1 day 02:03:04
 6 years
 5 mons
 5 mons 12:00:00
(10 rows)

SELECT * FROM INTERVAL_TBL
   WHERE INTERVAL_TBL.f1 <> interval '@ 10 days';
       f1        
-----------------
 00:01:00
 05:00:00
 34 years
 3 mons
 -00:00:14
 1 day 02:03:04
 6 years
 5 mons
 5 mons 12:00:00
(9 rows)

SELECT * FROM INTERVAL_TBL
   WHERE INTERVAL_TBL.f1 <= interval '@ 5 hours';
    f1     
-----------
 00:01:00
 05:00:00
 -00:00:14
(3 rows)

SELECT * FROM INTERVAL_TBL
   WHERE INTERVAL_TBL.f1 < interval '@ 1 day';
    f1     
-----------
 00:01:00
 05:00:00
 -00:00:14
(3 rows)

SELECT * FROM INTERVAL_TBL
   WHERE INTERVAL_TBL.f1 = interval '@ 34 years';
    f1    
----------
 34 years
(1 row)

SELECT * FROM INTERVAL_TBL
   WHERE INTERVAL_TBL.f1 >= interval '@ 1 month';
       f1        
-----------------
 34 years
 3 mons
 6 years
 5 mons
 5 mons 12:00:00
(5 rows)

SELECT * FROM INTERVAL_TBL
   WHERE INTERVAL_TBL.f1 > interval '@ 3 seconds ago';
       f1        
-----------------
 00:01:00
 05:00:00
 10 days
 34 years
 3 mons
 1 day 02:03:04
 6 years
 5 mons
 5 mons 12:00:00
(9 rows)

-- Test intervals that are large enough to overflow 64 bits in comparisons
CREATE TEMP TABLE INTERVAL_TBL_OF (f1 interval);
INSERT INTO INTERVAL_TBL_OF (f1) VALUES
  ('2147483647 days 2147483647 months'),
  ('2147483647 days -2147483648 months'),
  ('1 year'),
  ('-2147483648 days 2147483647 months'),
  ('-2147483648 days -2147483648 months');
-- these should fail as out-of-range
INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('2147483648 days');
ERROR:  interval field value out of range: "2147483648 days"
LINE 1: INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('2147483648 days');
                                                 ^
INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('-2147483649 days');
ERROR:  interval field value out of range: "-2147483649 days"
LINE 1: INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('-2147483649 days')...
                                                 ^
INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('2147483647 years');
ERROR:  interval out of range
LINE 1: INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('2147483647 years')...
                                                 ^
INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('-2147483648 years');
ERROR:  interval out of range
LINE 1: INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('-2147483648 years'...
                                                 ^
-- Test edge-case overflow detection in interval multiplication
select extract(epoch from '256 microseconds'::interval * (2^55)::float8);
ERROR:  interval out of range
CREATE INDEX ON INTERVAL_TBL_OF USING btree (f1);
DROP TABLE INTERVAL_TBL_OF;
-- Test multiplication and division with intervals.
-- Floating point arithmetic rounding errors can lead to unexpected results,
-- though the code attempts to do the right thing and round up to days and
-- minutes to avoid results such as '3 days 24:00 hours' or '14:20:60'.
-- Note that it is expected for some day components to be greater than 29 and
-- some time components be greater than 23:59:59 due to how intervals are
-- stored internally.
CREATE TABLE INTERVAL_MULDIV_TBL (span interval);
DROP TABLE INTERVAL_MULDIV_TBL;
SET DATESTYLE = 'postgres';
-- test fractional second input, and detection of duplicate units
SET DATESTYLE = 'ISO';
SELECT '1 millisecond'::interval, '1 microsecond'::interval,
       '500 seconds 99 milliseconds 51 microseconds'::interval;
   interval   |    interval     |    interval     
--------------+-----------------+-----------------
 00:00:00.001 | 00:00:00.000001 | 00:08:20.099051
(1 row)

SELECT '3 days 5 milliseconds'::interval;
      interval       
---------------------
 3 days 00:00:00.005
(1 row)

SELECT '1 second 2 seconds'::interval;              -- error
ERROR:  invalid input syntax for type interval: "1 second 2 seconds"
LINE 1: SELECT '1 second 2 seconds'::interval;
               ^
SELECT '10 milliseconds 20 milliseconds'::interval; -- error
ERROR:  invalid input syntax for type interval: "10 milliseconds 20 milliseconds"
LINE 1: SELECT '10 milliseconds 20 milliseconds'::interval;
               ^
SELECT '5.5 seconds 3 milliseconds'::interval;      -- error
ERROR:  invalid input syntax for type interval: "5.5 seconds 3 milliseconds"
LINE 1: SELECT '5.5 seconds 3 milliseconds'::interval;
               ^
SELECT '1:20:05 5 microseconds'::interval;          -- error
ERROR:  invalid input syntax for type interval: "1:20:05 5 microseconds"
LINE 1: SELECT '1:20:05 5 microseconds'::interval;
               ^
SELECT '1 day 1 day'::interval;                     -- error
ERROR:  invalid input syntax for type interval: "1 day 1 day"
LINE 1: SELECT '1 day 1 day'::interval;
               ^
SELECT interval '1-2';  -- SQL year-month literal
   interval    
---------------
 1 year 2 mons
(1 row)

SELECT interval '999' second;  -- oversize leading field is ok
 interval 
----------
 00:16:39
(1 row)

SELECT interval '999' minute;
 interval 
----------
 16:39:00
(1 row)

SELECT interval '999' hour;
 interval  
-----------
 999:00:00
(1 row)

SELECT interval '999' day;
 interval 
----------
 999 days
(1 row)

SELECT interval '999' month;
    interval     
-----------------
 83 years 3 mons
(1 row)

-- test SQL-spec syntaxes for restricted field sets
SELECT interval '1' year;
 interval 
----------
 1 year
(1 row)

SELECT interval '2' month;
 interval 
----------
 2 mons
(1 row)

SELECT interval '3' day;
 interval 
----------
 3 days
(1 row)

SELECT interval '4' hour;
 interval 
----------
 04:00:00
(1 row)

SELECT interval '5' minute;
 interval 
----------
 00:05:00
(1 row)

SELECT interval '6' second;
 interval 
----------
 00:00:06
(1 row)

SELECT interval '1' year to month;
 interval 
----------
 1 mon
(1 row)

SELECT interval '1-2' year to month;
   interval    
---------------
 1 year 2 mons
(1 row)

SELECT interval '1 2' day to hour;
    interval    
----------------
 1 day 02:00:00
(1 row)

SELECT interval '1 2:03' day to hour;
    interval    
----------------
 1 day 02:00:00
(1 row)

SELECT interval '1 2:03:04' day to hour;
    interval    
----------------
 1 day 02:00:00
(1 row)

SELECT interval '1 2' day to minute;
ERROR:  invalid input syntax for type interval: "1 2"
LINE 1: SELECT interval '1 2' day to minute;
                        ^
SELECT interval '1 2:03' day to minute;
    interval    
----------------
 1 day 02:03:00
(1 row)

SELECT interval '1 2:03:04' day to minute;
    interval    
----------------
 1 day 02:03:00
(1 row)

SELECT interval '1 2' day to second;
ERROR:  invalid input syntax for type interval: "1 2"
LINE 1: SELECT interval '1 2' day to second;
                        ^
SELECT interval '1 2:03' day to second;
    interval    
----------------
 1 day 02:03:00
(1 row)

SELECT interval '1 2:03:04' day to second;
    interval    
----------------
 1 day 02:03:04
(1 row)

SELECT interval '1 2' hour to minute;
ERROR:  invalid input syntax for type interval: "1 2"
LINE 1: SELECT interval '1 2' hour to minute;
                        ^
SELECT interval '1 2:03' hour to minute;
    interval    
----------------
 1 day 02:03:00
(1 row)

SELECT interval '1 2:03:04' hour to minute;
    interval    
----------------
 1 day 02:03:00
(1 row)

SELECT interval '1 2' hour to second;
ERROR:  invalid input syntax for type interval: "1 2"
LINE 1: SELECT interval '1 2' hour to second;
                        ^
SELECT interval '1 2:03' hour to second;
    interval    
----------------
 1 day 02:03:00
(1 row)

SELECT interval '1 2:03:04' hour to second;
    interval    
----------------
 1 day 02:03:04
(1 row)

SELECT interval '1 2' minute to second;
ERROR:  invalid input syntax for type interval: "1 2"
LINE 1: SELECT interval '1 2' minute to second;
                        ^
SELECT interval '1 2:03' minute to second;
    interval    
----------------
 1 day 00:02:03
(1 row)

SELECT interval '1 2:03:04' minute to second;
    interval    
----------------
 1 day 02:03:04
(1 row)

SELECT interval '1 +2:03' minute to second;
    interval    
----------------
 1 day 00:02:03
(1 row)

SELECT interval '1 +2:03:04' minute to second;
    interval    
----------------
 1 day 02:03:04
(1 row)

SELECT interval '1 -2:03' minute to second;
    interval     
-----------------
 1 day -00:02:03
(1 row)

SELECT interval '1 -2:03:04' minute to second;
    interval     
-----------------
 1 day -02:03:04
(1 row)

SELECT interval '123 11' day to hour; -- ok
     interval      
-------------------
 123 days 11:00:00
(1 row)

SELECT interval '123 11' day; -- not ok
ERROR:  invalid input syntax for type interval: "123 11"
LINE 1: SELECT interval '123 11' day;
                        ^
SELECT interval '123 11'; -- not ok, too ambiguous
ERROR:  invalid input syntax for type interval: "123 11"
LINE 1: SELECT interval '123 11';
                        ^
SELECT interval '123 2:03 -2:04'; -- not ok, redundant hh:mm fields
ERROR:  invalid input syntax for type interval: "123 2:03 -2:04"
LINE 1: SELECT interval '123 2:03 -2:04';
                        ^
-- test syntaxes for restricted precision
SELECT interval(0) '1 day 01:23:45.6789';
    interval    
----------------
 1 day 01:23:46
(1 row)

SELECT interval(2) '1 day 01:23:45.6789';
     interval      
-------------------
 1 day 01:23:45.68
(1 row)

SELECT interval '12:34.5678' minute to second(2);  -- per SQL spec
  interval   
-------------
 00:12:34.57
(1 row)

SELECT interval '1.234' second;
   interval   
--------------
 00:00:01.234
(1 row)

SELECT interval '1.234' second(2);
  interval   
-------------
 00:00:01.23
(1 row)

SELECT interval '1 2.345' day to second(2);
ERROR:  invalid input syntax for type interval: "1 2.345"
LINE 1: SELECT interval '1 2.345' day to second(2);
                        ^
SELECT interval '1 2:03' day to second(2);
    interval    
----------------
 1 day 02:03:00
(1 row)

SELECT interval '1 2:03.4567' day to second(2);
     interval      
-------------------
 1 day 00:02:03.46
(1 row)

SELECT interval '1 2:03:04.5678' day to second(2);
     interval      
-------------------
 1 day 02:03:04.57
(1 row)

SELECT interval '1 2.345' hour to second(2);
ERROR:  invalid input syntax for type interval: "1 2.345"
LINE 1: SELECT interval '1 2.345' hour to second(2);
                        ^
SELECT interval '1 2:03.45678' hour to second(2);
     interval      
-------------------
 1 day 00:02:03.46
(1 row)

SELECT interval '1 2:03:04.5678' hour to second(2);
     interval      
-------------------
 1 day 02:03:04.57
(1 row)

SELECT interval '1 2.3456' minute to second(2);
ERROR:  invalid input syntax for type interval: "1 2.3456"
LINE 1: SELECT interval '1 2.3456' minute to second(2);
                        ^
SELECT interval '1 2:03.5678' minute to second(2);
     interval      
-------------------
 1 day 00:02:03.57
(1 row)

SELECT interval '1 2:03:04.5678' minute to second(2);
     interval      
-------------------
 1 day 02:03:04.57
(1 row)

SELECT  interval '+1 -1:00:00',
        interval '-1 +1:00:00',
        interval '+1-2 -3 +4:05:06.789',
        interval '-1-2 +3 -4:05:06.789';
    interval     |     interval      |              interval               |                interval                
-----------------+-------------------+-------------------------------------+----------------------------------------
 1 day -01:00:00 | -1 days +01:00:00 | 1 year 2 mons -3 days +04:05:06.789 | -1 years -2 mons +3 days -04:05:06.789
(1 row)

select  interval 'P00021015T103020'       AS "ISO8601 Basic Format",
        interval 'P0002-10-15T10:30:20'   AS "ISO8601 Extended Format";
       ISO8601 Basic Format       |     ISO8601 Extended Format      
----------------------------------+----------------------------------
 2 years 10 mons 15 days 10:30:20 | 2 years 10 mons 15 days 10:30:20
(1 row)

-- Make sure optional ISO8601 alternative format fields are optional.
select  interval 'P0002'                  AS "year only",
        interval 'P0002-10'               AS "year month",
        interval 'P0002-10-15'            AS "year month day",
        interval 'P0002T1S'               AS "year only plus time",
        interval 'P0002-10T1S'            AS "year month plus time",
        interval 'P0002-10-15T1S'         AS "year month day plus time",
        interval 'PT10'                   AS "hour only",
        interval 'PT10:30'                AS "hour minute";
 year only |   year month    |     year month day      | year only plus time |   year month plus time   |     year month day plus time     | hour only | hour minute 
-----------+-----------------+-------------------------+---------------------+--------------------------+----------------------------------+-----------+-------------
 2 years   | 2 years 10 mons | 2 years 10 mons 15 days | 2 years 00:00:01    | 2 years 10 mons 00:00:01 | 2 years 10 mons 15 days 00:00:01 | 10:00:00  | 10:30:00
(1 row)

-- check that '30 days' equals '1 month' according to the hash function
select '30 days'::interval = '1 month'::interval as t;
 t 
---
 t
(1 row)

select interval_hash('30 days'::interval) = interval_hash('1 month'::interval) as t;
 t 
---
 t
(1 row)

SELECT EXTRACT(FORTNIGHT FROM INTERVAL '2 days');  -- error
ERROR:  interval units "fortnight" not recognized
SELECT EXTRACT(TIMEZONE FROM INTERVAL '2 days');  -- error
ERROR:  interval units "timezone" not supported
SELECT EXTRACT(DECADE FROM INTERVAL '100 y');
 extract 
---------
      10
(1 row)

SELECT EXTRACT(DECADE FROM INTERVAL '99 y');
 extract 
---------
       9
(1 row)

SELECT EXTRACT(DECADE FROM INTERVAL '-99 y');
 extract 
---------
      -9
(1 row)

SELECT EXTRACT(DECADE FROM INTERVAL '-100 y');
 extract 
---------
     -10
(1 row)

SELECT EXTRACT(CENTURY FROM INTERVAL '100 y');
 extract 
---------
       1
(1 row)

SELECT EXTRACT(CENTURY FROM INTERVAL '-100 y');
 extract 
---------
      -1
(1 row)

-- internal overflow test case
SELECT extract(epoch from interval '1000000000 days');
        extract        
-----------------------
 86400000000000.000000
(1 row)