Debian Codegraph @

contrib/ltree/ltree_op.c

raw
1 /*
2 * op function for ltree
3 * Teodor Sigaev <[email protected]>
4 * contrib/ltree/ltree_op.c
5 */
6 #include "postgres.h"
7
8 #include <ctype.h>
9
10 #include "common/hashfn.h"
11 #include "ltree.h"
12 #include "utils/builtins.h"
13 #include "utils/selfuncs.h"
14 #include "varatt.h"
15
16 PG_MODULE_MAGIC_EXT(
17 .name = "ltree",
18 .version = PG_VERSION
19 );
20
21 /* compare functions */
22 PG_FUNCTION_INFO_V1(ltree_cmp);
23 PG_FUNCTION_INFO_V1(ltree_lt);
24 PG_FUNCTION_INFO_V1(ltree_le);
25 PG_FUNCTION_INFO_V1(ltree_eq);
26 PG_FUNCTION_INFO_V1(ltree_ne);
27 PG_FUNCTION_INFO_V1(ltree_ge);
28 PG_FUNCTION_INFO_V1(ltree_gt);
29 PG_FUNCTION_INFO_V1(hash_ltree);
30 PG_FUNCTION_INFO_V1(hash_ltree_extended);
31 PG_FUNCTION_INFO_V1(nlevel);
32 PG_FUNCTION_INFO_V1(ltree_isparent);
33 PG_FUNCTION_INFO_V1(ltree_risparent);
34 PG_FUNCTION_INFO_V1(subltree);
35 PG_FUNCTION_INFO_V1(subpath);
36 PG_FUNCTION_INFO_V1(ltree_index);
37 PG_FUNCTION_INFO_V1(ltree_addltree);
38 PG_FUNCTION_INFO_V1(ltree_addtext);
39 PG_FUNCTION_INFO_V1(ltree_textadd);
40 PG_FUNCTION_INFO_V1(lca);
41 PG_FUNCTION_INFO_V1(ltree2text);
42 PG_FUNCTION_INFO_V1(text2ltree);
43 PG_FUNCTION_INFO_V1(ltreeparentsel);
44
45 /*
46 * btree-comparison function.
47 */
48 int
49 ltree_compare(const ltree *a, const ltree *b)
50 {
51 ltree_level *al = LTREE_FIRST(a);
52 ltree_level *bl = LTREE_FIRST(b);
53 int an = a->numlevel;
54 int bn = b->numlevel;
55
56 while (an > 0 && bn > 0)
57 {
58 int res;
59
60 res = memcmp(al->name, bl->name, Min(al->len, bl->len));
61 if (res == 0)
62 {
63 if (al->len != bl->len)
64 return (int) al->len - (int) bl->len;
65 }
66 else
67 return res;
68
69 an--;
70 bn--;
71 al = LEVEL_NEXT(al);
72 bl = LEVEL_NEXT(bl);
73 }
74
75 return a->numlevel - b->numlevel;
76 }
77
78 /*
79 * Returns a "distance" between a and b. If a < b, the distance is negative,
80 * consistent with the ltree_compare() ordering.
81 */
82 float
83 ltree_compare_distance(const ltree *a, const ltree *b)
84 {
85 ltree_level *al = LTREE_FIRST(a);
86 ltree_level *bl = LTREE_FIRST(b);
87 int an = a->numlevel;
88 int bn = b->numlevel;
89
90 while (an > 0 && bn > 0)
91 {
92 int res;
93
94 res = memcmp(al->name, bl->name, Min(al->len, bl->len));
95 if (res == 0)
96 {
97 if (al->len != bl->len)
98 return (float) (al->len - bl->len) * 10.0 * (an + 1);
99 }
100 else
101 {
102 if (res < 0)
103 return -1.0 * 10.0 * (an + 1);
104 else
105 return 1.0 * 10.0 * (an + 1);
106 }
107
108 an--;
109 bn--;
110 al = LEVEL_NEXT(al);
111 bl = LEVEL_NEXT(bl);
112 }
113
114 return ((float) (a->numlevel - b->numlevel)) * 10.0 * (an + 1);
115 }
116
117 #define RUNCMP \
118 ltree *a = PG_GETARG_LTREE_P(0); \
119 ltree *b = PG_GETARG_LTREE_P(1); \
120 int res = ltree_compare(a,b); \
121 PG_FREE_IF_COPY(a,0); \
122 PG_FREE_IF_COPY(b,1)
123
124 Datum
125 ltree_cmp(PG_FUNCTION_ARGS)
126 {
127 RUNCMP;
128 PG_RETURN_INT32(res);
129 }
130
131 Datum
132 ltree_lt(PG_FUNCTION_ARGS)
133 {
134 RUNCMP;
135 PG_RETURN_BOOL(res < 0);
136 }
137
138 Datum
139 ltree_le(PG_FUNCTION_ARGS)
140 {
141 RUNCMP;
142 PG_RETURN_BOOL(res <= 0);
143 }
144
145 Datum
146 ltree_eq(PG_FUNCTION_ARGS)
147 {
148 RUNCMP;
149 PG_RETURN_BOOL(res == 0);
150 }
151
152 Datum
153 ltree_ge(PG_FUNCTION_ARGS)
154 {
155 RUNCMP;
156 PG_RETURN_BOOL(res >= 0);
157 }
158
159 Datum
160 ltree_gt(PG_FUNCTION_ARGS)
161 {
162 RUNCMP;
163 PG_RETURN_BOOL(res > 0);
164 }
165
166 Datum
167 ltree_ne(PG_FUNCTION_ARGS)
168 {
169 RUNCMP;
170 PG_RETURN_BOOL(res != 0);
171 }
172
173 /* Compute a hash for the ltree */
174 Datum
175 hash_ltree(PG_FUNCTION_ARGS)
176 {
177 ltree *a = PG_GETARG_LTREE_P(0);
178 uint32 result = 1;
179 int an = a->numlevel;
180 ltree_level *al = LTREE_FIRST(a);
181
182 while (an > 0)
183 {
184 uint32 levelHash = DatumGetUInt32(hash_any((unsigned char *) al->name, al->len));
185
186 /*
187 * Combine hash values of successive elements by multiplying the
188 * current value by 31 and adding on the new element's hash value.
189 *
190 * This method is borrowed from hash_array(), which see for further
191 * commentary.
192 */
193 result = (result << 5) - result + levelHash;
194
195 an--;
196 al = LEVEL_NEXT(al);
197 }
198
199 PG_FREE_IF_COPY(a, 0);
200 PG_RETURN_UINT32(result);
201 }
202
203 /* Compute an extended hash for the ltree */
204 Datum
205 hash_ltree_extended(PG_FUNCTION_ARGS)
206 {
207 ltree *a = PG_GETARG_LTREE_P(0);
208 const uint64 seed = PG_GETARG_INT64(1);
209 uint64 result = 1;
210 int an = a->numlevel;
211 ltree_level *al = LTREE_FIRST(a);
212
213 /*
214 * If the path has length zero, return 1 + seed to ensure that the low 32
215 * bits of the result match hash_ltree when the seed is 0, as required by
216 * the hash index support functions, but to also return a different value
217 * when there is a seed.
218 */
219 if (an == 0)
220 {
221 PG_FREE_IF_COPY(a, 0);
222 PG_RETURN_UINT64(result + seed);
223 }
224
225 while (an > 0)
226 {
227 uint64 levelHash = DatumGetUInt64(hash_any_extended((unsigned char *) al->name, al->len, seed));
228
229 result = (result << 5) - result + levelHash;
230
231 an--;
232 al = LEVEL_NEXT(al);
233 }
234
235 PG_FREE_IF_COPY(a, 0);
236 PG_RETURN_UINT64(result);
237 }
238
239 Datum
240 nlevel(PG_FUNCTION_ARGS)
241 {
242 ltree *a = PG_GETARG_LTREE_P(0);
243 int res = a->numlevel;
244
245 PG_FREE_IF_COPY(a, 0);
246 PG_RETURN_INT32(res);
247 }
248
249 bool
250 inner_isparent(const ltree *c, const ltree *p)
251 {
252 ltree_level *cl = LTREE_FIRST(c);
253 ltree_level *pl = LTREE_FIRST(p);
254 int pn = p->numlevel;
255
256 if (pn > c->numlevel)
257 return false;
258
259 while (pn > 0)
260 {
261 if (cl->len != pl->len)
262 return false;
263 if (memcmp(cl->name, pl->name, cl->len) != 0)
264 return false;
265
266 pn--;
267 cl = LEVEL_NEXT(cl);
268 pl = LEVEL_NEXT(pl);
269 }
270 return true;
271 }
272
273 Datum
274 ltree_isparent(PG_FUNCTION_ARGS)
275 {
276 ltree *c = PG_GETARG_LTREE_P(1);
277 ltree *p = PG_GETARG_LTREE_P(0);
278 bool res = inner_isparent(c, p);
279
280 PG_FREE_IF_COPY(c, 1);
281 PG_FREE_IF_COPY(p, 0);
282 PG_RETURN_BOOL(res);
283 }
284
285 Datum
286 ltree_risparent(PG_FUNCTION_ARGS)
287 {
288 ltree *c = PG_GETARG_LTREE_P(0);
289 ltree *p = PG_GETARG_LTREE_P(1);
290 bool res = inner_isparent(c, p);
291
292 PG_FREE_IF_COPY(c, 0);
293 PG_FREE_IF_COPY(p, 1);
294 PG_RETURN_BOOL(res);
295 }
296
297
298 static ltree *
299 inner_subltree(ltree *t, int32 startpos, int32 endpos)
300 {
301 char *start = NULL,
302 *end = NULL;
303 ltree_level *ptr = LTREE_FIRST(t);
304 ltree *res;
305 int i;
306
307 if (startpos < 0 || endpos < 0 || startpos >= t->numlevel || startpos > endpos)
308 ereport(ERROR,
309 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
310 errmsg("invalid positions")));
311
312 if (endpos > t->numlevel)
313 endpos = t->numlevel;
314
315 start = end = (char *) ptr;
316 for (i = 0; i < endpos; i++)
317 {
318 if (i == startpos)
319 start = (char *) ptr;
320 if (i == endpos - 1)
321 {
322 end = (char *) LEVEL_NEXT(ptr);
323 break;
324 }
325 ptr = LEVEL_NEXT(ptr);
326 }
327
328 res = (ltree *) palloc0(LTREE_HDRSIZE + (end - start));
329 SET_VARSIZE(res, LTREE_HDRSIZE + (end - start));
330 res->numlevel = endpos - startpos;
331
332 memcpy(LTREE_FIRST(res), start, end - start);
333
334 return res;
335 }
336
337 Datum
338 subltree(PG_FUNCTION_ARGS)
339 {
340 ltree *t = PG_GETARG_LTREE_P(0);
341 ltree *res = inner_subltree(t, PG_GETARG_INT32(1), PG_GETARG_INT32(2));
342
343 PG_FREE_IF_COPY(t, 0);
344 PG_RETURN_POINTER(res);
345 }
346
347 Datum
348 subpath(PG_FUNCTION_ARGS)
349 {
350 ltree *t = PG_GETARG_LTREE_P(0);
351 int32 start = PG_GETARG_INT32(1);
352 int32 len = (fcinfo->nargs == 3) ? PG_GETARG_INT32(2) : 0;
353 int32 end;
354 ltree *res;
355
356 end = start + len;
357
358 if (start < 0)
359 {
360 start = t->numlevel + start;
361 end = start + len;
362 }
363 if (start < 0)
364 { /* start > t->numlevel */
365 start = t->numlevel + start;
366 end = start + len;
367 }
368
369 if (len < 0)
370 end = t->numlevel + len;
371 else if (len == 0)
372 end = (fcinfo->nargs == 3) ? start : 0xffff;
373
374 res = inner_subltree(t, start, end);
375
376 PG_FREE_IF_COPY(t, 0);
377 PG_RETURN_POINTER(res);
378 }
379
380 static ltree *
381 ltree_concat(ltree *a, ltree *b)
382 {
383 ltree *r;
384 int numlevel = (int) a->numlevel + b->numlevel;
385
386 if (numlevel > LTREE_MAX_LEVELS)
387 ereport(ERROR,
388 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
389 errmsg("number of ltree levels (%d) exceeds the maximum allowed (%d)",
390 numlevel, LTREE_MAX_LEVELS)));
391
392 r = (ltree *) palloc0(VARSIZE(a) + VARSIZE(b) - LTREE_HDRSIZE);
393 SET_VARSIZE(r, VARSIZE(a) + VARSIZE(b) - LTREE_HDRSIZE);
394 r->numlevel = (uint16) numlevel;
395
396 memcpy(LTREE_FIRST(r), LTREE_FIRST(a), VARSIZE(a) - LTREE_HDRSIZE);
397 memcpy(((char *) LTREE_FIRST(r)) + VARSIZE(a) - LTREE_HDRSIZE,
398 LTREE_FIRST(b),
399 VARSIZE(b) - LTREE_HDRSIZE);
400 return r;
401 }
402
403 Datum
404 ltree_addltree(PG_FUNCTION_ARGS)
405 {
406 ltree *a = PG_GETARG_LTREE_P(0);
407 ltree *b = PG_GETARG_LTREE_P(1);
408 ltree *r;
409
410 r = ltree_concat(a, b);
411 PG_FREE_IF_COPY(a, 0);
412 PG_FREE_IF_COPY(b, 1);
413 PG_RETURN_POINTER(r);
414 }
415
416 Datum
417 ltree_addtext(PG_FUNCTION_ARGS)
418 {
419 ltree *a = PG_GETARG_LTREE_P(0);
420 text *b = PG_GETARG_TEXT_PP(1);
421 char *s;
422 ltree *r,
423 *tmp;
424
425 s = text_to_cstring(b);
426
427 tmp = (ltree *) DatumGetPointer(DirectFunctionCall1(ltree_in,
428 PointerGetDatum(s)));
429
430 pfree(s);
431
432 r = ltree_concat(a, tmp);
433
434 pfree(tmp);
435
436 PG_FREE_IF_COPY(a, 0);
437 PG_FREE_IF_COPY(b, 1);
438 PG_RETURN_POINTER(r);
439 }
440
441 Datum
442 ltree_index(PG_FUNCTION_ARGS)
443 {
444 ltree *a = PG_GETARG_LTREE_P(0);
445 ltree *b = PG_GETARG_LTREE_P(1);
446 int start = (fcinfo->nargs == 3) ? PG_GETARG_INT32(2) : 0;
447 int i,
448 j;
449 ltree_level *startptr,
450 *aptr,
451 *bptr;
452 bool found = false;
453
454 if (start < 0)
455 {
456 if (-start >= a->numlevel)
457 start = 0;
458 else
459 start = (int) (a->numlevel) + start;
460 }
461
462 if (a->numlevel - start < b->numlevel || a->numlevel == 0 || b->numlevel == 0)
463 {
464 PG_FREE_IF_COPY(a, 0);
465 PG_FREE_IF_COPY(b, 1);
466 PG_RETURN_INT32(-1);
467 }
468
469 startptr = LTREE_FIRST(a);
470 for (i = 0; i <= a->numlevel - b->numlevel; i++)
471 {
472 if (i >= start)
473 {
474 aptr = startptr;
475 bptr = LTREE_FIRST(b);
476 for (j = 0; j < b->numlevel; j++)
477 {
478 if (!(aptr->len == bptr->len && memcmp(aptr->name, bptr->name, aptr->len) == 0))
479 break;
480 aptr = LEVEL_NEXT(aptr);
481 bptr = LEVEL_NEXT(bptr);
482 }
483
484 if (j == b->numlevel)
485 {
486 found = true;
487 break;
488 }
489 }
490 startptr = LEVEL_NEXT(startptr);
491 }
492
493 if (!found)
494 i = -1;
495
496 PG_FREE_IF_COPY(a, 0);
497 PG_FREE_IF_COPY(b, 1);
498 PG_RETURN_INT32(i);
499 }
500
501 Datum
502 ltree_textadd(PG_FUNCTION_ARGS)
503 {
504 ltree *a = PG_GETARG_LTREE_P(1);
505 text *b = PG_GETARG_TEXT_PP(0);
506 char *s;
507 ltree *r,
508 *tmp;
509
510 s = text_to_cstring(b);
511
512 tmp = (ltree *) DatumGetPointer(DirectFunctionCall1(ltree_in,
513 PointerGetDatum(s)));
514
515 pfree(s);
516
517 r = ltree_concat(tmp, a);
518
519 pfree(tmp);
520
521 PG_FREE_IF_COPY(a, 1);
522 PG_FREE_IF_COPY(b, 0);
523 PG_RETURN_POINTER(r);
524 }
525
526 /*
527 * Common code for variants of lca(), find longest common ancestor of inputs
528 *
529 * Returns NULL if there is no common ancestor, ie, the longest common
530 * prefix is empty.
531 */
532 ltree *
533 lca_inner(ltree **a, int len)
534 {
535 int tmp,
536 num,
537 i,
538 reslen;
539 ltree **ptr;
540 ltree_level *l1,
541 *l2;
542 ltree *res;
543
544 if (len <= 0)
545 return NULL; /* no inputs? */
546 if ((*a)->numlevel == 0)
547 return NULL; /* any empty input means NULL result */
548
549 /* num is the length of the longest common ancestor so far */
550 num = (*a)->numlevel - 1;
551
552 /* Compare each additional input to *a */
553 ptr = a + 1;
554 while (ptr - a < len)
555 {
556 if ((*ptr)->numlevel == 0)
557 return NULL;
558 else if ((*ptr)->numlevel == 1)
559 num = 0;
560 else
561 {
562 l1 = LTREE_FIRST(*a);
563 l2 = LTREE_FIRST(*ptr);
564 tmp = Min(num, (*ptr)->numlevel - 1);
565 num = 0;
566 for (i = 0; i < tmp; i++)
567 {
568 if (l1->len == l2->len &&
569 memcmp(l1->name, l2->name, l1->len) == 0)
570 num = i + 1;
571 else
572 break;
573 l1 = LEVEL_NEXT(l1);
574 l2 = LEVEL_NEXT(l2);
575 }
576 }
577 ptr++;
578 }
579
580 /* Now compute size of result ... */
581 reslen = LTREE_HDRSIZE;
582 l1 = LTREE_FIRST(*a);
583 for (i = 0; i < num; i++)
584 {
585 reslen += MAXALIGN(l1->len + LEVEL_HDRSIZE);
586 l1 = LEVEL_NEXT(l1);
587 }
588
589 /* ... and construct it by copying from *a */
590 res = (ltree *) palloc0(reslen);
591 SET_VARSIZE(res, reslen);
592 res->numlevel = num;
593
594 l1 = LTREE_FIRST(*a);
595 l2 = LTREE_FIRST(res);
596
597 for (i = 0; i < num; i++)
598 {
599 memcpy(l2, l1, MAXALIGN(l1->len + LEVEL_HDRSIZE));
600 l1 = LEVEL_NEXT(l1);
601 l2 = LEVEL_NEXT(l2);
602 }
603
604 return res;
605 }
606
607 Datum
608 lca(PG_FUNCTION_ARGS)
609 {
610 int i;
611 ltree **a,
612 *res;
613
614 a = (ltree **) palloc(sizeof(ltree *) * fcinfo->nargs);
615 for (i = 0; i < fcinfo->nargs; i++)
616 a[i] = PG_GETARG_LTREE_P(i);
617 res = lca_inner(a, (int) fcinfo->nargs);
618 for (i = 0; i < fcinfo->nargs; i++)
619 PG_FREE_IF_COPY(a[i], i);
620 pfree(a);
621
622 if (res)
623 PG_RETURN_POINTER(res);
624 else
625 PG_RETURN_NULL();
626 }
627
628 Datum
629 text2ltree(PG_FUNCTION_ARGS)
630 {
631 text *in = PG_GETARG_TEXT_PP(0);
632 char *s;
633 ltree *out;
634
635 s = text_to_cstring(in);
636
637 out = (ltree *) DatumGetPointer(DirectFunctionCall1(ltree_in,
638 PointerGetDatum(s)));
639 pfree(s);
640 PG_FREE_IF_COPY(in, 0);
641 PG_RETURN_POINTER(out);
642 }
643
644
645 Datum
646 ltree2text(PG_FUNCTION_ARGS)
647 {
648 ltree *in = PG_GETARG_LTREE_P(0);
649 char *ptr;
650 int i;
651 ltree_level *curlevel;
652 text *out;
653
654 out = (text *) palloc(VARSIZE(in) + VARHDRSZ);
655 ptr = VARDATA(out);
656 curlevel = LTREE_FIRST(in);
657 for (i = 0; i < in->numlevel; i++)
658 {
659 if (i != 0)
660 {
661 *ptr = '.';
662 ptr++;
663 }
664 memcpy(ptr, curlevel->name, curlevel->len);
665 ptr += curlevel->len;
666 curlevel = LEVEL_NEXT(curlevel);
667 }
668
669 SET_VARSIZE(out, ptr - ((char *) out));
670 PG_FREE_IF_COPY(in, 0);
671
672 PG_RETURN_POINTER(out);
673 }
674
675
676 /*
677 * ltreeparentsel - Selectivity of parent relationship for ltree data types.
678 *
679 * This function is not used anymore, if the ltree extension has been
680 * updated to 1.2 or later.
681 */
682 Datum
683 ltreeparentsel(PG_FUNCTION_ARGS)
684 {
685 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
686 Oid operator = PG_GETARG_OID(1);
687 List *args = (List *) PG_GETARG_POINTER(2);
688 int varRelid = PG_GETARG_INT32(3);
689 double selec;
690
691 /* Use generic restriction selectivity logic, with default 0.001. */
692 selec = generic_restriction_selectivity(root, operator, InvalidOid,
693 args, varRelid,
694 0.001);
695
696 PG_RETURN_FLOAT8((float8) selec);
697 }