PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ltree_op.c
Go to the documentation of this file.
1 /*
2  * op function for ltree
3  * Teodor Sigaev <teodor@stack.net>
4  * contrib/ltree/ltree_op.c
5  */
6 #include "postgres.h"
7 
8 #include <ctype.h>
9 
10 #include "access/htup_details.h"
11 #include "catalog/pg_statistic.h"
12 #include "utils/builtins.h"
13 #include "utils/lsyscache.h"
14 #include "utils/selfuncs.h"
15 #include "ltree.h"
16 
18 
19 /* compare functions */
40 
41 int
42 ltree_compare(const ltree *a, const ltree *b)
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 }
68 
69 #define RUNCMP \
70 ltree *a = PG_GETARG_LTREE(0); \
71 ltree *b = PG_GETARG_LTREE(1); \
72 int res = ltree_compare(a,b); \
73 PG_FREE_IF_COPY(a,0); \
74 PG_FREE_IF_COPY(b,1); \
75 
76 Datum
78 {
79  RUNCMP
80  PG_RETURN_INT32(res);
81 }
82 
83 Datum
85 {
86  RUNCMP
87  PG_RETURN_BOOL((res < 0) ? true : false);
88 }
89 
90 Datum
92 {
93  RUNCMP
94  PG_RETURN_BOOL((res <= 0) ? true : false);
95 }
96 
97 Datum
99 {
100  RUNCMP
101  PG_RETURN_BOOL((res == 0) ? true : false);
102 }
103 
104 Datum
106 {
107  RUNCMP
108  PG_RETURN_BOOL((res >= 0) ? true : false);
109 }
110 
111 Datum
113 {
114  RUNCMP
115  PG_RETURN_BOOL((res > 0) ? true : false);
116 }
117 
118 Datum
120 {
121  RUNCMP
122  PG_RETURN_BOOL((res != 0) ? true : false);
123 }
124 
125 Datum
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 }
134 
135 bool
136 inner_isparent(const ltree *c, const ltree *p)
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 }
158 
159 Datum
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 }
170 
171 Datum
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 }
182 
183 
184 static ltree *
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 }
222 
223 Datum
225 {
226  ltree *t = PG_GETARG_LTREE(0);
228 
229  PG_FREE_IF_COPY(t, 0);
230  PG_RETURN_POINTER(res);
231 }
232 
233 Datum
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 }
265 
266 static ltree *
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 }
281 
282 Datum
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 }
294 
295 Datum
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 }
319 
320 Datum
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 }
379 
380 Datum
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 }
404 
405 ltree *
406 lca_inner(ltree **a, int len)
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 }
469 
470 Datum
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 }
490 
491 Datum
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 }
506 
507 
508 Datum
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 }
537 
538 
539 #define DEFAULT_PARENT_SEL 0.001
540 
541 /*
542  * ltreeparentsel - Selectivity of parent relationship for ltree data types.
543  */
544 Datum
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_RETURN_POINTER(x)
Definition: fmgr.h:321
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: ltree.h:13
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
Definition: fmgr.h:56
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#define VARDATA(PTR)
Definition: postgres.h:303
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
Datum ltree_cmp(PG_FUNCTION_ARGS)
Definition: ltree_op.c:77
uint16 len
Definition: ltree.h:12
HeapTuple statsTuple
Definition: selfuncs.h:71
#define VARSIZE(PTR)
Definition: postgres.h:304
#define PointerGetDatum(X)
Definition: postgres.h:562
#define VARHDRSZ
Definition: c.h:445
Datum nlevel(PG_FUNCTION_ARGS)
Definition: ltree_op.c:126
bool get_restriction_variable(PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)
Definition: selfuncs.c:4450
Datum ltree_le(PG_FUNCTION_ARGS)
Definition: ltree_op.c:91
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:326
#define Min(x, y)
Definition: c.h:807
Datum ltree_index(PG_FUNCTION_ARGS)
Definition: ltree_op.c:321
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
ltree * lca_inner(ltree **a, int len)
Definition: ltree_op.c:406
Definition: nodes.h:509
#define LTREE_HDRSIZE
Definition: ltree.h:26
int errcode(int sqlerrcode)
Definition: elog.c:575
bool inner_isparent(const ltree *c, const ltree *p)
Definition: ltree_op.c:136
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:584
PG_FUNCTION_INFO_V1(ltree_cmp)
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
Datum ltree_in(PG_FUNCTION_ARGS)
Definition: ltree_io.c:38
signed int int32
Definition: c.h:256
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
#define PG_GETARG_LTREE(x)
Definition: ltree.h:168
Datum ltree2text(PG_FUNCTION_ARGS)
Definition: ltree_op.c:509
static XLogRecPtr endpos
Datum ltree_risparent(PG_FUNCTION_ARGS)
Definition: ltree_op.c:172
void pfree(void *pointer)
Definition: mcxt.c:950
PG_MODULE_MAGIC
Definition: ltree_op.c:17
#define ERROR
Definition: elog.h:43
double float8
Definition: c.h:381
static ltree * ltree_concat(ltree *a, ltree *b)
Definition: ltree_op.c:267
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
char * c
#define RUNCMP
Definition: ltree_op.c:69
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define LEVEL_NEXT(x)
Definition: ltree.h:17
#define ereport(elevel, rest)
Definition: elog.h:122
Definition: ltree.h:19
Datum text2ltree(PG_FUNCTION_ARGS)
Definition: ltree_op.c:492
uint16 numlevel
Definition: ltree.h:22
void * palloc0(Size size)
Definition: mcxt.c:878
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
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:704
Datum ltree_ne(PG_FUNCTION_ARGS)
Definition: ltree_op.c:119
int ltree_compare(const ltree *a, const ltree *b)
Definition: ltree_op.c:42
Datum ltree_eq(PG_FUNCTION_ARGS)
Definition: ltree_op.c:98
double mcv_selectivity(VariableStatData *vardata, FmgrInfo *opproc, Datum constval, bool varonleft, double *sumcommonp)
Definition: selfuncs.c:632
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1094
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
Datum lca(PG_FUNCTION_ARGS)
Definition: ltree_op.c:471
Datum ltree_addtext(PG_FUNCTION_ARGS)
Definition: ltree_op.c:296
#define MAXALIGN(LEN)
Definition: c.h:588
static XLogRecPtr startpos
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:225
Datum ltree_isparent(PG_FUNCTION_ARGS)
Definition: ltree_op.c:160
#define DatumGetPointer(X)
Definition: postgres.h:555
char * text_to_cstring(const text *t)
Definition: varlena.c:182
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:81
Datum ltree_ge(PG_FUNCTION_ARGS)
Definition: ltree_op.c:105
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
static ltree * inner_subltree(ltree *t, int32 startpos, int32 endpos)
Definition: ltree_op.c:185
#define LTREE_FIRST(x)
Definition: ltree.h:27
int i
Datum ltree_gt(PG_FUNCTION_ARGS)
Definition: ltree_op.c:112
Definition: c.h:439
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
Datum ltree_textadd(PG_FUNCTION_ARGS)
Definition: ltree_op.c:381
Datum ltreeparentsel(PG_FUNCTION_ARGS)
Definition: ltree_op.c:545
Datum subltree(PG_FUNCTION_ARGS)
Definition: ltree_op.c:224
Definition: pg_list.h:45
#define PG_RETURN_NULL()
Definition: fmgr.h:305
Datum ltree_addltree(PG_FUNCTION_ARGS)
Definition: ltree_op.c:283
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234
Datum ltree_lt(PG_FUNCTION_ARGS)
Definition: ltree_op.c:84
#define LEVEL_HDRSIZE
Definition: ltree.h:16