PostgreSQL Source Code  git master
ltree_op.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "access/htup_details.h"
#include "catalog/pg_statistic.h"
#include "ltree.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/selfuncs.h"
Include dependency graph for ltree_op.c:

Go to the source code of this file.

Macros

#define RUNCMP
 
#define DEFAULT_PARENT_SEL   0.001
 

Functions

 PG_FUNCTION_INFO_V1 (ltree_cmp)
 
 PG_FUNCTION_INFO_V1 (ltree_lt)
 
 PG_FUNCTION_INFO_V1 (ltree_le)
 
 PG_FUNCTION_INFO_V1 (ltree_eq)
 
 PG_FUNCTION_INFO_V1 (ltree_ne)
 
 PG_FUNCTION_INFO_V1 (ltree_ge)
 
 PG_FUNCTION_INFO_V1 (ltree_gt)
 
 PG_FUNCTION_INFO_V1 (nlevel)
 
 PG_FUNCTION_INFO_V1 (ltree_isparent)
 
 PG_FUNCTION_INFO_V1 (ltree_risparent)
 
 PG_FUNCTION_INFO_V1 (subltree)
 
 PG_FUNCTION_INFO_V1 (subpath)
 
 PG_FUNCTION_INFO_V1 (ltree_index)
 
 PG_FUNCTION_INFO_V1 (ltree_addltree)
 
 PG_FUNCTION_INFO_V1 (ltree_addtext)
 
 PG_FUNCTION_INFO_V1 (ltree_textadd)
 
 PG_FUNCTION_INFO_V1 (lca)
 
 PG_FUNCTION_INFO_V1 (ltree2text)
 
 PG_FUNCTION_INFO_V1 (text2ltree)
 
 PG_FUNCTION_INFO_V1 (ltreeparentsel)
 
int ltree_compare (const ltree *a, const ltree *b)
 
Datum ltree_cmp (PG_FUNCTION_ARGS)
 
Datum ltree_lt (PG_FUNCTION_ARGS)
 
Datum ltree_le (PG_FUNCTION_ARGS)
 
Datum ltree_eq (PG_FUNCTION_ARGS)
 
Datum ltree_ge (PG_FUNCTION_ARGS)
 
Datum ltree_gt (PG_FUNCTION_ARGS)
 
Datum ltree_ne (PG_FUNCTION_ARGS)
 
Datum nlevel (PG_FUNCTION_ARGS)
 
bool inner_isparent (const ltree *c, const ltree *p)
 
Datum ltree_isparent (PG_FUNCTION_ARGS)
 
Datum ltree_risparent (PG_FUNCTION_ARGS)
 
static ltreeinner_subltree (ltree *t, int32 startpos, int32 endpos)
 
Datum subltree (PG_FUNCTION_ARGS)
 
Datum subpath (PG_FUNCTION_ARGS)
 
static ltreeltree_concat (ltree *a, ltree *b)
 
Datum ltree_addltree (PG_FUNCTION_ARGS)
 
Datum ltree_addtext (PG_FUNCTION_ARGS)
 
Datum ltree_index (PG_FUNCTION_ARGS)
 
Datum ltree_textadd (PG_FUNCTION_ARGS)
 
ltreelca_inner (ltree **a, int len)
 
Datum lca (PG_FUNCTION_ARGS)
 
Datum text2ltree (PG_FUNCTION_ARGS)
 
Datum ltree2text (PG_FUNCTION_ARGS)
 
