PostgreSQL Source Code  git master
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_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 *sz)
 
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)
 

Variables

 PG_MODULE_MAGIC
 

Macro Definition Documentation

◆ ARRNELEMS

◆ ARRPTR

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

Definition at line 25 of file cube.c.

Referenced by _int_same(), _int_unique(), _intbig_contains(), _intbig_overlap(), array_to_tsvector(), boolop(), cnt_length(), compute_tsvector_stats(), copy_intArrayType(), cube_a_f8(), cube_a_f8_f8(), execconsistent(), find_wordentry(), g_int_compress(), g_int_decompress(), g_int_same(), g_int_union(), g_intbig_compress(), g_intbig_consistent(), ghstore_compress(), ghstore_consistent(), gin_extract_hstore(), gin_extract_tsvector(), ginint4_queryextract(), gtsvector_compress(), hstore_akeys(), hstore_avals(), hstore_cmp(), hstore_concat(), hstore_contains(), hstore_defined(), hstore_delete(), hstore_delete_array(), hstore_delete_hstore(), hstore_each(), hstore_fetchval(), hstore_hash(), hstore_hash_extended(), hstore_out(), hstore_populate_record(), hstore_send(), hstore_skeys(), hstore_slice_to_array(), hstore_slice_to_hstore(), hstore_subscript_assign(), hstore_subscript_fetch(), hstore_svals(), hstore_to_array_internal(), hstore_to_json(), hstore_to_json_loose(), hstore_to_jsonb(), hstore_to_jsonb_loose(), hstore_to_plperl(), hstore_to_plpython(), hstoreFindKey(), hstorePairs(), hstoreUpgrade(), hstoreValidNewFormat(), hstoreValidOldFormat(), inner_int_contains(), inner_int_inter(), inner_int_overlap(), inner_int_union(), insertStatEntry(), int_to_intset(), intarray_add_elem(), intarray_concat_arrays(), intarray_del_elem(), intarray_match_first(), intset_subtract(), make_tsvector(), silly_cmp_tsvector(), subarray(), ts_match_vq(), tsvector_bsearch(), tsvector_concat(), tsvector_delete_by_indices(), tsvector_filter(), tsvector_setweight(), tsvector_setweight_by_filter(), tsvector_strip(), tsvector_to_array(), tsvector_unnest(), tsvectorin(), tsvectorout(), tsvectorrecv(), and tsvectorsend().

Function Documentation

◆ cube_a_f8()

Datum cube_a_f8 ( PG_FUNCTION_ARGS  )

Definition at line 205 of file cube.c.

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

206 {
208  NDBOX *result;
209  int i;
210  int dim;
211  int size;
212  double *dur;
213 
214  if (array_contains_nulls(ur))
215  ereport(ERROR,
216  (errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
217  errmsg("cannot work with arrays containing NULLs")));
218 
219  dim = ARRNELEMS(ur);
220  if (dim > CUBE_MAX_DIM)
221  ereport(ERROR,
222  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
223  errmsg("array is too long"),
224  errdetail("A cube cannot have more than %d dimensions.",
225  CUBE_MAX_DIM)));
226 
227  dur = ARRPTR(ur);
228 
229  size = POINT_SIZE(dim);
230  result = (NDBOX *) palloc0(size);
231  SET_VARSIZE(result, size);
232  SET_DIM(result, dim);
233  SET_POINT_BIT(result);
234 
235  for (i = 0; i < dim; i++)
236  result->x[i] = dur[i];
237 
238  PG_RETURN_NDBOX_P(result);
239 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define CUBE_MAX_DIM
Definition: cubedata.h:7
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
#define ERROR
Definition: elog.h:46
int errdetail(const char *fmt,...)
Definition: elog.c:1042
void * palloc0(Size size)
Definition: mcxt.c:1093
#define ereport(elevel,...)
Definition: elog.h:157
Definition: cubedata.h:9
#define PG_RETURN_NDBOX_P(x)
Definition: cubedata.h:54
double x[FLEXIBLE_ARRAY_MEMBER]
Definition: cubedata.h:33
#define POINT_SIZE(_dim)
Definition: cubedata.h:48
#define ARRNELEMS(x)
Definition: cube.c:26
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define SET_POINT_BIT(cube)
Definition: cubedata.h:41
int i
#define SET_DIM(cube, _dim)
Definition: cubedata.h:43
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
#define ARRPTR(x)
Definition: cube.c:25
bool array_contains_nulls(ArrayType *array)
Definition: arrayfuncs.c:3557

◆ cube_a_f8_f8()

Datum cube_a_f8_f8 ( PG_FUNCTION_ARGS  )

Definition at line 138 of file cube.c.

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

139 {
142  NDBOX *result;
143  int i;
144  int dim;
145  int size;
146  bool point;
147  double *dur,
148  *dll;
149 
151  ereport(ERROR,
152  (errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
153  errmsg("cannot work with arrays containing NULLs")));
154 
155  dim = ARRNELEMS(ur);
156  if (dim > CUBE_MAX_DIM)
157  ereport(ERROR,
158  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
159  errmsg("can't extend cube"),
160  errdetail("A cube cannot have more than %d dimensions.",
161  CUBE_MAX_DIM)));
162 
163  if (ARRNELEMS(ll) != dim)
164  ereport(ERROR,
165  (errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
166  errmsg("UR and LL arrays must be of same length")));
167 
168  dur = ARRPTR(ur);
169  dll = ARRPTR(ll);
170 
171  /* Check if it's a point */
172  point = true;
173  for (i = 0; i < dim; i++)
174  {
175  if (dur[i] != dll[i])
176  {
177  point = false;
178  break;
179  }
180  }
181 
182  size = point ? POINT_SIZE(dim) : CUBE_SIZE(dim);
183  result = (NDBOX *) palloc0(size);
184  SET_VARSIZE(result, size);
185  SET_DIM(result, dim);
186 
187  for (i = 0; i < dim; i++)
188  result->x[i] = dur[i];
189 
190  if (!point)
191  {
192  for (i = 0; i < dim; i++)
193  result->x[i + dim] = dll[i];
194  }
195  else
196  SET_POINT_BIT(result);
197 
198  PG_RETURN_NDBOX_P(result);
199 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define CUBE_MAX_DIM
Definition: cubedata.h:7
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
#define ERROR
Definition: elog.h:46
int errdetail(const char *fmt,...)
Definition: elog.c:1042
void * palloc0(Size size)
Definition: mcxt.c:1093
#define ereport(elevel,...)
Definition: elog.h:157
Definition: cubedata.h:9
#define PG_RETURN_NDBOX_P(x)
Definition: cubedata.h:54
double x[FLEXIBLE_ARRAY_MEMBER]
Definition: cubedata.h:33
#define POINT_SIZE(_dim)
Definition: cubedata.h:48
#define ARRNELEMS(x)
Definition: cube.c:26
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define SET_POINT_BIT(cube)
Definition: cubedata.h:41
int i
#define SET_DIM(cube, _dim)
Definition: cubedata.h:43
#define CUBE_SIZE(_dim)
Definition: cubedata.h:49
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
#define ARRPTR(x)
Definition: cube.c:25
bool array_contains_nulls(ArrayType *array)
Definition: arrayfuncs.c:3557

◆ cube_c_f8()

Datum cube_c_f8 ( PG_FUNCTION_ARGS  )

Definition at line 1817 of file cube.c.

References CUBE_MAX_DIM, CUBE_SIZE, DIM, ereport, errcode(), errdetail(), errmsg(), ERROR, 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, and NDBOX::x.

1818 {
1819  NDBOX *cube = PG_GETARG_NDBOX_P(0);
1820  double x = PG_GETARG_FLOAT8(1);
1821  NDBOX *result;
1822  int size;
1823  int i;
1824 
1825  if (DIM(cube) + 1 > CUBE_MAX_DIM)
1826  ereport(ERROR,
1827  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1828  errmsg("can't extend cube"),
1829  errdetail("A cube cannot have more than %d dimensions.",
1830  CUBE_MAX_DIM)));
1831 
1832  if (IS_POINT(cube))
1833  {
1834  size = POINT_SIZE((DIM(cube) + 1));
1835  result = (NDBOX *) palloc0(size);
1836  SET_VARSIZE(result, size);
1837  SET_DIM(result, DIM(cube) + 1);
1838  SET_POINT_BIT(result);
1839  for (i = 0; i < DIM(cube); i++)
1840  result->x[i] = cube->x[i];
1841  result->x[DIM(result) - 1] = x;
1842  }
1843  else
1844  {
1845  size = CUBE_SIZE((DIM(cube) + 1));
1846  result = (NDBOX *) palloc0(size);
1847  SET_VARSIZE(result, size);
1848  SET_DIM(result, DIM(cube) + 1);
1849  for (i = 0; i < DIM(cube); i++)
1850  {
1851  result->x[i] = cube->x[i];
1852  result->x[DIM(result) + i] = cube->x[DIM(cube) + i];
1853  }
1854  result->x[DIM(result) - 1] = x;
1855  result->x[2 * DIM(result) - 1] = x;
1856  }
1857 
1858  PG_FREE_IF_COPY(cube, 0);
1859  PG_RETURN_NDBOX_P(result);
1860 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#define DIM(cube)
Definition: cubedata.h:42
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
#define CUBE_MAX_DIM
Definition: cubedata.h:7
#define ERROR
Definition: elog.h:46
int errdetail(const char *fmt,...)
Definition: elog.c:1042
#define IS_POINT(cube)
Definition: cubedata.h:40
void * palloc0(Size size)
Definition: mcxt.c:1093
#define ereport(elevel,...)
Definition: elog.h:157
Definition: cubedata.h:9
#define PG_RETURN_NDBOX_P(x)
Definition: cubedata.h:54
double x[FLEXIBLE_ARRAY_MEMBER]
Definition: cubedata.h:33
#define POINT_SIZE(_dim)
Definition: cubedata.h:48
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define SET_POINT_BIT(cube)
Definition: cubedata.h:41
int i
#define SET_DIM(cube, _dim)
Definition: cubedata.h:43
#define CUBE_SIZE(_dim)
Definition: cubedata.h:49
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ cube_c_f8_f8()

Datum cube_c_f8_f8 ( PG_FUNCTION_ARGS  )

Definition at line 1864 of file cube.c.

References CUBE_MAX_DIM, CUBE_SIZE, DIM, ereport, errcode(), errdetail(), errmsg(), ERROR, 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.

