PostgreSQL Source Code git master
Loading...
Searching...
No Matches
cube.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/gist.h"
#include "access/stratnum.h"
#include "cubedata.h"
#include "libpq/pqformat.h"
#include "utils/array.h"
#include "utils/float.h"
Include dependency graph for cube.c:

Go to the source code of this file.

Macros

#define ARRPTR(x)   ( (double *) ARR_DATA_PTR(x) )
 
#define ARRNELEMS(x)   ArrayGetNItems( ARR_NDIM(x), ARR_DIMS(x))
 

Functions

 PG_MODULE_MAGIC_EXT (.name="cube",.version=PG_VERSION)
 
 PG_FUNCTION_INFO_V1 (cube_in)
 
 PG_FUNCTION_INFO_V1 (cube_a_f8_f8)
 
 PG_FUNCTION_INFO_V1 (cube_a_f8)
 
 PG_FUNCTION_INFO_V1 (cube_out)
 
 PG_FUNCTION_INFO_V1 (cube_send)
 
 PG_FUNCTION_INFO_V1 (cube_recv)
 
 PG_FUNCTION_INFO_V1 (cube_f8)
 
 PG_FUNCTION_INFO_V1 (cube_f8_f8)
 
 PG_FUNCTION_INFO_V1 (cube_c_f8)
 
 PG_FUNCTION_INFO_V1 (cube_c_f8_f8)
 
 PG_FUNCTION_INFO_V1 (cube_dim)
 
 PG_FUNCTION_INFO_V1 (cube_ll_coord)
 
 PG_FUNCTION_INFO_V1 (cube_ur_coord)
 
 PG_FUNCTION_INFO_V1 (cube_coord)
 
 PG_FUNCTION_INFO_V1 (cube_coord_llur)
 
 PG_FUNCTION_INFO_V1 (cube_subset)
 
 PG_FUNCTION_INFO_V1 (g_cube_consistent)
 
 PG_FUNCTION_INFO_V1 (g_cube_compress)
 
 PG_FUNCTION_INFO_V1 (g_cube_decompress)
 
 PG_FUNCTION_INFO_V1 (g_cube_penalty)
 
 PG_FUNCTION_INFO_V1 (g_cube_picksplit)
 
 PG_FUNCTION_INFO_V1 (g_cube_union)
 
 PG_FUNCTION_INFO_V1 (g_cube_same)
 
 PG_FUNCTION_INFO_V1 (g_cube_distance)
 
 PG_FUNCTION_INFO_V1 (cube_eq)
 
 PG_FUNCTION_INFO_V1 (cube_ne)
 
 PG_FUNCTION_INFO_V1 (cube_lt)
 
 PG_FUNCTION_INFO_V1 (cube_gt)
 
 PG_FUNCTION_INFO_V1 (cube_le)
 
 PG_FUNCTION_INFO_V1 (cube_ge)
 
 PG_FUNCTION_INFO_V1 (cube_cmp)
 
 PG_FUNCTION_INFO_V1 (cube_contains)
 
 PG_FUNCTION_INFO_V1 (cube_contained)
 
 PG_FUNCTION_INFO_V1 (cube_overlap)
 
 PG_FUNCTION_INFO_V1 (cube_union)
 
 PG_FUNCTION_INFO_V1 (cube_inter)
 
 PG_FUNCTION_INFO_V1 (cube_size)
 
 PG_FUNCTION_INFO_V1 (distance_taxicab)
 
 PG_FUNCTION_INFO_V1 (cube_distance)
 
 PG_FUNCTION_INFO_V1 (distance_chebyshev)
 
 PG_FUNCTION_INFO_V1 (cube_is_point)
 
 PG_FUNCTION_INFO_V1 (cube_enlarge)
 
int32 cube_cmp_v0 (NDBOX *a, NDBOX *b)
 
bool cube_contains_v0 (NDBOX *a, NDBOX *b)
 
bool cube_overlap_v0 (NDBOX *a, NDBOX *b)
 
NDBOXcube_union_v0 (NDBOX *a, NDBOX *b)
 
void rt_cube_size (NDBOX *a, double *size)
 
NDBOXg_cube_binary_union (NDBOX *r1, NDBOX *r2, int *sizep)
 
bool g_cube_leaf_consistent (NDBOX *key, NDBOX *query, StrategyNumber strategy)
 
bool g_cube_internal_consistent (NDBOX *key, NDBOX *query, StrategyNumber strategy)
 
static double distance_1D (double a1, double a2, double b1, double b2)
 
static bool cube_is_point_internal (NDBOX *cube)
 
Datum cube_in (PG_FUNCTION_ARGS)
 
Datum cube_a_f8_f8 (PG_FUNCTION_ARGS)
 
Datum cube_a_f8 (PG_FUNCTION_ARGS)
 
Datum cube_subset (PG_FUNCTION_ARGS)
 
Datum cube_out (PG_FUNCTION_ARGS)
 
Datum cube_send (PG_FUNCTION_ARGS)
 
Datum cube_recv (PG_FUNCTION_ARGS)
 
Datum g_cube_consistent (PG_FUNCTION_ARGS)
 
Datum g_cube_union (PG_FUNCTION_ARGS)
 
Datum g_cube_compress (PG_FUNCTION_ARGS)
 
Datum g_cube_decompress (PG_FUNCTION_ARGS)
 
Datum g_cube_penalty (PG_FUNCTION_ARGS)
 
Datum g_cube_picksplit (PG_FUNCTION_ARGS)
 
Datum g_cube_same (PG_FUNCTION_ARGS)
 
Datum cube_union (PG_FUNCTION_ARGS)
 
Datum cube_inter (PG_FUNCTION_ARGS)
 
Datum cube_size (PG_FUNCTION_ARGS)
 
Datum cube_cmp (PG_FUNCTION_ARGS)
 
Datum cube_eq (PG_FUNCTION_ARGS)
 
Datum cube_ne (PG_FUNCTION_ARGS)
 
Datum cube_lt (PG_FUNCTION_ARGS)
 
Datum cube_gt (PG_FUNCTION_ARGS)
 
Datum cube_le (PG_FUNCTION_ARGS)
 
Datum cube_ge (PG_FUNCTION_ARGS)
 
Datum cube_contains (PG_FUNCTION_ARGS)
 
Datum cube_contained (PG_FUNCTION_ARGS)
 
Datum cube_overlap (PG_FUNCTION_ARGS)
 
Datum cube_distance (PG_FUNCTION_ARGS)
 
Datum distance_taxicab (PG_FUNCTION_ARGS)
 
Datum distance_chebyshev (PG_FUNCTION_ARGS)
 
Datum g_cube_distance (PG_FUNCTION_ARGS)
 
Datum cube_is_point (PG_FUNCTION_ARGS)
 
Datum cube_dim (PG_FUNCTION_ARGS)
 
Datum cube_ll_coord (PG_FUNCTION_ARGS)
 
Datum cube_ur_coord (PG_FUNCTION_ARGS)
 
Datum cube_coord (PG_FUNCTION_ARGS)
 
Datum cube_coord_llur (PG_FUNCTION_ARGS)
 
Datum cube_enlarge (PG_FUNCTION_ARGS)
 
Datum cube_f8 (PG_FUNCTION_ARGS)
 
Datum cube_f8_f8 (PG_FUNCTION_ARGS)
 
Datum cube_c_f8 (PG_FUNCTION_ARGS)
 
Datum cube_c_f8_f8 (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ ARRNELEMS

#define ARRNELEMS (   x)    ArrayGetNItems( ARR_NDIM(x), ARR_DIMS(x))

Definition at line 29 of file cube.c.

◆ ARRPTR

#define ARRPTR (   x)    ( (double *) ARR_DATA_PTR(x) )

Definition at line 28 of file cube.c.

Function Documentation

◆ cube_a_f8()

Datum cube_a_f8 ( PG_FUNCTION_ARGS  )

Definition at line 210 of file cube.c.

211{
213 NDBOX *result;
214 int i;
215 int dim;
216 int size;
217 double *dur;
218
222 errmsg("cannot work with arrays containing NULLs")));
223
224 dim = ARRNELEMS(ur);
225 if (dim > CUBE_MAX_DIM)
228 errmsg("array is too long"),
229 errdetail("A cube cannot have more than %d dimensions.",
230 CUBE_MAX_DIM)));
231
232 dur = ARRPTR(ur);
233
234 size = POINT_SIZE(dim);
235 result = (NDBOX *) palloc0(size);
236 SET_VARSIZE(result, size);
237 SET_DIM(result, dim);
238 SET_POINT_BIT(result);
239
240 for (i = 0; i < dim; i++)
241 result->x[i] = dur[i];
242
243 PG_RETURN_NDBOX_P(result);
244}
#define PG_GETARG_ARRAYTYPE_P(n)
Definition array.h:263
bool array_contains_nulls(const ArrayType *array)
#define ARRNELEMS(x)
Definition cube.c:29
#define ARRPTR(x)
Definition cube.c:28
#define SET_DIM(cube, _dim)
Definition cubedata.h:43
#define POINT_SIZE(_dim)
Definition cubedata.h:48
#define CUBE_MAX_DIM
Definition cubedata.h:7
#define PG_RETURN_NDBOX_P(x)
Definition cubedata.h:54
#define SET_POINT_BIT(cube)
Definition cubedata.h:41
int errdetail(const char *fmt,...)
Definition elog.c:1216
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
int i
Definition isn.c:77
void * palloc0(Size size)
Definition mcxt.c:1417
static int fb(int x)
double x[FLEXIBLE_ARRAY_MEMBER]
Definition cubedata.h:33
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432

