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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
<!-- doc/src/sgml/seg.sgml -->
<sect1 id="seg" xreflabel="seg">
<title>seg</title>
<indexterm zone="seg">
<primary>seg</primary>
</indexterm>
<para>
This module implements a data type <type>seg</type> for
representing line segments, or floating point intervals.
<type>seg</type> can represent uncertainty in the interval endpoints,
making it especially useful for representing laboratory measurements.
</para>
<para>
This module is considered <quote>trusted</quote>, that is, it can be
installed by non-superusers who have <literal>CREATE</literal> privilege
on the current database.
</para>
<sect2>
<title>Rationale</title>
<para>
The geometry of measurements is usually more complex than that of a
point in a numeric continuum. A measurement is usually a segment of
that continuum with somewhat fuzzy limits. The measurements come out
as intervals because of uncertainty and randomness, as well as because
the value being measured may naturally be an interval indicating some
condition, such as the temperature range of stability of a protein.
</para>
<para>
Using just common sense, it appears more convenient to store such data
as intervals, rather than pairs of numbers. In practice, it even turns
out more efficient in most applications.
</para>
<para>
Further along the line of common sense, the fuzziness of the limits
suggests that the use of traditional numeric data types leads to a
certain loss of information. Consider this: your instrument reads
6.50, and you input this reading into the database. What do you get
when you fetch it? Watch:
<screen>
test=> select 6.50 :: float8 as "pH";
pH
---
6.5
(1 row)
</screen>
In the world of measurements, 6.50 is not the same as 6.5. It may
sometimes be critically different. The experimenters usually write
down (and publish) the digits they trust. 6.50 is actually a fuzzy
interval contained within a bigger and even fuzzier interval, 6.5,
with their center points being (probably) the only common feature they
share. We definitely do not want such different data items to appear the
same.
</para>
<para>
Conclusion? It is nice to have a special data type that can record the
limits of an interval with arbitrarily variable precision. Variable in
the sense that each data element records its own precision.
</para>
<para>
Check this out:
<screen>
test=> select '6.25 .. 6.50'::seg as "pH";
pH
------------
6.25 .. 6.50
(1 row)
</screen>
</para>
</sect2>
<sect2>
<title>Syntax</title>
<para>
The external representation of an interval is formed using one or two
floating-point numbers joined by the range operator (<literal>..</literal>
or <literal>...</literal>). Alternatively, it can be specified as a
center point plus or minus a deviation.
Optional certainty indicators (<literal><</literal>,
<literal>></literal> or <literal>~</literal>) can be stored as well.
(Certainty indicators are ignored by all the built-in operators, however.)
<xref linkend="seg-repr-table"/> gives an overview of allowed
representations; <xref linkend="seg-input-examples"/> shows some
examples.
</para>
<para>
In <xref linkend="seg-repr-table"/>, <replaceable>x</replaceable>, <replaceable>y</replaceable>, and
<replaceable>delta</replaceable> denote
floating-point numbers. <replaceable>x</replaceable> and <replaceable>y</replaceable>, but
not <replaceable>delta</replaceable>, can be preceded by a certainty indicator.
</para>
<table id="seg-repr-table">
<title><type>seg</type> External Representations</title>
<tgroup cols="2">
<tbody>
<row>
<entry><literal><replaceable>x</replaceable></literal></entry>
<entry>Single value (zero-length interval)
</entry>
</row>
<row>
<entry><literal><replaceable>x</replaceable> .. <replaceable>y</replaceable></literal></entry>
<entry>Interval from <replaceable>x</replaceable> to <replaceable>y</replaceable>
</entry>
</row>
<row>
<entry><literal><replaceable>x</replaceable> (+-) <replaceable>delta</replaceable></literal></entry>
<entry>Interval from <replaceable>x</replaceable> - <replaceable>delta</replaceable> to
<replaceable>x</replaceable> + <replaceable>delta</replaceable>
</entry>
</row>
<row>
<entry><literal><replaceable>x</replaceable> ..</literal></entry>
<entry>Open interval with lower bound <replaceable>x</replaceable>
</entry>
</row>
<row>
<entry><literal>.. <replaceable>x</replaceable></literal></entry>
<entry>Open interval with upper bound <replaceable>x</replaceable>
</entry>
</row>
</tbody>
</tgroup>
</table>
<table id="seg-input-examples">
<title>Examples of Valid <type>seg</type> Input</title>
<tgroup cols="2">
<colspec colname="col1" colwidth="1*"/>
<colspec colname="col2" colwidth="2*"/>
<tbody>
<row>
<entry><literal>5.0</literal></entry>
<entry>
Creates a zero-length segment (a point, if you will)
</entry>
</row>
<row>
<entry><literal>~5.0</literal></entry>
<entry>
Creates a zero-length segment and records
<literal>~</literal> in the data. <literal>~</literal> is ignored
by <type>seg</type> operations, but
is preserved as a comment.
</entry>
</row>
<row>
<entry><literal><5.0</literal></entry>
<entry>
Creates a point at 5.0. <literal><</literal> is ignored but
is preserved as a comment.
</entry>
</row>
<row>
<entry><literal>>5.0</literal></entry>
<entry>
Creates a point at 5.0. <literal>></literal> is ignored but
is preserved as a comment.
</entry>
</row>
<row>
<entry><literal>5(+-)0.3</literal></entry>
<entry>
Creates an interval <literal>4.7 .. 5.3</literal>.
Note that the <literal>(+-)</literal> notation isn't preserved.
</entry>
</row>
<row>
<entry><literal>50 .. </literal></entry>
<entry>Everything that is greater than or equal to 50</entry>
</row>
<row>
<entry><literal>.. 0</literal></entry>
<entry>Everything that is less than or equal to 0</entry>
</row>
<row>
<entry><literal>1.5e-2 .. 2E-2 </literal></entry>
<entry>Creates an interval <literal>0.015 .. 0.02</literal></entry>
</row>
<row>
<entry><literal>1 ... 2</literal></entry>
<entry>
The same as <literal>1...2</literal>, or <literal>1 .. 2</literal>,
or <literal>1..2</literal>
(spaces around the range operator are ignored)
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
Because the <literal>...</literal> operator is widely used in data sources, it is allowed
as an alternative spelling of the <literal>..</literal> operator. Unfortunately, this
creates a parsing ambiguity: it is not clear whether the upper bound
in <literal>0...23</literal> is meant to be <literal>23</literal> or <literal>0.23</literal>.
This is resolved by requiring at least one digit before the decimal
point in all numbers in <type>seg</type> input.
</para>
<para>
As a sanity check, <type>seg</type> rejects intervals with the lower bound
greater than the upper, for example <literal>5 .. 2</literal>.
</para>
</sect2>
<sect2>
<title>Precision</title>
<para>
<type>seg</type> values are stored internally as pairs of 32-bit floating point
numbers. This means that numbers with more than 7 significant digits
will be truncated.
</para>
<para>
Numbers with 7 or fewer significant digits retain their
original precision. That is, if your query returns 0.00, you will be
sure that the trailing zeroes are not the artifacts of formatting: they
reflect the precision of the original data. The number of leading
zeroes does not affect precision: the value 0.0067 is considered to
have just 2 significant digits.
</para>
</sect2>
<sect2>
<title>Usage</title>
<para>
The <filename>seg</filename> module includes a GiST index operator class for
<type>seg</type> values.
The operators supported by the GiST operator class are shown in <xref linkend="seg-gist-operators"/>.
</para>
<table id="seg-gist-operators">
<title>Seg GiST Operators</title>
<tgroup cols="1">
<thead>
<row>
<entry role="func_table_entry"><para role="func_signature">
Operator
</para>
<para>
Description
</para></entry>
</row>
</thead>
<tbody>
<row>
<entry role="func_table_entry"><para role="func_signature">
<type>seg</type> <literal><<</literal> <type>seg</type>
<returnvalue>boolean</returnvalue>
</para>
<para>
Is the first <type>seg</type> entirely to the left of the second?
[a, b] << [c, d] is true if b < c.
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<type>seg</type> <literal>>></literal> <type>seg</type>
<returnvalue>boolean</returnvalue>
</para>
<para>
Is the first <type>seg</type> entirely to the right of the second?
[a, b] >> [c, d] is true if a > d.
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<type>seg</type> <literal>&<</literal> <type>seg</type>
<returnvalue>boolean</returnvalue>
</para>
<para>
Does the first <type>seg</type> not extend to the right of the
second?
[a, b] &< [c, d] is true if b <= d.
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<type>seg</type> <literal>&></literal> <type>seg</type>
<returnvalue>boolean</returnvalue>
</para>
<para>
Does the first <type>seg</type> not extend to the left of the
second?
[a, b] &> [c, d] is true if a >= c.
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<type>seg</type> <literal>=</literal> <type>seg</type>
<returnvalue>boolean</returnvalue>
</para>
<para>
Are the two <type>seg</type>s equal?
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<type>seg</type> <literal>&&</literal> <type>seg</type>
<returnvalue>boolean</returnvalue>
</para>
<para>
Do the two <type>seg</type>s overlap?
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<type>seg</type> <literal>@></literal> <type>seg</type>
<returnvalue>boolean</returnvalue>
</para>
<para>
Does the first <type>seg</type> contain the second?
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
<type>seg</type> <literal><@</literal> <type>seg</type>
<returnvalue>boolean</returnvalue>
</para>
<para>
Is the first <type>seg</type> contained in the second?
</para></entry>
</row>
</tbody>
</tgroup>
</table>
<para>
In addition to the above operators, the usual comparison
operators shown in <xref linkend="functions-comparison-op-table"/> are
available for type <type>seg</type>. These operators
first compare (a) to (c),
and if these are equal, compare (b) to (d). That results in
reasonably good sorting in most cases, which is useful if
you want to use ORDER BY with this type.
</para>
</sect2>
<sect2>
<title>Notes</title>
<para>
For examples of usage, see the regression test <filename>sql/seg.sql</filename>.
</para>
<para>
The mechanism that converts <literal>(+-)</literal> to regular ranges
isn't completely accurate in determining the number of significant digits
for the boundaries. For example, it adds an extra digit to the lower
boundary if the resulting interval includes a power of ten:
<screen>
postgres=> select '10(+-)1'::seg as seg;
seg
---------
9.0 .. 11 -- should be: 9 .. 11
</screen>
</para>
<para>
The performance of an R-tree index can largely depend on the initial
order of input values. It may be very helpful to sort the input table
on the <type>seg</type> column; see the script <filename>sort-segments.pl</filename>
for an example.
</para>
</sect2>
<sect2>
<title>Credits</title>
<para>
Original author: Gene Selkov, Jr. <email>selkovjr@mcs.anl.gov</email>,
Mathematics and Computer Science Division, Argonne National Laboratory.
</para>
<para>
My thanks are primarily to Prof. Joe Hellerstein
(<ulink url="https://dsf.berkeley.edu/jmh/"></ulink>) for elucidating the
gist of the GiST (<ulink url="http://gist.cs.berkeley.edu/"></ulink>). I am
also grateful to all Postgres developers, present and past, for enabling
myself to create my own world and live undisturbed in it. And I would like
to acknowledge my gratitude to Argonne Lab and to the U.S. Department of
Energy for the years of faithful support of my database research.
</para>
</sect2>
</sect1>
|