summaryrefslogtreecommitdiffstats
path: root/contrib/seg/sql/seg.sql
blob: c30f1f6bef10d81d2cb4b08891a00eb5cd3f11d2 (plain)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
--
--  Test seg datatype
--

CREATE EXTENSION seg;

-- Check whether any of our opclasses fail amvalidate
SELECT amname, opcname
FROM pg_opclass opc LEFT JOIN pg_am am ON am.oid = opcmethod
WHERE opc.oid >= 16384 AND NOT amvalidate(opc.oid);

--
-- testing the input and output functions
--

-- Any number
SELECT '1'::seg AS seg;
SELECT '-1'::seg AS seg;
SELECT '1.0'::seg AS seg;
SELECT '-1.0'::seg AS seg;
SELECT '1e7'::seg AS seg;
SELECT '-1e7'::seg AS seg;
SELECT '1.0e7'::seg AS seg;
SELECT '-1.0e7'::seg AS seg;
SELECT '1e+7'::seg AS seg;
SELECT '-1e+7'::seg AS seg;
SELECT '1.0e+7'::seg AS seg;
SELECT '-1.0e+7'::seg AS seg;
SELECT '1e-7'::seg AS seg;
SELECT '-1e-7'::seg AS seg;
SELECT '1.0e-7'::seg AS seg;
SELECT '-1.0e-7'::seg AS seg;
SELECT '2e-6'::seg AS seg;
SELECT '2e-5'::seg AS seg;
SELECT '2e-4'::seg AS seg;
SELECT '2e-3'::seg AS seg;
SELECT '2e-2'::seg AS seg;
SELECT '2e-1'::seg AS seg;
SELECT '2e-0'::seg AS seg;
SELECT '2e+0'::seg AS seg;
SELECT '2e+1'::seg AS seg;
SELECT '2e+2'::seg AS seg;
SELECT '2e+3'::seg AS seg;
SELECT '2e+4'::seg AS seg;
SELECT '2e+5'::seg AS seg;
SELECT '2e+6'::seg AS seg;


-- Significant digits preserved
SELECT '1'::seg AS seg;
SELECT '1.0'::seg AS seg;
SELECT '1.00'::seg AS seg;
SELECT '1.000'::seg AS seg;
SELECT '1.0000'::seg AS seg;
SELECT '1.00000'::seg AS seg;
SELECT '1.000000'::seg AS seg;
SELECT '0.000000120'::seg AS seg;
SELECT '3.400e5'::seg AS seg;

-- Digits truncated
SELECT '12.34567890123456'::seg AS seg;

-- Same, with a very long input
SELECT '12.3456789012345600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'::seg AS seg;

-- Numbers with certainty indicators
SELECT '~6.5'::seg AS seg;
SELECT '<6.5'::seg AS seg;
SELECT '>6.5'::seg AS seg;
SELECT '~ 6.5'::seg AS seg;
SELECT '< 6.5'::seg AS seg;
SELECT '> 6.5'::seg AS seg;

-- Open intervals
SELECT '0..'::seg AS seg;
SELECT '0...'::seg AS seg;
SELECT '0 ..'::seg AS seg;
SELECT '0 ...'::seg AS seg;
SELECT '..0'::seg AS seg;
SELECT '...0'::seg AS seg;
SELECT '.. 0'::seg AS seg;
SELECT '... 0'::seg AS seg;

-- Finite intervals
SELECT '0 .. 1'::seg AS seg;
SELECT '-1 .. 0'::seg AS seg;
SELECT '-1 .. 1'::seg AS seg;

-- (+/-) intervals
SELECT '0(+-)1'::seg AS seg;
SELECT '0(+-)1.0'::seg AS seg;
SELECT '1.0(+-)0.005'::seg AS seg;
SELECT '101(+-)1'::seg AS seg;
-- incorrect number of significant digits in 99.0:
SELECT '100(+-)1'::seg AS seg;