References array_contains_nulls(), ARRNELEMS, ARRPTR, CUBE_MAX_DIM, ereport, errcode(), errdetail(), errmsg(), ERROR, fb(), i, palloc0(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_NDBOX_P, POINT_SIZE, SET_DIM, SET_POINT_BIT, SET_VARSIZE(), and NDBOX::x.

◆ cube_a_f8_f8()

Datum cube_a_f8_f8 ( PG_FUNCTION_ARGS  )

Definition at line 143 of file cube.c.

144{
147 NDBOX *result;
148 int i;
149 int dim;
150 int size;
151 bool point;
152 double *dur,
153 *dll;
154
158 errmsg("cannot work with arrays containing NULLs")));
159
160 dim = ARRNELEMS(ur);
161 if (dim > CUBE_MAX_DIM)
164 errmsg("can't extend cube"),
165 errdetail("A cube cannot have more than %d dimensions.",
166 CUBE_MAX_DIM)));
167
168 if (ARRNELEMS(ll) != dim)
171 errmsg("UR and LL arrays must be of same length")));
172
173 dur = ARRPTR(ur);
174 dll = ARRPTR(ll);
175
176 /* Check if it's a point */
177 point = true;
178 for (i = 0; i < dim; i++)
179 {
180 if (dur[i] != dll[i])
181 {
182 point = false;
183 break;
184 }
185 }
186
187 size = point ? POINT_SIZE(dim) : CUBE_SIZE(dim);
188 result = (NDBOX *) palloc0(size);
189 SET_VARSIZE(result, size);
190 SET_DIM(result, dim);
191
192 for (i = 0; i < dim; i++)
193 result->x[i] = dur[i];
194
195 if (!point)
196 {
197 for (i = 0; i < dim; i++)
198 result->x[i + dim] = dll[i];
199 }
200 else
201 SET_POINT_BIT(result);
202
203 PG_RETURN_NDBOX_P(result);
204}
#define CUBE_SIZE(_dim)
Definition cubedata.h:49

References array_contains_nulls(), ARRNELEMS, ARRPTR, CUBE_MAX_DIM, CUBE_SIZE, ereport, errcode(), errdetail(), errmsg(), ERROR, fb(), i, palloc0(), PG_GETARG_ARRAYTYPE_P, PG_RETURN_NDBOX_P, POINT_SIZE, SET_DIM, SET_POINT_BIT, SET_VARSIZE(), and NDBOX::x.

◆ cube_c_f8()

Datum cube_c_f8 ( PG_FUNCTION_ARGS  )

Definition at line 1823 of file cube.c.

1824{
1826 double x = PG_GETARG_FLOAT8(1);
1827 NDBOX *result;
1828 int size;
1829 int i;
1830
1831 if (DIM(cube) + 1 > CUBE_MAX_DIM)
1832 ereport(ERROR,
1834 errmsg("can't extend cube"),
1835 errdetail("A cube cannot have more than %d dimensions.",
1836 CUBE_MAX_DIM)));
1837
1838 if (IS_POINT(cube))
1839 {
1840 size = POINT_SIZE((DIM(cube) + 1));
1841 result = (NDBOX *) palloc0(size);
1842 SET_VARSIZE(result, size);
1843 SET_DIM(result, DIM(cube) + 1);
1844 SET_POINT_BIT(result);
1845 for (i = 0; i < DIM(cube); i++)
1846 result->x[i] = cube->x[i];
1847 result->x[DIM(result) - 1] = x;
1848 }
1849 else
1850 {
1851 size = CUBE_SIZE((DIM(cube) + 1));
1852 result = (NDBOX *) palloc0(size);
1853 SET_VARSIZE(result, size);
1854 SET_DIM(result, DIM(cube) + 1);
1855 for (i = 0; i < DIM(cube); i++)
1856 {
1857 result->x[i] = cube->x[i];
1858 result->x[DIM(result) + i] = cube->x[DIM(cube) + i];
1859 }
1860 result->x[DIM(result) - 1] = x;
1861 result->x[2 * DIM(result) - 1] = x;
1862 }
1863
1865 PG_RETURN_NDBOX_P(result);
1866}
#define IS_POINT(cube)
Definition cubedata.h:40
#define DIM(cube)
Definition cubedata.h:42
#define PG_GETARG_NDBOX_P(x)
Definition cubedata.h:53
#define PG_FREE_IF_COPY(ptr, n)
Definition fmgr.h:260
#define PG_GETARG_FLOAT8(n)
Definition fmgr.h:283
int x
Definition isn.c:75

References CUBE_MAX_DIM, CUBE_SIZE, DIM, ereport, errcode(), errdetail(), errmsg(), ERROR, fb(), i, IS_POINT, palloc0(), PG_FREE_IF_COPY, PG_GETARG_FLOAT8, PG_GETARG_NDBOX_P, PG_RETURN_NDBOX_P, POINT_SIZE, SET_DIM, SET_POINT_BIT, SET_VARSIZE(), NDBOX::x, and x.

◆ cube_c_f8_f8()

Datum cube_c_f8_f8 ( PG_FUNCTION_ARGS  )

Definition at line 1870 of file cube.c.

1871{
1873 double x1 = PG_GETARG_FLOAT8(1);
1874 double x2 = PG_GETARG_FLOAT8(2);
1875 NDBOX *result;
1876 int size;
1877 int i;
1878
1879 if (DIM(cube) + 1 > CUBE_MAX_DIM)
1880 ereport(ERROR,
1882 errmsg("can't extend cube"),
1883 errdetail("A cube cannot have more than %d dimensions.",
1884 CUBE_MAX_DIM)));
1885
1886 if (IS_POINT(cube) && (x1 == x2))
1887 {
1888 size = POINT_SIZE((DIM(cube) + 1));
1889 result = (NDBOX *) palloc0(size);
1890 SET_VARSIZE(result, size);
1891 SET_DIM(result, DIM(cube) + 1);
1892 SET_POINT_BIT(result);
1893 for (i = 0; i < DIM(cube); i++)
1894 result->x[i] = cube->x[i];
1895 result->x[DIM(result) - 1] = x1;
1896 }
1897 else
1898 {
1899 size = CUBE_SIZE((DIM(cube) + 1));
1900 result = (NDBOX *) palloc0(size);
1901 SET_VARSIZE(result, size);
1902 SET_DIM(result, DIM(cube) + 1);
1903 for (i = 0; i < DIM(cube); i++)
1904 {
1905 result->x[i] = LL_COORD(cube, i);
1906 result->x[DIM(result) + i] = UR_COORD(cube, i);
1907 }
1908 result->x[DIM(result) - 1] = x1;
1909 result->x[2 * DIM(result) - 1] = x2;
1910 }
1911
1913 PG_RETURN_NDBOX_P(result);
1914}
#define LL_COORD(cube, i)
Definition cubedata.h:45
#define UR_COORD(cube, i)
Definition cubedata.h:46

References CUBE_MAX_DIM, CUBE_SIZE, DIM, ereport, errcode(), errdetail(), errmsg(), ERROR, fb(), i, IS_POINT, LL_COORD, palloc0(), PG_FREE_IF_COPY, PG_GETARG_FLOAT8, PG_GETARG_NDBOX_P, PG_RETURN_NDBOX_P, POINT_SIZE, SET_DIM, SET_POINT_BIT, SET_VARSIZE(), UR_COORD, and NDBOX::x.

◆ cube_cmp()

Datum cube_cmp ( PG_FUNCTION_ARGS  )

Definition at line 1022 of file cube.c.

1023{
1025 *b = PG_GETARG_NDBOX_P(1);
1026 int32 res;
1027
1028 res = cube_cmp_v0(a, b);
1029
1030 PG_FREE_IF_COPY(a, 0);
1031 PG_FREE_IF_COPY(b, 1);
1032 PG_RETURN_INT32(res);
1033}
int32_t int32
Definition c.h:542
int32 cube_cmp_v0(NDBOX *a, NDBOX *b)
Definition cube.c:942
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
int b
Definition isn.c:74
int a
Definition isn.c:73

References a, b, cube_cmp_v0(), PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, and PG_RETURN_INT32.

◆ cube_cmp_v0()

int32 cube_cmp_v0 ( NDBOX a,
NDBOX b 
)

Definition at line 942 of file cube.c.

943{
944 int i;
945 int dim;
946
947 dim = Min(DIM(a), DIM(b));
948
949 /* compare the common dimensions */
950 for (i = 0; i < dim; i++)
951 {
952 if (Min(LL_COORD(a, i), UR_COORD(a, i)) >
953 Min(LL_COORD(b, i), UR_COORD(b, i)))
954 return 1;
955 if (Min(LL_COORD(a, i), UR_COORD(a, i)) <
956 Min(LL_COORD(b, i), UR_COORD(b, i)))
957 return -1;
958 }
959 for (i = 0; i < dim; i++)
960 {
961 if (Max(LL_COORD(a, i), UR_COORD(a, i)) >
962 Max(LL_COORD(b, i), UR_COORD(b, i)))
963 return 1;
964 if (Max(LL_COORD(a, i), UR_COORD(a, i)) <
965 Max(LL_COORD(b, i), UR_COORD(b, i)))
966 return -1;
967 }
968
969 /* compare extra dimensions to zero */
970 if (DIM(a) > DIM(b))
971 {
972 for (i = dim; i < DIM(a); i++)
973 {
974 if (Min(LL_COORD(a, i), UR_COORD(a, i)) > 0)
975 return 1;
976 if (Min(LL_COORD(a, i), UR_COORD(a, i)) < 0)
977 return -1;
978 }
979 for (i = dim; i < DIM(a); i++)
980 {
981 if (Max(LL_COORD(a, i), UR_COORD(a, i)) > 0)
982 return 1;
983 if (Max(LL_COORD(a, i), UR_COORD(a, i)) < 0)
984 return -1;
985 }
986
987 /*
988 * if all common dimensions are equal, the cube with more dimensions
989 * wins
990 */
991 return 1;
992 }
993 if (DIM(a) < DIM(b))
994 {
995 for (i = dim; i < DIM(b); i++)
996 {
997 if (Min(LL_COORD(b, i), UR_COORD(b, i)) > 0)
998 return -1;
999 if (Min(LL_COORD(b, i), UR_COORD(b, i)) < 0)
1000 return 1;
1001 }
1002 for (i = dim; i < DIM(b); i++)
1003 {
1004 if (Max(LL_COORD(b, i), UR_COORD(b, i)) > 0)
1005 return -1;
1006 if (Max(LL_COORD(b, i), UR_COORD(b, i)) < 0)
1007 return 1;
1008 }
1009
1010 /*
1011 * if all common dimensions are equal, the cube with more dimensions
1012 * wins
1013 */
1014 return -1;
1015 }
1016
1017 /* They're really equal */
1018 return 0;
1019}
#define Min(x, y)
Definition c.h:997
#define Max(x, y)
Definition c.h:991

