1
<!-- doc/src/sgml/cube.sgml -->
6
<indexterm zone="cube">
7
<primary>cube</primary>
11
This module implements a data type <type>cube</> for
12
representing multidimensional cubes.
19
<xref linkend="cube-repr-table"> shows the valid external
20
representations for the <type>cube</>
21
type. <replaceable>x</>, <replaceable>y</>, etc. denote
22
floating-point numbers.
25
<table id="cube-repr-table">
26
<title>Cube External Representations</title>
30
<entry><literal><replaceable>x</></literal></entry>
31
<entry>A one-dimensional point
32
(or, zero-length one-dimensional interval)
36
<entry><literal>(<replaceable>x</>)</literal></entry>
37
<entry>Same as above</entry>
40
<entry><literal><replaceable>x1</>,<replaceable>x2</>,...,<replaceable>xn</></literal></entry>
41
<entry>A point in n-dimensional space, represented internally as a
46
<entry><literal>(<replaceable>x1</>,<replaceable>x2</>,...,<replaceable>xn</>)</literal></entry>
47
<entry>Same as above</entry>
50
<entry><literal>(<replaceable>x</>),(<replaceable>y</>)</literal></entry>
51
<entry>A one-dimensional interval starting at <replaceable>x</> and ending at <replaceable>y</> or vice versa; the
56
<entry><literal>[(<replaceable>x</>),(<replaceable>y</>)]</literal></entry>
57
<entry>Same as above</entry>
60
<entry><literal>(<replaceable>x1</>,...,<replaceable>xn</>),(<replaceable>y1</>,...,<replaceable>yn</>)</literal></entry>
61
<entry>An n-dimensional cube represented by a pair of its diagonally
66
<entry><literal>[(<replaceable>x1</>,...,<replaceable>xn</>),(<replaceable>y1</>,...,<replaceable>yn</>)]</literal></entry>
67
<entry>Same as above</entry>
74
It does not matter which order the opposite corners of a cube are
75
entered in. The <type>cube</> functions
76
automatically swap values if needed to create a uniform
77
<quote>lower left — upper right</> internal representation.
81
White space is ignored, so <literal>[(<replaceable>x</>),(<replaceable>y</>)]</literal> is the same as
82
<literal>[ ( <replaceable>x</> ), ( <replaceable>y</> ) ]</literal>.
87
<title>Precision</title>
90
Values are stored internally as 64-bit floating point numbers. This means
91
that numbers with more than about 16 significant digits will be truncated.
99
The <filename>cube</> module includes a GiST index operator class for
100
<type>cube</> values.
101
The operators supported by the GiST operator class are shown in <xref linkend="cube-gist-operators">.
104
<table id="cube-gist-operators">
105
<title>Cube GiST Operators</title>
109
<entry>Operator</entry>
110
<entry>Description</entry>
116
<entry><literal>a = b</></entry>
117
<entry>The cubes a and b are identical.</entry>
121
<entry><literal>a && b</></entry>
122
<entry>The cubes a and b overlap.</entry>
126
<entry><literal>a @> b</></entry>
127
<entry>The cube a contains the cube b.</entry>
131
<entry><literal>a <@ b</></entry>
132
<entry>The cube a is contained in the cube b.</entry>
139
(Before PostgreSQL 8.2, the containment operators <literal>@></> and <literal><@</> were
140
respectively called <literal>@</> and <literal>~</>. These names are still available, but are
141
deprecated and will eventually be retired. Notice that the old names
142
are reversed from the convention formerly followed by the core geometric
147
The standard B-tree operators are also provided, for example
153
<entry>Operator</entry>
154
<entry>Description</entry>
160
<entry><literal>[a, b] < [c, d]</literal></entry>
161
<entry>Less than</entry>
165
<entry><literal>[a, b] > [c, d]</literal></entry>
166
<entry>Greater than</entry>
172
These operators do not make a lot of sense for any practical
173
purpose but sorting. These operators first compare (a) to (c),
174
and if these are equal, compare (b) to (d). That results in
175
reasonably good sorting in most cases, which is useful if
176
you want to use ORDER BY with this type.
180
<xref linkend="cube-functions-table"> shows the available functions.
183
<table id="cube-functions-table">
184
<title>Cube Functions</title>
188
<entry><literal>cube(float8) returns cube</literal></entry>
189
<entry>Makes a one dimensional cube with both coordinates the same.
190
<literal>cube(1) == '(1)'</literal>
195
<entry><literal>cube(float8, float8) returns cube</literal></entry>
196
<entry>Makes a one dimensional cube.
197
<literal>cube(1,2) == '(1),(2)'</literal>
202
<entry><literal>cube(float8[]) returns cube</literal></entry>
203
<entry>Makes a zero-volume cube using the coordinates
204
defined by the array.
205
<literal>cube(ARRAY[1,2]) == '(1,2)'</literal>
210
<entry><literal>cube(float8[], float8[]) returns cube</literal></entry>
211
<entry>Makes a cube with upper right and lower left
212
coordinates as defined by the two arrays, which must be of the
214
<literal>cube('{1,2}'::float[], '{3,4}'::float[]) == '(1,2),(3,4)'
220
<entry><literal>cube(cube, float8) returns cube</literal></entry>
221
<entry>Makes a new cube by adding a dimension on to an
222
existing cube with the same values for both parts of the new coordinate.
223
This is useful for building cubes piece by piece from calculated values.
224
<literal>cube('(1)',2) == '(1,2),(1,2)'</literal>
229
<entry><literal>cube(cube, float8, float8) returns cube</literal></entry>
230
<entry>Makes a new cube by adding a dimension on to an
231
existing cube. This is useful for building cubes piece by piece from
232
calculated values. <literal>cube('(1,2)',3,4) == '(1,3),(2,4)'</literal>
237
<entry><literal>cube_dim(cube) returns int</literal></entry>
238
<entry>Returns the number of dimensions of the cube
243
<entry><literal>cube_ll_coord(cube, int) returns double </literal></entry>
244
<entry>Returns the n'th coordinate value for the lower left
250
<entry><literal>cube_ur_coord(cube, int) returns double
252
<entry>Returns the n'th coordinate value for the
253
upper right corner of a cube
258
<entry><literal>cube_is_point(cube) returns bool</literal></entry>
259
<entry>Returns true if a cube is a point, that is,
260
the two defining corners are the same.</entry>
264
<entry><literal>cube_distance(cube, cube) returns double</literal></entry>
265
<entry>Returns the distance between two cubes. If both
266
cubes are points, this is the normal distance function.
271
<entry><literal>cube_subset(cube, int[]) returns cube
273
<entry>Makes a new cube from an existing cube, using a list of
274
dimension indexes from an array. Can be used to find both the LL and UR
275
coordinates of a single dimension, e.g.
276
<literal>cube_subset(cube('(1,3,5),(6,7,8)'), ARRAY[2]) = '(3),(7)'</>.
277
Or can be used to drop dimensions, or reorder them as desired, e.g.
278
<literal>cube_subset(cube('(1,3,5),(6,7,8)'), ARRAY[3,2,1,1]) = '(5, 3,
279
1, 1),(8, 7, 6, 6)'</>.
284
<entry><literal>cube_union(cube, cube) returns cube</literal></entry>
285
<entry>Produces the union of two cubes
290
<entry><literal>cube_inter(cube, cube) returns cube</literal></entry>
291
<entry>Produces the intersection of two cubes
296
<entry><literal>cube_enlarge(cube c, double r, int n) returns cube</literal></entry>
297
<entry>Increases the size of a cube by a specified radius in at least
298
n dimensions. If the radius is negative the cube is shrunk instead. This
299
is useful for creating bounding boxes around a point for searching for
300
nearby points. All defined dimensions are changed by the radius r.
301
LL coordinates are decreased by r and UR coordinates are increased by r.
302
If a LL coordinate is increased to larger than the corresponding UR
303
coordinate (this can only happen when r < 0) than both coordinates
304
are set to their average. If n is greater than the number of defined
305
dimensions and the cube is being increased (r >= 0) then 0 is used
306
as the base for the extra coordinates.
315
<title>Defaults</title>
318
I believe this union:
321
select cube_union('(0,5,2),(2,3,1)', '0');
329
does not contradict common sense, neither does the intersection
333
select cube_inter('(0,-1),(1,1)', '(-2),(2)');
341
In all binary operations on differently-dimensioned cubes, I assume the
342
lower-dimensional one to be a Cartesian projection, i. e., having zeroes
343
in place of coordinates omitted in the string representation. The above
344
examples are equivalent to:
348
cube_union('(0,5,2),(2,3,1)','(0,0,0),(0,0,0)');
349
cube_inter('(0,-1),(1,1)','(-2,0),(2,0)');
353
The following containment predicate uses the point syntax,
354
while in fact the second argument is internally represented by a box.
355
This syntax makes it unnecessary to define a separate point type
356
and functions for (box,point) predicates.
360
select cube_contains('(0,0),(1,1)', '0.5,0.5');
372
For examples of usage, see the regression test <filename>sql/cube.sql</>.
376
To make it harder for people to break things, there
377
is a limit of 100 on the number of dimensions of cubes. This is set
378
in <filename>cubedata.h</> if you need something bigger.
383
<title>Credits</title>
386
Original author: Gene Selkov, Jr. <email>selkovjr@mcs.anl.gov</email>,
387
Mathematics and Computer Science Division, Argonne National Laboratory.
391
My thanks are primarily to Prof. Joe Hellerstein
392
(<ulink url="http://db.cs.berkeley.edu/jmh/"></ulink>) for elucidating the
393
gist of the GiST (<ulink url="http://gist.cs.berkeley.edu/"></ulink>), and
394
to his former student, Andy Dong (<ulink
395
url="http://best.me.berkeley.edu/~adong/"></ulink>), for his example
396
written for Illustra,
397
<ulink url="http://best.berkeley.edu/~adong/rtree/index.html"></ulink>.
398
I am also grateful to all Postgres developers, present and past, for
399
enabling myself to create my own world and live undisturbed in it. And I
400
would like to acknowledge my gratitude to Argonne Lab and to the
401
U.S. Department of Energy for the years of faithful support of my database
406
Minor updates to this package were made by Bruno Wolff III
407
<email>bruno@wolff.to</email> in August/September of 2002. These include
408
changing the precision from single precision to double precision and adding
413
Additional updates were made by Joshua Reich <email>josh@root.net</email> in
414
July 2006. These include <literal>cube(float8[], float8[])</literal> and
415
cleaning up the code to use the V1 call protocol instead of the deprecated