1865 {
1866  NDBOX *cube = PG_GETARG_NDBOX_P(0);
1867  double x1 = PG_GETARG_FLOAT8(1);
1868  double x2 = PG_GETARG_FLOAT8(2);
1869  NDBOX *result;
1870  int size;
1871  int i;
1872 
1873  if (DIM(cube) + 1 > CUBE_MAX_DIM)
1874  ereport(ERROR,
1875  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1876  errmsg("can't extend cube"),
1877  errdetail("A cube cannot have more than %d dimensions.",
1878  CUBE_MAX_DIM)));
1879 
1880  if (IS_POINT(cube) && (x1 == x2))
1881  {
1882  size = POINT_SIZE((DIM(cube) + 1));
1883  result = (NDBOX *) palloc0(size);
1884  SET_VARSIZE(result, size);
1885  SET_DIM(result, DIM(cube) + 1);
1886  SET_POINT_BIT(result);
1887  for (i = 0; i < DIM(cube); i++)
1888  result->x[i] = cube->x[i];
1889  result->x[DIM(result) - 1] = x1;
1890  }
1891  else
1892  {
1893  size = CUBE_SIZE((DIM(cube) + 1));
1894  result = (NDBOX *) palloc0(size);
1895  SET_VARSIZE(result, size);
1896  SET_DIM(result, DIM(cube) + 1);
1897  for (i = 0; i < DIM(cube); i++)
1898  {
1899  result->x[i] = LL_COORD(cube, i);
1900  result->x[DIM(result) + i] = UR_COORD(cube, i);
1901  }
1902  result->x[DIM(result) - 1] = x1;
1903  result->x[2 * DIM(result) - 1] = x2;
1904  }
1905 
1906  PG_FREE_IF_COPY(cube, 0);
1907  PG_RETURN_NDBOX_P(result);
1908 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#define DIM(cube)
Definition: cubedata.h:42
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
#define CUBE_MAX_DIM
Definition: cubedata.h:7
#define ERROR
Definition: elog.h:46
#define LL_COORD(cube, i)
Definition: cubedata.h:45
int errdetail(const char *fmt,...)
Definition: elog.c:1042
#define IS_POINT(cube)
Definition: cubedata.h:40
void * palloc0(Size size)
Definition: mcxt.c:1093
#define ereport(elevel,...)
Definition: elog.h:157
Definition: cubedata.h:9
#define PG_RETURN_NDBOX_P(x)
Definition: cubedata.h:54
double x[FLEXIBLE_ARRAY_MEMBER]
Definition: cubedata.h:33
#define POINT_SIZE(_dim)
Definition: cubedata.h:48
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define SET_POINT_BIT(cube)
Definition: cubedata.h:41
int i
#define SET_DIM(cube, _dim)
Definition: cubedata.h:43
#define CUBE_SIZE(_dim)
Definition: cubedata.h:49
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
#define UR_COORD(cube, i)
Definition: cubedata.h:46

◆ cube_cmp()

Datum cube_cmp ( PG_FUNCTION_ARGS  )

Definition at line 1016 of file cube.c.

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

1017 {
1018  NDBOX *a = PG_GETARG_NDBOX_P(0),
1019  *b = PG_GETARG_NDBOX_P(1);
1020  int32 res;
1021 
1022  res = cube_cmp_v0(a, b);
1023 
1024  PG_FREE_IF_COPY(a, 0);
1025  PG_FREE_IF_COPY(b, 1);
1026  PG_RETURN_INT32(res);
1027 }
int32 cube_cmp_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:936
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
signed int int32
Definition: c.h:429
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260

◆ cube_cmp_v0()

int32 cube_cmp_v0 ( NDBOX a,
NDBOX b 
)

Definition at line 936 of file cube.c.

References 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().

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

◆ cube_contained()

Datum cube_contained ( PG_FUNCTION_ARGS  )

Definition at line 1177 of file cube.c.

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

1178 {
1179  NDBOX *a = PG_GETARG_NDBOX_P(0),
1180  *b = PG_GETARG_NDBOX_P(1);
1181  bool res;
1182 
1183  res = cube_contains_v0(b, a);
1184 
1185  PG_FREE_IF_COPY(a, 0);
1186  PG_FREE_IF_COPY(b, 1);
1187  PG_RETURN_BOOL(res);
1188 }
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
bool cube_contains_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:1123

◆ cube_contains()

Datum cube_contains ( PG_FUNCTION_ARGS  )

Definition at line 1161 of file cube.c.

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

1162 {
1163  NDBOX *a = PG_GETARG_NDBOX_P(0),
1164  *b = PG_GETARG_NDBOX_P(1);
1165  bool res;
1166 
1167  res = cube_contains_v0(a, b);
1168 
1169  PG_FREE_IF_COPY(a, 0);
1170  PG_FREE_IF_COPY(b, 1);
1171  PG_RETURN_BOOL(res);
1172 }
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
bool cube_contains_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:1123

◆ cube_contains_v0()

bool cube_contains_v0 ( NDBOX a,
NDBOX b 
)

Definition at line 1123 of file cube.c.

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

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

1124 {
1125  int i;
1126 
1127  if ((a == NULL) || (b == NULL))
1128  return false;
1129 
1130  if (DIM(a) < DIM(b))
1131  {
1132  /*
1133  * the further comparisons will make sense if the excess dimensions of
1134  * (b) were zeroes Since both UL and UR coordinates must be zero, we
1135  * can check them all without worrying about which is which.
1136  */
1137  for (i = DIM(a); i < DIM(b); i++)
1138  {
1139  if (LL_COORD(b, i) != 0)
1140  return false;
1141  if (UR_COORD(b, i) != 0)
1142  return false;
1143  }
1144  }
1145 
1146  /* Can't care less about the excess dimensions of (a), if any */
1147  for (i = 0; i < Min(DIM(a), DIM(b)); i++)
1148  {
1149  if (Min(LL_COORD(a, i), UR_COORD(a, i)) >
1150  Min(LL_COORD(b, i), UR_COORD(b, i)))
1151  return false;
1152  if (Max(LL_COORD(a, i), UR_COORD(a, i)) <
1153  Max(LL_COORD(b, i), UR_COORD(b, i)))
1154  return false;
1155  }
1156 
1157  return true;
1158 }
#define DIM(cube)
Definition: cubedata.h:42
#define Min(x, y)
Definition: c.h:986
#define LL_COORD(cube, i)
Definition: cubedata.h:45
#define Max(x, y)
Definition: c.h:980
int i
#define UR_COORD(cube, i)
Definition: cubedata.h:46

◆ cube_coord()

Datum cube_coord ( PG_FUNCTION_ARGS  )

Definition at line 1602 of file cube.c.

References DIM, ereport, errcode(), errmsg(), ERROR, IS_POINT, PG_GETARG_INT32, PG_GETARG_NDBOX_P, PG_RETURN_FLOAT8, and NDBOX::x.

