interval.out 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. --
  2. -- INTERVAL
  3. --
  4. SET DATESTYLE = 'ISO';
  5. -- check acceptance of "time zone style"
  6. SELECT INTERVAL '01:00' AS "One hour";
  7. One hour
  8. ----------
  9. 01:00:00
  10. (1 row)
  11. SELECT INTERVAL '+02:00' AS "Two hours";
  12. Two hours
  13. -----------
  14. 02:00:00
  15. (1 row)
  16. SELECT INTERVAL '-08:00' AS "Eight hours";
  17. Eight hours
  18. -------------
  19. -08:00:00
  20. (1 row)
  21. SELECT INTERVAL '-1 +02:03' AS "22 hours ago...";
  22. 22 hours ago...
  23. -------------------
  24. -1 days +02:03:00
  25. (1 row)
  26. SELECT INTERVAL '-1 days +02:03' AS "22 hours ago...";
  27. 22 hours ago...
  28. -------------------
  29. -1 days +02:03:00
  30. (1 row)
  31. SELECT INTERVAL '1.5 weeks' AS "Ten days twelve hours";
  32. Ten days twelve hours
  33. -----------------------
  34. 10 days 12:00:00
  35. (1 row)
  36. SELECT INTERVAL '1.5 months' AS "One month 15 days";
  37. One month 15 days
  38. -------------------
  39. 1 mon 15 days
  40. (1 row)
  41. SELECT INTERVAL '10 years -11 month -12 days +13:14' AS "9 years...";
  42. 9 years...
  43. ----------------------------------
  44. 9 years 1 mon -12 days +13:14:00
  45. (1 row)
  46. CREATE TABLE INTERVAL_TBL (f1 interval);
  47. INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 1 minute');
  48. INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 5 hour');
  49. INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 10 day');
  50. INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 34 year');
  51. INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 3 months');
  52. INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 14 seconds ago');
  53. INSERT INTO INTERVAL_TBL (f1) VALUES ('1 day 2 hours 3 minutes 4 seconds');
  54. INSERT INTO INTERVAL_TBL (f1) VALUES ('6 years');
  55. INSERT INTO INTERVAL_TBL (f1) VALUES ('5 months');
  56. INSERT INTO INTERVAL_TBL (f1) VALUES ('5 months 12 hours');
  57. -- badly formatted interval
  58. INSERT INTO INTERVAL_TBL (f1) VALUES ('badly formatted interval');
  59. ERROR: invalid input syntax for type interval: "badly formatted interval"
  60. LINE 1: INSERT INTO INTERVAL_TBL (f1) VALUES ('badly formatted inter...
  61. ^
  62. INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 30 eons ago');
  63. ERROR: invalid input syntax for type interval: "@ 30 eons ago"
  64. LINE 1: INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 30 eons ago');
  65. ^
  66. -- test interval operators
  67. SELECT * FROM INTERVAL_TBL;
  68. f1
  69. -----------------
  70. 00:01:00
  71. 05:00:00
  72. 10 days
  73. 34 years
  74. 3 mons
  75. -00:00:14
  76. 1 day 02:03:04
  77. 6 years
  78. 5 mons
  79. 5 mons 12:00:00
  80. (10 rows)
  81. SELECT * FROM INTERVAL_TBL
  82. WHERE INTERVAL_TBL.f1 <> interval '@ 10 days';
  83. f1
  84. -----------------
  85. 00:01:00
  86. 05:00:00
  87. 34 years
  88. 3 mons
  89. -00:00:14
  90. 1 day 02:03:04
  91. 6 years
  92. 5 mons
  93. 5 mons 12:00:00
  94. (9 rows)
  95. SELECT * FROM INTERVAL_TBL
  96. WHERE INTERVAL_TBL.f1 <= interval '@ 5 hours';
  97. f1
  98. -----------
  99. 00:01:00
  100. 05:00:00
  101. -00:00:14
  102. (3 rows)
  103. SELECT * FROM INTERVAL_TBL
  104. WHERE INTERVAL_TBL.f1 < interval '@ 1 day';
  105. f1
  106. -----------
  107. 00:01:00
  108. 05:00:00
  109. -00:00:14
  110. (3 rows)
  111. SELECT * FROM INTERVAL_TBL
  112. WHERE INTERVAL_TBL.f1 = interval '@ 34 years';
  113. f1
  114. ----------
  115. 34 years
  116. (1 row)
  117. SELECT * FROM INTERVAL_TBL
  118. WHERE INTERVAL_TBL.f1 >= interval '@ 1 month';
  119. f1
  120. -----------------
  121. 34 years
  122. 3 mons
  123. 6 years
  124. 5 mons
  125. 5 mons 12:00:00
  126. (5 rows)
  127. SELECT * FROM INTERVAL_TBL
  128. WHERE INTERVAL_TBL.f1 > interval '@ 3 seconds ago';
  129. f1
  130. -----------------
  131. 00:01:00
  132. 05:00:00
  133. 10 days
  134. 34 years
  135. 3 mons
  136. 1 day 02:03:04
  137. 6 years
  138. 5 mons
  139. 5 mons 12:00:00
  140. (9 rows)
  141. -- Test intervals that are large enough to overflow 64 bits in comparisons
  142. CREATE TEMP TABLE INTERVAL_TBL_OF (f1 interval);
  143. INSERT INTO INTERVAL_TBL_OF (f1) VALUES
  144. ('2147483647 days 2147483647 months'),
  145. ('2147483647 days -2147483648 months'),
  146. ('1 year'),
  147. ('-2147483648 days 2147483647 months'),
  148. ('-2147483648 days -2147483648 months');
  149. -- these should fail as out-of-range
  150. INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('2147483648 days');
  151. ERROR: interval field value out of range: "2147483648 days"
  152. LINE 1: INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('2147483648 days');
  153. ^
  154. INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('-2147483649 days');
  155. ERROR: interval field value out of range: "-2147483649 days"
  156. LINE 1: INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('-2147483649 days')...
  157. ^
  158. INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('2147483647 years');
  159. ERROR: interval out of range
  160. LINE 1: INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('2147483647 years')...
  161. ^
  162. INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('-2147483648 years');
  163. ERROR: interval out of range
  164. LINE 1: INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('-2147483648 years'...
  165. ^
  166. -- Test edge-case overflow detection in interval multiplication
  167. select extract(epoch from '256 microseconds'::interval * (2^55)::float8);
  168. ERROR: interval out of range
  169. CREATE INDEX ON INTERVAL_TBL_OF USING btree (f1);
  170. DROP TABLE INTERVAL_TBL_OF;
  171. -- Test multiplication and division with intervals.
  172. -- Floating point arithmetic rounding errors can lead to unexpected results,
  173. -- though the code attempts to do the right thing and round up to days and
  174. -- minutes to avoid results such as '3 days 24:00 hours' or '14:20:60'.
  175. -- Note that it is expected for some day components to be greater than 29 and
  176. -- some time components be greater than 23:59:59 due to how intervals are
  177. -- stored internally.
  178. CREATE TABLE INTERVAL_MULDIV_TBL (span interval);
  179. DROP TABLE INTERVAL_MULDIV_TBL;
  180. SET DATESTYLE = 'postgres';
  181. -- test fractional second input, and detection of duplicate units
  182. SET DATESTYLE = 'ISO';
  183. SELECT '1 millisecond'::interval, '1 microsecond'::interval,
  184. '500 seconds 99 milliseconds 51 microseconds'::interval;
  185. interval | interval | interval
  186. --------------+-----------------+-----------------
  187. 00:00:00.001 | 00:00:00.000001 | 00:08:20.099051
  188. (1 row)
  189. SELECT '3 days 5 milliseconds'::interval;
  190. interval
  191. ---------------------
  192. 3 days 00:00:00.005
  193. (1 row)
  194. SELECT '1 second 2 seconds'::interval; -- error
  195. ERROR: invalid input syntax for type interval: "1 second 2 seconds"
  196. LINE 1: SELECT '1 second 2 seconds'::interval;
  197. ^
  198. SELECT '10 milliseconds 20 milliseconds'::interval; -- error
  199. ERROR: invalid input syntax for type interval: "10 milliseconds 20 milliseconds"
  200. LINE 1: SELECT '10 milliseconds 20 milliseconds'::interval;
  201. ^
  202. SELECT '5.5 seconds 3 milliseconds'::interval; -- error
  203. ERROR: invalid input syntax for type interval: "5.5 seconds 3 milliseconds"
  204. LINE 1: SELECT '5.5 seconds 3 milliseconds'::interval;
  205. ^
  206. SELECT '1:20:05 5 microseconds'::interval; -- error
  207. ERROR: invalid input syntax for type interval: "1:20:05 5 microseconds"
  208. LINE 1: SELECT '1:20:05 5 microseconds'::interval;
  209. ^
  210. SELECT '1 day 1 day'::interval; -- error
  211. ERROR: invalid input syntax for type interval: "1 day 1 day"
  212. LINE 1: SELECT '1 day 1 day'::interval;
  213. ^
  214. SELECT interval '1-2'; -- SQL year-month literal
  215. interval
  216. ---------------
  217. 1 year 2 mons
  218. (1 row)
  219. SELECT interval '999' second; -- oversize leading field is ok
  220. interval
  221. ----------
  222. 00:16:39
  223. (1 row)
  224. SELECT interval '999' minute;
  225. interval
  226. ----------
  227. 16:39:00
  228. (1 row)
  229. SELECT interval '999' hour;
  230. interval
  231. -----------
  232. 999:00:00
  233. (1 row)
  234. SELECT interval '999' day;
  235. interval
  236. ----------
  237. 999 days
  238. (1 row)
  239. SELECT interval '999' month;
  240. interval
  241. -----------------
  242. 83 years 3 mons
  243. (1 row)
  244. -- test SQL-spec syntaxes for restricted field sets
  245. SELECT interval '1' year;
  246. interval
  247. ----------
  248. 1 year
  249. (1 row)
  250. SELECT interval '2' month;
  251. interval
  252. ----------
  253. 2 mons
  254. (1 row)
  255. SELECT interval '3' day;
  256. interval
  257. ----------
  258. 3 days
  259. (1 row)
  260. SELECT interval '4' hour;
  261. interval
  262. ----------
  263. 04:00:00
  264. (1 row)
  265. SELECT interval '5' minute;
  266. interval
  267. ----------
  268. 00:05:00
  269. (1 row)
  270. SELECT interval '6' second;
  271. interval
  272. ----------
  273. 00:00:06
  274. (1 row)
  275. SELECT interval '1' year to month;
  276. interval
  277. ----------
  278. 1 mon
  279. (1 row)
  280. SELECT interval '1-2' year to month;
  281. interval
  282. ---------------
  283. 1 year 2 mons
  284. (1 row)
  285. SELECT interval '1 2' day to hour;
  286. interval
  287. ----------------
  288. 1 day 02:00:00
  289. (1 row)
  290. SELECT interval '1 2:03' day to hour;
  291. interval
  292. ----------------
  293. 1 day 02:00:00
  294. (1 row)
  295. SELECT interval '1 2:03:04' day to hour;
  296. interval
  297. ----------------
  298. 1 day 02:00:00
  299. (1 row)
  300. SELECT interval '1 2' day to minute;
  301. ERROR: invalid input syntax for type interval: "1 2"
  302. LINE 1: SELECT interval '1 2' day to minute;
  303. ^
  304. SELECT interval '1 2:03' day to minute;
  305. interval
  306. ----------------
  307. 1 day 02:03:00
  308. (1 row)
  309. SELECT interval '1 2:03:04' day to minute;
  310. interval
  311. ----------------
  312. 1 day 02:03:00
  313. (1 row)
  314. SELECT interval '1 2' day to second;
  315. ERROR: invalid input syntax for type interval: "1 2"
  316. LINE 1: SELECT interval '1 2' day to second;
  317. ^
  318. SELECT interval '1 2:03' day to second;
  319. interval
  320. ----------------
  321. 1 day 02:03:00
  322. (1 row)
  323. SELECT interval '1 2:03:04' day to second;
  324. interval
  325. ----------------
  326. 1 day 02:03:04
  327. (1 row)
  328. SELECT interval '1 2' hour to minute;
  329. ERROR: invalid input syntax for type interval: "1 2"
  330. LINE 1: SELECT interval '1 2' hour to minute;
  331. ^
  332. SELECT interval '1 2:03' hour to minute;
  333. interval
  334. ----------------
  335. 1 day 02:03:00
  336. (1 row)
  337. SELECT interval '1 2:03:04' hour to minute;
  338. interval
  339. ----------------
  340. 1 day 02:03:00
  341. (1 row)
  342. SELECT interval '1 2' hour to second;
  343. ERROR: invalid input syntax for type interval: "1 2"
  344. LINE 1: SELECT interval '1 2' hour to second;
  345. ^
  346. SELECT interval '1 2:03' hour to second;
  347. interval
  348. ----------------
  349. 1 day 02:03:00
  350. (1 row)
  351. SELECT interval '1 2:03:04' hour to second;
  352. interval
  353. ----------------
  354. 1 day 02:03:04
  355. (1 row)
  356. SELECT interval '1 2' minute to second;
  357. ERROR: invalid input syntax for type interval: "1 2"
  358. LINE 1: SELECT interval '1 2' minute to second;
  359. ^
  360. SELECT interval '1 2:03' minute to second;
  361. interval
  362. ----------------
  363. 1 day 00:02:03
  364. (1 row)
  365. SELECT interval '1 2:03:04' minute to second;
  366. interval
  367. ----------------
  368. 1 day 02:03:04
  369. (1 row)
  370. SELECT interval '1 +2:03' minute to second;
  371. interval
  372. ----------------
  373. 1 day 00:02:03
  374. (1 row)
  375. SELECT interval '1 +2:03:04' minute to second;
  376. interval
  377. ----------------
  378. 1 day 02:03:04
  379. (1 row)
  380. SELECT interval '1 -2:03' minute to second;
  381. interval
  382. -----------------
  383. 1 day -00:02:03
  384. (1 row)
  385. SELECT interval '1 -2:03:04' minute to second;
  386. interval
  387. -----------------
  388. 1 day -02:03:04
  389. (1 row)
  390. SELECT interval '123 11' day to hour; -- ok
  391. interval
  392. -------------------
  393. 123 days 11:00:00
  394. (1 row)
  395. SELECT interval '123 11' day; -- not ok
  396. ERROR: invalid input syntax for type interval: "123 11"
  397. LINE 1: SELECT interval '123 11' day;
  398. ^
  399. SELECT interval '123 11'; -- not ok, too ambiguous
  400. ERROR: invalid input syntax for type interval: "123 11"
  401. LINE 1: SELECT interval '123 11';
  402. ^
  403. SELECT interval '123 2:03 -2:04'; -- not ok, redundant hh:mm fields
  404. ERROR: invalid input syntax for type interval: "123 2:03 -2:04"
  405. LINE 1: SELECT interval '123 2:03 -2:04';
  406. ^
  407. -- test syntaxes for restricted precision
  408. SELECT interval(0) '1 day 01:23:45.6789';
  409. interval
  410. ----------------
  411. 1 day 01:23:46
  412. (1 row)
  413. SELECT interval(2) '1 day 01:23:45.6789';
  414. interval
  415. -------------------
  416. 1 day 01:23:45.68
  417. (1 row)
  418. SELECT interval '12:34.5678' minute to second(2); -- per SQL spec
  419. interval
  420. -------------
  421. 00:12:34.57
  422. (1 row)
  423. SELECT interval '1.234' second;
  424. interval
  425. --------------
  426. 00:00:01.234
  427. (1 row)
  428. SELECT interval '1.234' second(2);
  429. interval
  430. -------------
  431. 00:00:01.23
  432. (1 row)
  433. SELECT interval '1 2.345' day to second(2);
  434. ERROR: invalid input syntax for type interval: "1 2.345"
  435. LINE 1: SELECT interval '1 2.345' day to second(2);
  436. ^
  437. SELECT interval '1 2:03' day to second(2);
  438. interval
  439. ----------------
  440. 1 day 02:03:00
  441. (1 row)
  442. SELECT interval '1 2:03.4567' day to second(2);
  443. interval
  444. -------------------
  445. 1 day 00:02:03.46
  446. (1 row)
  447. SELECT interval '1 2:03:04.5678' day to second(2);
  448. interval
  449. -------------------
  450. 1 day 02:03:04.57
  451. (1 row)
  452. SELECT interval '1 2.345' hour to second(2);
  453. ERROR: invalid input syntax for type interval: "1 2.345"
  454. LINE 1: SELECT interval '1 2.345' hour to second(2);
  455. ^
  456. SELECT interval '1 2:03.45678' hour to second(2);
  457. interval
  458. -------------------
  459. 1 day 00:02:03.46
  460. (1 row)
  461. SELECT interval '1 2:03:04.5678' hour to second(2);
  462. interval
  463. -------------------
  464. 1 day 02:03:04.57
  465. (1 row)
  466. SELECT interval '1 2.3456' minute to second(2);
  467. ERROR: invalid input syntax for type interval: "1 2.3456"
  468. LINE 1: SELECT interval '1 2.3456' minute to second(2);
  469. ^
  470. SELECT interval '1 2:03.5678' minute to second(2);
  471. interval
  472. -------------------
  473. 1 day 00:02:03.57
  474. (1 row)
  475. SELECT interval '1 2:03:04.5678' minute to second(2);
  476. interval
  477. -------------------
  478. 1 day 02:03:04.57
  479. (1 row)
  480. SELECT interval '+1 -1:00:00',
  481. interval '-1 +1:00:00',
  482. interval '+1-2 -3 +4:05:06.789',
  483. interval '-1-2 +3 -4:05:06.789';
  484. interval | interval | interval | interval
  485. -----------------+-------------------+-------------------------------------+----------------------------------------
  486. 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
  487. (1 row)
  488. select interval 'P00021015T103020' AS "ISO8601 Basic Format",
  489. interval 'P0002-10-15T10:30:20' AS "ISO8601 Extended Format";
  490. ISO8601 Basic Format | ISO8601 Extended Format
  491. ----------------------------------+----------------------------------
  492. 2 years 10 mons 15 days 10:30:20 | 2 years 10 mons 15 days 10:30:20
  493. (1 row)
  494. -- Make sure optional ISO8601 alternative format fields are optional.
  495. select interval 'P0002' AS "year only",
  496. interval 'P0002-10' AS "year month",
  497. interval 'P0002-10-15' AS "year month day",
  498. interval 'P0002T1S' AS "year only plus time",
  499. interval 'P0002-10T1S' AS "year month plus time",
  500. interval 'P0002-10-15T1S' AS "year month day plus time",
  501. interval 'PT10' AS "hour only",
  502. interval 'PT10:30' AS "hour minute";
  503. year only | year month | year month day | year only plus time | year month plus time | year month day plus time | hour only | hour minute
  504. -----------+-----------------+-------------------------+---------------------+--------------------------+----------------------------------+-----------+-------------
  505. 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
  506. (1 row)
  507. -- check that '30 days' equals '1 month' according to the hash function
  508. select '30 days'::interval = '1 month'::interval as t;
  509. t
  510. ---
  511. t
  512. (1 row)
  513. select interval_hash('30 days'::interval) = interval_hash('1 month'::interval) as t;
  514. t
  515. ---
  516. t
  517. (1 row)
  518. SELECT EXTRACT(FORTNIGHT FROM INTERVAL '2 days'); -- error
  519. ERROR: interval units "fortnight" not recognized
  520. SELECT EXTRACT(TIMEZONE FROM INTERVAL '2 days'); -- error
  521. ERROR: interval units "timezone" not supported
  522. SELECT EXTRACT(DECADE FROM INTERVAL '100 y');
  523. extract
  524. ---------
  525. 10
  526. (1 row)
  527. SELECT EXTRACT(DECADE FROM INTERVAL '99 y');
  528. extract
  529. ---------
  530. 9
  531. (1 row)
  532. SELECT EXTRACT(DECADE FROM INTERVAL '-99 y');
  533. extract
  534. ---------
  535. -9
  536. (1 row)
  537. SELECT EXTRACT(DECADE FROM INTERVAL '-100 y');
  538. extract
  539. ---------
  540. -10
  541. (1 row)
  542. SELECT EXTRACT(CENTURY FROM INTERVAL '100 y');
  543. extract
  544. ---------
  545. 1
  546. (1 row)
  547. SELECT EXTRACT(CENTURY FROM INTERVAL '-100 y');
  548. extract
  549. ---------
  550. -1
  551. (1 row)
  552. -- internal overflow test case
  553. SELECT extract(epoch from interval '1000000000 days');
  554. extract
  555. -----------------------
  556. 86400000000000.000000
  557. (1 row)