References a, b, DIM, i, LL_COORD, Max, Min, and UR_COORD.

Referenced by cube_cmp(), cube_eq(), cube_ge(), cube_gt(), cube_le(), cube_lt(), cube_ne(), g_cube_leaf_consistent(), and g_cube_same().

◆ cube_contained()

Datum cube_contained ( PG_FUNCTION_ARGS  )

Definition at line 1183 of file cube.c.

1184{
1186 *b = PG_GETARG_NDBOX_P(1);
1187 bool res;
1188
1189 res = cube_contains_v0(b, a);
1190
1191 PG_FREE_IF_COPY(a, 0);
1192 PG_FREE_IF_COPY(b, 1);
1193 PG_RETURN_BOOL(res);
1194}
bool cube_contains_v0(NDBOX *a, NDBOX *b)
Definition cube.c:1129
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360

References a, b, cube_contains_v0(), PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, and PG_RETURN_BOOL.

◆ cube_contains()

Datum cube_contains ( PG_FUNCTION_ARGS  )

Definition at line 1167 of file cube.c.

1168{
1170 *b = PG_GETARG_NDBOX_P(1);
1171 bool res;
1172
1173 res = cube_contains_v0(a, b);
1174
1175 PG_FREE_IF_COPY(a, 0);
1176 PG_FREE_IF_COPY(b, 1);
1177 PG_RETURN_BOOL(res);
1178}

References a, b, cube_contains_v0(), PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, and PG_RETURN_BOOL.

◆ cube_contains_v0()

bool cube_contains_v0 ( NDBOX a,
NDBOX b 
)

Definition at line 1129 of file cube.c.

1130{
1131 int i;
1132
1133 if ((a == NULL) || (b == NULL))
1134 return false;
1135
1136 if (DIM(a) < DIM(b))
1137 {
1138 /*
1139 * the further comparisons will make sense if the excess dimensions of
1140 * (b) were zeroes Since both UL and UR coordinates must be zero, we
1141 * can check them all without worrying about which is which.
1142 */
1143 for (i = DIM(a); i < DIM(b); i++)
1144 {
1145 if (LL_COORD(b, i) != 0)
1146 return false;
1147 if (UR_COORD(b, i) != 0)
1148 return false;
1149 }
1150 }
1151
1152 /* Can't care less about the excess dimensions of (a), if any */
1153 for (i = 0; i < Min(DIM(a), DIM(b)); i++)
1154 {
1155 if (Min(LL_COORD(a, i), UR_COORD(a, i)) >
1156 Min(LL_COORD(b, i), UR_COORD(b, i)))
1157 return false;
1158 if (Max(LL_COORD(a, i), UR_COORD(a, i)) <
1159 Max(LL_COORD(b, i), UR_COORD(b, i)))
1160 return false;
1161 }
1162
1163 return true;
1164}

References a, b, DIM, fb(), i, LL_COORD, Max, Min, and UR_COORD.

Referenced by cube_contained(), cube_contains(), g_cube_internal_consistent(), and g_cube_leaf_consistent().

◆ cube_coord()

Datum cube_coord ( PG_FUNCTION_ARGS  )

Definition at line 1608 of file cube.c.

1609{
1611 int coord = PG_GETARG_INT32(1);
1612
1614 ereport(ERROR,
1616 errmsg("cube index %d is out of bounds", coord)));
1617
1618 if (IS_POINT(cube))
1619 PG_RETURN_FLOAT8(cube->x[(coord - 1) % DIM(cube)]);
1620 else
1621 PG_RETURN_FLOAT8(cube->x[coord - 1]);
1622}
#define PG_RETURN_FLOAT8(x)
Definition fmgr.h:369
#define PG_GETARG_INT32(n)
Definition fmgr.h:269

References DIM, ereport, errcode(), errmsg(), ERROR, fb(), IS_POINT, PG_GETARG_INT32, PG_GETARG_NDBOX_P, and PG_RETURN_FLOAT8.

◆ cube_coord_llur()

Datum cube_coord_llur ( PG_FUNCTION_ARGS  )

Definition at line 1649 of file cube.c.

1650{
1652 int coord = PG_GETARG_INT32(1);
1653 bool inverse = false;
1654 float8 result;
1655
1656 /* 0 is the only unsupported coordinate value */
1657 if (coord == 0)
1658 ereport(ERROR,
1660 errmsg("zero cube index is not defined")));
1661
1662 /* Return inversed value for negative coordinate */
1663 if (coord < 0)
1664 {
1665 coord = -coord;
1666 inverse = true;
1667 }
1668
1669 if (coord <= 2 * DIM(cube))
1670 {
1671 /* dimension index */
1672 int index = (coord - 1) / 2;
1673
1674 /* whether this is upper bound (lower bound otherwise) */
1675 bool upper = ((coord - 1) % 2 == 1);
1676
1677 if (IS_POINT(cube))
1678 {
1679 result = cube->x[index];
1680 }
1681 else
1682 {
1683 if (upper)
1684 result = Max(cube->x[index], cube->x[index + DIM(cube)]);
1685 else
1686 result = Min(cube->x[index], cube->x[index + DIM(cube)]);
1687 }
1688 }
1689 else
1690 {
1691 /*
1692 * Return zero if coordinate is out of bound. That reproduces logic
1693 * of how cubes with low dimension number are expanded during GiST
1694 * indexing.
1695 */
1696 result = 0.0;
1697 }
1698
1699 /* Inverse value if needed */
1700 if (inverse)
1701 result = -result;
1702
1703 PG_RETURN_FLOAT8(result);
1704}
double float8
Definition c.h:644
Datum upper(PG_FUNCTION_ARGS)
Definition type.h:96

References DIM, ereport, errcode(), errmsg(), ERROR, fb(), IS_POINT, Max, Min, PG_GETARG_INT32, PG_GETARG_NDBOX_P, PG_RETURN_FLOAT8, and upper().

◆ cube_dim()

Datum cube_dim ( PG_FUNCTION_ARGS  )

Definition at line 1559 of file cube.c.

1560{
1562 int dim = DIM(c);
1563
1564 PG_FREE_IF_COPY(c, 0);
1565 PG_RETURN_INT32(dim);
1566}
char * c

References DIM, PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, and PG_RETURN_INT32.

◆ cube_distance()

Datum cube_distance ( PG_FUNCTION_ARGS  )

Definition at line 1258 of file cube.c.

1259{
1261 *b = PG_GETARG_NDBOX_P(1);
1262 bool swapped = false;
1263 double d,
1264 distance;
1265 int i;
1266
1267 /* swap the box pointers if needed */
1268 if (DIM(a) < DIM(b))
1269 {
1270 NDBOX *tmp = b;
1271
1272 b = a;
1273 a = tmp;
1274 swapped = true;
1275 }
1276
1277 distance = 0.0;
1278 /* compute within the dimensions of (b) */
1279 for (i = 0; i < DIM(b); i++)
1280 {
1281 d = distance_1D(LL_COORD(a, i), UR_COORD(a, i), LL_COORD(b, i), UR_COORD(b, i));
1282 distance += d * d;
1283 }
1284
1285 /* compute distance to zero for those dimensions in (a) absent in (b) */
1286 for (i = DIM(b); i < DIM(a); i++)
1287 {
1288 d = distance_1D(LL_COORD(a, i), UR_COORD(a, i), 0.0, 0.0);
1289 distance += d * d;
1290 }
1291
1292 if (swapped)
1293 {
1294 PG_FREE_IF_COPY(b, 0);
1295 PG_FREE_IF_COPY(a, 1);
1296 }
1297 else
1298 {
1299 PG_FREE_IF_COPY(a, 0);
1300 PG_FREE_IF_COPY(b, 1);
1301 }
1302
1303 PG_RETURN_FLOAT8(sqrt(distance));
1304}
static double distance_1D(double a1, double a2, double b1, double b2)
Definition cube.c:1508

References a, b, DIM, distance_1D(), fb(), i, LL_COORD, PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, PG_RETURN_FLOAT8, and UR_COORD.

Referenced by g_cube_distance().

◆ cube_enlarge()

Datum cube_enlarge ( PG_FUNCTION_ARGS  )

Definition at line 1708 of file cube.c.

1709{
1711 double r = PG_GETARG_FLOAT8(1);
1712 int32 n = PG_GETARG_INT32(2);
1713 NDBOX *result;
1714 int dim = 0;
1715 int size;
1716 int i,
1717 j;
1718
1719 if (n > CUBE_MAX_DIM)
1720 n = CUBE_MAX_DIM;
1721 if (r > 0 && n > 0)
1722 dim = n;
1723 if (DIM(a) > dim)
1724 dim = DIM(a);
1725
1726 size = CUBE_SIZE(dim);
1727 result = (NDBOX *) palloc0(size);
1728 SET_VARSIZE(result, size);
1729 SET_DIM(result, dim);
1730
1731 for (i = 0, j = dim; i < DIM(a); i++, j++)
1732 {
1733 if (LL_COORD(a, i) >= UR_COORD(a, i))
1734 {
1735 result->x[i] = UR_COORD(a, i) - r;
1736 result->x[j] = LL_COORD(a, i) + r;
1737 }
1738 else
1739 {
1740 result->x[i] = LL_COORD(a, i) - r;
1741 result->x[j] = UR_COORD(a, i) + r;
1742 }
1743 if (result->x[i] > result->x[j])
1744 {
1745 result->x[i] = (result->x[i] + result->x[j]) / 2;
1746 result->x[j] = result->x[i];
1747 }
1748 }
1749 /* dim > a->dim only if r > 0 */
1750 for (; i < dim; i++, j++)
1751 {
1752 result->x[i] = -r;
1753 result->x[j] = r;
1754 }
1755
1756 /*
1757 * Check if the result was in fact a point, and set the flag in the datum
1758 * accordingly. (we don't bother to repalloc it smaller)
1759 */
1760 if (cube_is_point_internal(result))
1761 {
1762 size = POINT_SIZE(dim);
1763 SET_VARSIZE(result, size);
1764 SET_POINT_BIT(result);
1765 }
1766
1767 PG_FREE_IF_COPY(a, 0);
1768 PG_RETURN_NDBOX_P(result);
1769}
static bool cube_is_point_internal(NDBOX *cube)
Definition cube.c:1535
int j
Definition isn.c:78