-- invalid input
SELECT ''::seg AS seg;
SELECT 'ABC'::seg AS seg;
SELECT '1ABC'::seg AS seg;
SELECT '1.'::seg AS seg;
SELECT '1.....'::seg AS seg;
SELECT '.1'::seg AS seg;
SELECT '1..2.'::seg AS seg;
SELECT '1 e7'::seg AS seg;
SELECT '1e700'::seg AS seg;

--
-- testing the  operators
--

-- equality/inequality:
--
SELECT '24 .. 33.20'::seg = '24 .. 33.20'::seg AS bool;
SELECT '24 .. 33.20'::seg = '24 .. 33.21'::seg AS bool;
SELECT '24 .. 33.20'::seg != '24 .. 33.20'::seg AS bool;
SELECT '24 .. 33.20'::seg != '24 .. 33.21'::seg AS bool;

-- overlap
--
SELECT '1'::seg && '1'::seg AS bool;
SELECT '1'::seg && '2'::seg AS bool;
SELECT '0 ..'::seg && '0 ..'::seg AS bool;
SELECT '0 .. 1'::seg && '0 .. 1'::seg AS bool;
SELECT '..0'::seg && '0..'::seg AS bool;
SELECT '-1 .. 0.1'::seg && '0 .. 1'::seg AS bool;
SELECT '-1 .. 0'::seg && '0 .. 1'::seg AS bool;
SELECT '-1 .. -0.0001'::seg && '0 .. 1'::seg AS bool;
SELECT '0 ..'::seg && '1'::seg AS bool;
SELECT '0 .. 1'::seg && '1'::seg AS bool;
SELECT '0 .. 1'::seg && '2'::seg AS bool;
SELECT '0 .. 2'::seg && '1'::seg AS bool;
SELECT '1'::seg && '0 .. 1'::seg AS bool;
SELECT '2'::seg && '0 .. 1'::seg AS bool;
SELECT '1'::seg && '0 .. 2'::seg AS bool;

-- overlap on the left
--
SELECT '1'::seg &< '0'::seg AS bool;
SELECT '1'::seg &< '1'::seg AS bool;
SELECT '1'::seg &< '2'::seg AS bool;
SELECT '0 .. 1'::seg &< '0'::seg AS bool;
SELECT '0 .. 1'::seg &< '1'::seg AS bool;
SELECT '0 .. 1'::seg &< '2'::seg AS bool;
SELECT '0 .. 1'::seg &< '0 .. 0.5'::seg AS bool;
SELECT '0 .. 1'::seg &< '0 .. 1'::seg AS bool;
SELECT '0 .. 1'::seg &< '0 .. 2'::seg AS bool;
SELECT '0 .. 1'::seg &< '1 .. 2'::seg AS bool;
SELECT '0 .. 1'::seg &< '2 .. 3'::seg AS bool;

-- overlap on the right
--
SELECT '0'::seg &> '1'::seg AS bool;
SELECT '1'::seg &> '1'::seg AS bool;
SELECT '2'::seg &> '1'::seg AS bool;
SELECT '0'::seg &> '0 .. 1'::seg AS bool;
SELECT '1'::seg &> '0 .. 1'::seg AS bool;
SELECT '2'::seg &> '0 .. 1'::seg AS bool;
SELECT '0 .. 0.5'::seg &> '0 .. 1'::seg AS bool;
SELECT '0 .. 1'::seg &> '0 .. 1'::seg AS bool;
SELECT '0 .. 2'::seg &> '0 .. 2'::seg AS bool;
SELECT '1 .. 2'::seg &> '0 .. 1'::seg AS bool;
SELECT '2 .. 3'::seg &> '0 .. 1'::seg AS bool;

-- left
--
SELECT '1'::seg << '0'::seg AS bool;
SELECT '1'::seg << '1'::seg AS bool;
SELECT '1'::seg << '2'::seg AS bool;
SELECT '0 .. 1'::seg << '0'::seg AS bool;
SELECT '0 .. 1'::seg << '1'::seg AS bool;
SELECT '0 .. 1'::seg << '2'::seg AS bool;
SELECT '0 .. 1'::seg << '0 .. 0.5'::seg AS bool;
SELECT '0 .. 1'::seg << '0 .. 1'::seg AS bool;
SELECT '0 .. 1'::seg << '0 .. 2'::seg AS bool;
SELECT '0 .. 1'::seg << '1 .. 2'::seg AS bool;
SELECT '0 .. 1'::seg << '2 .. 3'::seg AS bool;

