2 * op function for ltree
3 * Teodor Sigaev <teodor@stack.net>
4 * contrib/ltree/ltree_op.c
10 #include "access/htup_details.h"
11 #include "catalog/pg_statistic.h"
13 #include "utils/builtins.h"
14 #include "utils/lsyscache.h"
15 #include "utils/selfuncs.h"
19 /* compare functions */
20 PG_FUNCTION_INFO_V1(ltree_cmp
);
21 PG_FUNCTION_INFO_V1(ltree_lt
);
22 PG_FUNCTION_INFO_V1(ltree_le
);
23 PG_FUNCTION_INFO_V1(ltree_eq
);
24 PG_FUNCTION_INFO_V1(ltree_ne
);
25 PG_FUNCTION_INFO_V1(ltree_ge
);
26 PG_FUNCTION_INFO_V1(ltree_gt
);
27 PG_FUNCTION_INFO_V1(nlevel
);
28 PG_FUNCTION_INFO_V1(ltree_isparent
);
29 PG_FUNCTION_INFO_V1(ltree_risparent
);
30 PG_FUNCTION_INFO_V1(subltree
);
31 PG_FUNCTION_INFO_V1(subpath
);
32 PG_FUNCTION_INFO_V1(ltree_index
);
33 PG_FUNCTION_INFO_V1(ltree_addltree
);
34 PG_FUNCTION_INFO_V1(ltree_addtext
);
35 PG_FUNCTION_INFO_V1(ltree_textadd
);
36 PG_FUNCTION_INFO_V1(lca
);
37 PG_FUNCTION_INFO_V1(ltree2text
);
38 PG_FUNCTION_INFO_V1(text2ltree
);
39 PG_FUNCTION_INFO_V1(ltreeparentsel
);
42 ltree_compare(const ltree
*a
, const ltree
*b
)
44 ltree_level
*al
= LTREE_FIRST(a
);
45 ltree_level
*bl
= LTREE_FIRST(b
);
49 while (an
> 0 && bn
> 0)
53 if ((res
= memcmp(al
->name
, bl
->name
, Min(al
->len
, bl
->len
))) == 0)
55 if (al
->len
!= bl
->len
)
56 return (al
->len
- bl
->len
) * 10 * (an
+ 1);
64 return res
* 10 * (an
+ 1);
73 return (a
->numlevel
- b
->numlevel
) * 10 * (an
+ 1);
77 ltree *a = PG_GETARG_LTREE_P(0); \
78 ltree *b = PG_GETARG_LTREE_P(1); \
79 int res = ltree_compare(a,b); \
80 PG_FREE_IF_COPY(a,0); \
84 ltree_cmp(PG_FUNCTION_ARGS
)
91 ltree_lt(PG_FUNCTION_ARGS
)
94 PG_RETURN_BOOL(res
< 0);
98 ltree_le(PG_FUNCTION_ARGS
)
101 PG_RETURN_BOOL(res
<= 0);
105 ltree_eq(PG_FUNCTION_ARGS
)
108 PG_RETURN_BOOL(res
== 0);
112 ltree_ge(PG_FUNCTION_ARGS
)
115 PG_RETURN_BOOL(res
>= 0);
119 ltree_gt(PG_FUNCTION_ARGS
)
122 PG_RETURN_BOOL(res
> 0);
126 ltree_ne(PG_FUNCTION_ARGS
)
129 PG_RETURN_BOOL(res
!= 0);
133 nlevel(PG_FUNCTION_ARGS
)
135 ltree
*a
= PG_GETARG_LTREE_P(0);
136 int res
= a
->numlevel
;
138 PG_FREE_IF_COPY(a
, 0);
139 PG_RETURN_INT32(res
);
143 inner_isparent(const ltree
*c
, const ltree
*p
)
145 ltree_level
*cl
= LTREE_FIRST(c
);
146 ltree_level
*pl
= LTREE_FIRST(p
);
147 int pn
= p
->numlevel
;
149 if (pn
> c
->numlevel
)
154 if (cl
->len
!= pl
->len
)
156 if (memcmp(cl
->name
, pl
->name
, cl
->len
) != 0)
167 ltree_isparent(PG_FUNCTION_ARGS
)
169 ltree
*c
= PG_GETARG_LTREE_P(1);
170 ltree
*p
= PG_GETARG_LTREE_P(0);
171 bool res
= inner_isparent(c
, p
);
173 PG_FREE_IF_COPY(c
, 1);
174 PG_FREE_IF_COPY(p
, 0);
179 ltree_risparent(PG_FUNCTION_ARGS
)
181 ltree
*c
= PG_GETARG_LTREE_P(0);
182 ltree
*p
= PG_GETARG_LTREE_P(1);
183 bool res
= inner_isparent(c
, p
);
185 PG_FREE_IF_COPY(c
, 0);
186 PG_FREE_IF_COPY(p
, 1);
192 inner_subltree(ltree
*t
, int32 startpos
, int32 endpos
)
196 ltree_level
*ptr
= LTREE_FIRST(t
);
200 if (startpos
< 0 || endpos
< 0 || startpos
>= t
->numlevel
|| startpos
> endpos
)
202 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
203 errmsg("invalid positions")));
205 if (endpos
> t
->numlevel
)
206 endpos
= t
->numlevel
;
208 start
= end
= (char *) ptr
;
209 for (i
= 0; i
< endpos
; i
++)
212 start
= (char *) ptr
;
215 end
= (char *) LEVEL_NEXT(ptr
);
218 ptr
= LEVEL_NEXT(ptr
);
221 res
= (ltree
*) palloc0(LTREE_HDRSIZE
+ (end
- start
));
222 SET_VARSIZE(res
, LTREE_HDRSIZE
+ (end
- start
));
223 res
->numlevel
= endpos
- startpos
;
225 memcpy(LTREE_FIRST(res
), start
, end
- start
);
231 subltree(PG_FUNCTION_ARGS
)
233 ltree
*t
= PG_GETARG_LTREE_P(0);
234 ltree
*res
= inner_subltree(t
, PG_GETARG_INT32(1), PG_GETARG_INT32(2));
236 PG_FREE_IF_COPY(t
, 0);
237 PG_RETURN_POINTER(res
);
241 subpath(PG_FUNCTION_ARGS
)
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;
253 start
= t
->numlevel
+ start
;
257 { /* start > t->numlevel */
258 start
= t
->numlevel
+ start
;
263 end
= t
->numlevel
+ len
;
265 end
= (fcinfo
->nargs
== 3) ? start
: 0xffff;
267 res
= inner_subltree(t
, start
, end
);
269 PG_FREE_IF_COPY(t
, 0);
270 PG_RETURN_POINTER(res
);
274 ltree_concat(ltree
*a
, ltree
*b
)
277 int numlevel
= (int) a
->numlevel
+ b
->numlevel
;
279 if (numlevel
> LTREE_MAX_LEVELS
)
281 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED
),
282 errmsg("number of ltree levels (%d) exceeds the maximum allowed (%d)",
283 numlevel
, LTREE_MAX_LEVELS
)));
285 r
= (ltree
*) palloc0(VARSIZE(a
) + VARSIZE(b
) - LTREE_HDRSIZE
);
286 SET_VARSIZE(r
, VARSIZE(a
) + VARSIZE(b
) - LTREE_HDRSIZE
);
287 r
->numlevel
= (uint16
) numlevel
;
289 memcpy(LTREE_FIRST(r
), LTREE_FIRST(a
), VARSIZE(a
) - LTREE_HDRSIZE
);
290 memcpy(((char *) LTREE_FIRST(r
)) + VARSIZE(a
) - LTREE_HDRSIZE
,
292 VARSIZE(b
) - LTREE_HDRSIZE
);
297 ltree_addltree(PG_FUNCTION_ARGS
)
299 ltree
*a
= PG_GETARG_LTREE_P(0);
300 ltree
*b
= PG_GETARG_LTREE_P(1);
303 r
= ltree_concat(a
, b
);
304 PG_FREE_IF_COPY(a
, 0);
305 PG_FREE_IF_COPY(b
, 1);
306 PG_RETURN_POINTER(r
);
310 ltree_addtext(PG_FUNCTION_ARGS
)
312 ltree
*a
= PG_GETARG_LTREE_P(0);
313 text
*b
= PG_GETARG_TEXT_PP(1);
318 s
= text_to_cstring(b
);
320 tmp
= (ltree
*) DatumGetPointer(DirectFunctionCall1(ltree_in
,
321 PointerGetDatum(s
)));
325 r
= ltree_concat(a
, tmp
);
329 PG_FREE_IF_COPY(a
, 0);
330 PG_FREE_IF_COPY(b
, 1);
331 PG_RETURN_POINTER(r
);
335 ltree_index(PG_FUNCTION_ARGS
)
337 ltree
*a
= PG_GETARG_LTREE_P(0);
338 ltree
*b
= PG_GETARG_LTREE_P(1);
339 int start
= (fcinfo
->nargs
== 3) ? PG_GETARG_INT32(2) : 0;
342 ltree_level
*startptr
,
349 if (-start
>= a
->numlevel
)
352 start
= (int) (a
->numlevel
) + start
;
355 if (a
->numlevel
- start
< b
->numlevel
|| a
->numlevel
== 0 || b
->numlevel
== 0)
357 PG_FREE_IF_COPY(a
, 0);
358 PG_FREE_IF_COPY(b
, 1);
362 startptr
= LTREE_FIRST(a
);
363 for (i
= 0; i
<= a
->numlevel
- b
->numlevel
; i
++)
368 bptr
= LTREE_FIRST(b
);
369 for (j
= 0; j
< b
->numlevel
; j
++)
371 if (!(aptr
->len
== bptr
->len
&& memcmp(aptr
->name
, bptr
->name
, aptr
->len
) == 0))
373 aptr
= LEVEL_NEXT(aptr
);
374 bptr
= LEVEL_NEXT(bptr
);
377 if (j
== b
->numlevel
)
383 startptr
= LEVEL_NEXT(startptr
);
389 PG_FREE_IF_COPY(a
, 0);
390 PG_FREE_IF_COPY(b
, 1);
395 ltree_textadd(PG_FUNCTION_ARGS
)
397 ltree
*a
= PG_GETARG_LTREE_P(1);
398 text
*b
= PG_GETARG_TEXT_PP(0);
403 s
= text_to_cstring(b
);
405 tmp
= (ltree
*) DatumGetPointer(DirectFunctionCall1(ltree_in
,
406 PointerGetDatum(s
)));
410 r
= ltree_concat(tmp
, a
);
414 PG_FREE_IF_COPY(a
, 1);
415 PG_FREE_IF_COPY(b
, 0);
416 PG_RETURN_POINTER(r
);
420 * Common code for variants of lca(), find longest common ancestor of inputs
422 * Returns NULL if there is no common ancestor, ie, the longest common
426 lca_inner(ltree
**a
, int len
)
438 return NULL
; /* no inputs? */
439 if ((*a
)->numlevel
== 0)
440 return NULL
; /* any empty input means NULL result */
442 /* num is the length of the longest common ancestor so far */
443 num
= (*a
)->numlevel
- 1;
445 /* Compare each additional input to *a */
447 while (ptr
- a
< len
)
449 if ((*ptr
)->numlevel
== 0)
451 else if ((*ptr
)->numlevel
== 1)
455 l1
= LTREE_FIRST(*a
);
456 l2
= LTREE_FIRST(*ptr
);
457 tmp
= Min(num
, (*ptr
)->numlevel
- 1);
459 for (i
= 0; i
< tmp
; i
++)
461 if (l1
->len
== l2
->len
&&
462 memcmp(l1
->name
, l2
->name
, l1
->len
) == 0)
473 /* Now compute size of result ... */
474 reslen
= LTREE_HDRSIZE
;
475 l1
= LTREE_FIRST(*a
);
476 for (i
= 0; i
< num
; i
++)
478 reslen
+= MAXALIGN(l1
->len
+ LEVEL_HDRSIZE
);
482 /* ... and construct it by copying from *a */
483 res
= (ltree
*) palloc0(reslen
);
484 SET_VARSIZE(res
, reslen
);
487 l1
= LTREE_FIRST(*a
);
488 l2
= LTREE_FIRST(res
);
490 for (i
= 0; i
< num
; i
++)
492 memcpy(l2
, l1
, MAXALIGN(l1
->len
+ LEVEL_HDRSIZE
));
501 lca(PG_FUNCTION_ARGS
)
507 a
= (ltree
**) palloc(sizeof(ltree
*) * fcinfo
->nargs
);
508 for (i
= 0; i
< fcinfo
->nargs
; i
++)
509 a
[i
] = PG_GETARG_LTREE_P(i
);
510 res
= lca_inner(a
, (int) fcinfo
->nargs
);
511 for (i
= 0; i
< fcinfo
->nargs
; i
++)
512 PG_FREE_IF_COPY(a
[i
], i
);
516 PG_RETURN_POINTER(res
);
522 text2ltree(PG_FUNCTION_ARGS
)
524 text
*in
= PG_GETARG_TEXT_PP(0);
528 s
= text_to_cstring(in
);
530 out
= (ltree
*) DatumGetPointer(DirectFunctionCall1(ltree_in
,
531 PointerGetDatum(s
)));
533 PG_FREE_IF_COPY(in
, 0);
534 PG_RETURN_POINTER(out
);
539 ltree2text(PG_FUNCTION_ARGS
)
541 ltree
*in
= PG_GETARG_LTREE_P(0);
544 ltree_level
*curlevel
;
547 out
= (text
*) palloc(VARSIZE(in
) + VARHDRSZ
);
549 curlevel
= LTREE_FIRST(in
);
550 for (i
= 0; i
< in
->numlevel
; i
++)
557 memcpy(ptr
, curlevel
->name
, curlevel
->len
);
558 ptr
+= curlevel
->len
;
559 curlevel
= LEVEL_NEXT(curlevel
);
562 SET_VARSIZE(out
, ptr
- ((char *) out
));
563 PG_FREE_IF_COPY(in
, 0);
565 PG_RETURN_POINTER(out
);
570 * ltreeparentsel - Selectivity of parent relationship for ltree data types.
572 * This function is not used anymore, if the ltree extension has been
573 * updated to 1.2 or later.
576 ltreeparentsel(PG_FUNCTION_ARGS
)
578 PlannerInfo
*root
= (PlannerInfo
*) PG_GETARG_POINTER(0);
579 Oid
operator = PG_GETARG_OID(1);
580 List
*args
= (List
*) PG_GETARG_POINTER(2);
581 int varRelid
= PG_GETARG_INT32(3);
584 /* Use generic restriction selectivity logic, with default 0.001. */
585 selec
= generic_restriction_selectivity(root
, operator, InvalidOid
,
589 PG_RETURN_FLOAT8((float8
) selec
);