1603 {
1604  NDBOX *cube = PG_GETARG_NDBOX_P(0);
1605  int coord = PG_GETARG_INT32(1);
1606 
1607  if (coord <= 0 || coord > 2 * DIM(cube))
1608  ereport(ERROR,
1609  (errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
1610  errmsg("cube index %d is out of bounds", coord)));
1611 
1612  if (IS_POINT(cube))
1613  PG_RETURN_FLOAT8(cube->x[(coord - 1) % DIM(cube)]);
1614  else
1615  PG_RETURN_FLOAT8(cube->x[coord - 1]);
1616 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define DIM(cube)
Definition: cubedata.h:42
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
#define ERROR
Definition: elog.h:46
#define IS_POINT(cube)
Definition: cubedata.h:40
#define ereport(elevel,...)
Definition: elog.h:157
Definition: cubedata.h:9
double x[FLEXIBLE_ARRAY_MEMBER]
Definition: cubedata.h:33
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ cube_coord_llur()

Datum cube_coord_llur ( PG_FUNCTION_ARGS  )

Definition at line 1643 of file cube.c.

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

1644 {
1645  NDBOX *cube = PG_GETARG_NDBOX_P(0);
1646  int coord = PG_GETARG_INT32(1);
1647  bool inverse = false;
1648  float8 result;
1649 
1650  /* 0 is the only unsupported coordinate value */
1651  if (coord == 0)
1652  ereport(ERROR,
1653  (errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
1654  errmsg("zero cube index is not defined")));
1655 
1656  /* Return inversed value for negative coordinate */
1657  if (coord < 0)
1658  {
1659  coord = -coord;
1660  inverse = true;
1661  }
1662 
1663  if (coord <= 2 * DIM(cube))
1664  {
1665  /* dimension index */
1666  int index = (coord - 1) / 2;
1667 
1668  /* whether this is upper bound (lower bound otherwise) */
1669  bool upper = ((coord - 1) % 2 == 1);
1670 
1671  if (IS_POINT(cube))
1672  {
1673  result = cube->x[index];
1674  }
1675  else
1676  {
1677  if (upper)
1678  result = Max(cube->x[index], cube->x[index + DIM(cube)]);
1679  else
1680  result = Min(cube->x[index], cube->x[index + DIM(cube)]);
1681  }
1682  }
1683  else
1684  {
1685  /*
1686  * Return zero if coordinate is out of bound. That reproduces logic
1687  * of how cubes with low dimension number are expanded during GiST
1688  * indexing.
1689  */
1690  result = 0.0;
1691  }
1692 
1693  /* Inverse value if needed */
1694  if (inverse)
1695  result = -result;
1696 
1697  PG_RETURN_FLOAT8(result);
1698 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define DIM(cube)
Definition: cubedata.h:42
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define Min(x, y)
Definition: c.h:986
int errcode(int sqlerrcode)
Definition: elog.c:698
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:77
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
Definition: type.h:89
#define ERROR
Definition: elog.h:46
double float8
Definition: c.h:565
#define IS_POINT(cube)
Definition: cubedata.h:40
#define ereport(elevel,...)
Definition: elog.h:157
#define Max(x, y)
Definition: c.h:980
Definition: cubedata.h:9
double x[FLEXIBLE_ARRAY_MEMBER]
Definition: cubedata.h:33
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ cube_dim()

Datum cube_dim ( PG_FUNCTION_ARGS  )

Definition at line 1553 of file cube.c.

References DIM, PG_FREE_IF_COPY, PG_GETARG_NDBOX_P, and PG_RETURN_INT32.

1554 {
1555  NDBOX *c = PG_GETARG_NDBOX_P(0);
1556  int dim = DIM(c);
1557 
1558  PG_FREE_IF_COPY(c, 0);
1559  PG_RETURN_INT32(dim);
1560 }
#define DIM(cube)
Definition: cubedata.h:42
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
char * c
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260

◆ cube_distance()

Datum cube_distance ( PG_FUNCTION_ARGS  )

Definition at line 1252 of file cube.c.

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

Referenced by g_cube_distance().

1253 {
1254  NDBOX *a = PG_GETARG_NDBOX_P(0),
1255  *b = PG_GETARG_NDBOX_P(1);
1256  bool swapped = false;
1257  double d,
1258  distance;
1259  int i;
1260 
1261  /* swap the box pointers if needed */
1262  if (DIM(a) < DIM(b))
1263  {
1264  NDBOX *tmp = b;
1265 
1266  b = a;
1267  a = tmp;
1268  swapped = true;
1269  }
1270 
1271  distance = 0.0;
1272  /* compute within the dimensions of (b) */
1273  for (i = 0; i < DIM(b); i++)
1274  {
1275  d = distance_1D(LL_COORD(a, i), UR_COORD(a, i), LL_COORD(b, i), UR_COORD(b, i));
1276  distance += d * d;
1277  }
1278 
1279  /* compute distance to zero for those dimensions in (a) absent in (b) */
1280  for (i = DIM(b); i < DIM(a); i++)
1281  {
1282  d = distance_1D(LL_COORD(a, i), UR_COORD(a, i), 0.0, 0.0);
1283  distance += d * d;
1284  }
1285 
1286  if (swapped)
1287  {
1288  PG_FREE_IF_COPY(b, 0);
1289  PG_FREE_IF_COPY(a, 1);
1290  }
1291  else
1292  {
1293  PG_FREE_IF_COPY(a, 0);
1294  PG_FREE_IF_COPY(b, 1);
1295  }
1296 
1297  PG_RETURN_FLOAT8(sqrt(distance));
1298 }
#define DIM(cube)
Definition: cubedata.h:42
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
static double distance_1D(double a1, double a2, double b1, double b2)
Definition: cube.c:1502
#define LL_COORD(cube, i)
Definition: cubedata.h:45
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
int i
#define UR_COORD(cube, i)
Definition: cubedata.h:46

◆ cube_enlarge()

Datum cube_enlarge ( PG_FUNCTION_ARGS  )

Definition at line 1702 of file cube.c.

References cube_is_point_internal(), CUBE_MAX_DIM, CUBE_SIZE, DIM, i, 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.

1703 {
1704  NDBOX *a = PG_GETARG_NDBOX_P(0);
1705  double r = PG_GETARG_FLOAT8(1);
1706  int32 n = PG_GETARG_INT32(2);
1707  NDBOX *result;
1708  int dim = 0;
1709  int size;
1710  int i,
1711  j;
1712 
1713  if (n > CUBE_MAX_DIM)
1714  n = CUBE_MAX_DIM;
1715  if (r > 0 && n > 0)
1716  dim = n;
1717  if (DIM(a) > dim)
1718  dim = DIM(a);
1719 
1720  size = CUBE_SIZE(dim);
1721  result = (NDBOX *) palloc0(size);
1722  SET_VARSIZE(result, size);
1723  SET_DIM(result, dim);
1724 
1725  for (i = 0, j = dim; i < DIM(a); i++, j++)
1726  {
1727  if (LL_COORD(a, i) >= UR_COORD(a, i))
1728  {
1729  result->x[i] = UR_COORD(a, i) - r;
1730  result->x[j] = LL_COORD(a, i) + r;
1731  }
1732  else
1733  {
1734  result->x[i] = LL_COORD(a, i) - r;
1735  result->x[j] = UR_COORD(a, i) + r;
1736  }
1737  if (result->x[i] > result->x[j])
1738  {
1739  result->x[i] = (result->x[i] + result->x[j]) / 2;
1740  result->x[j] = result->x[i];
1741  }
1742  }
1743  /* dim > a->dim only if r > 0 */
1744  for (; i < dim; i++, j++)
1745  {
1746  result->x[i] = -r;
1747  result->x[j] = r;
1748  }
1749 
1750  /*
1751  * Check if the result was in fact a point, and set the flag in the datum
1752  * accordingly. (we don't bother to repalloc it smaller)
1753  */
1754  if (cube_is_point_internal(result))
1755  {
1756  size = POINT_SIZE(dim);
1757  SET_VARSIZE(result, size);
1758  SET_POINT_BIT(result);
1759  }
1760 
1761  PG_FREE_IF_COPY(a, 0);
1762  PG_RETURN_NDBOX_P(result);
1763 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define DIM(cube)
Definition: cubedata.h:42
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
signed int int32
Definition: c.h:429
#define CUBE_MAX_DIM
Definition: cubedata.h:7
#define LL_COORD(cube, i)
Definition: cubedata.h:45
void * palloc0(Size size)
Definition: mcxt.c:1093
Definition: cubedata.h:9
#define PG_RETURN_NDBOX_P(x)
Definition: cubedata.h:54
double x[FLEXIBLE_ARRAY_MEMBER]
Definition: cubedata.h:33
#define POINT_SIZE(_dim)
Definition: cubedata.h:48
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define SET_POINT_BIT(cube)
Definition: cubedata.h:41
int i
#define SET_DIM(cube, _dim)
Definition: cubedata.h:43
#define CUBE_SIZE(_dim)
Definition: cubedata.h:49
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
static bool cube_is_point_internal(NDBOX *cube)
Definition: cube.c:1529
#define UR_COORD(cube, i)
Definition: cubedata.h:46

◆ cube_eq()

Datum cube_eq ( PG_FUNCTION_ARGS  )

Definition at line 1031 of file cube.c.

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

1032 {
1033  NDBOX *a = PG_GETARG_NDBOX_P(0),
1034  *b = PG_GETARG_NDBOX_P(1);
1035  int32 res;
1036 
1037  res = cube_cmp_v0(a, b);
1038 
1039  PG_FREE_IF_COPY(a, 0);
1040  PG_FREE_IF_COPY(b, 1);
1041  PG_RETURN_BOOL(res == 0);
1042 }
int32 cube_cmp_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:936
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
signed int int32
Definition: c.h:429
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260

◆ cube_f8()

Datum cube_f8 ( PG_FUNCTION_ARGS  )

Definition at line 1767 of file cube.c.

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

1768 {
1769  double x = PG_GETARG_FLOAT8(0);
1770  NDBOX *result;
1771  int size;
1772 
1773  size = POINT_SIZE(1);
1774  result = (NDBOX *) palloc0(size);
1775  SET_VARSIZE(result, size);
1776  SET_DIM(result, 1);
1777  SET_POINT_BIT(result);
1778  result->x[0] = x;
1779 
1780  PG_RETURN_NDBOX_P(result);
1781 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
void * palloc0(Size size)
Definition: mcxt.c:1093
Definition: cubedata.h:9
#define PG_RETURN_NDBOX_P(x)
Definition: cubedata.h:54
double x[FLEXIBLE_ARRAY_MEMBER]
Definition: cubedata.h:33
#define POINT_SIZE(_dim)
Definition: cubedata.h:48
#define SET_POINT_BIT(cube)
Definition: cubedata.h:41
#define SET_DIM(cube, _dim)
Definition: cubedata.h:43
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ cube_f8_f8()

Datum cube_f8_f8 ( PG_FUNCTION_ARGS  )

Definition at line 1785 of file cube.c.

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

1786 {
1787  double x0 = PG_GETARG_FLOAT8(0);
1788  double x1 = PG_GETARG_FLOAT8(1);
1789  NDBOX *result;
1790  int size;
1791 
1792  if (x0 == x1)
1793  {
1794  size = POINT_SIZE(1);
1795  result = (NDBOX *) palloc0(size);
1796  SET_VARSIZE(result, size);
1797  SET_DIM(result, 1);
1798  SET_POINT_BIT(result);
1799  result->x[0] = x0;
1800  }
1801  else
1802  {
1803  size = CUBE_SIZE(1);
1804  result = (NDBOX *) palloc0(size);
1805  SET_VARSIZE(result, size);
1806  SET_DIM(result, 1);
1807  result->x[0] = x0;
1808  result->x[1] = x1;
1809  }
1810 
1811  PG_RETURN_NDBOX_P(result);
1812 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
void * palloc0(Size size)
Definition: mcxt.c:1093
Definition: cubedata.h:9
#define PG_RETURN_NDBOX_P(x)
Definition: cubedata.h:54
double x[FLEXIBLE_ARRAY_MEMBER]
Definition: cubedata.h:33
#define POINT_SIZE(_dim)
Definition: cubedata.h:48
#define SET_POINT_BIT(cube)
Definition: cubedata.h:41
#define SET_DIM(cube, _dim)
Definition: cubedata.h:43
#define CUBE_SIZE(_dim)
Definition: cubedata.h:49
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342

◆ cube_ge()

Datum cube_ge ( PG_FUNCTION_ARGS  )

Definition at line 1106 of file cube.c.

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

1107 {
1108  NDBOX *a = PG_GETARG_NDBOX_P(0),
1109  *b = PG_GETARG_NDBOX_P(1);
1110  int32 res;
1111 
1112  res = cube_cmp_v0(a, b);
1113 
1114  PG_FREE_IF_COPY(a, 0);
1115  PG_FREE_IF_COPY(b, 1);
1116  PG_RETURN_BOOL(res >= 0);
1117 }
int32 cube_cmp_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:936
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
signed int int32
Definition: c.h:429
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260

◆ cube_gt()

Datum cube_gt ( PG_FUNCTION_ARGS  )

Definition at line 1076 of file cube.c.

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

1077 {
1078  NDBOX *a = PG_GETARG_NDBOX_P(0),
1079  *b = PG_GETARG_NDBOX_P(1);
1080  int32 res;
1081 
1082  res = cube_cmp_v0(a, b);
1083 
1084  PG_FREE_IF_COPY(a, 0);
1085  PG_FREE_IF_COPY(b, 1);
1086  PG_RETURN_BOOL(res > 0);
1087 }
int32 cube_cmp_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:936
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
signed int int32
Definition: c.h:429
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260

◆ cube_in()

Datum cube_in ( PG_FUNCTION_ARGS  )

Definition at line 118 of file cube.c.

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

119 {
120  char *str = PG_GETARG_CSTRING(0);
121  NDBOX *result;
122 
123  cube_scanner_init(str);
124 
125  if (cube_yyparse(&result) != 0)
126  cube_yyerror(&result, "cube parser failed");
127 
129 
130  PG_RETURN_NDBOX_P(result);
131 }
void cube_scanner_init(const char *str)
void cube_scanner_finish(void)
void cube_yyerror(NDBOX **result, const char *message) pg_attribute_noreturn()
Definition: cubedata.h:9
#define PG_RETURN_NDBOX_P(x)
Definition: cubedata.h:54
int cube_yyparse(NDBOX **result)
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277

◆ cube_inter()

Datum cube_inter ( PG_FUNCTION_ARGS  )

Definition at line 822 of file cube.c.

References cube_is_point_internal(), CUBE_SIZE, DIM, 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().

823 {
824  NDBOX *a = PG_GETARG_NDBOX_P(0);
825  NDBOX *b = PG_GETARG_NDBOX_P(1);
826  NDBOX *result;
827  bool swapped = false;
828  int i;
829  int dim;
830  int size;
831 
832  /* swap the arguments if needed, so that 'a' is always larger than 'b' */
833  if (DIM(a) < DIM(b))
834  {
835  NDBOX *tmp = b;
836 
837  b = a;
838  a = tmp;
839  swapped = true;
840  }
841  dim = DIM(a);
842 
843  size = CUBE_SIZE(dim);
844  result = (NDBOX *) palloc0(size);
845  SET_VARSIZE(result, size);
846  SET_DIM(result, dim);
847 
848  /* First compute intersection of the dimensions present in both args */
849  for (i = 0; i < DIM(b); i++)
850  {
851  result->x[i] = Max(Min(LL_COORD(a, i), UR_COORD(a, i)),
852  Min(LL_COORD(b, i), UR_COORD(b, i)));
853  result->x[i + DIM(a)] = Min(Max(LL_COORD(a, i), UR_COORD(a, i)),
854  Max(LL_COORD(b, i), UR_COORD(b, i)));
855  }
856  /* continue on the higher dimensions only present in 'a' */
857  for (; i < DIM(a); i++)
858  {
859  result->x[i] = Max(0,
860  Min(LL_COORD(a, i), UR_COORD(a, i))
861  );
862  result->x[i + DIM(a)] = Min(0,
863  Max(LL_COORD(a, i), UR_COORD(a, i))
864  );
865  }
866 
867  /*
868  * Check if the result was in fact a point, and set the flag in the datum
869  * accordingly. (we don't bother to repalloc it smaller)
870  */
871  if (cube_is_point_internal(result))
872  {
873  size = POINT_SIZE(dim);
874  result = repalloc(result, size);
875  SET_VARSIZE(result, size);
876  SET_POINT_BIT(result);
877  }
878 
879  if (swapped)
880  {
881  PG_FREE_IF_COPY(b, 0);
882  PG_FREE_IF_COPY(a, 1);
883  }
884  else
885  {
886  PG_FREE_IF_COPY(a, 0);
887  PG_FREE_IF_COPY(b, 1);
888  }
889 
890  /*
891  * Is it OK to return a non-null intersection for non-overlapping boxes?
892  */
893  PG_RETURN_NDBOX_P(result);
894 }
#define DIM(cube)
Definition: cubedata.h:42
#define Min(x, y)
Definition: c.h:986
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
#define LL_COORD(cube, i)
Definition: cubedata.h:45
void * palloc0(Size size)
Definition: mcxt.c:1093
#define Max(x, y)
Definition: c.h:980
Definition: cubedata.h:9
#define PG_RETURN_NDBOX_P(x)
Definition: cubedata.h:54
double x[FLEXIBLE_ARRAY_MEMBER]
Definition: cubedata.h:33
#define POINT_SIZE(_dim)
Definition: cubedata.h:48
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
#define SET_POINT_BIT(cube)
Definition: cubedata.h:41
int i
#define SET_DIM(cube, _dim)
Definition: cubedata.h:43
#define CUBE_SIZE(_dim)
Definition: cubedata.h:49
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
static bool cube_is_point_internal(NDBOX *cube)
Definition: cube.c:1529
#define UR_COORD(cube, i)
Definition: cubedata.h:46

◆ cube_is_point()

Datum cube_is_point ( PG_FUNCTION_ARGS  )

Definition at line 1518 of file cube.c.

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

1519 {
1520  NDBOX *cube = PG_GETARG_NDBOX_P(0);
1521  bool result;
1522 
1523  result = cube_is_point_internal(cube);
1524  PG_FREE_IF_COPY(cube, 0);
1525  PG_RETURN_BOOL(result);
1526 }
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
static bool cube_is_point_internal(NDBOX *cube)
Definition: cube.c:1529

◆ cube_is_point_internal()

static bool cube_is_point_internal ( NDBOX cube)
static

Definition at line 1529 of file cube.c.

References DIM, i, IS_POINT, LL_COORD, and UR_COORD.

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

1530 {
1531  int i;
1532 
1533  if (IS_POINT(cube))
1534  return true;
1535 
1536  /*
1537  * Even if the point-flag is not set, all the lower-left coordinates might
1538  * match the upper-right coordinates, so that the value is in fact a
1539  * point. Such values don't arise with current code - the point flag is
1540  * always set if appropriate - but they might be present on-disk in
1541  * clusters upgraded from pre-9.4 versions.
1542  */
1543  for (i = 0; i < DIM(cube); i++)
1544  {
1545  if (LL_COORD(cube, i) != UR_COORD(cube, i))
1546  return false;
1547  }
1548  return true;
1549 }
#define DIM(cube)
Definition: cubedata.h:42
#define LL_COORD(cube, i)
Definition: cubedata.h:45
#define IS_POINT(cube)
Definition: cubedata.h:40
int i
#define UR_COORD(cube, i)
Definition: cubedata.h:46

◆ cube_le()

Datum cube_le ( PG_FUNCTION_ARGS  )

Definition at line 1091 of file cube.c.

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

1092 {
1093  NDBOX *a = PG_GETARG_NDBOX_P(0),
1094  *b = PG_GETARG_NDBOX_P(1);
1095  int32 res;
1096 
1097  res = cube_cmp_v0(a, b);
1098 
1099  PG_FREE_IF_COPY(a, 0);
1100  PG_FREE_IF_COPY(b, 1);
1101  PG_RETURN_BOOL(res <= 0);
1102 }
int32 cube_cmp_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:936
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
signed int int32
Definition: c.h:429
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260

◆ cube_ll_coord()

Datum cube_ll_coord ( PG_FUNCTION_ARGS  )

Definition at line 1564 of file cube.c.

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

1565 {
1566  NDBOX *c = PG_GETARG_NDBOX_P(0);
1567  int n = PG_GETARG_INT32(1);
1568  double result;
1569 
1570  if (DIM(c) >= n && n > 0)
1571  result = Min(LL_COORD(c, n - 1), UR_COORD(c, n - 1));
1572  else
1573  result = 0;
1574 
1575  PG_FREE_IF_COPY(c, 0);
1576  PG_RETURN_FLOAT8(result);
1577 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define DIM(cube)
Definition: cubedata.h:42
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define Min(x, y)
Definition: c.h:986
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
#define LL_COORD(cube, i)
Definition: cubedata.h:45
char * c
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define UR_COORD(cube, i)
Definition: cubedata.h:46

◆ cube_lt()

Datum cube_lt ( PG_FUNCTION_ARGS  )

Definition at line 1061 of file cube.c.

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

1062 {
1063  NDBOX *a = PG_GETARG_NDBOX_P(0),
1064  *b = PG_GETARG_NDBOX_P(1);
1065  int32 res;
1066 
1067  res = cube_cmp_v0(a, b);
1068 
1069  PG_FREE_IF_COPY(a, 0);
1070  PG_FREE_IF_COPY(b, 1);
1071  PG_RETURN_BOOL(res < 0);
1072 }
int32 cube_cmp_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:936
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
signed int int32
Definition: c.h:429
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260

◆ cube_ne()

Datum cube_ne ( PG_FUNCTION_ARGS  )

Definition at line 1046 of file cube.c.

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

1047 {
1048  NDBOX *a = PG_GETARG_NDBOX_P(0),
1049  *b = PG_GETARG_NDBOX_P(1);
1050  int32 res;
1051 
1052  res = cube_cmp_v0(a, b);
1053 
1054  PG_FREE_IF_COPY(a, 0);
1055  PG_FREE_IF_COPY(b, 1);
1056  PG_RETURN_BOOL(res != 0);
1057 }
int32 cube_cmp_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:936
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
signed int int32
Definition: c.h:429
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260

◆ cube_out()

Datum cube_out ( PG_FUNCTION_ARGS  )

Definition at line 291 of file cube.c.

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

292 {
293  NDBOX *cube = PG_GETARG_NDBOX_P(0);
295  int dim = DIM(cube);
296  int i;
297 
298  initStringInfo(&buf);
299 
300  appendStringInfoChar(&buf, '(');
301  for (i = 0; i < dim; i++)
302  {
303  if (i > 0)
304  appendStringInfoString(&buf, ", ");
306  }
307  appendStringInfoChar(&buf, ')');
308 
309  if (!cube_is_point_internal(cube))
310  {
311  appendStringInfoString(&buf, ",(");
312  for (i = 0; i < dim; i++)
313  {
314  if (i > 0)
315  appendStringInfoString(&buf, ", ");
317  }
318  appendStringInfoChar(&buf, ')');
319  }
320 
321  PG_FREE_IF_COPY(cube, 0);
322  PG_RETURN_CSTRING(buf.data);
323 }
char * float8out_internal(double num)
Definition: float.c:541
#define DIM(cube)
Definition: cubedata.h:42
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
#define LL_COORD(cube, i)
Definition: cubedata.h:45
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
static char * buf
Definition: pg_test_fsync.c:68
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:188
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
Definition: cubedata.h:9
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
int i
static bool cube_is_point_internal(NDBOX *cube)
Definition: cube.c:1529
#define UR_COORD(cube, i)
Definition: cubedata.h:46

◆ cube_overlap()

Datum cube_overlap ( PG_FUNCTION_ARGS  )

Definition at line 1232 of file cube.c.

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

1233 {
1234  NDBOX *a = PG_GETARG_NDBOX_P(0),
1235  *b = PG_GETARG_NDBOX_P(1);
1236  bool res;
1237 
1238  res = cube_overlap_v0(a, b);
1239 
1240  PG_FREE_IF_COPY(a, 0);
1241  PG_FREE_IF_COPY(b, 1);
1242  PG_RETURN_BOOL(res);
1243 }
bool cube_overlap_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:1193
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260

◆ cube_overlap_v0()

bool cube_overlap_v0 ( NDBOX a,
NDBOX b 
)

Definition at line 1193 of file cube.c.

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

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

1194 {
1195  int i;
1196 
1197  if ((a == NULL) || (b == NULL))
1198  return false;
1199 
1200  /* swap the box pointers if needed */
1201  if (DIM(a) < DIM(b))
1202  {
1203  NDBOX *tmp = b;
1204 
1205  b = a;
1206  a = tmp;
1207  }
1208 
1209  /* compare within the dimensions of (b) */
1210  for (i = 0; i < DIM(b); i++)
1211  {
1212  if (Min(LL_COORD(a, i), UR_COORD(a, i)) > Max(LL_COORD(b, i), UR_COORD(b, i)))
1213  return false;
1214  if (Max(LL_COORD(a, i), UR_COORD(a, i)) < Min(LL_COORD(b, i), UR_COORD(b, i)))
1215  return false;
1216  }
1217 
1218  /* compare to zero those dimensions in (a) absent in (b) */
1219  for (i = DIM(b); i < DIM(a); i++)
1220  {
1221  if (Min(LL_COORD(a, i), UR_COORD(a, i)) > 0)
1222  return false;
1223  if (Max(LL_COORD(a, i), UR_COORD(a, i)) < 0)
1224  return false;
1225  }
1226 
1227  return true;
1228 }
#define DIM(cube)
Definition: cubedata.h:42
#define Min(x, y)
Definition: c.h:986
#define LL_COORD(cube, i)
Definition: cubedata.h:45
#define Max(x, y)
Definition: c.h:980
Definition: cubedata.h:9
int i
#define UR_COORD(cube, i)
Definition: cubedata.h:46

◆ cube_recv()

Datum cube_recv ( PG_FUNCTION_ARGS  )

Definition at line 351 of file cube.c.

References buf, CUBE_MAX_DIM, DIM_MASK, ereport, errcode(), errdetail(), errmsg(), ERROR, NDBOX::header, header(), i, offsetof, palloc(), PG_GETARG_POINTER, PG_RETURN_NDBOX_P, POINT_BIT, pq_getmsgfloat8(), pq_getmsgint(), SET_VARSIZE, and NDBOX::x.

352 {
354  int32 header;
355  int32 i,
356  nitems;
357  NDBOX *cube;
358 
359  header = pq_getmsgint(buf, sizeof(int32));
360  nitems = (header & DIM_MASK);
361  if (nitems > CUBE_MAX_DIM)
362  ereport(ERROR,
363  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
364  errmsg("cube dimension is too large"),
365  errdetail("A cube cannot have more than %d dimensions.",
366  CUBE_MAX_DIM)));
367  if ((header & POINT_BIT) == 0)
368  nitems += nitems;
369  cube = palloc(offsetof(NDBOX, x) + sizeof(double) * nitems);
370  SET_VARSIZE(cube, offsetof(NDBOX, x) + sizeof(double) * nitems);
371  cube->header = header;
372  for (i = 0; i < nitems; i++)
373  cube->x[i] = pq_getmsgfloat8(buf);
374 
375  PG_RETURN_NDBOX_P(cube);
376 }
unsigned int header
Definition: cubedata.h:27
StringInfoData * StringInfo
Definition: stringinfo.h:44
#define POINT_BIT
Definition: cubedata.h:37
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
signed int int32
Definition: c.h:429
#define CUBE_MAX_DIM
Definition: cubedata.h:7
#define ERROR
Definition: elog.h:46
static char * buf
Definition: pg_test_fsync.c:68
int errdetail(const char *fmt,...)
Definition: elog.c:1042
#define ereport(elevel,...)
Definition: elog.h:157
Definition: cubedata.h:9
float8 pq_getmsgfloat8(StringInfo msg)
Definition: pqformat.c:490
#define PG_RETURN_NDBOX_P(x)
Definition: cubedata.h:54
double x[FLEXIBLE_ARRAY_MEMBER]
Definition: cubedata.h:33
#define DIM_MASK
Definition: cubedata.h:38
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:210
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
#define offsetof(type, field)
Definition: c.h:727

◆ cube_send()

Datum cube_send ( PG_FUNCTION_ARGS  )

Definition at line 329 of file cube.c.

References buf, DIM, NDBOX::header, i, IS_POINT, PG_GETARG_NDBOX_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendfloat8(), pq_sendint32(), and NDBOX::x.

330 {
331  NDBOX *cube = PG_GETARG_NDBOX_P(0);
333  int32 i,
334  nitems = DIM(cube);
335 
336  pq_begintypsend(&buf);
337  pq_sendint32(&buf, cube->header);
338  if (!IS_POINT(cube))
339  nitems += nitems;
340  /* for symmetry with cube_recv, we don't use LL_COORD/UR_COORD here */
341  for (i = 0; i < nitems; i++)
342  pq_sendfloat8(&buf, cube->x[i]);
343 
345 }
unsigned int header
Definition: cubedata.h:27
#define DIM(cube)
Definition: cubedata.h:42
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
void pq_sendfloat8(StringInfo buf, float8 f)
Definition: pqformat.c:278
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
signed int int32
Definition: c.h:429
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:145
static char * buf
Definition: pg_test_fsync.c:68
#define IS_POINT(cube)
Definition: cubedata.h:40
Definition: cubedata.h:9
double x[FLEXIBLE_ARRAY_MEMBER]
Definition: cubedata.h:33
int i

◆ cube_size()

Datum cube_size ( PG_FUNCTION_ARGS  )

Definition at line 898 of file cube.c.

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

899 {
900  NDBOX *a = PG_GETARG_NDBOX_P(0);
901  double result;
902 
903  rt_cube_size(a, &result);
904  PG_FREE_IF_COPY(a, 0);
905  PG_RETURN_FLOAT8(result);
906 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
void rt_cube_size(NDBOX *a, double *sz)
Definition: cube.c:909
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260

◆ cube_subset()

Datum cube_subset ( PG_FUNCTION_ARGS  )

Definition at line 242 of file cube.c.

References ARR_DATA_PTR, array_contains_nulls(), ARRNELEMS, CUBE_MAX_DIM, CUBE_SIZE, DIM, ereport, errcode(), errdetail(), errmsg(), ERROR, 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.

243 {
244  NDBOX *c = PG_GETARG_NDBOX_P(0);
246  NDBOX *result;
247  int size,
248  dim,
249  i;
250  int *dx;
251 
252  if (array_contains_nulls(idx))
253  ereport(ERROR,
254  (errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
255  errmsg("cannot work with arrays containing NULLs")));
256 
257  dx = (int32 *) ARR_DATA_PTR(idx);
258 
259  dim = ARRNELEMS(idx);
260  if (dim > CUBE_MAX_DIM)
261  ereport(ERROR,
262  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
263  errmsg("array is too long"),
264  errdetail("A cube cannot have more than %d dimensions.",
265  CUBE_MAX_DIM)));
266 
267  size = IS_POINT(c) ? POINT_SIZE(dim) : CUBE_SIZE(dim);
268  result = (NDBOX *) palloc0(size);
269  SET_VARSIZE(result, size);
270  SET_DIM(result, dim);
271 
272  if (IS_POINT(c))
273  SET_POINT_BIT(result);
274 
275  for (i = 0; i < dim; i++)
276  {
277  if ((dx[i] <= 0) || (dx[i] > DIM(c)))
278  ereport(ERROR,
279  (errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
280  errmsg("Index out of bounds")));
281  result->x[i] = c->x[dx[i] - 1];
282  if (!IS_POINT(c))
283  result->x[i + dim] = c->x[dx[i] + DIM(c) - 1];
284  }
285 
286  PG_FREE_IF_COPY(c, 0);
287  PG_RETURN_NDBOX_P(result);
288 }
#define DIM(cube)
Definition: cubedata.h:42
int errcode(int sqlerrcode)
Definition: elog.c:698
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
signed int int32
Definition: c.h:429
#define CUBE_MAX_DIM
Definition: cubedata.h:7
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:256
#define ERROR
Definition: elog.h:46
#define ARR_DATA_PTR(a)
Definition: array.h:315
char * c
int errdetail(const char *fmt,...)
Definition: elog.c:1042
#define IS_POINT(cube)
Definition: cubedata.h:40
void * palloc0(Size size)
Definition: mcxt.c:1093
#define ereport(elevel,...)
Definition: elog.h:157
Definition: cubedata.h:9
#define PG_RETURN_NDBOX_P(x)
Definition: cubedata.h:54
double x[FLEXIBLE_ARRAY_MEMBER]
Definition: cubedata.h:33
#define POINT_SIZE(_dim)
Definition: cubedata.h:48
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define ARRNELEMS(x)
Definition: cube.c:26
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define SET_POINT_BIT(cube)
Definition: cubedata.h:41
int i
#define SET_DIM(cube, _dim)
Definition: cubedata.h:43
#define CUBE_SIZE(_dim)
Definition: cubedata.h:49
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
bool array_contains_nulls(ArrayType *array)
Definition: arrayfuncs.c:3557

◆ cube_union()

Datum cube_union ( PG_FUNCTION_ARGS  )

Definition at line 807 of file cube.c.

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

808 {
809  NDBOX *a = PG_GETARG_NDBOX_P(0);
810  NDBOX *b = PG_GETARG_NDBOX_P(1);
811  NDBOX *res;
812 
813  res = cube_union_v0(a, b);
814 
815  PG_FREE_IF_COPY(a, 0);
816  PG_FREE_IF_COPY(b, 1);
817  PG_RETURN_NDBOX_P(res);
818 }
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
Definition: cubedata.h:9
#define PG_RETURN_NDBOX_P(x)
Definition: cubedata.h:54
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
NDBOX * cube_union_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:747

◆ cube_union_v0()

NDBOX * cube_union_v0 ( NDBOX a,
NDBOX b 
)

Definition at line 747 of file cube.c.

References 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().

748 {
749  int i;
750  NDBOX *result;
751  int dim;
752  int size;
753 
754  /* trivial case */
755  if (a == b)
756  return a;
757 
758  /* swap the arguments if needed, so that 'a' is always larger than 'b' */
759  if (DIM(a) < DIM(b))
760  {
761  NDBOX *tmp = b;
762 
763  b = a;
764  a = tmp;
765  }
766  dim = DIM(a);
767 
768  size = CUBE_SIZE(dim);
769  result = palloc0(size);
770  SET_VARSIZE(result, size);
771  SET_DIM(result, dim);
772 
773  /* First compute the union of the dimensions present in both args */
774  for (i = 0; i < DIM(b); i++)
775  {
776  result->x[i] = Min(Min(LL_COORD(a, i), UR_COORD(a, i)),
777  Min(LL_COORD(b, i), UR_COORD(b, i)));
778  result->x[i + DIM(a)] = Max(Max(LL_COORD(a, i), UR_COORD(a, i)),
779  Max(LL_COORD(b, i), UR_COORD(b, i)));
780  }
781  /* continue on the higher dimensions only present in 'a' */
782  for (; i < DIM(a); i++)
783  {
784  result->x[i] = Min(0,
785  Min(LL_COORD(a, i), UR_COORD(a, i))
786  );
787  result->x[i + dim] = Max(0,
788  Max(LL_COORD(a, i), UR_COORD(a, i))
789  );
790  }
791 
792  /*
793  * Check if the result was in fact a point, and set the flag in the datum
794  * accordingly. (we don't bother to repalloc it smaller)
795  */
796  if (cube_is_point_internal(result))
797  {
798  size = POINT_SIZE(dim);
799  SET_VARSIZE(result, size);
800  SET_POINT_BIT(result);
801  }
802 
803  return result;
804 }
#define DIM(cube)
Definition: cubedata.h:42
#define Min(x, y)
Definition: c.h:986
#define LL_COORD(cube, i)
Definition: cubedata.h:45
void * palloc0(Size size)
Definition: mcxt.c:1093
#define Max(x, y)
Definition: c.h:980
Definition: cubedata.h:9
double x[FLEXIBLE_ARRAY_MEMBER]
Definition: cubedata.h:33
#define POINT_SIZE(_dim)
Definition: cubedata.h:48
#define SET_POINT_BIT(cube)
Definition: cubedata.h:41
int i
#define SET_DIM(cube, _dim)
Definition: cubedata.h:43
#define CUBE_SIZE(_dim)
Definition: cubedata.h:49
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:342
static bool cube_is_point_internal(NDBOX *cube)
Definition: cube.c:1529
#define UR_COORD(cube, i)
Definition: cubedata.h:46

◆ cube_ur_coord()

Datum cube_ur_coord ( PG_FUNCTION_ARGS  )

Definition at line 1581 of file cube.c.

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

1582 {
1583  NDBOX *c = PG_GETARG_NDBOX_P(0);
1584  int n = PG_GETARG_INT32(1);
1585  double result;
1586 
1587  if (DIM(c) >= n && n > 0)
1588  result = Max(LL_COORD(c, n - 1), UR_COORD(c, n - 1));
1589  else
1590  result = 0;
1591 
1592  PG_FREE_IF_COPY(c, 0);
1593  PG_RETURN_FLOAT8(result);
1594 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define DIM(cube)
Definition: cubedata.h:42
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
#define LL_COORD(cube, i)
Definition: cubedata.h:45
char * c
#define Max(x, y)
Definition: c.h:980
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define UR_COORD(cube, i)
Definition: cubedata.h:46

◆ distance_1D()

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

Definition at line 1502 of file cube.c.

References Max, and Min.

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

1503 {
1504  /* interval (a) is entirely on the left of (b) */
1505  if ((a1 <= b1) && (a2 <= b1) && (a1 <= b2) && (a2 <= b2))
1506  return (Min(b1, b2) - Max(a1, a2));
1507 
1508  /* interval (a) is entirely on the right of (b) */
1509  if ((a1 > b1) && (a2 > b1) && (a1 > b2) && (a2 > b2))
1510  return (Min(a1, a2) - Max(b1, b2));
1511 
1512  /* the rest are all sorts of intersections */
1513  return 0.0;
1514 }
#define Min(x, y)
Definition: c.h:986
static const FormData_pg_attribute a2
Definition: heap.c:166
#define Max(x, y)
Definition: c.h:980
static const FormData_pg_attribute a1
Definition: heap.c:152

◆ distance_chebyshev()

Datum distance_chebyshev ( PG_FUNCTION_ARGS  )

Definition at line 1345 of file cube.c.

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

Referenced by g_cube_distance().

1346 {
1347  NDBOX *a = PG_GETARG_NDBOX_P(0),
1348  *b = PG_GETARG_NDBOX_P(1);
1349  bool swapped = false;
1350  double d,
1351  distance;
1352  int i;
1353 
1354  /* swap the box pointers if needed */
1355  if (DIM(a) < DIM(b))
1356  {
1357  NDBOX *tmp = b;
1358 
1359  b = a;
1360  a = tmp;
1361  swapped = true;
1362  }
1363 
1364  distance = 0.0;
1365  /* compute within the dimensions of (b) */
1366  for (i = 0; i < DIM(b); i++)
1367  {
1368  d = fabs(distance_1D(LL_COORD(a, i), UR_COORD(a, i),
1369  LL_COORD(b, i), UR_COORD(b, i)));
1370  if (d > distance)
1371  distance = d;
1372  }
1373 
1374  /* compute distance to zero for those dimensions in (a) absent in (b) */
1375  for (i = DIM(b); i < DIM(a); i++)
1376  {
1377  d = fabs(distance_1D(LL_COORD(a, i), UR_COORD(a, i), 0.0, 0.0));
1378  if (d > distance)
1379  distance = d;
1380  }
1381 
1382  if (swapped)
1383  {
1384  PG_FREE_IF_COPY(b, 0);
1385  PG_FREE_IF_COPY(a, 1);
1386  }
1387  else
1388  {
1389  PG_FREE_IF_COPY(a, 0);
1390  PG_FREE_IF_COPY(b, 1);
1391  }
1392 
1393  PG_RETURN_FLOAT8(distance);
1394 }
#define DIM(cube)
Definition: cubedata.h:42
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
static double distance_1D(double a1, double a2, double b1, double b2)
Definition: cube.c:1502
#define LL_COORD(cube, i)
Definition: cubedata.h:45
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
int i
#define UR_COORD(cube, i)
Definition: cubedata.h:46

◆ distance_taxicab()

Datum distance_taxicab ( PG_FUNCTION_ARGS  )

Definition at line 1301 of file cube.c.

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

Referenced by g_cube_distance().

1302 {
1303  NDBOX *a = PG_GETARG_NDBOX_P(0),
1304  *b = PG_GETARG_NDBOX_P(1);
1305  bool swapped = false;
1306  double distance;
1307  int i;
1308 
1309  /* swap the box pointers if needed */
1310  if (DIM(a) < DIM(b))
1311  {
1312  NDBOX *tmp = b;
1313 
1314  b = a;
1315  a = tmp;
1316  swapped = true;
1317  }
1318 
1319  distance = 0.0;
1320  /* compute within the dimensions of (b) */
1321  for (i = 0; i < DIM(b); i++)
1322  distance += fabs(distance_1D(LL_COORD(a, i), UR_COORD(a, i),
1323  LL_COORD(b, i), UR_COORD(b, i)));
1324 
1325  /* compute distance to zero for those dimensions in (a) absent in (b) */
1326  for (i = DIM(b); i < DIM(a); i++)
1327  distance += fabs(distance_1D(LL_COORD(a, i), UR_COORD(a, i),
1328  0.0, 0.0));
1329 
1330  if (swapped)
1331  {
1332  PG_FREE_IF_COPY(b, 0);
1333  PG_FREE_IF_COPY(a, 1);
1334  }
1335  else
1336  {
1337  PG_FREE_IF_COPY(a, 0);
1338  PG_FREE_IF_COPY(b, 1);
1339  }
1340 
1341  PG_RETURN_FLOAT8(distance);
1342 }
#define DIM(cube)
Definition: cubedata.h:42
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
static double distance_1D(double a1, double a2, double b1, double b2)
Definition: cube.c:1502
#define LL_COORD(cube, i)
Definition: cubedata.h:45
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
int i
#define UR_COORD(cube, i)
Definition: cubedata.h:46

◆ g_cube_binary_union()

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

Definition at line 734 of file cube.c.

References cube_union_v0(), and VARSIZE.

Referenced by g_cube_union().

735 {
736  NDBOX *retval;
737 
738  retval = cube_union_v0(r1, r2);
739  *sizep = VARSIZE(retval);
740 
741  return retval;
742 }
#define VARSIZE(PTR)
Definition: postgres.h:316
Definition: cubedata.h:9
NDBOX * cube_union_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:747

◆ g_cube_compress()

Datum g_cube_compress ( PG_FUNCTION_ARGS  )

Definition at line 456 of file cube.c.

References PG_GETARG_DATUM, and PG_RETURN_DATUM.

457 {
459 }
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353

◆ g_cube_consistent()

Datum g_cube_consistent ( PG_FUNCTION_ARGS  )

Definition at line 390 of file cube.c.

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

391 {
392  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
393  NDBOX *query = PG_GETARG_NDBOX_P(1);
395 
396  /* Oid subtype = PG_GETARG_OID(3); */
397  bool *recheck = (bool *) PG_GETARG_POINTER(4);
398  bool res;
399 
400  /* All cases served by this function are exact */
401  *recheck = false;
402 
403  /*
404  * if entry is not leaf, use g_cube_internal_consistent, else use
405  * g_cube_leaf_consistent
406  */
407  if (GIST_LEAF(entry))
409  query, strategy);
410  else
412  query, strategy);
413 
414  PG_FREE_IF_COPY(query, 1);
415  PG_RETURN_BOOL(res);
416 }
#define GIST_LEAF(entry)
Definition: gist.h:170
#define DatumGetNDBOXP(x)
Definition: cubedata.h:52
bool g_cube_leaf_consistent(NDBOX *key, NDBOX *query, StrategyNumber strategy)
Definition: cube.c:678
uint16 StrategyNumber
Definition: stratnum.h:22
bool g_cube_internal_consistent(NDBOX *key, NDBOX *query, StrategyNumber strategy)
Definition: cube.c:707
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
Datum key
Definition: gist.h:160
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Definition: cubedata.h:9
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272

◆ g_cube_decompress()

Datum g_cube_decompress ( PG_FUNCTION_ARGS  )

Definition at line 462 of file cube.c.

References DatumGetNDBOXP, gistentryinit, sort-test::key, GISTENTRY::key, GISTENTRY::offset, GISTENTRY::page, palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, PointerGetDatum, and GISTENTRY::rel.

463 {
464  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
465  NDBOX *key = DatumGetNDBOXP(entry->key);
466 
467  if (key != DatumGetNDBOXP(entry->key))
468  {
469  GISTENTRY *retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
470 
471  gistentryinit(*retval, PointerGetDatum(key),
472  entry->rel, entry->page,
473  entry->offset, false);
474  PG_RETURN_POINTER(retval);
475  }
476  PG_RETURN_POINTER(entry);
477 }
Relation rel
Definition: gist.h:161
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define DatumGetNDBOXP(x)
Definition: cubedata.h:52
#define PointerGetDatum(X)
Definition: postgres.h:600
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
Page page
Definition: gist.h:162
Datum key
Definition: gist.h:160
Definition: cubedata.h:9
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:244
void * palloc(Size size)
Definition: mcxt.c:1062
OffsetNumber offset
Definition: gist.h:163

◆ g_cube_distance()

Datum g_cube_distance ( PG_FUNCTION_ARGS  )

Definition at line 1397 of file cube.c.

References cube_distance(), CubeKNNDistanceChebyshev, CubeKNNDistanceCoord, CubeKNNDistanceEuclid, CubeKNNDistanceTaxicab, DatumGetFloat8, DatumGetNDBOXP, DIM, DirectFunctionCall2, distance_chebyshev(), distance_taxicab(), elog, ereport, errcode(), errmsg(), ERROR, 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, upper(), and NDBOX::x.

1398 {
1399  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
1401  NDBOX *cube = DatumGetNDBOXP(entry->key);
1402  double retval;
1403 
1404  if (strategy == CubeKNNDistanceCoord)
1405  {
1406  /*
1407  * Handle ordering by ~> operator. See comments of cube_coord_llur()
1408  * for details
1409  */
1410  int coord = PG_GETARG_INT32(1);
1411  bool isLeaf = GistPageIsLeaf(entry->page);
1412  bool inverse = false;
1413 
1414  /* 0 is the only unsupported coordinate value */
1415  if (coord == 0)
1416  ereport(ERROR,
1417  (errcode(ERRCODE_ARRAY_ELEMENT_ERROR),
1418  errmsg("zero cube index is not defined")));
1419 
1420  /* Return inversed value for negative coordinate */
1421  if (coord < 0)
1422  {
1423  coord = -coord;
1424  inverse = true;
1425  }
1426 
1427  if (coord <= 2 * DIM(cube))
1428  {
1429  /* dimension index */
1430  int index = (coord - 1) / 2;
1431 
1432  /* whether this is upper bound (lower bound otherwise) */
1433  bool upper = ((coord - 1) % 2 == 1);
1434 
1435  if (IS_POINT(cube))
1436  {
1437  retval = cube->x[index];
1438  }
1439  else
1440  {
1441  if (isLeaf)
1442  {
1443  /* For leaf just return required upper/lower bound */
1444  if (upper)
1445  retval = Max(cube->x[index], cube->x[index + DIM(cube)]);
1446  else
1447  retval = Min(cube->x[index], cube->x[index + DIM(cube)]);
1448  }
1449  else
1450  {
1451  /*
1452  * For non-leaf we should always return lower bound,
1453  * because even upper bound of a child in the subtree can
1454  * be as small as our lower bound. For inversed case we
1455  * return upper bound because it becomes lower bound for
1456  * inversed value.
1457  */
1458  if (!inverse)
1459  retval = Min(cube->x[index], cube->x[index + DIM(cube)]);
1460  else
1461  retval = Max(cube->x[index], cube->x[index + DIM(cube)]);
1462  }
1463  }
1464  }
1465  else
1466  {
1467  retval = 0.0;
1468  }
1469 
1470  /* Inverse return value if needed */
1471  if (inverse)
1472  retval = -retval;
1473  }
1474  else
1475  {
1476  NDBOX *query = PG_GETARG_NDBOX_P(1);
1477 
1478  switch (strategy)
1479  {
1482  PointerGetDatum(cube), PointerGetDatum(query)));
1483  break;
1484  case CubeKNNDistanceEuclid:
1486  PointerGetDatum(cube), PointerGetDatum(query)));
1487  break;
1490  PointerGetDatum(cube), PointerGetDatum(query)));
1491  break;
1492  default:
1493  elog(ERROR, "unrecognized cube strategy number: %d", strategy);
1494  retval = 0; /* keep compiler quiet */
1495  break;
1496  }
1497  }
1498  PG_RETURN_FLOAT8(retval);
1499 }
Datum cube_distance(PG_FUNCTION_ARGS)
Definition: cube.c:1252
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
Datum distance_chebyshev(PG_FUNCTION_ARGS)
Definition: cube.c:1345
#define CubeKNNDistanceTaxicab
Definition: cubedata.h:58
#define DatumGetNDBOXP(x)
Definition: cubedata.h:52
#define PointerGetDatum(X)
Definition: postgres.h:600
#define DIM(cube)
Definition: cubedata.h:42
#define CubeKNNDistanceEuclid
Definition: cubedata.h:59
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define Min(x, y)
Definition: c.h:986
uint16 StrategyNumber
Definition: stratnum.h:22
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:77
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
Page page
Definition: gist.h:162
Definition: type.h:89
Datum distance_taxicab(PG_FUNCTION_ARGS)
Definition: cube.c:1301
#define CubeKNNDistanceChebyshev
Definition: cubedata.h:60
#define ERROR
Definition: elog.h:46
Datum key
Definition: gist.h:160
#define IS_POINT(cube)
Definition: cubedata.h:40
#define GistPageIsLeaf(page)
Definition: gist.h:169
#define DatumGetFloat8(X)
Definition: postgres.h:758
#define ereport(elevel,...)
Definition: elog.h:157
#define Max(x, y)
Definition: c.h:980
Definition: cubedata.h:9
double x[FLEXIBLE_ARRAY_MEMBER]
Definition: cubedata.h:33
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define CubeKNNDistanceCoord
Definition: cubedata.h:57
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628