References a, cube_is_point_internal(), CUBE_MAX_DIM, CUBE_SIZE, DIM, i, j, LL_COORD, palloc0(), PG_FREE_IF_COPY, PG_GETARG_FLOAT8, PG_GETARG_INT32, PG_GETARG_NDBOX_P, PG_RETURN_NDBOX_P, POINT_SIZE, SET_DIM, SET_POINT_BIT, SET_VARSIZE(), UR_COORD, and NDBOX::x.

◆ cube_eq()

Datum cube_eq ( PG_FUNCTION_ARGS  )

Definition at line 1037 of file cube.c.

1038{
1040 *b = PG_GETARG_NDBOX_P(1);
1041 int32 res;
1042
1043 res = cube_cmp_v0(a, b);
1044
1045 PG_FREE_IF_COPY(a, 0);
1046 PG_FREE_IF_COPY(b, 1);
1047 PG_RETURN_BOOL(res == 0);
1048}

References a, b, cube_cmp_v0(), PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, and PG_RETURN_BOOL.

◆ cube_f8()

Datum cube_f8 ( PG_FUNCTION_ARGS  )

Definition at line 1773 of file cube.c.

1774{
1775 double x = PG_GETARG_FLOAT8(0);
1776 NDBOX *result;
1777 int size;
1778
1779 size = POINT_SIZE(1);
1780 result = (NDBOX *) palloc0(size);
1781 SET_VARSIZE(result, size);
1782 SET_DIM(result, 1);
1783 SET_POINT_BIT(result);
1784 result->x[0] = x;
1785
1786 PG_RETURN_NDBOX_P(result);
1787}

References palloc0(), PG_GETARG_FLOAT8, PG_RETURN_NDBOX_P, POINT_SIZE, SET_DIM, SET_POINT_BIT, SET_VARSIZE(), NDBOX::x, and x.

◆ cube_f8_f8()

Datum cube_f8_f8 ( PG_FUNCTION_ARGS  )

Definition at line 1791 of file cube.c.

1792{
1793 double x0 = PG_GETARG_FLOAT8(0);
1794 double x1 = PG_GETARG_FLOAT8(1);
1795 NDBOX *result;
1796 int size;
1797
1798 if (x0 == x1)
1799 {
1800 size = POINT_SIZE(1);
1801 result = (NDBOX *) palloc0(size);
1802 SET_VARSIZE(result, size);
1803 SET_DIM(result, 1);
1804 SET_POINT_BIT(result);
1805 result->x[0] = x0;
1806 }
1807 else
1808 {
1809 size = CUBE_SIZE(1);
1810 result = (NDBOX *) palloc0(size);
1811 SET_VARSIZE(result, size);
1812 SET_DIM(result, 1);
1813 result->x[0] = x0;
1814 result->x[1] = x1;
1815 }
1816
1817 PG_RETURN_NDBOX_P(result);
1818}

References CUBE_SIZE, fb(), palloc0(), PG_GETARG_FLOAT8, PG_RETURN_NDBOX_P, POINT_SIZE, SET_DIM, SET_POINT_BIT, SET_VARSIZE(), and NDBOX::x.

◆ cube_ge()

Datum cube_ge ( PG_FUNCTION_ARGS  )

Definition at line 1112 of file cube.c.

1113{
1115 *b = PG_GETARG_NDBOX_P(1);
1116 int32 res;
1117
1118 res = cube_cmp_v0(a, b);
1119
1120 PG_FREE_IF_COPY(a, 0);
1121 PG_FREE_IF_COPY(b, 1);
1122 PG_RETURN_BOOL(res >= 0);
1123}

References a, b, cube_cmp_v0(), PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, and PG_RETURN_BOOL.

◆ cube_gt()

Datum cube_gt ( PG_FUNCTION_ARGS  )

Definition at line 1082 of file cube.c.

1083{
1085 *b = PG_GETARG_NDBOX_P(1);
1086 int32 res;
1087
1088 res = cube_cmp_v0(a, b);
1089
1090 PG_FREE_IF_COPY(a, 0);
1091 PG_FREE_IF_COPY(b, 1);
1092 PG_RETURN_BOOL(res > 0);
1093}

References a, b, cube_cmp_v0(), PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, and PG_RETURN_BOOL.

◆ cube_in()

Datum cube_in ( PG_FUNCTION_ARGS  )

Definition at line 121 of file cube.c.

122{
123 char *str = PG_GETARG_CSTRING(0);
124 NDBOX *result;
125 Size scanbuflen;
126 yyscan_t scanner;
127
128 cube_scanner_init(str, &scanbuflen, &scanner);
129
130 cube_yyparse(&result, scanbuflen, fcinfo->context, scanner);
131
132 /* We might as well run this even on failure. */
133 cube_scanner_finish(scanner);
134
135 PG_RETURN_NDBOX_P(result);
136}
size_t Size
Definition c.h:619
int cube_yyparse(NDBOX **result, Size scanbuflen, struct Node *escontext, yyscan_t yyscanner)
void cube_scanner_init(const char *str, Size *scanbuflen, yyscan_t *yyscannerp)
Definition cubescan.l:102
void * yyscan_t
Definition cubedata.h:65
void cube_scanner_finish(yyscan_t yyscanner)
Definition cubescan.l:121
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
const char * str

References cube_scanner_finish(), cube_scanner_init(), cube_yyparse(), PG_GETARG_CSTRING, PG_RETURN_NDBOX_P, and str.

◆ cube_inter()

Datum cube_inter ( PG_FUNCTION_ARGS  )

Definition at line 828 of file cube.c.

829{
832 NDBOX *result;
833 bool swapped = false;
834 int i;
835 int dim;
836 int size;
837
838 /* swap the arguments if needed, so that 'a' is always larger than 'b' */
839 if (DIM(a) < DIM(b))
840 {
841 NDBOX *tmp = b;
842
843 b = a;
844 a = tmp;
845 swapped = true;
846 }
847 dim = DIM(a);
848
849 size = CUBE_SIZE(dim);
850 result = (NDBOX *) palloc0(size);
851 SET_VARSIZE(result, size);
852 SET_DIM(result, dim);
853
854 /* First compute intersection of the dimensions present in both args */
855 for (i = 0; i < DIM(b); i++)
856 {
857 result->x[i] = Max(Min(LL_COORD(a, i), UR_COORD(a, i)),
858 Min(LL_COORD(b, i), UR_COORD(b, i)));
859 result->x[i + DIM(a)] = Min(Max(LL_COORD(a, i), UR_COORD(a, i)),
860 Max(LL_COORD(b, i), UR_COORD(b, i)));
861 }
862 /* continue on the higher dimensions only present in 'a' */
863 for (; i < DIM(a); i++)
864 {
865 result->x[i] = Max(0,
866 Min(LL_COORD(a, i), UR_COORD(a, i))
867 );
868 result->x[i + DIM(a)] = Min(0,
869 Max(LL_COORD(a, i), UR_COORD(a, i))
870 );
871 }
872
873 /*
874 * Check if the result was in fact a point, and set the flag in the datum
875 * accordingly. (we don't bother to repalloc it smaller)
876 */
877 if (cube_is_point_internal(result))
878 {
879 size = POINT_SIZE(dim);
880 result = repalloc(result, size);
881 SET_VARSIZE(result, size);
882 SET_POINT_BIT(result);
883 }
884
885 if (swapped)
886 {
887 PG_FREE_IF_COPY(b, 0);
888 PG_FREE_IF_COPY(a, 1);
889 }
890 else
891 {
892 PG_FREE_IF_COPY(a, 0);
893 PG_FREE_IF_COPY(b, 1);
894 }
895
896 /*
897 * Is it OK to return a non-null intersection for non-overlapping boxes?
898 */
899 PG_RETURN_NDBOX_P(result);
900}
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1632

References a, b, cube_is_point_internal(), CUBE_SIZE, DIM, fb(), i, LL_COORD, Max, Min, palloc0(), PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, PG_RETURN_NDBOX_P, POINT_SIZE, repalloc(), SET_DIM, SET_POINT_BIT, SET_VARSIZE(), UR_COORD, and NDBOX::x.

Referenced by g_cube_picksplit().

◆ cube_is_point()

Datum cube_is_point ( PG_FUNCTION_ARGS  )

Definition at line 1524 of file cube.c.

1525{
1527 bool result;
1528
1529 result = cube_is_point_internal(cube);
1531 PG_RETURN_BOOL(result);
1532}

References cube_is_point_internal(), fb(), PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, and PG_RETURN_BOOL.

◆ cube_is_point_internal()

static bool cube_is_point_internal ( NDBOX cube)
static

Definition at line 1535 of file cube.c.

1536{
1537 int i;
1538
1539 if (IS_POINT(cube))
1540 return true;
1541
1542 /*
1543 * Even if the point-flag is not set, all the lower-left coordinates might
1544 * match the upper-right coordinates, so that the value is in fact a
1545 * point. Such values don't arise with current code - the point flag is
1546 * always set if appropriate - but they might be present on-disk in
1547 * clusters upgraded from pre-9.4 versions.
1548 */
1549 for (i = 0; i < DIM(cube); i++)
1550 {
1551 if (LL_COORD(cube, i) != UR_COORD(cube, i))
1552 return false;
1553 }
1554 return true;
1555}

References DIM, fb(), i, IS_POINT, LL_COORD, and UR_COORD.

Referenced by cube_enlarge(), cube_inter(), cube_is_point(), cube_out(), and cube_union_v0().

◆ cube_le()

Datum cube_le ( PG_FUNCTION_ARGS  )

Definition at line 1097 of file cube.c.

1098{
1100 *b = PG_GETARG_NDBOX_P(1);
1101 int32 res;
1102
1103 res = cube_cmp_v0(a, b);
1104
1105 PG_FREE_IF_COPY(a, 0);
1106 PG_FREE_IF_COPY(b, 1);
1107 PG_RETURN_BOOL(res <= 0);
1108}

References a, b, cube_cmp_v0(), PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, and PG_RETURN_BOOL.

◆ cube_ll_coord()

Datum cube_ll_coord ( PG_FUNCTION_ARGS  )

