123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630 |
- --
- -- 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)
|