◆ g_cube_internal_consistent()

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

Definition at line 707 of file cube.c.

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

Referenced by g_cube_consistent().

710 {
711  bool retval;
712 
713  switch (strategy)
714  {
716  retval = (bool) cube_overlap_v0(key, query);
717  break;
721  retval = (bool) cube_contains_v0(key, query);
722  break;
725  retval = (bool) cube_overlap_v0(key, query);
726  break;
727  default:
728  retval = false;
729  }
730  return retval;
731 }
#define RTOldContainsStrategyNumber
Definition: stratnum.h:63
#define RTOldContainedByStrategyNumber
Definition: stratnum.h:64
#define RTContainedByStrategyNumber
Definition: stratnum.h:58
bool cube_overlap_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:1193
#define RTSameStrategyNumber
Definition: stratnum.h:56
#define RTContainsStrategyNumber
Definition: stratnum.h:57
#define RTOverlapStrategyNumber
Definition: stratnum.h:53
bool cube_contains_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:1123
unsigned char bool
Definition: c.h:391

◆ g_cube_leaf_consistent()

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

Definition at line 678 of file cube.c.

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

Referenced by g_cube_consistent().

681 {
682  bool retval;
683 
684  switch (strategy)
685  {
687  retval = cube_overlap_v0(key, query);
688  break;
690  retval = (cube_cmp_v0(key, query) == 0);
691  break;
694  retval = cube_contains_v0(key, query);
695  break;
698  retval = cube_contains_v0(query, key);
699  break;
700  default:
701  retval = false;
702  }
703  return retval;
704 }
int32 cube_cmp_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:936
#define RTOldContainsStrategyNumber
Definition: stratnum.h:63
#define RTOldContainedByStrategyNumber
Definition: stratnum.h:64
#define RTContainedByStrategyNumber
Definition: stratnum.h:58
bool cube_overlap_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:1193
#define RTSameStrategyNumber
Definition: stratnum.h:56
#define RTContainsStrategyNumber
Definition: stratnum.h:57
#define RTOverlapStrategyNumber
Definition: stratnum.h:53
bool cube_contains_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:1123

