interval.slt 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. mode cockroach
  10. query T colnames
  11. SELECT INTERVAL '1';
  12. ----
  13. interval
  14. 00:00:01
  15. ## SQL Standard
  16. query T
  17. SELECT INTERVAL '1:';
  18. ----
  19. 01:00:00
  20. query T
  21. SELECT INTERVAL '1-';
  22. ----
  23. 1 year
  24. query T
  25. SELECT INTERVAL '1 2:';
  26. ----
  27. 1 day 02:00:00
  28. # Parse Y-M H:M.
  29. query T
  30. SELECT INTERVAL '1-2 3:4';
  31. ----
  32. 1 year 2 months 03:04:00
  33. # Parse Y-M D H:M.
  34. query T
  35. SELECT INTERVAL '1-2 3 4:5';
  36. ----
  37. 1 year 2 months 3 days 04:05:00
  38. # Parse Y- D H:
  39. query T
  40. SELECT INTERVAL '1- 2 3:';
  41. ----
  42. 1 year 2 days 03:00:00
  43. # Allow parts in any order
  44. query T
  45. SELECT INTERVAL '1:2:3.4 5-6';
  46. ----
  47. 5 years 6 months 01:02:03.4
  48. # Implicit SECOND for ambiguous part.
  49. query T
  50. SELECT INTERVAL '1-2 3';
  51. ----
  52. 1 year 2 months 00:00:03
  53. # Explicitly set ambiguous part.
  54. query T
  55. SELECT INTERVAL '1-2 3' HOUR;
  56. ----
  57. 1 year 2 months 03:00:00
  58. # Disallow components to be set twice.
  59. statement error invalid input syntax for type interval: YEAR field set twice: "1 year 2 years"
  60. SELECT INTERVAL '1 year 2 years'
  61. statement error invalid input syntax for type interval: YEAR or MONTH field set twice: "1-2 3-4"
  62. SELECT INTERVAL '1-2 3-4'
  63. statement error invalid input syntax for type interval: YEAR field set twice: "1-2 3 year"
  64. SELECT INTERVAL '1-2 3 year'
  65. statement error invalid input syntax for type interval: MONTH field set twice: "1-2 3"
  66. SELECT INTERVAL '1-2 3' MONTH;
  67. # 5 would be parsed as second, but the H:M:S.NS
  68. # group was already set by 3:4/
  69. statement error invalid input syntax for type interval: SECOND field set twice: "1-2 3:4 5"
  70. SELECT INTERVAL '1-2 3:4 5';
  71. # Treat trailing TimeUnit as terminating range.
  72. query T
  73. SELECT INTERVAL '1-2 3 4:5:6.7' YEAR;
  74. ----
  75. 1 year
  76. query T
  77. SELECT INTERVAL '1-2 3 4:5:6.7' MONTH;
  78. ----
  79. 1 year 2 months
  80. query T
  81. SELECT INTERVAL '1-2 3 4:5:6.7' DAY;
  82. ----
  83. 1 year 2 months 3 days
  84. query T
  85. SELECT INTERVAL '1-2 3 4:5:6.7' HOUR;
  86. ----
  87. 1 year 2 months 3 days 04:00:00
  88. query T
  89. SELECT INTERVAL '1-2 3 4:5:6.7' MINUTE;
  90. ----
  91. 1 year 2 months 3 days 04:05:00
  92. query T
  93. SELECT INTERVAL '1-2 3 4:5:6.7' SECOND;
  94. ----
  95. 1 year 2 months 3 days 04:05:06.7
  96. # Treat trailing DateTimeFields as range of components to process.
  97. query T
  98. SELECT INTERVAL '1-2 3 4:5:6.7' MONTH TO MINUTE;
  99. ----
  100. 2 months 3 days 04:05:00
  101. query T
  102. SELECT INTERVAL '1-2 3 4:5:6.7' DAY TO HOUR;
  103. ----
  104. 3 days 04:00:00
  105. query T
  106. SELECT INTERVAL '12:34';
  107. ----
  108. 12:34:00
  109. query T
  110. SELECT INTERVAL '12:34' HOUR;
  111. ----
  112. 12:00:00
  113. query T
  114. SELECT INTERVAL '12:34' MINUTE;
  115. ----
  116. 12:34:00
  117. query T
  118. SELECT INTERVAL '12:34' SECOND;
  119. ----
  120. 12:34:00
  121. query T
  122. SELECT INTERVAL '12:34' HOUR TO MINUTE;
  123. ----
  124. 12:34:00
  125. query T
  126. SELECT INTERVAL '12:34' HOUR TO SECOND;
  127. ----
  128. 12:34:00
  129. query T
  130. SELECT INTERVAL '12:34' MINUTE TO SECOND;
  131. ----
  132. 00:12:34
  133. query T
  134. SELECT INTERVAL '12:34:56';
  135. ----
  136. 12:34:56
  137. query T
  138. SELECT INTERVAL '12:34:56' HOUR;
  139. ----
  140. 12:00:00
  141. query T
  142. SELECT INTERVAL '12:34:56' MINUTE;
  143. ----
  144. 12:34:00
  145. query T
  146. SELECT INTERVAL '12:34:56' SECOND;
  147. ----
  148. 12:34:56
  149. query T
  150. SELECT INTERVAL '12:34:56' HOUR TO MINUTE;
  151. ----
  152. 12:34:00
  153. query T
  154. SELECT INTERVAL '12:34:56' HOUR TO SECOND;
  155. ----
  156. 12:34:56
  157. query T
  158. SELECT INTERVAL '12:34:56' MINUTE TO SECOND;
  159. ----
  160. 00:34:56
  161. query T
  162. SELECT INTERVAL '12:34' DAY TO SECOND;
  163. ----
  164. 12:34:00
  165. query T
  166. SELECT INTERVAL '12:34' MONTH TO SECOND;
  167. ----
  168. 12:34:00
  169. # Disallow ranges where trailing element is greater than lead.
  170. statement error
  171. SELECT INTERVAL '1-2 3 4:5:6.7' MINUTE TO MONTH;
  172. # Disallow ranges where elements are explicitly equal.
  173. statement error
  174. SELECT INTERVAL '1-2 3 4:5:6.7' SECOND TO SECOND;
  175. # Disambiguate component (DAY), and treat as range.
  176. query T
  177. SELECT INTERVAL '1:2:3.4 5-6 7' DAY;
  178. ----
  179. 5 years 6 months 7 days
  180. # Disambiguate component (YEAR), but disallow because
  181. # the group it's in has been closed.
  182. statement error invalid input syntax for type interval: YEAR field set twice: "1:2:3.4 5-6 7"
  183. SELECT INTERVAL '1:2:3.4 5-6 7' YEAR;
  184. # Negative components
  185. query T
  186. SELECT INTERVAL '-1-2 -3 -4:5:6.7';
  187. ----
  188. -1 years -2 months -3 days -04:05:06.7
  189. # Mixed signs
  190. query T
  191. SELECT INTERVAL '-1-2 3 -4:5:6.7';
  192. ----
  193. -1 years -2 months +3 days -04:05:06.7
  194. # Allow skipping components.
  195. query T
  196. SELECT INTERVAL '1-2 3 4::.5';
  197. ----
  198. 1 year 2 months 3 days 04:00:00.5
  199. # Mix negatives and missing components.
  200. query T
  201. SELECT INTERVAL '-1-2 -3 4::.5';
  202. ----
  203. -1 years -2 months -3 days +04:00:00.5
  204. query T
  205. SELECT INTERVAL '-1-2 -3 -4::.5';
  206. ----
  207. -1 years -2 months -3 days -04:00:00.5
  208. # Parse - and infer second value.
  209. query T
  210. SELECT INTERVAL '-::1.27';
  211. ----
  212. -00:00:01.27
  213. # Trim all leading colons.
  214. query T
  215. SELECT INTERVAL ':::::1.27';
  216. ----
  217. 00:00:01.27
  218. query T
  219. SELECT INTERVAL ':::1- ::2 ::::3:';
  220. ----
  221. 1 year 2 days 03:00:00
  222. # Only trim leading colons if they are not preceded by any
  223. # other characters.
  224. statement error
  225. SELECT INTERVAL '-:::::1.27';
  226. # Disallow multiple ambiguous parts; the second part is ambiguous
  227. # because the leading colons are trimmed.
  228. statement error
  229. SELECT INTERVAL '-1 ::.27';
  230. # Negative time total with negative nanoseconds.
  231. query T
  232. SELECT INTERVAL '-1 -:.27';
  233. ----
  234. -1 days -00:00:00.27
  235. # Positive total time with positive nanoseconds.
  236. query T
  237. SELECT INTERVAL '1 0:.27';
  238. ----
  239. 1 day 00:00:00.27
  240. # Zero value
  241. query T
  242. SELECT INTERVAL '0-0 0 0:0:0.0';
  243. ----
  244. 00:00:00
  245. # Oversized components in SQL standard-style variables
  246. statement error invalid input syntax for type interval: MONTH must be \[-12, 12\], got 13: "100-13"
  247. SELECT INTERVAL '100-13';
  248. statement error invalid input syntax for type interval: MINUTE must be \[-59, 59\], got 61: "100-11 366 250:61"
  249. SELECT INTERVAL '100-11 366 250:61';
  250. statement error invalid input syntax for type interval: SECOND must be \[-60, 60\], got 61: "100-11 366 250:59:61"
  251. SELECT INTERVAL '100-11 366 250:59:61';
  252. # Invalid syntax
  253. statement error invalid input syntax for type interval: have unprocessed tokens .500000000
  254. SELECT INTERVAL '1:2:3.4.5';
  255. statement error
  256. SELECT INTERVAL '1+2:3.4';
  257. statement error unknown units x
  258. SELECT INTERVAL '1x2:3.4';
  259. ## PostgreSQL format
  260. query T
  261. SELECT INTERVAL '1 year 2 month 3 day 4 hour 5 minute 6.7 second';
  262. ----
  263. 1 year 2 months 3 days 04:05:06.7
  264. # Plural TimeUnits.
  265. query T
  266. SELECT INTERVAL '1 years 2 months 3 days 4 hours 5 minutes 6.7 seconds';
  267. ----
  268. 1 year 2 months 3 days 04:05:06.7
  269. # Shorthand.
  270. query T
  271. SELECT INTERVAL '1y 2mon 3d 4h 5m 6.7s';
  272. ----
  273. 1 year 2 months 3 days 04:05:06.7
  274. # Commutative expressions.
  275. query T
  276. SELECT INTERVAL '6.7 seconds 5 minutes 3 days 4 hours 1 year 2 month';
  277. ----
  278. 1 year 2 months 3 days 04:05:06.7
  279. # Allow mix of positive and negative components.
  280. query T
  281. SELECT INTERVAL '-6.7 seconds 5 minutes -3 days 4 hours -1 year 2 month';
  282. ----
  283. -10 months -3 days +04:04:53.3
  284. # Allow fractional components.
  285. query T
  286. SELECT INTERVAL '1y 2.3mon 4.5d';
  287. ----
  288. 1 year 2 months 13 days 12:00:00
  289. query T
  290. SELECT INTERVAL '-6.7 seconds 5.6 minutes -3.4 days 4.5 hours -1.2 year 2.3 month';
  291. ----
  292. -1 years +6 days -05:00:30.7
  293. # Positive total time with negative nanoseconds.
  294. query T
  295. SELECT INTERVAL '1 day -0.27 seconds';
  296. ----
  297. 1 day -00:00:00.27
  298. # Negative total time with positive nanoseconds.
  299. query T
  300. SELECT INTERVAL '-1 day 0.27 seconds';
  301. ----
  302. -1 days +00:00:00.27
  303. # Fractional year expands down to months.
  304. query T
  305. SELECT INTERVAL '10.333 years';
  306. ----
  307. 10 years 3 months
  308. # Fractional part with ambiguity and range.
  309. query T
  310. SELECT INTERVAL '10.333' years;
  311. ----
  312. 10 years
  313. # Fractional month, includes nanoseconds.
  314. query T
  315. SELECT INTERVAL '1.5555555 month';
  316. ----
  317. 1 month 16 days 15:59:59.856
  318. # Fractional day, includes nanoseconds.
  319. query T
  320. SELECT INTERVAL '1.5555555 day';
  321. ----
  322. 1 day 13:19:59.9952
  323. # Fractional minute, includes nanoseconds
  324. query T
  325. SELECT INTERVAL '1.5555555 minute';
  326. ----
  327. 00:01:33.33333
  328. # Print leading 0s in nanoseconds
  329. query T
  330. SELECT INTERVAL '.001 second';
  331. ----
  332. 00:00:00.001
  333. # Carry over nanoseconds into seconds.
  334. query T
  335. SELECT INTERVAL '.33333 minute .1 second';
  336. ----
  337. 00:00:20.0998
  338. # PostgreSQL-style without spaces.
  339. query T
  340. SELECT INTERVAL '1year 2months 3.4days';
  341. ----
  342. 1 year 2 months 3 days 09:36:00
  343. # Zero values.
  344. query T
  345. SELECT INTERVAL '0 year 0 month 0 day 0 hour 0 minute 0.0 second';
  346. ----
  347. 00:00:00
  348. # Fails any non-DateTimeField string.
  349. statement error unknown units foo
  350. SELECT INTERVAL '0 foo';
  351. ## Misc.
  352. # Mix SQL standard- and PostgreSQL-style.
  353. query T
  354. SELECT INTERVAL '1-2 3:4 5 day';
  355. ----
  356. 1 year 2 months 5 days 03:04:00
  357. # Parse + signs.
  358. query T
  359. SELECT INTERVAL '+1 year +2 days +3:4:5.6';
  360. ----
  361. 1 year 2 days 03:04:05.6
  362. # Differentiate between trailing DateTimeField name and
  363. # PostgreSQL TimeUnit.
  364. query T
  365. SELECT INTERVAL '01:02:03' MINUTE;
  366. ----
  367. 01:02:00
  368. query T
  369. SELECT INTERVAL '01:02:03minute';
  370. ----
  371. 01:02:03
  372. query T
  373. SELECT INTERVAL '01:02:03minute hour day year';
  374. ----
  375. 01:02:03
  376. # Use larger numbers.
  377. query T
  378. SELECT INTERVAL '-13-10 15 -16::.27';
  379. ----
  380. -13 years -10 months +15 days -16:00:00.27
  381. query T
  382. SELECT INTERVAL '-13-10 -15 -16::.27';
  383. ----
  384. -13 years -10 months -15 days -16:00:00.27
  385. # Parsing output as input.
  386. query T
  387. SELECT INTERVAL '-13 years -10 months -15 days -16:00:00.27';
  388. ----
  389. -13 years -10 months -15 days -16:00:00.27
  390. query T
  391. SELECT INTERVAL '-13 years -10 months +14 days +07:59:59.73';
  392. ----
  393. -13 years -10 months +14 days 07:59:59.73
  394. # Commutative mixed expressions.
  395. query T
  396. SELECT INTERVAL '5 day 3:4 1-2';
  397. ----
  398. 1 year 2 months 5 days 03:04:00
  399. query T
  400. SELECT INTERVAL '1-2 3:4 5 day';
  401. ----
  402. 1 year 2 months 5 days 03:04:00
  403. # Mix style allowed, but cannot assigning to closed group.
  404. statement error invalid input syntax for type interval: SECOND field set twice: "1-2 3:4 5 second"
  405. SELECT INTERVAL '1-2 3:4 5 second';
  406. # Commutativity means this is also not allowed.
  407. statement error invalid input syntax for type interval: HOUR, MINUTE, SECOND field set twice: "1-2 5 second 3:4"
  408. SELECT INTERVAL '1-2 5 second 3:4';
  409. # Fractional month in addition to other fields.
  410. query T
  411. SELECT INTERVAL '1.5555 month 2 3:4:5.6';
  412. ----
  413. 1 month 18 days 19:01:41.6
  414. # Fractional month with terminating range. Truncation should
  415. # only happen after computation.
  416. query T
  417. SELECT INTERVAL '1.5555 month 2 3:4:5.6' HOUR;
  418. ----
  419. 1 month 18 days 19:00:00
  420. # Fractional month with leading and terminating range.
  421. # Again, truncation should only happen after
  422. # computation.
  423. query T
  424. SELECT INTERVAL '1.5555 month 2 3:4:5.6' DAY TO HOUR;
  425. ----
  426. 18 days 19:00:00
  427. # Only allow disambiguation if ambiguous element
  428. # is final element of interval string.
  429. statement error
  430. SELECT INTERVAL '1 2-3 4:5' DAY
  431. ## Nanosecond precision
  432. # Default nanosecond precision of 6, rounded.
  433. query T
  434. SELECT INTERVAL '1.23456789' SECOND
  435. ----
  436. 00:00:01.234568
  437. # Allow specific nanosecond precision, which rounds.
  438. query T
  439. SELECT INTERVAL '1.23456789' SECOND(5);
  440. ----
  441. 00:00:01.23457
  442. query T
  443. SELECT INTERVAL '-1.23456789' SECOND(5);
  444. ----
  445. -00:00:01.23457
  446. # Ensure doesn't round when rounded digit is < 5.
  447. query T
  448. SELECT INTERVAL '1.23456789' SECOND(2);
  449. ----
  450. 00:00:01.23
  451. query T
  452. SELECT INTERVAL '-1.23456789' SECOND(2);
  453. ----
  454. -00:00:01.23
  455. # Postgres rounds micros to 6 digits every time a value is added before applying given precision
  456. query T
  457. SELECT INTERVAL '1.2345649' SECOND(5);
  458. ----
  459. 00:00:01.23457
  460. query T
  461. SELECT INTERVAL '-1.2345649' SECOND(5);
  462. ----
  463. -00:00:01.23457
  464. # Ensure overflow from rounding is handled properly
  465. query error interval field value out of range
  466. SELECT INTERVAL '2562047788:00:54.775807' SECOND(2);
  467. query error interval field value out of range
  468. SELECT INTERVAL '-2562047788:00:54.775808' SECOND(2);
  469. # 0.00000001 min is 0.0000006 seconds
  470. # 0.0006 millisecond is 0.0000006 seconds
  471. # 3.5 microseconds is 0.0000035 seconds
  472. # They each separately get rounded to 6 digits and added together
  473. query T
  474. SELECT INTERVAL '0.00000001 min 0.0006 millisecond 3.5 microsecond';
  475. ----
  476. 00:00:00.000006
  477. query T
  478. SELECT INTERVAL '0.00000001 min 0.0006 millisecond 3.5 microsecond' SECOND(5);
  479. ----
  480. 00:00:00.00001
  481. query T
  482. SELECT INTERVAL '-1.555555555 years 2.555555555 months -3.555555555 days 4.555555555 hours -5.555555555 minutes 6.555555555 seconds';
  483. ----
  484. -1 years -4 months +13 days 07:07:53.220829
  485. # Allow precision as second element in range.
  486. query T
  487. SELECT INTERVAL '1:2:31.23456789' MINUTE TO SECOND(2);
  488. ----
  489. 00:02:31.23
  490. # Do not allow more than 9 places of precision
  491. query T
  492. SELECT INTERVAL '1.999999999999999999 days'
  493. ----
  494. 1 day 23:59:59.999914
  495. # Do not allow more than 9 places of precision,
  496. # and properly convert TimeStrToken::Num() to
  497. # nanos.
  498. query T
  499. SELECT INTERVAL '1.999999999999999999days'
  500. ----
  501. 1 day 23:59:59.999914
  502. # Precision must be (0, 6)
  503. statement error SECOND precision must be \(0, 6\), have SECOND\(7\)
  504. SELECT INTERVAL '1 day 2-3 4' SECOND(7);
  505. # Only allow precision notation for trailing SECOND .
  506. statement error Expected end of statement, found SECOND
  507. SELECT INTERVAL '2-3 3:4' SECOND(1) to SECOND
  508. statement error Expected end of statement, found left parenthesis
  509. SELECT INTERVAL '2-3 3:4' DAY(1)
  510. # Only allow positive integer nanosecond precision
  511. statement error Expected end of statement, found string literal
  512. SELECT INTERVAL '1 day 2-3 4' SECOND(-1);
  513. # Arbitrary punctuation delimiters
  514. query T
  515. SELECT interval '02-01!1~01:02:03';
  516. ----
  517. 2 years 1 month 1 day 01:02:03
  518. query T
  519. SELECT interval '1! hour';
  520. ----
  521. 01:00:00
  522. statement error
  523. SELECT interval '02!01!1~01:02:03';
  524. ## Math
  525. # Support negating interval
  526. query T
  527. SELECT - INTERVAL '1-2 3 4:5:6.7';
  528. ----
  529. -1 years -2 months -3 days -04:05:06.7
  530. query T
  531. SELECT - INTERVAL '-1 year -2 months -3 days -04:05:06.7';
  532. ----
  533. 1 year 2 months 3 days 04:05:06.7
  534. query T
  535. SELECT - INTERVAL '1 year 2 months -3 days -04:05:06.7';
  536. ----
  537. -1 years -2 months +3 days 04:05:06.7
  538. query T
  539. SELECT - INTERVAL '-1 year -2 months 3 days 04:05:06.7';
  540. ----
  541. 1 year 2 months -3 days -04:05:06.7
  542. # Add, sub intervals
  543. query T
  544. SELECT INTERVAL '1-2 3 4:5:6.7' + INTERVAL '1-2 3 4:5:6.7';
  545. ----
  546. 2 years 4 months 6 days 08:10:13.4
  547. query T
  548. SELECT INTERVAL '1-2 3 4:5:6.7' - INTERVAL '7-6 5 4:3:2.1';
  549. ----
  550. -6 years -4 months -2 days +00:02:04.6
  551. query T
  552. SELECT INTERVAL '1-2 3 4:5:6.7' + - INTERVAL '7-6 5 4:3:2.1';
  553. ----
  554. -6 years -4 months -2 days +00:02:04.6
  555. query T
  556. SELECT INTERVAL '1-2 3 4:5:6.7' + INTERVAL '-7-6 -5 -4:3:2.1';
  557. ----
  558. -6 years -4 months -2 days +00:02:04.6
  559. query T
  560. SELECT INTERVAL '1-2 3 4:5:6.7' - INTERVAL '-7-6 -5 -4:3:2.1';
  561. ----
  562. 8 years 8 months 8 days 08:08:08.8
  563. query T
  564. SELECT INTERVAL '1-2 3 4:5:6.7' - INTERVAL '1-2 3 4:5:6.7'
  565. ----
  566. 00:00:00
  567. query T
  568. SELECT INTERVAL '1-2 3 4:5:6.7' + INTERVAL '-1-2 -3 -4:5:6.7'
  569. ----
  570. 00:00:00
  571. query T
  572. SELECT INTERVAL '-1-2 -3 -4:5:6.7' + INTERVAL '1-2 3 4:5:6.7'
  573. ----
  574. 00:00:00
  575. # Div, mul intervals
  576. query TT
  577. SELECT INTERVAL '1' MONTH * 0.5, 0.5 * INTERVAL '1' MONTH
  578. ----
  579. 15␠days 15␠days
  580. query T
  581. SELECT INTERVAL '1' MONTH / 30
  582. ----
  583. 1 day
  584. query T
  585. SELECT INTERVAL '1' YEAR * 13/12
  586. ----
  587. 1 year 1 month
  588. query error "10 years \* 10000000000" interval out of range
  589. SELECT INTERVAL '10 years' * 9999999999.999999999;
  590. query error "10 years / 0.000000000000000001" interval out of range
  591. SELECT INTERVAL '10 years' / 0.000000000000000001;
  592. # In an idiosyncratic fashion when dividing/multiplying an interval,
  593. # PostgreSQL considers a year 360 days. But when extracting an epoch,
  594. # it's considered 365.25 days instead.
  595. query T
  596. SELECT INTERVAL '1' YEAR / 360
  597. ----
  598. 1 day
  599. query T
  600. SELECT INTERVAL '1' YEAR * 0.9999999
  601. ----
  602. 11 months 29 days 23:59:56.8896
  603. query T
  604. SELECT INTERVAL '-1-2 -3 -4:5:6.7' * 999
  605. ----
  606. -1165 years -6 months -2997 days -4081:06:33.3
  607. # Division by zero
  608. statement error division by zero
  609. SELECT INTERVAL '1' YEAR / 0
  610. ## Largest values
  611. query T
  612. SELECT INTERVAL '2147483647 days 2562047788 hours 54.775807 seconds'
  613. ----
  614. 2147483647 days 2562047788:00:54.775807
  615. query T
  616. SELECT INTERVAL '-2147483648 days -2562047788 hours -54.775808 seconds'
  617. ----
  618. -2147483648 days -2562047788:00:54.775808
  619. query T
  620. SELECT INTERVAL '2147483647 days 2562047787 hours 59 minutes 59.999999 seconds'
  621. ----
  622. 2147483647 days 2562047787:59:59.999999
  623. query T
  624. SELECT INTERVAL '-2147483648 days -2562047786 hours -59 minutes -59.999999 seconds'
  625. ----
  626. -2147483648 days -2562047786:59:59.999999
  627. query T
  628. SELECT INTERVAL '-2147483648 days -2562047787 hours -59 minutes -59.999999 seconds'
  629. ----
  630. -2147483648 days -2562047787:59:59.999999
  631. query T
  632. SELECT INTERVAL '2147483647 months 2147483647 days 9223372036854775807 us'
  633. ----
  634. 178956970 years 7 months 2147483647 days 2562047788:00:54.775807
  635. query T
  636. SELECT INTERVAL '-2147483648 months -2147483648 days -9223372036854775808 us'
  637. ----
  638. -178956970 years -8 months -2147483648 days -2562047788:00:54.775808
  639. statement error Overflows maximum microseconds;
  640. SELECT INTERVAL '2147483647 days 2562047789 hours';
  641. statement error Overflows maximum microseconds;
  642. SELECT INTERVAL '-2147483648 days -2562047789 hours'
  643. statement error Overflows maximum microseconds;
  644. SELECT INTERVAL '-2147483648 days -2562047788 hours -60 min'
  645. statement error Overflows maximum microseconds;
  646. SELECT INTERVAL '-2147483648 days -2562047788 hours -59 min -60 sec'
  647. statement error "2147483647 days 2562047788:00:54 \+ 00:00:01" interval out of range
  648. SELECT INTERVAL '2147483647 days 2562047788 hours 54 seconds' + INTERVAL '1'
  649. statement error "-2147483648 days -2562047788:00:54 - 00:00:01" interval out of range
  650. SELECT INTERVAL '-2147483648 days -2562047788 hours -54 seconds' - INTERVAL '1'
  651. statement error "178956970 years 7 months / 0.99" interval out of range
  652. SELECT INTERVAL '2147483647' MONTH / 0.99
  653. statement error "-178956970 years -7 months \* 1.01" interval out of range
  654. SELECT INTERVAL '-2147483647' MONTH * 1.01
  655. # Largest number of cumulative nanoseconds
  656. query T
  657. SELECT INTERVAL '0.999999999 months 0.999999999 days 0.999999999 hours 0.999999999 minutes 0.999999999 seconds';
  658. ----
  659. 29 days 49:01:00.997318
  660. # Largest and smallest numbers of cumulative microseconds
  661. query T
  662. SELECT INTERVAL '2562047788.01521550194 hours';
  663. ----
  664. 2562047788:00:54.775804
  665. query T
  666. SELECT INTERVAL '2562047788.015215502 hours';
  667. ----
  668. 2562047788:00:54.775807
  669. query T
  670. SELECT INTERVAL '-2562047788.01521550222 hours';
  671. ----
  672. -2562047788:00:54.775807
  673. query T
  674. SELECT INTERVAL '-2562047788.0152155023 hours';
  675. ----
  676. -2562047788:00:54.775807
  677. query T
  678. SELECT INTERVAL '153722867280.912930117 minutes';
  679. ----
  680. 2562047788:00:54.775807
  681. query T
  682. SELECT INTERVAL '-153722867280.912930133 minutes';
  683. ----
  684. -2562047788:00:54.775808
  685. query T
  686. SELECT INTERVAL '9223372036854.775807 seconds';
  687. ----
  688. 2562047788:00:54.775807
  689. query T
  690. SELECT INTERVAL '-9223372036854.775808 seconds';
  691. ----
  692. -2562047788:00:54.775808
  693. query T
  694. SELECT INTERVAL '9223372036854775.807 milliseconds';
  695. ----
  696. 2562047788:00:54.775807
  697. query T
  698. SELECT INTERVAL '-9223372036854775.808 milliseconds';
  699. ----
  700. -2562047788:00:54.775808
  701. query T
  702. SELECT INTERVAL '9223372036854775807 microseconds'
  703. ----
  704. 2562047788:00:54.775807
  705. query T
  706. SELECT INTERVAL '-9223372036854775808 microseconds'
  707. ----
  708. -2562047788:00:54.775808
  709. ## Overflows
  710. statement error Overflows maximum months;
  711. SELECT INTERVAL '2147483647 millennium';
  712. statement error Overflows maximum months;
  713. SELECT INTERVAL '-2147483648 millennium';
  714. statement error Overflows maximum months;
  715. SELECT INTERVAL '2147483647 centuries';
  716. statement error Overflows maximum months;
  717. SELECT INTERVAL '-2147483648 centuries';
  718. statement error Overflows maximum months;
  719. SELECT INTERVAL '2147483647 decades';
  720. statement error Overflows maximum months;
  721. SELECT INTERVAL '-2147483648 decades';
  722. statement error Overflows maximum months;
  723. SELECT INTERVAL '2147483648 years';
  724. statement error Overflows maximum months;
  725. SELECT INTERVAL '-2147483649 years';
  726. statement error Overflows maximum months;
  727. SELECT INTERVAL '768614336404564651 year';
  728. statement error Overflows maximum months;
  729. SELECT INTERVAL '768614336404564650.7 year';
  730. statement error Overflows maximum months;
  731. SELECT INTERVAL '1 decade 2147483647 years';
  732. statement error Overflows maximum months;
  733. SELECT INTERVAL '1 century 2147483647 years';
  734. statement error Overflows maximum months;
  735. SELECT INTERVAL '1 millennium 2147483647 years';
  736. statement error Overflows maximum months;
  737. SELECT INTERVAL '-1 decade -2147483648 years';
  738. statement error Overflows maximum months;
  739. SELECT INTERVAL '-1 century -2147483648 years';
  740. statement error Overflows maximum months;
  741. SELECT INTERVAL '-1 millennium -2147483648 years';
  742. statement error Overflows maximum months;
  743. SELECT INTERVAL '2147483647 years 1 decade';
  744. statement error Overflows maximum months;
  745. SELECT INTERVAL '2147483647 years 1 century';
  746. statement error Overflows maximum months;
  747. SELECT INTERVAL '2147483647 years 1 millennium';
  748. statement error Overflows maximum months;
  749. SELECT INTERVAL '-2147483648 years -1 decade';
  750. statement error Overflows maximum months;
  751. SELECT INTERVAL '-2147483648 years -1 century';
  752. statement error Overflows maximum months;
  753. SELECT INTERVAL '-2147483648 years -1 millennium';
  754. statement error Overflows maximum months;
  755. SELECT INTERVAL '2147483648 months';
  756. statement error Overflows maximum months;
  757. SELECT INTERVAL '-2147483649 months';
  758. statement error Unable to parse value 9223372036854775808 as a number: number too large to fit in target type
  759. SELECT INTERVAL '9223372036854775808 months';
  760. statement error Overflows maximum months;
  761. SELECT INTERVAL '-9223372036854775808 months';
  762. statement error Overflows maximum months;
  763. SELECT INTERVAL '0.1 millennium 2147483647 months';
  764. statement error Overflows maximum months;
  765. SELECT INTERVAL '0.1 centuries 2147483647 months';
  766. statement error Overflows maximum months;
  767. SELECT INTERVAL '0.1 decades 2147483647 months';
  768. statement error Overflows maximum months;
  769. SELECT INTERVAL '0.1 yrs 2147483647 months';
  770. statement error Overflows maximum months;
  771. SELECT INTERVAL '-0.1 millennium -2147483648 months';
  772. statement error Overflows maximum months;
  773. SELECT INTERVAL '-0.1 centuries -2147483648 months';
  774. statement error Overflows maximum months;
  775. SELECT INTERVAL '-0.1 decades -2147483648 months';
  776. statement error Overflows maximum months;
  777. SELECT INTERVAL '-0.1 yrs -2147483648 months';
  778. statement error Overflows maximum months;
  779. SELECT INTERVAL '2147483647 months 0.1 millennium';
  780. statement error Overflows maximum months;
  781. SELECT INTERVAL '2147483647 months 0.1 centuries';
  782. statement error Overflows maximum months;
  783. SELECT INTERVAL '2147483647 months 0.1 decades';
  784. statement error Overflows maximum months;
  785. SELECT INTERVAL '2147483647 months 0.1 yrs';
  786. statement error Overflows maximum months;
  787. SELECT INTERVAL '-2147483648 months -0.1 millennium';
  788. statement error Overflows maximum months;
  789. SELECT INTERVAL '-2147483648 months -0.1 centuries';
  790. statement error Overflows maximum months;
  791. SELECT INTERVAL '-2147483648 months -0.1 decades';
  792. statement error Overflows maximum months;
  793. SELECT INTERVAL '-2147483648 months -0.1 yrs';
  794. statement error Overflows maximum days;
  795. SELECT INTERVAL '2147483648 days';
  796. statement error Overflows maximum days;
  797. SELECT INTERVAL '-2147483649 days';
  798. statement error Overflows maximum days;
  799. SELECT INTERVAL '0.1 months 2147483647 days';
  800. statement error Overflows maximum days;
  801. SELECT INTERVAL '-0.1 months -2147483648 days';
  802. statement error Overflows maximum days;
  803. SELECT INTERVAL '2147483647 days 0.1 months';
  804. statement error Overflows maximum days;
  805. SELECT INTERVAL '-2147483648 days -0.1 months';
  806. statement error Overflows maximum days;
  807. SELECT INTERVAL '106751991167300 days .1 month';
  808. statement error Overflows maximum days;
  809. SELECT INTERVAL '106751991167301 days';
  810. statement error Overflows maximum days;
  811. SELECT INTERVAL '106751991167300.9 days';
  812. statement error Overflows maximum microseconds;
  813. SELECT INTERVAL '2562047789 hours';
  814. statement error Overflows maximum microseconds;
  815. SELECT INTERVAL '-2562047789 hours';
  816. statement error Overflows maximum microseconds;
  817. SELECT INTERVAL '9223372036854775807 seconds 1 hour';
  818. statement error Overflows maximum microseconds;
  819. SELECT INTERVAL '9223372036854771807 seconds 1.9 hour';
  820. statement error Overflows maximum microseconds;
  821. SELECT INTERVAL '153722867281 minutes';
  822. statement error Overflows maximum microseconds;
  823. SELECT INTERVAL '-153722867281 minutes';
  824. statement error Overflows maximum microseconds;
  825. SELECT INTERVAL '9223372036854775807 seconds 1 minute';
  826. statement error Overflows maximum microseconds;
  827. SELECT INTERVAL '9223372036854775707 seconds 1.9 minute';
  828. statement error Overflows maximum microseconds;
  829. SELECT INTERVAL '9223372036855 seconds';
  830. statement error Overflows maximum microseconds;
  831. SELECT INTERVAL '-9223372036855 seconds';
  832. statement error Unable to parse value 9223372036854775808 as a number: number too large to fit in target type
  833. SELECT INTERVAL '9223372036854775808 seconds';
  834. statement error Overflows maximum microseconds;
  835. SELECT INTERVAL '-9223372036854775808 seconds';
  836. statement error Overflows maximum microseconds;
  837. SELECT INTERVAL '9223372036854777 millisecond';
  838. statement error Overflows maximum microseconds;
  839. SELECT INTERVAL '-9223372036854777 millisecond';
  840. statement error Unable to parse value 9223372036854775808 as a number: number too large to fit in target type
  841. SELECT INTERVAL '9223372036854775808 microsecond';
  842. statement error Unable to parse value 9223372036854775809 as a number: number too large to fit in target type
  843. SELECT INTERVAL '-9223372036854775809 microsecond';
  844. statement error Overflows maximum microseconds;
  845. SELECT INTERVAL '0.01 months 9223372036854775807 microseconds';
  846. statement error Overflows maximum microseconds;
  847. SELECT INTERVAL '-0.01 months -9223372036854775808 microseconds';
  848. statement error Overflows maximum microseconds;
  849. SELECT INTERVAL '9223372036854775807 microseconds 0.01 months';
  850. statement error Overflows maximum microseconds;
  851. SELECT INTERVAL '-9223372036854775808 microseconds -0.01 months';
  852. statement error Overflows maximum microseconds;
  853. SELECT INTERVAL '0.1 days 9223372036854775807 microseconds';
  854. statement error Overflows maximum microseconds;
  855. SELECT INTERVAL '-0.1 days -9223372036854775808 microseconds';
  856. statement error Overflows maximum microseconds;
  857. SELECT INTERVAL '9223372036854775807 microseconds 0.1 days';
  858. statement error Overflows maximum microseconds;
  859. SELECT INTERVAL '-9223372036854775808 microseconds -0.1 days';
  860. statement error Overflows maximum microseconds;
  861. SELECT INTERVAL '0.1 2562047788:0:54.775807';
  862. # 0 interval equality
  863. query B
  864. SELECT (interval '-1' day + interval '1' day) = (interval '1' day + interval '-1' day)
  865. ----
  866. true
  867. ## Millisecond/Microsecond parsing
  868. query T
  869. SELECT INTERVAL '1 milliseconds'
  870. ----
  871. 00:00:00.001
  872. query T
  873. SELECT INTERVAL '1002 milliseconds'
  874. ----
  875. 00:00:01.002
  876. query T
  877. SELECT INTERVAL '1002 milliseconds 1 second'
  878. ----
  879. 00:00:02.002
  880. query T
  881. SELECT INTERVAL '1 second 42 milliseconds'
  882. ----
  883. 00:00:01.042
  884. query T
  885. SELECT INTERVAL '1.0 second 42 milliseconds'
  886. ----
  887. 00:00:01.042
  888. query T
  889. SELECT INTERVAL '5 microseconds'
  890. ----
  891. 00:00:00.000005
  892. query T
  893. SELECT INTERVAL '5000006 microseconds'
  894. ----
  895. 00:00:05.000006
  896. query T
  897. SELECT INTERVAL '5000006 microseconds 1 second'
  898. ----
  899. 00:00:06.000006
  900. query T
  901. SELECT INTERVAL '27 second 24 microseconds'
  902. ----
  903. 00:00:27.000024
  904. query T
  905. SELECT INTERVAL '27.0 second 24 microseconds'
  906. ----
  907. 00:00:27.000024
  908. query T
  909. SELECT INTERVAL '27.0 second 46 milliseconds 24 microseconds'
  910. ----
  911. 00:00:27.046024
  912. statement error Cannot set MILLISECONDS or MICROSECONDS field if SECOND field has a fraction component
  913. SELECT INTERVAL '1.5 second 42 milliseconds'
  914. statement error Cannot set MILLISECONDS or MICROSECONDS field if SECOND field has a fraction component
  915. SELECT INTERVAL '1.5 second 43 microseconds'
  916. statement error Cannot set MILLISECONDS or MICROSECONDS field if SECOND field has a fraction component
  917. SELECT INTERVAL '1.5 second 42 milliseconds 43 microseconds'
  918. statement error Expected end of statement, found string literal
  919. SELECT INTERVAL '6 days 1.5 second 42 milliseconds' HOURS to MILLISECONDS
  920. ## Millennium/Century/Decade parsing
  921. query T
  922. SELECT INTERVAL '1 millennium'
  923. ----
  924. 1000 years
  925. query T
  926. SELECT INTERVAL '2 century'
  927. ----
  928. 200 years
  929. query T
  930. SELECT INTERVAL '3 decade'
  931. ----
  932. 30 years
  933. query T
  934. SELECT INTERVAL '4 year'
  935. ----
  936. 4 years
  937. query T
  938. SELECT INTERVAL '1 millenniums 2 centuries 3 decades 4 years'
  939. ----
  940. 1234 years
  941. query error "-178956970 years -8 months" interval out of range
  942. SELECT -INTERVAL '-2147483648 months';
  943. query error "-2147483648 days" interval out of range
  944. SELECT -INTERVAL '-2147483648 days';
  945. query error "-2562047788:00:54.775808" interval out of range
  946. SELECT -INTERVAL '-9223372036854775808 microseconds';
  947. query error "-1 months - -178956970 years -8 months" interval out of range
  948. SELECT INTERVAL '-1 months' - INTERVAL '-2147483648 months';
  949. query error "-1 days - -2147483648 days" interval out of range
  950. SELECT INTERVAL '-1 day' - INTERVAL '-2147483648 days';
  951. query error "-01:00:00 \+ -2562047788:00:00" interval out of range
  952. SELECT INTERVAL '-1 hours' + INTERVAL '-2562047788 hours';
  953. query error "-2147483648 days -2562047787:59:59.999999" interval out of range
  954. SELECT -INTERVAL '-2147483648 days -2562047787 hours -59 minutes -59.999999 seconds';
  955. # Cast Interval to time
  956. query T
  957. SELECT INTERVAL '-1 hour -2 minutes -3.45 seconds'::time;
  958. ----
  959. 22:57:56.55
  960. query T
  961. SELECT INTERVAL '1 hour 2 minutes 3.45 seconds'::time;
  962. ----
  963. 01:02:03.45
  964. query T
  965. SELECT INTERVAL '6 days'::time;
  966. ----
  967. 00:00:00
  968. query T
  969. SELECT INTERVAL '7 months'::time;
  970. ----
  971. 00:00:00
  972. query T
  973. SELECT INTERVAL '-86400000001 us'::time;
  974. ----
  975. 23:59:59.999999
  976. query T
  977. SELECT CAST(interval '-02:03' AS time) AS "21:57:00";
  978. ----
  979. 21:57:00
  980. query T
  981. SELECT CAST(interval '-9223372022400000000 us' AS time) AS "00:00:00";
  982. ----
  983. 00:00:00
  984. # Sub microseconds get rounded to microseconds
  985. query T
  986. SELECT INTERVAL '01:00:01.00000009';
  987. ----
  988. 01:00:01
  989. query T
  990. SELECT INTERVAL '01:00:01.0000009';
  991. ----
  992. 01:00:01.000001
  993. query T
  994. SELECT INTERVAL '01:00:01.0000004';
  995. ----
  996. 01:00:01
  997. query T
  998. SELECT INTERVAL '1.4 microseconds';
  999. ----
  1000. 00:00:00.000001
  1001. query T
  1002. SELECT INTERVAL '1.5 microseconds';
  1003. ----
  1004. 00:00:00.000002
  1005. query T
  1006. SELECT INTERVAL '1.4 microseconds';
  1007. ----
  1008. 00:00:00.000001
  1009. query T
  1010. SELECT INTERVAL '1.5 microseconds';
  1011. ----
  1012. 00:00:00.000002
  1013. query T
  1014. SELECT INTERVAL '0.4 microseconds';
  1015. ----
  1016. 00:00:00
  1017. # Postgres actually returns 00:00:00 here, which is most likely a bug/rounding error on their part
  1018. query T
  1019. SELECT INTERVAL '0.5 microseconds';
  1020. ----
  1021. 00:00:00.000001
  1022. query T
  1023. SELECT INTERVAL '0.6 microseconds';
  1024. ----
  1025. 00:00:00.000001
  1026. # PostgreSQL actually has a bug where when using EXTRACT it truncates this value to 365, but
  1027. # when using date_part it does not truncate this value. Therefore our EXTRACT function may differ
  1028. # from PostgreSQL.
  1029. # EXTRACT: https://github.com/postgres/postgres/blob/c2e8bd27519f47ff56987b30eb34a01969b9a9e8/src/backend/utils/adt/timestamp.c#L5270-L5273
  1030. # date_part: https://github.com/postgres/postgres/blob/c2e8bd27519f47ff56987b30eb34a01969b9a9e8/src/backend/utils/adt/timestamp.c#L5301
  1031. query T
  1032. SELECT EXTRACT(epoch FROM INTERVAL '2147483647 months 2147483647 days 2147483647 hours 59 minutes 59.999999 seconds');
  1033. ----
  1034. 5840726022849599.999999
  1035. query T
  1036. SELECT EXTRACT(epoch FROM INTERVAL '-2147483648 months -2147483648 days -2147483648 hours -59 minutes -59.999999 seconds');
  1037. ----
  1038. -5840726025531599.999999
  1039. # Interval comparison is done on a field by field basis, without combining the fields. This is due to a limitation in
  1040. # Materialize (See: https://github.com/MaterializeInc/database-issues/issues/3130)
  1041. query T
  1042. SELECT INTERVAL '1 month' > INTERVAL '9999999 days';
  1043. ----
  1044. true
  1045. query T
  1046. SELECT INTERVAL '-1 month' < INTERVAL '-9999999 days';
  1047. ----
  1048. true
  1049. query T
  1050. SELECT INTERVAL '1 month' > INTERVAL '999999999 hours';
  1051. ----
  1052. true
  1053. query T
  1054. SELECT INTERVAL '-1 month' < INTERVAL '-999999999 hours';
  1055. ----
  1056. true
  1057. query T
  1058. SELECT INTERVAL '1 day' > INTERVAL '9999999 hours';
  1059. ----
  1060. true
  1061. query T
  1062. SELECT INTERVAL '-1 day' < INTERVAL '-9999999 hours';
  1063. ----
  1064. true
  1065. # Justify tests
  1066. query T
  1067. SELECT justify_hours(interval '6 months 3 days 52 hours 3 minutes 2 seconds');
  1068. ----
  1069. 6 months 5 days 04:03:02
  1070. query T
  1071. SELECT justify_hours(interval '27 hours');
  1072. ----
  1073. 1 day 03:00:00
  1074. query T
  1075. SELECT justify_days(interval '6 months 36 days 5 hours 4 minutes 3 seconds');
  1076. ----
  1077. 7 months 6 days 05:04:03
  1078. query T
  1079. SELECT justify_days(interval '35 days');
  1080. ----
  1081. 1 month 5 days
  1082. query T
  1083. SELECT justify_interval(interval '1 month -1 hour');
  1084. ----
  1085. 29 days 23:00:00
  1086. query error "2147483647 days 24:00:00" interval out of range
  1087. SELECT justify_hours(interval '2147483647 days 24 hrs');
  1088. query error "178956970 years 7 months 30 days" interval out of range
  1089. SELECT justify_days(interval '2147483647 months 30 days');
  1090. query T
  1091. SELECT justify_interval(interval '2147483647 days 24 hrs');
  1092. ----
  1093. 5965232 years 4 months 8 days
  1094. query T
  1095. SELECT justify_interval(interval '-2147483648 days -24 hrs');
  1096. ----
  1097. -5965232 years -4 months -9 days
  1098. query error "178956970 years 7 months 30 days" interval out of range
  1099. SELECT justify_interval(interval '2147483647 months 30 days');
  1100. query error "-178956970 years -8 months -30 days" interval out of range
  1101. SELECT justify_interval(interval '-2147483648 months -30 days');
  1102. query T
  1103. SELECT justify_interval(interval '2147483647 months 30 days -24 hrs');
  1104. ----
  1105. 178956970 years 7 months 29 days
  1106. query T
  1107. SELECT justify_interval(interval '-2147483648 months -30 days 24 hrs');
  1108. ----
  1109. -178956970 years -8 months -29 days
  1110. query error "178956970 years 7 months -30 days \+1440:00:00" interval out of range
  1111. SELECT justify_interval(interval '2147483647 months -30 days 1440 hrs');
  1112. query error "-178956970 years -8 months \+30 days -1440:00:00" interval out of range
  1113. SELECT justify_interval(interval '-2147483648 months 30 days -1440 hrs');
  1114. # TIMESTAMP/DATE builtins.
  1115. query T
  1116. SELECT age('2001-04-10 22:06:45', '1957-06-13')
  1117. ----
  1118. 43 years 9 months 27 days 22:06:45
  1119. query T
  1120. SELECT age('2017-12-10'::timestamptz, '2017-12-01'::timestamptz)
  1121. ----
  1122. 9 days
  1123. query T
  1124. SELECT age('2017-12-10 04:05:01.555'::timestamp, '2017-12-10 04:05:01.550'::timestamp);
  1125. ----
  1126. 00:00:00.005
  1127. query T
  1128. SELECT age('0001-06-01 08:10:56.555'::timestamp, '9999-12-10 04:05:01.550'::timestamp);
  1129. ----
  1130. -9998 years -6 months -8 days -19:54:04.995
  1131. query B
  1132. SELECT age('2020-04-05'::date + '03:00'::time, '2020-04-05'::date + '02:00'::time) = '1 hour'::interval;
  1133. ----
  1134. true
  1135. query T
  1136. SELECT age(to_timestamp(-210833720368), to_timestamp(8200000000000));
  1137. ----
  1138. -266528 years -8 months -17 days -12:06:08
  1139. query T
  1140. SELECT age(to_timestamp(8200000000000), to_timestamp(-210833720368));
  1141. ----
  1142. 266528 years 8 months 17 days 12:06:08
  1143. # Aggregation
  1144. query T
  1145. SELECT MAX(unnest) FROM (SELECT UNNEST(ARRAY[INTERVAL '1-2 3 4:5:6.7' MONTH, INTERVAL '1-2 3 4:5:6.7' YEAR]));
  1146. ----
  1147. 1 year 2 months
  1148. query T
  1149. SELECT MAX(unnest) FROM (SELECT UNNEST(ARRAY[INTERVAL '2' SECOND, INTERVAL '1' SECOND]));
  1150. ----
  1151. 00:00:02
  1152. query T
  1153. SELECT MIN(unnest) FROM (SELECT UNNEST(ARRAY[INTERVAL '2' SECOND, INTERVAL '1' SECOND]));
  1154. ----
  1155. 00:00:01