Datum ltreeparentsel (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Macro Definition Documentation

◆ DEFAULT_PARENT_SEL

#define DEFAULT_PARENT_SEL   0.001

Definition at line 562 of file ltree_op.c.

Referenced by ltreeparentsel().

◆ RUNCMP

#define RUNCMP
Value:
ltree *b = PG_GETARG_LTREE_P(1); \
int res = ltree_compare(a,b); \
PG_FREE_IF_COPY(a,0); \
PG_FREE_IF_COPY(b,1)
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:171
Definition: ltree.h:19
int ltree_compare(const ltree *a, const ltree *b)
Definition: ltree_op.c:42

Definition at line 76 of file ltree_op.c.

Referenced by ltree_cmp(), ltree_eq(), ltree_ge(), ltree_gt(), ltree_le(), ltree_lt(), and ltree_ne().

Function Documentation

◆ inner_isparent()

bool inner_isparent ( const ltree c,
const ltree p 
)

Definition at line 143 of file ltree_op.c.

References ltree_level::len, LEVEL_NEXT, LTREE_FIRST, ltree_level::name, and ltree::numlevel.

Referenced by ltree_consistent(), ltree_isparent(), and ltree_risparent().

144 {
145  ltree_level *cl = LTREE_FIRST(c);
146  ltree_level *pl = LTREE_FIRST(p);
147  int pn = p->numlevel;
148 
149  if (pn > c->numlevel)
150  return false;
151 
152  while (pn > 0)
153  {
154  if (cl->len != pl->len)
155  return false;
156  if (memcmp(cl->name, pl->name, cl->len) != 0)
157  return false;
158 
159  pn--;
160  cl = LEVEL_NEXT(cl);
161  pl = LEVEL_NEXT(pl);
162  }
163  return true;
164 }
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:13
uint16 len
Definition: ltree.h:12
#define LEVEL_NEXT(x)
Definition: ltree.h:17
uint16 numlevel
Definition: ltree.h:22
#define LTREE_FIRST(x)
Definition: ltree.h:27

◆ inner_subltree()

static ltree* inner_subltree ( ltree t,
int32  startpos,
int32  endpos 
)
static

Definition at line 192 of file ltree_op.c.

References endpos, ereport, errcode(), errmsg(), ERROR, i, LEVEL_NEXT, LTREE_FIRST, LTREE_HDRSIZE, ltree::numlevel, palloc0(), SET_VARSIZE, and startpos.

Referenced by subltree(), and subpath().

193 {
194  char *start = NULL,
195  *end = NULL;
196  ltree_level *ptr = LTREE_FIRST(t);
197  ltree *res;
198  int i;
199 
200  if (startpos < 0 || endpos < 0 || startpos >= t->numlevel || startpos > endpos)
201  ereport(ERROR,
202  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
203  errmsg("invalid positions")));
204 
205  if (endpos > t->numlevel)
206  endpos = t->numlevel;
207 
208  start = end = (char *) ptr;
209  for (i = 0; i < endpos; i++)
210  {
211  if (i == startpos)
212  start = (char *) ptr;
213  if (i == endpos - 1)
214  {
215  end = (char *) LEVEL_NEXT(ptr);
216  break;
217  }
218  ptr = LEVEL_NEXT(ptr);
219  }
220 
221  res = (ltree *) palloc0(LTREE_HDRSIZE + (end - start));
222  SET_VARSIZE(res, LTREE_HDRSIZE + (end - start));
223  res->numlevel = endpos - startpos;
224 
225  memcpy(LTREE_FIRST(res), start, end - start);
226 
227  return res;
228 }
#define LTREE_HDRSIZE
Definition: ltree.h:26
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ERROR
Definition: elog.h:43
static XLogRecPtr endpos
Definition: pg_receivewal.c:46
#define LEVEL_NEXT(x)
Definition: ltree.h:17
#define ereport(elevel, rest)
Definition: elog.h:141
Definition: ltree.h:19
uint16 numlevel
Definition: ltree.h:22
void * palloc0(Size size)
Definition: mcxt.c:980
static XLogRecPtr startpos
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define LTREE_FIRST(x)
Definition: ltree.h:27
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ lca()

Definition at line 494 of file ltree_op.c.

References i, lca_inner(), palloc(), pfree(), PG_FREE_IF_COPY, PG_GETARG_LTREE_P, PG_RETURN_NULL, and PG_RETURN_POINTER.

Referenced by fix_indexqual_clause().