Definition at line 1570 of file cube.c.

1571{
1573 int n = PG_GETARG_INT32(1);
1574 double result;
1575
1576 if (DIM(c) >= n && n > 0)
1577 result = Min(LL_COORD(c, n - 1), UR_COORD(c, n - 1));
1578 else
1579 result = 0;
1580
1581 PG_FREE_IF_COPY(c, 0);
1582 PG_RETURN_FLOAT8(result);
1583}

References DIM, LL_COORD, Min, PG_FREE_IF_COPY, PG_GETARG_INT32, PG_GETARG_NDBOX_P, PG_RETURN_FLOAT8, and UR_COORD.

◆ cube_lt()

Datum cube_lt ( PG_FUNCTION_ARGS  )

Definition at line 1067 of file cube.c.

1068{
1070 *b = PG_GETARG_NDBOX_P(1);
1071 int32 res;
1072
1073 res = cube_cmp_v0(a, b);
1074
1075 PG_FREE_IF_COPY(a, 0);
1076 PG_FREE_IF_COPY(b, 1);
1077 PG_RETURN_BOOL(res < 0);
1078}

References a, b, cube_cmp_v0(), PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, and PG_RETURN_BOOL.

◆ cube_ne()

Datum cube_ne ( PG_FUNCTION_ARGS  )

Definition at line 1052 of file cube.c.

1053{
1055 *b = PG_GETARG_NDBOX_P(1);
1056 int32 res;
1057
1058 res = cube_cmp_v0(a, b);
1059
1060 PG_FREE_IF_COPY(a, 0);
1061 PG_FREE_IF_COPY(b, 1);
1062 PG_RETURN_BOOL(res != 0);
1063}

References a, b, cube_cmp_v0(), PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, and PG_RETURN_BOOL.

◆ cube_out()

Datum cube_out ( PG_FUNCTION_ARGS  )

Definition at line 296 of file cube.c.

297{
300 int dim = DIM(cube);
301 int i;
302
304
306 for (i = 0; i < dim; i++)
307 {
308 if (i > 0)
311 }
313
315 {
317 for (i = 0; i < dim; i++)
318 {
319 if (i > 0)
322 }
324 }
325
328}
char * float8out_internal(double num)
Definition float.c:537
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364
static char buf[DEFAULT_XLOG_SEG_SIZE]
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230
void appendStringInfoChar(StringInfo str, char ch)
Definition stringinfo.c:242
void initStringInfo(StringInfo str)
Definition stringinfo.c:97

References appendStringInfoChar(), appendStringInfoString(), buf, cube_is_point_internal(), DIM, fb(), float8out_internal(), i, initStringInfo(), LL_COORD, PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, PG_RETURN_CSTRING, and UR_COORD.

◆ cube_overlap()

Datum cube_overlap ( PG_FUNCTION_ARGS  )

Definition at line 1238 of file cube.c.

1239{
1241 *b = PG_GETARG_NDBOX_P(1);
1242 bool res;
1243
1244 res = cube_overlap_v0(a, b);
1245
1246 PG_FREE_IF_COPY(a, 0);
1247 PG_FREE_IF_COPY(b, 1);
1248 PG_RETURN_BOOL(res);
1249}
bool cube_overlap_v0(NDBOX *a, NDBOX *b)
Definition cube.c:1199

References a, b, cube_overlap_v0(), PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, and PG_RETURN_BOOL.

◆ cube_overlap_v0()

bool cube_overlap_v0 ( NDBOX a,
NDBOX b 
)

Definition at line 1199 of file cube.c.

1200{
1201 int i;
1202
1203 if ((a == NULL) || (b == NULL))
1204 return false;
1205
1206 /* swap the box pointers if needed */
1207 if (DIM(a) < DIM(b))
1208 {
1209 NDBOX *tmp = b;
1210
1211 b = a;
1212 a = tmp;
1213 }
1214
1215 /* compare within the dimensions of (b) */
1216 for (i = 0; i < DIM(b); i++)
1217 {
1218 if (Min(LL_COORD(a, i), UR_COORD(a, i)) > Max(LL_COORD(b, i), UR_COORD(b, i)))
1219 return false;
1220 if (Max(LL_COORD(a, i), UR_COORD(a, i)) < Min(LL_COORD(b, i), UR_COORD(b, i)))
1221 return false;
1222 }
1223
1224 /* compare to zero those dimensions in (a) absent in (b) */
1225 for (i = DIM(b); i < DIM(a); i++)
1226 {
1227 if (Min(LL_COORD(a, i), UR_COORD(a, i)) > 0)
1228 return false;
1229 if (Max(LL_COORD(a, i), UR_COORD(a, i)) < 0)
1230 return false;
1231 }
1232
1233 return true;
1234}

References a, b, DIM, fb(), i, LL_COORD, Max, Min, and UR_COORD.

Referenced by cube_overlap(), g_cube_internal_consistent(), and g_cube_leaf_consistent().

◆ cube_recv()

Datum cube_recv ( PG_FUNCTION_ARGS  )

Definition at line 356 of file cube.c.

357{
359 int32 header;
360 int32 i,
361 nitems;
362 NDBOX *cube;
363
364 header = pq_getmsgint(buf, sizeof(int32));
365 nitems = (header & DIM_MASK);
366 if (nitems > CUBE_MAX_DIM)
369 errmsg("cube dimension is too large"),
370 errdetail("A cube cannot have more than %d dimensions.",
371 CUBE_MAX_DIM)));
372 if ((header & POINT_BIT) == 0)
373 nitems += nitems;
374 cube = palloc(offsetof(NDBOX, x) + sizeof(double) * nitems);
375 SET_VARSIZE(cube, offsetof(NDBOX, x) + sizeof(double) * nitems);
376 cube->header = header;
377 for (i = 0; i < nitems; i++)
378 cube->x[i] = pq_getmsgfloat8(buf);
379
381}
#define DIM_MASK
Definition cubedata.h:38
#define POINT_BIT
Definition cubedata.h:37
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define nitems(x)
Definition indent.h:31
void * palloc(Size size)
Definition mcxt.c:1387
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition pqformat.c:414
float8 pq_getmsgfloat8(StringInfo msg)
Definition pqformat.c:487
struct StringInfoData * StringInfo
Definition string.h:15

References buf, CUBE_MAX_DIM, DIM_MASK, ereport, errcode(), errdetail(), errmsg(), ERROR, fb(), i, nitems, palloc(), PG_GETARG_POINTER, PG_RETURN_NDBOX_P, POINT_BIT, pq_getmsgfloat8(), pq_getmsgint(), SET_VARSIZE(), and x.

◆ cube_send()

Datum cube_send ( PG_FUNCTION_ARGS  )

Definition at line 334 of file cube.c.

335{
338 int32 i,
339 nitems = DIM(cube);
340
342 pq_sendint32(&buf, cube->header);
343 if (!IS_POINT(cube))
344 nitems += nitems;
345 /* for symmetry with cube_recv, we don't use LL_COORD/UR_COORD here */
346 for (i = 0; i < nitems; i++)
347 pq_sendfloat8(&buf, cube->x[i]);
348
350}
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
void pq_begintypsend(StringInfo buf)
Definition pqformat.c:325
bytea * pq_endtypsend(StringInfo buf)
Definition pqformat.c:345
void pq_sendfloat8(StringInfo buf, float8 f)
Definition pqformat.c:276
static void pq_sendint32(StringInfo buf, uint32 i)
Definition pqformat.h:144

References buf, DIM, fb(), i, IS_POINT, nitems, PG_GETARG_NDBOX_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendfloat8(), and pq_sendint32().

◆ cube_size()

Datum cube_size ( PG_FUNCTION_ARGS  )

Definition at line 904 of file cube.c.

905{
907 double result;
908
909 rt_cube_size(a, &result);
910 PG_FREE_IF_COPY(a, 0);
911 PG_RETURN_FLOAT8(result);
912}
void rt_cube_size(NDBOX *a, double *size)
Definition cube.c:915

References a, PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, PG_RETURN_FLOAT8, and rt_cube_size().

◆ cube_subset()

Datum cube_subset ( PG_FUNCTION_ARGS  )

Definition at line 247 of file cube.c.

248{
251 NDBOX *result;
252 int size,
253 dim,
254 i;
255 int *dx;
256
260 errmsg("cannot work with arrays containing NULLs")));
261
262 dx = (int32 *) ARR_DATA_PTR(idx);
263
264 dim = ARRNELEMS(idx);
265 if (dim > CUBE_MAX_DIM)
268 errmsg("array is too long"),
269 errdetail("A cube cannot have more than %d dimensions.",
270 CUBE_MAX_DIM)));
271
272 size = IS_POINT(c) ? POINT_SIZE(dim) : CUBE_SIZE(dim);
273 result = (NDBOX *) palloc0(size);
274 SET_VARSIZE(result, size);
275 SET_DIM(result, dim);
276
277 if (IS_POINT(c))
278 SET_POINT_BIT(result);
279
280 for (i = 0; i < dim; i++)
281 {
282 if ((dx[i] <= 0) || (dx[i] > DIM(c)))
285 errmsg("Index out of bounds")));
286 result->x[i] = c->x[dx[i] - 1];
287 if (!IS_POINT(c))
288 result->x[i + dim] = c->x[dx[i] + DIM(c) - 1];
289 }
290
291 PG_FREE_IF_COPY(c, 0);
292 PG_RETURN_NDBOX_P(result);
293}
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:262
#define ARR_DATA_PTR(a)
Definition array.h:322

References ARR_DATA_PTR, array_contains_nulls(), ARRNELEMS, CUBE_MAX_DIM, CUBE_SIZE, DIM, ereport, errcode(), errdetail(), errmsg(), ERROR, fb(), i, idx(), IS_POINT, palloc0(), PG_FREE_IF_COPY, PG_GETARG_ARRAYTYPE_P, PG_GETARG_NDBOX_P, PG_RETURN_NDBOX_P, POINT_SIZE, SET_DIM, SET_POINT_BIT, SET_VARSIZE(), and NDBOX::x.

◆ cube_union()

Datum cube_union ( PG_FUNCTION_ARGS  )

Definition at line 813 of file cube.c.