◆ g_cube_penalty()

Datum g_cube_penalty ( PG_FUNCTION_ARGS  )

Definition at line 485 of file cube.c.

References cube_union_v0(), DatumGetNDBOXP, GISTENTRY::key, PG_GETARG_POINTER, PG_RETURN_FLOAT8, and rt_cube_size().

486 {
487  GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0);
488  GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
489  float *result = (float *) PG_GETARG_POINTER(2);
490  NDBOX *ud;
491  double tmp1,
492  tmp2;
493 
494  ud = cube_union_v0(DatumGetNDBOXP(origentry->key),
495  DatumGetNDBOXP(newentry->key));
496  rt_cube_size(ud, &tmp1);
497  rt_cube_size(DatumGetNDBOXP(origentry->key), &tmp2);
498  *result = (float) (tmp1 - tmp2);
499 
500  PG_RETURN_FLOAT8(*result);
501 }
#define DatumGetNDBOXP(x)
Definition: cubedata.h:52
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
Datum key
Definition: gist.h:160
void rt_cube_size(NDBOX *a, double *sz)
Definition: cube.c:909
Definition: cubedata.h:9
NDBOX * cube_union_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:747

◆ g_cube_picksplit()

Datum g_cube_picksplit ( PG_FUNCTION_ARGS  )

