timestamp.slt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. # bin_date
  11. simple conn=mz_system,user=mz_system
  12. ALTER SYSTEM SET enable_binary_date_bin = true
  13. ----
  14. COMPLETE 0
  15. # case 1: AD dates, origin < input
  16. query TTT rowsort
  17. SELECT
  18. str,
  19. "interval",
  20. date_trunc(str, ts) = date_bin("interval"::interval, ts, timestamp '2001-01-01') AS equal
  21. FROM (
  22. VALUES
  23. ('week', '7d'),
  24. ('day', '1d'),
  25. ('hour', '1h'),
  26. ('minute', '1m'),
  27. ('second', '1s')
  28. ) intervals (str, interval),
  29. (VALUES (timestamp '2020-02-29 15:44:17.71393')) ts (ts);
  30. ----
  31. day 1d true
  32. hour 1h true
  33. week 7d true
  34. minute 1m true
  35. second 1s true
  36. # case 3: AD dates, origin > input
  37. query TTT rowsort
  38. SELECT
  39. str,
  40. "interval",
  41. date_trunc(str, ts) = date_bin("interval"::interval, ts, timestamp '2020-03-02') AS equal
  42. FROM (
  43. VALUES
  44. ('week', '7d'),
  45. ('day', '1d'),
  46. ('hour', '1h'),
  47. ('minute', '1m'),
  48. ('second', '1s')
  49. ) intervals (str, interval),
  50. (VALUES (timestamp '2020-02-29 15:44:17.71393')) ts (ts);
  51. ----
  52. day 1d true
  53. hour 1h true
  54. week 7d true
  55. minute 1m true
  56. second 1s true
  57. # bin timestamps into arbitrary intervals
  58. query TT rowsort
  59. SELECT
  60. "interval",
  61. date_bin("interval"::interval, ts, origin)
  62. FROM (
  63. VALUES
  64. ('15 days'),
  65. ('2 hours'),
  66. ('1 hour 30 minutes'),
  67. ('15 minutes'),
  68. ('10 seconds')
  69. ) intervals (interval),
  70. (VALUES (timestamp '2020-02-11 15:44:17.71393')) ts (ts),
  71. (VALUES (timestamp '2001-01-01')) origin (origin);
  72. ----
  73. 15␠days 2020-02-06␠00:00:00
  74. 2␠hours 2020-02-11␠14:00:00
  75. 10␠seconds 2020-02-11␠15:44:10
  76. 15␠minutes 2020-02-11␠15:30:00
  77. 1␠hour␠30␠minutes 2020-02-11␠15:00:00
  78. # shift bins using the origin parameter:
  79. query T
  80. SELECT date_bin('5 min'::interval, timestamp '2020-02-01 01:01:01', timestamp '2020-02-01 00:02:30');
  81. ----
  82. 2020-02-01 00:57:30
  83. # disallow > day intervals
  84. query error timestamps cannot be binned into intervals containing months or years
  85. SELECT date_bin('5 months'::interval, timestamp '2020-02-01 01:01:01', timestamp '2001-01-01');
  86. query error timestamps cannot be binned into intervals containing months or years
  87. SELECT date_bin('5 years'::interval, timestamp '2020-02-01 01:01:01', timestamp '2001-01-01');
  88. # disallow zero intervals
  89. query error stride must be greater than zero
  90. SELECT date_bin('0 days'::interval, timestamp '1970-01-01 01:00:00' , timestamp '1970-01-01 00:00:00');
  91. # disallow negative intervals
  92. query error stride must be greater than zero
  93. SELECT date_bin('-2 days'::interval, timestamp '1970-01-01 01:00:00' , timestamp '1970-01-01 00:00:00');
  94. # max stride caught
  95. query error stride cannot exceed
  96. SELECT date_bin('9223372037 s'::interval, timestamp '2020-02-01 01:01:01', timestamp '2020-02-01 00:02:30');
  97. # max diff caught
  98. query error source and origin must not differ more than 2\^63 nanoseconds
  99. SELECT date_bin('1m', timestamp '2262-04-11 23:47:17');
  100. # Test unintuitive day binning
  101. query TT rowsort
  102. SELECT
  103. "interval",
  104. date_bin("interval"::interval, timestamp '2020-02-27 15:44:17.71393', timestamp '2001-01-01')
  105. FROM (
  106. VALUES
  107. ('1d'), ('2d'), ('3d'),
  108. ('4d'), ('5d'), ('6d'),
  109. ('7d'), ('8d'), ('9d')
  110. ) intervals ("interval");
  111. ----
  112. 1d 2020-02-27␠00:00:00
  113. 2d 2020-02-27␠00:00:00
  114. 3d 2020-02-27␠00:00:00
  115. 4d 2020-02-27␠00:00:00
  116. 5d 2020-02-26␠00:00:00
  117. 6d 2020-02-27␠00:00:00
  118. 7d 2020-02-24␠00:00:00
  119. 8d 2020-02-23␠00:00:00
  120. 9d 2020-02-24␠00:00:00
  121. # Test that binary extension behaves as expected
  122. query TT rowsort
  123. SELECT
  124. "interval",
  125. date_bin("interval"::interval, ts) = date_bin("interval"::interval, ts, timestamp '1970-01-01') AS equal
  126. FROM (
  127. VALUES
  128. ('15 days'),
  129. ('2 hours'),
  130. ('1 hour 30 minutes'),
  131. ('15 minutes'),
  132. ('10 seconds')
  133. ) intervals (interval),
  134. (VALUES (timestamp '2020-02-29 15:44:17.71393')) ts (ts);
  135. ----
  136. 15␠days true
  137. 2␠hours true
  138. 10␠seconds true
  139. 15␠minutes true
  140. 1␠hour␠30␠minutes true
  141. query T
  142. SELECT '99999-01-01'::TIMESTAMP + '162143 y';
  143. ----
  144. 262142-01-01 00:00:00
  145. query error timestamp out of range
  146. SELECT '99999-01-01'::TIMESTAMP + '162144 y';
  147. # date_bin_hopping
  148. simple conn=mz_system,user=mz_system
  149. ALTER SYSTEM SET enable_date_bin_hopping = true
  150. ----
  151. COMPLETE 0
  152. query T rowsort
  153. SELECT * FROM date_bin_hopping('45s', '1m', '2001-01-01 00:01:20') ORDER BY 1;
  154. ----
  155. 2001-01-01 00:00:30+00
  156. 2001-01-01 00:01:15+00
  157. statement ok
  158. CREATE TABLE t (ts timestamp, v int);
  159. statement ok
  160. INSERT INTO t VALUES
  161. ('2021-01-01 01:05', 41),
  162. ('2021-01-01 01:07', 21),
  163. ('2021-01-01 01:09', 51),
  164. ('2021-01-01 01:11', 31),
  165. ('2021-01-01 01:13', 11),
  166. ('2021-01-01 01:17', 61);
  167. # ts unix epoch origin
  168. query TI rowsort
  169. SELECT date_bin_hopping AS w, sum(v)
  170. FROM t,
  171. date_bin_hopping('5m', '10m', t.ts)
  172. GROUP BY w
  173. ORDER BY 1;
  174. ----
  175. 2021-01-01␠01:00:00 113
  176. 2021-01-01␠01:05:00 155
  177. 2021-01-01␠01:10:00 103
  178. 2021-01-01␠01:15:00 61
  179. # tstz unix epoch origin
  180. query TI rowsort
  181. SELECT date_bin_hopping AS w, sum(v)
  182. FROM t,
  183. date_bin_hopping('5m', '10m', t.ts::timestamptz)
  184. GROUP BY w
  185. ORDER BY 1;
  186. ----
  187. 2021-01-01␠01:00:00+00 113
  188. 2021-01-01␠01:05:00+00 155
  189. 2021-01-01␠01:10:00+00 103
  190. 2021-01-01␠01:15:00+00 61
  191. # ts
  192. query TI rowsort
  193. SELECT date_bin_hopping AS w, sum(v)
  194. FROM t,
  195. date_bin_hopping('5m', '10m', t.ts, '1970-01-01')
  196. GROUP BY w
  197. ORDER BY 1;
  198. ----
  199. 2021-01-01␠01:00:00 113
  200. 2021-01-01␠01:05:00 155
  201. 2021-01-01␠01:10:00 103
  202. 2021-01-01␠01:15:00 61
  203. #tstz
  204. query TI rowsort
  205. SELECT date_bin_hopping AS w, sum(v)
  206. FROM t,
  207. date_bin_hopping('5m', '10m', t.ts::timestamptz, '1970-01-01')
  208. GROUP BY w
  209. ORDER BY 1;
  210. ----
  211. 2021-01-01␠01:00:00+00 113
  212. 2021-01-01␠01:05:00+00 155
  213. 2021-01-01␠01:10:00+00 103
  214. 2021-01-01␠01:15:00+00 61
  215. statement ok
  216. DROP TABLE t;
  217. ### to_char
  218. statement ok
  219. CREATE TABLE t (ts timestamp)
  220. statement ok
  221. INSERT INTO t VALUES
  222. ('1997-01-01 00:00:00'),
  223. ('2021-02-09 01:07:12'),
  224. ('2024-03-21 12:09:23'),
  225. ('2060-12-31 23:59:59.999999')
  226. query T rowsort
  227. SELECT to_char(ts, 'DAY Day day DY Dy dy MONTH Month month RM MON Mon mon') FROM t
  228. ----
  229. FRIDAY Friday friday FRI Fri fri DECEMBER December december XII DEC Dec dec
  230. THURSDAY Thursday thursday THU Thu thu MARCH March march III MAR Mar mar
  231. TUESDAY Tuesday tuesday TUE Tue tue FEBRUARY February february II FEB Feb feb
  232. WEDNESDAY Wednesday wednesday WED Wed wed JANUARY January january I JAN Jan jan
  233. query T rowsort
  234. SELECT to_char(ts, 'FMDAY FMDay FMday FMMONTH FMMonth FMmonth FMRM') FROM t
  235. ----
  236. FRIDAY Friday friday DECEMBER December december XII
  237. THURSDAY Thursday thursday MARCH March march III
  238. TUESDAY Tuesday tuesday FEBRUARY February february II
  239. WEDNESDAY Wednesday wednesday JANUARY January january I
  240. query T rowsort
  241. SELECT to_char(ts, 'Y,YYY YYYY YYY YY Y CC Q MM WW DDD DD D J') FROM t
  242. ----
  243. 1,997 1997 997 97 7 20 1 01 01 001 01 4 2450450
  244. 2,021 2021 021 21 1 21 1 02 06 040 09 3 2459255
  245. 2,024 2024 024 24 4 21 1 03 12 081 21 5 2460391
  246. 2,060 2060 060 60 0 21 4 12 53 366 31 6 2473825
  247. query T rowsort
  248. SELECT to_char(ts, 'FMY,YYY FMYYYY FMYYY FMYY FMY FMCC FMQ FMMM FMWW FMDDD FMDD FMD FMJ') FROM t
  249. ----
  250. 1,997 1997 997 97 7 20 1 1 1 1 1 4 2450450
  251. 2,021 2021 21 21 1 21 1 2 6 40 9 3 2459255
  252. 2,024 2024 24 24 4 21 1 3 12 81 21 5 2460391
  253. 2,060 2060 60 60 0 21 4 12 53 366 31 6 2473825
  254. query T rowsort
  255. SELECT to_char(ts, 'HH HH12 HH24 MI SS SSSS') FROM t
  256. ----
  257. 01 01 01 07 12 4032
  258. 11 11 23 59 59 86399
  259. 12 12 00 00 00 0
  260. 12 12 12 09 23 43763
  261. query T rowsort
  262. SELECT to_char(ts, '"HH:MI:SS is" HH:MI:SS "\"text between quote marks\""') FROM t
  263. ----
  264. HH:MI:SS is 01:07:12 "text between quote marks"
  265. HH:MI:SS is 11:59:59 "text between quote marks"
  266. HH:MI:SS is 12:00:00 "text between quote marks"
  267. HH:MI:SS is 12:09:23 "text between quote marks"
  268. query T rowsort
  269. SELECT to_char(ts, 'HH24--text--MI--text--SS') FROM t
  270. ----
  271. 00--text--00--text--00
  272. 01--text--07--text--12
  273. 12--text--09--text--23
  274. 23--text--59--text--59
  275. query T rowsort
  276. SELECT to_char(ts, 'YYYYTH YYYYth Jth') FROM t
  277. ----
  278. 1997TH 1997th 2450450th
  279. 2021ST 2021st 2459255th
  280. 2024TH 2024th 2460391st
  281. 2060TH 2060th 2473825th
  282. query T rowsort
  283. SELECT to_char(ts, 'YYYY A.D. YYYY a.d. YYYY bc HH:MI:SS P.M. HH:MI:SS p.m. HH:MI:SS pm') FROM t
  284. ----
  285. 1997 A.D. 1997 a.d. 1997 ad 12:00:00 A.M. 12:00:00 a.m. 12:00:00 am
  286. 2021 A.D. 2021 a.d. 2021 ad 01:07:12 A.M. 01:07:12 a.m. 01:07:12 am
  287. 2024 A.D. 2024 a.d. 2024 ad 12:09:23 P.M. 12:09:23 p.m. 12:09:23 pm
  288. 2060 A.D. 2060 a.d. 2060 ad 11:59:59 P.M. 11:59:59 p.m. 11:59:59 pm
  289. query T rowsort
  290. SELECT to_char(ts, 'IYYY IYY IY I IW IDDD ID') FROM t
  291. ----
  292. 1997 997 97 7 01 003 3
  293. 2021 021 21 1 06 037 2
  294. 2024 024 24 4 12 081 4
  295. 2060 060 60 0 53 369 5
  296. query T rowsort
  297. SELECT to_char(ts, 'FMIYYY FMIYY FMIY FMI FMIW FMIDDD FMID') FROM t
  298. ----
  299. 1997 997 97 7 1 3 3
  300. 2021 21 21 1 6 37 2
  301. 2024 24 24 4 12 81 4
  302. 2060 60 60 0 53 369 5
  303. query T rowsort
  304. SELECT to_char(ts, 'FF1 FF2 FF3 FF4 FF5 FF6 ff1 ff2 ff3 ff4 ff5 ff6 MS US') FROM (
  305. VALUES
  306. ('2018-11-02 12:34:56'::timestamp),
  307. ('2018-11-02 12:34:56.78'),
  308. ('2018-11-02 12:34:56.78901'),
  309. ('2018-11-02 12:34:56.78901234')
  310. ) d(ts)
  311. ----
  312. FF1 FF2 FF3 FF4 FF5 FF6 ff1 ff2 ff3 ff4 ff5 ff6 000 000000
  313. FF1 FF2 FF3 FF4 FF5 FF6 ff1 ff2 ff3 ff4 ff5 ff6 780 780000
  314. FF1 FF2 FF3 FF4 FF5 FF6 ff1 ff2 ff3 ff4 ff5 ff6 789 789010
  315. FF1 FF2 FF3 FF4 FF5 FF6 ff1 ff2 ff3 ff4 ff5 ff6 789 789012
  316. # Verify that the format string gets precompiled.
  317. query T multiline
  318. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT to_char(ts, 'HH:MI:SS') FROM t
  319. ----
  320. Explained Query:
  321. Project (#1)
  322. Map (tocharts[HH:MI:SS](#0{ts}))
  323. ReadStorage materialize.public.t
  324. Source materialize.public.t
  325. Target cluster: quickstart
  326. EOF
  327. # Verify handling of NULL arguments.
  328. statement ok
  329. INSERT INTO t VALUES (NULL)
  330. query T rowsort
  331. SELECT to_char(ts, 'HH:MI:SS') FROM t
  332. ----
  333. 01:07:12
  334. 11:59:59
  335. 12:00:00
  336. 12:09:23
  337. NULL
  338. query T rowsort
  339. SELECT to_char(ts, NULL) FROM t
  340. ----
  341. NULL
  342. NULL
  343. NULL
  344. NULL
  345. NULL
  346. query T
  347. SELECT '0001-12-31 19:03:58 BC'::timestamp;
  348. ----
  349. 0001-12-31 19:03:58 BC
  350. query T
  351. SELECT '0001-12-31 19:03:58 EST BC'::timestamp;
  352. ----
  353. 0001-12-31 19:03:58 BC
  354. query T
  355. SELECT '0001-12-31 19:03:58 bc'::timestamp;
  356. ----
  357. 0001-12-31 19:03:58 BC
  358. query T
  359. SELECT '0001-12-31 19:03:58 BC '::timestamp;
  360. ----
  361. 0001-12-31 19:03:58 BC
  362. query T
  363. SELECT '0001-12-31 19:03:58BC '::timestamp;
  364. ----
  365. 0001-12-31 19:03:58 BC
  366. query T
  367. SELECT '0001-12-31 19:03:58 AD'::timestamp;
  368. ----
  369. 0001-12-31 19:03:58
  370. query T
  371. SELECT '0001-12-31 19:03:58 EST AD'::timestamp;
  372. ----
  373. 0001-12-31 19:03:58
  374. query T
  375. SELECT '0001-12-31 19:03:58 ad'::timestamp;
  376. ----
  377. 0001-12-31 19:03:58
  378. query T
  379. SELECT '0001-12-31 19:03:58 AD '::timestamp;
  380. ----
  381. 0001-12-31 19:03:58
  382. query T
  383. SELECT '0001-12-31 19:03:58AD '::timestamp;
  384. ----
  385. 0001-12-31 19:03:58
  386. # Regression test for #9236 which mis-parsed timezones ending with 'AD' or 'BC'.
  387. query T
  388. SELECT '2020-12-21 18:53:49 Asia/Baghdad'::timestamp;
  389. ----
  390. 2020-12-21 18:53:49
  391. # Regression test covering case where a characters lower is 1 byte and upper is 2 bytes.
  392. # Incorrect failing case is a panic due to splitting a string in the middle of a char.
  393. query error invalid input syntax for type timestamp: Invalid character at offset 19 in 2025-01-01 00:00:00ı: 'ı': "2025-01-01 00:00:00ı AD"
  394. SELECT '2025-01-01 00:00:00ı AD'::timestamp;