timestamptz.slt 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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, timestamptz '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 (timestamptz '2020-02-29 15:44:17.71393+00')) 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, timestamptz '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 (timestamptz '2020-02-29 15:44:17.71393+00')) 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 (timestamptz '2020-02-11 15:44:17.71393+00')) ts (ts),
  71. (VALUES (timestamptz '2001-01-01')) origin (origin);
  72. ----
  73. 15␠days 2020-02-06␠00:00:00+00
  74. 2␠hours 2020-02-11␠14:00:00+00
  75. 10␠seconds 2020-02-11␠15:44:10+00
  76. 15␠minutes 2020-02-11␠15:30:00+00
  77. 1␠hour␠30␠minutes 2020-02-11␠15:00:00+00
  78. # shift bins using the origin parameter:
  79. query T
  80. SELECT date_bin('5 min'::interval, timestamptz '2020-02-01 01:01:01+00', timestamptz '2020-02-01 00:02:30+00');
  81. ----
  82. 2020-02-01 00:57:30+00
  83. # disallow > day intervals
  84. query error timestamps cannot be binned into intervals containing months or years
  85. SELECT date_bin('5 months'::interval, timestamptz '2020-02-01 01:01:01+00', timestamptz '2001-01-01');
  86. query error timestamps cannot be binned into intervals containing months or years
  87. SELECT date_bin('5 years'::interval, timestamptz '2020-02-01 01:01:01+00', timestamptz '2001-01-01');
  88. # disallow zero intervals
  89. query error stride must be greater than zero
  90. SELECT date_bin('0 days'::interval, timestamptz '1970-01-01 01:00:00+00' , timestamptz '1970-01-01 00:00:00+00');
  91. # disallow negative intervals
  92. query error stride must be greater than zero
  93. SELECT date_bin('-2 days'::interval, timestamptz '1970-01-01 01:00:00+00' , timestamptz '1970-01-01 00:00:00+00');
  94. # max stride caught
  95. query error stride cannot exceed
  96. SELECT date_bin('9223372037 s'::interval, timestamptz '2020-02-01 01:01:01+00', timestamptz '2020-02-01 00:02:30+00');
  97. # max diff caught
  98. query error source and origin must not differ more than 2\^63 nanoseconds
  99. SELECT date_bin('1m', timestamptz '2262-04-11 23:47:17+00');
  100. # Test unintuitive day binning
  101. query TT rowsort
  102. SELECT
  103. "interval",
  104. date_bin("interval"::interval, timestamptz '2020-02-27 15:44:17.71393+00', timestamptz '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+00
  113. 2d 2020-02-27␠00:00:00+00
  114. 3d 2020-02-27␠00:00:00+00
  115. 4d 2020-02-27␠00:00:00+00
  116. 5d 2020-02-26␠00:00:00+00
  117. 6d 2020-02-27␠00:00:00+00
  118. 7d 2020-02-24␠00:00:00+00
  119. 8d 2020-02-23␠00:00:00+00
  120. 9d 2020-02-24␠00: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, timestamptz '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 (timestamptz '2020-02-29 15:44:17.71393+00')) 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 error timestamp out of range
  142. select timezone('1 day'::interval, '1-12-31'::timestamptz+'262141 years'::interval)
  143. ### to_char
  144. statement ok
  145. CREATE TABLE t (ts timestamp with time zone)
  146. statement ok
  147. INSERT INTO t VALUES
  148. ('1997-01-01 00:00:00 GMT'),
  149. ('2021-02-09 01:07:12 UTC'),
  150. ('2024-03-21 12:09:23 +0000'),
  151. ('2060-12-31 23:59:59.999999 -0800')
  152. query T rowsort
  153. SELECT to_char(ts, 'DAY Day day DY Dy dy MONTH Month month RM MON Mon mon') FROM t
  154. ----
  155. SATURDAY Saturday saturday SAT Sat sat JANUARY January january I JAN Jan jan
  156. THURSDAY Thursday thursday THU Thu thu MARCH March march III MAR Mar mar
  157. TUESDAY Tuesday tuesday TUE Tue tue FEBRUARY February february II FEB Feb feb
  158. WEDNESDAY Wednesday wednesday WED Wed wed JANUARY January january I JAN Jan jan
  159. query T rowsort
  160. SELECT to_char(ts, 'FMDAY FMDay FMday FMMONTH FMMonth FMmonth FMRM') FROM t
  161. ----
  162. SATURDAY Saturday saturday JANUARY January january I
  163. THURSDAY Thursday thursday MARCH March march III
  164. TUESDAY Tuesday tuesday FEBRUARY February february II
  165. WEDNESDAY Wednesday wednesday JANUARY January january I
  166. query T rowsort
  167. SELECT to_char(ts, 'Y,YYY YYYY YYY YY Y CC Q MM WW DDD DD D J') FROM t
  168. ----
  169. 1,997 1997 997 97 7 20 1 01 01 001 01 4 2450450
  170. 2,021 2021 021 21 1 21 1 02 06 040 09 3 2459255
  171. 2,024 2024 024 24 4 21 1 03 12 081 21 5 2460391
  172. 2,061 2061 061 61 1 21 1 01 01 001 01 7 2473826
  173. query T rowsort
  174. SELECT to_char(ts, 'FMY,YYY FMYYYY FMYYY FMYY FMY FMCC FMQ FMMM FMWW FMDDD FMDD FMD FMJ') FROM t
  175. ----
  176. 1,997 1997 997 97 7 20 1 1 1 1 1 4 2450450
  177. 2,021 2021 21 21 1 21 1 2 6 40 9 3 2459255
  178. 2,024 2024 24 24 4 21 1 3 12 81 21 5 2460391
  179. 2,061 2061 61 61 1 21 1 1 1 1 1 7 2473826
  180. query T rowsort
  181. SELECT to_char(ts, 'HH HH12 HH24 MI SS SSSS') FROM t
  182. ----
  183. 01 01 01 07 12 4032
  184. 07 07 07 59 59 28799
  185. 12 12 00 00 00 0
  186. 12 12 12 09 23 43763
  187. query T rowsort
  188. SELECT to_char(ts, '"HH:MI:SS is" HH:MI:SS "\"text between quote marks\""') FROM t
  189. ----
  190. HH:MI:SS is 01:07:12 "text between quote marks"
  191. HH:MI:SS is 07:59:59 "text between quote marks"
  192. HH:MI:SS is 12:00:00 "text between quote marks"
  193. HH:MI:SS is 12:09:23 "text between quote marks"
  194. query T rowsort
  195. SELECT to_char(ts, 'HH24--text--MI--text--SS') FROM t
  196. ----
  197. 00--text--00--text--00
  198. 01--text--07--text--12
  199. 07--text--59--text--59
  200. 12--text--09--text--23
  201. query T rowsort
  202. SELECT to_char(ts, 'YYYYTH YYYYth Jth') FROM t
  203. ----
  204. 1997TH 1997th 2450450th
  205. 2021ST 2021st 2459255th
  206. 2024TH 2024th 2460391st
  207. 2061ST 2061st 2473826th
  208. query T rowsort
  209. 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
  210. ----
  211. 1997 A.D. 1997 a.d. 1997 ad 12:00:00 A.M. 12:00:00 a.m. 12:00:00 am
  212. 2021 A.D. 2021 a.d. 2021 ad 01:07:12 A.M. 01:07:12 a.m. 01:07:12 am
  213. 2024 A.D. 2024 a.d. 2024 ad 12:09:23 P.M. 12:09:23 p.m. 12:09:23 pm
  214. 2061 A.D. 2061 a.d. 2061 ad 07:59:59 A.M. 07:59:59 a.m. 07:59:59 am
  215. query T rowsort
  216. SELECT to_char(ts, 'IYYY IYY IY I IW IDDD ID') FROM t
  217. ----
  218. 1997 997 97 7 01 003 3
  219. 2021 021 21 1 06 037 2
  220. 2024 024 24 4 12 081 4
  221. 2060 060 60 0 53 370 6
  222. query T rowsort
  223. SELECT to_char(ts, 'FMIYYY FMIYY FMIY FMI FMIW FMIDDD FMID') FROM t
  224. ----
  225. 1997 997 97 7 1 3 3
  226. 2021 21 21 1 6 37 2
  227. 2024 24 24 4 12 81 4
  228. 2060 60 60 0 53 370 6
  229. query T
  230. SELECT to_char(ts, 'FF1 FF2 FF3 FF4 FF5 FF6 ff1 ff2 ff3 ff4 ff5 ff6 MS US') FROM (
  231. VALUES
  232. ('2018-11-02 12:34:56'::timestamp),
  233. ('2018-11-02 12:34:56.78'),
  234. ('2018-11-02 12:34:56.78901'),
  235. ('2018-11-02 12:34:56.78901234')
  236. ) d(ts)
  237. ----
  238. FF1 FF2 FF3 FF4 FF5 FF6 ff1 ff2 ff3 ff4 ff5 ff6 000 000000
  239. FF1 FF2 FF3 FF4 FF5 FF6 ff1 ff2 ff3 ff4 ff5 ff6 780 780000
  240. FF1 FF2 FF3 FF4 FF5 FF6 ff1 ff2 ff3 ff4 ff5 ff6 789 789010
  241. FF1 FF2 FF3 FF4 FF5 FF6 ff1 ff2 ff3 ff4 ff5 ff6 789 789012
  242. # Verify that the format string gets precompiled.
  243. query T multiline
  244. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR SELECT to_char(ts, 'HH:MI:SS') FROM t
  245. ----
  246. Explained Query:
  247. Project (#1)
  248. Map (tochartstz[HH:MI:SS](#0{ts}))
  249. ReadStorage materialize.public.t
  250. Source materialize.public.t
  251. Target cluster: quickstart
  252. EOF
  253. # Verify handling of NULL arguments.
  254. statement ok
  255. INSERT INTO t VALUES (NULL)
  256. query T rowsort
  257. SELECT to_char(ts, 'HH:MI:SS') FROM t
  258. ----
  259. 01:07:12
  260. 07:59:59
  261. 12:00:00
  262. 12:09:23
  263. NULL
  264. query T
  265. SELECT to_char(ts, NULL) FROM t
  266. ----
  267. NULL
  268. NULL
  269. NULL
  270. NULL
  271. NULL