Definition at line 510 of file cube.c.

References cube_inter(), cube_union_v0(), DatumGetNDBOXP, DirectFunctionCall2, FirstOffsetNumber, i, GISTENTRY::key, GistEntryVector::n, 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, GIST_SPLITVEC::spl_right, and GistEntryVector::vector.

511 {
514  OffsetNumber i,
515  j;
516  NDBOX *datum_alpha,
517  *datum_beta;
518  NDBOX *datum_l,
519  *datum_r;
520  NDBOX *union_d,
521  *union_dl,
522  *union_dr;
523  NDBOX *inter_d;
524  bool firsttime;
525  double size_alpha,
526  size_beta,
527  size_union,
528  size_inter;
529  double size_waste,
530  waste;
531  double size_l,
532  size_r;
533  int nbytes;
534  OffsetNumber seed_1 = 1,
535  seed_2 = 2;
536  OffsetNumber *left,
537  *right;
538  OffsetNumber maxoff;
539 
540  maxoff = entryvec->n - 2;
541  nbytes = (maxoff + 2) * sizeof(OffsetNumber);
542  v->spl_left = (OffsetNumber *) palloc(nbytes);
543  v->spl_right = (OffsetNumber *) palloc(nbytes);
544 
545  firsttime = true;
546  waste = 0.0;
547 
548  for (i = FirstOffsetNumber; i < maxoff; i = OffsetNumberNext(i))
549  {
550  datum_alpha = DatumGetNDBOXP(entryvec->vector[i].key);
551  for (j = OffsetNumberNext(i); j <= maxoff; j = OffsetNumberNext(j))
552  {
553  datum_beta = DatumGetNDBOXP(entryvec->vector[j].key);
554 
555  /* compute the wasted space by unioning these guys */
556  /* size_waste = size_union - size_inter; */
557  union_d = cube_union_v0(datum_alpha, datum_beta);
558  rt_cube_size(union_d, &size_union);
560  entryvec->vector[i].key,
561  entryvec->vector[j].key));
562  rt_cube_size(inter_d, &size_inter);
563  size_waste = size_union - size_inter;
564 
565  /*
566  * are these a more promising split than what we've already seen?
567  */
568 
569  if (size_waste > waste || firsttime)
570  {
571  waste = size_waste;
572  seed_1 = i;
573  seed_2 = j;
574  firsttime = false;
575  }
576  }
577  }
578 
579  left = v->spl_left;
580  v->spl_nleft = 0;
581  right = v->spl_right;
582  v->spl_nright = 0;
583 
584  datum_alpha = DatumGetNDBOXP(entryvec->vector[seed_1].key);
585  datum_l = cube_union_v0(datum_alpha, datum_alpha);
586  rt_cube_size(datum_l, &size_l);
587  datum_beta = DatumGetNDBOXP(entryvec->vector[seed_2].key);
588  datum_r = cube_union_v0(datum_beta, datum_beta);
589  rt_cube_size(datum_r, &size_r);
590 
591  /*
592  * Now split up the regions between the two seeds. An important property
593  * of this split algorithm is that the split vector v has the indices of
594  * items to be split in order in its left and right vectors. We exploit
595  * this property by doing a merge in the code that actually splits the
596  * page.
597  *
598  * For efficiency, we also place the new index tuple in this loop. This is
599  * handled at the very end, when we have placed all the existing tuples
600  * and i == maxoff + 1.
601  */
602 
603  maxoff = OffsetNumberNext(maxoff);
604  for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
605  {
606  /*
607  * If we've already decided where to place this item, just put it on
608  * the right list. Otherwise, we need to figure out which page needs
609  * the least enlargement in order to store the item.
610  */
611 
612  if (i == seed_1)
613  {
614  *left++ = i;
615  v->spl_nleft++;
616  continue;
617  }
618  else if (i == seed_2)
619  {
620  *right++ = i;
621  v->spl_nright++;
622  continue;
623  }
624 
625  /* okay, which page needs least enlargement? */
626  datum_alpha = DatumGetNDBOXP(entryvec->vector[i].key);
627  union_dl = cube_union_v0(datum_l, datum_alpha);
628  union_dr = cube_union_v0(datum_r, datum_alpha);
629  rt_cube_size(union_dl, &size_alpha);
630  rt_cube_size(union_dr, &size_beta);
631 
632  /* pick which page to add it to */
633  if (size_alpha - size_l < size_beta - size_r)
634  {
635  datum_l = union_dl;
636  size_l = size_alpha;
637  *left++ = i;
638  v->spl_nleft++;
639  }
640  else
641  {
642  datum_r = union_dr;
643  size_r = size_beta;
644  *right++ = i;
645  v->spl_nright++;
646  }
647  }
648  *left = *right = FirstOffsetNumber; /* sentinel value */
649 
650  v->spl_ldatum = PointerGetDatum(datum_l);
651  v->spl_rdatum = PointerGetDatum(datum_r);
652 
654 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define DatumGetNDBOXP(x)
Definition: cubedata.h:52
#define PointerGetDatum(X)
Definition: postgres.h:600
OffsetNumber * spl_left
Definition: gist.h:142
Datum spl_rdatum
Definition: gist.h:149
int32 n
Definition: gist.h:235
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
int spl_nleft
Definition: gist.h:143
uint16 OffsetNumber
Definition: off.h:24
GISTENTRY vector[FLEXIBLE_ARRAY_MEMBER]
Definition: gist.h:236
int spl_nright
Definition: gist.h:148
Datum key
Definition: gist.h:160
Datum cube_inter(PG_FUNCTION_ARGS)
Definition: cube.c:822
#define FirstOffsetNumber
Definition: off.h:27
void rt_cube_size(NDBOX *a, double *sz)
Definition: cube.c:909
Definition: cubedata.h:9
Datum spl_ldatum
Definition: gist.h:144
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
OffsetNumber * spl_right
Definition: gist.h:147
NDBOX * cube_union_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:747
void * palloc(Size size)
Definition: mcxt.c:1062
int i
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:628