814{
817 NDBOX *res;
818
819 res = cube_union_v0(a, b);
820
821 PG_FREE_IF_COPY(a, 0);
822 PG_FREE_IF_COPY(b, 1);
824}
NDBOX * cube_union_v0(NDBOX *a, NDBOX *b)
Definition cube.c:753

References a, b, cube_union_v0(), PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, and PG_RETURN_NDBOX_P.

◆ cube_union_v0()

NDBOX * cube_union_v0 ( NDBOX a,
NDBOX b 
)

Definition at line 753 of file cube.c.

754{
755 int i;
756 NDBOX *result;
757 int dim;
758 int size;
759
760 /* trivial case */
761 if (a == b)
762 return a;
763
764 /* swap the arguments if needed, so that 'a' is always larger than 'b' */
765 if (DIM(a) < DIM(b))
766 {
767 NDBOX *tmp = b;
768
769 b = a;
770 a = tmp;
771 }
772 dim = DIM(a);
773
774 size = CUBE_SIZE(dim);
775 result = palloc0(size);
776 SET_VARSIZE(result, size);
777 SET_DIM(result, dim);
778
779 /* First compute the union of the dimensions present in both args */
780 for (i = 0; i < DIM(b); i++)
781 {
782 result->x[i] = Min(Min(LL_COORD(a, i), UR_COORD(a, i)),
783 Min(LL_COORD(b, i), UR_COORD(b, i)));
784 result->x[i + DIM(a)] = Max(Max(LL_COORD(a, i), UR_COORD(a, i)),
785 Max(LL_COORD(b, i), UR_COORD(b, i)));
786 }
787 /* continue on the higher dimensions only present in 'a' */
788 for (; i < DIM(a); i++)
789 {
790 result->x[i] = Min(0,
791 Min(LL_COORD(a, i), UR_COORD(a, i))
792 );
793 result->x[i + dim] = Max(0,
794 Max(LL_COORD(a, i), UR_COORD(a, i))
795 );
796 }
797
798 /*
799 * Check if the result was in fact a point, and set the flag in the datum
800 * accordingly. (we don't bother to repalloc it smaller)
801 */
802 if (cube_is_point_internal(result))
803 {
804 size = POINT_SIZE(dim);
805 SET_VARSIZE(result, size);
806 SET_POINT_BIT(result);
807 }
808
809 return result;
810}

References a, b, cube_is_point_internal(), CUBE_SIZE, DIM, i, LL_COORD, Max, Min, palloc0(), POINT_SIZE, SET_DIM, SET_POINT_BIT, SET_VARSIZE(), UR_COORD, and NDBOX::x.

Referenced by cube_union(), g_cube_binary_union(), g_cube_penalty(), and g_cube_picksplit().

◆ cube_ur_coord()

Datum cube_ur_coord ( PG_FUNCTION_ARGS  )

Definition at line 1587 of file cube.c.

1588{
1590 int n = PG_GETARG_INT32(1);
1591 double result;
1592
1593 if (DIM(c) >= n && n > 0)
1594 result = Max(LL_COORD(c, n - 1), UR_COORD(c, n - 1));
1595 else
1596 result = 0;
1597
1598 PG_FREE_IF_COPY(c, 0);
1599 PG_RETURN_FLOAT8(result);
1600}

References DIM, LL_COORD, Max, PG_FREE_IF_COPY, PG_GETARG_INT32, PG_GETARG_NDBOX_P, PG_RETURN_FLOAT8, and UR_COORD.

◆ distance_1D()

static double distance_1D ( double  a1,
double  a2,
double  b1,
double  b2 
)
static

Definition at line 1508 of file cube.c.

1509{
1510 /* interval (a) is entirely on the left of (b) */
1511 if ((a1 <= b1) && (a2 <= b1) && (a1 <= b2) && (a2 <= b2))
1512 return (Min(b1, b2) - Max(a1, a2));
1513
1514 /* interval (a) is entirely on the right of (b) */
1515 if ((a1 > b1) && (a2 > b1) && (a1 > b2) && (a2 > b2))
1516 return (Min(a1, a2) - Max(b1, b2));
1517
1518 /* the rest are all sorts of intersections */
1519 return 0.0;
1520}
static const FormData_pg_attribute a1
Definition heap.c:144
static const FormData_pg_attribute a2
Definition heap.c:157

References a1, a2, fb(), Max, and Min.

Referenced by cube_distance(), distance_chebyshev(), and distance_taxicab().

◆ distance_chebyshev()

Datum distance_chebyshev ( PG_FUNCTION_ARGS  )

Definition at line 1351 of file cube.c.

1352{
1354 *b = PG_GETARG_NDBOX_P(1);
1355 bool swapped = false;
1356 double d,
1357 distance;
1358 int i;
1359
1360 /* swap the box pointers if needed */
1361 if (DIM(a) < DIM(b))
1362 {
1363 NDBOX *tmp = b;
1364
1365 b = a;
1366 a = tmp;
1367 swapped = true;
1368 }
1369
1370 distance = 0.0;
1371 /* compute within the dimensions of (b) */
1372 for (i = 0; i < DIM(b); i++)
1373 {
1375 LL_COORD(b, i), UR_COORD(b, i)));
1376 if (d > distance)
1377 distance = d;
1378 }
1379
1380 /* compute distance to zero for those dimensions in (a) absent in (b) */
1381 for (i = DIM(b); i < DIM(a); i++)
1382 {
1383 d = fabs(distance_1D(LL_COORD(a, i), UR_COORD(a, i), 0.0, 0.0));
1384 if (d > distance)
1385 distance = d;
1386 }
1387
1388 if (swapped)
1389 {
1390 PG_FREE_IF_COPY(b, 0);
1391 PG_FREE_IF_COPY(a, 1);
1392 }
1393 else
1394 {
1395 PG_FREE_IF_COPY(a, 0);
1396 PG_FREE_IF_COPY(b, 1);
1397 }
1398
1399 PG_RETURN_FLOAT8(distance);
1400}

References a, b, DIM, distance_1D(), fb(), i, LL_COORD, PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, PG_RETURN_FLOAT8, and UR_COORD.

Referenced by g_cube_distance().

◆ distance_taxicab()

Datum distance_taxicab ( PG_FUNCTION_ARGS  )

Definition at line 1307 of file cube.c.

1308{
1310 *b = PG_GETARG_NDBOX_P(1);
1311 bool swapped = false;
1312 double distance;
1313 int i;
1314
1315 /* swap the box pointers if needed */
1316 if (DIM(a) < DIM(b))
1317 {
1318 NDBOX *tmp = b;
1319
1320 b = a;
1321 a = tmp;
1322 swapped = true;
1323 }
1324
1325 distance = 0.0;
1326 /* compute within the dimensions of (b) */
1327 for (i = 0; i < DIM(b); i++)
1328 distance += fabs(distance_1D(LL_COORD(a, i), UR_COORD(a, i),
1329 LL_COORD(b, i), UR_COORD(b, i)));
1330
1331 /* compute distance to zero for those dimensions in (a) absent in (b) */
1332 for (i = DIM(b); i < DIM(a); i++)
1333 distance += fabs(distance_1D(LL_COORD(a, i), UR_COORD(a, i),
1334 0.0, 0.0));
1335
1336 if (swapped)
1337 {
1338 PG_FREE_IF_COPY(b, 0);
1339 PG_FREE_IF_COPY(a, 1);
1340 }
1341 else
1342 {
1343 PG_FREE_IF_COPY(a, 0);
1344 PG_FREE_IF_COPY(b, 1);
1345 }
1346
1347 PG_RETURN_FLOAT8(distance);
1348}

References a, b, DIM, distance_1D(), fb(), i, LL_COORD, PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, PG_RETURN_FLOAT8, and UR_COORD.

Referenced by g_cube_distance().

◆ g_cube_binary_union()

NDBOX * g_cube_binary_union ( NDBOX r1,
NDBOX r2,
int sizep 
)

Definition at line 740 of file cube.c.

741{
742 NDBOX *retval;
743
744 retval = cube_union_v0(r1, r2);
745 *sizep = VARSIZE(retval);
746
747 return retval;
748}
static Size VARSIZE(const void *PTR)
Definition varatt.h:298

References cube_union_v0(), fb(), and VARSIZE().

Referenced by g_cube_union().

◆ g_cube_compress()

Datum g_cube_compress ( PG_FUNCTION_ARGS  )

Definition at line 462 of file cube.c.

463{
465}
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define PG_RETURN_DATUM(x)
Definition fmgr.h:354

References PG_GETARG_DATUM, and PG_RETURN_DATUM.

◆ g_cube_consistent()

Datum g_cube_consistent ( PG_FUNCTION_ARGS  )

Definition at line 395 of file cube.c.

396{
397 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
398 NDBOX *query = PG_GETARG_NDBOX_P(1);
400#ifdef NOT_USED
401 Oid subtype = PG_GETARG_OID(3);
402#endif
403 bool *recheck = (bool *) PG_GETARG_POINTER(4);
404 bool res;
405
406 /* All cases served by this function are exact */
407 *recheck = false;
408
409 /*
410 * if entry is not leaf, use g_cube_internal_consistent, else use
411 * g_cube_leaf_consistent
412 */
413 if (GIST_LEAF(entry))
415 query, strategy);
416 else
418 query, strategy);
419
420 PG_FREE_IF_COPY(query, 1);
421 PG_RETURN_BOOL(res);
422}
bool g_cube_leaf_consistent(NDBOX *key, NDBOX *query, StrategyNumber strategy)
Definition cube.c:684
bool g_cube_internal_consistent(NDBOX *key, NDBOX *query, StrategyNumber strategy)
Definition cube.c:713
#define DatumGetNDBOXP(x)
Definition cubedata.h:52
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_GETARG_UINT16(n)
Definition fmgr.h:272
#define GIST_LEAF(entry)
Definition gist.h:171
unsigned int Oid
uint16 StrategyNumber
Definition stratnum.h:22
Datum key
Definition gist.h:161

References DatumGetNDBOXP, g_cube_internal_consistent(), g_cube_leaf_consistent(), GIST_LEAF, GISTENTRY::key, PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, PG_GETARG_OID, PG_GETARG_POINTER, PG_GETARG_UINT16, and PG_RETURN_BOOL.

