forked from MaterializeInc/materialize
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimestamptz.slt
190 lines (176 loc) · 4.17 KB
/
timestamptz.slt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Copyright Materialize, Inc. and contributors. All rights reserved.
#
# Use of this software is governed by the Business Source License
# included in the LICENSE file at the root of this repository.
#
# As of the Change Date specified in that file, in accordance with
# the Business Source License, use of this software will be governed
# by the Apache License, Version 2.0.
# bin_date
# Tests adapted from Postgresql
# https://github.com/postgres/postgres/blob/master/src/test/regress/sql/timestamptz.sql
# case 1: AD dates, origin < input
query TTT
SELECT
str,
"interval",
date_trunc(str, ts) = date_bin("interval"::interval, ts, timestamptz '2001-01-01') AS equal
FROM (
VALUES
('week', '7 d'),
('day', '1 d'),
('hour', '1 h'),
('minute', '1 m'),
('second', '1 s')
) intervals (str, interval),
(VALUES (timestamptz '2020-02-29 15:44:17.71393+00')) ts (ts);
----
day
1 d
true
hour
1 h
true
week
7 d
true
minute
1 m
true
second
1 s
true
# case 3: AD dates, origin > input
query TTT
SELECT
str,
"interval",
date_trunc(str, ts) = date_bin("interval"::interval, ts, timestamptz '2020-03-02') AS equal
FROM (
VALUES
('week', '7 d'),
('day', '1 d'),
('hour', '1 h'),
('minute', '1 m'),
('second', '1 s')
) intervals (str, interval),
(VALUES (timestamptz '2020-02-29 15:44:17.71393+00')) ts (ts);
----
day
1 d
true
hour
1 h
true
week
7 d
true
minute
1 m
true
second
1 s
true
# bin timestamps into arbitrary intervals
query TT
SELECT
"interval",
date_bin("interval"::interval, ts, origin)
FROM (
VALUES
('15 days'),
('2 hours'),
('1 hour 30 minutes'),
('15 minutes'),
('10 seconds')
) intervals (interval),
(VALUES (timestamptz '2020-02-11 15:44:17.71393+00')) ts (ts),
(VALUES (timestamptz '2001-01-01')) origin (origin);
----
15 days
2020-02-06 00:00:00+00
2 hours
2020-02-11 14:00:00+00
10 seconds
2020-02-11 15:44:10+00
15 minutes
2020-02-11 15:30:00+00
1 hour 30 minutes
2020-02-11 15:00:00+00
# shift bins using the origin parameter:
query T
SELECT date_bin('5 min'::interval, timestamptz '2020-02-01 01:01:01+00', timestamptz '2020-02-01 00:02:30+00');
----
2020-02-01 00:57:30+00
# disallow > day intervals
query error timestamps cannot be binned into intervals containing months or years
SELECT date_bin('5 months'::interval, timestamptz '2020-02-01 01:01:01+00', timestamptz '2001-01-01');
query error timestamps cannot be binned into intervals containing months or years
SELECT date_bin('5 years'::interval, timestamptz '2020-02-01 01:01:01+00', timestamptz '2001-01-01');
# disallow zero intervals
query error stride must be greater than zero
SELECT date_bin('0 days'::interval, timestamptz '1970-01-01 01:00:00+00' , timestamptz '1970-01-01 00:00:00+00');
# disallow negative intervals
query error stride must be greater than zero
SELECT date_bin('-2 days'::interval, timestamptz '1970-01-01 01:00:00+00' , timestamptz '1970-01-01 00:00:00+00');
# max stride caught
query error stride cannot exceed 2\^63 nanoseconds
SELECT date_bin('9223372037 s'::interval, timestamptz '2020-02-01 01:01:01+00', timestamptz '2020-02-01 00:02:30+00');
# max diff caught
query error source and origin must not differ more than 2\^63 nanoseconds
SELECT date_bin('1m', timestamptz '2262-04-11 23:47:17+00');
# Test unintuitive day binning
query TT
SELECT
"interval",
date_bin("interval"::interval, timestamptz '2020-02-27 15:44:17.71393+00', timestamptz '2001-01-01')
FROM (
VALUES
('1d'), ('2d'), ('3d'),
('4d'), ('5d'), ('6d'),
('7d'), ('8d'), ('9d')
) intervals ("interval");
----
1d
2020-02-27 00:00:00+00
2d
2020-02-27 00:00:00+00
3d
2020-02-27 00:00:00+00
4d
2020-02-27 00:00:00+00
5d
2020-02-26 00:00:00+00
6d
2020-02-27 00:00:00+00
7d
2020-02-24 00:00:00+00
8d
2020-02-23 00:00:00+00
9d
2020-02-24 00:00:00+00
# Test that binary extension behaves as expected
query TT
SELECT
"interval",
date_bin("interval"::interval, ts) = date_bin("interval"::interval, ts, timestamptz '1970-01-01') AS equal
FROM (
VALUES
('15 days'),
('2 hours'),
('1 hour 30 minutes'),
('15 minutes'),
('10 seconds')
) intervals (interval),
(VALUES (timestamptz '2020-02-29 15:44:17.71393+00')) ts (ts);
----
15 days
true
2 hours
true
10 seconds
true
15 minutes
true
1 hour 30 minutes
true