6 <indexterm zone=
"cube">
7 <primary>cube
</primary>
11 This module implements a data type
<type>cube<
/> for
12 representing multi-dimensional cubes.
19 The following are valid external representations for the
<type>cube<
/>
20 type.
<replaceable>x<
/>,
<replaceable>y<
/>, etc denote floating-point
25 <title>Cube external representations
</title>
29 <entry><literal><replaceable>x<
/></literal></entry>
30 <entry>A one-dimensional point
31 (or, zero-length one-dimensional interval)
35 <entry><literal>(
<replaceable>x<
/>)
</literal></entry>
36 <entry>Same as above
</entry>
39 <entry><literal><replaceable>x1<
/>,
<replaceable>x2<
/>,...,
<replaceable>xn<
/></literal></entry>
40 <entry>A point in n-dimensional space, represented internally as a
45 <entry><literal>(
<replaceable>x1<
/>,
<replaceable>x2<
/>,...,
<replaceable>xn<
/>)
</literal></entry>
46 <entry>Same as above
</entry>
49 <entry><literal>(
<replaceable>x<
/>),(
<replaceable>y<
/>)
</literal></entry>
50 <entry>A one-dimensional interval starting at
<replaceable>x<
/> and ending at
<replaceable>y<
/> or vice versa; the
55 <entry><literal>[(
<replaceable>x<
/>),(
<replaceable>y<
/>)]
</literal></entry>
56 <entry>Same as above
</entry>
59 <entry><literal>(
<replaceable>x1<
/>,...,
<replaceable>xn<
/>),(
<replaceable>y1<
/>,...,
<replaceable>yn<
/>)
</literal></entry>
60 <entry>An n-dimensional cube represented by a pair of its diagonally
65 <entry><literal>[(
<replaceable>x1<
/>,...,
<replaceable>xn<
/>),(
<replaceable>y1<
/>,...,
<replaceable>yn<
/>)]
</literal></entry>
66 <entry>Same as above
</entry>
73 It does not matter which order the opposite corners of a cube are
74 entered in. The
<type>cube<
/> functions
75 automatically swap values if needed to create a uniform
76 <quote>lower left
— upper right<
/> internal representation.
80 White space is ignored, so
<literal>[(
<replaceable>x<
/>),(
<replaceable>y<
/>)]
</literal> is the same as
81 <literal>[ (
<replaceable>x<
/> ), (
<replaceable>y<
/> ) ]
</literal>.
86 <title>Precision
</title>
89 Values are stored internally as
64-bit floating point numbers. This means
90 that numbers with more than about
16 significant digits will be truncated.
98 The
<filename>cube<
/> module includes a GiST index operator class for
100 The operators supported by the GiST opclass include:
109 The cubes a and b are identical.
114 a
&& b Overlaps
117 The cubes a and b overlap.
125 The cube a contains the cube b.
130 a
<@ b Contained in
133 The cube a is contained in the cube b.
139 (Before PostgreSQL
8.2, the containment operators @
> and
<@ were
140 respectively called @ and ~. 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
150 [a, b]
< [c, d] Less than
151 [a, b]
> [c, d] Greater than
154 These operators do not make a lot of sense for any practical
155 purpose but sorting. These operators first compare (a) to (c),
156 and if these are equal, compare (b) to (d). That results in
157 reasonably good sorting in most cases, which is useful if
158 you want to use ORDER BY with this type.
162 The following functions are available:
166 <title>Cube functions
</title>
170 <entry><literal>cube(float8) returns cube
</literal></entry>
171 <entry>Makes a one dimensional cube with both coordinates the same.
172 <literal>cube(
1) == '(
1)'
</literal>
177 <entry><literal>cube(float8, float8) returns cube
</literal></entry>
178 <entry>Makes a one dimensional cube.
179 <literal>cube(
1,
2) == '(
1),(
2)'
</literal>
184 <entry><literal>cube(float8[]) returns cube
</literal></entry>
185 <entry>Makes a zero-volume cube using the coordinates
186 defined by the array.
187 <literal>cube(ARRAY[
1,
2]) == '(
1,
2)'
</literal>
192 <entry><literal>cube(float8[], float8[]) returns cube
</literal></entry>
193 <entry>Makes a cube with upper right and lower left
194 coordinates as defined by the two arrays, which must be of the
196 <literal>cube('{
1,
2}'::float[], '{
3,
4}'::float[]) == '(
1,
2),(
3,
4)'
202 <entry><literal>cube(cube, float8) returns cube
</literal></entry>
203 <entry>Makes a new cube by adding a dimension on to an
204 existing cube with the same values for both parts of the new coordinate.
205 This is useful for building cubes piece by piece from calculated values.
206 <literal>cube('(
1)',
2) == '(
1,
2),(
1,
2)'
</literal>
211 <entry><literal>cube(cube, float8, float8) returns cube
</literal></entry>
212 <entry>Makes a new cube by adding a dimension on to an
213 existing cube. This is useful for building cubes piece by piece from
214 calculated values.
<literal>cube('(
1,
2)',
3,
4) == '(
1,
3),(
2,
4)'
</literal>
219 <entry><literal>cube_dim(cube) returns int
</literal></entry>
220 <entry>Returns the number of dimensions of the cube
225 <entry><literal>cube_ll_coord(cube, int) returns double
</literal></entry>
226 <entry>Returns the n'th coordinate value for the lower left
232 <entry><literal>cube_ur_coord(cube, int) returns double
234 <entry>Returns the n'th coordinate value for the
235 upper right corner of a cube
240 <entry><literal>cube_is_point(cube) returns bool
</literal></entry>
241 <entry>Returns true if a cube is a point, that is,
242 the two defining corners are the same.
</entry>
246 <entry><literal>cube_distance(cube, cube) returns double
</literal></entry>
247 <entry>Returns the distance between two cubes. If both
248 cubes are points, this is the normal distance function.
253 <entry><literal>cube_subset(cube, int[]) returns cube
255 <entry>Makes a new cube from an existing cube, using a list of
256 dimension indexes from an array. Can be used to find both the LL and UR
257 coordinates of a single dimension, e.g.
258 <literal>cube_subset(cube('(
1,
3,
5),(
6,
7,
8)'), ARRAY[
2]) = '(
3),(
7)'<
/>.
259 Or can be used to drop dimensions, or reorder them as desired, e.g.
260 <literal>cube_subset(cube('(
1,
3,
5),(
6,
7,
8)'), ARRAY[
3,
2,
1,
1]) = '(
5,
3,
261 1,
1),(
8,
7,
6,
6)'<
/>.
266 <entry><literal>cube_union(cube, cube) returns cube
</literal></entry>
267 <entry>Produces the union of two cubes
272 <entry><literal>cube_inter(cube, cube) returns cube
</literal></entry>
273 <entry>Produces the intersection of two cubes
278 <entry><literal>cube_enlarge(cube c, double r, int n) returns cube
</literal></entry>
279 <entry>Increases the size of a cube by a specified radius in at least
280 n dimensions. If the radius is negative the cube is shrunk instead. This
281 is useful for creating bounding boxes around a point for searching for
282 nearby points. All defined dimensions are changed by the radius r.
283 LL coordinates are decreased by r and UR coordinates are increased by r.
284 If a LL coordinate is increased to larger than the corresponding UR
285 coordinate (this can only happen when r
< 0) than both coordinates
286 are set to their average. If n is greater than the number of defined
287 dimensions and the cube is being increased (r
>=
0) then
0 is used
288 as the base for the extra coordinates.
297 <title>Defaults
</title>
300 I believe this union:
303 select cube_union('(
0,
5,
2),(
2,
3,
1)', '
0');
311 does not contradict common sense, neither does the intersection
315 select cube_inter('(
0,-
1),(
1,
1)', '(-
2),(
2)');
323 In all binary operations on differently-dimensioned cubes, I assume the
324 lower-dimensional one to be a cartesian projection, i. e., having zeroes
325 in place of coordinates omitted in the string representation. The above
326 examples are equivalent to:
330 cube_union('(
0,
5,
2),(
2,
3,
1)','(
0,
0,
0),(
0,
0,
0)');
331 cube_inter('(
0,-
1),(
1,
1)','(-
2,
0),(
2,
0)');
335 The following containment predicate uses the point syntax,
336 while in fact the second argument is internally represented by a box.
337 This syntax makes it unnecessary to define a separate point type
338 and functions for (box,point) predicates.
342 select cube_contains('(
0,
0),(
1,
1)', '
0.5,
0.5');
354 For examples of usage, see the regression test
<filename>sql/cube.sql<
/>.
358 To make it harder for people to break things, there
359 is a limit of
100 on the number of dimensions of cubes. This is set
360 in
<filename>cubedata.h<
/> if you need something bigger.
365 <title>Credits
</title>
368 Original author: Gene Selkov, Jr.
<email>selkovjr@mcs.anl.gov
</email>,
369 Mathematics and Computer Science Division, Argonne National Laboratory.
373 My thanks are primarily to Prof. Joe Hellerstein
374 (
<ulink url=
"http://db.cs.berkeley.edu/~jmh/"></ulink>) for elucidating the
375 gist of the GiST (
<ulink url=
"http://gist.cs.berkeley.edu/"></ulink>), and
376 to his former student, Andy Dong (
<ulink
377 url=
"http://best.me.berkeley.edu/~adong/"></ulink>), for his example
378 written for Illustra,
379 <ulink url=
"http://garcia.me.berkeley.edu/~adong/rtree"></ulink>.
380 I am also grateful to all Postgres developers, present and past, for
381 enabling myself to create my own world and live undisturbed in it. And I
382 would like to acknowledge my gratitude to Argonne Lab and to the
383 U.S. Department of Energy for the years of faithful support of my database
388 Minor updates to this package were made by Bruno Wolff III
389 <email>bruno@wolff.to
</email> in August/September of
2002. These include
390 changing the precision from single precision to double precision and adding
395 Additional updates were made by Joshua Reich
<email>josh@root.net
</email> in
396 July
2006. These include
<literal>cube(float8[], float8[])
</literal> and
397 cleaning up the code to use the V1 call protocol instead of the deprecated