◆ g_cube_same()

Datum g_cube_same ( PG_FUNCTION_ARGS  )

Definition at line 660 of file cube.c.

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

661 {
662  NDBOX *b1 = PG_GETARG_NDBOX_P(0);
663  NDBOX *b2 = PG_GETARG_NDBOX_P(1);
664  bool *result = (bool *) PG_GETARG_POINTER(2);
665 
666  if (cube_cmp_v0(b1, b2) == 0)
667  *result = true;
668  else
669  *result = false;
670 
671  PG_RETURN_NDBOX_P(result);
672 }
int32 cube_cmp_v0(NDBOX *a, NDBOX *b)
Definition: cube.c:936
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_NDBOX_P(x)
Definition: cubedata.h:53
Definition: cubedata.h:9
#define PG_RETURN_NDBOX_P(x)
Definition: cubedata.h:54

◆ g_cube_union()

Datum g_cube_union ( PG_FUNCTION_ARGS  )

Definition at line 424 of file cube.c.

References DatumGetNDBOXP, g_cube_binary_union(), i, GISTENTRY::key, GistEntryVector::n, PG_GETARG_POINTER, PG_RETURN_POINTER, VARSIZE, and GistEntryVector::vector.

425 {
427  int *sizep = (int *) PG_GETARG_POINTER(1);
428  NDBOX *out = (NDBOX *) NULL;
429  NDBOX *tmp;
430  int i;
431 
432  tmp = DatumGetNDBOXP(entryvec->vector[0].key);
433 
434  /*
435  * sizep = sizeof(NDBOX); -- NDBOX has variable size
436  */
437  *sizep = VARSIZE(tmp);
438 
439  for (i = 1; i < entryvec->n; i++)
440  {
441  out = g_cube_binary_union(tmp,
442  DatumGetNDBOXP(entryvec->vector[i].key),
443  sizep);
444  tmp = out;
445  }
446 
447  PG_RETURN_POINTER(out);
448 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define DatumGetNDBOXP(x)
Definition: cubedata.h:52
#define VARSIZE(PTR)
Definition: postgres.h:316
int32 n
Definition: gist.h:235
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
GISTENTRY vector[FLEXIBLE_ARRAY_MEMBER]
Definition: gist.h:236
Datum key
Definition: gist.h:160
NDBOX * g_cube_binary_union(NDBOX *r1, NDBOX *r2, int *sizep)
Definition: cube.c:734
Definition: cubedata.h:9
int i

◆ PG_FUNCTION_INFO_V1() [1/42]

PG_FUNCTION_INFO_V1 ( cube_in  )

◆ 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_a_f8  )

