PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ltree_op.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "access/htup_details.h"
#include "catalog/pg_statistic.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/selfuncs.h"
#include "ltree.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

#define DEFAULT_PARENT_SEL   0.001

Definition at line 539 of file ltree_op.c.

Referenced by ltreeparentsel().

#define RUNCMP
Value:
ltree *b = PG_GETARG_LTREE(1); \
int res = ltree_compare(a,b); \
#define PG_GETARG_LTREE(x)
Definition: ltree.h:168
Definition: ltree.h:19
int ltree_compare(const ltree *a, const ltree *b)
Definition: ltree_op.c:42
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225

Definition at line 69 of file ltree_op.c.

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

Function Documentation

bool inner_isparent ( const ltree c,
const ltree p 
)

Definition at line 136 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().

137 {
138  ltree_level *cl = LTREE_FIRST(c);
139  ltree_level *pl = LTREE_FIRST(p);
140  int pn = p->numlevel;
141 
142  if (pn > c->numlevel)
143  return false;
144 
145  while (pn > 0)
146  {
147  if (cl->len != pl->len)
148  return false;
149  if (memcmp(cl->name, pl->name, cl->len))
150  return false;
151 
152  pn--;
153  cl = LEVEL_NEXT(cl);
154  pl = LEVEL_NEXT(pl);
155  }
156  return true;
157 }
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
static ltree* inner_subltree ( ltree t,
int32  startpos,
int32  endpos 
)
static

Definition at line 185 of file ltree_op.c.

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

Referenced by subltree(), and subpath().

186 {
187  char *start = NULL,
188  *end = NULL;
189  ltree_level *ptr = LTREE_FIRST(t);
190  ltree *res;
191  int i;
192 
193  if (startpos < 0 || endpos < 0 || startpos >= t->numlevel || startpos > endpos)
194  ereport(ERROR,
195  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
196  errmsg("invalid positions")));
197 
198  if (endpos > t->numlevel)
199  endpos = t->numlevel;
200 
201  start = end = (char *) ptr;
202  for (i = 0; i < endpos; i++)
203  {
204  if (i == startpos)
205  start = (char *) ptr;
206  if (i == endpos - 1)
207  {
208  end = (char *) LEVEL_NEXT(ptr);
209  break;
210  }
211  ptr = LEVEL_NEXT(ptr);
212  }
213 
214  res = (ltree *) palloc0(LTREE_HDRSIZE + (end - start));
215  SET_VARSIZE(res, LTREE_HDRSIZE + (end - start));
216  res->numlevel = endpos - startpos;
217 
218  memcpy(LTREE_FIRST(res), start, end - start);
219 
220  return res;
221 }
#define LTREE_HDRSIZE
Definition: ltree.h:26
int errcode(int sqlerrcode)
Definition: elog.c:575
static XLogRecPtr endpos
#define ERROR
Definition: elog.h:43
#define LEVEL_NEXT(x)
Definition: ltree.h:17
#define ereport(elevel, rest)
Definition: elog.h:122
Definition: ltree.h:19
uint16 numlevel
Definition: ltree.h:22
void * palloc0(Size size)
Definition: mcxt.c:878
#define NULL
Definition: c.h:229
static XLogRecPtr startpos
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define LTREE_FIRST(x)
Definition: ltree.h:27
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328

Definition at line 471 of file ltree_op.c.

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

Referenced by fix_indexqual_references(), and pg_import_system_collations().

