interval.out 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. --
  2. -- INTERVAL
  3. --
  4. SET DATESTYLE = 'ISO';
  5. SET IntervalStyle to postgres;
  6. -- check acceptance of "time zone style"
  7. SELECT INTERVAL '01:00' AS "One hour";
  8. One hour
  9. ----------
  10. 01:00:00
  11. (1 row)
  12. SELECT INTERVAL '+02:00' AS "Two hours";
  13. Two hours
  14. -----------
  15. 02:00:00
  16. (1 row)
  17. SELECT INTERVAL '-08:00' AS "Eight hours";
  18. Eight hours
  19. -------------
  20. -08:00:00
  21. (1 row)
  22. SELECT INTERVAL '-1 +02:03' AS "22 hours ago...";
  23. 22 hours ago...
  24. -------------------
  25. -1 days +02:03:00
  26. (1 row)
  27. SELECT INTERVAL '-1 days +02:03' AS "22 hours ago...";
  28. 22 hours ago...
  29. -------------------
  30. -1 days +02:03:00
  31. (1 row)
  32. SELECT INTERVAL '1.5 weeks' AS "Ten days twelve hours";
  33. Ten days twelve hours
  34. -----------------------
  35. 10 days 12:00:00
  36. (1 row)
  37. SELECT INTERVAL '1.5 months' AS "One month 15 days";
  38. One month 15 days
  39. -------------------
  40. 1 mon 15 days
  41. (1 row)
  42. SELECT INTERVAL '10 years -11 month -12 days +13:14' AS "9 years...";
  43. 9 years...
  44. ----------------------------------
  45. 9 years 1 mon -12 days +13:14:00
  46. (1 row)
  47. CREATE TABLE INTERVAL_TBL (f1 interval);
  48. INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 1 minute');
  49. INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 5 hour');
  50. INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 10 day');
  51. INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 34 year');
  52. INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 3 months');
  53. INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 14 seconds ago');
  54. INSERT INTO INTERVAL_TBL (f1) VALUES ('1 day 2 hours 3 minutes 4 seconds');
  55. INSERT INTO INTERVAL_TBL (f1) VALUES ('6 years');
  56. INSERT INTO INTERVAL_TBL (f1) VALUES ('5 months');
  57. INSERT INTO INTERVAL_TBL (f1) VALUES ('5 months 12 hours');
  58. -- badly formatted interval
  59. INSERT INTO INTERVAL_TBL (f1) VALUES ('badly formatted interval');
  60. ERROR: invalid input syntax for type interval: "badly formatted interval"
  61. LINE 1: INSERT INTO INTERVAL_TBL (f1) VALUES ('badly formatted inter...
  62. ^
  63. INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 30 eons ago');
  64. ERROR: invalid input syntax for type interval: "@ 30 eons ago"
  65. LINE 1: INSERT INTO INTERVAL_TBL (f1) VALUES ('@ 30 eons ago');
  66. ^
  67. -- test interval operators
  68. SELECT * FROM INTERVAL_TBL;
  69. f1
  70. -----------------
  71. 00:01:00
  72. 05:00:00
  73. 10 days
  74. 34 years
  75. 3 mons
  76. -00:00:14
  77. 1 day 02:03:04
  78. 6 years
  79. 5 mons
  80. 5 mons 12:00:00
  81. (10 rows)
  82. SELECT * FROM INTERVAL_TBL
  83. WHERE INTERVAL_TBL.f1 <> interval '@ 10 days';
  84. f1
  85. -----------------
  86. 00:01:00
  87. 05:00:00
  88. 34 years
  89. 3 mons
  90. -00:00:14
  91. 1 day 02:03:04
  92. 6 years
  93. 5 mons
  94. 5 mons 12:00:00
  95. (9 rows)
  96. SELECT * FROM INTERVAL_TBL
  97. WHERE INTERVAL_TBL.f1 <= interval '@ 5 hours';
  98. f1
  99. -----------
  100. 00:01:00
  101. 05:00:00
  102. -00:00:14
  103. (3 rows)
  104. SELECT * FROM INTERVAL_TBL
  105. WHERE INTERVAL_TBL.f1 < interval '@ 1 day';
  106. f1
  107. -----------
  108. 00:01:00
  109. 05:00:00
  110. -00:00:14
  111. (3 rows)
  112. SELECT * FROM INTERVAL_TBL
  113. WHERE INTERVAL_TBL.f1 = interval '@ 34 years';
  114. f1
  115. ----------
  116. 34 years
  117. (1 row)
  118. SELECT * FROM INTERVAL_TBL
  119. WHERE INTERVAL_TBL.f1 >= interval '@ 1 month';
  120. f1
  121. -----------------
  122. 34 years
  123. 3 mons
  124. 6 years
  125. 5 mons
  126. 5 mons 12:00:00
  127. (5 rows)
  128. SELECT * FROM INTERVAL_TBL
  129. WHERE INTERVAL_TBL.f1 > interval '@ 3 seconds ago';
  130. f1
  131. -----------------
  132. 00:01:00
  133. 05:00:00
  134. 10 days
  135. 34 years
  136. 3 mons
  137. 1 day 02:03:04
  138. 6 years
  139. 5 mons
  140. 5 mons 12:00:00
  141. (9 rows)
  142. SELECT r1.*, r2.*
  143. FROM INTERVAL_TBL r1, INTERVAL_TBL r2
  144. WHERE r1.f1 > r2.f1
  145. ORDER BY r1.f1, r2.f1;
  146. f1 | f1
  147. -----------------+-----------------
  148. 00:01:00 | -00:00:14
  149. 05:00:00 | -00:00:14
  150. 05:00:00 | 00:01:00
  151. 1 day 02:03:04 | -00:00:14
  152. 1 day 02:03:04 | 00:01:00
  153. 1 day 02:03:04 | 05:00:00
  154. 10 days | -00:00:14
  155. 10 days | 00:01:00
  156. 10 days | 05:00:00
  157. 10 days | 1 day 02:03:04
  158. 3 mons | -00:00:14
  159. 3 mons | 00:01:00
  160. 3 mons | 05:00:00
  161. 3 mons | 1 day 02:03:04
  162. 3 mons | 10 days
  163. 5 mons | -00:00:14
  164. 5 mons | 00:01:00
  165. 5 mons | 05:00:00
  166. 5 mons | 1 day 02:03:04
  167. 5 mons | 10 days
  168. 5 mons | 3 mons
  169. 5 mons 12:00:00 | -00:00:14
  170. 5 mons 12:00:00 | 00:01:00
  171. 5 mons 12:00:00 | 05:00:00
  172. 5 mons 12:00:00 | 1 day 02:03:04
  173. 5 mons 12:00:00 | 10 days
  174. 5 mons 12:00:00 | 3 mons
  175. 5 mons 12:00:00 | 5 mons
  176. 6 years | -00:00:14
  177. 6 years | 00:01:00
  178. 6 years | 05:00:00
  179. 6 years | 1 day 02:03:04
  180. 6 years | 10 days
  181. 6 years | 3 mons
  182. 6 years | 5 mons
  183. 6 years | 5 mons 12:00:00
  184. 34 years | -00:00:14
  185. 34 years | 00:01:00
  186. 34 years | 05:00:00
  187. 34 years | 1 day 02:03:04
  188. 34 years | 10 days
  189. 34 years | 3 mons
  190. 34 years | 5 mons
  191. 34 years | 5 mons 12:00:00
  192. 34 years | 6 years
  193. (45 rows)
  194. -- Test intervals that are large enough to overflow 64 bits in comparisons
  195. CREATE TEMP TABLE INTERVAL_TBL_OF (f1 interval);
  196. INSERT INTO INTERVAL_TBL_OF (f1) VALUES
  197. ('2147483647 days 2147483647 months'),
  198. ('2147483647 days -2147483648 months'),
  199. ('1 year'),
  200. ('-2147483648 days 2147483647 months'),
  201. ('-2147483648 days -2147483648 months');
  202. -- these should fail as out-of-range
  203. INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('2147483648 days');
  204. ERROR: interval field value out of range: "2147483648 days"
  205. LINE 1: INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('2147483648 days');
  206. ^
  207. INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('-2147483649 days');
  208. ERROR: interval field value out of range: "-2147483649 days"
  209. LINE 1: INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('-2147483649 days')...
  210. ^
  211. INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('2147483647 years');
  212. ERROR: interval out of range
  213. LINE 1: INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('2147483647 years')...
  214. ^
  215. INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('-2147483648 years');
  216. ERROR: interval out of range
  217. LINE 1: INSERT INTO INTERVAL_TBL_OF (f1) VALUES ('-2147483648 years'...
  218. ^
  219. -- Test edge-case overflow detection in interval multiplication
  220. select extract(epoch from '256 microseconds'::interval * (2^55)::float8);
  221. ERROR: interval out of range
  222. SELECT r1.*, r2.*
  223. FROM INTERVAL_TBL_OF r1, INTERVAL_TBL_OF r2
  224. WHERE r1.f1 > r2.f1
  225. ORDER BY r1.f1, r2.f1;
  226. f1 | f1
  227. -------------------------------------------+-------------------------------------------
  228. -178956970 years -8 mons +2147483647 days | -178956970 years -8 mons -2147483648 days
  229. 1 year | -178956970 years -8 mons -2147483648 days
  230. 1 year | -178956970 years -8 mons +2147483647 days
  231. 178956970 years 7 mons -2147483648 days | -178956970 years -8 mons -2147483648 days
  232. 178956970 years 7 mons -2147483648 days | -178956970 years -8 mons +2147483647 days
  233. 178956970 years 7 mons -2147483648 days | 1 year
  234. 178956970 years 7 mons 2147483647 days | -178956970 years -8 mons -2147483648 days
  235. 178956970 years 7 mons 2147483647 days | -178956970 years -8 mons +2147483647 days
  236. 178956970 years 7 mons 2147483647 days | 1 year
  237. 178956970 years 7 mons 2147483647 days | 178956970 years 7 mons -2147483648 days
  238. (10 rows)
  239. CREATE INDEX ON INTERVAL_TBL_OF USING btree (f1);
  240. SET enable_seqscan TO false;
  241. EXPLAIN (COSTS OFF)
  242. SELECT f1 FROM INTERVAL_TBL_OF r1 ORDER BY f1;
  243. QUERY PLAN
  244. --------------------------------------------------------------------
  245. Index Only Scan using interval_tbl_of_f1_idx on interval_tbl_of r1
  246. (1 row)
  247. SELECT f1 FROM INTERVAL_TBL_OF r1 ORDER BY f1;
  248. f1
  249. -------------------------------------------
  250. -178956970 years -8 mons -2147483648 days
  251. -178956970 years -8 mons +2147483647 days
  252. 1 year
  253. 178956970 years 7 mons -2147483648 days
  254. 178956970 years 7 mons 2147483647 days
  255. (5 rows)
  256. RESET enable_seqscan;
  257. DROP TABLE INTERVAL_TBL_OF;
  258. -- Test multiplication and division with intervals.
  259. -- Floating point arithmetic rounding errors can lead to unexpected results,
  260. -- though the code attempts to do the right thing and round up to days and
  261. -- minutes to avoid results such as '3 days 24:00 hours' or '14:20:60'.
  262. -- Note that it is expected for some day components to be greater than 29 and
  263. -- some time components be greater than 23:59:59 due to how intervals are
  264. -- stored internally.
  265. CREATE TABLE INTERVAL_MULDIV_TBL (span interval);
  266. COPY INTERVAL_MULDIV_TBL FROM STDIN;
  267. SELECT span * 0.3 AS product
  268. FROM INTERVAL_MULDIV_TBL;
  269. product
  270. ------------------------------------
  271. 1 year 12 days 122:24:00
  272. -1 years -12 days +93:36:00
  273. -3 days -14:24:00
  274. 2 mons 13 days 01:22:28.8
  275. -10 mons +120 days 37:28:21.6567
  276. 1 mon 6 days
  277. 4 mons 6 days
  278. 24 years 11 mons 320 days 16:48:00
  279. (8 rows)
  280. SELECT span * 8.2 AS product
  281. FROM INTERVAL_MULDIV_TBL;
  282. product
  283. ---------------------------------------------
  284. 28 years 104 days 2961:36:00
  285. -28 years -104 days +2942:24:00
  286. -98 days -09:36:00
  287. 6 years 1 mon -197 days +93:34:27.2
  288. -24 years -7 mons +3946 days 640:15:11.9498
  289. 2 years 8 mons 24 days
  290. 9 years 6 mons 24 days
  291. 682 years 7 mons 8215 days 19:12:00
  292. (8 rows)
  293. SELECT span / 10 AS quotient
  294. FROM INTERVAL_MULDIV_TBL;
  295. quotient
  296. ----------------------------------
  297. 4 mons 4 days 40:48:00
  298. -4 mons -4 days +31:12:00
  299. -1 days -04:48:00
  300. 25 days -15:32:30.4
  301. -3 mons +30 days 12:29:27.2189
  302. 12 days
  303. 1 mon 12 days
  304. 8 years 3 mons 126 days 21:36:00
  305. (8 rows)
  306. SELECT span / 100 AS quotient
  307. FROM INTERVAL_MULDIV_TBL;
  308. quotient
  309. -------------------------
  310. 12 days 13:40:48
  311. -12 days -06:28:48
  312. -02:52:48
  313. 2 days 10:26:44.96
  314. -6 days +01:14:56.72189
  315. 1 day 04:48:00
  316. 4 days 04:48:00
  317. 9 mons 39 days 16:33:36
  318. (8 rows)
  319. DROP TABLE INTERVAL_MULDIV_TBL;
  320. SET DATESTYLE = 'postgres';
  321. SET IntervalStyle to postgres_verbose;
  322. SELECT * FROM INTERVAL_TBL;
  323. f1
  324. -------------------------------
  325. @ 1 min
  326. @ 5 hours
  327. @ 10 days
  328. @ 34 years
  329. @ 3 mons
  330. @ 14 secs ago
  331. @ 1 day 2 hours 3 mins 4 secs
  332. @ 6 years
  333. @ 5 mons
  334. @ 5 mons 12 hours
  335. (10 rows)
  336. -- test avg(interval), which is somewhat fragile since people have been
  337. -- known to change the allowed input syntax for type interval without
  338. -- updating pg_aggregate.agginitval
  339. select avg(f1) from interval_tbl;
  340. avg
  341. -------------------------------------------------
  342. @ 4 years 1 mon 10 days 4 hours 18 mins 23 secs
  343. (1 row)
  344. -- test long interval input
  345. select '4 millenniums 5 centuries 4 decades 1 year 4 months 4 days 17 minutes 31 seconds'::interval;
  346. interval
  347. --------------------------------------------
  348. @ 4541 years 4 mons 4 days 17 mins 31 secs
  349. (1 row)
  350. -- test long interval output
  351. -- Note: the actual maximum length of the interval output is longer,
  352. -- but we need the test to work for both integer and floating-point
  353. -- timestamps.
  354. select '100000000y 10mon -1000000000d -100000h -10min -10.000001s ago'::interval;
  355. interval
  356. ---------------------------------------------------------------------------------------
  357. @ 100000000 years 10 mons -1000000000 days -100000 hours -10 mins -10.000001 secs ago
  358. (1 row)
  359. -- test justify_hours() and justify_days()
  360. SELECT justify_hours(interval '6 months 3 days 52 hours 3 minutes 2 seconds') as "6 mons 5 days 4 hours 3 mins 2 seconds";
  361. 6 mons 5 days 4 hours 3 mins 2 seconds
  362. ----------------------------------------
  363. @ 6 mons 5 days 4 hours 3 mins 2 secs
  364. (1 row)
  365. SELECT justify_days(interval '6 months 36 days 5 hours 4 minutes 3 seconds') as "7 mons 6 days 5 hours 4 mins 3 seconds";
  366. 7 mons 6 days 5 hours 4 mins 3 seconds
  367. ----------------------------------------
  368. @ 7 mons 6 days 5 hours 4 mins 3 secs
  369. (1 row)
  370. -- test justify_interval()
  371. SELECT justify_interval(interval '1 month -1 hour') as "1 month -1 hour";
  372. 1 month -1 hour
  373. --------------------
  374. @ 29 days 23 hours
  375. (1 row)
  376. -- test fractional second input, and detection of duplicate units
  377. SET DATESTYLE = 'ISO';
  378. SET IntervalStyle TO postgres;
  379. SELECT '1 millisecond'::interval, '1 microsecond'::interval,
  380. '500 seconds 99 milliseconds 51 microseconds'::interval;
  381. interval | interval | interval
  382. --------------+-----------------+-----------------
  383. 00:00:00.001 | 00:00:00.000001 | 00:08:20.099051
  384. (1 row)
  385. SELECT '3 days 5 milliseconds'::interval;
  386. interval
  387. ---------------------
  388. 3 days 00:00:00.005
  389. (1 row)
  390. SELECT '1 second 2 seconds'::interval; -- error
  391. ERROR: invalid input syntax for type interval: "1 second 2 seconds"
  392. LINE 1: SELECT '1 second 2 seconds'::interval;
  393. ^
  394. SELECT '10 milliseconds 20 milliseconds'::interval; -- error
  395. ERROR: invalid input syntax for type interval: "10 milliseconds 20 milliseconds"
  396. LINE 1: SELECT '10 milliseconds 20 milliseconds'::interval;
  397. ^
  398. SELECT '5.5 seconds 3 milliseconds'::interval; -- error
  399. ERROR: invalid input syntax for type interval: "5.5 seconds 3 milliseconds"
  400. LINE 1: SELECT '5.5 seconds 3 milliseconds'::interval;
  401. ^
  402. SELECT '1:20:05 5 microseconds'::interval; -- error
  403. ERROR: invalid input syntax for type interval: "1:20:05 5 microseconds"
  404. LINE 1: SELECT '1:20:05 5 microseconds'::interval;
  405. ^
  406. SELECT '1 day 1 day'::interval; -- error
  407. ERROR: invalid input syntax for type interval: "1 day 1 day"
  408. LINE 1: SELECT '1 day 1 day'::interval;
  409. ^
  410. SELECT interval '1-2'; -- SQL year-month literal
  411. interval
  412. ---------------
  413. 1 year 2 mons
  414. (1 row)
  415. SELECT interval '999' second; -- oversize leading field is ok
  416. interval
  417. ----------
  418. 00:16:39
  419. (1 row)
  420. SELECT interval '999' minute;
  421. interval
  422. ----------
  423. 16:39:00
  424. (1 row)
  425. SELECT interval '999' hour;
  426. interval
  427. -----------
  428. 999:00:00
  429. (1 row)
  430. SELECT interval '999' day;
  431. interval
  432. ----------
  433. 999 days
  434. (1 row)
  435. SELECT interval '999' month;
  436. interval
  437. -----------------
  438. 83 years 3 mons
  439. (1 row)
  440. -- test SQL-spec syntaxes for restricted field sets
  441. SELECT interval '1' year;
  442. interval
  443. ----------
  444. 1 year
  445. (1 row)
  446. SELECT interval '2' month;
  447. interval
  448. ----------
  449. 2 mons
  450. (1 row)
  451. SELECT interval '3' day;
  452. interval
  453. ----------
  454. 3 days
  455. (1 row)
  456. SELECT interval '4' hour;
  457. interval
  458. ----------
  459. 04:00:00
  460. (1 row)
  461. SELECT interval '5' minute;
  462. interval
  463. ----------
  464. 00:05:00
  465. (1 row)
  466. SELECT interval '6' second;
  467. interval
  468. ----------
  469. 00:00:06
  470. (1 row)
  471. SELECT interval '1' year to month;
  472. interval
  473. ----------
  474. 1 mon
  475. (1 row)
  476. SELECT interval '1-2' year to month;
  477. interval
  478. ---------------
  479. 1 year 2 mons
  480. (1 row)
  481. SELECT interval '1 2' day to hour;
  482. interval
  483. ----------------
  484. 1 day 02:00:00
  485. (1 row)
  486. SELECT interval '1 2:03' day to hour;
  487. interval
  488. ----------------
  489. 1 day 02:00:00
  490. (1 row)
  491. SELECT interval '1 2:03:04' day to hour;
  492. interval
  493. ----------------
  494. 1 day 02:00:00
  495. (1 row)
  496. SELECT interval '1 2' day to minute;
  497. ERROR: invalid input syntax for type interval: "1 2"
  498. LINE 1: SELECT interval '1 2' day to minute;
  499. ^
  500. SELECT interval '1 2:03' day to minute;
  501. interval
  502. ----------------
  503. 1 day 02:03:00
  504. (1 row)
  505. SELECT interval '1 2:03:04' day to minute;
  506. interval
  507. ----------------
  508. 1 day 02:03:00
  509. (1 row)
  510. SELECT interval '1 2' day to second;
  511. ERROR: invalid input syntax for type interval: "1 2"
  512. LINE 1: SELECT interval '1 2' day to second;
  513. ^
  514. SELECT interval '1 2:03' day to second;
  515. interval
  516. ----------------
  517. 1 day 02:03:00
  518. (1 row)
  519. SELECT interval '1 2:03:04' day to second;
  520. interval
  521. ----------------
  522. 1 day 02:03:04
  523. (1 row)
  524. SELECT interval '1 2' hour to minute;
  525. ERROR: invalid input syntax for type interval: "1 2"
  526. LINE 1: SELECT interval '1 2' hour to minute;
  527. ^
  528. SELECT interval '1 2:03' hour to minute;
  529. interval
  530. ----------------
  531. 1 day 02:03:00
  532. (1 row)
  533. SELECT interval '1 2:03:04' hour to minute;
  534. interval
  535. ----------------
  536. 1 day 02:03:00
  537. (1 row)
  538. SELECT interval '1 2' hour to second;
  539. ERROR: invalid input syntax for type interval: "1 2"
  540. LINE 1: SELECT interval '1 2' hour to second;
  541. ^
  542. SELECT interval '1 2:03' hour to second;
  543. interval
  544. ----------------
  545. 1 day 02:03:00
  546. (1 row)
  547. SELECT interval '1 2:03:04' hour to second;
  548. interval
  549. ----------------
  550. 1 day 02:03:04
  551. (1 row)
  552. SELECT interval '1 2' minute to second;
  553. ERROR: invalid input syntax for type interval: "1 2"
  554. LINE 1: SELECT interval '1 2' minute to second;
  555. ^
  556. SELECT interval '1 2:03' minute to second;
  557. interval
  558. ----------------
  559. 1 day 00:02:03
  560. (1 row)
  561. SELECT interval '1 2:03:04' minute to second;
  562. interval
  563. ----------------
  564. 1 day 02:03:04
  565. (1 row)
  566. SELECT interval '1 +2:03' minute to second;
  567. interval
  568. ----------------
  569. 1 day 00:02:03
  570. (1 row)
  571. SELECT interval '1 +2:03:04' minute to second;
  572. interval
  573. ----------------
  574. 1 day 02:03:04
  575. (1 row)
  576. SELECT interval '1 -2:03' minute to second;
  577. interval
  578. -----------------
  579. 1 day -00:02:03
  580. (1 row)
  581. SELECT interval '1 -2:03:04' minute to second;
  582. interval
  583. -----------------
  584. 1 day -02:03:04
  585. (1 row)
  586. SELECT interval '123 11' day to hour; -- ok
  587. interval
  588. -------------------
  589. 123 days 11:00:00
  590. (1 row)
  591. SELECT interval '123 11' day; -- not ok
  592. ERROR: invalid input syntax for type interval: "123 11"
  593. LINE 1: SELECT interval '123 11' day;
  594. ^
  595. SELECT interval '123 11'; -- not ok, too ambiguous
  596. ERROR: invalid input syntax for type interval: "123 11"
  597. LINE 1: SELECT interval '123 11';
  598. ^
  599. SELECT interval '123 2:03 -2:04'; -- not ok, redundant hh:mm fields
  600. ERROR: invalid input syntax for type interval: "123 2:03 -2:04"
  601. LINE 1: SELECT interval '123 2:03 -2:04';
  602. ^
  603. -- test syntaxes for restricted precision
  604. SELECT interval(0) '1 day 01:23:45.6789';
  605. interval
  606. ----------------
  607. 1 day 01:23:46
  608. (1 row)
  609. SELECT interval(2) '1 day 01:23:45.6789';
  610. interval
  611. -------------------
  612. 1 day 01:23:45.68
  613. (1 row)
  614. SELECT interval '12:34.5678' minute to second(2); -- per SQL spec
  615. interval
  616. -------------
  617. 00:12:34.57
  618. (1 row)
  619. SELECT interval '1.234' second;
  620. interval
  621. --------------
  622. 00:00:01.234
  623. (1 row)
  624. SELECT interval '1.234' second(2);
  625. interval
  626. -------------
  627. 00:00:01.23
  628. (1 row)
  629. SELECT interval '1 2.345' day to second(2);
  630. ERROR: invalid input syntax for type interval: "1 2.345"
  631. LINE 1: SELECT interval '1 2.345' day to second(2);
  632. ^
  633. SELECT interval '1 2:03' day to second(2);
  634. interval
  635. ----------------
  636. 1 day 02:03:00
  637. (1 row)
  638. SELECT interval '1 2:03.4567' day to second(2);
  639. interval
  640. -------------------
  641. 1 day 00:02:03.46
  642. (1 row)
  643. SELECT interval '1 2:03:04.5678' day to second(2);
  644. interval
  645. -------------------
  646. 1 day 02:03:04.57
  647. (1 row)
  648. SELECT interval '1 2.345' hour to second(2);
  649. ERROR: invalid input syntax for type interval: "1 2.345"
  650. LINE 1: SELECT interval '1 2.345' hour to second(2);
  651. ^
  652. SELECT interval '1 2:03.45678' hour to second(2);
  653. interval
  654. -------------------
  655. 1 day 00:02:03.46
  656. (1 row)
  657. SELECT interval '1 2:03:04.5678' hour to second(2);
  658. interval
  659. -------------------
  660. 1 day 02:03:04.57
  661. (1 row)
  662. SELECT interval '1 2.3456' minute to second(2);
  663. ERROR: invalid input syntax for type interval: "1 2.3456"
  664. LINE 1: SELECT interval '1 2.3456' minute to second(2);
  665. ^
  666. SELECT interval '1 2:03.5678' minute to second(2);
  667. interval
  668. -------------------
  669. 1 day 00:02:03.57
  670. (1 row)
  671. SELECT interval '1 2:03:04.5678' minute to second(2);
  672. interval
  673. -------------------
  674. 1 day 02:03:04.57
  675. (1 row)
  676. -- test casting to restricted precision (bug #14479)
  677. SELECT f1, f1::INTERVAL DAY TO MINUTE AS "minutes",
  678. (f1 + INTERVAL '1 month')::INTERVAL MONTH::INTERVAL YEAR AS "years"
  679. FROM interval_tbl;
  680. f1 | minutes | years
  681. -----------------+-----------------+----------
  682. 00:01:00 | 00:01:00 | 00:00:00
  683. 05:00:00 | 05:00:00 | 00:00:00
  684. 10 days | 10 days | 00:00:00
  685. 34 years | 34 years | 34 years
  686. 3 mons | 3 mons | 00:00:00
  687. -00:00:14 | 00:00:00 | 00:00:00
  688. 1 day 02:03:04 | 1 day 02:03:00 | 00:00:00
  689. 6 years | 6 years | 6 years
  690. 5 mons | 5 mons | 00:00:00
  691. 5 mons 12:00:00 | 5 mons 12:00:00 | 00:00:00
  692. (10 rows)
  693. -- test inputting and outputting SQL standard interval literals
  694. SET IntervalStyle TO sql_standard;
  695. SELECT interval '0' AS "zero",
  696. interval '1-2' year to month AS "year-month",
  697. interval '1 2:03:04' day to second AS "day-time",
  698. - interval '1-2' AS "negative year-month",
  699. - interval '1 2:03:04' AS "negative day-time";
  700. zero | year-month | day-time | negative year-month | negative day-time
  701. ------+------------+-----------+---------------------+-------------------
  702. 0 | 1-2 | 1 2:03:04 | -1-2 | -1 2:03:04
  703. (1 row)
  704. -- test input of some not-quite-standard interval values in the sql style
  705. SET IntervalStyle TO postgres;
  706. SELECT interval '+1 -1:00:00',
  707. interval '-1 +1:00:00',
  708. interval '+1-2 -3 +4:05:06.789',
  709. interval '-1-2 +3 -4:05:06.789';
  710. interval | interval | interval | interval
  711. -----------------+-------------------+-------------------------------------+----------------------------------------
  712. 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
  713. (1 row)
  714. -- test output of couple non-standard interval values in the sql style
  715. SET IntervalStyle TO sql_standard;
  716. SELECT interval '1 day -1 hours',
  717. interval '-1 days +1 hours',
  718. interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds',
  719. - interval '1 years 2 months -3 days 4 hours 5 minutes 6.789 seconds';
  720. interval | interval | interval | ?column?
  721. ------------------+------------------+----------------------+----------------------
  722. +0-0 +1 -1:00:00 | +0-0 -1 +1:00:00 | +1-2 -3 +4:05:06.789 | -1-2 +3 -4:05:06.789
  723. (1 row)
  724. -- test outputting iso8601 intervals
  725. SET IntervalStyle to iso_8601;
  726. select interval '0' AS "zero",
  727. interval '1-2' AS "a year 2 months",
  728. interval '1 2:03:04' AS "a bit over a day",
  729. interval '2:03:04.45679' AS "a bit over 2 hours",
  730. (interval '1-2' + interval '3 4:05:06.7') AS "all fields",
  731. (interval '1-2' - interval '3 4:05:06.7') AS "mixed sign",
  732. (- interval '1-2' + interval '3 4:05:06.7') AS "negative";
  733. zero | a year 2 months | a bit over a day | a bit over 2 hours | all fields | mixed sign | negative
  734. ------+-----------------+------------------+--------------------+------------------+----------------------+--------------------
  735. PT0S | P1Y2M | P1DT2H3M4S | PT2H3M4.45679S | P1Y2M3DT4H5M6.7S | P1Y2M-3DT-4H-5M-6.7S | P-1Y-2M3DT4H5M6.7S
  736. (1 row)
  737. -- test inputting ISO 8601 4.4.2.1 "Format With Time Unit Designators"
  738. SET IntervalStyle to sql_standard;
  739. select interval 'P0Y' AS "zero",
  740. interval 'P1Y2M' AS "a year 2 months",
  741. interval 'P1W' AS "a week",
  742. interval 'P1DT2H3M4S' AS "a bit over a day",
  743. interval 'P1Y2M3DT4H5M6.7S' AS "all fields",
  744. interval 'P-1Y-2M-3DT-4H-5M-6.7S' AS "negative",
  745. interval 'PT-0.1S' AS "fractional second";
  746. zero | a year 2 months | a week | a bit over a day | all fields | negative | fractional second
  747. ------+-----------------+-----------+------------------+--------------------+--------------------+-------------------
  748. 0 | 1-2 | 7 0:00:00 | 1 2:03:04 | +1-2 +3 +4:05:06.7 | -1-2 -3 -4:05:06.7 | -0:00:00.1
  749. (1 row)
  750. -- test inputting ISO 8601 4.4.2.2 "Alternative Format"
  751. SET IntervalStyle to postgres;
  752. select interval 'P00021015T103020' AS "ISO8601 Basic Format",
  753. interval 'P0002-10-15T10:30:20' AS "ISO8601 Extended Format";
  754. ISO8601 Basic Format | ISO8601 Extended Format
  755. ----------------------------------+----------------------------------
  756. 2 years 10 mons 15 days 10:30:20 | 2 years 10 mons 15 days 10:30:20
  757. (1 row)
  758. -- Make sure optional ISO8601 alternative format fields are optional.
  759. select interval 'P0002' AS "year only",
  760. interval 'P0002-10' AS "year month",
  761. interval 'P0002-10-15' AS "year month day",
  762. interval 'P0002T1S' AS "year only plus time",
  763. interval 'P0002-10T1S' AS "year month plus time",
  764. interval 'P0002-10-15T1S' AS "year month day plus time",
  765. interval 'PT10' AS "hour only",
  766. interval 'PT10:30' AS "hour minute";
  767. year only | year month | year month day | year only plus time | year month plus time | year month day plus time | hour only | hour minute
  768. -----------+-----------------+-------------------------+---------------------+--------------------------+----------------------------------+-----------+-------------
  769. 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
  770. (1 row)
  771. -- test a couple rounding cases that changed since 8.3 w/ HAVE_INT64_TIMESTAMP.
  772. SET IntervalStyle to postgres_verbose;
  773. select interval '-10 mons -3 days +03:55:06.70';
  774. interval
  775. --------------------------------------------------
  776. @ 10 mons 3 days -3 hours -55 mins -6.7 secs ago
  777. (1 row)
  778. select interval '1 year 2 mons 3 days 04:05:06.699999';
  779. interval
  780. -----------------------------------------------------
  781. @ 1 year 2 mons 3 days 4 hours 5 mins 6.699999 secs
  782. (1 row)
  783. select interval '0:0:0.7', interval '@ 0.70 secs', interval '0.7 seconds';
  784. interval | interval | interval
  785. ------------+------------+------------
  786. @ 0.7 secs | @ 0.7 secs | @ 0.7 secs
  787. (1 row)
  788. -- check that '30 days' equals '1 month' according to the hash function
  789. select '30 days'::interval = '1 month'::interval as t;
  790. t
  791. ---
  792. t
  793. (1 row)
  794. select interval_hash('30 days'::interval) = interval_hash('1 month'::interval) as t;
  795. t
  796. ---
  797. t
  798. (1 row)
  799. -- numeric constructor
  800. select make_interval(years := 2);
  801. make_interval
  802. ---------------
  803. @ 2 years
  804. (1 row)
  805. select make_interval(years := 1, months := 6);
  806. make_interval
  807. -----------------
  808. @ 1 year 6 mons
  809. (1 row)
  810. select make_interval(years := 1, months := -1, weeks := 5, days := -7, hours := 25, mins := -180);
  811. make_interval
  812. ----------------------------
  813. @ 11 mons 28 days 22 hours
  814. (1 row)
  815. select make_interval() = make_interval(years := 0, months := 0, weeks := 0, days := 0, mins := 0, secs := 0.0);
  816. ?column?
  817. ----------
  818. t
  819. (1 row)
  820. select make_interval(hours := -2, mins := -10, secs := -25.3);
  821. make_interval
  822. ---------------------------------
  823. @ 2 hours 10 mins 25.3 secs ago
  824. (1 row)
  825. select make_interval(years := 'inf'::float::int);
  826. ERROR: integer out of range
  827. select make_interval(months := 'NaN'::float::int);
  828. ERROR: integer out of range
  829. select make_interval(secs := 'inf');
  830. ERROR: interval out of range
  831. select make_interval(secs := 'NaN');
  832. ERROR: interval out of range
  833. select make_interval(secs := 7e12);
  834. make_interval
  835. ------------------------------------
  836. @ 1944444444 hours 26 mins 40 secs
  837. (1 row)
  838. --
  839. -- test EXTRACT
  840. --
  841. SELECT f1,
  842. EXTRACT(MICROSECOND FROM f1) AS MICROSECOND,
  843. EXTRACT(MILLISECOND FROM f1) AS MILLISECOND,
  844. EXTRACT(SECOND FROM f1) AS SECOND,
  845. EXTRACT(MINUTE FROM f1) AS MINUTE,
  846. EXTRACT(HOUR FROM f1) AS HOUR,
  847. EXTRACT(DAY FROM f1) AS DAY,
  848. EXTRACT(MONTH FROM f1) AS MONTH,
  849. EXTRACT(QUARTER FROM f1) AS QUARTER,
  850. EXTRACT(YEAR FROM f1) AS YEAR,
  851. EXTRACT(DECADE FROM f1) AS DECADE,
  852. EXTRACT(CENTURY FROM f1) AS CENTURY,
  853. EXTRACT(MILLENNIUM FROM f1) AS MILLENNIUM,
  854. EXTRACT(EPOCH FROM f1) AS EPOCH
  855. FROM INTERVAL_TBL;
  856. f1 | microsecond | millisecond | second | minute | hour | day | month | quarter | year | decade | century | millennium | epoch
  857. -------------------------------+-------------+-------------+------------+--------+------+-----+-------+---------+------+--------+---------+------------+-------------------
  858. @ 1 min | 0 | 0.000 | 0.000000 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 60.000000
  859. @ 5 hours | 0 | 0.000 | 0.000000 | 0 | 5 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 18000.000000
  860. @ 10 days | 0 | 0.000 | 0.000000 | 0 | 0 | 10 | 0 | 1 | 0 | 0 | 0 | 0 | 864000.000000
  861. @ 34 years | 0 | 0.000 | 0.000000 | 0 | 0 | 0 | 0 | 1 | 34 | 3 | 0 | 0 | 1072958400.000000
  862. @ 3 mons | 0 | 0.000 | 0.000000 | 0 | 0 | 0 | 3 | 2 | 0 | 0 | 0 | 0 | 7776000.000000
  863. @ 14 secs ago | -14000000 | -14000.000 | -14.000000 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | -14.000000
  864. @ 1 day 2 hours 3 mins 4 secs | 4000000 | 4000.000 | 4.000000 | 3 | 2 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 93784.000000
  865. @ 6 years | 0 | 0.000 | 0.000000 | 0 | 0 | 0 | 0 | 1 | 6 | 0 | 0 | 0 | 189345600.000000
  866. @ 5 mons | 0 | 0.000 | 0.000000 | 0 | 0 | 0 | 5 | 2 | 0 | 0 | 0 | 0 | 12960000.000000
  867. @ 5 mons 12 hours | 0 | 0.000 | 0.000000 | 0 | 12 | 0 | 5 | 2 | 0 | 0 | 0 | 0 | 13003200.000000
  868. (10 rows)
  869. SELECT EXTRACT(FORTNIGHT FROM INTERVAL '2 days'); -- error
  870. ERROR: interval units "fortnight" not recognized
  871. SELECT EXTRACT(TIMEZONE FROM INTERVAL '2 days'); -- error
  872. ERROR: interval units "timezone" not supported
  873. SELECT EXTRACT(DECADE FROM INTERVAL '100 y');
  874. extract
  875. ---------
  876. 10
  877. (1 row)
  878. SELECT EXTRACT(DECADE FROM INTERVAL '99 y');
  879. extract
  880. ---------
  881. 9
  882. (1 row)
  883. SELECT EXTRACT(DECADE FROM INTERVAL '-99 y');
  884. extract
  885. ---------
  886. -9
  887. (1 row)
  888. SELECT EXTRACT(DECADE FROM INTERVAL '-100 y');
  889. extract
  890. ---------
  891. -10
  892. (1 row)
  893. SELECT EXTRACT(CENTURY FROM INTERVAL '100 y');
  894. extract
  895. ---------
  896. 1
  897. (1 row)
  898. SELECT EXTRACT(CENTURY FROM INTERVAL '99 y');
  899. extract
  900. ---------
  901. 0
  902. (1 row)
  903. SELECT EXTRACT(CENTURY FROM INTERVAL '-99 y');
  904. extract
  905. ---------
  906. 0
  907. (1 row)
  908. SELECT EXTRACT(CENTURY FROM INTERVAL '-100 y');
  909. extract
  910. ---------
  911. -1
  912. (1 row)
  913. -- date_part implementation is mostly the same as extract, so only
  914. -- test a few cases for additional coverage.
  915. SELECT f1,
  916. date_part('microsecond', f1) AS microsecond,
  917. date_part('millisecond', f1) AS millisecond,
  918. date_part('second', f1) AS second,
  919. date_part('epoch', f1) AS epoch
  920. FROM INTERVAL_TBL;
  921. f1 | microsecond | millisecond | second | epoch
  922. -------------------------------+-------------+-------------+--------+------------
  923. @ 1 min | 0 | 0 | 0 | 60
  924. @ 5 hours | 0 | 0 | 0 | 18000
  925. @ 10 days | 0 | 0 | 0 | 864000
  926. @ 34 years | 0 | 0 | 0 | 1072958400
  927. @ 3 mons | 0 | 0 | 0 | 7776000
  928. @ 14 secs ago | -14000000 | -14000 | -14 | -14
  929. @ 1 day 2 hours 3 mins 4 secs | 4000000 | 4000 | 4 | 93784
  930. @ 6 years | 0 | 0 | 0 | 189345600
  931. @ 5 mons | 0 | 0 | 0 | 12960000
  932. @ 5 mons 12 hours | 0 | 0 | 0 | 13003200
  933. (10 rows)
  934. -- internal overflow test case
  935. SELECT extract(epoch from interval '1000000000 days');
  936. extract
  937. -----------------------
  938. 86400000000000.000000
  939. (1 row)