◆ g_cube_decompress()

Datum g_cube_decompress ( PG_FUNCTION_ARGS  )

Definition at line 468 of file cube.c.

469{
470 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
471 NDBOX *key = DatumGetNDBOXP(entry->key);
472
473 if (key != DatumGetNDBOXP(entry->key))
474 {
476
477 gistentryinit(*retval, PointerGetDatum(key),
478 entry->rel, entry->page,
479 entry->offset, false);
480 PG_RETURN_POINTER(retval);
481 }
482 PG_RETURN_POINTER(entry);
483}
#define palloc_object(type)
Definition fe_memutils.h:74
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define gistentryinit(e, k, r, pg, o, l)
Definition gist.h:245
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
OffsetNumber offset
Definition gist.h:164
Page page
Definition gist.h:163
Relation rel
Definition gist.h:162

References DatumGetNDBOXP, gistentryinit, GISTENTRY::key, GISTENTRY::offset, GISTENTRY::page, palloc_object, PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum(), and GISTENTRY::rel.

◆ g_cube_distance()

Datum g_cube_distance ( PG_FUNCTION_ARGS  )

Definition at line 1403 of file cube.c.

1404{
1405 GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
1407 NDBOX *cube = DatumGetNDBOXP(entry->key);
1408 double retval;
1409
1410 if (strategy == CubeKNNDistanceCoord)
1411 {
1412 /*
1413 * Handle ordering by ~> operator. See comments of cube_coord_llur()
1414 * for details
1415 */
1416 int coord = PG_GETARG_INT32(1);
1417 bool isLeaf = GistPageIsLeaf(entry->page);
1418 bool inverse = false;
1419
1420 /* 0 is the only unsupported coordinate value */
1421 if (coord == 0)
1422 ereport(ERROR,
1424 errmsg("zero cube index is not defined")));
1425
1426 /* Return inversed value for negative coordinate */
1427 if (coord < 0)
1428 {
1429 coord = -coord;
1430 inverse = true;
1431 }
1432
1433 if (coord <= 2 * DIM(cube))
1434 {
1435 /* dimension index */
1436 int index = (coord - 1) / 2;
1437
1438 /* whether this is upper bound (lower bound otherwise) */
1439 bool upper = ((coord - 1) % 2 == 1);
1440
1441 if (IS_POINT(cube))
1442 {
1443 retval = cube->x[index];
1444 }
1445 else
1446 {
1447 if (isLeaf)
1448 {
1449 /* For leaf just return required upper/lower bound */
1450 if (upper)
1451 retval = Max(cube->x[index], cube->x[index + DIM(cube)]);
1452 else
1453 retval = Min(cube->x[index], cube->x[index + DIM(cube)]);
1454 }
1455 else
1456 {
1457 /*
1458 * For non-leaf we should always return lower bound,
1459 * because even upper bound of a child in the subtree can
1460 * be as small as our lower bound. For inversed case we
1461 * return upper bound because it becomes lower bound for
1462 * inversed value.
1463 */
1464 if (!inverse)
1465 retval = Min(cube->x[index], cube->x[index + DIM(cube)]);
1466 else
1467 retval = Max(cube->x[index], cube->x[index + DIM(cube)]);
1468 }
1469 }
1470 }
1471 else
1472 {
1473 retval = 0.0;
1474 }
1475
1476 /* Inverse return value if needed */
1477 if (inverse)
1478 retval = -retval;
1479 }
1480 else
1481 {
1482 NDBOX *query = PG_GETARG_NDBOX_P(1);
1483
1484 switch (strategy)
1485 {
1489 break;
1493 break;
1497 break;
1498 default:
1499 elog(ERROR, "unrecognized cube strategy number: %d", strategy);
1500 retval = 0; /* keep compiler quiet */
1501 break;
1502 }
1503 }
1504 PG_RETURN_FLOAT8(retval);
1505}
Datum distance_taxicab(PG_FUNCTION_ARGS)
Definition cube.c:1307
Datum cube_distance(PG_FUNCTION_ARGS)
Definition cube.c:1258
Datum distance_chebyshev(PG_FUNCTION_ARGS)
Definition cube.c:1351
#define CubeKNNDistanceCoord
Definition cubedata.h:57
#define CubeKNNDistanceEuclid
Definition cubedata.h:59
#define CubeKNNDistanceTaxicab
Definition cubedata.h:58
#define CubeKNNDistanceChebyshev
Definition cubedata.h:60
#define elog(elevel,...)
Definition elog.h:226
#define DirectFunctionCall2(func, arg1, arg2)
Definition fmgr.h:686
#define GistPageIsLeaf(page)
Definition gist.h:170
static float8 DatumGetFloat8(Datum X)
Definition postgres.h:495

References cube_distance(), CubeKNNDistanceChebyshev, CubeKNNDistanceCoord, CubeKNNDistanceEuclid, CubeKNNDistanceTaxicab, DatumGetFloat8(), DatumGetNDBOXP, DIM, DirectFunctionCall2, distance_chebyshev(), distance_taxicab(), elog, ereport, errcode(), errmsg(), ERROR, fb(), GistPageIsLeaf, IS_POINT, GISTENTRY::key, Max, Min, GISTENTRY::page, PG_GETARG_INT32, PG_GETARG_NDBOX_P, PG_GETARG_POINTER, PG_GETARG_UINT16, PG_RETURN_FLOAT8, PointerGetDatum(), and upper().

◆ g_cube_internal_consistent()

bool g_cube_internal_consistent ( NDBOX key,
NDBOX query,
StrategyNumber  strategy 
)

Definition at line 713 of file cube.c.

716{
717 bool retval;
718
719 switch (strategy)
720 {
722 retval = cube_overlap_v0(key, query);
723 break;
727 retval = cube_contains_v0(key, query);
728 break;
731 retval = cube_overlap_v0(key, query);
732 break;
733 default:
734 retval = false;
735 }
736 return retval;
737}
#define RTOldContainsStrategyNumber
Definition stratnum.h:63
#define RTOverlapStrategyNumber
Definition stratnum.h:53
#define RTSameStrategyNumber
Definition stratnum.h:56
#define RTContainsStrategyNumber
Definition stratnum.h:57
#define RTOldContainedByStrategyNumber
Definition stratnum.h:64
#define RTContainedByStrategyNumber
Definition stratnum.h:58

References cube_contains_v0(), cube_overlap_v0(), RTContainedByStrategyNumber, RTContainsStrategyNumber, RTOldContainedByStrategyNumber, RTOldContainsStrategyNumber, RTOverlapStrategyNumber, and RTSameStrategyNumber.

Referenced by g_cube_consistent().

◆ g_cube_leaf_consistent()

bool g_cube_leaf_consistent ( NDBOX key,
NDBOX query,
StrategyNumber  strategy 
)

Definition at line 684 of file cube.c.

687{
688 bool retval;
689
690 switch (strategy)
691 {
693 retval = cube_overlap_v0(key, query);
694 break;
696 retval = (cube_cmp_v0(key, query) == 0);
697 break;
700 retval = cube_contains_v0(key, query);
701 break;
704 retval = cube_contains_v0(query, key);
705 break;
706 default:
707 retval = false;
708 }
709 return retval;
710}

References cube_cmp_v0(), cube_contains_v0(), cube_overlap_v0(), RTContainedByStrategyNumber, RTContainsStrategyNumber, RTOldContainedByStrategyNumber, RTOldContainsStrategyNumber, RTOverlapStrategyNumber, and RTSameStrategyNumber.

Referenced by g_cube_consistent().

◆ g_cube_penalty()

Datum g_cube_penalty ( PG_FUNCTION_ARGS  )

Definition at line 491 of file cube.c.

492{
495 float *result = (float *) PG_GETARG_POINTER(2);
496 NDBOX *ud;
497 double tmp1,
498 tmp2;
499
504 *result = (float) (tmp1 - tmp2);
505
506 PG_RETURN_FLOAT8(*result);
507}

References cube_union_v0(), DatumGetNDBOXP, fb(), PG_GETARG_POINTER, PG_RETURN_FLOAT8, and rt_cube_size().

◆ g_cube_picksplit()

Datum g_cube_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 516 of file cube.c.

517{
521 j;
523 *datum_beta;
524 NDBOX *datum_l,
525 *datum_r;
526 NDBOX *union_d,
527 *union_dl,
528 *union_dr;
529 NDBOX *inter_d;
530 bool firsttime;
531 double size_alpha,
532 size_beta,
535 double size_waste,
536 waste;
537 double size_l,
538 size_r;
539 int nbytes;
541 seed_2 = 2;
542 OffsetNumber *left,
543 *right;
544 OffsetNumber maxoff;
545
546 maxoff = entryvec->n - 2;
547 nbytes = (maxoff + 2) * sizeof(OffsetNumber);
548 v->spl_left = (OffsetNumber *) palloc(nbytes);
549 v->spl_right = (OffsetNumber *) palloc(nbytes);
550
551 firsttime = true;
552 waste = 0.0;
553
554 for (i = FirstOffsetNumber; i < maxoff; i = OffsetNumberNext(i))
555 {
556 datum_alpha = DatumGetNDBOXP(entryvec->vector[i].key);
557 for (j = OffsetNumberNext(i); j <= maxoff; j = OffsetNumberNext(j))
558 {
559 datum_beta = DatumGetNDBOXP(entryvec->vector[j].key);
560
561 /* compute the wasted space by unioning these guys */
562 /* size_waste = size_union - size_inter; */
566 entryvec->vector[i].key,
567 entryvec->vector[j].key));
570
571 /*
572 * are these a more promising split than what we've already seen?
573 */
574
575 if (size_waste > waste || firsttime)
576 {
577 waste = size_waste;
578 seed_1 = i;
579 seed_2 = j;
580 firsttime = false;
581 }
582 }
583 }
584
585 left = v->spl_left;
586 v->spl_nleft = 0;
587 right = v->spl_right;
588 v->spl_nright = 0;
589
596
597 /*
598 * Now split up the regions between the two seeds. An important property
599 * of this split algorithm is that the split vector v has the indices of
600 * items to be split in order in its left and right vectors. We exploit
601 * this property by doing a merge in the code that actually splits the
602 * page.
603 *
604 * For efficiency, we also place the new index tuple in this loop. This is
605 * handled at the very end, when we have placed all the existing tuples
606 * and i == maxoff + 1.
607 */
608
609 maxoff = OffsetNumberNext(maxoff);
610 for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
611 {
612 /*
613 * If we've already decided where to place this item, just put it on
614 * the right list. Otherwise, we need to figure out which page needs
615 * the least enlargement in order to store the item.
616 */
617
618 if (i == seed_1)
619 {
620 *left++ = i;
621 v->spl_nleft++;
622 continue;
623 }
624 else if (i == seed_2)
625 {
626 *right++ = i;
627 v->spl_nright++;
628 continue;
629 }
630
631 /* okay, which page needs least enlargement? */
632 datum_alpha = DatumGetNDBOXP(entryvec->vector[i].key);
637
638 /* pick which page to add it to */
640 {
643 *left++ = i;
644 v->spl_nleft++;
645 }
646 else
647 {
650 *right++ = i;
651 v->spl_nright++;
652 }
653 }
654 *left = *right = FirstOffsetNumber; /* sentinel value */
655
658
660}
Datum cube_inter(PG_FUNCTION_ARGS)
Definition cube.c:828
#define OffsetNumberNext(offsetNumber)
Definition off.h:52
uint16 OffsetNumber
Definition off.h:24
#define FirstOffsetNumber
Definition off.h:27
int spl_nleft
Definition gist.h:144
OffsetNumber * spl_right
Definition gist.h:148
Datum spl_ldatum
Definition gist.h:145
Datum spl_rdatum
Definition gist.h:150
int spl_nright
Definition gist.h:149
OffsetNumber * spl_left
Definition gist.h:143