-- right
--
SELECT '0'::seg >> '1'::seg AS bool;
SELECT '1'::seg >> '1'::seg AS bool;
SELECT '2'::seg >> '1'::seg AS bool;
SELECT '0'::seg >> '0 .. 1'::seg AS bool;
SELECT '1'::seg >> '0 .. 1'::seg AS bool;
SELECT '2'::seg >> '0 .. 1'::seg AS bool;
SELECT '0 .. 0.5'::seg >> '0 .. 1'::seg AS bool;
SELECT '0 .. 1'::seg >> '0 .. 1'::seg AS bool;
SELECT '0 .. 2'::seg >> '0 .. 2'::seg AS bool;
SELECT '1 .. 2'::seg >> '0 .. 1'::seg AS bool;
SELECT '2 .. 3'::seg >> '0 .. 1'::seg AS bool;


-- "contained in" (the left value belongs within the interval specified in the right value):
--
SELECT '0'::seg        <@ '0'::seg AS bool;
SELECT '0'::seg        <@ '0 ..'::seg AS bool;
SELECT '0'::seg        <@ '.. 0'::seg AS bool;
SELECT '0'::seg        <@ '-1 .. 1'::seg AS bool;
SELECT '0'::seg        <@ '-1 .. 1'::seg AS bool;
SELECT '-1'::seg       <@ '-1 .. 1'::seg AS bool;
SELECT '1'::seg        <@ '-1 .. 1'::seg AS bool;
SELECT '-1 .. 1'::seg  <@ '-1 .. 1'::seg AS bool;

-- "contains" (the left value contains the interval specified in the right value):
--
SELECT '0'::seg @> '0'::seg AS bool;
SELECT '0 .. '::seg <@ '0'::seg AS bool;
SELECT '.. 0'::seg <@ '0'::seg AS bool;
SELECT '-1 .. 1'::seg <@ '0'::seg AS bool;
SELECT '0'::seg <@ '-1 .. 1'::seg AS bool;
SELECT '-1'::seg <@ '-1 .. 1'::seg AS bool;
SELECT '1'::seg <@ '-1 .. 1'::seg AS bool;

-- Load some example data and build the index
--
CREATE TABLE test_seg (s seg);

\copy test_seg from 'data/test_seg.data'

CREATE INDEX test_seg_ix ON test_seg USING gist (s);

SET enable_indexscan = false;
EXPLAIN (COSTS OFF)
SELECT count(*) FROM test_seg WHERE s @> '11..11.3';
SELECT count(*) FROM test_seg WHERE s @> '11..11.3';
RESET enable_indexscan;

SET enable_bitmapscan = false;
EXPLAIN (COSTS OFF)
SELECT count(*) FROM test_seg WHERE s @> '11..11.3';
SELECT count(*) FROM test_seg WHERE s @> '11..11.3';
RESET enable_bitmapscan;

-- Test sorting
SELECT * FROM test_seg WHERE s @> '11..11.3' GROUP BY s;

-- Test functions
SELECT seg_lower(s), seg_center(s), seg_upper(s)
FROM test_seg WHERE s @> '11.2..11.3' OR s IS NULL ORDER BY s;


-- test non error throwing API

SELECT str as seg,
       pg_input_is_valid(str,'seg') as ok,
       errinfo.sql_error_code,
       errinfo.message,
       errinfo.detail,
       errinfo.hint
FROM unnest(ARRAY['-1 .. 1'::text,
                  '100(+-)1',
                  '',
                  'ABC',
                  '1 e7',
                  '1e700']) str,
     LATERAL pg_input_error_info(str, 'seg') as errinfo;