◆ PG_FUNCTION_INFO_V1() [4/42]

PG_FUNCTION_INFO_V1 ( cube_out  )

◆ PG_FUNCTION_INFO_V1() [5/42]

PG_FUNCTION_INFO_V1 ( cube_send  )

◆ PG_FUNCTION_INFO_V1() [6/42]

PG_FUNCTION_INFO_V1 ( cube_recv  )

◆ PG_FUNCTION_INFO_V1() [7/42]

PG_FUNCTION_INFO_V1 ( cube_f8  )

◆ PG_FUNCTION_INFO_V1() [8/42]

PG_FUNCTION_INFO_V1 ( cube_f8_f8  )

◆ PG_FUNCTION_INFO_V1() [9/42]

PG_FUNCTION_INFO_V1 ( cube_c_f8  )

◆ PG_FUNCTION_INFO_V1() [10/42]

PG_FUNCTION_INFO_V1 ( cube_c_f8_f8  )

◆ PG_FUNCTION_INFO_V1() [11/42]

PG_FUNCTION_INFO_V1 ( cube_dim  )

◆ PG_FUNCTION_INFO_V1() [12/42]

PG_FUNCTION_INFO_V1 ( cube_ll_coord  )

◆ PG_FUNCTION_INFO_V1() [13/42]

PG_FUNCTION_INFO_V1 ( cube_ur_coord  )

◆ PG_FUNCTION_INFO_V1() [14/42]

PG_FUNCTION_INFO_V1 ( cube_coord  )

◆ PG_FUNCTION_INFO_V1() [15/42]

PG_FUNCTION_INFO_V1 ( cube_coord_llur  )

◆ PG_FUNCTION_INFO_V1() [16/42]

PG_FUNCTION_INFO_V1 ( cube_subset  )

◆ PG_FUNCTION_INFO_V1() [17/42]

PG_FUNCTION_INFO_V1 ( g_cube_consistent  )

◆ PG_FUNCTION_INFO_V1() [18/42]

PG_FUNCTION_INFO_V1 ( g_cube_compress  )

◆ PG_FUNCTION_INFO_V1() [19/42]

PG_FUNCTION_INFO_V1 ( g_cube_decompress  )

◆ PG_FUNCTION_INFO_V1() [20/42]

PG_FUNCTION_INFO_V1 ( g_cube_penalty  )

◆ PG_FUNCTION_INFO_V1() [21/42]

PG_FUNCTION_INFO_V1 ( g_cube_picksplit  )

◆ PG_FUNCTION_INFO_V1() [22/42]

PG_FUNCTION_INFO_V1 ( g_cube_union  )

◆ PG_FUNCTION_INFO_V1() [23/42]

PG_FUNCTION_INFO_V1 ( g_cube_same  )

◆ PG_FUNCTION_INFO_V1() [24/42]

PG_FUNCTION_INFO_V1 ( g_cube_distance  )

◆ PG_FUNCTION_INFO_V1() [25/42]

PG_FUNCTION_INFO_V1 ( cube_eq  )

◆ PG_FUNCTION_INFO_V1() [26/42]

PG_FUNCTION_INFO_V1 ( cube_ne  )

◆ PG_FUNCTION_INFO_V1() [27/42]

PG_FUNCTION_INFO_V1 ( cube_lt  )

◆ PG_FUNCTION_INFO_V1() [28/42]

PG_FUNCTION_INFO_V1 ( cube_gt  )

◆ PG_FUNCTION_INFO_V1() [29/42]

PG_FUNCTION_INFO_V1 ( cube_le  )

◆ PG_FUNCTION_INFO_V1() [30/42]

PG_FUNCTION_INFO_V1 ( cube_ge  )

◆ PG_FUNCTION_INFO_V1() [31/42]

PG_FUNCTION_INFO_V1 ( cube_cmp  )

◆ PG_FUNCTION_INFO_V1() [32/42]

PG_FUNCTION_INFO_V1 ( cube_contains  )

◆ PG_FUNCTION_INFO_V1() [33/42]

PG_FUNCTION_INFO_V1 ( cube_contained  )

◆ PG_FUNCTION_INFO_V1() [34/42]

PG_FUNCTION_INFO_V1 ( cube_overlap  )

◆ PG_FUNCTION_INFO_V1() [35/42]

PG_FUNCTION_INFO_V1 ( cube_union  )

◆ PG_FUNCTION_INFO_V1() [36/42]

PG_FUNCTION_INFO_V1 ( cube_inter  )

◆ PG_FUNCTION_INFO_V1() [37/42]

PG_FUNCTION_INFO_V1 ( cube_size  )

◆ PG_FUNCTION_INFO_V1() [38/42]

PG_FUNCTION_INFO_V1 ( distance_taxicab  )

◆ PG_FUNCTION_INFO_V1() [39/42]

PG_FUNCTION_INFO_V1 ( cube_distance  )

◆ PG_FUNCTION_INFO_V1() [40/42]

PG_FUNCTION_INFO_V1 ( distance_chebyshev  )

◆ PG_FUNCTION_INFO_V1() [41/42]

PG_FUNCTION_INFO_V1 ( cube_is_point  )

◆ PG_FUNCTION_INFO_V1() [42/42]

PG_FUNCTION_INFO_V1 ( cube_enlarge  )

◆ rt_cube_size()

void rt_cube_size ( NDBOX a,
double *  sz 
)

Definition at line 909 of file cube.c.

References Abs, DIM, i, IS_POINT, LL_COORD, and UR_COORD.

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

910 {
911  double result;
912  int i;
913 
914  if (a == (NDBOX *) NULL)
915  {
916  /* special case for GiST */
917  result = 0.0;
918  }
919  else if (IS_POINT(a) || DIM(a) == 0)
920  {
921  /* necessarily has zero size */
922  result = 0.0;
923  }
924  else
925  {
926  result = 1.0;
927  for (i = 0; i < DIM(a); i++)
928  result *= Abs(UR_COORD(a, i) - LL_COORD(a, i));
929  }
930  *size = result;
931 }
#define DIM(cube)
Definition: cubedata.h:42
#define Abs(x)
Definition: c.h:992
#define LL_COORD(cube, i)
Definition: cubedata.h:45
#define IS_POINT(cube)
Definition: cubedata.h:40
Definition: cubedata.h:9
int i
#define UR_COORD(cube, i)
Definition: cubedata.h:46

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 20 of file cube.c.