temporal.td 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. #
  10. # Using mz_now() with a VIEW source
  11. #
  12. > CREATE VIEW one_ts (ts) AS VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(NULL)
  13. > CREATE MATERIALIZED VIEW one_bound1 AS SELECT * FROM one_ts WHERE mz_now() >= ts
  14. > SELECT * FROM one_bound1 AS OF 0
  15. > SELECT * FROM one_bound1 AS OF 2
  16. 1
  17. 2
  18. > CREATE MATERIALIZED VIEW one_bound2 AS SELECT * FROM one_ts WHERE mz_now() <= ts
  19. > SELECT * FROM one_bound2 AS OF 0
  20. 1
  21. 2
  22. 3
  23. 4
  24. 5
  25. 6
  26. 7
  27. 8
  28. 9
  29. 10
  30. > SELECT * FROM one_bound2 AS OF 2
  31. 2
  32. 3
  33. 4
  34. 5
  35. 6
  36. 7
  37. 8
  38. 9
  39. 10
  40. > SELECT * FROM one_bound2 AS OF 10
  41. 10
  42. > SELECT * FROM one_bound2 AS OF 11
  43. > CREATE MATERIALIZED VIEW two_bounds AS SELECT * FROM one_ts WHERE mz_now() >= ts AND mz_now() >= ts + 3
  44. > SELECT * FROM two_bounds AS OF 2
  45. > SELECT * FROM two_bounds AS OF 5
  46. ts
  47. ---
  48. 1
  49. 2
  50. > CREATE MATERIALIZED VIEW two_bounds2 AS SELECT * FROM one_ts WHERE mz_now() <= ts AND mz_now() <= ts + 3
  51. > SELECT * FROM two_bounds2 AS OF 2
  52. ts
  53. ---
  54. 2
  55. 3
  56. 4
  57. 5
  58. 6
  59. 7
  60. 8
  61. 9
  62. 10
  63. > SELECT * FROM two_bounds2 AS OF 9
  64. ts
  65. ---
  66. 9
  67. 10
  68. > CREATE MATERIALIZED VIEW two_bounds_between AS SELECT * FROM one_ts WHERE mz_now() BETWEEN ts AND ts + 3
  69. > SELECT * FROM two_bounds_between AS OF 3
  70. ts
  71. ---
  72. 1
  73. 2
  74. 3
  75. > SELECT * FROM two_bounds_between AS OF 5
  76. ts
  77. ---
  78. 2
  79. 3
  80. 4
  81. 5
  82. > CREATE MATERIALIZED VIEW two_bounds_and AS SELECT * FROM one_ts WHERE mz_now() >= ts AND mz_now() <= ts + 3
  83. > SELECT * FROM two_bounds_and AS OF 3
  84. ts
  85. ---
  86. 1
  87. 2
  88. 3
  89. > SELECT * FROM two_bounds_and AS OF 5
  90. ts
  91. ---
  92. 2
  93. 3
  94. 4
  95. 5
  96. > CREATE VIEW two_ts (ts1,ts2) AS VALUES (1,1+5),(2,2+5),(3,3+5),(4,4+5),(5,5+5),(6,6+5),(7,7+5),(8,8+5),(9,9+5),(10,10+5)
  97. > DROP VIEW IF EXISTS two_bounds_two_cols CASCADE
  98. > CREATE MATERIALIZED VIEW two_bounds_two_cols AS SELECT * FROM two_ts WHERE mz_now() >= ts1 AND mz_now() >= ts2
  99. > SELECT * FROM two_bounds_two_cols AS OF 3
  100. > SELECT * FROM two_bounds_two_cols AS OF 6
  101. 1 6
  102. > SELECT * FROM two_bounds_two_cols AS OF 15
  103. 1 6
  104. 2 7
  105. 3 8
  106. 4 9
  107. 5 10
  108. 6 11
  109. 7 12
  110. 8 13
  111. 9 14
  112. 10 15
  113. > CREATE MATERIALIZED VIEW two_bounds_two_cols2 AS SELECT * FROM two_ts WHERE mz_now() >= ts1 AND mz_now() <= ts2;
  114. > SELECT * FROM two_bounds_two_cols2 AS OF 3
  115. 1 6
  116. 2 7
  117. 3 8
  118. > SELECT * FROM two_bounds_two_cols2 AS OF 6
  119. 1 6
  120. 2 7
  121. 3 8
  122. 4 9
  123. 5 10
  124. 6 11
  125. > SELECT * FROM two_bounds_two_cols2 AS OF 15
  126. 10 15
  127. > SELECT * FROM two_bounds_two_cols2 AS OF 16
  128. > DROP VIEW one_ts CASCADE;
  129. > DROP VIEW two_ts CASCADE;
  130. #
  131. # Using a TABLE source
  132. #
  133. > CREATE TABLE one_ts (ts mz_timestamp)
  134. > INSERT INTO one_ts VALUES ('2000-01-01'::timestamp)
  135. > INSERT INTO one_ts VALUES ('2199-12-31'::timestamp)
  136. > INSERT INTO one_ts VALUES (NULL)
  137. > CREATE MATERIALIZED VIEW one_bound1 AS SELECT to_timestamp(ts::string::numeric / 1000) FROM one_ts WHERE ts >= mz_now()
  138. > SELECT * FROM one_bound1
  139. to_timestamp
  140. "2199-12-31 00:00:00 UTC"
  141. > INSERT INTO one_ts VALUES ('2000-01-01'::timestamp)
  142. > INSERT INTO one_ts VALUES ('2199-12-31'::timestamp)
  143. > INSERT INTO one_ts VALUES (NULL)
  144. > SELECT * FROM one_bound1
  145. to_timestamp
  146. "2199-12-31 00:00:00 UTC"
  147. "2199-12-31 00:00:00 UTC"
  148. > CREATE MATERIALIZED VIEW one_bound2 AS SELECT to_timestamp(ts::string::numeric / 1000) FROM one_ts WHERE ts <= mz_now()
  149. > SELECT * FROM one_bound2
  150. to_timestamp
  151. "2000-01-01 00:00:00 UTC"
  152. "2000-01-01 00:00:00 UTC"
  153. > INSERT INTO one_ts VALUES ('2000-01-01'::timestamp)
  154. > INSERT INTO one_ts VALUES ('2199-12-31'::timestamp)
  155. > INSERT INTO one_ts VALUES (NULL)
  156. > SELECT * FROM one_bound2
  157. to_timestamp
  158. "2000-01-01 00:00:00 UTC"
  159. "2000-01-01 00:00:00 UTC"
  160. "2000-01-01 00:00:00 UTC"
  161. > CREATE TABLE first_ts (ts mz_timestamp)
  162. > INSERT INTO first_ts VALUES ('2000-01-01'::timestamp)
  163. > INSERT INTO first_ts VALUES ('2199-12-31'::timestamp)
  164. > INSERT INTO first_ts VALUES (NULL)
  165. > CREATE TABLE second_ts (ts mz_timestamp)
  166. > INSERT INTO second_ts VALUES ('2000-01-01'::timestamp)
  167. > INSERT INTO second_ts VALUES ('2199-12-31'::timestamp)
  168. > INSERT INTO second_ts VALUES (NULL)
  169. > CREATE MATERIALIZED VIEW both_ts (first_ts_to_ts, second_ts_to_ts) AS SELECT to_timestamp(first_ts.ts::string::numeric / 1000), to_timestamp(second_ts.ts::string::numeric / 1000) FROM first_ts, second_ts WHERE mz_now() BETWEEN first_ts.ts AND second_ts.ts
  170. > SELECT * FROM both_ts
  171. to_timestamp
  172. "2000-01-01 00:00:00 UTC" "2199-12-31 00:00:00 UTC"
  173. > INSERT INTO first_ts VALUES ('2000-01-01'::timestamp)
  174. > INSERT INTO first_ts VALUES (NULL)
  175. > INSERT INTO second_ts VALUES ('2199-12-31'::timestamp)
  176. > INSERT INTO second_ts VALUES (NULL)
  177. > SELECT * FROM both_ts
  178. "2000-01-01 00:00:00 UTC" "2199-12-31 00:00:00 UTC"
  179. "2000-01-01 00:00:00 UTC" "2199-12-31 00:00:00 UTC"
  180. "2000-01-01 00:00:00 UTC" "2199-12-31 00:00:00 UTC"
  181. "2000-01-01 00:00:00 UTC" "2199-12-31 00:00:00 UTC"
  182. > CREATE MATERIALIZED VIEW v2 (first_ts_a, first_ts_b) AS SELECT * FROM first_ts a, first_ts b WHERE mz_now() = a.ts;
  183. > CREATE MATERIALIZED VIEW v3 (first_ts_a, first_ts_b) AS SELECT * FROM first_ts a, first_ts b WHERE mz_now() = a.ts AND mz_now() = b.ts;
  184. #
  185. # Various errors in the placement of mz_now()
  186. #
  187. !CREATE MATERIALIZED VIEW v1 AS SELECT * FROM first_ts WHERE mz_now() != ts;
  188. contains:Unsupported binary temporal operation: NotEq
  189. !CREATE MATERIALIZED VIEW v1 AS SELECT * FROM first_ts WHERE mz_now() + 1 = ts;
  190. contains:operator does not exist: mz_timestamp + integer
  191. !CREATE MATERIALIZED VIEW v1 AS SELECT * FROM first_ts WHERE mz_now() > ts OR ts = 1;
  192. contains:Unsupported temporal predicate. Note: `mz_now()` must be directly compared to a non-temporal expression of mz_timestamp-castable type. Expression found: ((#0{ts} = 1) OR (mz_now() > #0{ts}))
  193. !CREATE MATERIALIZED VIEW v1 AS SELECT * FROM first_ts WHERE ts BETWEEN mz_now() AND mz_now() + 1;
  194. contains:operator does not exist: mz_timestamp + integer
  195. #
  196. # Numeric comparisons
  197. #
  198. # Checks that comparisons against mz_now using values with
  199. # fractional components fail due to lossy conversion of numeric to u64.
  200. > CREATE OR REPLACE MATERIALIZED VIEW numeric_trunc AS
  201. SELECT 1
  202. WHERE mz_now() > 1927418240000.1;
  203. ! SELECT * FROM numeric_trunc;
  204. contains:Evaluation error: "1927418240000.1" mz_timestamp out of range