472 {
473  int i;
474  ltree **a,
475  *res;
476 
477  a = (ltree **) palloc(sizeof(ltree *) * fcinfo->nargs);
478  for (i = 0; i < fcinfo->nargs; i++)
479  a[i] = PG_GETARG_LTREE(i);
480  res = lca_inner(a, (int) fcinfo->nargs);
481  for (i = 0; i < fcinfo->nargs; i++)
482  PG_FREE_IF_COPY(a[i], i);
483  pfree(a);
484 
485  if (res)
486  PG_RETURN_POINTER(res);
487  else
488  PG_RETURN_NULL();
489 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
ltree * lca_inner(ltree **a, int len)
Definition: ltree_op.c:406
#define PG_GETARG_LTREE(x)
Definition: ltree.h:168
void pfree(void *pointer)
Definition: mcxt.c:950
Definition: ltree.h:19
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
void * palloc(Size size)
Definition: mcxt.c:849
int i
#define PG_RETURN_NULL()
Definition: fmgr.h:305
ltree* lca_inner ( ltree **  a,
int  len 
)

Definition at line 406 of file ltree_op.c.

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

Referenced by _lca(), and lca().

407 {
408  int tmp,
409  num = ((*a)->numlevel) ? (*a)->numlevel - 1 : 0;
410  ltree **ptr = a + 1;
411  int i,
412  reslen = LTREE_HDRSIZE;
413  ltree_level *l1,
414  *l2;
415  ltree *res;
416 
417 
418  if ((*a)->numlevel == 0)
419  return NULL;
420 
421  while (ptr - a < len)
422  {
423  if ((*ptr)->numlevel == 0)
424  return NULL;
425  else if ((*ptr)->numlevel == 1)
426  num = 0;
427  else
428  {
429  l1 = LTREE_FIRST(*a);
430  l2 = LTREE_FIRST(*ptr);
431  tmp = num;
432  num = 0;
433  for (i = 0; i < Min(tmp, (*ptr)->numlevel - 1); i++)
434  {
435  if (l1->len == l2->len && memcmp(l1->name, l2->name, l1->len) == 0)
436  num = i + 1;
437  else
438  break;
439  l1 = LEVEL_NEXT(l1);
440  l2 = LEVEL_NEXT(l2);
441  }
442  }
443  ptr++;
444  }
445 
446  l1 = LTREE_FIRST(*a);
447  for (i = 0; i < num; i++)
448  {
449  reslen += MAXALIGN(l1->len + LEVEL_HDRSIZE);
450  l1 = LEVEL_NEXT(l1);
451  }
452 
453  res = (ltree *) palloc0(reslen);
454  SET_VARSIZE(res, reslen);
455  res->numlevel = num;
456 
457  l1 = LTREE_FIRST(*a);
458  l2 = LTREE_FIRST(res);
459 
460  for (i = 0; i < num; i++)
461  {
462  memcpy(l2, l1, MAXALIGN(l1->len + LEVEL_HDRSIZE));
463  l1 = LEVEL_NEXT(l1);
464  l2 = LEVEL_NEXT(l2);
465  }
466 
467  return res;
468 }
#define Min(x, y)
Definition: c.h:806
#define LTREE_HDRSIZE
Definition: ltree.h:26
#define LEVEL_NEXT(x)
Definition: ltree.h:17
Definition: ltree.h:19
void * palloc0(Size size)
Definition: mcxt.c:878
#define NULL
Definition: c.h:229
#define MAXALIGN(LEN)
Definition: c.h:588
#define LTREE_FIRST(x)
Definition: ltree.h:27
int i
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define LEVEL_HDRSIZE
Definition: ltree.h:16
Datum ltree2text ( PG_FUNCTION_ARGS  )

Definition at line 509 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, PG_RETURN_POINTER, SET_VARSIZE, VARDATA, VARHDRSZ, and VARSIZE.

510 {
511  ltree *in = PG_GETARG_LTREE(0);
512  char *ptr;
513  int i;
514  ltree_level *curlevel;
515  text *out;
516 
517  out = (text *) palloc(VARSIZE(in) + VARHDRSZ);
518  ptr = VARDATA(out);
519  curlevel = LTREE_FIRST(in);
520  for (i = 0; i < in->numlevel; i++)
521  {
522  if (i != 0)
523  {
524  *ptr = '.';
525  ptr++;
526  }
527  memcpy(ptr, curlevel->name, curlevel->len);
528  ptr += curlevel->len;
529  curlevel = LEVEL_NEXT(curlevel);
530  }
531 
532  SET_VARSIZE(out, ptr - ((char *) out));
533  PG_FREE_IF_COPY(in, 0);
534 
535  PG_RETURN_POINTER(out);
536 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:13
#define VARDATA(PTR)
Definition: postgres.h:303
uint16 len
Definition: ltree.h:12
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:445
#define PG_GETARG_LTREE(x)
Definition: ltree.h:168
#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:225
void * palloc(Size size)
Definition: mcxt.c:849
#define LTREE_FIRST(x)
Definition: ltree.h:27
int i
Definition: c.h:439
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
Datum ltree_addltree ( PG_FUNCTION_ARGS  )

Definition at line 283 of file ltree_op.c.

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

284 {
285  ltree *a = PG_GETARG_LTREE(0);
286  ltree *b = PG_GETARG_LTREE(1);
287  ltree *r;
288 
289  r = ltree_concat(a, b);
290  PG_FREE_IF_COPY(a, 0);
291  PG_FREE_IF_COPY(b, 1);
293 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_LTREE(x)
Definition: ltree.h:168
static ltree * ltree_concat(ltree *a, ltree *b)
Definition: ltree_op.c:267
Definition: ltree.h:19
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Datum ltree_addtext ( PG_FUNCTION_ARGS  )

Definition at line 296 of file ltree_op.c.

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

297 {
298  ltree *a = PG_GETARG_LTREE(0);
299  text *b = PG_GETARG_TEXT_PP(1);
300  char *s;
301  ltree *r,
302  *tmp;
303 
304  s = text_to_cstring(b);
305 
307  PointerGetDatum(s)));
308 
309  pfree(s);
310 
311  r = ltree_concat(a, tmp);
312 
313  pfree(tmp);
314 
315  PG_FREE_IF_COPY(a, 0);
316  PG_FREE_IF_COPY(b, 1);
318 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PointerGetDatum(X)
Definition: postgres.h:562
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
Datum ltree_in(PG_FUNCTION_ARGS)
Definition: ltree_io.c:38
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_GETARG_LTREE(x)
Definition: ltree.h:168
void pfree(void *pointer)
Definition: mcxt.c:950
static ltree * ltree_concat(ltree *a, ltree *b)
Definition: ltree_op.c:267
Definition: ltree.h:19
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define DatumGetPointer(X)
Definition: postgres.h:555
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Definition: c.h:439
Datum ltree_cmp ( PG_FUNCTION_ARGS  )

Definition at line 77 of file ltree_op.c.

References PG_RETURN_INT32, and RUNCMP.

78 {
79  RUNCMP
80  PG_RETURN_INT32(res);
81 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define RUNCMP
Definition: ltree_op.c:69
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  int res = 0;
49 
50  while (an > 0 && bn > 0)
51  {
52  if ((res = memcmp(al->name, bl->name, Min(al->len, bl->len))) == 0)
53  {
54  if (al->len != bl->len)
55  return (al->len - bl->len) * 10 * (an + 1);
56  }
57  else
58  return res * 10 * (an + 1);
59 
60  an--;
61  bn--;
62  al = LEVEL_NEXT(al);
63  bl = LEVEL_NEXT(bl);
64  }
65 
66  return (a->numlevel - b->numlevel) * 10 * (an + 1);
67 }
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:13
uint16 len
Definition: ltree.h:12
#define Min(x, y)
Definition: c.h:806
#define LEVEL_NEXT(x)
Definition: ltree.h:17
uint16 numlevel
Definition: ltree.h:22
#define LTREE_FIRST(x)
Definition: ltree.h:27
static ltree* ltree_concat ( ltree a,
ltree b 
)
static

Definition at line 267 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().

268 {
269  ltree *r;
270 
271  r = (ltree *) palloc0(VARSIZE(a) + VARSIZE(b) - LTREE_HDRSIZE);
273  r->numlevel = a->numlevel + b->numlevel;
274 
275  memcpy(LTREE_FIRST(r), LTREE_FIRST(a), VARSIZE(a) - LTREE_HDRSIZE);
276  memcpy(((char *) LTREE_FIRST(r)) + VARSIZE(a) - LTREE_HDRSIZE,
277  LTREE_FIRST(b),
278  VARSIZE(b) - LTREE_HDRSIZE);
279  return r;
280 }
#define VARSIZE(PTR)
Definition: postgres.h:304
#define LTREE_HDRSIZE
Definition: ltree.h:26
Definition: ltree.h:19
uint16 numlevel
Definition: ltree.h:22
void * palloc0(Size size)
Definition: mcxt.c:878
#define LTREE_FIRST(x)
Definition: ltree.h:27
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
Datum ltree_eq ( 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:69
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum ltree_ge ( 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:69
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum ltree_gt ( 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:69
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum ltree_index ( PG_FUNCTION_ARGS  )

Definition at line 321 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, and PG_RETURN_INT32.

322 {
323  ltree *a = PG_GETARG_LTREE(0);
324  ltree *b = PG_GETARG_LTREE(1);
325  int start = (fcinfo->nargs == 3) ? PG_GETARG_INT32(2) : 0;
326  int i,
327  j;
328  ltree_level *startptr,
329  *aptr,
330  *bptr;
331  bool found = false;
332 
333  if (start < 0)
334  {
335  if (-start >= a->numlevel)
336  start = 0;
337  else
338  start = (int) (a->numlevel) + start;
339  }
340 
341  if (a->numlevel - start < b->numlevel || a->numlevel == 0 || b->numlevel == 0)
342  {
343  PG_FREE_IF_COPY(a, 0);
344  PG_FREE_IF_COPY(b, 1);
345  PG_RETURN_INT32(-1);
346  }
347 
348  startptr = LTREE_FIRST(a);
349  for (i = 0; i <= a->numlevel - b->numlevel; i++)
350  {
351  if (i >= start)
352  {
353  aptr = startptr;
354  bptr = LTREE_FIRST(b);
355  for (j = 0; j < b->numlevel; j++)
356  {
357  if (!(aptr->len == bptr->len && memcmp(aptr->name, bptr->name, aptr->len) == 0))
358  break;
359  aptr = LEVEL_NEXT(aptr);
360  bptr = LEVEL_NEXT(bptr);
361  }
362 
363  if (j == b->numlevel)
364  {
365  found = true;
366  break;
367  }
368  }
369  startptr = LEVEL_NEXT(startptr);
370  }
371 
372  if (!found)
373  i = -1;
374 
375  PG_FREE_IF_COPY(a, 0);
376  PG_FREE_IF_COPY(b, 1);
377  PG_RETURN_INT32(i);
378 }
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:13
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
uint16 len
Definition: ltree.h:12
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GETARG_LTREE(x)
Definition: ltree.h:168
#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:225
#define LTREE_FIRST(x)
Definition: ltree.h:27
int i
Datum ltree_isparent ( PG_FUNCTION_ARGS  )

Definition at line 160 of file ltree_op.c.

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

Referenced by _ltree_extract_isparent(), and _ltree_isparent().

161 {
162  ltree *c = PG_GETARG_LTREE(1);
163  ltree *p = PG_GETARG_LTREE(0);
164  bool res = inner_isparent(c, p);
165 
166  PG_FREE_IF_COPY(c, 1);
167  PG_FREE_IF_COPY(p, 0);
168  PG_RETURN_BOOL(res);
169 }
bool inner_isparent(const ltree *c, const ltree *p)
Definition: ltree_op.c:136
#define PG_GETARG_LTREE(x)
Definition: ltree.h:168
char * c
Definition: ltree.h:19
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Datum ltree_le ( 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:69
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum ltree_lt ( PG_FUNCTION_ARGS  )

Definition at line 84 of file ltree_op.c.

References PG_RETURN_BOOL, and RUNCMP.

85 {
86  RUNCMP
87  PG_RETURN_BOOL((res < 0) ? true : false);
88 }
#define RUNCMP
Definition: ltree_op.c:69
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum ltree_ne ( 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:69
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
Datum ltree_risparent ( PG_FUNCTION_ARGS  )

Definition at line 172 of file ltree_op.c.

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

Referenced by _ltree_extract_risparent(), and _ltree_risparent().

173 {
174  ltree *c = PG_GETARG_LTREE(0);
175  ltree *p = PG_GETARG_LTREE(1);
176  bool res = inner_isparent(c, p);
177 
178  PG_FREE_IF_COPY(c, 0);
179  PG_FREE_IF_COPY(p, 1);
180  PG_RETURN_BOOL(res);
181 }
bool inner_isparent(const ltree *c, const ltree *p)
Definition: ltree_op.c:136
#define PG_GETARG_LTREE(x)
Definition: ltree.h:168
char * c
Definition: ltree.h:19
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Datum ltree_textadd ( PG_FUNCTION_ARGS  )

Definition at line 381 of file ltree_op.c.

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

382 {
383  ltree *a = PG_GETARG_LTREE(1);
384  text *b = PG_GETARG_TEXT_PP(0);
385  char *s;
386  ltree *r,
387  *tmp;
388 
389  s = text_to_cstring(b);
390 
392  PointerGetDatum(s)));
393 
394  pfree(s);
395 
396  r = ltree_concat(tmp, a);
397 
398  pfree(tmp);
399 
400  PG_FREE_IF_COPY(a, 1);
401  PG_FREE_IF_COPY(b, 0);
403 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PointerGetDatum(X)
Definition: postgres.h:562
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
Datum ltree_in(PG_FUNCTION_ARGS)
Definition: ltree_io.c:38
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_GETARG_LTREE(x)
Definition: ltree.h:168
void pfree(void *pointer)
Definition: mcxt.c:950
static ltree * ltree_concat(ltree *a, ltree *b)
Definition: ltree_op.c:267
Definition: ltree.h:19
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define DatumGetPointer(X)
Definition: postgres.h:555
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Definition: c.h:439
Datum ltreeparentsel ( PG_FUNCTION_ARGS  )

Definition at line 545 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.

546 {
548  Oid operator = PG_GETARG_OID(1);
549  List *args = (List *) PG_GETARG_POINTER(2);
550  int varRelid = PG_GETARG_INT32(3);
551  VariableStatData vardata;
552  Node *other;
553  bool varonleft;
554  double selec;
555 
556  /*
557  * If expression is not variable <@ something or something <@ variable,
558  * then punt and return a default estimate.
559  */
560  if (!get_restriction_variable(root, args, varRelid,
561  &vardata, &other, &varonleft))
563 
564  /*
565  * If the something is a NULL constant, assume operator is strict and
566  * return zero, ie, operator will never return TRUE.
567  */
568  if (IsA(other, Const) &&
569  ((Const *) other)->constisnull)
570  {
571  ReleaseVariableStats(vardata);
572  PG_RETURN_FLOAT8(0.0);
573  }
574 
575  if (IsA(other, Const))
576  {
577  /* Variable is being compared to a known non-null constant */
578  Datum constval = ((Const *) other)->constvalue;
579  FmgrInfo contproc;
580  double mcvsum;
581  double mcvsel;
582  double nullfrac;
583  int hist_size;
584 
585  fmgr_info(get_opcode(operator), &contproc);
586 
587  /*
588  * Is the constant "<@" to any of the column's most common values?
589  */
590  mcvsel = mcv_selectivity(&vardata, &contproc, constval, varonleft,
591  &mcvsum);
592 
593  /*
594  * If the histogram is large enough, see what fraction of it the
595  * constant is "<@" to, and assume that's representative of the
596  * non-MCV population. Otherwise use the default selectivity for the
597  * non-MCV population.
598  */
599  selec = histogram_selectivity(&vardata, &contproc,
600  constval, varonleft,
601  10, 1, &hist_size);
602  if (selec < 0)
603  {
604  /* Nope, fall back on default */
605  selec = DEFAULT_PARENT_SEL;
606  }
607  else if (hist_size < 100)
608  {
609  /*
610  * For histogram sizes from 10 to 100, we combine the histogram
611  * and default selectivities, putting increasingly more trust in
612  * the histogram for larger sizes.
613  */
614  double hist_weight = hist_size / 100.0;
615 
616  selec = selec * hist_weight +
617  DEFAULT_PARENT_SEL * (1.0 - hist_weight);
618  }
619 
620  /* In any case, don't believe extremely small or large estimates. */
621  if (selec < 0.0001)
622  selec = 0.0001;
623  else if (selec > 0.9999)
624  selec = 0.9999;
625 
626  if (HeapTupleIsValid(vardata.statsTuple))
627  nullfrac = ((Form_pg_statistic) GETSTRUCT(vardata.statsTuple))->stanullfrac;
628  else
629  nullfrac = 0.0;
630 
631  /*
632  * Now merge the results from the MCV and histogram calculations,
633  * realizing that the histogram covers only the non-null values that
634  * are not listed in MCV.
635  */
636  selec *= 1.0 - nullfrac - mcvsum;
637  selec += mcvsel;
638  }
639  else
640  selec = DEFAULT_PARENT_SEL;
641 
642  ReleaseVariableStats(vardata);
643 
644  /* result should be in range, but make sure... */
645  CLAMP_PROBABILITY(selec);
646 
647  PG_RETURN_FLOAT8((float8) selec);
648 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
Definition: fmgr.h:56
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
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:4397
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:326
Definition: nodes.h:509
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
unsigned int Oid
Definition: postgres_ext.h:31
#define DEFAULT_PARENT_SEL
Definition: ltree_op.c:539
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
double float8
Definition: c.h:381
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
uintptr_t Datum
Definition: postgres.h:372
double histogram_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval, bool varonleft, int min_hist_size, int n_skip, int *hist_size)
Definition: selfuncs.c:677
double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval, bool varonleft, double *sumcommonp)
Definition: selfuncs.c:605
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1094
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
Definition: pg_list.h:45
Datum nlevel ( PG_FUNCTION_ARGS  )

Definition at line 126 of file ltree_op.c.

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

127 {
128  ltree *a = PG_GETARG_LTREE(0);
129  int res = a->numlevel;
130 
131  PG_FREE_IF_COPY(a, 0);
132  PG_RETURN_INT32(res);
133 }
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define PG_GETARG_LTREE(x)
Definition: ltree.h:168
Definition: ltree.h:19
uint16 numlevel
Definition: ltree.h:22
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
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  )
Datum subltree ( PG_FUNCTION_ARGS  )

Definition at line 224 of file ltree_op.c.

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

225 {
226  ltree *t = PG_GETARG_LTREE(0);
228 
229  PG_FREE_IF_COPY(t, 0);
230  PG_RETURN_POINTER(res);
231 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_GETARG_LTREE(x)
Definition: ltree.h:168
Definition: ltree.h:19
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
static ltree * inner_subltree(ltree *t, int32 startpos, int32 endpos)
Definition: ltree_op.c:185
Datum subpath ( PG_FUNCTION_ARGS  )

Definition at line 234 of file ltree_op.c.

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

Referenced by _outAggPath(), _outGatherMergePath(), _outGatherPath(), _outGroupingSetsPath(), _outGroupPath(), _outLimitPath(), _outLockRowsPath(), _outMaterialPath(), _outProjectionPath(), _outProjectSetPath(), _outSetOpPath(), _outSortPath(), _outSubqueryScanPath(), _outUniquePath(), _outUpperUniquePath(), _outWindowAggPath(), add_paths_to_append_rel(), adjust_paths_for_srfs(), 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_grouping_paths(), 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(), grouping_planner(), inheritance_planner(), recurse_set_operations(), set_subquery_pathlist(), and walkdir().

235 {
236  ltree *t = PG_GETARG_LTREE(0);
237  int32 start = PG_GETARG_INT32(1);
238  int32 len = (fcinfo->nargs == 3) ? PG_GETARG_INT32(2) : 0;
239  int32 end;
240  ltree *res;
241 
242  end = start + len;
243 
244  if (start < 0)
245  {
246  start = t->numlevel + start;
247  end = start + len;
248  }
249  if (start < 0)
250  { /* start > t->numlevel */
251  start = t->numlevel + start;
252  end = start + len;
253  }
254 
255  if (len < 0)
256  end = t->numlevel + len;
257  else if (len == 0)
258  end = (fcinfo->nargs == 3) ? start : 0xffff;
259 
260  res = inner_subltree(t, start, end);
261 
262  PG_FREE_IF_COPY(t, 0);
263  PG_RETURN_POINTER(res);
264 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
signed int int32
Definition: c.h:256
#define PG_GETARG_LTREE(x)
Definition: ltree.h:168
Definition: ltree.h:19
uint16 numlevel
Definition: ltree.h:22
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
static ltree * inner_subltree(ltree *t, int32 startpos, int32 endpos)
Definition: ltree_op.c:185
Datum text2ltree ( PG_FUNCTION_ARGS  )

Definition at line 492 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().

493 {
494  text *in = PG_GETARG_TEXT_PP(0);
495  char *s;
496  ltree *out;
497 
498  s = text_to_cstring(in);
499 
501  PointerGetDatum(s)));
502  pfree(s);
503  PG_FREE_IF_COPY(in, 0);
504  PG_RETURN_POINTER(out);
505 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define PointerGetDatum(X)
Definition: postgres.h:562
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
Datum ltree_in(PG_FUNCTION_ARGS)
Definition: ltree_io.c:38
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
void pfree(void *pointer)
Definition: mcxt.c:950
Definition: ltree.h:19
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
#define DatumGetPointer(X)
Definition: postgres.h:555
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Definition: c.h:439

Variable Documentation

PG_MODULE_MAGIC

Definition at line 17 of file ltree_op.c.