References cube_inter(), cube_union_v0(), DatumGetNDBOXP, DirectFunctionCall2, fb(), FirstOffsetNumber, i, j, OffsetNumberNext, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum(), rt_cube_size(), GIST_SPLITVEC::spl_ldatum, GIST_SPLITVEC::spl_left, GIST_SPLITVEC::spl_nleft, GIST_SPLITVEC::spl_nright, GIST_SPLITVEC::spl_rdatum, and GIST_SPLITVEC::spl_right.

◆ g_cube_same()

Datum g_cube_same ( PG_FUNCTION_ARGS  )

Definition at line 666 of file cube.c.

667{
670 bool *result = (bool *) PG_GETARG_POINTER(2);
671
672 if (cube_cmp_v0(b1, b2) == 0)
673 *result = true;
674 else
675 *result = false;
676
677 PG_RETURN_NDBOX_P(result);
678}

References cube_cmp_v0(), fb(), PG_GETARG_NDBOX_P, PG_GETARG_POINTER, and PG_RETURN_NDBOX_P.

◆ g_cube_union()

Datum g_cube_union ( PG_FUNCTION_ARGS  )

Definition at line 430 of file cube.c.

431{
433 int *sizep = (int *) PG_GETARG_POINTER(1);
434 NDBOX *out = (NDBOX *) NULL;
435 NDBOX *tmp;
436 int i;
437
438 tmp = DatumGetNDBOXP(entryvec->vector[0].key);
439
440 /*
441 * sizep = sizeof(NDBOX); -- NDBOX has variable size
442 */
443 *sizep = VARSIZE(tmp);
444
445 for (i = 1; i < entryvec->n; i++)
446 {
447 out = g_cube_binary_union(tmp,
448 DatumGetNDBOXP(entryvec->vector[i].key),
449 sizep);
450 tmp = out;
451 }
452
454}
NDBOX * g_cube_binary_union(NDBOX *r1, NDBOX *r2, int *sizep)
Definition cube.c:740

References DatumGetNDBOXP, fb(), g_cube_binary_union(), i, PG_GETARG_POINTER, PG_RETURN_POINTER, and VARSIZE().

◆ PG_FUNCTION_INFO_V1() [1/42]

PG_FUNCTION_INFO_V1 ( cube_a_f8  )

◆ PG_FUNCTION_INFO_V1() [2/42]

PG_FUNCTION_INFO_V1 ( cube_a_f8_f8  )

◆ PG_FUNCTION_INFO_V1() [3/42]

PG_FUNCTION_INFO_V1 ( cube_c_f8  )

◆ PG_FUNCTION_INFO_V1() [4/42]

PG_FUNCTION_INFO_V1 ( cube_c_f8_f8  )

◆ PG_FUNCTION_INFO_V1() [5/42]

PG_FUNCTION_INFO_V1 ( cube_cmp  )

◆ PG_FUNCTION_INFO_V1() [6/42]

PG_FUNCTION_INFO_V1 ( cube_contained  )

◆ PG_FUNCTION_INFO_V1() [7/42]

PG_FUNCTION_INFO_V1 ( cube_contains  )

◆ PG_FUNCTION_INFO_V1() [8/42]

PG_FUNCTION_INFO_V1 ( cube_coord  )

◆ PG_FUNCTION_INFO_V1() [9/42]

PG_FUNCTION_INFO_V1 ( cube_coord_llur  )

◆ PG_FUNCTION_INFO_V1() [10/42]

PG_FUNCTION_INFO_V1 ( cube_dim  )

◆ PG_FUNCTION_INFO_V1() [11/42]

PG_FUNCTION_INFO_V1 ( cube_distance  )

◆ PG_FUNCTION_INFO_V1() [12/42]

PG_FUNCTION_INFO_V1 ( cube_enlarge  )

◆ PG_FUNCTION_INFO_V1() [13/42]

PG_FUNCTION_INFO_V1 ( cube_eq  )

◆ PG_FUNCTION_INFO_V1() [14/42]

PG_FUNCTION_INFO_V1 ( cube_f8  )

◆ PG_FUNCTION_INFO_V1() [15/42]

PG_FUNCTION_INFO_V1 ( cube_f8_f8  )

◆ PG_FUNCTION_INFO_V1() [16/42]

PG_FUNCTION_INFO_V1 ( cube_ge  )

◆ PG_FUNCTION_INFO_V1() [17/42]

PG_FUNCTION_INFO_V1 ( cube_gt  )

◆ PG_FUNCTION_INFO_V1() [18/42]

PG_FUNCTION_INFO_V1 ( cube_in  )

◆ PG_FUNCTION_INFO_V1() [19/42]

PG_FUNCTION_INFO_V1 ( cube_inter  )

◆ PG_FUNCTION_INFO_V1() [20/42]

PG_FUNCTION_INFO_V1 ( cube_is_point  )

◆ PG_FUNCTION_INFO_V1() [21/42]

PG_FUNCTION_INFO_V1 ( cube_le  )

◆ PG_FUNCTION_INFO_V1() [22/42]

PG_FUNCTION_INFO_V1 ( cube_ll_coord  )

◆ PG_FUNCTION_INFO_V1() [23/42]

PG_FUNCTION_INFO_V1 ( cube_lt  )

◆ PG_FUNCTION_INFO_V1() [24/42]

PG_FUNCTION_INFO_V1 ( cube_ne  )

◆ PG_FUNCTION_INFO_V1() [25/42]

PG_FUNCTION_INFO_V1 ( cube_out  )

◆ PG_FUNCTION_INFO_V1() [26/42]

PG_FUNCTION_INFO_V1 ( cube_overlap  )

◆ PG_FUNCTION_INFO_V1() [27/42]

PG_FUNCTION_INFO_V1 ( cube_recv  )

◆ PG_FUNCTION_INFO_V1() [28/42]

PG_FUNCTION_INFO_V1 ( cube_send  )

◆ PG_FUNCTION_INFO_V1() [29/42]

PG_FUNCTION_INFO_V1 ( cube_size  )

◆ PG_FUNCTION_INFO_V1() [30/42]

PG_FUNCTION_INFO_V1 ( cube_subset  )

◆ PG_FUNCTION_INFO_V1() [31/42]

PG_FUNCTION_INFO_V1 ( cube_union  )

◆ PG_FUNCTION_INFO_V1() [32/42]

PG_FUNCTION_INFO_V1 ( cube_ur_coord  )

◆ PG_FUNCTION_INFO_V1() [33/42]

PG_FUNCTION_INFO_V1 ( distance_chebyshev  )

◆ PG_FUNCTION_INFO_V1() [34/42]

PG_FUNCTION_INFO_V1 ( distance_taxicab  )

◆ PG_FUNCTION_INFO_V1() [35/42]

PG_FUNCTION_INFO_V1 ( g_cube_compress  )

◆ PG_FUNCTION_INFO_V1() [36/42]

PG_FUNCTION_INFO_V1 ( g_cube_consistent  )

◆ PG_FUNCTION_INFO_V1() [37/42]

PG_FUNCTION_INFO_V1 ( g_cube_decompress  )

◆ PG_FUNCTION_INFO_V1() [38/42]

PG_FUNCTION_INFO_V1 ( g_cube_distance  )

◆ PG_FUNCTION_INFO_V1() [39/42]

PG_FUNCTION_INFO_V1 ( g_cube_penalty  )

◆ PG_FUNCTION_INFO_V1() [40/42]

PG_FUNCTION_INFO_V1 ( g_cube_picksplit  )

◆ PG_FUNCTION_INFO_V1() [41/42]

PG_FUNCTION_INFO_V1 ( g_cube_same  )

◆ PG_FUNCTION_INFO_V1() [42/42]

PG_FUNCTION_INFO_V1 ( g_cube_union  )

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "cube",
version = PG_VERSION 
)

◆ rt_cube_size()

void rt_cube_size ( NDBOX a,
double size 
)

Definition at line 915 of file cube.c.

916{
917 double result;
918 int i;
919
920 if (a == (NDBOX *) NULL)
921 {
922 /* special case for GiST */
923 result = 0.0;
924 }
925 else if (IS_POINT(a) || DIM(a) == 0)
926 {
927 /* necessarily has zero size */
928 result = 0.0;
929 }
930 else
931 {
932 result = 1.0;
933 for (i = 0; i < DIM(a); i++)
934 result *= fabs(UR_COORD(a, i) - LL_COORD(a, i));
935 }
936 *size = result;
937}

References a, DIM, fb(), i, IS_POINT, LL_COORD, and UR_COORD.

Referenced by cube_size(), g_cube_penalty(), and g_cube_picksplit().