Debian Codegraph @

src/backend/utils/adt/varlena.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * varlena.c
4 * Functions for the variable-length built-in types.
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/utils/adt/varlena.c
12 *
13 *-------------------------------------------------------------------------
14 */
15 #include "postgres.h"
16
17 #include <ctype.h>
18 #include <limits.h>
19
20 #include "access/detoast.h"
21 #include "access/toast_compression.h"
22 #include "catalog/pg_collation.h"
23 #include "catalog/pg_type.h"
24 #include "common/hashfn.h"
25 #include "common/int.h"
26 #include "common/unicode_category.h"
27 #include "common/unicode_norm.h"
28 #include "common/unicode_version.h"
29 #include "funcapi.h"
30 #include "lib/hyperloglog.h"
31 #include "libpq/pqformat.h"
32 #include "miscadmin.h"
33 #include "nodes/execnodes.h"
34 #include "parser/scansup.h"
35 #include "port/pg_bswap.h"
36 #include "regex/regex.h"
37 #include "utils/builtins.h"
38 #include "utils/bytea.h"
39 #include "utils/guc.h"
40 #include "utils/lsyscache.h"
41 #include "utils/memutils.h"
42 #include "utils/pg_locale.h"
43 #include "utils/sortsupport.h"
44 #include "utils/varlena.h"
45
46
47 /* GUC variable */
48 int bytea_output = BYTEA_OUTPUT_HEX;
49
50 typedef struct varlena VarString;
51
52 /*
53 * State for text_position_* functions.
54 */
55 typedef struct
56 {
57 pg_locale_t locale; /* collation used for substring matching */
58 bool is_multibyte_char_in_char; /* need to check char boundaries? */
59 bool greedy; /* find longest possible substring? */
60
61 char *str1; /* haystack string */
62 char *str2; /* needle string */
63 int len1; /* string lengths in bytes */
64 int len2;
65
66 /* Skip table for Boyer-Moore-Horspool search algorithm: */
67 int skiptablemask; /* mask for ANDing with skiptable subscripts */
68 int skiptable[256]; /* skip distance for given mismatched char */
69
70 /*
71 * Note that with nondeterministic collations, the length of the last
72 * match is not necessarily equal to the length of the "needle" passed in.
73 */
74 char *last_match; /* pointer to last match in 'str1' */
75 int last_match_len; /* length of last match */
76 int last_match_len_tmp; /* same but for internal use */
77
78 /*
79 * Sometimes we need to convert the byte position of a match to a
80 * character position. These store the last position that was converted,
81 * so that on the next call, we can continue from that point, rather than
82 * count characters from the very beginning.
83 */
84 char *refpoint; /* pointer within original haystack string */
85 int refpos; /* 0-based character offset of the same point */
86 } TextPositionState;
87
88 typedef struct
89 {
90 char *buf1; /* 1st string, or abbreviation original string
91 * buf */
92 char *buf2; /* 2nd string, or abbreviation strxfrm() buf */
93 int buflen1; /* Allocated length of buf1 */
94 int buflen2; /* Allocated length of buf2 */
95 int last_len1; /* Length of last buf1 string/strxfrm() input */
96 int last_len2; /* Length of last buf2 string/strxfrm() blob */
97 int last_returned; /* Last comparison result (cache) */
98 bool cache_blob; /* Does buf2 contain strxfrm() blob, etc? */
99 bool collate_c;
100 Oid typid; /* Actual datatype (text/bpchar/bytea/name) */
101 hyperLogLogState abbr_card; /* Abbreviated key cardinality state */
102 hyperLogLogState full_card; /* Full key cardinality state */
103 double prop_card; /* Required cardinality proportion */
104 pg_locale_t locale;
105 } VarStringSortSupport;
106
107 /*
108 * Output data for split_text(): we output either to an array or a table.
109 * tupstore and tupdesc must be set up in advance to output to a table.
110 */
111 typedef struct
112 {
113 ArrayBuildState *astate;
114 Tuplestorestate *tupstore;
115 TupleDesc tupdesc;
116 } SplitTextOutputData;
117
118 /*
119 * This should be large enough that most strings will fit, but small enough
120 * that we feel comfortable putting it on the stack
121 */
122 #define TEXTBUFLEN 1024
123
124 #define DatumGetVarStringP(X) ((VarString *) PG_DETOAST_DATUM(X))
125 #define DatumGetVarStringPP(X) ((VarString *) PG_DETOAST_DATUM_PACKED(X))
126
127 static int varstrfastcmp_c(Datum x, Datum y, SortSupport ssup);
128 static int bpcharfastcmp_c(Datum x, Datum y, SortSupport ssup);
129 static int namefastcmp_c(Datum x, Datum y, SortSupport ssup);
130 static int varlenafastcmp_locale(Datum x, Datum y, SortSupport ssup);
131 static int namefastcmp_locale(Datum x, Datum y, SortSupport ssup);
132 static int varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup);
133 static Datum varstr_abbrev_convert(Datum original, SortSupport ssup);
134 static bool varstr_abbrev_abort(int memtupcount, SortSupport ssup);
135 static int32 text_length(Datum str);
136 static text *text_catenate(text *t1, text *t2);
137 static text *text_substring(Datum str,
138 int32 start,
139 int32 length,
140 bool length_not_specified);
141 static int pg_mbcharcliplen_chars(const char *mbstr, int len, int limit);
142 static text *text_overlay(text *t1, text *t2, int sp, int sl);
143 static int text_position(text *t1, text *t2, Oid collid);
144 static void text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state);
145 static bool text_position_next(TextPositionState *state);
146 static char *text_position_next_internal(char *start_ptr, TextPositionState *state);
147 static char *text_position_get_match_ptr(TextPositionState *state);
148 static int text_position_get_match_pos(TextPositionState *state);
149 static void text_position_cleanup(TextPositionState *state);
150 static void check_collation_set(Oid collid);
151 static int text_cmp(text *arg1, text *arg2, Oid collid);
152 static bytea *bytea_catenate(bytea *t1, bytea *t2);
153 static bytea *bytea_substring(Datum str,
154 int S,
155 int L,
156 bool length_not_specified);
157 static bytea *bytea_overlay(bytea *t1, bytea *t2, int sp, int sl);
158 static void appendStringInfoText(StringInfo str, const text *t);
159 static bool split_text(FunctionCallInfo fcinfo, SplitTextOutputData *tstate);
160 static void split_text_accum_result(SplitTextOutputData *tstate,
161 text *field_value,
162 text *null_string,
163 Oid collation);
164 static text *array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v,
165 const char *fldsep, const char *null_string);
166 static StringInfo makeStringAggState(FunctionCallInfo fcinfo);
167 static bool text_format_parse_digits(const char **ptr, const char *end_ptr,
168 int *value);
169 static const char *text_format_parse_format(const char *start_ptr,
170 const char *end_ptr,
171 int *argpos, int *widthpos,
172 int *flags, int *width);
173 static void text_format_string_conversion(StringInfo buf, char conversion,
174 FmgrInfo *typOutputInfo,
175 Datum value, bool isNull,
176 int flags, int width);
177 static void text_format_append_string(StringInfo buf, const char *str,
178 int flags, int width);
179
180
181 /*****************************************************************************
182 * CONVERSION ROUTINES EXPORTED FOR USE BY C CODE *
183 *****************************************************************************/
184
185 /*
186 * cstring_to_text
187 *
188 * Create a text value from a null-terminated C string.
189 *
190 * The new text value is freshly palloc'd with a full-size VARHDR.
191 */
192 text *
193 cstring_to_text(const char *s)
194 {
195 return cstring_to_text_with_len(s, strlen(s));
196 }
197
198 /*
199 * cstring_to_text_with_len
200 *
201 * Same as cstring_to_text except the caller specifies the string length;
202 * the string need not be null_terminated.
203 */
204 text *
205 cstring_to_text_with_len(const char *s, int len)
206 {
207 text *result = (text *) palloc(len + VARHDRSZ);
208
209 SET_VARSIZE(result, len + VARHDRSZ);
210 memcpy(VARDATA(result), s, len);
211
212 return result;
213 }
214
215 /*
216 * text_to_cstring
217 *
218 * Create a palloc'd, null-terminated C string from a text value.
219 *
220 * We support being passed a compressed or toasted text value.
221 * This is a bit bogus since such values shouldn't really be referred to as
222 * "text *", but it seems useful for robustness. If we didn't handle that
223 * case here, we'd need another routine that did, anyway.
224 */
225 char *
226 text_to_cstring(const text *t)
227 {
228 /* must cast away the const, unfortunately */
229 text *tunpacked = pg_detoast_datum_packed(unconstify(text *, t));
230 int len = VARSIZE_ANY_EXHDR(tunpacked);
231 char *result;
232
233 result = (char *) palloc(len + 1);
234 memcpy(result, VARDATA_ANY(tunpacked), len);
235 result[len] = '\0';
236
237 if (tunpacked != t)
238 pfree(tunpacked);
239
240 return result;
241 }
242
243 /*
244 * text_to_cstring_buffer
245 *
246 * Copy a text value into a caller-supplied buffer of size dst_len.
247 *
248 * The text string is truncated if necessary to fit. The result is
249 * guaranteed null-terminated (unless dst_len == 0).
250 *
251 * We support being passed a compressed or toasted text value.
252 * This is a bit bogus since such values shouldn't really be referred to as
253 * "text *", but it seems useful for robustness. If we didn't handle that
254 * case here, we'd need another routine that did, anyway.
255 */
256 void
257 text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
258 {
259 /* must cast away the const, unfortunately */
260 text *srcunpacked = pg_detoast_datum_packed(unconstify(text *, src));
261 size_t src_len = VARSIZE_ANY_EXHDR(srcunpacked);
262
263 if (dst_len > 0)
264 {
265 dst_len--;
266 if (dst_len >= src_len)
267 dst_len = src_len;
268 else /* ensure truncation is encoding-safe */
269 dst_len = pg_mbcliplen(VARDATA_ANY(srcunpacked), src_len, dst_len);
270 memcpy(dst, VARDATA_ANY(srcunpacked), dst_len);
271 dst[dst_len] = '\0';
272 }
273
274 if (srcunpacked != src)
275 pfree(srcunpacked);
276 }
277
278
279 /*****************************************************************************
280 * USER I/O ROUTINES *
281 *****************************************************************************/
282
283
284 #define VAL(CH) ((CH) - '0')
285 #define DIG(VAL) ((VAL) + '0')
286
287 /*
288 * byteain - converts from printable representation of byte array
289 *
290 * Non-printable characters must be passed as '\nnn' (octal) and are
291 * converted to internal form. '\' must be passed as '\\'.
292 * ereport(ERROR, ...) if bad form.
293 *
294 * BUGS:
295 * The input is scanned twice.
296 * The error checking of input is minimal.
297 */
298 Datum
299 byteain(PG_FUNCTION_ARGS)
300 {
301 char *inputText = PG_GETARG_CSTRING(0);
302 Node *escontext = fcinfo->context;
303 char *tp;
304 char *rp;
305 int bc;
306 bytea *result;
307
308 /* Recognize hex input */
309 if (inputText[0] == '\\' && inputText[1] == 'x')
310 {
311 size_t len = strlen(inputText);
312
313 bc = (len - 2) / 2 + VARHDRSZ; /* maximum possible length */
314 result = palloc(bc);
315 bc = hex_decode_safe(inputText + 2, len - 2, VARDATA(result),
316 escontext);
317 SET_VARSIZE(result, bc + VARHDRSZ); /* actual length */
318
319 PG_RETURN_BYTEA_P(result);
320 }
321
322 /* Else, it's the traditional escaped style */
323 for (bc = 0, tp = inputText; *tp != '\0'; bc++)
324 {
325 if (tp[0] != '\\')
326 tp++;
327 else if ((tp[0] == '\\') &&
328 (tp[1] >= '0' && tp[1] <= '3') &&
329 (tp[2] >= '0' && tp[2] <= '7') &&
330 (tp[3] >= '0' && tp[3] <= '7'))
331 tp += 4;
332 else if ((tp[0] == '\\') &&
333 (tp[1] == '\\'))
334 tp += 2;
335 else
336 {
337 /*
338 * one backslash, not followed by another or ### valid octal
339 */
340 ereturn(escontext, (Datum) 0,
341 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
342 errmsg("invalid input syntax for type %s", "bytea")));
343 }
344 }
345
346 bc += VARHDRSZ;
347
348 result = (bytea *) palloc(bc);
349 SET_VARSIZE(result, bc);
350
351 tp = inputText;
352 rp = VARDATA(result);
353 while (*tp != '\0')
354 {
355 if (tp[0] != '\\')
356 *rp++ = *tp++;
357 else if ((tp[0] == '\\') &&
358 (tp[1] >= '0' && tp[1] <= '3') &&
359 (tp[2] >= '0' && tp[2] <= '7') &&
360 (tp[3] >= '0' && tp[3] <= '7'))
361 {
362 bc = VAL(tp[1]);
363 bc <<= 3;
364 bc += VAL(tp[2]);
365 bc <<= 3;
366 *rp++ = bc + VAL(tp[3]);
367
368 tp += 4;
369 }
370 else if ((tp[0] == '\\') &&
371 (tp[1] == '\\'))
372 {
373 *rp++ = '\\';
374 tp += 2;
375 }
376 else
377 {
378 /*
379 * We should never get here. The first pass should not allow it.
380 */
381 ereturn(escontext, (Datum) 0,
382 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
383 errmsg("invalid input syntax for type %s", "bytea")));
384 }
385 }
386
387 PG_RETURN_BYTEA_P(result);
388 }
389
390 /*
391 * byteaout - converts to printable representation of byte array
392 *
393 * In the traditional escaped format, non-printable characters are
394 * printed as '\nnn' (octal) and '\' as '\\'.
395 */
396 Datum
397 byteaout(PG_FUNCTION_ARGS)
398 {
399 bytea *vlena = PG_GETARG_BYTEA_PP(0);
400 char *result;
401 char *rp;
402
403 if (bytea_output == BYTEA_OUTPUT_HEX)
404 {
405 /* Print hex format */
406 rp = result = palloc(VARSIZE_ANY_EXHDR(vlena) * 2 + 2 + 1);
407 *rp++ = '\\';
408 *rp++ = 'x';
409 rp += hex_encode(VARDATA_ANY(vlena), VARSIZE_ANY_EXHDR(vlena), rp);
410 }
411 else if (bytea_output == BYTEA_OUTPUT_ESCAPE)
412 {
413 /* Print traditional escaped format */
414 char *vp;
415 uint64 len;
416 int i;
417
418 len = 1; /* empty string has 1 char */
419 vp = VARDATA_ANY(vlena);
420 for (i = VARSIZE_ANY_EXHDR(vlena); i != 0; i--, vp++)
421 {
422 if (*vp == '\\')
423 len += 2;
424 else if ((unsigned char) *vp < 0x20 || (unsigned char) *vp > 0x7e)
425 len += 4;
426 else
427 len++;
428 }
429
430 /*
431 * In principle len can't overflow uint32 if the input fit in 1GB, but
432 * for safety let's check rather than relying on palloc's internal
433 * check.
434 */
435 if (len > MaxAllocSize)
436 ereport(ERROR,
437 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
438 errmsg_internal("result of bytea output conversion is too large")));
439 rp = result = (char *) palloc(len);
440
441 vp = VARDATA_ANY(vlena);
442 for (i = VARSIZE_ANY_EXHDR(vlena); i != 0; i--, vp++)
443 {
444 if (*vp == '\\')
445 {
446 *rp++ = '\\';
447 *rp++ = '\\';
448 }
449 else if ((unsigned char) *vp < 0x20 || (unsigned char) *vp > 0x7e)
450 {
451 int val; /* holds unprintable chars */
452
453 val = *vp;
454 rp[0] = '\\';
455 rp[3] = DIG(val & 07);
456 val >>= 3;
457 rp[2] = DIG(val & 07);
458 val >>= 3;
459 rp[1] = DIG(val & 03);
460 rp += 4;
461 }
462 else
463 *rp++ = *vp;
464 }
465 }
466 else
467 {
468 elog(ERROR, "unrecognized \"bytea_output\" setting: %d",
469 bytea_output);
470 rp = result = NULL; /* keep compiler quiet */
471 }
472 *rp = '\0';
473 PG_RETURN_CSTRING(result);
474 }
475
476 /*
477 * bytearecv - converts external binary format to bytea
478 */
479 Datum
480 bytearecv(PG_FUNCTION_ARGS)
481 {
482 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
483 bytea *result;
484 int nbytes;
485
486 nbytes = buf->len - buf->cursor;
487 result = (bytea *) palloc(nbytes + VARHDRSZ);
488 SET_VARSIZE(result, nbytes + VARHDRSZ);
489 pq_copymsgbytes(buf, VARDATA(result), nbytes);
490 PG_RETURN_BYTEA_P(result);
491 }
492
493 /*
494 * byteasend - converts bytea to binary format
495 *
496 * This is a special case: just copy the input...
497 */
498 Datum
499 byteasend(PG_FUNCTION_ARGS)
500 {
501 bytea *vlena = PG_GETARG_BYTEA_P_COPY(0);
502
503 PG_RETURN_BYTEA_P(vlena);
504 }
505
506 Datum
507 bytea_string_agg_transfn(PG_FUNCTION_ARGS)
508 {
509 StringInfo state;
510
511 state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
512
513 /* Append the value unless null, preceding it with the delimiter. */
514 if (!PG_ARGISNULL(1))
515 {
516 bytea *value = PG_GETARG_BYTEA_PP(1);
517 bool isfirst = false;
518
519 /*
520 * You might think we can just throw away the first delimiter, however
521 * we must keep it as we may be a parallel worker doing partial
522 * aggregation building a state to send to the main process. We need
523 * to keep the delimiter of every aggregation so that the combine
524 * function can properly join up the strings of two separately
525 * partially aggregated results. The first delimiter is only stripped
526 * off in the final function. To know how much to strip off the front
527 * of the string, we store the length of the first delimiter in the
528 * StringInfo's cursor field, which we don't otherwise need here.
529 */
530 if (state == NULL)
531 {
532 state = makeStringAggState(fcinfo);
533 isfirst = true;
534 }
535
536 if (!PG_ARGISNULL(2))
537 {
538 bytea *delim = PG_GETARG_BYTEA_PP(2);
539
540 appendBinaryStringInfo(state, VARDATA_ANY(delim),
541 VARSIZE_ANY_EXHDR(delim));
542 if (isfirst)
543 state->cursor = VARSIZE_ANY_EXHDR(delim);
544 }
545
546 appendBinaryStringInfo(state, VARDATA_ANY(value),
547 VARSIZE_ANY_EXHDR(value));
548 }
549
550 /*
551 * The transition type for string_agg() is declared to be "internal",
552 * which is a pass-by-value type the same size as a pointer.
553 */
554 if (state)
555 PG_RETURN_POINTER(state);
556 PG_RETURN_NULL();
557 }
558
559 Datum
560 bytea_string_agg_finalfn(PG_FUNCTION_ARGS)
561 {
562 StringInfo state;
563
564 /* cannot be called directly because of internal-type argument */
565 Assert(AggCheckCallContext(fcinfo, NULL));
566
567 state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
568
569 if (state != NULL)
570 {
571 /* As per comment in transfn, strip data before the cursor position */
572 bytea *result;
573 int strippedlen = state->len - state->cursor;
574
575 result = (bytea *) palloc(strippedlen + VARHDRSZ);
576 SET_VARSIZE(result, strippedlen + VARHDRSZ);
577 memcpy(VARDATA(result), &state->data[state->cursor], strippedlen);
578 PG_RETURN_BYTEA_P(result);
579 }
580 else
581 PG_RETURN_NULL();
582 }
583
584 /*
585 * textin - converts cstring to internal representation
586 */
587 Datum
588 textin(PG_FUNCTION_ARGS)
589 {
590 char *inputText = PG_GETARG_CSTRING(0);
591
592 PG_RETURN_TEXT_P(cstring_to_text(inputText));
593 }
594
595 /*
596 * textout - converts internal representation to cstring
597 */
598 Datum
599 textout(PG_FUNCTION_ARGS)
600 {
601 Datum txt = PG_GETARG_DATUM(0);
602
603 PG_RETURN_CSTRING(TextDatumGetCString(txt));
604 }
605
606 /*
607 * textrecv - converts external binary format to text
608 */
609 Datum
610 textrecv(PG_FUNCTION_ARGS)
611 {
612 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
613 text *result;
614 char *str;
615 int nbytes;
616
617 str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
618
619 result = cstring_to_text_with_len(str, nbytes);
620 pfree(str);
621 PG_RETURN_TEXT_P(result);
622 }
623
624 /*
625 * textsend - converts text to binary format
626 */
627 Datum
628 textsend(PG_FUNCTION_ARGS)
629 {
630 text *t = PG_GETARG_TEXT_PP(0);
631 StringInfoData buf;
632
633 pq_begintypsend(&buf);
634 pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
635 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
636 }
637
638
639 /*
640 * unknownin - converts cstring to internal representation
641 */
642 Datum
643 unknownin(PG_FUNCTION_ARGS)
644 {
645 char *str = PG_GETARG_CSTRING(0);
646
647 /* representation is same as cstring */
648 PG_RETURN_CSTRING(pstrdup(str));
649 }
650
651 /*
652 * unknownout - converts internal representation to cstring
653 */
654 Datum
655 unknownout(PG_FUNCTION_ARGS)
656 {
657 /* representation is same as cstring */
658 char *str = PG_GETARG_CSTRING(0);
659
660 PG_RETURN_CSTRING(pstrdup(str));
661 }
662
663 /*
664 * unknownrecv - converts external binary format to unknown
665 */
666 Datum
667 unknownrecv(PG_FUNCTION_ARGS)
668 {
669 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
670 char *str;
671 int nbytes;
672
673 str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
674 /* representation is same as cstring */
675 PG_RETURN_CSTRING(str);
676 }
677
678 /*
679 * unknownsend - converts unknown to binary format
680 */
681 Datum
682 unknownsend(PG_FUNCTION_ARGS)
683 {
684 /* representation is same as cstring */
685 char *str = PG_GETARG_CSTRING(0);
686 StringInfoData buf;
687
688 pq_begintypsend(&buf);
689 pq_sendtext(&buf, str, strlen(str));
690 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
691 }
692
693
694 /* ========== PUBLIC ROUTINES ========== */
695
696 /*
697 * textlen -
698 * returns the logical length of a text*
699 * (which is less than the VARSIZE of the text*)
700 */
701 Datum
702 textlen(PG_FUNCTION_ARGS)
703 {
704 Datum str = PG_GETARG_DATUM(0);
705
706 /* try to avoid decompressing argument */
707 PG_RETURN_INT32(text_length(str));
708 }
709
710 /*
711 * text_length -
712 * Does the real work for textlen()
713 *
714 * This is broken out so it can be called directly by other string processing
715 * functions. Note that the argument is passed as a Datum, to indicate that
716 * it may still be in compressed form. We can avoid decompressing it at all
717 * in some cases.
718 */
719 static int32
720 text_length(Datum str)
721 {
722 /* fastpath when max encoding length is one */
723 if (pg_database_encoding_max_length() == 1)
724 PG_RETURN_INT32(toast_raw_datum_size(str) - VARHDRSZ);
725 else
726 {
727 text *t = DatumGetTextPP(str);
728
729 PG_RETURN_INT32(pg_mbstrlen_with_len(VARDATA_ANY(t),
730 VARSIZE_ANY_EXHDR(t)));
731 }
732 }
733
734 /*
735 * textoctetlen -
736 * returns the physical length of a text*
737 * (which is less than the VARSIZE of the text*)
738 */
739 Datum
740 textoctetlen(PG_FUNCTION_ARGS)
741 {
742 Datum str = PG_GETARG_DATUM(0);
743
744 /* We need not detoast the input at all */
745 PG_RETURN_INT32(toast_raw_datum_size(str) - VARHDRSZ);
746 }
747
748 /*
749 * textcat -
750 * takes two text* and returns a text* that is the concatenation of
751 * the two.
752 *
753 * Rewritten by Sapa, [email protected]. 8-Jul-96.
754 * Updated by Thomas, [email protected] 1997-07-10.
755 * Allocate space for output in all cases.
756 * XXX - thomas 1997-07-10
757 */
758 Datum
759 textcat(PG_FUNCTION_ARGS)
760 {
761 text *t1 = PG_GETARG_TEXT_PP(0);
762 text *t2 = PG_GETARG_TEXT_PP(1);
763
764 PG_RETURN_TEXT_P(text_catenate(t1, t2));
765 }
766
767 /*
768 * text_catenate
769 * Guts of textcat(), broken out so it can be used by other functions
770 *
771 * Arguments can be in short-header form, but not compressed or out-of-line
772 */
773 static text *
774 text_catenate(text *t1, text *t2)
775 {
776 text *result;
777 int len1,
778 len2,
779 len;
780 char *ptr;
781
782 len1 = VARSIZE_ANY_EXHDR(t1);
783 len2 = VARSIZE_ANY_EXHDR(t2);
784
785 /* paranoia ... probably should throw error instead? */
786 if (len1 < 0)
787 len1 = 0;
788 if (len2 < 0)
789 len2 = 0;
790
791 len = len1 + len2 + VARHDRSZ;
792 result = (text *) palloc(len);
793
794 /* Set size of result string... */
795 SET_VARSIZE(result, len);
796
797 /* Fill data field of result string... */
798 ptr = VARDATA(result);
799 if (len1 > 0)
800 memcpy(ptr, VARDATA_ANY(t1), len1);
801 if (len2 > 0)
802 memcpy(ptr + len1, VARDATA_ANY(t2), len2);
803
804 return result;
805 }
806
807 /*
808 * charlen_to_bytelen()
809 * Compute the number of bytes occupied by n characters starting at *p
810 *
811 * The caller shall ensure there are n complete characters. Callers achieve
812 * this by deriving "n" from regmatch_t findings from searching a wchar array.
813 * pg_mb2wchar_with_len() skips any trailing incomplete character, so regex
814 * matches will end no later than the last complete character. (The string
815 * need not be null-terminated.)
816 */
817 static int
818 charlen_to_bytelen(const char *p, int n)
819 {
820 if (pg_database_encoding_max_length() == 1)
821 {
822 /* Optimization for single-byte encodings */
823 return n;
824 }
825 else
826 {
827 const char *s;
828
829 for (s = p; n > 0; n--)
830 s += pg_mblen_unbounded(s); /* caller verified encoding */
831
832 return s - p;
833 }
834 }
835
836 /*
837 * text_substr()
838 * Return a substring starting at the specified position.
839 * - thomas 1997-12-31
840 *
841 * Input:
842 * - string
843 * - starting position (is one-based)
844 * - string length
845 *
846 * If the starting position is zero or less, then return from the start of the string
847 * adjusting the length to be consistent with the "negative start" per SQL.
848 * If the length is less than zero, return the remaining string.
849 *
850 * Added multibyte support.
851 * - Tatsuo Ishii 1998-4-21
852 * Changed behavior if starting position is less than one to conform to SQL behavior.
853 * Formerly returned the entire string; now returns a portion.
854 * - Thomas Lockhart 1998-12-10
855 * Now uses faster TOAST-slicing interface
856 * - John Gray 2002-02-22
857 * Remove "#ifdef MULTIBYTE" and test for encoding_max_length instead. Change
858 * behaviors conflicting with SQL to meet SQL (if E = S + L < S throw
859 * error; if E < 1, return '', not entire string). Fixed MB related bug when
860 * S > LC and < LC + 4 sometimes garbage characters are returned.
861 * - Joe Conway 2002-08-10
862 */
863 Datum
864 text_substr(PG_FUNCTION_ARGS)
865 {
866 PG_RETURN_TEXT_P(text_substring(PG_GETARG_DATUM(0),
867 PG_GETARG_INT32(1),
868 PG_GETARG_INT32(2),
869 false));
870 }
871
872 /*
873 * text_substr_no_len -
874 * Wrapper to avoid opr_sanity failure due to
875 * one function accepting a different number of args.
876 */
877 Datum
878 text_substr_no_len(PG_FUNCTION_ARGS)
879 {
880 PG_RETURN_TEXT_P(text_substring(PG_GETARG_DATUM(0),
881 PG_GETARG_INT32(1),
882 -1, true));
883 }
884
885 /*
886 * text_substring -
887 * Does the real work for text_substr() and text_substr_no_len()
888 *
889 * This is broken out so it can be called directly by other string processing
890 * functions. Note that the argument is passed as a Datum, to indicate that
891 * it may still be in compressed/toasted form. We can avoid detoasting all
892 * of it in some cases.
893 *
894 * The result is always a freshly palloc'd datum.
895 */
896 static text *
897 text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
898 {
899 int32 eml = pg_database_encoding_max_length();
900 int32 S = start; /* start position */
901 int32 S1; /* adjusted start position */
902 int32 L1; /* adjusted substring length */
903 int32 E; /* end position, exclusive */
904
905 /*
906 * SQL99 says S can be zero or negative (which we don't document), but we
907 * still must fetch from the start of the string.
908 * https://www.postgresql.org/message-id/170905442373.643.11536838320909376197%40wrigleys.postgresql.org
909 */
910 S1 = Max(S, 1);
911
912 /* life is easy if the encoding max length is 1 */
913 if (eml == 1)
914 {
915 if (length_not_specified) /* special case - get length to end of
916 * string */
917 L1 = -1;
918 else if (length < 0)
919 {
920 /* SQL99 says to throw an error for E < S, i.e., negative length */
921 ereport(ERROR,
922 (errcode(ERRCODE_SUBSTRING_ERROR),
923 errmsg("negative substring length not allowed")));
924 L1 = -1; /* silence stupider compilers */
925 }
926 else if (pg_add_s32_overflow(S, length, &E))
927 {
928 /*
929 * L could be large enough for S + L to overflow, in which case
930 * the substring must run to end of string.
931 */
932 L1 = -1;
933 }
934 else
935 {
936 /*
937 * A zero or negative value for the end position can happen if the
938 * start was negative or one. SQL99 says to return a zero-length
939 * string.
940 */
941 if (E < 1)
942 return cstring_to_text("");
943
944 L1 = E - S1;
945 }
946
947 /*
948 * If the start position is past the end of the string, SQL99 says to
949 * return a zero-length string -- DatumGetTextPSlice() will do that
950 * for us. We need only convert S1 to zero-based starting position.
951 */
952 return DatumGetTextPSlice(str, S1 - 1, L1);
953 }
954 else if (eml > 1)
955 {
956 /*
957 * When encoding max length is > 1, we can't get LC without
958 * detoasting, so we'll grab a conservatively large slice now and go
959 * back later to do the right thing
960 */
961 int32 slice_start;
962 int32 slice_size;
963 int32 slice_strlen;
964 int32 slice_len;
965 text *slice;
966 int32 E1;
967 int32 i;
968 char *p;
969 char *s;
970 text *ret;
971
972 /*
973 * We need to start at position zero because there is no way to know
974 * in advance which byte offset corresponds to the supplied start
975 * position.
976 */
977 slice_start = 0;
978
979 if (length_not_specified) /* special case - get length to end of
980 * string */
981 E = slice_size = L1 = -1;
982 else if (length < 0)
983 {
984 /* SQL99 says to throw an error for E < S, i.e., negative length */
985 ereport(ERROR,
986 (errcode(ERRCODE_SUBSTRING_ERROR),
987 errmsg("negative substring length not allowed")));
988 E = slice_size = L1 = -1; /* silence stupider compilers */
989 }
990 else if (pg_add_s32_overflow(S, length, &E))
991 {
992 /*
993 * L could be large enough for S + L to overflow, in which case
994 * the substring must run to end of string.
995 */
996 slice_size = L1 = -1;
997 }
998 else
999 {
1000 /*
1001 * Ending at position 1, exclusive, obviously yields an empty
1002 * string. A zero or negative value can happen if the start was
1003 * negative or one. SQL99 says to return a zero-length string.
1004 */
1005 if (E <= 1)
1006 return cstring_to_text("");
1007
1008 /*
1009 * if E is past the end of the string, the tuple toaster will
1010 * truncate the length for us
1011 */
1012 L1 = E - S1;
1013
1014 /*
1015 * Total slice size in bytes can't be any longer than the
1016 * inclusive end position times the encoding max length. If that
1017 * overflows, we can just use -1.
1018 */
1019 if (pg_mul_s32_overflow(E - 1, eml, &slice_size))
1020 slice_size = -1;
1021 }
1022
1023 /*
1024 * If we're working with an untoasted source, no need to do an extra
1025 * copying step.
1026 */
1027 if (VARATT_IS_COMPRESSED(DatumGetPointer(str)) ||
1028 VARATT_IS_EXTERNAL(DatumGetPointer(str)))
1029 slice = DatumGetTextPSlice(str, slice_start, slice_size);
1030 else
1031 slice = (text *) DatumGetPointer(str);
1032
1033 /* see if we got back an empty string */
1034 slice_len = VARSIZE_ANY_EXHDR(slice);
1035 if (slice_len == 0)
1036 {
1037 if (slice != (text *) DatumGetPointer(str))
1038 pfree(slice);
1039 return cstring_to_text("");
1040 }
1041
1042 /*
1043 * Now we can get the actual length of the slice in MB characters,
1044 * stopping at the end of the substring. Continuing beyond the
1045 * substring end could find an incomplete character attributable
1046 * solely to DatumGetTextPSlice() chopping in the middle of a
1047 * character, and it would be superfluous work at best.
1048 */
1049 slice_strlen =
1050 (slice_size == -1 ?
1051 pg_mbstrlen_with_len(VARDATA_ANY(slice), slice_len) :
1052 pg_mbcharcliplen_chars(VARDATA_ANY(slice), slice_len, E - 1));
1053
1054 /*
1055 * Check that the start position wasn't > slice_strlen. If so, SQL99
1056 * says to return a zero-length string.
1057 */
1058 if (S1 > slice_strlen)
1059 {
1060 if (slice != (text *) DatumGetPointer(str))
1061 pfree(slice);
1062 return cstring_to_text("");
1063 }
1064
1065 /*
1066 * Adjust L1 and E1 now that we know the slice string length. Again
1067 * remember that S1 is one based, and slice_start is zero based.
1068 */
1069 if (L1 > -1)
1070 E1 = Min(S1 + L1, slice_start + 1 + slice_strlen);
1071 else
1072 E1 = slice_start + 1 + slice_strlen;
1073
1074 /*
1075 * Find the start position in the slice; remember S1 is not zero based
1076 */
1077 p = VARDATA_ANY(slice);
1078 for (i = 0; i < S1 - 1; i++)
1079 p += pg_mblen_unbounded(p);
1080
1081 /* hang onto a pointer to our start position */
1082 s = p;
1083
1084 /*
1085 * Count the actual bytes used by the substring of the requested
1086 * length.
1087 */
1088 for (i = S1; i < E1; i++)
1089 p += pg_mblen_unbounded(p);
1090
1091 ret = (text *) palloc(VARHDRSZ + (p - s));
1092 SET_VARSIZE(ret, VARHDRSZ + (p - s));
1093 memcpy(VARDATA(ret), s, (p - s));
1094
1095 if (slice != (text *) DatumGetPointer(str))
1096 pfree(slice);
1097
1098 return ret;
1099 }
1100 else
1101 elog(ERROR, "invalid backend encoding: encoding max length < 1");
1102
1103 /* not reached: suppress compiler warning */
1104 return NULL;
1105 }
1106
1107 /*
1108 * pg_mbcharcliplen_chars -
1109 * Mirror pg_mbcharcliplen(), except return value unit is chars, not bytes.
1110 *
1111 * This mirrors all the dubious historical behavior, so it's static to
1112 * discourage proliferation. The assertions are specific to the one caller.
1113 */
1114 static int
1115 pg_mbcharcliplen_chars(const char *mbstr, int len, int limit)
1116 {
1117 int nch = 0;
1118 int l;
1119
1120 Assert(len > 0);
1121 Assert(limit > 0);
1122 Assert(pg_database_encoding_max_length() > 1);
1123
1124 while (len > 0 && *mbstr)
1125 {
1126 l = pg_mblen_with_len(mbstr, len);
1127 nch++;
1128 if (nch == limit)
1129 break;
1130 len -= l;
1131 mbstr += l;
1132 }
1133 return nch;
1134 }
1135
1136 /*
1137 * textoverlay
1138 * Replace specified substring of first string with second
1139 *
1140 * The SQL standard defines OVERLAY() in terms of substring and concatenation.
1141 * This code is a direct implementation of what the standard says.
1142 */
1143 Datum
1144 textoverlay(PG_FUNCTION_ARGS)
1145 {
1146 text *t1 = PG_GETARG_TEXT_PP(0);
1147 text *t2 = PG_GETARG_TEXT_PP(1);
1148 int sp = PG_GETARG_INT32(2); /* substring start position */
1149 int sl = PG_GETARG_INT32(3); /* substring length */
1150
1151 PG_RETURN_TEXT_P(text_overlay(t1, t2, sp, sl));
1152 }
1153
1154 Datum
1155 textoverlay_no_len(PG_FUNCTION_ARGS)
1156 {
1157 text *t1 = PG_GETARG_TEXT_PP(0);
1158 text *t2 = PG_GETARG_TEXT_PP(1);
1159 int sp = PG_GETARG_INT32(2); /* substring start position */
1160 int sl;
1161
1162 sl = text_length(PointerGetDatum(t2)); /* defaults to length(t2) */
1163 PG_RETURN_TEXT_P(text_overlay(t1, t2, sp, sl));
1164 }
1165
1166 static text *
1167 text_overlay(text *t1, text *t2, int sp, int sl)
1168 {
1169 text *result;
1170 text *s1;
1171 text *s2;
1172 int sp_pl_sl;
1173
1174 /*
1175 * Check for possible integer-overflow cases. For negative sp, throw a
1176 * "substring length" error because that's what should be expected
1177 * according to the spec's definition of OVERLAY().
1178 */
1179 if (sp <= 0)
1180 ereport(ERROR,
1181 (errcode(ERRCODE_SUBSTRING_ERROR),
1182 errmsg("negative substring length not allowed")));
1183 if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
1184 ereport(ERROR,
1185 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1186 errmsg("integer out of range")));
1187
1188 s1 = text_substring(PointerGetDatum(t1), 1, sp - 1, false);
1189 s2 = text_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
1190 result = text_catenate(s1, t2);
1191 result = text_catenate(result, s2);
1192
1193 return result;
1194 }
1195
1196 /*
1197 * textpos -
1198 * Return the position of the specified substring.
1199 * Implements the SQL POSITION() function.
1200 * Ref: A Guide To The SQL Standard, Date & Darwen, 1997
1201 * - thomas 1997-07-27
1202 */
1203 Datum
1204 textpos(PG_FUNCTION_ARGS)
1205 {
1206 text *str = PG_GETARG_TEXT_PP(0);
1207 text *search_str = PG_GETARG_TEXT_PP(1);
1208
1209 PG_RETURN_INT32((int32) text_position(str, search_str, PG_GET_COLLATION()));
1210 }
1211
1212 /*
1213 * text_position -
1214 * Does the real work for textpos()
1215 *
1216 * Inputs:
1217 * t1 - string to be searched
1218 * t2 - pattern to match within t1
1219 * Result:
1220 * Character index of the first matched char, starting from 1,
1221 * or 0 if no match.
1222 *
1223 * This is broken out so it can be called directly by other string processing
1224 * functions.
1225 */
1226 static int
1227 text_position(text *t1, text *t2, Oid collid)
1228 {
1229 TextPositionState state;
1230 int result;
1231
1232 check_collation_set(collid);
1233
1234 /* Empty needle always matches at position 1 */
1235 if (VARSIZE_ANY_EXHDR(t2) < 1)
1236 return 1;
1237
1238 /* Otherwise, can't match if haystack is shorter than needle */
1239 if (VARSIZE_ANY_EXHDR(t1) < VARSIZE_ANY_EXHDR(t2) &&
1240 pg_newlocale_from_collation(collid)->deterministic)
1241 return 0;
1242
1243 text_position_setup(t1, t2, collid, &state);
1244 /* don't need greedy mode here */
1245 state.greedy = false;
1246
1247 if (!text_position_next(&state))
1248 result = 0;
1249 else
1250 result = text_position_get_match_pos(&state);
1251 text_position_cleanup(&state);
1252 return result;
1253 }
1254
1255
1256 /*
1257 * text_position_setup, text_position_next, text_position_cleanup -
1258 * Component steps of text_position()
1259 *
1260 * These are broken out so that a string can be efficiently searched for
1261 * multiple occurrences of the same pattern. text_position_next may be
1262 * called multiple times, and it advances to the next match on each call.
1263 * text_position_get_match_ptr() and text_position_get_match_pos() return
1264 * a pointer or 1-based character position of the last match, respectively.
1265 *
1266 * The "state" variable is normally just a local variable in the caller.
1267 *
1268 * NOTE: text_position_next skips over the matched portion. For example,
1269 * searching for "xx" in "xxx" returns only one match, not two.
1270 */
1271
1272 static void
1273 text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state)
1274 {
1275 int len1 = VARSIZE_ANY_EXHDR(t1);
1276 int len2 = VARSIZE_ANY_EXHDR(t2);
1277
1278 check_collation_set(collid);
1279
1280 state->locale = pg_newlocale_from_collation(collid);
1281
1282 /*
1283 * Most callers need greedy mode, but some might want to unset this to
1284 * optimize.
1285 */
1286 state->greedy = true;
1287
1288 Assert(len2 > 0);
1289
1290 /*
1291 * Even with a multi-byte encoding, we perform the search using the raw
1292 * byte sequence, ignoring multibyte issues. For UTF-8, that works fine,
1293 * because in UTF-8 the byte sequence of one character cannot contain
1294 * another character. For other multi-byte encodings, we do the search
1295 * initially as a simple byte search, ignoring multibyte issues, but
1296 * verify afterwards that the match we found is at a character boundary,
1297 * and continue the search if it was a false match.
1298 */
1299 if (pg_database_encoding_max_length() == 1)
1300 state->is_multibyte_char_in_char = false;
1301 else if (GetDatabaseEncoding() == PG_UTF8)
1302 state->is_multibyte_char_in_char = false;
1303 else
1304 state->is_multibyte_char_in_char = true;
1305
1306 state->str1 = VARDATA_ANY(t1);
1307 state->str2 = VARDATA_ANY(t2);
1308 state->len1 = len1;
1309 state->len2 = len2;
1310 state->last_match = NULL;
1311 state->refpoint = state->str1;
1312 state->refpos = 0;
1313
1314 /*
1315 * Prepare the skip table for Boyer-Moore-Horspool searching. In these
1316 * notes we use the terminology that the "haystack" is the string to be
1317 * searched (t1) and the "needle" is the pattern being sought (t2).
1318 *
1319 * If the needle is empty or bigger than the haystack then there is no
1320 * point in wasting cycles initializing the table. We also choose not to
1321 * use B-M-H for needles of length 1, since the skip table can't possibly
1322 * save anything in that case.
1323 *
1324 * (With nondeterministic collations, the search is already
1325 * multibyte-aware, so we don't need this.)
1326 */
1327 if (len1 >= len2 && len2 > 1 && state->locale->deterministic)
1328 {
1329 int searchlength = len1 - len2;
1330 int skiptablemask;
1331 int last;
1332 int i;
1333 const char *str2 = state->str2;
1334
1335 /*
1336 * First we must determine how much of the skip table to use. The
1337 * declaration of TextPositionState allows up to 256 elements, but for
1338 * short search problems we don't really want to have to initialize so
1339 * many elements --- it would take too long in comparison to the
1340 * actual search time. So we choose a useful skip table size based on
1341 * the haystack length minus the needle length. The closer the needle
1342 * length is to the haystack length the less useful skipping becomes.
1343 *
1344 * Note: since we use bit-masking to select table elements, the skip
1345 * table size MUST be a power of 2, and so the mask must be 2^N-1.
1346 */
1347 if (searchlength < 16)
1348 skiptablemask = 3;
1349 else if (searchlength < 64)
1350 skiptablemask = 7;
1351 else if (searchlength < 128)
1352 skiptablemask = 15;
1353 else if (searchlength < 512)
1354 skiptablemask = 31;
1355 else if (searchlength < 2048)
1356 skiptablemask = 63;
1357 else if (searchlength < 4096)
1358 skiptablemask = 127;
1359 else
1360 skiptablemask = 255;
1361 state->skiptablemask = skiptablemask;
1362
1363 /*
1364 * Initialize the skip table. We set all elements to the needle
1365 * length, since this is the correct skip distance for any character
1366 * not found in the needle.
1367 */
1368 for (i = 0; i <= skiptablemask; i++)
1369 state->skiptable[i] = len2;
1370
1371 /*
1372 * Now examine the needle. For each character except the last one,
1373 * set the corresponding table element to the appropriate skip
1374 * distance. Note that when two characters share the same skip table
1375 * entry, the one later in the needle must determine the skip
1376 * distance.
1377 */
1378 last = len2 - 1;
1379
1380 for (i = 0; i < last; i++)
1381 state->skiptable[(unsigned char) str2[i] & skiptablemask] = last - i;
1382 }
1383 }
1384
1385 /*
1386 * Advance to the next match, starting from the end of the previous match
1387 * (or the beginning of the string, on first call). Returns true if a match
1388 * is found.
1389 *
1390 * Note that this refuses to match an empty-string needle. Most callers
1391 * will have handled that case specially and we'll never see it here.
1392 */
1393 static bool
1394 text_position_next(TextPositionState *state)
1395 {
1396 int needle_len = state->len2;
1397 char *start_ptr;
1398 char *matchptr;
1399
1400 if (needle_len <= 0)
1401 return false; /* result for empty pattern */
1402
1403 /* Start from the point right after the previous match. */
1404 if (state->last_match)
1405 start_ptr = state->last_match + state->last_match_len;
1406 else
1407 start_ptr = state->str1;
1408
1409 retry:
1410 matchptr = text_position_next_internal(start_ptr, state);
1411
1412 if (!matchptr)
1413 return false;
1414
1415 /*
1416 * Found a match for the byte sequence. If this is a multibyte encoding,
1417 * where one character's byte sequence can appear inside a longer
1418 * multi-byte character, we need to verify that the match was at a
1419 * character boundary, not in the middle of a multi-byte character.
1420 */
1421 if (state->is_multibyte_char_in_char && state->locale->deterministic)
1422 {
1423 const char *haystack_end = state->str1 + state->len1;
1424
1425 /* Walk one character at a time, until we reach the match. */
1426
1427 /* the search should never move backwards. */
1428 Assert(state->refpoint <= matchptr);
1429
1430 while (state->refpoint < matchptr)
1431 {
1432 /* step to next character. */
1433 state->refpoint += pg_mblen_range(state->refpoint, haystack_end);
1434 state->refpos++;
1435
1436 /*
1437 * If we stepped over the match's start position, then it was a
1438 * false positive, where the byte sequence appeared in the middle
1439 * of a multi-byte character. Skip it, and continue the search at
1440 * the next character boundary.
1441 */
1442 if (state->refpoint > matchptr)
1443 {
1444 start_ptr = state->refpoint;
1445 goto retry;
1446 }
1447 }
1448 }
1449
1450 state->last_match = matchptr;
1451 state->last_match_len = state->last_match_len_tmp;
1452 return true;
1453 }
1454
1455 /*
1456 * Subroutine of text_position_next(). This searches for the raw byte
1457 * sequence, ignoring any multi-byte encoding issues. Returns the first
1458 * match starting at 'start_ptr', or NULL if no match is found.
1459 */
1460 static char *
1461 text_position_next_internal(char *start_ptr, TextPositionState *state)
1462 {
1463 int haystack_len = state->len1;
1464 int needle_len = state->len2;
1465 int skiptablemask = state->skiptablemask;
1466 const char *haystack = state->str1;
1467 const char *needle = state->str2;
1468 const char *haystack_end = &haystack[haystack_len];
1469 const char *hptr;
1470
1471 Assert(start_ptr >= haystack && start_ptr <= haystack_end);
1472 Assert(needle_len > 0);
1473
1474 state->last_match_len_tmp = needle_len;
1475
1476 if (!state->locale->deterministic)
1477 {
1478 /*
1479 * With a nondeterministic collation, we have to use an unoptimized
1480 * route. We walk through the haystack and see if at each position
1481 * there is a substring of the remaining string that is equal to the
1482 * needle under the given collation.
1483 *
1484 * Note, the found substring could have a different length than the
1485 * needle. Callers that want to skip over the found string need to
1486 * read the length of the found substring from last_match_len rather
1487 * than just using the length of their needle.
1488 *
1489 * Most callers will require "greedy" semantics, meaning that we need
1490 * to find the longest such substring, not the shortest. For callers
1491 * that don't need greedy semantics, we can finish on the first match.
1492 *
1493 * This loop depends on the assumption that the needle is nonempty and
1494 * any matching substring must also be nonempty. (Even if the
1495 * collation would accept an empty match, returning one would send
1496 * callers that search for successive matches into an infinite loop.)
1497 */
1498 const char *result_hptr = NULL;
1499
1500 hptr = start_ptr;
1501 while (hptr < haystack_end)
1502 {
1503 const char *test_end;
1504
1505 /*
1506 * First check the common case that there is a match in the
1507 * haystack of exactly the length of the needle.
1508 */
1509 if (!state->greedy &&
1510 haystack_end - hptr >= needle_len &&
1511 pg_strncoll(hptr, needle_len, needle, needle_len, state->locale) == 0)
1512 return (char *) hptr;
1513
1514 /*
1515 * Else check if any of the non-empty substrings starting at hptr
1516 * compare equal to the needle.
1517 */
1518 test_end = hptr;
1519 do
1520 {
1521 test_end += pg_mblen_range(test_end, haystack_end);
1522 if (pg_strncoll(hptr, (test_end - hptr), needle, needle_len, state->locale) == 0)
1523 {
1524 state->last_match_len_tmp = (test_end - hptr);
1525 result_hptr = hptr;
1526 if (!state->greedy)
1527 break;
1528 }
1529 } while (test_end < haystack_end);
1530
1531 if (result_hptr)
1532 break;
1533
1534 hptr += pg_mblen_range(hptr, haystack_end);
1535 }
1536
1537 return (char *) result_hptr;
1538 }
1539 else if (needle_len == 1)
1540 {
1541 /* No point in using B-M-H for a one-character needle */
1542 char nchar = *needle;
1543
1544 hptr = start_ptr;
1545 while (hptr < haystack_end)
1546 {
1547 if (*hptr == nchar)
1548 return (char *) hptr;
1549 hptr++;
1550 }
1551 }
1552 else
1553 {
1554 const char *needle_last = &needle[needle_len - 1];
1555
1556 /* Start at startpos plus the length of the needle */
1557 hptr = start_ptr + needle_len - 1;
1558 while (hptr < haystack_end)
1559 {
1560 /* Match the needle scanning *backward* */
1561 const char *nptr;
1562 const char *p;
1563
1564 nptr = needle_last;
1565 p = hptr;
1566 while (*nptr == *p)
1567 {
1568 /* Matched it all? If so, return 1-based position */
1569 if (nptr == needle)
1570 return (char *) p;
1571 nptr--, p--;
1572 }
1573
1574 /*
1575 * No match, so use the haystack char at hptr to decide how far to
1576 * advance. If the needle had any occurrence of that character
1577 * (or more precisely, one sharing the same skiptable entry)
1578 * before its last character, then we advance far enough to align
1579 * the last such needle character with that haystack position.
1580 * Otherwise we can advance by the whole needle length.
1581 */
1582 hptr += state->skiptable[(unsigned char) *hptr & skiptablemask];
1583 }
1584 }
1585
1586 return 0; /* not found */
1587 }
1588
1589 /*
1590 * Return a pointer to the current match.
1591 *
1592 * The returned pointer points into the original haystack string.
1593 */
1594 static char *
1595 text_position_get_match_ptr(TextPositionState *state)
1596 {
1597 return state->last_match;
1598 }
1599
1600 /*
1601 * Return the offset of the current match.
1602 *
1603 * The offset is in characters, 1-based.
1604 */
1605 static int
1606 text_position_get_match_pos(TextPositionState *state)
1607 {
1608 /* Convert the byte position to char position. */
1609 state->refpos += pg_mbstrlen_with_len(state->refpoint,
1610 state->last_match - state->refpoint);
1611 state->refpoint = state->last_match;
1612 return state->refpos + 1;
1613 }
1614
1615 /*
1616 * Reset search state to the initial state installed by text_position_setup.
1617 *
1618 * The next call to text_position_next will search from the beginning
1619 * of the string.
1620 */
1621 static void
1622 text_position_reset(TextPositionState *state)
1623 {
1624 state->last_match = NULL;
1625 state->refpoint = state->str1;
1626 state->refpos = 0;
1627 }
1628
1629 static void
1630 text_position_cleanup(TextPositionState *state)
1631 {
1632 /* no cleanup needed */
1633 }
1634
1635
1636 static void
1637 check_collation_set(Oid collid)
1638 {
1639 if (!OidIsValid(collid))
1640 {
1641 /*
1642 * This typically means that the parser could not resolve a conflict
1643 * of implicit collations, so report it that way.
1644 */
1645 ereport(ERROR,
1646 (errcode(ERRCODE_INDETERMINATE_COLLATION),
1647 errmsg("could not determine which collation to use for string comparison"),
1648 errhint("Use the COLLATE clause to set the collation explicitly.")));
1649 }
1650 }
1651
1652 /*
1653 * varstr_cmp()
1654 *
1655 * Comparison function for text strings with given lengths, using the
1656 * appropriate locale. Returns an integer less than, equal to, or greater than
1657 * zero, indicating whether arg1 is less than, equal to, or greater than arg2.
1658 *
1659 * Note: many functions that depend on this are marked leakproof; therefore,
1660 * avoid reporting the actual contents of the input when throwing errors.
1661 * All errors herein should be things that can't happen except on corrupt
1662 * data, anyway; otherwise we will have trouble with indexing strings that
1663 * would cause them.
1664 */
1665 int
1666 varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
1667 {
1668 int result;
1669 pg_locale_t mylocale;
1670
1671 check_collation_set(collid);
1672
1673 mylocale = pg_newlocale_from_collation(collid);
1674
1675 if (mylocale->collate_is_c)
1676 {
1677 result = memcmp(arg1, arg2, Min(len1, len2));
1678 if ((result == 0) && (len1 != len2))
1679 result = (len1 < len2) ? -1 : 1;
1680 }
1681 else
1682 {
1683 /*
1684 * memcmp() can't tell us which of two unequal strings sorts first,
1685 * but it's a cheap way to tell if they're equal. Testing shows that
1686 * memcmp() followed by strcoll() is only trivially slower than
1687 * strcoll() by itself, so we don't lose much if this doesn't work out
1688 * very often, and if it does - for example, because there are many
1689 * equal strings in the input - then we win big by avoiding expensive
1690 * collation-aware comparisons.
1691 */
1692 if (len1 == len2 && memcmp(arg1, arg2, len1) == 0)
1693 return 0;
1694
1695 result = pg_strncoll(arg1, len1, arg2, len2, mylocale);
1696
1697 /* Break tie if necessary. */
1698 if (result == 0 && mylocale->deterministic)
1699 {
1700 result = memcmp(arg1, arg2, Min(len1, len2));
1701 if ((result == 0) && (len1 != len2))
1702 result = (len1 < len2) ? -1 : 1;
1703 }
1704 }
1705
1706 return result;
1707 }
1708
1709 /* text_cmp()
1710 * Internal comparison function for text strings.
1711 * Returns -1, 0 or 1
1712 */
1713 static int
1714 text_cmp(text *arg1, text *arg2, Oid collid)
1715 {
1716 char *a1p,
1717 *a2p;
1718 int len1,
1719 len2;
1720
1721 a1p = VARDATA_ANY(arg1);
1722 a2p = VARDATA_ANY(arg2);
1723
1724 len1 = VARSIZE_ANY_EXHDR(arg1);
1725 len2 = VARSIZE_ANY_EXHDR(arg2);
1726
1727 return varstr_cmp(a1p, len1, a2p, len2, collid);
1728 }
1729
1730 /*
1731 * Comparison functions for text strings.
1732 *
1733 * Note: btree indexes need these routines not to leak memory; therefore,
1734 * be careful to free working copies of toasted datums. Most places don't
1735 * need to be so careful.
1736 */
1737
1738 Datum
1739 texteq(PG_FUNCTION_ARGS)
1740 {
1741 Oid collid = PG_GET_COLLATION();
1742 pg_locale_t mylocale = 0;
1743 bool result;
1744
1745 check_collation_set(collid);
1746
1747 mylocale = pg_newlocale_from_collation(collid);
1748
1749 if (mylocale->deterministic)
1750 {
1751 Datum arg1 = PG_GETARG_DATUM(0);
1752 Datum arg2 = PG_GETARG_DATUM(1);
1753 Size len1,
1754 len2;
1755
1756 /*
1757 * Since we only care about equality or not-equality, we can avoid all
1758 * the expense of strcoll() here, and just do bitwise comparison. In
1759 * fact, we don't even have to do a bitwise comparison if we can show
1760 * the lengths of the strings are unequal; which might save us from
1761 * having to detoast one or both values.
1762 */
1763 len1 = toast_raw_datum_size(arg1);
1764 len2 = toast_raw_datum_size(arg2);
1765 if (len1 != len2)
1766 result = false;
1767 else
1768 {
1769 text *targ1 = DatumGetTextPP(arg1);
1770 text *targ2 = DatumGetTextPP(arg2);
1771
1772 result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
1773 len1 - VARHDRSZ) == 0);
1774
1775 PG_FREE_IF_COPY(targ1, 0);
1776 PG_FREE_IF_COPY(targ2, 1);
1777 }
1778 }
1779 else
1780 {
1781 text *arg1 = PG_GETARG_TEXT_PP(0);
1782 text *arg2 = PG_GETARG_TEXT_PP(1);
1783
1784 result = (text_cmp(arg1, arg2, collid) == 0);
1785
1786 PG_FREE_IF_COPY(arg1, 0);
1787 PG_FREE_IF_COPY(arg2, 1);
1788 }
1789
1790 PG_RETURN_BOOL(result);
1791 }
1792
1793 Datum
1794 textne(PG_FUNCTION_ARGS)
1795 {
1796 Oid collid = PG_GET_COLLATION();
1797 pg_locale_t mylocale;
1798 bool result;
1799
1800 check_collation_set(collid);
1801
1802 mylocale = pg_newlocale_from_collation(collid);
1803
1804 if (mylocale->deterministic)
1805 {
1806 Datum arg1 = PG_GETARG_DATUM(0);
1807 Datum arg2 = PG_GETARG_DATUM(1);
1808 Size len1,
1809 len2;
1810
1811 /* See comment in texteq() */
1812 len1 = toast_raw_datum_size(arg1);
1813 len2 = toast_raw_datum_size(arg2);
1814 if (len1 != len2)
1815 result = true;
1816 else
1817 {
1818 text *targ1 = DatumGetTextPP(arg1);
1819 text *targ2 = DatumGetTextPP(arg2);
1820
1821 result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
1822 len1 - VARHDRSZ) != 0);
1823
1824 PG_FREE_IF_COPY(targ1, 0);
1825 PG_FREE_IF_COPY(targ2, 1);
1826 }
1827 }
1828 else
1829 {
1830 text *arg1 = PG_GETARG_TEXT_PP(0);
1831 text *arg2 = PG_GETARG_TEXT_PP(1);
1832
1833 result = (text_cmp(arg1, arg2, collid) != 0);
1834
1835 PG_FREE_IF_COPY(arg1, 0);
1836 PG_FREE_IF_COPY(arg2, 1);
1837 }
1838
1839 PG_RETURN_BOOL(result);
1840 }
1841
1842 Datum
1843 text_lt(PG_FUNCTION_ARGS)
1844 {
1845 text *arg1 = PG_GETARG_TEXT_PP(0);
1846 text *arg2 = PG_GETARG_TEXT_PP(1);
1847 bool result;
1848
1849 result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0);
1850
1851 PG_FREE_IF_COPY(arg1, 0);
1852 PG_FREE_IF_COPY(arg2, 1);
1853
1854 PG_RETURN_BOOL(result);
1855 }
1856
1857 Datum
1858 text_le(PG_FUNCTION_ARGS)
1859 {
1860 text *arg1 = PG_GETARG_TEXT_PP(0);
1861 text *arg2 = PG_GETARG_TEXT_PP(1);
1862 bool result;
1863
1864 result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) <= 0);
1865
1866 PG_FREE_IF_COPY(arg1, 0);
1867 PG_FREE_IF_COPY(arg2, 1);
1868
1869 PG_RETURN_BOOL(result);
1870 }
1871
1872 Datum
1873 text_gt(PG_FUNCTION_ARGS)
1874 {
1875 text *arg1 = PG_GETARG_TEXT_PP(0);
1876 text *arg2 = PG_GETARG_TEXT_PP(1);
1877 bool result;
1878
1879 result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0);
1880
1881 PG_FREE_IF_COPY(arg1, 0);
1882 PG_FREE_IF_COPY(arg2, 1);
1883
1884 PG_RETURN_BOOL(result);
1885 }
1886
1887 Datum
1888 text_ge(PG_FUNCTION_ARGS)
1889 {
1890 text *arg1 = PG_GETARG_TEXT_PP(0);
1891 text *arg2 = PG_GETARG_TEXT_PP(1);
1892 bool result;
1893
1894 result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) >= 0);
1895
1896 PG_FREE_IF_COPY(arg1, 0);
1897 PG_FREE_IF_COPY(arg2, 1);
1898
1899 PG_RETURN_BOOL(result);
1900 }
1901
1902 Datum
1903 text_starts_with(PG_FUNCTION_ARGS)
1904 {
1905 Datum arg1 = PG_GETARG_DATUM(0);
1906 Datum arg2 = PG_GETARG_DATUM(1);
1907 Oid collid = PG_GET_COLLATION();
1908 pg_locale_t mylocale;
1909 bool result;
1910 Size len1,
1911 len2;
1912
1913 check_collation_set(collid);
1914
1915 mylocale = pg_newlocale_from_collation(collid);
1916
1917 if (!mylocale->deterministic)
1918 ereport(ERROR,
1919 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1920 errmsg("nondeterministic collations are not supported for substring searches")));
1921
1922 len1 = toast_raw_datum_size(arg1);
1923 len2 = toast_raw_datum_size(arg2);
1924 if (len2 > len1)
1925 result = false;
1926 else
1927 {
1928 text *targ1 = text_substring(arg1, 1, len2, false);
1929 text *targ2 = DatumGetTextPP(arg2);
1930
1931 result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
1932 VARSIZE_ANY_EXHDR(targ2)) == 0);
1933
1934 PG_FREE_IF_COPY(targ1, 0);
1935 PG_FREE_IF_COPY(targ2, 1);
1936 }
1937
1938 PG_RETURN_BOOL(result);
1939 }
1940
1941 Datum
1942 bttextcmp(PG_FUNCTION_ARGS)
1943 {
1944 text *arg1 = PG_GETARG_TEXT_PP(0);
1945 text *arg2 = PG_GETARG_TEXT_PP(1);
1946 int32 result;
1947
1948 result = text_cmp(arg1, arg2, PG_GET_COLLATION());
1949
1950 PG_FREE_IF_COPY(arg1, 0);
1951 PG_FREE_IF_COPY(arg2, 1);
1952
1953 PG_RETURN_INT32(result);
1954 }
1955
1956 Datum
1957 bttextsortsupport(PG_FUNCTION_ARGS)
1958 {
1959 SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
1960 Oid collid = ssup->ssup_collation;
1961 MemoryContext oldcontext;
1962
1963 oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
1964
1965 /* Use generic string SortSupport */
1966 varstr_sortsupport(ssup, TEXTOID, collid);
1967
1968 MemoryContextSwitchTo(oldcontext);
1969
1970 PG_RETURN_VOID();
1971 }
1972
1973 /*
1974 * Generic sortsupport interface for character type's operator classes.
1975 * Includes locale support, and support for BpChar semantics (i.e. removing
1976 * trailing spaces before comparison).
1977 *
1978 * Relies on the assumption that text, VarChar, BpChar, and bytea all have the
1979 * same representation. Callers that always use the C collation (e.g.
1980 * non-collatable type callers like bytea) may have NUL bytes in their strings;
1981 * this will not work with any other collation, though.
1982 */
1983 void
1984 varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid)
1985 {
1986 bool abbreviate = ssup->abbreviate;
1987 bool collate_c = false;
1988 VarStringSortSupport *sss;
1989 pg_locale_t locale;
1990
1991 check_collation_set(collid);
1992
1993 locale = pg_newlocale_from_collation(collid);
1994
1995 /*
1996 * If possible, set ssup->comparator to a function which can be used to
1997 * directly compare two datums. If we can do this, we'll avoid the
1998 * overhead of a trip through the fmgr layer for every comparison, which
1999 * can be substantial.
2000 *
2001 * Most typically, we'll set the comparator to varlenafastcmp_locale,
2002 * which uses strcoll() to perform comparisons. We use that for the
2003 * BpChar case too, but type NAME uses namefastcmp_locale. However, if
2004 * LC_COLLATE = C, we can make things quite a bit faster with
2005 * varstrfastcmp_c, bpcharfastcmp_c, or namefastcmp_c, all of which use
2006 * memcmp() rather than strcoll().
2007 */
2008 if (locale->collate_is_c)
2009 {
2010 if (typid == BPCHAROID)
2011 ssup->comparator = bpcharfastcmp_c;
2012 else if (typid == NAMEOID)
2013 {
2014 ssup->comparator = namefastcmp_c;
2015 /* Not supporting abbreviation with type NAME, for now */
2016 abbreviate = false;
2017 }
2018 else
2019 ssup->comparator = varstrfastcmp_c;
2020
2021 collate_c = true;
2022 }
2023 else
2024 {
2025 /*
2026 * We use varlenafastcmp_locale except for type NAME.
2027 */
2028 if (typid == NAMEOID)
2029 {
2030 ssup->comparator = namefastcmp_locale;
2031 /* Not supporting abbreviation with type NAME, for now */
2032 abbreviate = false;
2033 }
2034 else
2035 ssup->comparator = varlenafastcmp_locale;
2036
2037 /*
2038 * Unfortunately, it seems that abbreviation for non-C collations is
2039 * broken on many common platforms; see pg_strxfrm_enabled().
2040 *
2041 * Even apart from the risk of broken locales, it's possible that
2042 * there are platforms where the use of abbreviated keys should be
2043 * disabled at compile time. Having only 4 byte datums could make
2044 * worst-case performance drastically more likely, for example.
2045 * Moreover, macOS's strxfrm() implementation is known to not
2046 * effectively concentrate a significant amount of entropy from the
2047 * original string in earlier transformed blobs. It's possible that
2048 * other supported platforms are similarly encumbered. So, if we ever
2049 * get past disabling this categorically, we may still want or need to
2050 * disable it for particular platforms.
2051 */
2052 if (!pg_strxfrm_enabled(locale))
2053 abbreviate = false;
2054 }
2055
2056 /*
2057 * If we're using abbreviated keys, or if we're using a locale-aware
2058 * comparison, we need to initialize a VarStringSortSupport object. Both
2059 * cases will make use of the temporary buffers we initialize here for
2060 * scratch space (and to detect requirement for BpChar semantics from
2061 * caller), and the abbreviation case requires additional state.
2062 */
2063 if (abbreviate || !collate_c)
2064 {
2065 sss = palloc(sizeof(VarStringSortSupport));
2066 sss->buf1 = palloc(TEXTBUFLEN);
2067 sss->buflen1 = TEXTBUFLEN;
2068 sss->buf2 = palloc(TEXTBUFLEN);
2069 sss->buflen2 = TEXTBUFLEN;
2070 /* Start with invalid values */
2071 sss->last_len1 = -1;
2072 sss->last_len2 = -1;
2073 /* Initialize */
2074 sss->last_returned = 0;
2075 if (collate_c)
2076 sss->locale = NULL;
2077 else
2078 sss->locale = locale;
2079
2080 /*
2081 * To avoid somehow confusing a strxfrm() blob and an original string,
2082 * constantly keep track of the variety of data that buf1 and buf2
2083 * currently contain.
2084 *
2085 * Comparisons may be interleaved with conversion calls. Frequently,
2086 * conversions and comparisons are batched into two distinct phases,
2087 * but the correctness of caching cannot hinge upon this. For
2088 * comparison caching, buffer state is only trusted if cache_blob is
2089 * found set to false, whereas strxfrm() caching only trusts the state
2090 * when cache_blob is found set to true.
2091 *
2092 * Arbitrarily initialize cache_blob to true.
2093 */
2094 sss->cache_blob = true;
2095 sss->collate_c = collate_c;
2096 sss->typid = typid;
2097 ssup->ssup_extra = sss;
2098
2099 /*
2100 * If possible, plan to use the abbreviated keys optimization. The
2101 * core code may switch back to authoritative comparator should
2102 * abbreviation be aborted.
2103 */
2104 if (abbreviate)
2105 {
2106 sss->prop_card = 0.20;
2107 initHyperLogLog(&sss->abbr_card, 10);
2108 initHyperLogLog(&sss->full_card, 10);
2109 ssup->abbrev_full_comparator = ssup->comparator;
2110 ssup->comparator = ssup_datum_unsigned_cmp;
2111 ssup->abbrev_converter = varstr_abbrev_convert;
2112 ssup->abbrev_abort = varstr_abbrev_abort;
2113 }
2114 }
2115 }
2116
2117 /*
2118 * sortsupport comparison func (for C locale case)
2119 */
2120 static int
2121 varstrfastcmp_c(Datum x, Datum y, SortSupport ssup)
2122 {
2123 VarString *arg1 = DatumGetVarStringPP(x);
2124 VarString *arg2 = DatumGetVarStringPP(y);
2125 char *a1p,
2126 *a2p;
2127 int len1,
2128 len2,
2129 result;
2130
2131 a1p = VARDATA_ANY(arg1);
2132 a2p = VARDATA_ANY(arg2);
2133
2134 len1 = VARSIZE_ANY_EXHDR(arg1);
2135 len2 = VARSIZE_ANY_EXHDR(arg2);
2136
2137 result = memcmp(a1p, a2p, Min(len1, len2));
2138 if ((result == 0) && (len1 != len2))
2139 result = (len1 < len2) ? -1 : 1;
2140
2141 /* We can't afford to leak memory here. */
2142 if (PointerGetDatum(arg1) != x)
2143 pfree(arg1);
2144 if (PointerGetDatum(arg2) != y)
2145 pfree(arg2);
2146
2147 return result;
2148 }
2149
2150 /*
2151 * sortsupport comparison func (for BpChar C locale case)
2152 *
2153 * BpChar outsources its sortsupport to this module. Specialization for the
2154 * varstr_sortsupport BpChar case, modeled on
2155 * internal_bpchar_pattern_compare().
2156 */
2157 static int
2158 bpcharfastcmp_c(Datum x, Datum y, SortSupport ssup)
2159 {
2160 BpChar *arg1 = DatumGetBpCharPP(x);
2161 BpChar *arg2 = DatumGetBpCharPP(y);
2162 char *a1p,
2163 *a2p;
2164 int len1,
2165 len2,
2166 result;
2167
2168 a1p = VARDATA_ANY(arg1);
2169 a2p = VARDATA_ANY(arg2);
2170
2171 len1 = bpchartruelen(a1p, VARSIZE_ANY_EXHDR(arg1));
2172 len2 = bpchartruelen(a2p, VARSIZE_ANY_EXHDR(arg2));
2173
2174 result = memcmp(a1p, a2p, Min(len1, len2));
2175 if ((result == 0) && (len1 != len2))
2176 result = (len1 < len2) ? -1 : 1;
2177
2178 /* We can't afford to leak memory here. */
2179 if (PointerGetDatum(arg1) != x)
2180 pfree(arg1);
2181 if (PointerGetDatum(arg2) != y)
2182 pfree(arg2);
2183
2184 return result;
2185 }
2186
2187 /*
2188 * sortsupport comparison func (for NAME C locale case)
2189 */
2190 static int
2191 namefastcmp_c(Datum x, Datum y, SortSupport ssup)
2192 {
2193 Name arg1 = DatumGetName(x);
2194 Name arg2 = DatumGetName(y);
2195
2196 return strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN);
2197 }
2198
2199 /*
2200 * sortsupport comparison func (for locale case with all varlena types)
2201 */
2202 static int
2203 varlenafastcmp_locale(Datum x, Datum y, SortSupport ssup)
2204 {
2205 VarString *arg1 = DatumGetVarStringPP(x);
2206 VarString *arg2 = DatumGetVarStringPP(y);
2207 char *a1p,
2208 *a2p;
2209 int len1,
2210 len2,
2211 result;
2212
2213 a1p = VARDATA_ANY(arg1);
2214 a2p = VARDATA_ANY(arg2);
2215
2216 len1 = VARSIZE_ANY_EXHDR(arg1);
2217 len2 = VARSIZE_ANY_EXHDR(arg2);
2218
2219 result = varstrfastcmp_locale(a1p, len1, a2p, len2, ssup);
2220
2221 /* We can't afford to leak memory here. */
2222 if (PointerGetDatum(arg1) != x)
2223 pfree(arg1);
2224 if (PointerGetDatum(arg2) != y)
2225 pfree(arg2);
2226
2227 return result;
2228 }
2229
2230 /*
2231 * sortsupport comparison func (for locale case with NAME type)
2232 */
2233 static int
2234 namefastcmp_locale(Datum x, Datum y, SortSupport ssup)
2235 {
2236 Name arg1 = DatumGetName(x);
2237 Name arg2 = DatumGetName(y);
2238
2239 return varstrfastcmp_locale(NameStr(*arg1), strlen(NameStr(*arg1)),
2240 NameStr(*arg2), strlen(NameStr(*arg2)),
2241 ssup);
2242 }
2243
2244 /*
2245 * sortsupport comparison func for locale cases
2246 */
2247 static int
2248 varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
2249 {
2250 VarStringSortSupport *sss = (VarStringSortSupport *) ssup->ssup_extra;
2251 int result;
2252 bool arg1_match;
2253
2254 /* Fast pre-check for equality, as discussed in varstr_cmp() */
2255 if (len1 == len2 && memcmp(a1p, a2p, len1) == 0)
2256 {
2257 /*
2258 * No change in buf1 or buf2 contents, so avoid changing last_len1 or
2259 * last_len2. Existing contents of buffers might still be used by
2260 * next call.
2261 *
2262 * It's fine to allow the comparison of BpChar padding bytes here,
2263 * even though that implies that the memcmp() will usually be
2264 * performed for BpChar callers (though multibyte characters could
2265 * still prevent that from occurring). The memcmp() is still very
2266 * cheap, and BpChar's funny semantics have us remove trailing spaces
2267 * (not limited to padding), so we need make no distinction between
2268 * padding space characters and "real" space characters.
2269 */
2270 return 0;
2271 }
2272
2273 if (sss->typid == BPCHAROID)
2274 {
2275 /* Get true number of bytes, ignoring trailing spaces */
2276 len1 = bpchartruelen(a1p, len1);
2277 len2 = bpchartruelen(a2p, len2);
2278 }
2279
2280 if (len1 >= sss->buflen1)
2281 {
2282 sss->buflen1 = Max(len1 + 1, Min(sss->buflen1 * 2, MaxAllocSize));
2283 sss->buf1 = repalloc(sss->buf1, sss->buflen1);
2284 }
2285 if (len2 >= sss->buflen2)
2286 {
2287 sss->buflen2 = Max(len2 + 1, Min(sss->buflen2 * 2, MaxAllocSize));
2288 sss->buf2 = repalloc(sss->buf2, sss->buflen2);
2289 }
2290
2291 /*
2292 * We're likely to be asked to compare the same strings repeatedly, and
2293 * memcmp() is so much cheaper than strcoll() that it pays to try to cache
2294 * comparisons, even though in general there is no reason to think that
2295 * that will work out (every string datum may be unique). Caching does
2296 * not slow things down measurably when it doesn't work out, and can speed
2297 * things up by rather a lot when it does. In part, this is because the
2298 * memcmp() compares data from cachelines that are needed in L1 cache even
2299 * when the last comparison's result cannot be reused.
2300 */
2301 arg1_match = true;
2302 if (len1 != sss->last_len1 || memcmp(sss->buf1, a1p, len1) != 0)
2303 {
2304 arg1_match = false;
2305 memcpy(sss->buf1, a1p, len1);
2306 sss->buf1[len1] = '\0';
2307 sss->last_len1 = len1;
2308 }
2309
2310 /*
2311 * If we're comparing the same two strings as last time, we can return the
2312 * same answer without calling strcoll() again. This is more likely than
2313 * it seems (at least with moderate to low cardinality sets), because
2314 * quicksort compares the same pivot against many values.
2315 */
2316 if (len2 != sss->last_len2 || memcmp(sss->buf2, a2p, len2) != 0)
2317 {
2318 memcpy(sss->buf2, a2p, len2);
2319 sss->buf2[len2] = '\0';
2320 sss->last_len2 = len2;
2321 }
2322 else if (arg1_match && !sss->cache_blob)
2323 {
2324 /* Use result cached following last actual strcoll() call */
2325 return sss->last_returned;
2326 }
2327
2328 result = pg_strcoll(sss->buf1, sss->buf2, sss->locale);
2329
2330 /* Break tie if necessary. */
2331 if (result == 0 && sss->locale->deterministic)
2332 result = strcmp(sss->buf1, sss->buf2);
2333
2334 /* Cache result, perhaps saving an expensive strcoll() call next time */
2335 sss->cache_blob = false;
2336 sss->last_returned = result;
2337 return result;
2338 }
2339
2340 /*
2341 * Conversion routine for sortsupport. Converts original to abbreviated key
2342 * representation. Our encoding strategy is simple -- pack the first 8 bytes
2343 * of a strxfrm() blob into a Datum (on little-endian machines, the 8 bytes are
2344 * stored in reverse order), and treat it as an unsigned integer. When the "C"
2345 * locale is used, or in case of bytea, just memcpy() from original instead.
2346 */
2347 static Datum
2348 varstr_abbrev_convert(Datum original, SortSupport ssup)
2349 {
2350 const size_t max_prefix_bytes = sizeof(Datum);
2351 VarStringSortSupport *sss = (VarStringSortSupport *) ssup->ssup_extra;
2352 VarString *authoritative = DatumGetVarStringPP(original);
2353 char *authoritative_data = VARDATA_ANY(authoritative);
2354
2355 /* working state */
2356 Datum res;
2357 char *pres;
2358 int len;
2359 uint32 hash;
2360
2361 pres = (char *) &res;
2362 /* memset(), so any non-overwritten bytes are NUL */
2363 memset(pres, 0, max_prefix_bytes);
2364 len = VARSIZE_ANY_EXHDR(authoritative);
2365
2366 /* Get number of bytes, ignoring trailing spaces */
2367 if (sss->typid == BPCHAROID)
2368 len = bpchartruelen(authoritative_data, len);
2369
2370 /*
2371 * If we're using the C collation, use memcpy(), rather than strxfrm(), to
2372 * abbreviate keys. The full comparator for the C locale is always
2373 * memcmp(). It would be incorrect to allow bytea callers (callers that
2374 * always force the C collation -- bytea isn't a collatable type, but this
2375 * approach is convenient) to use strxfrm(). This is because bytea
2376 * strings may contain NUL bytes. Besides, this should be faster, too.
2377 *
2378 * More generally, it's okay that bytea callers can have NUL bytes in
2379 * strings because abbreviated cmp need not make a distinction between
2380 * terminating NUL bytes, and NUL bytes representing actual NULs in the
2381 * authoritative representation. Hopefully a comparison at or past one
2382 * abbreviated key's terminating NUL byte will resolve the comparison
2383 * without consulting the authoritative representation; specifically, some
2384 * later non-NUL byte in the longer string can resolve the comparison
2385 * against a subsequent terminating NUL in the shorter string. There will
2386 * usually be what is effectively a "length-wise" resolution there and
2387 * then.
2388 *
2389 * If that doesn't work out -- if all bytes in the longer string
2390 * positioned at or past the offset of the smaller string's (first)
2391 * terminating NUL are actually representative of NUL bytes in the
2392 * authoritative binary string (perhaps with some *terminating* NUL bytes
2393 * towards the end of the longer string iff it happens to still be small)
2394 * -- then an authoritative tie-breaker will happen, and do the right
2395 * thing: explicitly consider string length.
2396 */
2397 if (sss->collate_c)
2398 memcpy(pres, authoritative_data, Min(len, max_prefix_bytes));
2399 else
2400 {
2401 Size bsize;
2402
2403 /*
2404 * We're not using the C collation, so fall back on strxfrm or ICU
2405 * analogs.
2406 */
2407
2408 /* By convention, we use buffer 1 to store and NUL-terminate */
2409 if (len >= sss->buflen1)
2410 {
2411 sss->buflen1 = Max(len + 1, Min(sss->buflen1 * 2, MaxAllocSize));
2412 sss->buf1 = repalloc(sss->buf1, sss->buflen1);
2413 }
2414
2415 /* Might be able to reuse strxfrm() blob from last call */
2416 if (sss->last_len1 == len && sss->cache_blob &&
2417 memcmp(sss->buf1, authoritative_data, len) == 0)
2418 {
2419 memcpy(pres, sss->buf2, Min(max_prefix_bytes, sss->last_len2));
2420 /* No change affecting cardinality, so no hashing required */
2421 goto done;
2422 }
2423
2424 memcpy(sss->buf1, authoritative_data, len);
2425
2426 /*
2427 * pg_strxfrm() and pg_strxfrm_prefix expect NUL-terminated strings.
2428 */
2429 sss->buf1[len] = '\0';
2430 sss->last_len1 = len;
2431
2432 if (pg_strxfrm_prefix_enabled(sss->locale))
2433 {
2434 if (sss->buflen2 < max_prefix_bytes)
2435 {
2436 sss->buflen2 = Max(max_prefix_bytes,
2437 Min(sss->buflen2 * 2, MaxAllocSize));
2438 sss->buf2 = repalloc(sss->buf2, sss->buflen2);
2439 }
2440
2441 bsize = pg_strxfrm_prefix(sss->buf2, sss->buf1,
2442 max_prefix_bytes, sss->locale);
2443 sss->last_len2 = bsize;
2444 }
2445 else
2446 {
2447 /*
2448 * Loop: Call pg_strxfrm(), possibly enlarge buffer, and try
2449 * again. The pg_strxfrm() function leaves the result buffer
2450 * content undefined if the result did not fit, so we need to
2451 * retry until everything fits, even though we only need the first
2452 * few bytes in the end.
2453 */
2454 for (;;)
2455 {
2456 bsize = pg_strxfrm(sss->buf2, sss->buf1, sss->buflen2,
2457 sss->locale);
2458
2459 sss->last_len2 = bsize;
2460 if (bsize < sss->buflen2)
2461 break;
2462
2463 /*
2464 * Grow buffer and retry.
2465 */
2466 sss->buflen2 = Max(bsize + 1,
2467 Min(sss->buflen2 * 2, MaxAllocSize));
2468 sss->buf2 = repalloc(sss->buf2, sss->buflen2);
2469 }
2470 }
2471
2472 /*
2473 * Every Datum byte is always compared. This is safe because the
2474 * strxfrm() blob is itself NUL terminated, leaving no danger of
2475 * misinterpreting any NUL bytes not intended to be interpreted as
2476 * logically representing termination.
2477 *
2478 * (Actually, even if there were NUL bytes in the blob it would be
2479 * okay. See remarks on bytea case above.)
2480 */
2481 memcpy(pres, sss->buf2, Min(max_prefix_bytes, bsize));
2482 }
2483
2484 /*
2485 * Maintain approximate cardinality of both abbreviated keys and original,
2486 * authoritative keys using HyperLogLog. Used as cheap insurance against
2487 * the worst case, where we do many string transformations for no saving
2488 * in full strcoll()-based comparisons. These statistics are used by
2489 * varstr_abbrev_abort().
2490 *
2491 * First, Hash key proper, or a significant fraction of it. Mix in length
2492 * in order to compensate for cases where differences are past
2493 * PG_CACHE_LINE_SIZE bytes, so as to limit the overhead of hashing.
2494 */
2495 hash = DatumGetUInt32(hash_any((unsigned char *) authoritative_data,
2496 Min(len, PG_CACHE_LINE_SIZE)));
2497
2498 if (len > PG_CACHE_LINE_SIZE)
2499 hash ^= DatumGetUInt32(hash_uint32((uint32) len));
2500
2501 addHyperLogLog(&sss->full_card, hash);
2502
2503 /* Hash abbreviated key */
2504 #if SIZEOF_DATUM == 8
2505 {
2506 uint32 lohalf,
2507 hihalf;
2508
2509 lohalf = (uint32) res;
2510 hihalf = (uint32) (res >> 32);
2511 hash = DatumGetUInt32(hash_uint32(lohalf ^ hihalf));
2512 }
2513 #else /* SIZEOF_DATUM != 8 */
2514 hash = DatumGetUInt32(hash_uint32((uint32) res));
2515 #endif
2516
2517 addHyperLogLog(&sss->abbr_card, hash);
2518
2519 /* Cache result, perhaps saving an expensive strxfrm() call next time */
2520 sss->cache_blob = true;
2521 done:
2522
2523 /*
2524 * Byteswap on little-endian machines.
2525 *
2526 * This is needed so that ssup_datum_unsigned_cmp() (an unsigned integer
2527 * 3-way comparator) works correctly on all platforms. If we didn't do
2528 * this, the comparator would have to call memcmp() with a pair of
2529 * pointers to the first byte of each abbreviated key, which is slower.
2530 */
2531 res = DatumBigEndianToNative(res);
2532
2533 /* Don't leak memory here */
2534 if (PointerGetDatum(authoritative) != original)
2535 pfree(authoritative);
2536
2537 return res;
2538 }
2539
2540 /*
2541 * Callback for estimating effectiveness of abbreviated key optimization, using
2542 * heuristic rules. Returns value indicating if the abbreviation optimization
2543 * should be aborted, based on its projected effectiveness.
2544 */
2545 static bool
2546 varstr_abbrev_abort(int memtupcount, SortSupport ssup)
2547 {
2548 VarStringSortSupport *sss = (VarStringSortSupport *) ssup->ssup_extra;
2549 double abbrev_distinct,
2550 key_distinct;
2551
2552 Assert(ssup->abbreviate);
2553
2554 /* Have a little patience */
2555 if (memtupcount < 100)
2556 return false;
2557
2558 abbrev_distinct = estimateHyperLogLog(&sss->abbr_card);
2559 key_distinct = estimateHyperLogLog(&sss->full_card);
2560
2561 /*
2562 * Clamp cardinality estimates to at least one distinct value. While
2563 * NULLs are generally disregarded, if only NULL values were seen so far,
2564 * that might misrepresent costs if we failed to clamp.
2565 */
2566 if (abbrev_distinct <= 1.0)
2567 abbrev_distinct = 1.0;
2568
2569 if (key_distinct <= 1.0)
2570 key_distinct = 1.0;
2571
2572 /*
2573 * In the worst case all abbreviated keys are identical, while at the same
2574 * time there are differences within full key strings not captured in
2575 * abbreviations.
2576 */
2577 if (trace_sort)
2578 {
2579 double norm_abbrev_card = abbrev_distinct / (double) memtupcount;
2580
2581 elog(LOG, "varstr_abbrev: abbrev_distinct after %d: %f "
2582 "(key_distinct: %f, norm_abbrev_card: %f, prop_card: %f)",
2583 memtupcount, abbrev_distinct, key_distinct, norm_abbrev_card,
2584 sss->prop_card);
2585 }
2586
2587 /*
2588 * If the number of distinct abbreviated keys approximately matches the
2589 * number of distinct authoritative original keys, that's reason enough to
2590 * proceed. We can win even with a very low cardinality set if most
2591 * tie-breakers only memcmp(). This is by far the most important
2592 * consideration.
2593 *
2594 * While comparisons that are resolved at the abbreviated key level are
2595 * considerably cheaper than tie-breakers resolved with memcmp(), both of
2596 * those two outcomes are so much cheaper than a full strcoll() once
2597 * sorting is underway that it doesn't seem worth it to weigh abbreviated
2598 * cardinality against the overall size of the set in order to more
2599 * accurately model costs. Assume that an abbreviated comparison, and an
2600 * abbreviated comparison with a cheap memcmp()-based authoritative
2601 * resolution are equivalent.
2602 */
2603 if (abbrev_distinct > key_distinct * sss->prop_card)
2604 {
2605 /*
2606 * When we have exceeded 10,000 tuples, decay required cardinality
2607 * aggressively for next call.
2608 *
2609 * This is useful because the number of comparisons required on
2610 * average increases at a linearithmic rate, and at roughly 10,000
2611 * tuples that factor will start to dominate over the linear costs of
2612 * string transformation (this is a conservative estimate). The decay
2613 * rate is chosen to be a little less aggressive than halving -- which
2614 * (since we're called at points at which memtupcount has doubled)
2615 * would never see the cost model actually abort past the first call
2616 * following a decay. This decay rate is mostly a precaution against
2617 * a sudden, violent swing in how well abbreviated cardinality tracks
2618 * full key cardinality. The decay also serves to prevent a marginal
2619 * case from being aborted too late, when too much has already been
2620 * invested in string transformation.
2621 *
2622 * It's possible for sets of several million distinct strings with
2623 * mere tens of thousands of distinct abbreviated keys to still
2624 * benefit very significantly. This will generally occur provided
2625 * each abbreviated key is a proxy for a roughly uniform number of the
2626 * set's full keys. If it isn't so, we hope to catch that early and
2627 * abort. If it isn't caught early, by the time the problem is
2628 * apparent it's probably not worth aborting.
2629 */
2630 if (memtupcount > 10000)
2631 sss->prop_card *= 0.65;
2632
2633 return false;
2634 }
2635
2636 /*
2637 * Abort abbreviation strategy.
2638 *
2639 * The worst case, where all abbreviated keys are identical while all
2640 * original strings differ will typically only see a regression of about
2641 * 10% in execution time for small to medium sized lists of strings.
2642 * Whereas on modern CPUs where cache stalls are the dominant cost, we can
2643 * often expect very large improvements, particularly with sets of strings
2644 * of moderately high to high abbreviated cardinality. There is little to
2645 * lose but much to gain, which our strategy reflects.
2646 */
2647 if (trace_sort)
2648 elog(LOG, "varstr_abbrev: aborted abbreviation at %d "
2649 "(abbrev_distinct: %f, key_distinct: %f, prop_card: %f)",
2650 memtupcount, abbrev_distinct, key_distinct, sss->prop_card);
2651
2652 return true;
2653 }
2654
2655 /*
2656 * Generic equalimage support function for character type's operator classes.
2657 * Disables the use of deduplication with nondeterministic collations.
2658 */
2659 Datum
2660 btvarstrequalimage(PG_FUNCTION_ARGS)
2661 {
2662 /* Oid opcintype = PG_GETARG_OID(0); */
2663 Oid collid = PG_GET_COLLATION();
2664 pg_locale_t locale;
2665
2666 check_collation_set(collid);
2667
2668 locale = pg_newlocale_from_collation(collid);
2669
2670 PG_RETURN_BOOL(locale->deterministic);
2671 }
2672
2673 Datum
2674 text_larger(PG_FUNCTION_ARGS)
2675 {
2676 text *arg1 = PG_GETARG_TEXT_PP(0);
2677 text *arg2 = PG_GETARG_TEXT_PP(1);
2678 text *result;
2679
2680 result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0) ? arg1 : arg2);
2681
2682 PG_RETURN_TEXT_P(result);
2683 }
2684
2685 Datum
2686 text_smaller(PG_FUNCTION_ARGS)
2687 {
2688 text *arg1 = PG_GETARG_TEXT_PP(0);
2689 text *arg2 = PG_GETARG_TEXT_PP(1);
2690 text *result;
2691
2692 result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0) ? arg1 : arg2);
2693
2694 PG_RETURN_TEXT_P(result);
2695 }
2696
2697
2698 /*
2699 * Cross-type comparison functions for types text and name.
2700 */
2701
2702 Datum
2703 nameeqtext(PG_FUNCTION_ARGS)
2704 {
2705 Name arg1 = PG_GETARG_NAME(0);
2706 text *arg2 = PG_GETARG_TEXT_PP(1);
2707 size_t len1 = strlen(NameStr(*arg1));
2708 size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2709 Oid collid = PG_GET_COLLATION();
2710 bool result;
2711
2712 check_collation_set(collid);
2713
2714 if (collid == C_COLLATION_OID)
2715 result = (len1 == len2 &&
2716 memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2717 else
2718 result = (varstr_cmp(NameStr(*arg1), len1,
2719 VARDATA_ANY(arg2), len2,
2720 collid) == 0);
2721
2722 PG_FREE_IF_COPY(arg2, 1);
2723
2724 PG_RETURN_BOOL(result);
2725 }
2726
2727 Datum
2728 texteqname(PG_FUNCTION_ARGS)
2729 {
2730 text *arg1 = PG_GETARG_TEXT_PP(0);
2731 Name arg2 = PG_GETARG_NAME(1);
2732 size_t len1 = VARSIZE_ANY_EXHDR(arg1);
2733 size_t len2 = strlen(NameStr(*arg2));
2734 Oid collid = PG_GET_COLLATION();
2735 bool result;
2736
2737 check_collation_set(collid);
2738
2739 if (collid == C_COLLATION_OID)
2740 result = (len1 == len2 &&
2741 memcmp(VARDATA_ANY(arg1), NameStr(*arg2), len1) == 0);
2742 else
2743 result = (varstr_cmp(VARDATA_ANY(arg1), len1,
2744 NameStr(*arg2), len2,
2745 collid) == 0);
2746
2747 PG_FREE_IF_COPY(arg1, 0);
2748
2749 PG_RETURN_BOOL(result);
2750 }
2751
2752 Datum
2753 namenetext(PG_FUNCTION_ARGS)
2754 {
2755 Name arg1 = PG_GETARG_NAME(0);
2756 text *arg2 = PG_GETARG_TEXT_PP(1);
2757 size_t len1 = strlen(NameStr(*arg1));
2758 size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2759 Oid collid = PG_GET_COLLATION();
2760 bool result;
2761
2762 check_collation_set(collid);
2763
2764 if (collid == C_COLLATION_OID)
2765 result = !(len1 == len2 &&
2766 memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2767 else
2768 result = !(varstr_cmp(NameStr(*arg1), len1,
2769 VARDATA_ANY(arg2), len2,
2770 collid) == 0);
2771
2772 PG_FREE_IF_COPY(arg2, 1);
2773
2774 PG_RETURN_BOOL(result);
2775 }
2776
2777 Datum
2778 textnename(PG_FUNCTION_ARGS)
2779 {
2780 text *arg1 = PG_GETARG_TEXT_PP(0);
2781 Name arg2 = PG_GETARG_NAME(1);
2782 size_t len1 = VARSIZE_ANY_EXHDR(arg1);
2783 size_t len2 = strlen(NameStr(*arg2));
2784 Oid collid = PG_GET_COLLATION();
2785 bool result;
2786
2787 check_collation_set(collid);
2788
2789 if (collid == C_COLLATION_OID)
2790 result = !(len1 == len2 &&
2791 memcmp(VARDATA_ANY(arg1), NameStr(*arg2), len1) == 0);
2792 else
2793 result = !(varstr_cmp(VARDATA_ANY(arg1), len1,
2794 NameStr(*arg2), len2,
2795 collid) == 0);
2796
2797 PG_FREE_IF_COPY(arg1, 0);
2798
2799 PG_RETURN_BOOL(result);
2800 }
2801
2802 Datum
2803 btnametextcmp(PG_FUNCTION_ARGS)
2804 {
2805 Name arg1 = PG_GETARG_NAME(0);
2806 text *arg2 = PG_GETARG_TEXT_PP(1);
2807 int32 result;
2808
2809 result = varstr_cmp(NameStr(*arg1), strlen(NameStr(*arg1)),
2810 VARDATA_ANY(arg2), VARSIZE_ANY_EXHDR(arg2),
2811 PG_GET_COLLATION());
2812
2813 PG_FREE_IF_COPY(arg2, 1);
2814
2815 PG_RETURN_INT32(result);
2816 }
2817
2818 Datum
2819 bttextnamecmp(PG_FUNCTION_ARGS)
2820 {
2821 text *arg1 = PG_GETARG_TEXT_PP(0);
2822 Name arg2 = PG_GETARG_NAME(1);
2823 int32 result;
2824
2825 result = varstr_cmp(VARDATA_ANY(arg1), VARSIZE_ANY_EXHDR(arg1),
2826 NameStr(*arg2), strlen(NameStr(*arg2)),
2827 PG_GET_COLLATION());
2828
2829 PG_FREE_IF_COPY(arg1, 0);
2830
2831 PG_RETURN_INT32(result);
2832 }
2833
2834 #define CmpCall(cmpfunc) \
2835 DatumGetInt32(DirectFunctionCall2Coll(cmpfunc, \
2836 PG_GET_COLLATION(), \
2837 PG_GETARG_DATUM(0), \
2838 PG_GETARG_DATUM(1)))
2839
2840 Datum
2841 namelttext(PG_FUNCTION_ARGS)
2842 {
2843 PG_RETURN_BOOL(CmpCall(btnametextcmp) < 0);
2844 }
2845
2846 Datum
2847 nameletext(PG_FUNCTION_ARGS)
2848 {
2849 PG_RETURN_BOOL(CmpCall(btnametextcmp) <= 0);
2850 }
2851
2852 Datum
2853 namegttext(PG_FUNCTION_ARGS)
2854 {
2855 PG_RETURN_BOOL(CmpCall(btnametextcmp) > 0);
2856 }
2857
2858 Datum
2859 namegetext(PG_FUNCTION_ARGS)
2860 {
2861 PG_RETURN_BOOL(CmpCall(btnametextcmp) >= 0);
2862 }
2863
2864 Datum
2865 textltname(PG_FUNCTION_ARGS)
2866 {
2867 PG_RETURN_BOOL(CmpCall(bttextnamecmp) < 0);
2868 }
2869
2870 Datum
2871 textlename(PG_FUNCTION_ARGS)
2872 {
2873 PG_RETURN_BOOL(CmpCall(bttextnamecmp) <= 0);
2874 }
2875
2876 Datum
2877 textgtname(PG_FUNCTION_ARGS)
2878 {
2879 PG_RETURN_BOOL(CmpCall(bttextnamecmp) > 0);
2880 }
2881
2882 Datum
2883 textgename(PG_FUNCTION_ARGS)
2884 {
2885 PG_RETURN_BOOL(CmpCall(bttextnamecmp) >= 0);
2886 }
2887
2888 #undef CmpCall
2889
2890
2891 /*
2892 * The following operators support character-by-character comparison
2893 * of text datums, to allow building indexes suitable for LIKE clauses.
2894 * Note that the regular texteq/textne comparison operators, and regular
2895 * support functions 1 and 2 with "C" collation are assumed to be
2896 * compatible with these!
2897 */
2898
2899 static int
2900 internal_text_pattern_compare(text *arg1, text *arg2)
2901 {
2902 int result;
2903 int len1,
2904 len2;
2905
2906 len1 = VARSIZE_ANY_EXHDR(arg1);
2907 len2 = VARSIZE_ANY_EXHDR(arg2);
2908
2909 result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
2910 if (result != 0)
2911 return result;
2912 else if (len1 < len2)
2913 return -1;
2914 else if (len1 > len2)
2915 return 1;
2916 else
2917 return 0;
2918 }
2919
2920
2921 Datum
2922 text_pattern_lt(PG_FUNCTION_ARGS)
2923 {
2924 text *arg1 = PG_GETARG_TEXT_PP(0);
2925 text *arg2 = PG_GETARG_TEXT_PP(1);
2926 int result;
2927
2928 result = internal_text_pattern_compare(arg1, arg2);
2929
2930 PG_FREE_IF_COPY(arg1, 0);
2931 PG_FREE_IF_COPY(arg2, 1);
2932
2933 PG_RETURN_BOOL(result < 0);
2934 }
2935
2936
2937 Datum
2938 text_pattern_le(PG_FUNCTION_ARGS)
2939 {
2940 text *arg1 = PG_GETARG_TEXT_PP(0);
2941 text *arg2 = PG_GETARG_TEXT_PP(1);
2942 int result;
2943
2944 result = internal_text_pattern_compare(arg1, arg2);
2945
2946 PG_FREE_IF_COPY(arg1, 0);
2947 PG_FREE_IF_COPY(arg2, 1);
2948
2949 PG_RETURN_BOOL(result <= 0);
2950 }
2951
2952
2953 Datum
2954 text_pattern_ge(PG_FUNCTION_ARGS)
2955 {
2956 text *arg1 = PG_GETARG_TEXT_PP(0);
2957 text *arg2 = PG_GETARG_TEXT_PP(1);
2958 int result;
2959
2960 result = internal_text_pattern_compare(arg1, arg2);
2961
2962 PG_FREE_IF_COPY(arg1, 0);
2963 PG_FREE_IF_COPY(arg2, 1);
2964
2965 PG_RETURN_BOOL(result >= 0);
2966 }
2967
2968
2969 Datum
2970 text_pattern_gt(PG_FUNCTION_ARGS)
2971 {
2972 text *arg1 = PG_GETARG_TEXT_PP(0);
2973 text *arg2 = PG_GETARG_TEXT_PP(1);
2974 int result;
2975
2976 result = internal_text_pattern_compare(arg1, arg2);
2977
2978 PG_FREE_IF_COPY(arg1, 0);
2979 PG_FREE_IF_COPY(arg2, 1);
2980
2981 PG_RETURN_BOOL(result > 0);
2982 }
2983
2984
2985 Datum
2986 bttext_pattern_cmp(PG_FUNCTION_ARGS)
2987 {
2988 text *arg1 = PG_GETARG_TEXT_PP(0);
2989 text *arg2 = PG_GETARG_TEXT_PP(1);
2990 int result;
2991
2992 result = internal_text_pattern_compare(arg1, arg2);
2993
2994 PG_FREE_IF_COPY(arg1, 0);
2995 PG_FREE_IF_COPY(arg2, 1);
2996
2997 PG_RETURN_INT32(result);
2998 }
2999
3000
3001 Datum
3002 bttext_pattern_sortsupport(PG_FUNCTION_ARGS)
3003 {
3004 SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
3005 MemoryContext oldcontext;
3006
3007 oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
3008
3009 /* Use generic string SortSupport, forcing "C" collation */
3010 varstr_sortsupport(ssup, TEXTOID, C_COLLATION_OID);
3011
3012 MemoryContextSwitchTo(oldcontext);
3013
3014 PG_RETURN_VOID();
3015 }
3016
3017
3018 /*-------------------------------------------------------------
3019 * byteaoctetlen
3020 *
3021 * get the number of bytes contained in an instance of type 'bytea'
3022 *-------------------------------------------------------------
3023 */
3024 Datum
3025 byteaoctetlen(PG_FUNCTION_ARGS)
3026 {
3027 Datum str = PG_GETARG_DATUM(0);
3028
3029 /* We need not detoast the input at all */
3030 PG_RETURN_INT32(toast_raw_datum_size(str) - VARHDRSZ);
3031 }
3032
3033 /*
3034 * byteacat -
3035 * takes two bytea* and returns a bytea* that is the concatenation of
3036 * the two.
3037 *
3038 * Cloned from textcat and modified as required.
3039 */
3040 Datum
3041 byteacat(PG_FUNCTION_ARGS)
3042 {
3043 bytea *t1 = PG_GETARG_BYTEA_PP(0);
3044 bytea *t2 = PG_GETARG_BYTEA_PP(1);
3045
3046 PG_RETURN_BYTEA_P(bytea_catenate(t1, t2));
3047 }
3048
3049 /*
3050 * bytea_catenate
3051 * Guts of byteacat(), broken out so it can be used by other functions
3052 *
3053 * Arguments can be in short-header form, but not compressed or out-of-line
3054 */
3055 static bytea *
3056 bytea_catenate(bytea *t1, bytea *t2)
3057 {
3058 bytea *result;
3059 int len1,
3060 len2,
3061 len;
3062 char *ptr;
3063
3064 len1 = VARSIZE_ANY_EXHDR(t1);
3065 len2 = VARSIZE_ANY_EXHDR(t2);
3066
3067 /* paranoia ... probably should throw error instead? */
3068 if (len1 < 0)
3069 len1 = 0;
3070 if (len2 < 0)
3071 len2 = 0;
3072
3073 len = len1 + len2 + VARHDRSZ;
3074 result = (bytea *) palloc(len);
3075
3076 /* Set size of result string... */
3077 SET_VARSIZE(result, len);
3078
3079 /* Fill data field of result string... */
3080 ptr = VARDATA(result);
3081 if (len1 > 0)
3082 memcpy(ptr, VARDATA_ANY(t1), len1);
3083 if (len2 > 0)
3084 memcpy(ptr + len1, VARDATA_ANY(t2), len2);
3085
3086 return result;
3087 }
3088
3089 #define PG_STR_GET_BYTEA(str_) \
3090 DatumGetByteaPP(DirectFunctionCall1(byteain, CStringGetDatum(str_)))
3091
3092 /*
3093 * bytea_substr()
3094 * Return a substring starting at the specified position.
3095 * Cloned from text_substr and modified as required.
3096 *
3097 * Input:
3098 * - string
3099 * - starting position (is one-based)
3100 * - string length (optional)
3101 *
3102 * If the starting position is zero or less, then return from the start of the string
3103 * adjusting the length to be consistent with the "negative start" per SQL.
3104 * If the length is less than zero, an ERROR is thrown. If no third argument
3105 * (length) is provided, the length to the end of the string is assumed.
3106 */
3107 Datum
3108 bytea_substr(PG_FUNCTION_ARGS)
3109 {
3110 PG_RETURN_BYTEA_P(bytea_substring(PG_GETARG_DATUM(0),
3111 PG_GETARG_INT32(1),
3112 PG_GETARG_INT32(2),
3113 false));
3114 }
3115
3116 /*
3117 * bytea_substr_no_len -
3118 * Wrapper to avoid opr_sanity failure due to
3119 * one function accepting a different number of args.
3120 */
3121 Datum
3122 bytea_substr_no_len(PG_FUNCTION_ARGS)
3123 {
3124 PG_RETURN_BYTEA_P(bytea_substring(PG_GETARG_DATUM(0),
3125 PG_GETARG_INT32(1),
3126 -1,
3127 true));
3128 }
3129
3130 static bytea *
3131 bytea_substring(Datum str,
3132 int S,
3133 int L,
3134 bool length_not_specified)
3135 {
3136 int32 S1; /* adjusted start position */
3137 int32 L1; /* adjusted substring length */
3138 int32 E; /* end position */
3139
3140 /*
3141 * The logic here should generally match text_substring().
3142 */
3143 S1 = Max(S, 1);
3144
3145 if (length_not_specified)
3146 {
3147 /*
3148 * Not passed a length - DatumGetByteaPSlice() grabs everything to the
3149 * end of the string if we pass it a negative value for length.
3150 */
3151 L1 = -1;
3152 }
3153 else if (L < 0)
3154 {
3155 /* SQL99 says to throw an error for E < S, i.e., negative length */
3156 ereport(ERROR,
3157 (errcode(ERRCODE_SUBSTRING_ERROR),
3158 errmsg("negative substring length not allowed")));
3159 L1 = -1; /* silence stupider compilers */
3160 }
3161 else if (pg_add_s32_overflow(S, L, &E))
3162 {
3163 /*
3164 * L could be large enough for S + L to overflow, in which case the
3165 * substring must run to end of string.
3166 */
3167 L1 = -1;
3168 }
3169 else
3170 {
3171 /*
3172 * A zero or negative value for the end position can happen if the
3173 * start was negative or one. SQL99 says to return a zero-length
3174 * string.
3175 */
3176 if (E < 1)
3177 return PG_STR_GET_BYTEA("");
3178
3179 L1 = E - S1;
3180 }
3181
3182 /*
3183 * If the start position is past the end of the string, SQL99 says to
3184 * return a zero-length string -- DatumGetByteaPSlice() will do that for
3185 * us. We need only convert S1 to zero-based starting position.
3186 */
3187 return DatumGetByteaPSlice(str, S1 - 1, L1);
3188 }
3189
3190 /*
3191 * byteaoverlay
3192 * Replace specified substring of first string with second
3193 *
3194 * The SQL standard defines OVERLAY() in terms of substring and concatenation.
3195 * This code is a direct implementation of what the standard says.
3196 */
3197 Datum
3198 byteaoverlay(PG_FUNCTION_ARGS)
3199 {
3200 bytea *t1 = PG_GETARG_BYTEA_PP(0);
3201 bytea *t2 = PG_GETARG_BYTEA_PP(1);
3202 int sp = PG_GETARG_INT32(2); /* substring start position */
3203 int sl = PG_GETARG_INT32(3); /* substring length */
3204
3205 PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
3206 }
3207
3208 Datum
3209 byteaoverlay_no_len(PG_FUNCTION_ARGS)
3210 {
3211 bytea *t1 = PG_GETARG_BYTEA_PP(0);
3212 bytea *t2 = PG_GETARG_BYTEA_PP(1);
3213 int sp = PG_GETARG_INT32(2); /* substring start position */
3214 int sl;
3215
3216 sl = VARSIZE_ANY_EXHDR(t2); /* defaults to length(t2) */
3217 PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
3218 }
3219
3220 static bytea *
3221 bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
3222 {
3223 bytea *result;
3224 bytea *s1;
3225 bytea *s2;
3226 int sp_pl_sl;
3227
3228 /*
3229 * Check for possible integer-overflow cases. For negative sp, throw a
3230 * "substring length" error because that's what should be expected
3231 * according to the spec's definition of OVERLAY().
3232 */
3233 if (sp <= 0)
3234 ereport(ERROR,
3235 (errcode(ERRCODE_SUBSTRING_ERROR),
3236 errmsg("negative substring length not allowed")));
3237 if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
3238 ereport(ERROR,
3239 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3240 errmsg("integer out of range")));
3241
3242 s1 = bytea_substring(PointerGetDatum(t1), 1, sp - 1, false);
3243 s2 = bytea_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
3244 result = bytea_catenate(s1, t2);
3245 result = bytea_catenate(result, s2);
3246
3247 return result;
3248 }
3249
3250 /*
3251 * bit_count
3252 */
3253 Datum
3254 bytea_bit_count(PG_FUNCTION_ARGS)
3255 {
3256 bytea *t1 = PG_GETARG_BYTEA_PP(0);
3257
3258 PG_RETURN_INT64(pg_popcount(VARDATA_ANY(t1), VARSIZE_ANY_EXHDR(t1)));
3259 }
3260
3261 /*
3262 * byteapos -
3263 * Return the position of the specified substring.
3264 * Implements the SQL POSITION() function.
3265 * Cloned from textpos and modified as required.
3266 */
3267 Datum
3268 byteapos(PG_FUNCTION_ARGS)
3269 {
3270 bytea *t1 = PG_GETARG_BYTEA_PP(0);
3271 bytea *t2 = PG_GETARG_BYTEA_PP(1);
3272 int pos;
3273 int px,
3274 p;
3275 int len1,
3276 len2;
3277 char *p1,
3278 *p2;
3279
3280 len1 = VARSIZE_ANY_EXHDR(t1);
3281 len2 = VARSIZE_ANY_EXHDR(t2);
3282
3283 if (len2 <= 0)
3284 PG_RETURN_INT32(1); /* result for empty pattern */
3285
3286 p1 = VARDATA_ANY(t1);
3287 p2 = VARDATA_ANY(t2);
3288
3289 pos = 0;
3290 px = (len1 - len2);
3291 for (p = 0; p <= px; p++)
3292 {
3293 if ((*p2 == *p1) && (memcmp(p1, p2, len2) == 0))
3294 {
3295 pos = p + 1;
3296 break;
3297 };
3298 p1++;
3299 };
3300
3301 PG_RETURN_INT32(pos);
3302 }
3303
3304 /*-------------------------------------------------------------
3305 * byteaGetByte
3306 *
3307 * this routine treats "bytea" as an array of bytes.
3308 * It returns the Nth byte (a number between 0 and 255).
3309 *-------------------------------------------------------------
3310 */
3311 Datum
3312 byteaGetByte(PG_FUNCTION_ARGS)
3313 {
3314 bytea *v = PG_GETARG_BYTEA_PP(0);
3315 int32 n = PG_GETARG_INT32(1);
3316 int len;
3317 int byte;
3318
3319 len = VARSIZE_ANY_EXHDR(v);
3320
3321 if (n < 0 || n >= len)
3322 ereport(ERROR,
3323 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3324 errmsg("index %d out of valid range, 0..%d",
3325 n, len - 1)));
3326
3327 byte = ((unsigned char *) VARDATA_ANY(v))[n];
3328
3329 PG_RETURN_INT32(byte);
3330 }
3331
3332 /*-------------------------------------------------------------
3333 * byteaGetBit
3334 *
3335 * This routine treats a "bytea" type like an array of bits.
3336 * It returns the value of the Nth bit (0 or 1).
3337 *
3338 *-------------------------------------------------------------
3339 */
3340 Datum
3341 byteaGetBit(PG_FUNCTION_ARGS)
3342 {
3343 bytea *v = PG_GETARG_BYTEA_PP(0);
3344 int64 n = PG_GETARG_INT64(1);
3345 int byteNo,
3346 bitNo;
3347 int len;
3348 int byte;
3349
3350 len = VARSIZE_ANY_EXHDR(v);
3351
3352 if (n < 0 || n >= (int64) len * 8)
3353 ereport(ERROR,
3354 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3355 errmsg("index %" PRId64 " out of valid range, 0..%" PRId64,
3356 n, (int64) len * 8 - 1)));
3357
3358 /* n/8 is now known < len, so safe to cast to int */
3359 byteNo = (int) (n / 8);
3360 bitNo = (int) (n % 8);
3361
3362 byte = ((unsigned char *) VARDATA_ANY(v))[byteNo];
3363
3364 if (byte & (1 << bitNo))
3365 PG_RETURN_INT32(1);
3366 else
3367 PG_RETURN_INT32(0);
3368 }
3369
3370 /*-------------------------------------------------------------
3371 * byteaSetByte
3372 *
3373 * Given an instance of type 'bytea' creates a new one with
3374 * the Nth byte set to the given value.
3375 *
3376 *-------------------------------------------------------------
3377 */
3378 Datum
3379 byteaSetByte(PG_FUNCTION_ARGS)
3380 {
3381 bytea *res = PG_GETARG_BYTEA_P_COPY(0);
3382 int32 n = PG_GETARG_INT32(1);
3383 int32 newByte = PG_GETARG_INT32(2);
3384 int len;
3385
3386 len = VARSIZE(res) - VARHDRSZ;
3387
3388 if (n < 0 || n >= len)
3389 ereport(ERROR,
3390 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3391 errmsg("index %d out of valid range, 0..%d",
3392 n, len - 1)));
3393
3394 /*
3395 * Now set the byte.
3396 */
3397 ((unsigned char *) VARDATA(res))[n] = newByte;
3398
3399 PG_RETURN_BYTEA_P(res);
3400 }
3401
3402 /*-------------------------------------------------------------
3403 * byteaSetBit
3404 *
3405 * Given an instance of type 'bytea' creates a new one with
3406 * the Nth bit set to the given value.
3407 *
3408 *-------------------------------------------------------------
3409 */
3410 Datum
3411 byteaSetBit(PG_FUNCTION_ARGS)
3412 {
3413 bytea *res = PG_GETARG_BYTEA_P_COPY(0);
3414 int64 n = PG_GETARG_INT64(1);
3415 int32 newBit = PG_GETARG_INT32(2);
3416 int len;
3417 int oldByte,
3418 newByte;
3419 int byteNo,
3420 bitNo;
3421
3422 len = VARSIZE(res) - VARHDRSZ;
3423
3424 if (n < 0 || n >= (int64) len * 8)
3425 ereport(ERROR,
3426 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3427 errmsg("index %" PRId64 " out of valid range, 0..%" PRId64,
3428 n, (int64) len * 8 - 1)));
3429
3430 /* n/8 is now known < len, so safe to cast to int */
3431 byteNo = (int) (n / 8);
3432 bitNo = (int) (n % 8);
3433
3434 /*
3435 * sanity check!
3436 */
3437 if (newBit != 0 && newBit != 1)
3438 ereport(ERROR,
3439 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3440 errmsg("new bit must be 0 or 1")));
3441
3442 /*
3443 * Update the byte.
3444 */
3445 oldByte = ((unsigned char *) VARDATA(res))[byteNo];
3446
3447 if (newBit == 0)
3448 newByte = oldByte & (~(1 << bitNo));
3449 else
3450 newByte = oldByte | (1 << bitNo);
3451
3452 ((unsigned char *) VARDATA(res))[byteNo] = newByte;
3453
3454 PG_RETURN_BYTEA_P(res);
3455 }
3456
3457 /*
3458 * Return reversed bytea
3459 */
3460 Datum
3461 bytea_reverse(PG_FUNCTION_ARGS)
3462 {
3463 bytea *v = PG_GETARG_BYTEA_PP(0);
3464 const char *p = VARDATA_ANY(v);
3465 int len = VARSIZE_ANY_EXHDR(v);
3466 const char *endp = p + len;
3467 bytea *result = palloc(len + VARHDRSZ);
3468 char *dst = (char *) VARDATA(result) + len;
3469
3470 SET_VARSIZE(result, len + VARHDRSZ);
3471
3472 while (p < endp)
3473 *(--dst) = *p++;
3474
3475 PG_RETURN_BYTEA_P(result);
3476 }
3477
3478
3479 /* text_name()
3480 * Converts a text type to a Name type.
3481 */
3482 Datum
3483 text_name(PG_FUNCTION_ARGS)
3484 {
3485 text *s = PG_GETARG_TEXT_PP(0);
3486 Name result;
3487 int len;
3488
3489 len = VARSIZE_ANY_EXHDR(s);
3490
3491 /* Truncate oversize input */
3492 if (len >= NAMEDATALEN)
3493 len = pg_mbcliplen(VARDATA_ANY(s), len, NAMEDATALEN - 1);
3494
3495 /* We use palloc0 here to ensure result is zero-padded */
3496 result = (Name) palloc0(NAMEDATALEN);
3497 memcpy(NameStr(*result), VARDATA_ANY(s), len);
3498
3499 PG_RETURN_NAME(result);
3500 }
3501
3502 /* name_text()
3503 * Converts a Name type to a text type.
3504 */
3505 Datum
3506 name_text(PG_FUNCTION_ARGS)
3507 {
3508 Name s = PG_GETARG_NAME(0);
3509
3510 PG_RETURN_TEXT_P(cstring_to_text(NameStr(*s)));
3511 }
3512
3513
3514 /*
3515 * textToQualifiedNameList - convert a text object to list of names
3516 *
3517 * This implements the input parsing needed by nextval() and other
3518 * functions that take a text parameter representing a qualified name.
3519 * We split the name at dots, downcase if not double-quoted, and
3520 * truncate names if they're too long.
3521 */
3522 List *
3523 textToQualifiedNameList(text *textval)
3524 {
3525 char *rawname;
3526 List *result = NIL;
3527 List *namelist;
3528 ListCell *l;
3529
3530 /* Convert to C string (handles possible detoasting). */
3531 /* Note we rely on being able to modify rawname below. */
3532 rawname = text_to_cstring(textval);
3533
3534 if (!SplitIdentifierString(rawname, '.', &namelist))
3535 ereport(ERROR,
3536 (errcode(ERRCODE_INVALID_NAME),
3537 errmsg("invalid name syntax")));
3538
3539 if (namelist == NIL)
3540 ereport(ERROR,
3541 (errcode(ERRCODE_INVALID_NAME),
3542 errmsg("invalid name syntax")));
3543
3544 foreach(l, namelist)
3545 {
3546 char *curname = (char *) lfirst(l);
3547
3548 result = lappend(result, makeString(pstrdup(curname)));
3549 }
3550
3551 pfree(rawname);
3552 list_free(namelist);
3553
3554 return result;
3555 }
3556
3557 /*
3558 * SplitIdentifierString --- parse a string containing identifiers
3559 *
3560 * This is the guts of textToQualifiedNameList, and is exported for use in
3561 * other situations such as parsing GUC variables. In the GUC case, it's
3562 * important to avoid memory leaks, so the API is designed to minimize the
3563 * amount of stuff that needs to be allocated and freed.
3564 *
3565 * Inputs:
3566 * rawstring: the input string; must be overwritable! On return, it's
3567 * been modified to contain the separated identifiers.
3568 * separator: the separator punctuation expected between identifiers
3569 * (typically '.' or ','). Whitespace may also appear around
3570 * identifiers.
3571 * Outputs:
3572 * namelist: filled with a palloc'd list of pointers to identifiers within
3573 * rawstring. Caller should list_free() this even on error return.
3574 *
3575 * Returns true if okay, false if there is a syntax error in the string.
3576 *
3577 * Note that an empty string is considered okay here, though not in
3578 * textToQualifiedNameList.
3579 */
3580 bool
3581 SplitIdentifierString(char *rawstring, char separator,
3582 List **namelist)
3583 {
3584 char *nextp = rawstring;
3585 bool done = false;
3586
3587 *namelist = NIL;
3588
3589 while (scanner_isspace(*nextp))
3590 nextp++; /* skip leading whitespace */
3591
3592 if (*nextp == '\0')
3593 return true; /* allow empty string */
3594
3595 /* At the top of the loop, we are at start of a new identifier. */
3596 do
3597 {
3598 char *curname;
3599 char *endp;
3600
3601 if (*nextp == '"')
3602 {
3603 /* Quoted name --- collapse quote-quote pairs, no downcasing */
3604 curname = nextp + 1;
3605 for (;;)
3606 {
3607 endp = strchr(nextp + 1, '"');
3608 if (endp == NULL)
3609 return false; /* mismatched quotes */
3610 if (endp[1] != '"')
3611 break; /* found end of quoted name */
3612 /* Collapse adjacent quotes into one quote, and look again */
3613 memmove(endp, endp + 1, strlen(endp));
3614 nextp = endp;
3615 }
3616 /* endp now points at the terminating quote */
3617 nextp = endp + 1;
3618 }
3619 else
3620 {
3621 /* Unquoted name --- extends to separator or whitespace */
3622 char *downname;
3623 int len;
3624
3625 curname = nextp;
3626 while (*nextp && *nextp != separator &&
3627 !scanner_isspace(*nextp))
3628 nextp++;
3629 endp = nextp;
3630 if (curname == nextp)
3631 return false; /* empty unquoted name not allowed */
3632
3633 /*
3634 * Downcase the identifier, using same code as main lexer does.
3635 *
3636 * XXX because we want to overwrite the input in-place, we cannot
3637 * support a downcasing transformation that increases the string
3638 * length. This is not a problem given the current implementation
3639 * of downcase_truncate_identifier, but we'll probably have to do
3640 * something about this someday.
3641 */
3642 len = endp - curname;
3643 downname = downcase_truncate_identifier(curname, len, false);
3644 Assert(strlen(downname) <= len);
3645 strncpy(curname, downname, len); /* strncpy is required here */
3646 pfree(downname);
3647 }
3648
3649 while (scanner_isspace(*nextp))
3650 nextp++; /* skip trailing whitespace */
3651
3652 if (*nextp == separator)
3653 {
3654 nextp++;
3655 while (scanner_isspace(*nextp))
3656 nextp++; /* skip leading whitespace for next */
3657 /* we expect another name, so done remains false */
3658 }
3659 else if (*nextp == '\0')
3660 done = true;
3661 else
3662 return false; /* invalid syntax */
3663
3664 /* Now safe to overwrite separator with a null */
3665 *endp = '\0';
3666
3667 /* Truncate name if it's overlength */
3668 truncate_identifier(curname, strlen(curname), false);
3669
3670 /*
3671 * Finished isolating current name --- add it to list
3672 */
3673 *namelist = lappend(*namelist, curname);
3674
3675 /* Loop back if we didn't reach end of string */
3676 } while (!done);
3677
3678 return true;
3679 }
3680
3681
3682 /*
3683 * SplitDirectoriesString --- parse a string containing file/directory names
3684 *
3685 * This works fine on file names too; the function name is historical.
3686 *
3687 * This is similar to SplitIdentifierString, except that the parsing
3688 * rules are meant to handle pathnames instead of identifiers: there is
3689 * no downcasing, embedded spaces are allowed, the max length is MAXPGPATH-1,
3690 * and we apply canonicalize_path() to each extracted string. Because of the
3691 * last, the returned strings are separately palloc'd rather than being
3692 * pointers into rawstring --- but we still scribble on rawstring.
3693 *
3694 * Inputs:
3695 * rawstring: the input string; must be modifiable!
3696 * separator: the separator punctuation expected between directories
3697 * (typically ',' or ';'). Whitespace may also appear around
3698 * directories.
3699 * Outputs:
3700 * namelist: filled with a palloc'd list of directory names.
3701 * Caller should list_free_deep() this even on error return.
3702 *
3703 * Returns true if okay, false if there is a syntax error in the string.
3704 *
3705 * Note that an empty string is considered okay here.
3706 */
3707 bool
3708 SplitDirectoriesString(char *rawstring, char separator,
3709 List **namelist)
3710 {
3711 char *nextp = rawstring;
3712 bool done = false;
3713
3714 *namelist = NIL;
3715
3716 while (scanner_isspace(*nextp))
3717 nextp++; /* skip leading whitespace */
3718
3719 if (*nextp == '\0')
3720 return true; /* allow empty string */
3721
3722 /* At the top of the loop, we are at start of a new directory. */
3723 do
3724 {
3725 char *curname;
3726 char *endp;
3727
3728 if (*nextp == '"')
3729 {
3730 /* Quoted name --- collapse quote-quote pairs */
3731 curname = nextp + 1;
3732 for (;;)
3733 {
3734 endp = strchr(nextp + 1, '"');
3735 if (endp == NULL)
3736 return false; /* mismatched quotes */
3737 if (endp[1] != '"')
3738 break; /* found end of quoted name */
3739 /* Collapse adjacent quotes into one quote, and look again */
3740 memmove(endp, endp + 1, strlen(endp));
3741 nextp = endp;
3742 }
3743 /* endp now points at the terminating quote */
3744 nextp = endp + 1;
3745 }
3746 else
3747 {
3748 /* Unquoted name --- extends to separator or end of string */
3749 curname = endp = nextp;
3750 while (*nextp && *nextp != separator)
3751 {
3752 /* trailing whitespace should not be included in name */
3753 if (!scanner_isspace(*nextp))
3754 endp = nextp + 1;
3755 nextp++;
3756 }
3757 if (curname == endp)
3758 return false; /* empty unquoted name not allowed */
3759 }
3760
3761 while (scanner_isspace(*nextp))
3762 nextp++; /* skip trailing whitespace */
3763
3764 if (*nextp == separator)
3765 {
3766 nextp++;
3767 while (scanner_isspace(*nextp))
3768 nextp++; /* skip leading whitespace for next */
3769 /* we expect another name, so done remains false */
3770 }
3771 else if (*nextp == '\0')
3772 done = true;
3773 else
3774 return false; /* invalid syntax */
3775
3776 /* Now safe to overwrite separator with a null */
3777 *endp = '\0';
3778
3779 /* Truncate path if it's overlength */
3780 if (strlen(curname) >= MAXPGPATH)
3781 curname[MAXPGPATH - 1] = '\0';
3782
3783 /*
3784 * Finished isolating current name --- add it to list
3785 */
3786 curname = pstrdup(curname);
3787 canonicalize_path(curname);
3788 *namelist = lappend(*namelist, curname);
3789
3790 /* Loop back if we didn't reach end of string */
3791 } while (!done);
3792
3793 return true;
3794 }
3795
3796
3797 /*
3798 * SplitGUCList --- parse a string containing identifiers or file names
3799 *
3800 * This is used to split the value of a GUC_LIST_QUOTE GUC variable, without
3801 * presuming whether the elements will be taken as identifiers or file names.
3802 * We assume the input has already been through flatten_set_variable_args(),
3803 * so that we need never downcase (if appropriate, that was done already).
3804 * Nor do we ever truncate, since we don't know the correct max length.
3805 * We disallow embedded whitespace for simplicity (it shouldn't matter,
3806 * because any embedded whitespace should have led to double-quoting).
3807 * Otherwise the API is identical to SplitIdentifierString.
3808 *
3809 * XXX it's annoying to have so many copies of this string-splitting logic.
3810 * However, it's not clear that having one function with a bunch of option
3811 * flags would be much better.
3812 *
3813 * XXX there is a version of this function in src/fe_utils/string_utils.c.
3814 * Be sure to update that if you have to change this.
3815 *
3816 * Inputs:
3817 * rawstring: the input string; must be overwritable! On return, it's
3818 * been modified to contain the separated identifiers.
3819 * separator: the separator punctuation expected between identifiers
3820 * (typically '.' or ','). Whitespace may also appear around
3821 * identifiers.
3822 * Outputs:
3823 * namelist: filled with a palloc'd list of pointers to identifiers within
3824 * rawstring. Caller should list_free() this even on error return.
3825 *
3826 * Returns true if okay, false if there is a syntax error in the string.
3827 */
3828 bool
3829 SplitGUCList(char *rawstring, char separator,
3830 List **namelist)
3831 {
3832 char *nextp = rawstring;
3833 bool done = false;
3834
3835 *namelist = NIL;
3836
3837 while (scanner_isspace(*nextp))
3838 nextp++; /* skip leading whitespace */
3839
3840 if (*nextp == '\0')
3841 return true; /* allow empty string */
3842
3843 /* At the top of the loop, we are at start of a new identifier. */
3844 do
3845 {
3846 char *curname;
3847 char *endp;
3848
3849 if (*nextp == '"')
3850 {
3851 /* Quoted name --- collapse quote-quote pairs */
3852 curname = nextp + 1;
3853 for (;;)
3854 {
3855 endp = strchr(nextp + 1, '"');
3856 if (endp == NULL)
3857 return false; /* mismatched quotes */
3858 if (endp[1] != '"')
3859 break; /* found end of quoted name */
3860 /* Collapse adjacent quotes into one quote, and look again */
3861 memmove(endp, endp + 1, strlen(endp));
3862 nextp = endp;
3863 }
3864 /* endp now points at the terminating quote */
3865 nextp = endp + 1;
3866 }
3867 else
3868 {
3869 /* Unquoted name --- extends to separator or whitespace */
3870 curname = nextp;
3871 while (*nextp && *nextp != separator &&
3872 !scanner_isspace(*nextp))
3873 nextp++;
3874 endp = nextp;
3875 if (curname == nextp)
3876 return false; /* empty unquoted name not allowed */
3877 }
3878
3879 while (scanner_isspace(*nextp))
3880 nextp++; /* skip trailing whitespace */
3881
3882 if (*nextp == separator)
3883 {
3884 nextp++;
3885 while (scanner_isspace(*nextp))
3886 nextp++; /* skip leading whitespace for next */
3887 /* we expect another name, so done remains false */
3888 }
3889 else if (*nextp == '\0')
3890 done = true;
3891 else
3892 return false; /* invalid syntax */
3893
3894 /* Now safe to overwrite separator with a null */
3895 *endp = '\0';
3896
3897 /*
3898 * Finished isolating current name --- add it to list
3899 */
3900 *namelist = lappend(*namelist, curname);
3901
3902 /* Loop back if we didn't reach end of string */
3903 } while (!done);
3904
3905 return true;
3906 }
3907
3908
3909 /*****************************************************************************
3910 * Comparison Functions used for bytea
3911 *
3912 * Note: btree indexes need these routines not to leak memory; therefore,
3913 * be careful to free working copies of toasted datums. Most places don't
3914 * need to be so careful.
3915 *****************************************************************************/
3916
3917 Datum
3918 byteaeq(PG_FUNCTION_ARGS)
3919 {
3920 Datum arg1 = PG_GETARG_DATUM(0);
3921 Datum arg2 = PG_GETARG_DATUM(1);
3922 bool result;
3923 Size len1,
3924 len2;
3925
3926 /*
3927 * We can use a fast path for unequal lengths, which might save us from
3928 * having to detoast one or both values.
3929 */
3930 len1 = toast_raw_datum_size(arg1);
3931 len2 = toast_raw_datum_size(arg2);
3932 if (len1 != len2)
3933 result = false;
3934 else
3935 {
3936 bytea *barg1 = DatumGetByteaPP(arg1);
3937 bytea *barg2 = DatumGetByteaPP(arg2);
3938
3939 result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
3940 len1 - VARHDRSZ) == 0);
3941
3942 PG_FREE_IF_COPY(barg1, 0);
3943 PG_FREE_IF_COPY(barg2, 1);
3944 }
3945
3946 PG_RETURN_BOOL(result);
3947 }
3948
3949 Datum
3950 byteane(PG_FUNCTION_ARGS)
3951 {
3952 Datum arg1 = PG_GETARG_DATUM(0);
3953 Datum arg2 = PG_GETARG_DATUM(1);
3954 bool result;
3955 Size len1,
3956 len2;
3957
3958 /*
3959 * We can use a fast path for unequal lengths, which might save us from
3960 * having to detoast one or both values.
3961 */
3962 len1 = toast_raw_datum_size(arg1);
3963 len2 = toast_raw_datum_size(arg2);
3964 if (len1 != len2)
3965 result = true;
3966 else
3967 {
3968 bytea *barg1 = DatumGetByteaPP(arg1);
3969 bytea *barg2 = DatumGetByteaPP(arg2);
3970
3971 result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
3972 len1 - VARHDRSZ) != 0);
3973
3974 PG_FREE_IF_COPY(barg1, 0);
3975 PG_FREE_IF_COPY(barg2, 1);
3976 }
3977
3978 PG_RETURN_BOOL(result);
3979 }
3980
3981 Datum
3982 bytealt(PG_FUNCTION_ARGS)
3983 {
3984 bytea *arg1 = PG_GETARG_BYTEA_PP(0);
3985 bytea *arg2 = PG_GETARG_BYTEA_PP(1);
3986 int len1,
3987 len2;
3988 int cmp;
3989
3990 len1 = VARSIZE_ANY_EXHDR(arg1);
3991 len2 = VARSIZE_ANY_EXHDR(arg2);
3992
3993 cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3994
3995 PG_FREE_IF_COPY(arg1, 0);
3996 PG_FREE_IF_COPY(arg2, 1);
3997
3998 PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 < len2)));
3999 }
4000
4001 Datum
4002 byteale(PG_FUNCTION_ARGS)
4003 {
4004 bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4005 bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4006 int len1,
4007 len2;
4008 int cmp;
4009
4010 len1 = VARSIZE_ANY_EXHDR(arg1);
4011 len2 = VARSIZE_ANY_EXHDR(arg2);
4012
4013 cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4014
4015 PG_FREE_IF_COPY(arg1, 0);
4016 PG_FREE_IF_COPY(arg2, 1);
4017
4018 PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 <= len2)));
4019 }
4020
4021 Datum
4022 byteagt(PG_FUNCTION_ARGS)
4023 {
4024 bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4025 bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4026 int len1,
4027 len2;
4028 int cmp;
4029
4030 len1 = VARSIZE_ANY_EXHDR(arg1);
4031 len2 = VARSIZE_ANY_EXHDR(arg2);
4032
4033 cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4034
4035 PG_FREE_IF_COPY(arg1, 0);
4036 PG_FREE_IF_COPY(arg2, 1);
4037
4038 PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 > len2)));
4039 }
4040
4041 Datum
4042 byteage(PG_FUNCTION_ARGS)
4043 {
4044 bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4045 bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4046 int len1,
4047 len2;
4048 int cmp;
4049
4050 len1 = VARSIZE_ANY_EXHDR(arg1);
4051 len2 = VARSIZE_ANY_EXHDR(arg2);
4052
4053 cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4054
4055 PG_FREE_IF_COPY(arg1, 0);
4056 PG_FREE_IF_COPY(arg2, 1);
4057
4058 PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 >= len2)));
4059 }
4060
4061 Datum
4062 byteacmp(PG_FUNCTION_ARGS)
4063 {
4064 bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4065 bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4066 int len1,
4067 len2;
4068 int cmp;
4069
4070 len1 = VARSIZE_ANY_EXHDR(arg1);
4071 len2 = VARSIZE_ANY_EXHDR(arg2);
4072
4073 cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4074 if ((cmp == 0) && (len1 != len2))
4075 cmp = (len1 < len2) ? -1 : 1;
4076
4077 PG_FREE_IF_COPY(arg1, 0);
4078 PG_FREE_IF_COPY(arg2, 1);
4079
4080 PG_RETURN_INT32(cmp);
4081 }
4082
4083 Datum
4084 bytea_larger(PG_FUNCTION_ARGS)
4085 {
4086 bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4087 bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4088 bytea *result;
4089 int len1,
4090 len2;
4091 int cmp;
4092
4093 len1 = VARSIZE_ANY_EXHDR(arg1);
4094 len2 = VARSIZE_ANY_EXHDR(arg2);
4095
4096 cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4097 result = ((cmp > 0) || ((cmp == 0) && (len1 > len2)) ? arg1 : arg2);
4098
4099 PG_RETURN_BYTEA_P(result);
4100 }
4101
4102 Datum
4103 bytea_smaller(PG_FUNCTION_ARGS)
4104 {
4105 bytea *arg1 = PG_GETARG_BYTEA_PP(0);
4106 bytea *arg2 = PG_GETARG_BYTEA_PP(1);
4107 bytea *result;
4108 int len1,
4109 len2;
4110 int cmp;
4111
4112 len1 = VARSIZE_ANY_EXHDR(arg1);
4113 len2 = VARSIZE_ANY_EXHDR(arg2);
4114
4115 cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
4116 result = ((cmp < 0) || ((cmp == 0) && (len1 < len2)) ? arg1 : arg2);
4117
4118 PG_RETURN_BYTEA_P(result);
4119 }
4120
4121 Datum
4122 bytea_sortsupport(PG_FUNCTION_ARGS)
4123 {
4124 SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
4125 MemoryContext oldcontext;
4126
4127 oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
4128
4129 /* Use generic string SortSupport, forcing "C" collation */
4130 varstr_sortsupport(ssup, BYTEAOID, C_COLLATION_OID);
4131
4132 MemoryContextSwitchTo(oldcontext);
4133
4134 PG_RETURN_VOID();
4135 }
4136
4137 /* Cast bytea -> int2 */
4138 Datum
4139 bytea_int2(PG_FUNCTION_ARGS)
4140 {
4141 bytea *v = PG_GETARG_BYTEA_PP(0);
4142 int len = VARSIZE_ANY_EXHDR(v);
4143 uint16 result;
4144
4145 /* Check that the byte array is not too long */
4146 if (len > sizeof(result))
4147 ereport(ERROR,
4148 errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4149 errmsg("smallint out of range"));
4150
4151 /* Convert it to an integer; most significant bytes come first */
4152 result = 0;
4153 for (int i = 0; i < len; i++)
4154 {
4155 result <<= BITS_PER_BYTE;
4156 result |= ((unsigned char *) VARDATA_ANY(v))[i];
4157 }
4158
4159 PG_RETURN_INT16(result);
4160 }
4161
4162 /* Cast bytea -> int4 */
4163 Datum
4164 bytea_int4(PG_FUNCTION_ARGS)
4165 {
4166 bytea *v = PG_GETARG_BYTEA_PP(0);
4167 int len = VARSIZE_ANY_EXHDR(v);
4168 uint32 result;
4169
4170 /* Check that the byte array is not too long */
4171 if (len > sizeof(result))
4172 ereport(ERROR,
4173 errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4174 errmsg("integer out of range"));
4175
4176 /* Convert it to an integer; most significant bytes come first */
4177 result = 0;
4178 for (int i = 0; i < len; i++)
4179 {
4180 result <<= BITS_PER_BYTE;
4181 result |= ((unsigned char *) VARDATA_ANY(v))[i];
4182 }
4183
4184 PG_RETURN_INT32(result);
4185 }
4186
4187 /* Cast bytea -> int8 */
4188 Datum
4189 bytea_int8(PG_FUNCTION_ARGS)
4190 {
4191 bytea *v = PG_GETARG_BYTEA_PP(0);
4192 int len = VARSIZE_ANY_EXHDR(v);
4193 uint64 result;
4194
4195 /* Check that the byte array is not too long */
4196 if (len > sizeof(result))
4197 ereport(ERROR,
4198 errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4199 errmsg("bigint out of range"));
4200
4201 /* Convert it to an integer; most significant bytes come first */
4202 result = 0;
4203 for (int i = 0; i < len; i++)
4204 {
4205 result <<= BITS_PER_BYTE;
4206 result |= ((unsigned char *) VARDATA_ANY(v))[i];
4207 }
4208
4209 PG_RETURN_INT64(result);
4210 }
4211
4212 /* Cast int2 -> bytea; can just use int2send() */
4213 Datum
4214 int2_bytea(PG_FUNCTION_ARGS)
4215 {
4216 return int2send(fcinfo);
4217 }
4218
4219 /* Cast int4 -> bytea; can just use int4send() */
4220 Datum
4221 int4_bytea(PG_FUNCTION_ARGS)
4222 {
4223 return int4send(fcinfo);
4224 }
4225
4226 /* Cast int8 -> bytea; can just use int8send() */
4227 Datum
4228 int8_bytea(PG_FUNCTION_ARGS)
4229 {
4230 return int8send(fcinfo);
4231 }
4232
4233 /*
4234 * appendStringInfoText
4235 *
4236 * Append a text to str.
4237 * Like appendStringInfoString(str, text_to_cstring(t)) but faster.
4238 */
4239 static void
4240 appendStringInfoText(StringInfo str, const text *t)
4241 {
4242 appendBinaryStringInfo(str, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
4243 }
4244
4245 /*
4246 * replace_text
4247 * replace all occurrences of 'old_sub_str' in 'orig_str'
4248 * with 'new_sub_str' to form 'new_str'
4249 *
4250 * returns 'orig_str' if 'old_sub_str' == '' or 'orig_str' == ''
4251 * otherwise returns 'new_str'
4252 */
4253 Datum
4254 replace_text(PG_FUNCTION_ARGS)
4255 {
4256 text *src_text = PG_GETARG_TEXT_PP(0);
4257 text *from_sub_text = PG_GETARG_TEXT_PP(1);
4258 text *to_sub_text = PG_GETARG_TEXT_PP(2);
4259 int src_text_len;
4260 int from_sub_text_len;
4261 TextPositionState state;
4262 text *ret_text;
4263 int chunk_len;
4264 char *curr_ptr;
4265 char *start_ptr;
4266 StringInfoData str;
4267 bool found;
4268
4269 src_text_len = VARSIZE_ANY_EXHDR(src_text);
4270 from_sub_text_len = VARSIZE_ANY_EXHDR(from_sub_text);
4271
4272 /* Return unmodified source string if empty source or pattern */
4273 if (src_text_len < 1 || from_sub_text_len < 1)
4274 {
4275 PG_RETURN_TEXT_P(src_text);
4276 }
4277
4278 text_position_setup(src_text, from_sub_text, PG_GET_COLLATION(), &state);
4279
4280 found = text_position_next(&state);
4281
4282 /* When the from_sub_text is not found, there is nothing to do. */
4283 if (!found)
4284 {
4285 text_position_cleanup(&state);
4286 PG_RETURN_TEXT_P(src_text);
4287 }
4288 curr_ptr = text_position_get_match_ptr(&state);
4289 start_ptr = VARDATA_ANY(src_text);
4290
4291 initStringInfo(&str);
4292
4293 do
4294 {
4295 CHECK_FOR_INTERRUPTS();
4296
4297 /* copy the data skipped over by last text_position_next() */
4298 chunk_len = curr_ptr - start_ptr;
4299 appendBinaryStringInfo(&str, start_ptr, chunk_len);
4300
4301 appendStringInfoText(&str, to_sub_text);
4302
4303 start_ptr = curr_ptr + state.last_match_len;
4304
4305 found = text_position_next(&state);
4306 if (found)
4307 curr_ptr = text_position_get_match_ptr(&state);
4308 }
4309 while (found);
4310
4311 /* copy trailing data */
4312 chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4313 appendBinaryStringInfo(&str, start_ptr, chunk_len);
4314
4315 text_position_cleanup(&state);
4316
4317 ret_text = cstring_to_text_with_len(str.data, str.len);
4318 pfree(str.data);
4319
4320 PG_RETURN_TEXT_P(ret_text);
4321 }
4322
4323 /*
4324 * check_replace_text_has_escape
4325 *
4326 * Returns 0 if text contains no backslashes that need processing.
4327 * Returns 1 if text contains backslashes, but not regexp submatch specifiers.
4328 * Returns 2 if text contains regexp submatch specifiers (\1 .. \9).
4329 */
4330 static int
4331 check_replace_text_has_escape(const text *replace_text)
4332 {
4333 int result = 0;
4334 const char *p = VARDATA_ANY(replace_text);
4335 const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
4336
4337 while (p < p_end)
4338 {
4339 /* Find next escape char, if any. */
4340 p = memchr(p, '\\', p_end - p);
4341 if (p == NULL)
4342 break;
4343 p++;
4344 /* Note: a backslash at the end doesn't require extra processing. */
4345 if (p < p_end)
4346 {
4347 if (*p >= '1' && *p <= '9')
4348 return 2; /* Found a submatch specifier, so done */
4349 result = 1; /* Found some other sequence, keep looking */
4350 p++;
4351 }
4352 }
4353 return result;
4354 }
4355
4356 /*
4357 * appendStringInfoRegexpSubstr
4358 *
4359 * Append replace_text to str, substituting regexp back references for
4360 * \n escapes. start_ptr is the start of the match in the source string,
4361 * at logical character position data_pos.
4362 */
4363 static void
4364 appendStringInfoRegexpSubstr(StringInfo str, text *replace_text,
4365 regmatch_t *pmatch,
4366 char *start_ptr, int data_pos)
4367 {
4368 const char *p = VARDATA_ANY(replace_text);
4369 const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
4370
4371 while (p < p_end)
4372 {
4373 const char *chunk_start = p;
4374 int so;
4375 int eo;
4376
4377 /* Find next escape char, if any. */
4378 p = memchr(p, '\\', p_end - p);
4379 if (p == NULL)
4380 p = p_end;
4381
4382 /* Copy the text we just scanned over, if any. */
4383 if (p > chunk_start)
4384 appendBinaryStringInfo(str, chunk_start, p - chunk_start);
4385
4386 /* Done if at end of string, else advance over escape char. */
4387 if (p >= p_end)
4388 break;
4389 p++;
4390
4391 if (p >= p_end)
4392 {
4393 /* Escape at very end of input. Treat same as unexpected char */
4394 appendStringInfoChar(str, '\\');
4395 break;
4396 }
4397
4398 if (*p >= '1' && *p <= '9')
4399 {
4400 /* Use the back reference of regexp. */
4401 int idx = *p - '0';
4402
4403 so = pmatch[idx].rm_so;
4404 eo = pmatch[idx].rm_eo;
4405 p++;
4406 }
4407 else if (*p == '&')
4408 {
4409 /* Use the entire matched string. */
4410 so = pmatch[0].rm_so;
4411 eo = pmatch[0].rm_eo;
4412 p++;
4413 }
4414 else if (*p == '\\')
4415 {
4416 /* \\ means transfer one \ to output. */
4417 appendStringInfoChar(str, '\\');
4418 p++;
4419 continue;
4420 }
4421 else
4422 {
4423 /*
4424 * If escape char is not followed by any expected char, just treat
4425 * it as ordinary data to copy. (XXX would it be better to throw
4426 * an error?)
4427 */
4428 appendStringInfoChar(str, '\\');
4429 continue;
4430 }
4431
4432 if (so >= 0 && eo >= 0)
4433 {
4434 /*
4435 * Copy the text that is back reference of regexp. Note so and eo
4436 * are counted in characters not bytes.
4437 */
4438 char *chunk_start;
4439 int chunk_len;
4440
4441 Assert(so >= data_pos);
4442 chunk_start = start_ptr;
4443 chunk_start += charlen_to_bytelen(chunk_start, so - data_pos);
4444 chunk_len = charlen_to_bytelen(chunk_start, eo - so);
4445 appendBinaryStringInfo(str, chunk_start, chunk_len);
4446 }
4447 }
4448 }
4449
4450 /*
4451 * replace_text_regexp
4452 *
4453 * replace substring(s) in src_text that match pattern with replace_text.
4454 * The replace_text can contain backslash markers to substitute
4455 * (parts of) the matched text.
4456 *
4457 * cflags: regexp compile flags.
4458 * collation: collation to use.
4459 * search_start: the character (not byte) offset in src_text at which to
4460 * begin searching.
4461 * n: if 0, replace all matches; if > 0, replace only the N'th match.
4462 */
4463 text *
4464 replace_text_regexp(text *src_text, text *pattern_text,
4465 text *replace_text,
4466 int cflags, Oid collation,
4467 int search_start, int n)
4468 {
4469 text *ret_text;
4470 regex_t *re;
4471 int src_text_len = VARSIZE_ANY_EXHDR(src_text);
4472 int nmatches = 0;
4473 StringInfoData buf;
4474 regmatch_t pmatch[10]; /* main match, plus \1 to \9 */
4475 int nmatch = lengthof(pmatch);
4476 pg_wchar *data;
4477 size_t data_len;
4478 int data_pos;
4479 char *start_ptr;
4480 int escape_status;
4481
4482 initStringInfo(&buf);
4483
4484 /* Convert data string to wide characters. */
4485 data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar));
4486 data_len = pg_mb2wchar_with_len(VARDATA_ANY(src_text), data, src_text_len);
4487
4488 /* Check whether replace_text has escapes, especially regexp submatches. */
4489 escape_status = check_replace_text_has_escape(replace_text);
4490
4491 /* If no regexp submatches, we can use REG_NOSUB. */
4492 if (escape_status < 2)
4493 {
4494 cflags |= REG_NOSUB;
4495 /* Also tell pg_regexec we only want the whole-match location. */
4496 nmatch = 1;
4497 }
4498
4499 /* Prepare the regexp. */
4500 re = RE_compile_and_cache(pattern_text, cflags, collation);
4501
4502 /* start_ptr points to the data_pos'th character of src_text */
4503 start_ptr = (char *) VARDATA_ANY(src_text);
4504 data_pos = 0;
4505
4506 while (search_start <= data_len)
4507 {
4508 int regexec_result;
4509
4510 CHECK_FOR_INTERRUPTS();
4511
4512 regexec_result = pg_regexec(re,
4513 data,
4514 data_len,
4515 search_start,
4516 NULL, /* no details */
4517 nmatch,
4518 pmatch,
4519 0);
4520
4521 if (regexec_result == REG_NOMATCH)
4522 break;
4523
4524 if (regexec_result != REG_OKAY)
4525 {
4526 char errMsg[100];
4527
4528 pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
4529 ereport(ERROR,
4530 (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
4531 errmsg("regular expression failed: %s", errMsg)));
4532 }
4533
4534 /*
4535 * Count matches, and decide whether to replace this match.
4536 */
4537 nmatches++;
4538 if (n > 0 && nmatches != n)
4539 {
4540 /*
4541 * No, so advance search_start, but not start_ptr/data_pos. (Thus,
4542 * we treat the matched text as if it weren't matched, and copy it
4543 * to the output later.)
4544 */
4545 search_start = pmatch[0].rm_eo;
4546 if (pmatch[0].rm_so == pmatch[0].rm_eo)
4547 search_start++;
4548 continue;
4549 }
4550
4551 /*
4552 * Copy the text to the left of the match position. Note we are given
4553 * character not byte indexes.
4554 */
4555 if (pmatch[0].rm_so - data_pos > 0)
4556 {
4557 int chunk_len;
4558
4559 chunk_len = charlen_to_bytelen(start_ptr,
4560 pmatch[0].rm_so - data_pos);
4561 appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4562
4563 /*
4564 * Advance start_ptr over that text, to avoid multiple rescans of
4565 * it if the replace_text contains multiple back-references.
4566 */
4567 start_ptr += chunk_len;
4568 data_pos = pmatch[0].rm_so;
4569 }
4570
4571 /*
4572 * Copy the replace_text, processing escapes if any are present.
4573 */
4574 if (escape_status > 0)
4575 appendStringInfoRegexpSubstr(&buf, replace_text, pmatch,
4576 start_ptr, data_pos);
4577 else
4578 appendStringInfoText(&buf, replace_text);
4579
4580 /* Advance start_ptr and data_pos over the matched text. */
4581 start_ptr += charlen_to_bytelen(start_ptr,
4582 pmatch[0].rm_eo - data_pos);
4583 data_pos = pmatch[0].rm_eo;
4584
4585 /*
4586 * If we only want to replace one occurrence, we're done.
4587 */
4588 if (n > 0)
4589 break;
4590
4591 /*
4592 * Advance search position. Normally we start the next search at the
4593 * end of the previous match; but if the match was of zero length, we
4594 * have to advance by one character, or we'd just find the same match
4595 * again.
4596 */
4597 search_start = data_pos;
4598 if (pmatch[0].rm_so == pmatch[0].rm_eo)
4599 search_start++;
4600 }
4601
4602 /*
4603 * Copy the text to the right of the last match.
4604 */
4605 if (data_pos < data_len)
4606 {
4607 int chunk_len;
4608
4609 chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4610 appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4611 }
4612
4613 ret_text = cstring_to_text_with_len(buf.data, buf.len);
4614 pfree(buf.data);
4615 pfree(data);
4616
4617 return ret_text;
4618 }
4619
4620 /*
4621 * split_part
4622 * parse input string based on provided field separator
4623 * return N'th item (1 based, negative counts from end)
4624 */
4625 Datum
4626 split_part(PG_FUNCTION_ARGS)
4627 {
4628 text *inputstring = PG_GETARG_TEXT_PP(0);
4629 text *fldsep = PG_GETARG_TEXT_PP(1);
4630 int fldnum = PG_GETARG_INT32(2);
4631 int inputstring_len;
4632 int fldsep_len;
4633 TextPositionState state;
4634 char *start_ptr;
4635 char *end_ptr;
4636 text *result_text;
4637 bool found;
4638
4639 /* field number is 1 based */
4640 if (fldnum == 0)
4641 ereport(ERROR,
4642 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4643 errmsg("field position must not be zero")));
4644
4645 inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4646 fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
4647
4648 /* return empty string for empty input string */
4649 if (inputstring_len < 1)
4650 PG_RETURN_TEXT_P(cstring_to_text(""));
4651
4652 /* handle empty field separator */
4653 if (fldsep_len < 1)
4654 {
4655 /* if first or last field, return input string, else empty string */
4656 if (fldnum == 1 || fldnum == -1)
4657 PG_RETURN_TEXT_P(inputstring);
4658 else
4659 PG_RETURN_TEXT_P(cstring_to_text(""));
4660 }
4661
4662 /* find the first field separator */
4663 text_position_setup(inputstring, fldsep, PG_GET_COLLATION(), &state);
4664
4665 found = text_position_next(&state);
4666
4667 /* special case if fldsep not found at all */
4668 if (!found)
4669 {
4670 text_position_cleanup(&state);
4671 /* if first or last field, return input string, else empty string */
4672 if (fldnum == 1 || fldnum == -1)
4673 PG_RETURN_TEXT_P(inputstring);
4674 else
4675 PG_RETURN_TEXT_P(cstring_to_text(""));
4676 }
4677
4678 /*
4679 * take care of a negative field number (i.e. count from the right) by
4680 * converting to a positive field number; we need total number of fields
4681 */
4682 if (fldnum < 0)
4683 {
4684 /* we found a fldsep, so there are at least two fields */
4685 int numfields = 2;
4686
4687 while (text_position_next(&state))
4688 numfields++;
4689
4690 /* special case of last field does not require an extra pass */
4691 if (fldnum == -1)
4692 {
4693 start_ptr = text_position_get_match_ptr(&state) + state.last_match_len;
4694 end_ptr = VARDATA_ANY(inputstring) + inputstring_len;
4695 text_position_cleanup(&state);
4696 PG_RETURN_TEXT_P(cstring_to_text_with_len(start_ptr,
4697 end_ptr - start_ptr));
4698 }
4699
4700 /* else, convert fldnum to positive notation */
4701 fldnum += numfields + 1;
4702
4703 /* if nonexistent field, return empty string */
4704 if (fldnum <= 0)
4705 {
4706 text_position_cleanup(&state);
4707 PG_RETURN_TEXT_P(cstring_to_text(""));
4708 }
4709
4710 /* reset to pointing at first match, but now with positive fldnum */
4711 text_position_reset(&state);
4712 found = text_position_next(&state);
4713 Assert(found);
4714 }
4715
4716 /* identify bounds of first field */
4717 start_ptr = VARDATA_ANY(inputstring);
4718 end_ptr = text_position_get_match_ptr(&state);
4719
4720 while (found && --fldnum > 0)
4721 {
4722 /* identify bounds of next field */
4723 start_ptr = end_ptr + state.last_match_len;
4724 found = text_position_next(&state);
4725 if (found)
4726 end_ptr = text_position_get_match_ptr(&state);
4727 }
4728
4729 text_position_cleanup(&state);
4730
4731 if (fldnum > 0)
4732 {
4733 /* N'th field separator not found */
4734 /* if last field requested, return it, else empty string */
4735 if (fldnum == 1)
4736 {
4737 int last_len = start_ptr - VARDATA_ANY(inputstring);
4738
4739 result_text = cstring_to_text_with_len(start_ptr,
4740 inputstring_len - last_len);
4741 }
4742 else
4743 result_text = cstring_to_text("");
4744 }
4745 else
4746 {
4747 /* non-last field requested */
4748 result_text = cstring_to_text_with_len(start_ptr, end_ptr - start_ptr);
4749 }
4750
4751 PG_RETURN_TEXT_P(result_text);
4752 }
4753
4754 /*
4755 * Convenience function to return true when two text params are equal.
4756 */
4757 static bool
4758 text_isequal(text *txt1, text *txt2, Oid collid)
4759 {
4760 return DatumGetBool(DirectFunctionCall2Coll(texteq,
4761 collid,
4762 PointerGetDatum(txt1),
4763 PointerGetDatum(txt2)));
4764 }
4765
4766 /*
4767 * text_to_array
4768 * parse input string and return text array of elements,
4769 * based on provided field separator
4770 */
4771 Datum
4772 text_to_array(PG_FUNCTION_ARGS)
4773 {
4774 SplitTextOutputData tstate;
4775
4776 /* For array output, tstate should start as all zeroes */
4777 memset(&tstate, 0, sizeof(tstate));
4778
4779 if (!split_text(fcinfo, &tstate))
4780 PG_RETURN_NULL();
4781
4782 if (tstate.astate == NULL)
4783 PG_RETURN_ARRAYTYPE_P(construct_empty_array(TEXTOID));
4784
4785 PG_RETURN_DATUM(makeArrayResult(tstate.astate,
4786 CurrentMemoryContext));
4787 }
4788
4789 /*
4790 * text_to_array_null
4791 * parse input string and return text array of elements,
4792 * based on provided field separator and null string
4793 *
4794 * This is a separate entry point only to prevent the regression tests from
4795 * complaining about different argument sets for the same internal function.
4796 */
4797 Datum
4798 text_to_array_null(PG_FUNCTION_ARGS)
4799 {
4800 return text_to_array(fcinfo);
4801 }
4802
4803 /*
4804 * text_to_table
4805 * parse input string and return table of elements,
4806 * based on provided field separator
4807 */
4808 Datum
4809 text_to_table(PG_FUNCTION_ARGS)
4810 {
4811 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
4812 SplitTextOutputData tstate;
4813
4814 tstate.astate = NULL;
4815 InitMaterializedSRF(fcinfo, MAT_SRF_USE_EXPECTED_DESC);
4816 tstate.tupstore = rsi->setResult;
4817 tstate.tupdesc = rsi->setDesc;
4818
4819 (void) split_text(fcinfo, &tstate);
4820
4821 return (Datum) 0;
4822 }
4823
4824 /*
4825 * text_to_table_null
4826 * parse input string and return table of elements,
4827 * based on provided field separator and null string
4828 *
4829 * This is a separate entry point only to prevent the regression tests from
4830 * complaining about different argument sets for the same internal function.
4831 */
4832 Datum
4833 text_to_table_null(PG_FUNCTION_ARGS)
4834 {
4835 return text_to_table(fcinfo);
4836 }
4837
4838 /*
4839 * Common code for text_to_array, text_to_array_null, text_to_table
4840 * and text_to_table_null functions.
4841 *
4842 * These are not strict so we have to test for null inputs explicitly.
4843 * Returns false if result is to be null, else returns true.
4844 *
4845 * Note that if the result is valid but empty (zero elements), we return
4846 * without changing *tstate --- caller must handle that case, too.
4847 */
4848 static bool
4849 split_text(FunctionCallInfo fcinfo, SplitTextOutputData *tstate)
4850 {
4851 text *inputstring;
4852 text *fldsep;
4853 text *null_string;
4854 Oid collation = PG_GET_COLLATION();
4855 int inputstring_len;
4856 int fldsep_len;
4857 char *start_ptr;
4858 text *result_text;
4859
4860 /* when input string is NULL, then result is NULL too */
4861 if (PG_ARGISNULL(0))
4862 return false;
4863
4864 inputstring = PG_GETARG_TEXT_PP(0);
4865
4866 /* fldsep can be NULL */
4867 if (!PG_ARGISNULL(1))
4868 fldsep = PG_GETARG_TEXT_PP(1);
4869 else
4870 fldsep = NULL;
4871
4872 /* null_string can be NULL or omitted */
4873 if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
4874 null_string = PG_GETARG_TEXT_PP(2);
4875 else
4876 null_string = NULL;
4877
4878 if (fldsep != NULL)
4879 {
4880 /*
4881 * Normal case with non-null fldsep. Use the text_position machinery
4882 * to search for occurrences of fldsep.
4883 */
4884 TextPositionState state;
4885
4886 inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4887 fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
4888
4889 /* return empty set for empty input string */
4890 if (inputstring_len < 1)
4891 return true;
4892
4893 /* empty field separator: return input string as a one-element set */
4894 if (fldsep_len < 1)
4895 {
4896 split_text_accum_result(tstate, inputstring,
4897 null_string, collation);
4898 return true;
4899 }
4900
4901 text_position_setup(inputstring, fldsep, collation, &state);
4902
4903 start_ptr = VARDATA_ANY(inputstring);
4904
4905 for (;;)
4906 {
4907 bool found;
4908 char *end_ptr;
4909 int chunk_len;
4910
4911 CHECK_FOR_INTERRUPTS();
4912
4913 found = text_position_next(&state);
4914 if (!found)
4915 {
4916 /* fetch last field */
4917 chunk_len = ((char *) inputstring + VARSIZE_ANY(inputstring)) - start_ptr;
4918 end_ptr = NULL; /* not used, but some compilers complain */
4919 }
4920 else
4921 {
4922 /* fetch non-last field */
4923 end_ptr = text_position_get_match_ptr(&state);
4924 chunk_len = end_ptr - start_ptr;
4925 }
4926
4927 /* build a temp text datum to pass to split_text_accum_result */
4928 result_text = cstring_to_text_with_len(start_ptr, chunk_len);
4929
4930 /* stash away this field */
4931 split_text_accum_result(tstate, result_text,
4932 null_string, collation);
4933
4934 pfree(result_text);
4935
4936 if (!found)
4937 break;
4938
4939 start_ptr = end_ptr + state.last_match_len;
4940 }
4941
4942 text_position_cleanup(&state);
4943 }
4944 else
4945 {
4946 const char *end_ptr;
4947
4948 /*
4949 * When fldsep is NULL, each character in the input string becomes a
4950 * separate element in the result set. The separator is effectively
4951 * the space between characters.
4952 */
4953 inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4954
4955 start_ptr = VARDATA_ANY(inputstring);
4956 end_ptr = start_ptr + inputstring_len;
4957
4958 while (inputstring_len > 0)
4959 {
4960 int chunk_len = pg_mblen_range(start_ptr, end_ptr);
4961
4962 CHECK_FOR_INTERRUPTS();
4963
4964 /* build a temp text datum to pass to split_text_accum_result */
4965 result_text = cstring_to_text_with_len(start_ptr, chunk_len);
4966
4967 /* stash away this field */
4968 split_text_accum_result(tstate, result_text,
4969 null_string, collation);
4970
4971 pfree(result_text);
4972
4973 start_ptr += chunk_len;
4974 inputstring_len -= chunk_len;
4975 }
4976 }
4977
4978 return true;
4979 }
4980
4981 /*
4982 * Add text item to result set (table or array).
4983 *
4984 * This is also responsible for checking to see if the item matches
4985 * the null_string, in which case we should emit NULL instead.
4986 */
4987 static void
4988 split_text_accum_result(SplitTextOutputData *tstate,
4989 text *field_value,
4990 text *null_string,
4991 Oid collation)
4992 {
4993 bool is_null = false;
4994
4995 if (null_string && text_isequal(field_value, null_string, collation))
4996 is_null = true;
4997
4998 if (tstate->tupstore)
4999 {
5000 Datum values[1];
5001 bool nulls[1];
5002
5003 values[0] = PointerGetDatum(field_value);
5004 nulls[0] = is_null;
5005
5006 tuplestore_putvalues(tstate->tupstore,
5007 tstate->tupdesc,
5008 values,
5009 nulls);
5010 }
5011 else
5012 {
5013 tstate->astate = accumArrayResult(tstate->astate,
5014 PointerGetDatum(field_value),
5015 is_null,
5016 TEXTOID,
5017 CurrentMemoryContext);
5018 }
5019 }
5020
5021 /*
5022 * array_to_text
5023 * concatenate Cstring representation of input array elements
5024 * using provided field separator
5025 */
5026 Datum
5027 array_to_text(PG_FUNCTION_ARGS)
5028 {
5029 ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
5030 char *fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
5031
5032 PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, NULL));
5033 }
5034
5035 /*
5036 * array_to_text_null
5037 * concatenate Cstring representation of input array elements
5038 * using provided field separator and null string
5039 *
5040 * This version is not strict so we have to test for null inputs explicitly.
5041 */
5042 Datum
5043 array_to_text_null(PG_FUNCTION_ARGS)
5044 {
5045 ArrayType *v;
5046 char *fldsep;
5047 char *null_string;
5048
5049 /* returns NULL when first or second parameter is NULL */
5050 if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
5051 PG_RETURN_NULL();
5052
5053 v = PG_GETARG_ARRAYTYPE_P(0);
5054 fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
5055
5056 /* NULL null string is passed through as a null pointer */
5057 if (!PG_ARGISNULL(2))
5058 null_string = text_to_cstring(PG_GETARG_TEXT_PP(2));
5059 else
5060 null_string = NULL;
5061
5062 PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, null_string));
5063 }
5064
5065 /*
5066 * common code for array_to_text and array_to_text_null functions
5067 */
5068 static text *
5069 array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v,
5070 const char *fldsep, const char *null_string)
5071 {
5072 text *result;
5073 int nitems,
5074 *dims,
5075 ndims;
5076 Oid element_type;
5077 int typlen;
5078 bool typbyval;
5079 char typalign;
5080 StringInfoData buf;
5081 bool printed = false;
5082 char *p;
5083 bits8 *bitmap;
5084 int bitmask;
5085 int i;
5086 ArrayMetaState *my_extra;
5087
5088 ndims = ARR_NDIM(v);
5089 dims = ARR_DIMS(v);
5090 nitems = ArrayGetNItems(ndims, dims);
5091
5092 /* if there are no elements, return an empty string */
5093 if (nitems == 0)
5094 return cstring_to_text_with_len("", 0);
5095
5096 element_type = ARR_ELEMTYPE(v);
5097 initStringInfo(&buf);
5098
5099 /*
5100 * We arrange to look up info about element type, including its output
5101 * conversion proc, only once per series of calls, assuming the element
5102 * type doesn't change underneath us.
5103 */
5104 my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
5105 if (my_extra == NULL)
5106 {
5107 fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5108 sizeof(ArrayMetaState));
5109 my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
5110 my_extra->element_type = ~element_type;
5111 }
5112
5113 if (my_extra->element_type != element_type)
5114 {
5115 /*
5116 * Get info about element type, including its output conversion proc
5117 */
5118 get_type_io_data(element_type, IOFunc_output,
5119 &my_extra->typlen, &my_extra->typbyval,
5120 &my_extra->typalign, &my_extra->typdelim,
5121 &my_extra->typioparam, &my_extra->typiofunc);
5122 fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc,
5123 fcinfo->flinfo->fn_mcxt);
5124 my_extra->element_type = element_type;
5125 }
5126 typlen = my_extra->typlen;
5127 typbyval = my_extra->typbyval;
5128 typalign = my_extra->typalign;
5129
5130 p = ARR_DATA_PTR(v);
5131 bitmap = ARR_NULLBITMAP(v);
5132 bitmask = 1;
5133
5134 for (i = 0; i < nitems; i++)
5135 {
5136 Datum itemvalue;
5137 char *value;
5138
5139 /* Get source element, checking for NULL */
5140 if (bitmap && (*bitmap & bitmask) == 0)
5141 {
5142 /* if null_string is NULL, we just ignore null elements */
5143 if (null_string != NULL)
5144 {
5145 if (printed)
5146 appendStringInfo(&buf, "%s%s", fldsep, null_string);
5147 else
5148 appendStringInfoString(&buf, null_string);
5149 printed = true;
5150 }
5151 }
5152 else
5153 {
5154 itemvalue = fetch_att(p, typbyval, typlen);
5155
5156 value = OutputFunctionCall(&my_extra->proc, itemvalue);
5157
5158 if (printed)
5159 appendStringInfo(&buf, "%s%s", fldsep, value);
5160 else
5161 appendStringInfoString(&buf, value);
5162 printed = true;
5163
5164 p = att_addlength_pointer(p, typlen, p);
5165 p = (char *) att_align_nominal(p, typalign);
5166 }
5167
5168 /* advance bitmap pointer if any */
5169 if (bitmap)
5170 {
5171 bitmask <<= 1;
5172 if (bitmask == 0x100)
5173 {
5174 bitmap++;
5175 bitmask = 1;
5176 }
5177 }
5178 }
5179
5180 result = cstring_to_text_with_len(buf.data, buf.len);
5181 pfree(buf.data);
5182
5183 return result;
5184 }
5185
5186 /*
5187 * Workhorse for to_bin, to_oct, and to_hex. Note that base must be > 1 and <=
5188 * 16.
5189 */
5190 static inline text *
5191 convert_to_base(uint64 value, int base)
5192 {
5193 const char *digits = "0123456789abcdef";
5194
5195 /* We size the buffer for to_bin's longest possible return value. */
5196 char buf[sizeof(uint64) * BITS_PER_BYTE];
5197 char *const end = buf + sizeof(buf);
5198 char *ptr = end;
5199
5200 Assert(base > 1);
5201 Assert(base <= 16);
5202
5203 do
5204 {
5205 *--ptr = digits[value % base];
5206 value /= base;
5207 } while (ptr > buf && value);
5208
5209 return cstring_to_text_with_len(ptr, end - ptr);
5210 }
5211
5212 /*
5213 * Convert an integer to a string containing a base-2 (binary) representation
5214 * of the number.
5215 */
5216 Datum
5217 to_bin32(PG_FUNCTION_ARGS)
5218 {
5219 uint64 value = (uint32) PG_GETARG_INT32(0);
5220
5221 PG_RETURN_TEXT_P(convert_to_base(value, 2));
5222 }
5223 Datum
5224 to_bin64(PG_FUNCTION_ARGS)
5225 {
5226 uint64 value = (uint64) PG_GETARG_INT64(0);
5227
5228 PG_RETURN_TEXT_P(convert_to_base(value, 2));
5229 }
5230
5231 /*
5232 * Convert an integer to a string containing a base-8 (oct) representation of
5233 * the number.
5234 */
5235 Datum
5236 to_oct32(PG_FUNCTION_ARGS)
5237 {
5238 uint64 value = (uint32) PG_GETARG_INT32(0);
5239
5240 PG_RETURN_TEXT_P(convert_to_base(value, 8));
5241 }
5242 Datum
5243 to_oct64(PG_FUNCTION_ARGS)
5244 {
5245 uint64 value = (uint64) PG_GETARG_INT64(0);
5246
5247 PG_RETURN_TEXT_P(convert_to_base(value, 8));
5248 }
5249
5250 /*
5251 * Convert an integer to a string containing a base-16 (hex) representation of
5252 * the number.
5253 */
5254 Datum
5255 to_hex32(PG_FUNCTION_ARGS)
5256 {
5257 uint64 value = (uint32) PG_GETARG_INT32(0);
5258
5259 PG_RETURN_TEXT_P(convert_to_base(value, 16));
5260 }
5261 Datum
5262 to_hex64(PG_FUNCTION_ARGS)
5263 {
5264 uint64 value = (uint64) PG_GETARG_INT64(0);
5265
5266 PG_RETURN_TEXT_P(convert_to_base(value, 16));
5267 }
5268
5269 /*
5270 * Return the size of a datum, possibly compressed
5271 *
5272 * Works on any data type
5273 */
5274 Datum
5275 pg_column_size(PG_FUNCTION_ARGS)
5276 {
5277 Datum value = PG_GETARG_DATUM(0);
5278 int32 result;
5279 int typlen;
5280
5281 /* On first call, get the input type's typlen, and save at *fn_extra */
5282 if (fcinfo->flinfo->fn_extra == NULL)
5283 {
5284 /* Lookup the datatype of the supplied argument */
5285 Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5286
5287 typlen = get_typlen(argtypeid);
5288 if (typlen == 0) /* should not happen */
5289 elog(ERROR, "cache lookup failed for type %u", argtypeid);
5290
5291 fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5292 sizeof(int));
5293 *((int *) fcinfo->flinfo->fn_extra) = typlen;
5294 }
5295 else
5296 typlen = *((int *) fcinfo->flinfo->fn_extra);
5297
5298 if (typlen == -1)
5299 {
5300 /* varlena type, possibly toasted */
5301 result = toast_datum_size(value);
5302 }
5303 else if (typlen == -2)
5304 {
5305 /* cstring */
5306 result = strlen(DatumGetCString(value)) + 1;
5307 }
5308 else
5309 {
5310 /* ordinary fixed-width type */
5311 result = typlen;
5312 }
5313
5314 PG_RETURN_INT32(result);
5315 }
5316
5317 /*
5318 * Return the compression method stored in the compressed attribute. Return
5319 * NULL for non varlena type or uncompressed data.
5320 */
5321 Datum
5322 pg_column_compression(PG_FUNCTION_ARGS)
5323 {
5324 int typlen;
5325 char *result;
5326 ToastCompressionId cmid;
5327
5328 /* On first call, get the input type's typlen, and save at *fn_extra */
5329 if (fcinfo->flinfo->fn_extra == NULL)
5330 {
5331 /* Lookup the datatype of the supplied argument */
5332 Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5333
5334 typlen = get_typlen(argtypeid);
5335 if (typlen == 0) /* should not happen */
5336 elog(ERROR, "cache lookup failed for type %u", argtypeid);
5337
5338 fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5339 sizeof(int));
5340 *((int *) fcinfo->flinfo->fn_extra) = typlen;
5341 }
5342 else
5343 typlen = *((int *) fcinfo->flinfo->fn_extra);
5344
5345 if (typlen != -1)
5346 PG_RETURN_NULL();
5347
5348 /* get the compression method id stored in the compressed varlena */
5349 cmid = toast_get_compression_id((struct varlena *)
5350 DatumGetPointer(PG_GETARG_DATUM(0)));
5351 if (cmid == TOAST_INVALID_COMPRESSION_ID)
5352 PG_RETURN_NULL();
5353
5354 /* convert compression method id to compression method name */
5355 switch (cmid)
5356 {
5357 case TOAST_PGLZ_COMPRESSION_ID:
5358 result = "pglz";
5359 break;
5360 case TOAST_LZ4_COMPRESSION_ID:
5361 result = "lz4";
5362 break;
5363 default:
5364 elog(ERROR, "invalid compression method id %d", cmid);
5365 }
5366
5367 PG_RETURN_TEXT_P(cstring_to_text(result));
5368 }
5369
5370 /*
5371 * Return the chunk_id of the on-disk TOASTed value. Return NULL if the value
5372 * is un-TOASTed or not on-disk.
5373 */
5374 Datum
5375 pg_column_toast_chunk_id(PG_FUNCTION_ARGS)
5376 {
5377 int typlen;
5378 struct varlena *attr;
5379 struct varatt_external toast_pointer;
5380
5381 /* On first call, get the input type's typlen, and save at *fn_extra */
5382 if (fcinfo->flinfo->fn_extra == NULL)
5383 {
5384 /* Lookup the datatype of the supplied argument */
5385 Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
5386
5387 typlen = get_typlen(argtypeid);
5388 if (typlen == 0) /* should not happen */
5389 elog(ERROR, "cache lookup failed for type %u", argtypeid);
5390
5391 fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5392 sizeof(int));
5393 *((int *) fcinfo->flinfo->fn_extra) = typlen;
5394 }
5395 else
5396 typlen = *((int *) fcinfo->flinfo->fn_extra);
5397
5398 if (typlen != -1)
5399 PG_RETURN_NULL();
5400
5401 attr = (struct varlena *) DatumGetPointer(PG_GETARG_DATUM(0));
5402
5403 if (!VARATT_IS_EXTERNAL_ONDISK(attr))
5404 PG_RETURN_NULL();
5405
5406 VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
5407
5408 PG_RETURN_OID(toast_pointer.va_valueid);
5409 }
5410
5411 /*
5412 * string_agg - Concatenates values and returns string.
5413 *
5414 * Syntax: string_agg(value text, delimiter text) RETURNS text
5415 *
5416 * Note: Any NULL values are ignored. The first-call delimiter isn't
5417 * actually used at all, and on subsequent calls the delimiter precedes
5418 * the associated value.
5419 */
5420
5421 /* subroutine to initialize state */
5422 static StringInfo
5423 makeStringAggState(FunctionCallInfo fcinfo)
5424 {
5425 StringInfo state;
5426 MemoryContext aggcontext;
5427 MemoryContext oldcontext;
5428
5429 if (!AggCheckCallContext(fcinfo, &aggcontext))
5430 {
5431 /* cannot be called directly because of internal-type argument */
5432 elog(ERROR, "string_agg_transfn called in non-aggregate context");
5433 }
5434
5435 /*
5436 * Create state in aggregate context. It'll stay there across subsequent
5437 * calls.
5438 */
5439 oldcontext = MemoryContextSwitchTo(aggcontext);
5440 state = makeStringInfo();
5441 MemoryContextSwitchTo(oldcontext);
5442
5443 return state;
5444 }
5445
5446 Datum
5447 string_agg_transfn(PG_FUNCTION_ARGS)
5448 {
5449 StringInfo state;
5450
5451 state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5452
5453 /* Append the value unless null, preceding it with the delimiter. */
5454 if (!PG_ARGISNULL(1))
5455 {
5456 text *value = PG_GETARG_TEXT_PP(1);
5457 bool isfirst = false;
5458
5459 /*
5460 * You might think we can just throw away the first delimiter, however
5461 * we must keep it as we may be a parallel worker doing partial
5462 * aggregation building a state to send to the main process. We need
5463 * to keep the delimiter of every aggregation so that the combine
5464 * function can properly join up the strings of two separately
5465 * partially aggregated results. The first delimiter is only stripped
5466 * off in the final function. To know how much to strip off the front
5467 * of the string, we store the length of the first delimiter in the
5468 * StringInfo's cursor field, which we don't otherwise need here.
5469 */
5470 if (state == NULL)
5471 {
5472 state = makeStringAggState(fcinfo);
5473 isfirst = true;
5474 }
5475
5476 if (!PG_ARGISNULL(2))
5477 {
5478 text *delim = PG_GETARG_TEXT_PP(2);
5479
5480 appendStringInfoText(state, delim);
5481 if (isfirst)
5482 state->cursor = VARSIZE_ANY_EXHDR(delim);
5483 }
5484
5485 appendStringInfoText(state, value);
5486 }
5487
5488 /*
5489 * The transition type for string_agg() is declared to be "internal",
5490 * which is a pass-by-value type the same size as a pointer.
5491 */
5492 if (state)
5493 PG_RETURN_POINTER(state);
5494 PG_RETURN_NULL();
5495 }
5496
5497 /*
5498 * string_agg_combine
5499 * Aggregate combine function for string_agg(text) and string_agg(bytea)
5500 */
5501 Datum
5502 string_agg_combine(PG_FUNCTION_ARGS)
5503 {
5504 StringInfo state1;
5505 StringInfo state2;
5506 MemoryContext agg_context;
5507
5508 if (!AggCheckCallContext(fcinfo, &agg_context))
5509 elog(ERROR, "aggregate function called in non-aggregate context");
5510
5511 state1 = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5512 state2 = PG_ARGISNULL(1) ? NULL : (StringInfo) PG_GETARG_POINTER(1);
5513
5514 if (state2 == NULL)
5515 {
5516 /*
5517 * NULL state2 is easy, just return state1, which we know is already
5518 * in the agg_context
5519 */
5520 if (state1 == NULL)
5521 PG_RETURN_NULL();
5522 PG_RETURN_POINTER(state1);
5523 }
5524
5525 if (state1 == NULL)
5526 {
5527 /* We must copy state2's data into the agg_context */
5528 MemoryContext old_context;
5529
5530 old_context = MemoryContextSwitchTo(agg_context);
5531 state1 = makeStringAggState(fcinfo);
5532 appendBinaryStringInfo(state1, state2->data, state2->len);
5533 state1->cursor = state2->cursor;
5534 MemoryContextSwitchTo(old_context);
5535 }
5536 else if (state2->len > 0)
5537 {
5538 /* Combine ... state1->cursor does not change in this case */
5539 appendBinaryStringInfo(state1, state2->data, state2->len);
5540 }
5541
5542 PG_RETURN_POINTER(state1);
5543 }
5544
5545 /*
5546 * string_agg_serialize
5547 * Aggregate serialize function for string_agg(text) and string_agg(bytea)
5548 *
5549 * This is strict, so we need not handle NULL input
5550 */
5551 Datum
5552 string_agg_serialize(PG_FUNCTION_ARGS)
5553 {
5554 StringInfo state;
5555 StringInfoData buf;
5556 bytea *result;
5557
5558 /* cannot be called directly because of internal-type argument */
5559 Assert(AggCheckCallContext(fcinfo, NULL));
5560
5561 state = (StringInfo) PG_GETARG_POINTER(0);
5562
5563 pq_begintypsend(&buf);
5564
5565 /* cursor */
5566 pq_sendint(&buf, state->cursor, 4);
5567
5568 /* data */
5569 pq_sendbytes(&buf, state->data, state->len);
5570
5571 result = pq_endtypsend(&buf);
5572
5573 PG_RETURN_BYTEA_P(result);
5574 }
5575
5576 /*
5577 * string_agg_deserialize
5578 * Aggregate deserial function for string_agg(text) and string_agg(bytea)
5579 *
5580 * This is strict, so we need not handle NULL input
5581 */
5582 Datum
5583 string_agg_deserialize(PG_FUNCTION_ARGS)
5584 {
5585 bytea *sstate;
5586 StringInfo result;
5587 StringInfoData buf;
5588 char *data;
5589 int datalen;
5590
5591 /* cannot be called directly because of internal-type argument */
5592 Assert(AggCheckCallContext(fcinfo, NULL));
5593
5594 sstate = PG_GETARG_BYTEA_PP(0);
5595
5596 /*
5597 * Initialize a StringInfo so that we can "receive" it using the standard
5598 * recv-function infrastructure.
5599 */
5600 initReadOnlyStringInfo(&buf, VARDATA_ANY(sstate),
5601 VARSIZE_ANY_EXHDR(sstate));
5602
5603 result = makeStringAggState(fcinfo);
5604
5605 /* cursor */
5606 result->cursor = pq_getmsgint(&buf, 4);
5607
5608 /* data */
5609 datalen = VARSIZE_ANY_EXHDR(sstate) - 4;
5610 data = (char *) pq_getmsgbytes(&buf, datalen);
5611 appendBinaryStringInfo(result, data, datalen);
5612
5613 pq_getmsgend(&buf);
5614
5615 PG_RETURN_POINTER(result);
5616 }
5617
5618 Datum
5619 string_agg_finalfn(PG_FUNCTION_ARGS)
5620 {
5621 StringInfo state;
5622
5623 /* cannot be called directly because of internal-type argument */
5624 Assert(AggCheckCallContext(fcinfo, NULL));
5625
5626 state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
5627
5628 if (state != NULL)
5629 {
5630 /* As per comment in transfn, strip data before the cursor position */
5631 PG_RETURN_TEXT_P(cstring_to_text_with_len(&state->data[state->cursor],
5632 state->len - state->cursor));
5633 }
5634 else
5635 PG_RETURN_NULL();
5636 }
5637
5638 /*
5639 * Prepare cache with fmgr info for the output functions of the datatypes of
5640 * the arguments of a concat-like function, beginning with argument "argidx".
5641 * (Arguments before that will have corresponding slots in the resulting
5642 * FmgrInfo array, but we don't fill those slots.)
5643 */
5644 static FmgrInfo *
5645 build_concat_foutcache(FunctionCallInfo fcinfo, int argidx)
5646 {
5647 FmgrInfo *foutcache;
5648 int i;
5649
5650 /* We keep the info in fn_mcxt so it survives across calls */
5651 foutcache = (FmgrInfo *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
5652 PG_NARGS() * sizeof(FmgrInfo));
5653
5654 for (i = argidx; i < PG_NARGS(); i++)
5655 {
5656 Oid valtype;
5657 Oid typOutput;
5658 bool typIsVarlena;
5659
5660 valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
5661 if (!OidIsValid(valtype))
5662 elog(ERROR, "could not determine data type of concat() input");
5663
5664 getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
5665 fmgr_info_cxt(typOutput, &foutcache[i], fcinfo->flinfo->fn_mcxt);
5666 }
5667
5668 fcinfo->flinfo->fn_extra = foutcache;
5669
5670 return foutcache;
5671 }
5672
5673 /*
5674 * Implementation of both concat() and concat_ws().
5675 *
5676 * sepstr is the separator string to place between values.
5677 * argidx identifies the first argument to concatenate (counting from zero);
5678 * note that this must be constant across any one series of calls.
5679 *
5680 * Returns NULL if result should be NULL, else text value.
5681 */
5682 static text *
5683 concat_internal(const char *sepstr, int argidx,
5684 FunctionCallInfo fcinfo)
5685 {
5686 text *result;
5687 StringInfoData str;
5688 FmgrInfo *foutcache;
5689 bool first_arg = true;
5690 int i;
5691
5692 /*
5693 * concat(VARIADIC some-array) is essentially equivalent to
5694 * array_to_text(), ie concat the array elements with the given separator.
5695 * So we just pass the case off to that code.
5696 */
5697 if (get_fn_expr_variadic(fcinfo->flinfo))
5698 {
5699 ArrayType *arr;
5700
5701 /* Should have just the one argument */
5702 Assert(argidx == PG_NARGS() - 1);
5703
5704 /* concat(VARIADIC NULL) is defined as NULL */
5705 if (PG_ARGISNULL(argidx))
5706 return NULL;
5707
5708 /*
5709 * Non-null argument had better be an array. We assume that any call
5710 * context that could let get_fn_expr_variadic return true will have
5711 * checked that a VARIADIC-labeled parameter actually is an array. So
5712 * it should be okay to just Assert that it's an array rather than
5713 * doing a full-fledged error check.
5714 */
5715 Assert(OidIsValid(get_base_element_type(get_fn_expr_argtype(fcinfo->flinfo, argidx))));
5716
5717 /* OK, safe to fetch the array value */
5718 arr = PG_GETARG_ARRAYTYPE_P(argidx);
5719
5720 /*
5721 * And serialize the array. We tell array_to_text to ignore null
5722 * elements, which matches the behavior of the loop below.
5723 */
5724 return array_to_text_internal(fcinfo, arr, sepstr, NULL);
5725 }
5726
5727 /* Normal case without explicit VARIADIC marker */
5728 initStringInfo(&str);
5729
5730 /* Get output function info, building it if first time through */
5731 foutcache = (FmgrInfo *) fcinfo->flinfo->fn_extra;
5732 if (foutcache == NULL)
5733 foutcache = build_concat_foutcache(fcinfo, argidx);
5734
5735 for (i = argidx; i < PG_NARGS(); i++)
5736 {
5737 if (!PG_ARGISNULL(i))
5738 {
5739 Datum value = PG_GETARG_DATUM(i);
5740
5741 /* add separator if appropriate */
5742 if (first_arg)
5743 first_arg = false;
5744 else
5745 appendStringInfoString(&str, sepstr);
5746
5747 /* call the appropriate type output function, append the result */
5748 appendStringInfoString(&str,
5749 OutputFunctionCall(&foutcache[i], value));
5750 }
5751 }
5752
5753 result = cstring_to_text_with_len(str.data, str.len);
5754 pfree(str.data);
5755
5756 return result;
5757 }
5758
5759 /*
5760 * Concatenate all arguments. NULL arguments are ignored.
5761 */
5762 Datum
5763 text_concat(PG_FUNCTION_ARGS)
5764 {
5765 text *result;
5766
5767 result = concat_internal("", 0, fcinfo);
5768 if (result == NULL)
5769 PG_RETURN_NULL();
5770 PG_RETURN_TEXT_P(result);
5771 }
5772
5773 /*
5774 * Concatenate all but first argument value with separators. The first
5775 * parameter is used as the separator. NULL arguments are ignored.
5776 */
5777 Datum
5778 text_concat_ws(PG_FUNCTION_ARGS)
5779 {
5780 char *sep;
5781 text *result;
5782
5783 /* return NULL when separator is NULL */
5784 if (PG_ARGISNULL(0))
5785 PG_RETURN_NULL();
5786 sep = text_to_cstring(PG_GETARG_TEXT_PP(0));
5787
5788 result = concat_internal(sep, 1, fcinfo);
5789 if (result == NULL)
5790 PG_RETURN_NULL();
5791 PG_RETURN_TEXT_P(result);
5792 }
5793
5794 /*
5795 * Return first n characters in the string. When n is negative,
5796 * return all but last |n| characters.
5797 */
5798 Datum
5799 text_left(PG_FUNCTION_ARGS)
5800 {
5801 int n = PG_GETARG_INT32(1);
5802
5803 if (n < 0)
5804 {
5805 text *str = PG_GETARG_TEXT_PP(0);
5806 const char *p = VARDATA_ANY(str);
5807 int len = VARSIZE_ANY_EXHDR(str);
5808 int rlen;
5809
5810 n = pg_mbstrlen_with_len(p, len) + n;
5811 rlen = pg_mbcharcliplen(p, len, n);
5812 PG_RETURN_TEXT_P(cstring_to_text_with_len(p, rlen));
5813 }
5814 else
5815 PG_RETURN_TEXT_P(text_substring(PG_GETARG_DATUM(0), 1, n, false));
5816 }
5817
5818 /*
5819 * Return last n characters in the string. When n is negative,
5820 * return all but first |n| characters.
5821 */
5822 Datum
5823 text_right(PG_FUNCTION_ARGS)
5824 {
5825 text *str = PG_GETARG_TEXT_PP(0);
5826 const char *p = VARDATA_ANY(str);
5827 int len = VARSIZE_ANY_EXHDR(str);
5828 int n = PG_GETARG_INT32(1);
5829 int off;
5830
5831 if (n < 0)
5832 n = -n;
5833 else
5834 n = pg_mbstrlen_with_len(p, len) - n;
5835 off = pg_mbcharcliplen(p, len, n);
5836
5837 PG_RETURN_TEXT_P(cstring_to_text_with_len(p + off, len - off));
5838 }
5839
5840 /*
5841 * Return reversed string
5842 */
5843 Datum
5844 text_reverse(PG_FUNCTION_ARGS)
5845 {
5846 text *str = PG_GETARG_TEXT_PP(0);
5847 const char *p = VARDATA_ANY(str);
5848 int len = VARSIZE_ANY_EXHDR(str);
5849 const char *endp = p + len;
5850 text *result;
5851 char *dst;
5852
5853 result = palloc(len + VARHDRSZ);
5854 dst = (char *) VARDATA(result) + len;
5855 SET_VARSIZE(result, len + VARHDRSZ);
5856
5857 if (pg_database_encoding_max_length() > 1)
5858 {
5859 /* multibyte version */
5860 while (p < endp)
5861 {
5862 int sz;
5863
5864 sz = pg_mblen_range(p, endp);
5865 dst -= sz;
5866 memcpy(dst, p, sz);
5867 p += sz;
5868 }
5869 }
5870 else
5871 {
5872 /* single byte version */
5873 while (p < endp)
5874 *(--dst) = *p++;
5875 }
5876
5877 PG_RETURN_TEXT_P(result);
5878 }
5879
5880
5881 /*
5882 * Support macros for text_format()
5883 */
5884 #define TEXT_FORMAT_FLAG_MINUS 0x0001 /* is minus flag present? */
5885
5886 #define ADVANCE_PARSE_POINTER(ptr,end_ptr) \
5887 do { \
5888 if (++(ptr) >= (end_ptr)) \
5889 ereport(ERROR, \
5890 (errcode(ERRCODE_INVALID_PARAMETER_VALUE), \
5891 errmsg("unterminated format() type specifier"), \
5892 errhint("For a single \"%%\" use \"%%%%\"."))); \
5893 } while (0)
5894
5895 /*
5896 * Returns a formatted string
5897 */
5898 Datum
5899 text_format(PG_FUNCTION_ARGS)
5900 {
5901 text *fmt;
5902 StringInfoData str;
5903 const char *cp;
5904 const char *start_ptr;
5905 const char *end_ptr;
5906 text *result;
5907 int arg;
5908 bool funcvariadic;
5909 int nargs;
5910 Datum *elements = NULL;
5911 bool *nulls = NULL;
5912 Oid element_type = InvalidOid;
5913 Oid prev_type = InvalidOid;
5914 Oid prev_width_type = InvalidOid;
5915 FmgrInfo typoutputfinfo;
5916 FmgrInfo typoutputinfo_width;
5917
5918 /* When format string is null, immediately return null */
5919 if (PG_ARGISNULL(0))
5920 PG_RETURN_NULL();
5921
5922 /* If argument is marked VARIADIC, expand array into elements */
5923 if (get_fn_expr_variadic(fcinfo->flinfo))
5924 {
5925 ArrayType *arr;
5926 int16 elmlen;
5927 bool elmbyval;
5928 char elmalign;
5929 int nitems;
5930
5931 /* Should have just the one argument */
5932 Assert(PG_NARGS() == 2);
5933
5934 /* If argument is NULL, we treat it as zero-length array */
5935 if (PG_ARGISNULL(1))
5936 nitems = 0;
5937 else
5938 {
5939 /*
5940 * Non-null argument had better be an array. We assume that any
5941 * call context that could let get_fn_expr_variadic return true
5942 * will have checked that a VARIADIC-labeled parameter actually is
5943 * an array. So it should be okay to just Assert that it's an
5944 * array rather than doing a full-fledged error check.
5945 */
5946 Assert(OidIsValid(get_base_element_type(get_fn_expr_argtype(fcinfo->flinfo, 1))));
5947
5948 /* OK, safe to fetch the array value */
5949 arr = PG_GETARG_ARRAYTYPE_P(1);
5950
5951 /* Get info about array element type */
5952 element_type = ARR_ELEMTYPE(arr);
5953 get_typlenbyvalalign(element_type,
5954 &elmlen, &elmbyval, &elmalign);
5955
5956 /* Extract all array elements */
5957 deconstruct_array(arr, element_type, elmlen, elmbyval, elmalign,
5958 &elements, &nulls, &nitems);
5959 }
5960
5961 nargs = nitems + 1;
5962 funcvariadic = true;
5963 }
5964 else
5965 {
5966 /* Non-variadic case, we'll process the arguments individually */
5967 nargs = PG_NARGS();
5968 funcvariadic = false;
5969 }
5970
5971 /* Setup for main loop. */
5972 fmt = PG_GETARG_TEXT_PP(0);
5973 start_ptr = VARDATA_ANY(fmt);
5974 end_ptr = start_ptr + VARSIZE_ANY_EXHDR(fmt);
5975 initStringInfo(&str);
5976 arg = 1; /* next argument position to print */
5977
5978 /* Scan format string, looking for conversion specifiers. */
5979 for (cp = start_ptr; cp < end_ptr; cp++)
5980 {
5981 int argpos;
5982 int widthpos;
5983 int flags;
5984 int width;
5985 Datum value;
5986 bool isNull;
5987 Oid typid;
5988
5989 /*
5990 * If it's not the start of a conversion specifier, just copy it to
5991 * the output buffer.
5992 */
5993 if (*cp != '%')
5994 {
5995 appendStringInfoCharMacro(&str, *cp);
5996 continue;
5997 }
5998
5999 ADVANCE_PARSE_POINTER(cp, end_ptr);
6000
6001 /* Easy case: %% outputs a single % */
6002 if (*cp == '%')
6003 {
6004 appendStringInfoCharMacro(&str, *cp);
6005 continue;
6006 }
6007
6008 /* Parse the optional portions of the format specifier */
6009 cp = text_format_parse_format(cp, end_ptr,
6010 &argpos, &widthpos,
6011 &flags, &width);
6012
6013 /*
6014 * Next we should see the main conversion specifier. Whether or not
6015 * an argument position was present, it's known that at least one
6016 * character remains in the string at this point. Experience suggests
6017 * that it's worth checking that that character is one of the expected
6018 * ones before we try to fetch arguments, so as to produce the least
6019 * confusing response to a mis-formatted specifier.
6020 */
6021 if (strchr("sIL", *cp) == NULL)
6022 ereport(ERROR,
6023 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6024 errmsg("unrecognized format() type specifier \"%.*s\"",
6025 pg_mblen_range(cp, end_ptr), cp),
6026 errhint("For a single \"%%\" use \"%%%%\".")));
6027
6028 /* If indirect width was specified, get its value */
6029 if (widthpos >= 0)
6030 {
6031 /* Collect the specified or next argument position */
6032 if (widthpos > 0)
6033 arg = widthpos;
6034 if (arg >= nargs)
6035 ereport(ERROR,
6036 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6037 errmsg("too few arguments for format()")));
6038
6039 /* Get the value and type of the selected argument */
6040 if (!funcvariadic)
6041 {
6042 value = PG_GETARG_DATUM(arg);
6043 isNull = PG_ARGISNULL(arg);
6044 typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
6045 }
6046 else
6047 {
6048 value = elements[arg - 1];
6049 isNull = nulls[arg - 1];
6050 typid = element_type;
6051 }
6052 if (!OidIsValid(typid))
6053 elog(ERROR, "could not determine data type of format() input");
6054
6055 arg++;
6056
6057 /* We can treat NULL width the same as zero */
6058 if (isNull)
6059 width = 0;
6060 else if (typid == INT4OID)
6061 width = DatumGetInt32(value);
6062 else if (typid == INT2OID)
6063 width = DatumGetInt16(value);
6064 else
6065 {
6066 /* For less-usual datatypes, convert to text then to int */
6067 char *str;
6068
6069 if (typid != prev_width_type)
6070 {
6071 Oid typoutputfunc;
6072 bool typIsVarlena;
6073
6074 getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
6075 fmgr_info(typoutputfunc, &typoutputinfo_width);
6076 prev_width_type = typid;
6077 }
6078
6079 str = OutputFunctionCall(&typoutputinfo_width, value);
6080
6081 /* pg_strtoint32 will complain about bad data or overflow */
6082 width = pg_strtoint32(str);
6083
6084 pfree(str);
6085 }
6086 }
6087
6088 /* Collect the specified or next argument position */
6089 if (argpos > 0)
6090 arg = argpos;
6091 if (arg >= nargs)
6092 ereport(ERROR,
6093 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6094 errmsg("too few arguments for format()")));
6095
6096 /* Get the value and type of the selected argument */
6097 if (!funcvariadic)
6098 {
6099 value = PG_GETARG_DATUM(arg);
6100 isNull = PG_ARGISNULL(arg);
6101 typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
6102 }
6103 else
6104 {
6105 value = elements[arg - 1];
6106 isNull = nulls[arg - 1];
6107 typid = element_type;
6108 }
6109 if (!OidIsValid(typid))
6110 elog(ERROR, "could not determine data type of format() input");
6111
6112 arg++;
6113
6114 /*
6115 * Get the appropriate typOutput function, reusing previous one if
6116 * same type as previous argument. That's particularly useful in the
6117 * variadic-array case, but often saves work even for ordinary calls.
6118 */
6119 if (typid != prev_type)
6120 {
6121 Oid typoutputfunc;
6122 bool typIsVarlena;
6123
6124 getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
6125 fmgr_info(typoutputfunc, &typoutputfinfo);
6126 prev_type = typid;
6127 }
6128
6129 /*
6130 * And now we can format the value.
6131 */
6132 switch (*cp)
6133 {
6134 case 's':
6135 case 'I':
6136 case 'L':
6137 text_format_string_conversion(&str, *cp, &typoutputfinfo,
6138 value, isNull,
6139 flags, width);
6140 break;
6141 default:
6142 /* should not get here, because of previous check */
6143 ereport(ERROR,
6144 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6145 errmsg("unrecognized format() type specifier \"%.*s\"",
6146 pg_mblen_range(cp, end_ptr), cp),
6147 errhint("For a single \"%%\" use \"%%%%\".")));
6148 break;
6149 }
6150 }
6151
6152 /* Don't need deconstruct_array results anymore. */
6153 if (elements != NULL)
6154 pfree(elements);
6155 if (nulls != NULL)
6156 pfree(nulls);
6157
6158 /* Generate results. */
6159 result = cstring_to_text_with_len(str.data, str.len);
6160 pfree(str.data);
6161
6162 PG_RETURN_TEXT_P(result);
6163 }
6164
6165 /*
6166 * Parse contiguous digits as a decimal number.
6167 *
6168 * Returns true if some digits could be parsed.
6169 * The value is returned into *value, and *ptr is advanced to the next
6170 * character to be parsed.
6171 *
6172 * Note parsing invariant: at least one character is known available before
6173 * string end (end_ptr) at entry, and this is still true at exit.
6174 */
6175 static bool
6176 text_format_parse_digits(const char **ptr, const char *end_ptr, int *value)
6177 {
6178 bool found = false;
6179 const char *cp = *ptr;
6180 int val = 0;
6181
6182 while (*cp >= '0' && *cp <= '9')
6183 {
6184 int8 digit = (*cp - '0');
6185
6186 if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
6187 unlikely(pg_add_s32_overflow(val, digit, &val)))
6188 ereport(ERROR,
6189 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6190 errmsg("number is out of range")));
6191 ADVANCE_PARSE_POINTER(cp, end_ptr);
6192 found = true;
6193 }
6194
6195 *ptr = cp;
6196 *value = val;
6197
6198 return found;
6199 }
6200
6201 /*
6202 * Parse a format specifier (generally following the SUS printf spec).
6203 *
6204 * We have already advanced over the initial '%', and we are looking for
6205 * [argpos][flags][width]type (but the type character is not consumed here).
6206 *
6207 * Inputs are start_ptr (the position after '%') and end_ptr (string end + 1).
6208 * Output parameters:
6209 * argpos: argument position for value to be printed. -1 means unspecified.
6210 * widthpos: argument position for width. Zero means the argument position
6211 * was unspecified (ie, take the next arg) and -1 means no width
6212 * argument (width was omitted or specified as a constant).
6213 * flags: bitmask of flags.
6214 * width: directly-specified width value. Zero means the width was omitted
6215 * (note it's not necessary to distinguish this case from an explicit
6216 * zero width value).
6217 *
6218 * The function result is the next character position to be parsed, ie, the
6219 * location where the type character is/should be.
6220 *
6221 * Note parsing invariant: at least one character is known available before
6222 * string end (end_ptr) at entry, and this is still true at exit.
6223 */
6224 static const char *
6225 text_format_parse_format(const char *start_ptr, const char *end_ptr,
6226 int *argpos, int *widthpos,
6227 int *flags, int *width)
6228 {
6229 const char *cp = start_ptr;
6230 int n;
6231
6232 /* set defaults for output parameters */
6233 *argpos = -1;
6234 *widthpos = -1;
6235 *flags = 0;
6236 *width = 0;
6237
6238 /* try to identify first number */
6239 if (text_format_parse_digits(&cp, end_ptr, &n))
6240 {
6241 if (*cp != '$')
6242 {
6243 /* Must be just a width and a type, so we're done */
6244 *width = n;
6245 return cp;
6246 }
6247 /* The number was argument position */
6248 *argpos = n;
6249 /* Explicit 0 for argument index is immediately refused */
6250 if (n == 0)
6251 ereport(ERROR,
6252 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6253 errmsg("format specifies argument 0, but arguments are numbered from 1")));
6254 ADVANCE_PARSE_POINTER(cp, end_ptr);
6255 }
6256
6257 /* Handle flags (only minus is supported now) */
6258 while (*cp == '-')
6259 {
6260 *flags |= TEXT_FORMAT_FLAG_MINUS;
6261 ADVANCE_PARSE_POINTER(cp, end_ptr);
6262 }
6263
6264 if (*cp == '*')
6265 {
6266 /* Handle indirect width */
6267 ADVANCE_PARSE_POINTER(cp, end_ptr);
6268 if (text_format_parse_digits(&cp, end_ptr, &n))
6269 {
6270 /* number in this position must be closed by $ */
6271 if (*cp != '$')
6272 ereport(ERROR,
6273 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6274 errmsg("width argument position must be ended by \"$\"")));
6275 /* The number was width argument position */
6276 *widthpos = n;
6277 /* Explicit 0 for argument index is immediately refused */
6278 if (n == 0)
6279 ereport(ERROR,
6280 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6281 errmsg("format specifies argument 0, but arguments are numbered from 1")));
6282 ADVANCE_PARSE_POINTER(cp, end_ptr);
6283 }
6284 else
6285 *widthpos = 0; /* width's argument position is unspecified */
6286 }
6287 else
6288 {
6289 /* Check for direct width specification */
6290 if (text_format_parse_digits(&cp, end_ptr, &n))
6291 *width = n;
6292 }
6293
6294 /* cp should now be pointing at type character */
6295 return cp;
6296 }
6297
6298 /*
6299 * Format a %s, %I, or %L conversion
6300 */
6301 static void
6302 text_format_string_conversion(StringInfo buf, char conversion,
6303 FmgrInfo *typOutputInfo,
6304 Datum value, bool isNull,
6305 int flags, int width)
6306 {
6307 char *str;
6308
6309 /* Handle NULL arguments before trying to stringify the value. */
6310 if (isNull)
6311 {
6312 if (conversion == 's')
6313 text_format_append_string(buf, "", flags, width);
6314 else if (conversion == 'L')
6315 text_format_append_string(buf, "NULL", flags, width);
6316 else if (conversion == 'I')
6317 ereport(ERROR,
6318 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
6319 errmsg("null values cannot be formatted as an SQL identifier")));
6320 return;
6321 }
6322
6323 /* Stringify. */
6324 str = OutputFunctionCall(typOutputInfo, value);
6325
6326 /* Escape. */
6327 if (conversion == 'I')
6328 {
6329 /* quote_identifier may or may not allocate a new string. */
6330 text_format_append_string(buf, quote_identifier(str), flags, width);
6331 }
6332 else if (conversion == 'L')
6333 {
6334 char *qstr = quote_literal_cstr(str);
6335
6336 text_format_append_string(buf, qstr, flags, width);
6337 /* quote_literal_cstr() always allocates a new string */
6338 pfree(qstr);
6339 }
6340 else
6341 text_format_append_string(buf, str, flags, width);
6342
6343 /* Cleanup. */
6344 pfree(str);
6345 }
6346
6347 /*
6348 * Append str to buf, padding as directed by flags/width
6349 */
6350 static void
6351 text_format_append_string(StringInfo buf, const char *str,
6352 int flags, int width)
6353 {
6354 bool align_to_left = false;
6355 int len;
6356
6357 /* fast path for typical easy case */
6358 if (width == 0)
6359 {
6360 appendStringInfoString(buf, str);
6361 return;
6362 }
6363
6364 if (width < 0)
6365 {
6366 /* Negative width: implicit '-' flag, then take absolute value */
6367 align_to_left = true;
6368 /* -INT_MIN is undefined */
6369 if (width <= INT_MIN)
6370 ereport(ERROR,
6371 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6372 errmsg("number is out of range")));
6373 width = -width;
6374 }
6375 else if (flags & TEXT_FORMAT_FLAG_MINUS)
6376 align_to_left = true;
6377
6378 len = pg_mbstrlen(str);
6379 if (align_to_left)
6380 {
6381 /* left justify */
6382 appendStringInfoString(buf, str);
6383 if (len < width)
6384 appendStringInfoSpaces(buf, width - len);
6385 }
6386 else
6387 {
6388 /* right justify */
6389 if (len < width)
6390 appendStringInfoSpaces(buf, width - len);
6391 appendStringInfoString(buf, str);
6392 }
6393 }
6394
6395 /*
6396 * text_format_nv - nonvariadic wrapper for text_format function.
6397 *
6398 * note: this wrapper is necessary to pass the sanity check in opr_sanity,
6399 * which checks that all built-in functions that share the implementing C
6400 * function take the same number of arguments.
6401 */
6402 Datum
6403 text_format_nv(PG_FUNCTION_ARGS)
6404 {
6405 return text_format(fcinfo);
6406 }
6407
6408 /*
6409 * Helper function for Levenshtein distance functions. Faster than memcmp(),
6410 * for this use case.
6411 */
6412 static inline bool
6413 rest_of_char_same(const char *s1, const char *s2, int len)
6414 {
6415 while (len > 0)
6416 {
6417 len--;
6418 if (s1[len] != s2[len])
6419 return false;
6420 }
6421 return true;
6422 }
6423
6424 /*
6425 * Helper function for checking return value of Levenshtein distance functions.
6426 * We calculate it as an int64, but the distance functions return an int32.
6427 */
6428 static inline int
6429 levenshtein_result(int64 res)
6430 {
6431 if (unlikely(res < PG_INT32_MIN || res > PG_INT32_MAX))
6432 ereport(ERROR,
6433 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
6434 errmsg("levenshtein distance out of range")));
6435 return res;
6436 }
6437
6438 /* Expand each Levenshtein distance variant */
6439 #include "levenshtein.c"
6440 #define LEVENSHTEIN_LESS_EQUAL
6441 #include "levenshtein.c"
6442
6443
6444 /*
6445 * The following *ClosestMatch() functions can be used to determine whether a
6446 * user-provided string resembles any known valid values, which is useful for
6447 * providing hints in log messages, among other things. Use these functions
6448 * like so:
6449 *
6450 * initClosestMatch(&state, source_string, max_distance);
6451 *
6452 * for (int i = 0; i < num_valid_strings; i++)
6453 * updateClosestMatch(&state, valid_strings[i]);
6454 *
6455 * closestMatch = getClosestMatch(&state);
6456 */
6457
6458 /*
6459 * Initialize the given state with the source string and maximum Levenshtein
6460 * distance to consider.
6461 */
6462 void
6463 initClosestMatch(ClosestMatchState *state, const char *source, int max_d)
6464 {
6465 Assert(state);
6466 Assert(max_d >= 0);
6467
6468 state->source = source;
6469 state->min_d = -1;
6470 state->max_d = max_d;
6471 state->match = NULL;
6472 }
6473
6474 /*
6475 * If the candidate string is a closer match than the current one saved (or
6476 * there is no match saved), save it as the closest match.
6477 *
6478 * If the source or candidate string is NULL, empty, or too long, this function
6479 * takes no action. Likewise, if the Levenshtein distance exceeds the maximum
6480 * allowed or more than half the characters are different, no action is taken.
6481 */
6482 void
6483 updateClosestMatch(ClosestMatchState *state, const char *candidate)
6484 {
6485 int dist;
6486
6487 Assert(state);
6488
6489 if (state->source == NULL || state->source[0] == '\0' ||
6490 candidate == NULL || candidate[0] == '\0')
6491 return;
6492
6493 /*
6494 * To avoid ERROR-ing, we check the lengths here instead of setting
6495 * 'trusted' to false in the call to varstr_levenshtein_less_equal().
6496 */
6497 if (strlen(state->source) > MAX_LEVENSHTEIN_STRLEN ||
6498 strlen(candidate) > MAX_LEVENSHTEIN_STRLEN)
6499 return;
6500
6501 dist = varstr_levenshtein_less_equal(state->source, strlen(state->source),
6502 candidate, strlen(candidate), 1, 1, 1,
6503 state->max_d, true);
6504 if (dist <= state->max_d &&
6505 dist <= strlen(state->source) / 2 &&
6506 (state->min_d == -1 || dist < state->min_d))
6507 {
6508 state->min_d = dist;
6509 state->match = candidate;
6510 }
6511 }
6512
6513 /*
6514 * Return the closest match. If no suitable candidates were provided via
6515 * updateClosestMatch(), return NULL.
6516 */
6517 const char *
6518 getClosestMatch(ClosestMatchState *state)
6519 {
6520 Assert(state);
6521
6522 return state->match;
6523 }
6524
6525
6526 /*
6527 * Unicode support
6528 */
6529
6530 static UnicodeNormalizationForm
6531 unicode_norm_form_from_string(const char *formstr)
6532 {
6533 UnicodeNormalizationForm form = -1;
6534
6535 /*
6536 * Might as well check this while we're here.
6537 */
6538 if (GetDatabaseEncoding() != PG_UTF8)
6539 ereport(ERROR,
6540 (errcode(ERRCODE_SYNTAX_ERROR),
6541 errmsg("Unicode normalization can only be performed if server encoding is UTF8")));
6542
6543 if (pg_strcasecmp(formstr, "NFC") == 0)
6544 form = UNICODE_NFC;
6545 else if (pg_strcasecmp(formstr, "NFD") == 0)
6546 form = UNICODE_NFD;
6547 else if (pg_strcasecmp(formstr, "NFKC") == 0)
6548 form = UNICODE_NFKC;
6549 else if (pg_strcasecmp(formstr, "NFKD") == 0)
6550 form = UNICODE_NFKD;
6551 else
6552 ereport(ERROR,
6553 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6554 errmsg("invalid normalization form: %s", formstr)));
6555
6556 return form;
6557 }
6558
6559 /*
6560 * Returns version of Unicode used by Postgres in "major.minor" format (the
6561 * same format as the Unicode version reported by ICU). The third component
6562 * ("update version") never involves additions to the character repertoire and
6563 * is unimportant for most purposes.
6564 *
6565 * See: https://unicode.org/versions/
6566 */
6567 Datum
6568 unicode_version(PG_FUNCTION_ARGS)
6569 {
6570 PG_RETURN_TEXT_P(cstring_to_text(PG_UNICODE_VERSION));
6571 }
6572
6573 /*
6574 * Returns version of Unicode used by ICU, if enabled; otherwise NULL.
6575 */
6576 Datum
6577 icu_unicode_version(PG_FUNCTION_ARGS)
6578 {
6579 #ifdef USE_ICU
6580 PG_RETURN_TEXT_P(cstring_to_text(U_UNICODE_VERSION));
6581 #else
6582 PG_RETURN_NULL();
6583 #endif
6584 }
6585
6586 /*
6587 * Check whether the string contains only assigned Unicode code
6588 * points. Requires that the database encoding is UTF-8.
6589 */
6590 Datum
6591 unicode_assigned(PG_FUNCTION_ARGS)
6592 {
6593 text *input = PG_GETARG_TEXT_PP(0);
6594 unsigned char *p;
6595 int size;
6596
6597 if (GetDatabaseEncoding() != PG_UTF8)
6598 ereport(ERROR,
6599 (errmsg("Unicode categorization can only be performed if server encoding is UTF8")));
6600
6601 /* convert to pg_wchar */
6602 size = pg_mbstrlen_with_len(VARDATA_ANY(input), VARSIZE_ANY_EXHDR(input));
6603 p = (unsigned char *) VARDATA_ANY(input);
6604 for (int i = 0; i < size; i++)
6605 {
6606 pg_wchar uchar = utf8_to_unicode(p);
6607 int category = unicode_category(uchar);
6608
6609 if (category == PG_U_UNASSIGNED)
6610 PG_RETURN_BOOL(false);
6611
6612 p += pg_utf_mblen(p);
6613 }
6614
6615 PG_RETURN_BOOL(true);
6616 }
6617
6618 Datum
6619 unicode_normalize_func(PG_FUNCTION_ARGS)
6620 {
6621 text *input = PG_GETARG_TEXT_PP(0);
6622 char *formstr = text_to_cstring(PG_GETARG_TEXT_PP(1));
6623 UnicodeNormalizationForm form;
6624 size_t size;
6625 pg_wchar *input_chars;
6626 pg_wchar *output_chars;
6627 unsigned char *p;
6628 text *result;
6629 size_t i;
6630
6631 form = unicode_norm_form_from_string(formstr);
6632
6633 /* convert to pg_wchar */
6634 size = pg_mbstrlen_with_len(VARDATA_ANY(input), VARSIZE_ANY_EXHDR(input));
6635 input_chars = palloc_array(pg_wchar, size + 1);
6636 p = (unsigned char *) VARDATA_ANY(input);
6637 for (i = 0; i < size; i++)
6638 {
6639 input_chars[i] = utf8_to_unicode(p);
6640 p += pg_utf_mblen(p);
6641 }
6642 input_chars[i] = (pg_wchar) '\0';
6643 Assert((char *) p == VARDATA_ANY(input) + VARSIZE_ANY_EXHDR(input));
6644
6645 /* action */
6646 output_chars = unicode_normalize(form, input_chars);
6647
6648 /* convert back to UTF-8 string */
6649 size = 0;
6650 for (pg_wchar *wp = output_chars; *wp; wp++)
6651 {
6652 unsigned char buf[4];
6653
6654 unicode_to_utf8(*wp, buf);
6655 size += pg_utf_mblen(buf);
6656 }
6657
6658 result = palloc(size + VARHDRSZ);
6659 SET_VARSIZE(result, size + VARHDRSZ);
6660
6661 p = (unsigned char *) VARDATA_ANY(result);
6662 for (pg_wchar *wp = output_chars; *wp; wp++)
6663 {
6664 unicode_to_utf8(*wp, p);
6665 p += pg_utf_mblen(p);
6666 }
6667 Assert((char *) p == (char *) result + size + VARHDRSZ);
6668
6669 PG_RETURN_TEXT_P(result);
6670 }
6671
6672 /*
6673 * Check whether the string is in the specified Unicode normalization form.
6674 *
6675 * This is done by converting the string to the specified normal form and then
6676 * comparing that to the original string. To speed that up, we also apply the
6677 * "quick check" algorithm specified in UAX #15, which can give a yes or no
6678 * answer for many strings by just scanning the string once.
6679 *
6680 * This function should generally be optimized for the case where the string
6681 * is in fact normalized. In that case, we'll end up looking at the entire
6682 * string, so it's probably not worth doing any incremental conversion etc.
6683 */
6684 Datum
6685 unicode_is_normalized(PG_FUNCTION_ARGS)
6686 {
6687 text *input = PG_GETARG_TEXT_PP(0);
6688 char *formstr = text_to_cstring(PG_GETARG_TEXT_PP(1));
6689 UnicodeNormalizationForm form;
6690 size_t size;
6691 pg_wchar *input_chars;
6692 pg_wchar *output_chars;
6693 unsigned char *p;
6694 size_t i;
6695 UnicodeNormalizationQC quickcheck;
6696 size_t output_size;
6697 bool result;
6698
6699 form = unicode_norm_form_from_string(formstr);
6700
6701 /* convert to pg_wchar */
6702 size = pg_mbstrlen_with_len(VARDATA_ANY(input), VARSIZE_ANY_EXHDR(input));
6703 input_chars = palloc_array(pg_wchar, size + 1);
6704 p = (unsigned char *) VARDATA_ANY(input);
6705 for (i = 0; i < size; i++)
6706 {
6707 input_chars[i] = utf8_to_unicode(p);
6708 p += pg_utf_mblen(p);
6709 }
6710 input_chars[i] = (pg_wchar) '\0';
6711 Assert((char *) p == VARDATA_ANY(input) + VARSIZE_ANY_EXHDR(input));
6712
6713 /* quick check (see UAX #15) */
6714 quickcheck = unicode_is_normalized_quickcheck(form, input_chars);
6715 if (quickcheck == UNICODE_NORM_QC_YES)
6716 PG_RETURN_BOOL(true);
6717 else if (quickcheck == UNICODE_NORM_QC_NO)
6718 PG_RETURN_BOOL(false);
6719
6720 /* normalize and compare with original */
6721 output_chars = unicode_normalize(form, input_chars);
6722
6723 output_size = 0;
6724 for (pg_wchar *wp = output_chars; *wp; wp++)
6725 output_size++;
6726
6727 result = (size == output_size) &&
6728 (memcmp(input_chars, output_chars, size * sizeof(pg_wchar)) == 0);
6729
6730 PG_RETURN_BOOL(result);
6731 }
6732
6733 /*
6734 * Check if first n chars are hexadecimal digits
6735 */
6736 static bool
6737 isxdigits_n(const char *instr, size_t n)
6738 {
6739 for (size_t i = 0; i < n; i++)
6740 if (!isxdigit((unsigned char) instr[i]))
6741 return false;
6742
6743 return true;
6744 }
6745
6746 static unsigned int
6747 hexval(unsigned char c)
6748 {
6749 if (c >= '0' && c <= '9')
6750 return c - '0';
6751 if (c >= 'a' && c <= 'f')
6752 return c - 'a' + 0xA;
6753 if (c >= 'A' && c <= 'F')
6754 return c - 'A' + 0xA;
6755 elog(ERROR, "invalid hexadecimal digit");
6756 return 0; /* not reached */
6757 }
6758
6759 /*
6760 * Translate string with hexadecimal digits to number
6761 */
6762 static unsigned int
6763 hexval_n(const char *instr, size_t n)
6764 {
6765 unsigned int result = 0;
6766
6767 for (size_t i = 0; i < n; i++)
6768 result += hexval(instr[i]) << (4 * (n - i - 1));
6769
6770 return result;
6771 }
6772
6773 /*
6774 * Replaces Unicode escape sequences by Unicode characters
6775 */
6776 Datum
6777 unistr(PG_FUNCTION_ARGS)
6778 {
6779 text *input_text = PG_GETARG_TEXT_PP(0);
6780 char *instr;
6781 int len;
6782 StringInfoData str;
6783 text *result;
6784 pg_wchar pair_first = 0;
6785 char cbuf[MAX_UNICODE_EQUIVALENT_STRING + 1];
6786
6787 instr = VARDATA_ANY(input_text);
6788 len = VARSIZE_ANY_EXHDR(input_text);
6789
6790 initStringInfo(&str);
6791
6792 while (len > 0)
6793 {
6794 if (instr[0] == '\\')
6795 {
6796 if (len >= 2 &&
6797 instr[1] == '\\')
6798 {
6799 if (pair_first)
6800 goto invalid_pair;
6801 appendStringInfoChar(&str, '\\');
6802 instr += 2;
6803 len -= 2;
6804 }
6805 else if ((len >= 5 && isxdigits_n(instr + 1, 4)) ||
6806 (len >= 6 && instr[1] == 'u' && isxdigits_n(instr + 2, 4)))
6807 {
6808 pg_wchar unicode;
6809 int offset = instr[1] == 'u' ? 2 : 1;
6810
6811 unicode = hexval_n(instr + offset, 4);
6812
6813 if (!is_valid_unicode_codepoint(unicode))
6814 ereport(ERROR,
6815 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6816 errmsg("invalid Unicode code point: %04X", unicode));
6817
6818 if (pair_first)
6819 {
6820 if (is_utf16_surrogate_second(unicode))
6821 {
6822 unicode = surrogate_pair_to_codepoint(pair_first, unicode);
6823 pair_first = 0;
6824 }
6825 else
6826 goto invalid_pair;
6827 }
6828 else if (is_utf16_surrogate_second(unicode))
6829 goto invalid_pair;
6830
6831 if (is_utf16_surrogate_first(unicode))
6832 pair_first = unicode;
6833 else
6834 {
6835 pg_unicode_to_server(unicode, (unsigned char *) cbuf);
6836 appendStringInfoString(&str, cbuf);
6837 }
6838
6839 instr += 4 + offset;
6840 len -= 4 + offset;
6841 }
6842 else if (len >= 8 && instr[1] == '+' && isxdigits_n(instr + 2, 6))
6843 {
6844 pg_wchar unicode;
6845
6846 unicode = hexval_n(instr + 2, 6);
6847
6848 if (!is_valid_unicode_codepoint(unicode))
6849 ereport(ERROR,
6850 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6851 errmsg("invalid Unicode code point: %04X", unicode));
6852
6853 if (pair_first)
6854 {
6855 if (is_utf16_surrogate_second(unicode))
6856 {
6857 unicode = surrogate_pair_to_codepoint(pair_first, unicode);
6858 pair_first = 0;
6859 }
6860 else
6861 goto invalid_pair;
6862 }
6863 else if (is_utf16_surrogate_second(unicode))
6864 goto invalid_pair;
6865
6866 if (is_utf16_surrogate_first(unicode))
6867 pair_first = unicode;
6868 else
6869 {
6870 pg_unicode_to_server(unicode, (unsigned char *) cbuf);
6871 appendStringInfoString(&str, cbuf);
6872 }
6873
6874 instr += 8;
6875 len -= 8;
6876 }
6877 else if (len >= 10 && instr[1] == 'U' && isxdigits_n(instr + 2, 8))
6878 {
6879 pg_wchar unicode;
6880
6881 unicode = hexval_n(instr + 2, 8);
6882
6883 if (!is_valid_unicode_codepoint(unicode))
6884 ereport(ERROR,
6885 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6886 errmsg("invalid Unicode code point: %04X", unicode));
6887
6888 if (pair_first)
6889 {
6890 if (is_utf16_surrogate_second(unicode))
6891 {
6892 unicode = surrogate_pair_to_codepoint(pair_first, unicode);
6893 pair_first = 0;
6894 }
6895 else
6896 goto invalid_pair;
6897 }
6898 else if (is_utf16_surrogate_second(unicode))
6899 goto invalid_pair;
6900
6901 if (is_utf16_surrogate_first(unicode))
6902 pair_first = unicode;
6903 else
6904 {
6905 pg_unicode_to_server(unicode, (unsigned char *) cbuf);
6906 appendStringInfoString(&str, cbuf);
6907 }
6908
6909 instr += 10;
6910 len -= 10;
6911 }
6912 else
6913 ereport(ERROR,
6914 (errcode(ERRCODE_SYNTAX_ERROR),
6915 errmsg("invalid Unicode escape"),
6916 errhint("Unicode escapes must be \\XXXX, \\+XXXXXX, \\uXXXX, or \\UXXXXXXXX.")));
6917 }
6918 else
6919 {
6920 if (pair_first)
6921 goto invalid_pair;
6922
6923 appendStringInfoChar(&str, *instr++);
6924 len--;
6925 }
6926 }
6927
6928 /* unfinished surrogate pair? */
6929 if (pair_first)
6930 goto invalid_pair;
6931
6932 result = cstring_to_text_with_len(str.data, str.len);
6933 pfree(str.data);
6934
6935 PG_RETURN_TEXT_P(result);
6936
6937 invalid_pair:
6938 ereport(ERROR,
6939 (errcode(ERRCODE_SYNTAX_ERROR),
6940 errmsg("invalid Unicode surrogate pair")));
6941 PG_RETURN_NULL(); /* keep compiler quiet */
6942 }