495 {
496  int i;
497  ltree **a,
498  *res;
499 
500  a = (ltree **) palloc(sizeof(ltree *) * fcinfo->nargs);
501  for (i = 0; i < fcinfo->nargs; i++)
502  a[i] = PG_GETARG_LTREE_P(i);
503  res = lca_inner(a, (int) fcinfo->nargs);
504  for (i = 0; i < fcinfo->nargs; i++)
505  PG_FREE_IF_COPY(a[i], i);
506  pfree(a);
507 
508  if (res)
509  PG_RETURN_POINTER(res);
510  else
511  PG_RETURN_NULL();
512 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
ltree * lca_inner(ltree **a, int len)
Definition: ltree_op.c:419
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:171
void pfree(void *pointer)
Definition: mcxt.c:1056
Definition: ltree.h:19
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
void * palloc(Size size)
Definition: mcxt.c:949
int i
#define PG_RETURN_NULL()
Definition: fmgr.h:335

◆ lca_inner()

ltree* lca_inner ( ltree **  a,
int  len 
)

Definition at line 419 of file ltree_op.c.

References i, ltree_level::len, LEVEL_HDRSIZE, LEVEL_NEXT, LTREE_FIRST, LTREE_HDRSIZE, MAXALIGN, Min, ltree_level::name, ltree::numlevel, palloc0(), and SET_VARSIZE.

Referenced by _lca(), and lca().

420 {
421  int tmp,
422  num,
423  i,
424  reslen;
425  ltree **ptr;
426  ltree_level *l1,
427  *l2;
428  ltree *res;
429 
430  if (len <= 0)
431  return NULL; /* no inputs? */
432  if ((*a)->numlevel == 0)
433  return NULL; /* any empty input means NULL result */
434 
435  /* num is the length of the longest common ancestor so far */
436  num = (*a)->numlevel - 1;
437 
438  /* Compare each additional input to *a */
439  ptr = a + 1;
440  while (ptr - a < len)
441  {
442  if ((*ptr)->numlevel == 0)
443  return NULL;
444  else if ((*ptr)->numlevel == 1)
445  num = 0;
446  else
447  {
448  l1 = LTREE_FIRST(*a);
449  l2 = LTREE_FIRST(*ptr);
450  tmp = Min(num, (*ptr)->numlevel - 1);
451  num = 0;
452  for (i = 0; i < tmp; i++)
453  {
454  if (l1->len == l2->len &&
455  memcmp(l1->name, l2->name, l1->len) == 0)
456  num = i + 1;
457  else
458  break;
459  l1 = LEVEL_NEXT(l1);
460  l2 = LEVEL_NEXT(l2);
461  }
462  }
463  ptr++;
464  }
465 
466  /* Now compute size of result ... */
467  reslen = LTREE_HDRSIZE;
468  l1 = LTREE_FIRST(*a);
469  for (i = 0; i < num; i++)
470  {
471  reslen += MAXALIGN(l1->len + LEVEL_HDRSIZE);
472  l1 = LEVEL_NEXT(l1);
473  }
474 
475  /* ... and construct it by copying from *a */
476  res = (ltree *) palloc0(reslen);
477  SET_VARSIZE(res, reslen);
478  res->numlevel = num;
479 
480  l1 = LTREE_FIRST(*a);
481  l2 = LTREE_FIRST(res);
482 
483  for (i = 0; i < num; i++)
484  {
485  memcpy(l2, l1, MAXALIGN(l1->len + LEVEL_HDRSIZE));
486  l1 = LEVEL_NEXT(l1);
487  l2 = LEVEL_NEXT(l2);
488  }
489 
490  return res;
491 }
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:13
uint16 len
Definition: ltree.h:12
#define Min(x, y)
Definition: c.h:911
#define LTREE_HDRSIZE
Definition: ltree.h:26
#define LEVEL_NEXT(x)
Definition: ltree.h:17
Definition: ltree.h:19
uint16 numlevel
Definition: ltree.h:22
void * palloc0(Size size)
Definition: mcxt.c:980
#define MAXALIGN(LEN)
Definition: c.h:692
#define LTREE_FIRST(x)
Definition: ltree.h:27
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
#define LEVEL_HDRSIZE
Definition: ltree.h:16

◆ ltree2text()

Datum ltree2text ( PG_FUNCTION_ARGS  )

Definition at line 532 of file ltree_op.c.

References i, ltree_level::len, LEVEL_NEXT, LTREE_FIRST, ltree_level::name, ltree::numlevel, palloc(), PG_FREE_IF_COPY, PG_GETARG_LTREE_P, PG_RETURN_POINTER, SET_VARSIZE, VARDATA, VARHDRSZ, and VARSIZE.

533 {
534  ltree *in = PG_GETARG_LTREE_P(0);
535  char *ptr;
536  int i;
537  ltree_level *curlevel;
538  text *out;
539 
540  out = (text *) palloc(VARSIZE(in) + VARHDRSZ);
541  ptr = VARDATA(out);
542  curlevel = LTREE_FIRST(in);
543  for (i = 0; i < in->numlevel; i++)
544  {
545  if (i != 0)
546  {
547  *ptr = '.';
548  ptr++;
549  }
550  memcpy(ptr, curlevel->name, curlevel->len);
551  ptr += curlevel->len;
552  curlevel = LEVEL_NEXT(curlevel);
553  }
554 
555  SET_VARSIZE(out, ptr - ((char *) out));
556  PG_FREE_IF_COPY(in, 0);
557 
558  PG_RETURN_POINTER(out);
559 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:13
#define VARDATA(PTR)
Definition: postgres.h:302
uint16 len
Definition: ltree.h:12
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:562
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:171
#define LEVEL_NEXT(x)
Definition: ltree.h:17
Definition: ltree.h:19
uint16 numlevel
Definition: ltree.h:22
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
void * palloc(Size size)
Definition: mcxt.c:949
#define LTREE_FIRST(x)
Definition: ltree.h:27
int i
Definition: c.h:556
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ ltree_addltree()

Datum ltree_addltree ( PG_FUNCTION_ARGS  )

Definition at line 290 of file ltree_op.c.

References ltree_concat(), PG_FREE_IF_COPY, PG_GETARG_LTREE_P, and PG_RETURN_POINTER.

291 {
292  ltree *a = PG_GETARG_LTREE_P(0);
293  ltree *b = PG_GETARG_LTREE_P(1);
294  ltree *r;
295 
296  r = ltree_concat(a, b);
297  PG_FREE_IF_COPY(a, 0);
298  PG_FREE_IF_COPY(b, 1);
300 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:171
static ltree * ltree_concat(ltree *a, ltree *b)
Definition: ltree_op.c:274
Definition: ltree.h:19
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255

◆ ltree_addtext()

Datum ltree_addtext ( PG_FUNCTION_ARGS  )

Definition at line 303 of file ltree_op.c.

References DatumGetPointer, DirectFunctionCall1, ltree_concat(), ltree_in(), pfree(), PG_FREE_IF_COPY, PG_GETARG_LTREE_P, PG_GETARG_TEXT_PP, PG_RETURN_POINTER, PointerGetDatum, and text_to_cstring().

304 {
305  ltree *a = PG_GETARG_LTREE_P(0);
306  text *b = PG_GETARG_TEXT_PP(1);
307  char *s;
308  ltree *r,
309  *tmp;
310 
311  s = text_to_cstring(b);
312 
314  PointerGetDatum(s)));
315 
316  pfree(s);
317 
318  r = ltree_concat(a, tmp);
319 
320  pfree(tmp);
321 
322  PG_FREE_IF_COPY(a, 0);
323  PG_FREE_IF_COPY(b, 1);
325 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:171
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:615
Datum ltree_in(PG_FUNCTION_ARGS)
Definition: ltree_io.c:38
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
void pfree(void *pointer)
Definition: mcxt.c:1056
static ltree * ltree_concat(ltree *a, ltree *b)
Definition: ltree_op.c:274
Definition: ltree.h:19
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define DatumGetPointer(X)
Definition: postgres.h:549
char * text_to_cstring(const text *t)
Definition: varlena.c:204
Definition: c.h:556

◆ ltree_cmp()

Datum ltree_cmp ( PG_FUNCTION_ARGS  )

Definition at line 84 of file ltree_op.c.

References PG_RETURN_INT32, and RUNCMP.

85 {
86  RUNCMP;
87  PG_RETURN_INT32(res);
88 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define RUNCMP
Definition: ltree_op.c:76

◆ ltree_compare()

int ltree_compare ( const ltree a,
const ltree b 
)

Definition at line 42 of file ltree_op.c.

References ltree_level::len, LEVEL_NEXT, LTREE_FIRST, Min, ltree_level::name, and ltree::numlevel.

Referenced by gist_ischild(), gist_isparent(), ltree_consistent(), ltree_penalty(), ltree_picksplit(), ltree_union(), and treekey_cmp().

43 {
44  ltree_level *al = LTREE_FIRST(a);
45  ltree_level *bl = LTREE_FIRST(b);
46  int an = a->numlevel;
47  int bn = b->numlevel;
48 
49  while (an > 0 && bn > 0)
50  {
51  int res;
52 
53  if ((res = memcmp(al->name, bl->name, Min(al->len, bl->len))) == 0)
54  {
55  if (al->len != bl->len)
56  return (al->len - bl->len) * 10 * (an + 1);
57  }
58  else
59  {
60  if (res < 0)
61  res = -1;
62  else
63  res = 1;
64  return res * 10 * (an + 1);
65  }
66 
67  an--;
68  bn--;
69  al = LEVEL_NEXT(al);
70  bl = LEVEL_NEXT(bl);
71  }
72 
73  return (a->numlevel - b->numlevel) * 10 * (an + 1);
74 }
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:13
uint16 len
Definition: ltree.h:12
#define Min(x, y)
Definition: c.h:911
#define LEVEL_NEXT(x)
Definition: ltree.h:17
uint16 numlevel
Definition: ltree.h:22
#define LTREE_FIRST(x)
Definition: ltree.h:27

◆ ltree_concat()

static ltree* ltree_concat ( ltree a,
ltree b 
)
static

Definition at line 274 of file ltree_op.c.

References LTREE_FIRST, LTREE_HDRSIZE, ltree::numlevel, palloc0(), SET_VARSIZE, and VARSIZE.

Referenced by ltree_addltree(), ltree_addtext(), and ltree_textadd().

275 {
276  ltree *r;
277 
278  r = (ltree *) palloc0(VARSIZE(a) + VARSIZE(b) - LTREE_HDRSIZE);
280  r->numlevel = a->numlevel + b->numlevel;
281 
282  memcpy(LTREE_FIRST(r), LTREE_FIRST(a), VARSIZE(a) - LTREE_HDRSIZE);
283  memcpy(((char *) LTREE_FIRST(r)) + VARSIZE(a) - LTREE_HDRSIZE,
284  LTREE_FIRST(b),
285  VARSIZE(b) - LTREE_HDRSIZE);
286  return r;
287 }
#define VARSIZE(PTR)
Definition: postgres.h:303
#define LTREE_HDRSIZE
Definition: ltree.h:26
Definition: ltree.h:19
uint16 numlevel
Definition: ltree.h:22
void * palloc0(Size size)
Definition: mcxt.c:980
#define LTREE_FIRST(x)
Definition: ltree.h:27
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329

◆ ltree_eq()

Datum ltree_eq ( PG_FUNCTION_ARGS  )

Definition at line 105 of file ltree_op.c.

References PG_RETURN_BOOL, and RUNCMP.

106 {
107  RUNCMP;
108  PG_RETURN_BOOL((res == 0) ? true : false);
109 }
#define RUNCMP
Definition: ltree_op.c:76
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ ltree_ge()

Datum ltree_ge ( PG_FUNCTION_ARGS  )

Definition at line 112 of file ltree_op.c.

References PG_RETURN_BOOL, and RUNCMP.

113 {
114  RUNCMP;
115  PG_RETURN_BOOL((res >= 0) ? true : false);
116 }
#define RUNCMP
Definition: ltree_op.c:76
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ ltree_gt()

Datum ltree_gt ( PG_FUNCTION_ARGS  )

Definition at line 119 of file ltree_op.c.

References PG_RETURN_BOOL, and RUNCMP.

120 {
121  RUNCMP;
122  PG_RETURN_BOOL((res > 0) ? true : false);
123 }
#define RUNCMP
Definition: ltree_op.c:76
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ ltree_index()

Datum ltree_index ( PG_FUNCTION_ARGS  )

Definition at line 328 of file ltree_op.c.

References i, ltree_level::len, LEVEL_NEXT, LTREE_FIRST, ltree_level::name, ltree::numlevel, PG_FREE_IF_COPY, PG_GETARG_INT32, PG_GETARG_LTREE_P, PG_RETURN_INT32, and startptr.

329 {
330  ltree *a = PG_GETARG_LTREE_P(0);
331  ltree *b = PG_GETARG_LTREE_P(1);
332  int start = (fcinfo->nargs == 3) ? PG_GETARG_INT32(2) : 0;
333  int i,
334  j;
336  *aptr,
337  *bptr;
338  bool found = false;
339 
340  if (start < 0)
341  {
342  if (-start >= a->numlevel)
343  start = 0;
344  else
345  start = (int) (a->numlevel) + start;
346  }
347 
348  if (a->numlevel - start < b->numlevel || a->numlevel == 0 || b->numlevel == 0)
349  {
350  PG_FREE_IF_COPY(a, 0);
351  PG_FREE_IF_COPY(b, 1);
352  PG_RETURN_INT32(-1);
353  }
354 
355  startptr = LTREE_FIRST(a);
356  for (i = 0; i <= a->numlevel - b->numlevel; i++)
357  {
358  if (i >= start)
359  {
360  aptr = startptr;
361  bptr = LTREE_FIRST(b);
362  for (j = 0; j < b->numlevel; j++)
363  {
364  if (!(aptr->len == bptr->len && memcmp(aptr->name, bptr->name, aptr->len) == 0))
365  break;
366  aptr = LEVEL_NEXT(aptr);
367  bptr = LEVEL_NEXT(bptr);
368  }
369 
370  if (j == b->numlevel)
371  {
372  found = true;
373  break;
374  }
375  }
376  startptr = LEVEL_NEXT(startptr);
377  }
378 
379  if (!found)
380  i = -1;
381 
382  PG_FREE_IF_COPY(a, 0);
383  PG_FREE_IF_COPY(b, 1);
384  PG_RETURN_INT32(i);
385 }
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:13
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
uint16 len
Definition: ltree.h:12
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:171
#define LEVEL_NEXT(x)
Definition: ltree.h:17
Definition: ltree.h:19
uint16 numlevel
Definition: ltree.h:22
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define LTREE_FIRST(x)
Definition: ltree.h:27
int i
static XLogRecPtr startptr
Definition: basebackup.c:117

◆ ltree_isparent()

Datum ltree_isparent ( PG_FUNCTION_ARGS  )

Definition at line 167 of file ltree_op.c.

References inner_isparent(), PG_FREE_IF_COPY, PG_GETARG_LTREE_P, and PG_RETURN_BOOL.

Referenced by _ltree_extract_isparent(), and _ltree_isparent().

168 {
169  ltree *c = PG_GETARG_LTREE_P(1);
170  ltree *p = PG_GETARG_LTREE_P(0);
171  bool res = inner_isparent(c, p);
172 
173  PG_FREE_IF_COPY(c, 1);
174  PG_FREE_IF_COPY(p, 0);
175  PG_RETURN_BOOL(res);
176 }
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:171
bool inner_isparent(const ltree *c, const ltree *p)
Definition: ltree_op.c:143
char * c
Definition: ltree.h:19
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255

◆ ltree_le()

Datum ltree_le ( PG_FUNCTION_ARGS  )

Definition at line 98 of file ltree_op.c.

References PG_RETURN_BOOL, and RUNCMP.

99 {
100  RUNCMP;
101  PG_RETURN_BOOL((res <= 0) ? true : false);
102 }
#define RUNCMP
Definition: ltree_op.c:76
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ ltree_lt()

Datum ltree_lt ( PG_FUNCTION_ARGS  )

Definition at line 91 of file ltree_op.c.

References PG_RETURN_BOOL, and RUNCMP.

92 {
93  RUNCMP;
94  PG_RETURN_BOOL((res < 0) ? true : false);
95 }
#define RUNCMP
Definition: ltree_op.c:76
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ ltree_ne()

Datum ltree_ne ( PG_FUNCTION_ARGS  )

Definition at line 126 of file ltree_op.c.

References PG_RETURN_BOOL, and RUNCMP.

127 {
128  RUNCMP;
129  PG_RETURN_BOOL((res != 0) ? true : false);
130 }
#define RUNCMP
Definition: ltree_op.c:76
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349

◆ ltree_risparent()

Datum ltree_risparent ( PG_FUNCTION_ARGS  )

Definition at line 179 of file ltree_op.c.

References inner_isparent(), PG_FREE_IF_COPY, PG_GETARG_LTREE_P, and PG_RETURN_BOOL.

Referenced by _ltree_extract_risparent(), and _ltree_risparent().

180 {
181  ltree *c = PG_GETARG_LTREE_P(0);
182  ltree *p = PG_GETARG_LTREE_P(1);
183  bool res = inner_isparent(c, p);
184 
185  PG_FREE_IF_COPY(c, 0);
186  PG_FREE_IF_COPY(p, 1);
187  PG_RETURN_BOOL(res);
188 }
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:171
bool inner_isparent(const ltree *c, const ltree *p)
Definition: ltree_op.c:143
char * c
Definition: ltree.h:19
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255

◆ ltree_textadd()

Datum ltree_textadd ( PG_FUNCTION_ARGS  )

Definition at line 388 of file ltree_op.c.

References DatumGetPointer, DirectFunctionCall1, ltree_concat(), ltree_in(), pfree(), PG_FREE_IF_COPY, PG_GETARG_LTREE_P, PG_GETARG_TEXT_PP, PG_RETURN_POINTER, PointerGetDatum, and text_to_cstring().

389 {
390  ltree *a = PG_GETARG_LTREE_P(1);
391  text *b = PG_GETARG_TEXT_PP(0);
392  char *s;
393  ltree *r,
394  *tmp;
395 
396  s = text_to_cstring(b);
397 
399  PointerGetDatum(s)));
400 
401  pfree(s);
402 
403  r = ltree_concat(tmp, a);
404 
405  pfree(tmp);
406 
407  PG_FREE_IF_COPY(a, 1);
408  PG_FREE_IF_COPY(b, 0);
410 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PointerGetDatum(X)
Definition: postgres.h:556
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:171
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:615
Datum ltree_in(PG_FUNCTION_ARGS)
Definition: ltree_io.c:38
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
void pfree(void *pointer)
Definition: mcxt.c:1056
static ltree * ltree_concat(ltree *a, ltree *b)
Definition: ltree_op.c:274
Definition: ltree.h:19
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define DatumGetPointer(X)
Definition: postgres.h:549
char * text_to_cstring(const text *t)
Definition: varlena.c:204
Definition: c.h:556

◆ ltreeparentsel()

Datum ltreeparentsel ( PG_FUNCTION_ARGS  )

Definition at line 568 of file ltree_op.c.

References generate_unaccent_rules::args, CLAMP_PROBABILITY, DEFAULT_PARENT_SEL, fmgr_info(), get_opcode(), get_restriction_variable(), GETSTRUCT, HeapTupleIsValid, histogram_selectivity(), IsA, mcv_selectivity(), PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_FLOAT8, ReleaseVariableStats, and VariableStatData::statsTuple.

569 {
571  Oid operator = PG_GETARG_OID(1);
572  List *args = (List *) PG_GETARG_POINTER(2);
573  int varRelid = PG_GETARG_INT32(3);
574  VariableStatData vardata;
575  Node *other;
576  bool varonleft;
577  double selec;
578 
579  /*
580  * If expression is not variable <@ something or something <@ variable,
581  * then punt and return a default estimate.
582  */
583  if (!get_restriction_variable(root, args, varRelid,
584  &vardata, &other, &varonleft))
586 
587  /*
588  * If the something is a NULL constant, assume operator is strict and
589  * return zero, ie, operator will never return TRUE.
590  */
591  if (IsA(other, Const) &&
592  ((Const *) other)->constisnull)
593  {
594  ReleaseVariableStats(vardata);
595  PG_RETURN_FLOAT8(0.0);
596  }
597 
598  if (IsA(other, Const))
599  {
600  /* Variable is being compared to a known non-null constant */
601  Datum constval = ((Const *) other)->constvalue;
602  FmgrInfo contproc;
603  double mcvsum;
604  double mcvsel;
605  double nullfrac;
606  int hist_size;
607 
608  fmgr_info(get_opcode(operator), &contproc);
609 
610  /*
611  * Is the constant "<@" to any of the column's most common values?
612  */
613  mcvsel = mcv_selectivity(&vardata, &contproc, constval, varonleft,
614  &mcvsum);
615 
616  /*
617  * If the histogram is large enough, see what fraction of it the
618  * constant is "<@" to, and assume that's representative of the
619  * non-MCV population. Otherwise use the default selectivity for the
620  * non-MCV population.
621  */
622  selec = histogram_selectivity(&vardata, &contproc,
623  constval, varonleft,
624  10, 1, &hist_size);
625  if (selec < 0)
626  {
627  /* Nope, fall back on default */
628  selec = DEFAULT_PARENT_SEL;
629  }
630  else if (hist_size < 100)
631  {
632  /*
633  * For histogram sizes from 10 to 100, we combine the histogram
634  * and default selectivities, putting increasingly more trust in
635  * the histogram for larger sizes.
636  */
637  double hist_weight = hist_size / 100.0;
638 
639  selec = selec * hist_weight +
640  DEFAULT_PARENT_SEL * (1.0 - hist_weight);
641  }
642 
643  /* In any case, don't believe extremely small or large estimates. */
644  if (selec < 0.0001)
645  selec = 0.0001;
646  else if (selec > 0.9999)
647  selec = 0.9999;
648 
649  if (HeapTupleIsValid(vardata.statsTuple))
650  nullfrac = ((Form_pg_statistic) GETSTRUCT(vardata.statsTuple))->stanullfrac;
651  else
652  nullfrac = 0.0;
653 
654  /*
655  * Now merge the results from the MCV and histogram calculations,
656  * realizing that the histogram covers only the non-null values that
657  * are not listed in MCV.
658  */
659  selec *= 1.0 - nullfrac - mcvsum;
660  selec += mcvsel;
661  }
662  else
663  selec = DEFAULT_PARENT_SEL;
664 
665  ReleaseVariableStats(vardata);
666 
667  /* result should be in range, but make sure... */
668  CLAMP_PROBABILITY(selec);
669 
670  PG_RETURN_FLOAT8((float8) selec);
671 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
Definition: fmgr.h:56
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
HeapTuple statsTuple
Definition: selfuncs.h:71
bool get_restriction_variable(PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)
Definition: selfuncs.c:4296
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:356
Definition: nodes.h:525
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:271
unsigned int Oid
Definition: postgres_ext.h:31
#define DEFAULT_PARENT_SEL
Definition: ltree_op.c:562
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:134
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
double float8
Definition: c.h:492
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
uintptr_t Datum
Definition: postgres.h:367
double histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval, bool varonleft, int min_hist_size, int n_skip, int *hist_size)
Definition: selfuncs.c:779
double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval, bool varonleft, double *sumcommonp)
Definition: selfuncs.c:707
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1092
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
Definition: pg_list.h:50

◆ nlevel()

Datum nlevel ( PG_FUNCTION_ARGS  )

Definition at line 133 of file ltree_op.c.

References ltree::numlevel, PG_FREE_IF_COPY, PG_GETARG_LTREE_P, and PG_RETURN_INT32.

134 {
135  ltree *a = PG_GETARG_LTREE_P(0);
136  int res = a->numlevel;
137 
138  PG_FREE_IF_COPY(a, 0);
139  PG_RETURN_INT32(res);
140 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:344
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:171
Definition: ltree.h:19
uint16 numlevel
Definition: ltree.h:22
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255

◆ PG_FUNCTION_INFO_V1() [1/20]

PG_FUNCTION_INFO_V1 ( ltree_cmp  )

◆ PG_FUNCTION_INFO_V1() [2/20]

PG_FUNCTION_INFO_V1 ( ltree_lt  )

◆ PG_FUNCTION_INFO_V1() [3/20]

PG_FUNCTION_INFO_V1 ( ltree_le  )

◆ PG_FUNCTION_INFO_V1() [4/20]

PG_FUNCTION_INFO_V1 ( ltree_eq  )

◆ PG_FUNCTION_INFO_V1() [5/20]

PG_FUNCTION_INFO_V1 ( ltree_ne  )

◆ PG_FUNCTION_INFO_V1() [6/20]

PG_FUNCTION_INFO_V1 ( ltree_ge  )

◆ PG_FUNCTION_INFO_V1() [7/20]

PG_FUNCTION_INFO_V1 ( ltree_gt  )

◆ PG_FUNCTION_INFO_V1() [8/20]

PG_FUNCTION_INFO_V1 ( nlevel  )

◆ PG_FUNCTION_INFO_V1() [9/20]

PG_FUNCTION_INFO_V1 ( ltree_isparent  )

◆ PG_FUNCTION_INFO_V1() [10/20]

PG_FUNCTION_INFO_V1 ( ltree_risparent  )

◆ PG_FUNCTION_INFO_V1() [11/20]

PG_FUNCTION_INFO_V1 ( subltree  )

◆ PG_FUNCTION_INFO_V1() [12/20]

PG_FUNCTION_INFO_V1 ( subpath  )

◆ PG_FUNCTION_INFO_V1() [13/20]

PG_FUNCTION_INFO_V1 ( ltree_index  )

◆ PG_FUNCTION_INFO_V1() [14/20]

PG_FUNCTION_INFO_V1 ( ltree_addltree  )

◆ PG_FUNCTION_INFO_V1() [15/20]

PG_FUNCTION_INFO_V1 ( ltree_addtext  )

◆ PG_FUNCTION_INFO_V1() [16/20]

PG_FUNCTION_INFO_V1 ( ltree_textadd  )

◆ PG_FUNCTION_INFO_V1() [17/20]

PG_FUNCTION_INFO_V1 ( lca  )

◆ PG_FUNCTION_INFO_V1() [18/20]

PG_FUNCTION_INFO_V1 ( ltree2text  )

◆ PG_FUNCTION_INFO_V1() [19/20]

PG_FUNCTION_INFO_V1 ( text2ltree  )

◆ PG_FUNCTION_INFO_V1() [20/20]

PG_FUNCTION_INFO_V1 ( ltreeparentsel  )

◆ subltree()

Datum subltree ( PG_FUNCTION_ARGS  )

Definition at line 231 of file ltree_op.c.

References inner_subltree(), PG_FREE_IF_COPY, PG_GETARG_INT32, PG_GETARG_LTREE_P, and PG_RETURN_POINTER.

232 {
233  ltree *t = PG_GETARG_LTREE_P(0);
235 
236  PG_FREE_IF_COPY(t, 0);
237  PG_RETURN_POINTER(res);
238 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:171
Definition: ltree.h:19
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
static ltree * inner_subltree(ltree *t, int32 startpos, int32 endpos)
Definition: ltree_op.c:192

◆ subpath()

Datum subpath ( PG_FUNCTION_ARGS  )

Definition at line 241 of file ltree_op.c.

References inner_subltree(), ltree::numlevel, PG_FREE_IF_COPY, PG_GETARG_INT32, PG_GETARG_LTREE_P, and PG_RETURN_POINTER.

Referenced by _outAggPath(), _outGatherMergePath(), _outGatherPath(), _outGroupingSetsPath(), _outGroupPath(), _outLimitPath(), _outLockRowsPath(), _outMaterialPath(), _outProjectionPath(), _outProjectSetPath(), _outSetOpPath(), _outSortPath(), _outSubqueryScanPath(), _outUniquePath(), _outUpperUniquePath(), _outWindowAggPath(), add_foreign_final_paths(), add_paths_to_append_rel(), adjust_paths_for_srfs(), append_nonpartial_cost(), apply_scanjoin_target_to_paths(), cost_append(), cost_bitmap_and_node(), cost_bitmap_or_node(), create_agg_path(), create_append_path(), create_append_plan(), create_gather_merge_path(), create_gather_path(), create_group_path(), create_groupingsets_path(), create_limit_path(), create_lockrows_path(), create_material_path(), create_merge_append_path(), create_merge_append_plan(), create_modifytable_path(), create_modifytable_plan(), create_projection_path(), create_set_projection_path(), create_sort_path(), create_subqueryscan_path(), create_unique_path(), create_upper_unique_path(), create_windowagg_path(), ExecSupportsMarkRestore(), generate_gather_paths(), generate_partitionwise_join_paths(), inheritance_planner(), is_dummy_rel(), recurse_set_operations(), set_subquery_pathlist(), and walkdir().

242 {
243  ltree *t = PG_GETARG_LTREE_P(0);
244  int32 start = PG_GETARG_INT32(1);
245  int32 len = (fcinfo->nargs == 3) ? PG_GETARG_INT32(2) : 0;
246  int32 end;
247  ltree *res;
248 
249  end = start + len;
250 
251  if (start < 0)
252  {
253  start = t->numlevel + start;
254  end = start + len;
255  }
256  if (start < 0)
257  { /* start > t->numlevel */
258  start = t->numlevel + start;
259  end = start + len;
260  }
261 
262  if (len < 0)
263  end = t->numlevel + len;
264  else if (len == 0)
265  end = (fcinfo->nargs == 3) ? start : 0xffff;
266 
267  res = inner_subltree(t, start, end);
268 
269  PG_FREE_IF_COPY(t, 0);
270  PG_RETURN_POINTER(res);
271 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define PG_GETARG_LTREE_P(n)
Definition: ltree.h:171
signed int int32
Definition: c.h:347
Definition: ltree.h:19
uint16 numlevel
Definition: ltree.h:22
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
static ltree * inner_subltree(ltree *t, int32 startpos, int32 endpos)
Definition: ltree_op.c:192

◆ text2ltree()

Datum text2ltree ( PG_FUNCTION_ARGS  )

Definition at line 515 of file ltree_op.c.

References DatumGetPointer, DirectFunctionCall1, ltree_in(), pfree(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_POINTER, PointerGetDatum, and text_to_cstring().

516 {
517  text *in = PG_GETARG_TEXT_PP(0);
518  char *s;
519  ltree *out;
520 
521  s = text_to_cstring(in);
522 
524  PointerGetDatum(s)));
525  pfree(s);
526  PG_FREE_IF_COPY(in, 0);
527  PG_RETURN_POINTER(out);
528 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
#define PointerGetDatum(X)
Definition: postgres.h:556
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:615
Datum ltree_in(PG_FUNCTION_ARGS)
Definition: ltree_io.c:38
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
void pfree(void *pointer)
Definition: mcxt.c:1056
Definition: ltree.h:19
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:255
#define DatumGetPointer(X)
Definition: postgres.h:549
char * text_to_cstring(const text *t)
Definition: varlena.c:204
Definition: c.h:556

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 17 of file ltree_op.c.