1 /*-------------------------------------------------------------------------
2  *
3  * varlena.c
4  *	  Functions for the variable-length built-in types.
5  *
6  * Portions Copyright (c) 1996-2018, 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/hash.h"
21 #include "access/tuptoaster.h"
22 #include "catalog/pg_collation.h"
23 #include "catalog/pg_type.h"
24 #include "common/int.h"
25 #include "lib/hyperloglog.h"
26 #include "libpq/pqformat.h"
27 #include "miscadmin.h"
28 #include "parser/scansup.h"
29 #include "port/pg_bswap.h"
30 #include "regex/regex.h"
31 #include "utils/builtins.h"
32 #include "utils/bytea.h"
33 #include "utils/lsyscache.h"
34 #include "utils/memutils.h"
35 #include "utils/pg_locale.h"
36 #include "utils/sortsupport.h"
37 #include "utils/varlena.h"
38 
39 
40 /* GUC variable */
41 int			bytea_output = BYTEA_OUTPUT_HEX;
42 
43 typedef struct varlena unknown;
44 typedef struct varlena VarString;
45 
46 typedef struct
47 {
48 	bool		use_wchar;		/* T if multibyte encoding */
49 	char	   *str1;			/* use these if not use_wchar */
50 	char	   *str2;			/* note: these point to original texts */
51 	pg_wchar   *wstr1;			/* use these if use_wchar */
52 	pg_wchar   *wstr2;			/* note: these are palloc'd */
53 	int			len1;			/* string lengths in logical characters */
54 	int			len2;
55 	/* Skip table for Boyer-Moore-Horspool search algorithm: */
56 	int			skiptablemask;	/* mask for ANDing with skiptable subscripts */
57 	int			skiptable[256]; /* skip distance for given mismatched char */
58 } TextPositionState;
59 
60 typedef struct
61 {
62 	char	   *buf1;			/* 1st string, or abbreviation original string
63 								 * buf */
64 	char	   *buf2;			/* 2nd string, or abbreviation strxfrm() buf */
65 	int			buflen1;
66 	int			buflen2;
67 	int			last_len1;		/* Length of last buf1 string/strxfrm() input */
68 	int			last_len2;		/* Length of last buf2 string/strxfrm() blob */
69 	int			last_returned;	/* Last comparison result (cache) */
70 	bool		cache_blob;		/* Does buf2 contain strxfrm() blob, etc? */
71 	bool		collate_c;
72 	bool		bpchar;			/* Sorting bpchar, not varchar/text/bytea? */
73 	hyperLogLogState abbr_card; /* Abbreviated key cardinality state */
74 	hyperLogLogState full_card; /* Full key cardinality state */
75 	double		prop_card;		/* Required cardinality proportion */
76 	pg_locale_t locale;
77 } VarStringSortSupport;
78 
79 /*
80  * This should be large enough that most strings will fit, but small enough
81  * that we feel comfortable putting it on the stack
82  */
83 #define TEXTBUFLEN		1024
84 
85 #define DatumGetUnknownP(X)			((unknown *) PG_DETOAST_DATUM(X))
86 #define DatumGetUnknownPCopy(X)		((unknown *) PG_DETOAST_DATUM_COPY(X))
87 #define PG_GETARG_UNKNOWN_P(n)		DatumGetUnknownP(PG_GETARG_DATUM(n))
88 #define PG_GETARG_UNKNOWN_P_COPY(n) DatumGetUnknownPCopy(PG_GETARG_DATUM(n))
89 #define PG_RETURN_UNKNOWN_P(x)		PG_RETURN_POINTER(x)
90 
91 #define DatumGetVarStringP(X)		((VarString *) PG_DETOAST_DATUM(X))
92 #define DatumGetVarStringPP(X)		((VarString *) PG_DETOAST_DATUM_PACKED(X))
93 
94 static int	varstrfastcmp_c(Datum x, Datum y, SortSupport ssup);
95 static int	bpcharfastcmp_c(Datum x, Datum y, SortSupport ssup);
96 static int	varstrfastcmp_locale(Datum x, Datum y, SortSupport ssup);
97 static int	varstrcmp_abbrev(Datum x, Datum y, SortSupport ssup);
98 static Datum varstr_abbrev_convert(Datum original, SortSupport ssup);
99 static bool varstr_abbrev_abort(int memtupcount, SortSupport ssup);
100 static int32 text_length(Datum str);
101 static text *text_catenate(text *t1, text *t2);
102 static text *text_substring(Datum str,
103 			   int32 start,
104 			   int32 length,
105 			   bool length_not_specified);
106 static text *text_overlay(text *t1, text *t2, int sp, int sl);
107 static int	text_position(text *t1, text *t2);
108 static void text_position_setup(text *t1, text *t2, TextPositionState *state);
109 static int	text_position_next(int start_pos, TextPositionState *state);
110 static void text_position_cleanup(TextPositionState *state);
111 static int	text_cmp(text *arg1, text *arg2, Oid collid);
112 static bytea *bytea_catenate(bytea *t1, bytea *t2);
113 static bytea *bytea_substring(Datum str,
114 				int S,
115 				int L,
116 				bool length_not_specified);
117 static bytea *bytea_overlay(bytea *t1, bytea *t2, int sp, int sl);
118 static void appendStringInfoText(StringInfo str, const text *t);
119 static Datum text_to_array_internal(PG_FUNCTION_ARGS);
120 static text *array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v,
121 					   const char *fldsep, const char *null_string);
122 static StringInfo makeStringAggState(FunctionCallInfo fcinfo);
123 static bool text_format_parse_digits(const char **ptr, const char *end_ptr,
124 						 int *value);
125 static const char *text_format_parse_format(const char *start_ptr,
126 						 const char *end_ptr,
127 						 int *argpos, int *widthpos,
128 						 int *flags, int *width);
129 static void text_format_string_conversion(StringInfo buf, char conversion,
130 							  FmgrInfo *typOutputInfo,
131 							  Datum value, bool isNull,
132 							  int flags, int width);
133 static void text_format_append_string(StringInfo buf, const char *str,
134 						  int flags, int width);
135 
136 
137 /*****************************************************************************
138  *	 CONVERSION ROUTINES EXPORTED FOR USE BY C CODE							 *
139  *****************************************************************************/
140 
141 /*
142  * cstring_to_text
143  *
144  * Create a text value from a null-terminated C string.
145  *
146  * The new text value is freshly palloc'd with a full-size VARHDR.
147  */
148 text *
cstring_to_text(const char * s)149 cstring_to_text(const char *s)
150 {
151 	return cstring_to_text_with_len(s, strlen(s));
152 }
153 
154 /*
155  * cstring_to_text_with_len
156  *
157  * Same as cstring_to_text except the caller specifies the string length;
158  * the string need not be null_terminated.
159  */
160 text *
cstring_to_text_with_len(const char * s,int len)161 cstring_to_text_with_len(const char *s, int len)
162 {
163 	text	   *result = (text *) palloc(len + VARHDRSZ);
164 
165 	SET_VARSIZE(result, len + VARHDRSZ);
166 	memcpy(VARDATA(result), s, len);
167 
168 	return result;
169 }
170 
171 /*
172  * text_to_cstring
173  *
174  * Create a palloc'd, null-terminated C string from a text value.
175  *
176  * We support being passed a compressed or toasted text value.
177  * This is a bit bogus since such values shouldn't really be referred to as
178  * "text *", but it seems useful for robustness.  If we didn't handle that
179  * case here, we'd need another routine that did, anyway.
180  */
181 char *
text_to_cstring(const text * t)182 text_to_cstring(const text *t)
183 {
184 	/* must cast away the const, unfortunately */
185 	text	   *tunpacked = pg_detoast_datum_packed((struct varlena *) t);
186 	int			len = VARSIZE_ANY_EXHDR(tunpacked);
187 	char	   *result;
188 
189 	result = (char *) palloc(len + 1);
190 	memcpy(result, VARDATA_ANY(tunpacked), len);
191 	result[len] = '\0';
192 
193 	if (tunpacked != t)
194 		pfree(tunpacked);
195 
196 	return result;
197 }
198 
199 /*
200  * text_to_cstring_buffer
201  *
202  * Copy a text value into a caller-supplied buffer of size dst_len.
203  *
204  * The text string is truncated if necessary to fit.  The result is
205  * guaranteed null-terminated (unless dst_len == 0).
206  *
207  * We support being passed a compressed or toasted text value.
208  * This is a bit bogus since such values shouldn't really be referred to as
209  * "text *", but it seems useful for robustness.  If we didn't handle that
210  * case here, we'd need another routine that did, anyway.
211  */
212 void
text_to_cstring_buffer(const text * src,char * dst,size_t dst_len)213 text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
214 {
215 	/* must cast away the const, unfortunately */
216 	text	   *srcunpacked = pg_detoast_datum_packed((struct varlena *) src);
217 	size_t		src_len = VARSIZE_ANY_EXHDR(srcunpacked);
218 
219 	if (dst_len > 0)
220 	{
221 		dst_len--;
222 		if (dst_len >= src_len)
223 			dst_len = src_len;
224 		else					/* ensure truncation is encoding-safe */
225 			dst_len = pg_mbcliplen(VARDATA_ANY(srcunpacked), src_len, dst_len);
226 		memcpy(dst, VARDATA_ANY(srcunpacked), dst_len);
227 		dst[dst_len] = '\0';
228 	}
229 
230 	if (srcunpacked != src)
231 		pfree(srcunpacked);
232 }
233 
234 
235 /*****************************************************************************
236  *	 USER I/O ROUTINES														 *
237  *****************************************************************************/
238 
239 
240 #define VAL(CH)			((CH) - '0')
241 #define DIG(VAL)		((VAL) + '0')
242 
243 /*
244  *		byteain			- converts from printable representation of byte array
245  *
246  *		Non-printable characters must be passed as '\nnn' (octal) and are
247  *		converted to internal form.  '\' must be passed as '\\'.
248  *		ereport(ERROR, ...) if bad form.
249  *
250  *		BUGS:
251  *				The input is scanned twice.
252  *				The error checking of input is minimal.
253  */
254 Datum
byteain(PG_FUNCTION_ARGS)255 byteain(PG_FUNCTION_ARGS)
256 {
257 	char	   *inputText = PG_GETARG_CSTRING(0);
258 	char	   *tp;
259 	char	   *rp;
260 	int			bc;
261 	bytea	   *result;
262 
263 	/* Recognize hex input */
264 	if (inputText[0] == '\\' && inputText[1] == 'x')
265 	{
266 		size_t		len = strlen(inputText);
267 
268 		bc = (len - 2) / 2 + VARHDRSZ;	/* maximum possible length */
269 		result = palloc(bc);
270 		bc = hex_decode(inputText + 2, len - 2, VARDATA(result));
271 		SET_VARSIZE(result, bc + VARHDRSZ); /* actual length */
272 
273 		PG_RETURN_BYTEA_P(result);
274 	}
275 
276 	/* Else, it's the traditional escaped style */
277 	for (bc = 0, tp = inputText; *tp != '\0'; bc++)
278 	{
279 		if (tp[0] != '\\')
280 			tp++;
281 		else if ((tp[0] == '\\') &&
282 				 (tp[1] >= '0' && tp[1] <= '3') &&
283 				 (tp[2] >= '0' && tp[2] <= '7') &&
284 				 (tp[3] >= '0' && tp[3] <= '7'))
285 			tp += 4;
286 		else if ((tp[0] == '\\') &&
287 				 (tp[1] == '\\'))
288 			tp += 2;
289 		else
290 		{
291 			/*
292 			 * one backslash, not followed by another or ### valid octal
293 			 */
294 			ereport(ERROR,
295 					(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
296 					 errmsg("invalid input syntax for type %s", "bytea")));
297 		}
298 	}
299 
300 	bc += VARHDRSZ;
301 
302 	result = (bytea *) palloc(bc);
303 	SET_VARSIZE(result, bc);
304 
305 	tp = inputText;
306 	rp = VARDATA(result);
307 	while (*tp != '\0')
308 	{
309 		if (tp[0] != '\\')
310 			*rp++ = *tp++;
311 		else if ((tp[0] == '\\') &&
312 				 (tp[1] >= '0' && tp[1] <= '3') &&
313 				 (tp[2] >= '0' && tp[2] <= '7') &&
314 				 (tp[3] >= '0' && tp[3] <= '7'))
315 		{
316 			bc = VAL(tp[1]);
317 			bc <<= 3;
318 			bc += VAL(tp[2]);
319 			bc <<= 3;
320 			*rp++ = bc + VAL(tp[3]);
321 
322 			tp += 4;
323 		}
324 		else if ((tp[0] == '\\') &&
325 				 (tp[1] == '\\'))
326 		{
327 			*rp++ = '\\';
328 			tp += 2;
329 		}
330 		else
331 		{
332 			/*
333 			 * We should never get here. The first pass should not allow it.
334 			 */
335 			ereport(ERROR,
336 					(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
337 					 errmsg("invalid input syntax for type %s", "bytea")));
338 		}
339 	}
340 
341 	PG_RETURN_BYTEA_P(result);
342 }
343 
344 /*
345  *		byteaout		- converts to printable representation of byte array
346  *
347  *		In the traditional escaped format, non-printable characters are
348  *		printed as '\nnn' (octal) and '\' as '\\'.
349  */
350 Datum
byteaout(PG_FUNCTION_ARGS)351 byteaout(PG_FUNCTION_ARGS)
352 {
353 	bytea	   *vlena = PG_GETARG_BYTEA_PP(0);
354 	char	   *result;
355 	char	   *rp;
356 
357 	if (bytea_output == BYTEA_OUTPUT_HEX)
358 	{
359 		/* Print hex format */
360 		rp = result = palloc(VARSIZE_ANY_EXHDR(vlena) * 2 + 2 + 1);
361 		*rp++ = '\\';
362 		*rp++ = 'x';
363 		rp += hex_encode(VARDATA_ANY(vlena), VARSIZE_ANY_EXHDR(vlena), rp);
364 	}
365 	else if (bytea_output == BYTEA_OUTPUT_ESCAPE)
366 	{
367 		/* Print traditional escaped format */
368 		char	   *vp;
369 		int			len;
370 		int			i;
371 
372 		len = 1;				/* empty string has 1 char */
373 		vp = VARDATA_ANY(vlena);
374 		for (i = VARSIZE_ANY_EXHDR(vlena); i != 0; i--, vp++)
375 		{
376 			if (*vp == '\\')
377 				len += 2;
378 			else if ((unsigned char) *vp < 0x20 || (unsigned char) *vp > 0x7e)
379 				len += 4;
380 			else
381 				len++;
382 		}
383 		rp = result = (char *) palloc(len);
384 		vp = VARDATA_ANY(vlena);
385 		for (i = VARSIZE_ANY_EXHDR(vlena); i != 0; i--, vp++)
386 		{
387 			if (*vp == '\\')
388 			{
389 				*rp++ = '\\';
390 				*rp++ = '\\';
391 			}
392 			else if ((unsigned char) *vp < 0x20 || (unsigned char) *vp > 0x7e)
393 			{
394 				int			val;	/* holds unprintable chars */
395 
396 				val = *vp;
397 				rp[0] = '\\';
398 				rp[3] = DIG(val & 07);
399 				val >>= 3;
400 				rp[2] = DIG(val & 07);
401 				val >>= 3;
402 				rp[1] = DIG(val & 03);
403 				rp += 4;
404 			}
405 			else
406 				*rp++ = *vp;
407 		}
408 	}
409 	else
410 	{
411 		elog(ERROR, "unrecognized bytea_output setting: %d",
412 			 bytea_output);
413 		rp = result = NULL;		/* keep compiler quiet */
414 	}
415 	*rp = '\0';
416 	PG_RETURN_CSTRING(result);
417 }
418 
419 /*
420  *		bytearecv			- converts external binary format to bytea
421  */
422 Datum
bytearecv(PG_FUNCTION_ARGS)423 bytearecv(PG_FUNCTION_ARGS)
424 {
425 	StringInfo	buf = (StringInfo) PG_GETARG_POINTER(0);
426 	bytea	   *result;
427 	int			nbytes;
428 
429 	nbytes = buf->len - buf->cursor;
430 	result = (bytea *) palloc(nbytes + VARHDRSZ);
431 	SET_VARSIZE(result, nbytes + VARHDRSZ);
432 	pq_copymsgbytes(buf, VARDATA(result), nbytes);
433 	PG_RETURN_BYTEA_P(result);
434 }
435 
436 /*
437  *		byteasend			- converts bytea to binary format
438  *
439  * This is a special case: just copy the input...
440  */
441 Datum
byteasend(PG_FUNCTION_ARGS)442 byteasend(PG_FUNCTION_ARGS)
443 {
444 	bytea	   *vlena = PG_GETARG_BYTEA_P_COPY(0);
445 
446 	PG_RETURN_BYTEA_P(vlena);
447 }
448 
449 Datum
bytea_string_agg_transfn(PG_FUNCTION_ARGS)450 bytea_string_agg_transfn(PG_FUNCTION_ARGS)
451 {
452 	StringInfo	state;
453 
454 	state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
455 
456 	/* Append the value unless null. */
457 	if (!PG_ARGISNULL(1))
458 	{
459 		bytea	   *value = PG_GETARG_BYTEA_PP(1);
460 
461 		/* On the first time through, we ignore the delimiter. */
462 		if (state == NULL)
463 			state = makeStringAggState(fcinfo);
464 		else if (!PG_ARGISNULL(2))
465 		{
466 			bytea	   *delim = PG_GETARG_BYTEA_PP(2);
467 
468 			appendBinaryStringInfo(state, VARDATA_ANY(delim), VARSIZE_ANY_EXHDR(delim));
469 		}
470 
471 		appendBinaryStringInfo(state, VARDATA_ANY(value), VARSIZE_ANY_EXHDR(value));
472 	}
473 
474 	/*
475 	 * The transition type for string_agg() is declared to be "internal",
476 	 * which is a pass-by-value type the same size as a pointer.
477 	 */
478 	PG_RETURN_POINTER(state);
479 }
480 
481 Datum
bytea_string_agg_finalfn(PG_FUNCTION_ARGS)482 bytea_string_agg_finalfn(PG_FUNCTION_ARGS)
483 {
484 	StringInfo	state;
485 
486 	/* cannot be called directly because of internal-type argument */
487 	Assert(AggCheckCallContext(fcinfo, NULL));
488 
489 	state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
490 
491 	if (state != NULL)
492 	{
493 		bytea	   *result;
494 
495 		result = (bytea *) palloc(state->len + VARHDRSZ);
496 		SET_VARSIZE(result, state->len + VARHDRSZ);
497 		memcpy(VARDATA(result), state->data, state->len);
498 		PG_RETURN_BYTEA_P(result);
499 	}
500 	else
501 		PG_RETURN_NULL();
502 }
503 
504 /*
505  *		textin			- converts "..." to internal representation
506  */
507 Datum
textin(PG_FUNCTION_ARGS)508 textin(PG_FUNCTION_ARGS)
509 {
510 	char	   *inputText = PG_GETARG_CSTRING(0);
511 
512 	PG_RETURN_TEXT_P(cstring_to_text(inputText));
513 }
514 
515 /*
516  *		textout			- converts internal representation to "..."
517  */
518 Datum
textout(PG_FUNCTION_ARGS)519 textout(PG_FUNCTION_ARGS)
520 {
521 	Datum		txt = PG_GETARG_DATUM(0);
522 
523 	PG_RETURN_CSTRING(TextDatumGetCString(txt));
524 }
525 
526 /*
527  *		textrecv			- converts external binary format to text
528  */
529 Datum
textrecv(PG_FUNCTION_ARGS)530 textrecv(PG_FUNCTION_ARGS)
531 {
532 	StringInfo	buf = (StringInfo) PG_GETARG_POINTER(0);
533 	text	   *result;
534 	char	   *str;
535 	int			nbytes;
536 
537 	str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
538 
539 	result = cstring_to_text_with_len(str, nbytes);
540 	pfree(str);
541 	PG_RETURN_TEXT_P(result);
542 }
543 
544 /*
545  *		textsend			- converts text to binary format
546  */
547 Datum
textsend(PG_FUNCTION_ARGS)548 textsend(PG_FUNCTION_ARGS)
549 {
550 	text	   *t = PG_GETARG_TEXT_PP(0);
551 	StringInfoData buf;
552 
553 	pq_begintypsend(&buf);
554 	pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
555 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
556 }
557 
558 
559 /*
560  *		unknownin			- converts "..." to internal representation
561  */
562 Datum
unknownin(PG_FUNCTION_ARGS)563 unknownin(PG_FUNCTION_ARGS)
564 {
565 	char	   *str = PG_GETARG_CSTRING(0);
566 
567 	/* representation is same as cstring */
568 	PG_RETURN_CSTRING(pstrdup(str));
569 }
570 
571 /*
572  *		unknownout			- converts internal representation to "..."
573  */
574 Datum
unknownout(PG_FUNCTION_ARGS)575 unknownout(PG_FUNCTION_ARGS)
576 {
577 	/* representation is same as cstring */
578 	char	   *str = PG_GETARG_CSTRING(0);
579 
580 	PG_RETURN_CSTRING(pstrdup(str));
581 }
582 
583 /*
584  *		unknownrecv			- converts external binary format to unknown
585  */
586 Datum
unknownrecv(PG_FUNCTION_ARGS)587 unknownrecv(PG_FUNCTION_ARGS)
588 {
589 	StringInfo	buf = (StringInfo) PG_GETARG_POINTER(0);
590 	char	   *str;
591 	int			nbytes;
592 
593 	str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
594 	/* representation is same as cstring */
595 	PG_RETURN_CSTRING(str);
596 }
597 
598 /*
599  *		unknownsend			- converts unknown to binary format
600  */
601 Datum
unknownsend(PG_FUNCTION_ARGS)602 unknownsend(PG_FUNCTION_ARGS)
603 {
604 	/* representation is same as cstring */
605 	char	   *str = PG_GETARG_CSTRING(0);
606 	StringInfoData buf;
607 
608 	pq_begintypsend(&buf);
609 	pq_sendtext(&buf, str, strlen(str));
610 	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
611 }
612 
613 
614 /* ========== PUBLIC ROUTINES ========== */
615 
616 /*
617  * textlen -
618  *	  returns the logical length of a text*
619  *	   (which is less than the VARSIZE of the text*)
620  */
621 Datum
textlen(PG_FUNCTION_ARGS)622 textlen(PG_FUNCTION_ARGS)
623 {
624 	Datum		str = PG_GETARG_DATUM(0);
625 
626 	/* try to avoid decompressing argument */
627 	PG_RETURN_INT32(text_length(str));
628 }
629 
630 /*
631  * text_length -
632  *	Does the real work for textlen()
633  *
634  *	This is broken out so it can be called directly by other string processing
635  *	functions.  Note that the argument is passed as a Datum, to indicate that
636  *	it may still be in compressed form.  We can avoid decompressing it at all
637  *	in some cases.
638  */
639 static int32
text_length(Datum str)640 text_length(Datum str)
641 {
642 	/* fastpath when max encoding length is one */
643 	if (pg_database_encoding_max_length() == 1)
644 		PG_RETURN_INT32(toast_raw_datum_size(str) - VARHDRSZ);
645 	else
646 	{
647 		text	   *t = DatumGetTextPP(str);
648 
649 		PG_RETURN_INT32(pg_mbstrlen_with_len(VARDATA_ANY(t),
650 											 VARSIZE_ANY_EXHDR(t)));
651 	}
652 }
653 
654 /*
655  * textoctetlen -
656  *	  returns the physical length of a text*
657  *	   (which is less than the VARSIZE of the text*)
658  */
659 Datum
textoctetlen(PG_FUNCTION_ARGS)660 textoctetlen(PG_FUNCTION_ARGS)
661 {
662 	Datum		str = PG_GETARG_DATUM(0);
663 
664 	/* We need not detoast the input at all */
665 	PG_RETURN_INT32(toast_raw_datum_size(str) - VARHDRSZ);
666 }
667 
668 /*
669  * textcat -
670  *	  takes two text* and returns a text* that is the concatenation of
671  *	  the two.
672  *
673  * Rewritten by Sapa, sapa@hq.icb.chel.su. 8-Jul-96.
674  * Updated by Thomas, Thomas.Lockhart@jpl.nasa.gov 1997-07-10.
675  * Allocate space for output in all cases.
676  * XXX - thomas 1997-07-10
677  */
678 Datum
textcat(PG_FUNCTION_ARGS)679 textcat(PG_FUNCTION_ARGS)
680 {
681 	text	   *t1 = PG_GETARG_TEXT_PP(0);
682 	text	   *t2 = PG_GETARG_TEXT_PP(1);
683 
684 	PG_RETURN_TEXT_P(text_catenate(t1, t2));
685 }
686 
687 /*
688  * text_catenate
689  *	Guts of textcat(), broken out so it can be used by other functions
690  *
691  * Arguments can be in short-header form, but not compressed or out-of-line
692  */
693 static text *
text_catenate(text * t1,text * t2)694 text_catenate(text *t1, text *t2)
695 {
696 	text	   *result;
697 	int			len1,
698 				len2,
699 				len;
700 	char	   *ptr;
701 
702 	len1 = VARSIZE_ANY_EXHDR(t1);
703 	len2 = VARSIZE_ANY_EXHDR(t2);
704 
705 	/* paranoia ... probably should throw error instead? */
706 	if (len1 < 0)
707 		len1 = 0;
708 	if (len2 < 0)
709 		len2 = 0;
710 
711 	len = len1 + len2 + VARHDRSZ;
712 	result = (text *) palloc(len);
713 
714 	/* Set size of result string... */
715 	SET_VARSIZE(result, len);
716 
717 	/* Fill data field of result string... */
718 	ptr = VARDATA(result);
719 	if (len1 > 0)
720 		memcpy(ptr, VARDATA_ANY(t1), len1);
721 	if (len2 > 0)
722 		memcpy(ptr + len1, VARDATA_ANY(t2), len2);
723 
724 	return result;
725 }
726 
727 /*
728  * charlen_to_bytelen()
729  *	Compute the number of bytes occupied by n characters starting at *p
730  *
731  * It is caller's responsibility that there actually are n characters;
732  * the string need not be null-terminated.
733  */
734 static int
charlen_to_bytelen(const char * p,int n)735 charlen_to_bytelen(const char *p, int n)
736 {
737 	if (pg_database_encoding_max_length() == 1)
738 	{
739 		/* Optimization for single-byte encodings */
740 		return n;
741 	}
742 	else
743 	{
744 		const char *s;
745 
746 		for (s = p; n > 0; n--)
747 			s += pg_mblen(s);
748 
749 		return s - p;
750 	}
751 }
752 
753 /*
754  * text_substr()
755  * Return a substring starting at the specified position.
756  * - thomas 1997-12-31
757  *
758  * Input:
759  *	- string
760  *	- starting position (is one-based)
761  *	- string length
762  *
763  * If the starting position is zero or less, then return from the start of the string
764  *	adjusting the length to be consistent with the "negative start" per SQL.
765  * If the length is less than zero, return the remaining string.
766  *
767  * Added multibyte support.
768  * - Tatsuo Ishii 1998-4-21
769  * Changed behavior if starting position is less than one to conform to SQL behavior.
770  * Formerly returned the entire string; now returns a portion.
771  * - Thomas Lockhart 1998-12-10
772  * Now uses faster TOAST-slicing interface
773  * - John Gray 2002-02-22
774  * Remove "#ifdef MULTIBYTE" and test for encoding_max_length instead. Change
775  * behaviors conflicting with SQL to meet SQL (if E = S + L < S throw
776  * error; if E < 1, return '', not entire string). Fixed MB related bug when
777  * S > LC and < LC + 4 sometimes garbage characters are returned.
778  * - Joe Conway 2002-08-10
779  */
780 Datum
text_substr(PG_FUNCTION_ARGS)781 text_substr(PG_FUNCTION_ARGS)
782 {
783 	PG_RETURN_TEXT_P(text_substring(PG_GETARG_DATUM(0),
784 									PG_GETARG_INT32(1),
785 									PG_GETARG_INT32(2),
786 									false));
787 }
788 
789 /*
790  * text_substr_no_len -
791  *	  Wrapper to avoid opr_sanity failure due to
792  *	  one function accepting a different number of args.
793  */
794 Datum
text_substr_no_len(PG_FUNCTION_ARGS)795 text_substr_no_len(PG_FUNCTION_ARGS)
796 {
797 	PG_RETURN_TEXT_P(text_substring(PG_GETARG_DATUM(0),
798 									PG_GETARG_INT32(1),
799 									-1, true));
800 }
801 
802 /*
803  * text_substring -
804  *	Does the real work for text_substr() and text_substr_no_len()
805  *
806  *	This is broken out so it can be called directly by other string processing
807  *	functions.  Note that the argument is passed as a Datum, to indicate that
808  *	it may still be in compressed/toasted form.  We can avoid detoasting all
809  *	of it in some cases.
810  *
811  *	The result is always a freshly palloc'd datum.
812  */
813 static text *
text_substring(Datum str,int32 start,int32 length,bool length_not_specified)814 text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
815 {
816 	int32		eml = pg_database_encoding_max_length();
817 	int32		S = start;		/* start position */
818 	int32		S1;				/* adjusted start position */
819 	int32		L1;				/* adjusted substring length */
820 	int32		E;				/* end position */
821 
822 	/*
823 	 * SQL99 says S can be zero or negative, but we still must fetch from the
824 	 * start of the string.
825 	 */
826 	S1 = Max(S, 1);
827 
828 	/* life is easy if the encoding max length is 1 */
829 	if (eml == 1)
830 	{
831 		if (length_not_specified)	/* special case - get length to end of
832 									 * string */
833 			L1 = -1;
834 		else if (length < 0)
835 		{
836 			/* SQL99 says to throw an error for E < S, i.e., negative length */
837 			ereport(ERROR,
838 					(errcode(ERRCODE_SUBSTRING_ERROR),
839 					 errmsg("negative substring length not allowed")));
840 			L1 = -1;			/* silence stupider compilers */
841 		}
842 		else if (pg_add_s32_overflow(S, length, &E))
843 		{
844 			/*
845 			 * L could be large enough for S + L to overflow, in which case
846 			 * the substring must run to end of string.
847 			 */
848 			L1 = -1;
849 		}
850 		else
851 		{
852 			/*
853 			 * A zero or negative value for the end position can happen if the
854 			 * start was negative or one. SQL99 says to return a zero-length
855 			 * string.
856 			 */
857 			if (E < 1)
858 				return cstring_to_text("");
859 
860 			L1 = E - S1;
861 		}
862 
863 		/*
864 		 * If the start position is past the end of the string, SQL99 says to
865 		 * return a zero-length string -- DatumGetTextPSlice() will do that
866 		 * for us.  We need only convert S1 to zero-based starting position.
867 		 */
868 		return DatumGetTextPSlice(str, S1 - 1, L1);
869 	}
870 	else if (eml > 1)
871 	{
872 		/*
873 		 * When encoding max length is > 1, we can't get LC without
874 		 * detoasting, so we'll grab a conservatively large slice now and go
875 		 * back later to do the right thing
876 		 */
877 		int32		slice_start;
878 		int32		slice_size;
879 		int32		slice_strlen;
880 		text	   *slice;
881 		int32		E1;
882 		int32		i;
883 		char	   *p;
884 		char	   *s;
885 		text	   *ret;
886 
887 		/*
888 		 * We need to start at position zero because there is no way to know
889 		 * in advance which byte offset corresponds to the supplied start
890 		 * position.
891 		 */
892 		slice_start = 0;
893 
894 		if (length_not_specified)	/* special case - get length to end of
895 									 * string */
896 			slice_size = L1 = -1;
897 		else if (length < 0)
898 		{
899 			/* SQL99 says to throw an error for E < S, i.e., negative length */
900 			ereport(ERROR,
901 					(errcode(ERRCODE_SUBSTRING_ERROR),
902 					 errmsg("negative substring length not allowed")));
903 			slice_size = L1 = -1;	/* silence stupider compilers */
904 		}
905 		else if (pg_add_s32_overflow(S, length, &E))
906 		{
907 			/*
908 			 * L could be large enough for S + L to overflow, in which case
909 			 * the substring must run to end of string.
910 			 */
911 			slice_size = L1 = -1;
912 		}
913 		else
914 		{
915 			/*
916 			 * A zero or negative value for the end position can happen if the
917 			 * start was negative or one. SQL99 says to return a zero-length
918 			 * string.
919 			 */
920 			if (E < 1)
921 				return cstring_to_text("");
922 
923 			/*
924 			 * if E is past the end of the string, the tuple toaster will
925 			 * truncate the length for us
926 			 */
927 			L1 = E - S1;
928 
929 			/*
930 			 * Total slice size in bytes can't be any longer than the start
931 			 * position plus substring length times the encoding max length.
932 			 * If that overflows, we can just use -1.
933 			 */
934 			if (pg_mul_s32_overflow(E, eml, &slice_size))
935 				slice_size = -1;
936 		}
937 
938 		/*
939 		 * If we're working with an untoasted source, no need to do an extra
940 		 * copying step.
941 		 */
942 		if (VARATT_IS_COMPRESSED(DatumGetPointer(str)) ||
943 			VARATT_IS_EXTERNAL(DatumGetPointer(str)))
944 			slice = DatumGetTextPSlice(str, slice_start, slice_size);
945 		else
946 			slice = (text *) DatumGetPointer(str);
947 
948 		/* see if we got back an empty string */
949 		if (VARSIZE_ANY_EXHDR(slice) == 0)
950 		{
951 			if (slice != (text *) DatumGetPointer(str))
952 				pfree(slice);
953 			return cstring_to_text("");
954 		}
955 
956 		/* Now we can get the actual length of the slice in MB characters */
957 		slice_strlen = pg_mbstrlen_with_len(VARDATA_ANY(slice),
958 											VARSIZE_ANY_EXHDR(slice));
959 
960 		/*
961 		 * Check that the start position wasn't > slice_strlen. If so, SQL99
962 		 * says to return a zero-length string.
963 		 */
964 		if (S1 > slice_strlen)
965 		{
966 			if (slice != (text *) DatumGetPointer(str))
967 				pfree(slice);
968 			return cstring_to_text("");
969 		}
970 
971 		/*
972 		 * Adjust L1 and E1 now that we know the slice string length. Again
973 		 * remember that S1 is one based, and slice_start is zero based.
974 		 */
975 		if (L1 > -1)
976 			E1 = Min(S1 + L1, slice_start + 1 + slice_strlen);
977 		else
978 			E1 = slice_start + 1 + slice_strlen;
979 
980 		/*
981 		 * Find the start position in the slice; remember S1 is not zero based
982 		 */
983 		p = VARDATA_ANY(slice);
984 		for (i = 0; i < S1 - 1; i++)
985 			p += pg_mblen(p);
986 
987 		/* hang onto a pointer to our start position */
988 		s = p;
989 
990 		/*
991 		 * Count the actual bytes used by the substring of the requested
992 		 * length.
993 		 */
994 		for (i = S1; i < E1; i++)
995 			p += pg_mblen(p);
996 
997 		ret = (text *) palloc(VARHDRSZ + (p - s));
998 		SET_VARSIZE(ret, VARHDRSZ + (p - s));
999 		memcpy(VARDATA(ret), s, (p - s));
1000 
1001 		if (slice != (text *) DatumGetPointer(str))
1002 			pfree(slice);
1003 
1004 		return ret;
1005 	}
1006 	else
1007 		elog(ERROR, "invalid backend encoding: encoding max length < 1");
1008 
1009 	/* not reached: suppress compiler warning */
1010 	return NULL;
1011 }
1012 
1013 /*
1014  * textoverlay
1015  *	Replace specified substring of first string with second
1016  *
1017  * The SQL standard defines OVERLAY() in terms of substring and concatenation.
1018  * This code is a direct implementation of what the standard says.
1019  */
1020 Datum
textoverlay(PG_FUNCTION_ARGS)1021 textoverlay(PG_FUNCTION_ARGS)
1022 {
1023 	text	   *t1 = PG_GETARG_TEXT_PP(0);
1024 	text	   *t2 = PG_GETARG_TEXT_PP(1);
1025 	int			sp = PG_GETARG_INT32(2);	/* substring start position */
1026 	int			sl = PG_GETARG_INT32(3);	/* substring length */
1027 
1028 	PG_RETURN_TEXT_P(text_overlay(t1, t2, sp, sl));
1029 }
1030 
1031 Datum
textoverlay_no_len(PG_FUNCTION_ARGS)1032 textoverlay_no_len(PG_FUNCTION_ARGS)
1033 {
1034 	text	   *t1 = PG_GETARG_TEXT_PP(0);
1035 	text	   *t2 = PG_GETARG_TEXT_PP(1);
1036 	int			sp = PG_GETARG_INT32(2);	/* substring start position */
1037 	int			sl;
1038 
1039 	sl = text_length(PointerGetDatum(t2));	/* defaults to length(t2) */
1040 	PG_RETURN_TEXT_P(text_overlay(t1, t2, sp, sl));
1041 }
1042 
1043 static text *
text_overlay(text * t1,text * t2,int sp,int sl)1044 text_overlay(text *t1, text *t2, int sp, int sl)
1045 {
1046 	text	   *result;
1047 	text	   *s1;
1048 	text	   *s2;
1049 	int			sp_pl_sl;
1050 
1051 	/*
1052 	 * Check for possible integer-overflow cases.  For negative sp, throw a
1053 	 * "substring length" error because that's what should be expected
1054 	 * according to the spec's definition of OVERLAY().
1055 	 */
1056 	if (sp <= 0)
1057 		ereport(ERROR,
1058 				(errcode(ERRCODE_SUBSTRING_ERROR),
1059 				 errmsg("negative substring length not allowed")));
1060 	if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
1061 		ereport(ERROR,
1062 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1063 				 errmsg("integer out of range")));
1064 
1065 	s1 = text_substring(PointerGetDatum(t1), 1, sp - 1, false);
1066 	s2 = text_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
1067 	result = text_catenate(s1, t2);
1068 	result = text_catenate(result, s2);
1069 
1070 	return result;
1071 }
1072 
1073 /*
1074  * textpos -
1075  *	  Return the position of the specified substring.
1076  *	  Implements the SQL POSITION() function.
1077  *	  Ref: A Guide To The SQL Standard, Date & Darwen, 1997
1078  * - thomas 1997-07-27
1079  */
1080 Datum
textpos(PG_FUNCTION_ARGS)1081 textpos(PG_FUNCTION_ARGS)
1082 {
1083 	text	   *str = PG_GETARG_TEXT_PP(0);
1084 	text	   *search_str = PG_GETARG_TEXT_PP(1);
1085 
1086 	PG_RETURN_INT32((int32) text_position(str, search_str));
1087 }
1088 
1089 /*
1090  * text_position -
1091  *	Does the real work for textpos()
1092  *
1093  * Inputs:
1094  *		t1 - string to be searched
1095  *		t2 - pattern to match within t1
1096  * Result:
1097  *		Character index of the first matched char, starting from 1,
1098  *		or 0 if no match.
1099  *
1100  *	This is broken out so it can be called directly by other string processing
1101  *	functions.
1102  */
1103 static int
text_position(text * t1,text * t2)1104 text_position(text *t1, text *t2)
1105 {
1106 	TextPositionState state;
1107 	int			result;
1108 
1109 	text_position_setup(t1, t2, &state);
1110 	result = text_position_next(1, &state);
1111 	text_position_cleanup(&state);
1112 	return result;
1113 }
1114 
1115 
1116 /*
1117  * text_position_setup, text_position_next, text_position_cleanup -
1118  *	Component steps of text_position()
1119  *
1120  * These are broken out so that a string can be efficiently searched for
1121  * multiple occurrences of the same pattern.  text_position_next may be
1122  * called multiple times with increasing values of start_pos, which is
1123  * the 1-based character position to start the search from.  The "state"
1124  * variable is normally just a local variable in the caller.
1125  */
1126 
1127 static void
text_position_setup(text * t1,text * t2,TextPositionState * state)1128 text_position_setup(text *t1, text *t2, TextPositionState *state)
1129 {
1130 	int			len1 = VARSIZE_ANY_EXHDR(t1);
1131 	int			len2 = VARSIZE_ANY_EXHDR(t2);
1132 
1133 	if (pg_database_encoding_max_length() == 1)
1134 	{
1135 		/* simple case - single byte encoding */
1136 		state->use_wchar = false;
1137 		state->str1 = VARDATA_ANY(t1);
1138 		state->str2 = VARDATA_ANY(t2);
1139 		state->len1 = len1;
1140 		state->len2 = len2;
1141 	}
1142 	else
1143 	{
1144 		/* not as simple - multibyte encoding */
1145 		pg_wchar   *p1,
1146 				   *p2;
1147 
1148 		p1 = (pg_wchar *) palloc((len1 + 1) * sizeof(pg_wchar));
1149 		len1 = pg_mb2wchar_with_len(VARDATA_ANY(t1), p1, len1);
1150 		p2 = (pg_wchar *) palloc((len2 + 1) * sizeof(pg_wchar));
1151 		len2 = pg_mb2wchar_with_len(VARDATA_ANY(t2), p2, len2);
1152 
1153 		state->use_wchar = true;
1154 		state->wstr1 = p1;
1155 		state->wstr2 = p2;
1156 		state->len1 = len1;
1157 		state->len2 = len2;
1158 	}
1159 
1160 	/*
1161 	 * Prepare the skip table for Boyer-Moore-Horspool searching.  In these
1162 	 * notes we use the terminology that the "haystack" is the string to be
1163 	 * searched (t1) and the "needle" is the pattern being sought (t2).
1164 	 *
1165 	 * If the needle is empty or bigger than the haystack then there is no
1166 	 * point in wasting cycles initializing the table.  We also choose not to
1167 	 * use B-M-H for needles of length 1, since the skip table can't possibly
1168 	 * save anything in that case.
1169 	 */
1170 	if (len1 >= len2 && len2 > 1)
1171 	{
1172 		int			searchlength = len1 - len2;
1173 		int			skiptablemask;
1174 		int			last;
1175 		int			i;
1176 
1177 		/*
1178 		 * First we must determine how much of the skip table to use.  The
1179 		 * declaration of TextPositionState allows up to 256 elements, but for
1180 		 * short search problems we don't really want to have to initialize so
1181 		 * many elements --- it would take too long in comparison to the
1182 		 * actual search time.  So we choose a useful skip table size based on
1183 		 * the haystack length minus the needle length.  The closer the needle
1184 		 * length is to the haystack length the less useful skipping becomes.
1185 		 *
1186 		 * Note: since we use bit-masking to select table elements, the skip
1187 		 * table size MUST be a power of 2, and so the mask must be 2^N-1.
1188 		 */
1189 		if (searchlength < 16)
1190 			skiptablemask = 3;
1191 		else if (searchlength < 64)
1192 			skiptablemask = 7;
1193 		else if (searchlength < 128)
1194 			skiptablemask = 15;
1195 		else if (searchlength < 512)
1196 			skiptablemask = 31;
1197 		else if (searchlength < 2048)
1198 			skiptablemask = 63;
1199 		else if (searchlength < 4096)
1200 			skiptablemask = 127;
1201 		else
1202 			skiptablemask = 255;
1203 		state->skiptablemask = skiptablemask;
1204 
1205 		/*
1206 		 * Initialize the skip table.  We set all elements to the needle
1207 		 * length, since this is the correct skip distance for any character
1208 		 * not found in the needle.
1209 		 */
1210 		for (i = 0; i <= skiptablemask; i++)
1211 			state->skiptable[i] = len2;
1212 
1213 		/*
1214 		 * Now examine the needle.  For each character except the last one,
1215 		 * set the corresponding table element to the appropriate skip
1216 		 * distance.  Note that when two characters share the same skip table
1217 		 * entry, the one later in the needle must determine the skip
1218 		 * distance.
1219 		 */
1220 		last = len2 - 1;
1221 
1222 		if (!state->use_wchar)
1223 		{
1224 			const char *str2 = state->str2;
1225 
1226 			for (i = 0; i < last; i++)
1227 				state->skiptable[(unsigned char) str2[i] & skiptablemask] = last - i;
1228 		}
1229 		else
1230 		{
1231 			const pg_wchar *wstr2 = state->wstr2;
1232 
1233 			for (i = 0; i < last; i++)
1234 				state->skiptable[wstr2[i] & skiptablemask] = last - i;
1235 		}
1236 	}
1237 }
1238 
1239 static int
text_position_next(int start_pos,TextPositionState * state)1240 text_position_next(int start_pos, TextPositionState *state)
1241 {
1242 	int			haystack_len = state->len1;
1243 	int			needle_len = state->len2;
1244 	int			skiptablemask = state->skiptablemask;
1245 
1246 	Assert(start_pos > 0);		/* else caller error */
1247 
1248 	if (needle_len <= 0)
1249 		return start_pos;		/* result for empty pattern */
1250 
1251 	start_pos--;				/* adjust for zero based arrays */
1252 
1253 	/* Done if the needle can't possibly fit */
1254 	if (haystack_len < start_pos + needle_len)
1255 		return 0;
1256 
1257 	if (!state->use_wchar)
1258 	{
1259 		/* simple case - single byte encoding */
1260 		const char *haystack = state->str1;
1261 		const char *needle = state->str2;
1262 		const char *haystack_end = &haystack[haystack_len];
1263 		const char *hptr;
1264 
1265 		if (needle_len == 1)
1266 		{
1267 			/* No point in using B-M-H for a one-character needle */
1268 			char		nchar = *needle;
1269 
1270 			hptr = &haystack[start_pos];
1271 			while (hptr < haystack_end)
1272 			{
1273 				if (*hptr == nchar)
1274 					return hptr - haystack + 1;
1275 				hptr++;
1276 			}
1277 		}
1278 		else
1279 		{
1280 			const char *needle_last = &needle[needle_len - 1];
1281 
1282 			/* Start at startpos plus the length of the needle */
1283 			hptr = &haystack[start_pos + needle_len - 1];
1284 			while (hptr < haystack_end)
1285 			{
1286 				/* Match the needle scanning *backward* */
1287 				const char *nptr;
1288 				const char *p;
1289 
1290 				nptr = needle_last;
1291 				p = hptr;
1292 				while (*nptr == *p)
1293 				{
1294 					/* Matched it all?	If so, return 1-based position */
1295 					if (nptr == needle)
1296 						return p - haystack + 1;
1297 					nptr--, p--;
1298 				}
1299 
1300 				/*
1301 				 * No match, so use the haystack char at hptr to decide how
1302 				 * far to advance.  If the needle had any occurrence of that
1303 				 * character (or more precisely, one sharing the same
1304 				 * skiptable entry) before its last character, then we advance
1305 				 * far enough to align the last such needle character with
1306 				 * that haystack position.  Otherwise we can advance by the
1307 				 * whole needle length.
1308 				 */
1309 				hptr += state->skiptable[(unsigned char) *hptr & skiptablemask];
1310 			}
1311 		}
1312 	}
1313 	else
1314 	{
1315 		/* The multibyte char version. This works exactly the same way. */
1316 		const pg_wchar *haystack = state->wstr1;
1317 		const pg_wchar *needle = state->wstr2;
1318 		const pg_wchar *haystack_end = &haystack[haystack_len];
1319 		const pg_wchar *hptr;
1320 
1321 		if (needle_len == 1)
1322 		{
1323 			/* No point in using B-M-H for a one-character needle */
1324 			pg_wchar	nchar = *needle;
1325 
1326 			hptr = &haystack[start_pos];
1327 			while (hptr < haystack_end)
1328 			{
1329 				if (*hptr == nchar)
1330 					return hptr - haystack + 1;
1331 				hptr++;
1332 			}
1333 		}
1334 		else
1335 		{
1336 			const pg_wchar *needle_last = &needle[needle_len - 1];
1337 
1338 			/* Start at startpos plus the length of the needle */
1339 			hptr = &haystack[start_pos + needle_len - 1];
1340 			while (hptr < haystack_end)
1341 			{
1342 				/* Match the needle scanning *backward* */
1343 				const pg_wchar *nptr;
1344 				const pg_wchar *p;
1345 
1346 				nptr = needle_last;
1347 				p = hptr;
1348 				while (*nptr == *p)
1349 				{
1350 					/* Matched it all?	If so, return 1-based position */
1351 					if (nptr == needle)
1352 						return p - haystack + 1;
1353 					nptr--, p--;
1354 				}
1355 
1356 				/*
1357 				 * No match, so use the haystack char at hptr to decide how
1358 				 * far to advance.  If the needle had any occurrence of that
1359 				 * character (or more precisely, one sharing the same
1360 				 * skiptable entry) before its last character, then we advance
1361 				 * far enough to align the last such needle character with
1362 				 * that haystack position.  Otherwise we can advance by the
1363 				 * whole needle length.
1364 				 */
1365 				hptr += state->skiptable[*hptr & skiptablemask];
1366 			}
1367 		}
1368 	}
1369 
1370 	return 0;					/* not found */
1371 }
1372 
1373 static void
text_position_cleanup(TextPositionState * state)1374 text_position_cleanup(TextPositionState *state)
1375 {
1376 	if (state->use_wchar)
1377 	{
1378 		pfree(state->wstr1);
1379 		pfree(state->wstr2);
1380 	}
1381 }
1382 
1383 /* varstr_cmp()
1384  * Comparison function for text strings with given lengths.
1385  * Includes locale support, but must copy strings to temporary memory
1386  *	to allow null-termination for inputs to strcoll().
1387  * Returns an integer less than, equal to, or greater than zero, indicating
1388  * whether arg1 is less than, equal to, or greater than arg2.
1389  */
1390 int
varstr_cmp(const char * arg1,int len1,const char * arg2,int len2,Oid collid)1391 varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
1392 {
1393 	int			result;
1394 
1395 	/*
1396 	 * Unfortunately, there is no strncoll(), so in the non-C locale case we
1397 	 * have to do some memory copying.  This turns out to be significantly
1398 	 * slower, so we optimize the case where LC_COLLATE is C.  We also try to
1399 	 * optimize relatively-short strings by avoiding palloc/pfree overhead.
1400 	 */
1401 	if (lc_collate_is_c(collid))
1402 	{
1403 		result = memcmp(arg1, arg2, Min(len1, len2));
1404 		if ((result == 0) && (len1 != len2))
1405 			result = (len1 < len2) ? -1 : 1;
1406 	}
1407 	else
1408 	{
1409 		char		a1buf[TEXTBUFLEN];
1410 		char		a2buf[TEXTBUFLEN];
1411 		char	   *a1p,
1412 				   *a2p;
1413 		pg_locale_t mylocale = 0;
1414 
1415 		if (collid != DEFAULT_COLLATION_OID)
1416 		{
1417 			if (!OidIsValid(collid))
1418 			{
1419 				/*
1420 				 * This typically means that the parser could not resolve a
1421 				 * conflict of implicit collations, so report it that way.
1422 				 */
1423 				ereport(ERROR,
1424 						(errcode(ERRCODE_INDETERMINATE_COLLATION),
1425 						 errmsg("could not determine which collation to use for string comparison"),
1426 						 errhint("Use the COLLATE clause to set the collation explicitly.")));
1427 			}
1428 			mylocale = pg_newlocale_from_collation(collid);
1429 		}
1430 
1431 		/*
1432 		 * memcmp() can't tell us which of two unequal strings sorts first,
1433 		 * but it's a cheap way to tell if they're equal.  Testing shows that
1434 		 * memcmp() followed by strcoll() is only trivially slower than
1435 		 * strcoll() by itself, so we don't lose much if this doesn't work out
1436 		 * very often, and if it does - for example, because there are many
1437 		 * equal strings in the input - then we win big by avoiding expensive
1438 		 * collation-aware comparisons.
1439 		 */
1440 		if (len1 == len2 && memcmp(arg1, arg2, len1) == 0)
1441 			return 0;
1442 
1443 #ifdef WIN32
1444 		/* Win32 does not have UTF-8, so we need to map to UTF-16 */
1445 		if (GetDatabaseEncoding() == PG_UTF8
1446 			&& (!mylocale || mylocale->provider == COLLPROVIDER_LIBC))
1447 		{
1448 			int			a1len;
1449 			int			a2len;
1450 			int			r;
1451 
1452 			if (len1 >= TEXTBUFLEN / 2)
1453 			{
1454 				a1len = len1 * 2 + 2;
1455 				a1p = palloc(a1len);
1456 			}
1457 			else
1458 			{
1459 				a1len = TEXTBUFLEN;
1460 				a1p = a1buf;
1461 			}
1462 			if (len2 >= TEXTBUFLEN / 2)
1463 			{
1464 				a2len = len2 * 2 + 2;
1465 				a2p = palloc(a2len);
1466 			}
1467 			else
1468 			{
1469 				a2len = TEXTBUFLEN;
1470 				a2p = a2buf;
1471 			}
1472 
1473 			/* stupid Microsloth API does not work for zero-length input */
1474 			if (len1 == 0)
1475 				r = 0;
1476 			else
1477 			{
1478 				r = MultiByteToWideChar(CP_UTF8, 0, arg1, len1,
1479 										(LPWSTR) a1p, a1len / 2);
1480 				if (!r)
1481 					ereport(ERROR,
1482 							(errmsg("could not convert string to UTF-16: error code %lu",
1483 									GetLastError())));
1484 			}
1485 			((LPWSTR) a1p)[r] = 0;
1486 
1487 			if (len2 == 0)
1488 				r = 0;
1489 			else
1490 			{
1491 				r = MultiByteToWideChar(CP_UTF8, 0, arg2, len2,
1492 										(LPWSTR) a2p, a2len / 2);
1493 				if (!r)
1494 					ereport(ERROR,
1495 							(errmsg("could not convert string to UTF-16: error code %lu",
1496 									GetLastError())));
1497 			}
1498 			((LPWSTR) a2p)[r] = 0;
1499 
1500 			errno = 0;
1501 #ifdef HAVE_LOCALE_T
1502 			if (mylocale)
1503 				result = wcscoll_l((LPWSTR) a1p, (LPWSTR) a2p, mylocale->info.lt);
1504 			else
1505 #endif
1506 				result = wcscoll((LPWSTR) a1p, (LPWSTR) a2p);
1507 			if (result == 2147483647)	/* _NLSCMPERROR; missing from mingw
1508 										 * headers */
1509 				ereport(ERROR,
1510 						(errmsg("could not compare Unicode strings: %m")));
1511 
1512 			/*
1513 			 * In some locales wcscoll() can claim that nonidentical strings
1514 			 * are equal.  Believing that would be bad news for a number of
1515 			 * reasons, so we follow Perl's lead and sort "equal" strings
1516 			 * according to strcmp (on the UTF-8 representation).
1517 			 */
1518 			if (result == 0)
1519 			{
1520 				result = memcmp(arg1, arg2, Min(len1, len2));
1521 				if ((result == 0) && (len1 != len2))
1522 					result = (len1 < len2) ? -1 : 1;
1523 			}
1524 
1525 			if (a1p != a1buf)
1526 				pfree(a1p);
1527 			if (a2p != a2buf)
1528 				pfree(a2p);
1529 
1530 			return result;
1531 		}
1532 #endif							/* WIN32 */
1533 
1534 		if (len1 >= TEXTBUFLEN)
1535 			a1p = (char *) palloc(len1 + 1);
1536 		else
1537 			a1p = a1buf;
1538 		if (len2 >= TEXTBUFLEN)
1539 			a2p = (char *) palloc(len2 + 1);
1540 		else
1541 			a2p = a2buf;
1542 
1543 		memcpy(a1p, arg1, len1);
1544 		a1p[len1] = '\0';
1545 		memcpy(a2p, arg2, len2);
1546 		a2p[len2] = '\0';
1547 
1548 		if (mylocale)
1549 		{
1550 			if (mylocale->provider == COLLPROVIDER_ICU)
1551 			{
1552 #ifdef USE_ICU
1553 #ifdef HAVE_UCOL_STRCOLLUTF8
1554 				if (GetDatabaseEncoding() == PG_UTF8)
1555 				{
1556 					UErrorCode	status;
1557 
1558 					status = U_ZERO_ERROR;
1559 					result = ucol_strcollUTF8(mylocale->info.icu.ucol,
1560 											  arg1, len1,
1561 											  arg2, len2,
1562 											  &status);
1563 					if (U_FAILURE(status))
1564 						ereport(ERROR,
1565 								(errmsg("collation failed: %s", u_errorName(status))));
1566 				}
1567 				else
1568 #endif
1569 				{
1570 					int32_t		ulen1,
1571 								ulen2;
1572 					UChar	   *uchar1,
1573 							   *uchar2;
1574 
1575 					ulen1 = icu_to_uchar(&uchar1, arg1, len1);
1576 					ulen2 = icu_to_uchar(&uchar2, arg2, len2);
1577 
1578 					result = ucol_strcoll(mylocale->info.icu.ucol,
1579 										  uchar1, ulen1,
1580 										  uchar2, ulen2);
1581 
1582 					pfree(uchar1);
1583 					pfree(uchar2);
1584 				}
1585 #else							/* not USE_ICU */
1586 				/* shouldn't happen */
1587 				elog(ERROR, "unsupported collprovider: %c", mylocale->provider);
1588 #endif							/* not USE_ICU */
1589 			}
1590 			else
1591 			{
1592 #ifdef HAVE_LOCALE_T
1593 				result = strcoll_l(a1p, a2p, mylocale->info.lt);
1594 #else
1595 				/* shouldn't happen */
1596 				elog(ERROR, "unsupported collprovider: %c", mylocale->provider);
1597 #endif
1598 			}
1599 		}
1600 		else
1601 			result = strcoll(a1p, a2p);
1602 
1603 		/*
1604 		 * In some locales strcoll() can claim that nonidentical strings are
1605 		 * equal.  Believing that would be bad news for a number of reasons,
1606 		 * so we follow Perl's lead and sort "equal" strings according to
1607 		 * strcmp().
1608 		 */
1609 		if (result == 0)
1610 			result = strcmp(a1p, a2p);
1611 
1612 		if (a1p != a1buf)
1613 			pfree(a1p);
1614 		if (a2p != a2buf)
1615 			pfree(a2p);
1616 	}
1617 
1618 	return result;
1619 }
1620 
1621 /* text_cmp()
1622  * Internal comparison function for text strings.
1623  * Returns -1, 0 or 1
1624  */
1625 static int
text_cmp(text * arg1,text * arg2,Oid collid)1626 text_cmp(text *arg1, text *arg2, Oid collid)
1627 {
1628 	char	   *a1p,
1629 			   *a2p;
1630 	int			len1,
1631 				len2;
1632 
1633 	a1p = VARDATA_ANY(arg1);
1634 	a2p = VARDATA_ANY(arg2);
1635 
1636 	len1 = VARSIZE_ANY_EXHDR(arg1);
1637 	len2 = VARSIZE_ANY_EXHDR(arg2);
1638 
1639 	return varstr_cmp(a1p, len1, a2p, len2, collid);
1640 }
1641 
1642 /*
1643  * Comparison functions for text strings.
1644  *
1645  * Note: btree indexes need these routines not to leak memory; therefore,
1646  * be careful to free working copies of toasted datums.  Most places don't
1647  * need to be so careful.
1648  */
1649 
1650 Datum
texteq(PG_FUNCTION_ARGS)1651 texteq(PG_FUNCTION_ARGS)
1652 {
1653 	Datum		arg1 = PG_GETARG_DATUM(0);
1654 	Datum		arg2 = PG_GETARG_DATUM(1);
1655 	bool		result;
1656 	Size		len1,
1657 				len2;
1658 
1659 	/*
1660 	 * Since we only care about equality or not-equality, we can avoid all the
1661 	 * expense of strcoll() here, and just do bitwise comparison.  In fact, we
1662 	 * don't even have to do a bitwise comparison if we can show the lengths
1663 	 * of the strings are unequal; which might save us from having to detoast
1664 	 * one or both values.
1665 	 */
1666 	len1 = toast_raw_datum_size(arg1);
1667 	len2 = toast_raw_datum_size(arg2);
1668 	if (len1 != len2)
1669 		result = false;
1670 	else
1671 	{
1672 		text	   *targ1 = DatumGetTextPP(arg1);
1673 		text	   *targ2 = DatumGetTextPP(arg2);
1674 
1675 		result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
1676 						 len1 - VARHDRSZ) == 0);
1677 
1678 		PG_FREE_IF_COPY(targ1, 0);
1679 		PG_FREE_IF_COPY(targ2, 1);
1680 	}
1681 
1682 	PG_RETURN_BOOL(result);
1683 }
1684 
1685 Datum
textne(PG_FUNCTION_ARGS)1686 textne(PG_FUNCTION_ARGS)
1687 {
1688 	Datum		arg1 = PG_GETARG_DATUM(0);
1689 	Datum		arg2 = PG_GETARG_DATUM(1);
1690 	bool		result;
1691 	Size		len1,
1692 				len2;
1693 
1694 	/* See comment in texteq() */
1695 	len1 = toast_raw_datum_size(arg1);
1696 	len2 = toast_raw_datum_size(arg2);
1697 	if (len1 != len2)
1698 		result = true;
1699 	else
1700 	{
1701 		text	   *targ1 = DatumGetTextPP(arg1);
1702 		text	   *targ2 = DatumGetTextPP(arg2);
1703 
1704 		result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
1705 						 len1 - VARHDRSZ) != 0);
1706 
1707 		PG_FREE_IF_COPY(targ1, 0);
1708 		PG_FREE_IF_COPY(targ2, 1);
1709 	}
1710 
1711 	PG_RETURN_BOOL(result);
1712 }
1713 
1714 Datum
text_lt(PG_FUNCTION_ARGS)1715 text_lt(PG_FUNCTION_ARGS)
1716 {
1717 	text	   *arg1 = PG_GETARG_TEXT_PP(0);
1718 	text	   *arg2 = PG_GETARG_TEXT_PP(1);
1719 	bool		result;
1720 
1721 	result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0);
1722 
1723 	PG_FREE_IF_COPY(arg1, 0);
1724 	PG_FREE_IF_COPY(arg2, 1);
1725 
1726 	PG_RETURN_BOOL(result);
1727 }
1728 
1729 Datum
text_le(PG_FUNCTION_ARGS)1730 text_le(PG_FUNCTION_ARGS)
1731 {
1732 	text	   *arg1 = PG_GETARG_TEXT_PP(0);
1733 	text	   *arg2 = PG_GETARG_TEXT_PP(1);
1734 	bool		result;
1735 
1736 	result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) <= 0);
1737 
1738 	PG_FREE_IF_COPY(arg1, 0);
1739 	PG_FREE_IF_COPY(arg2, 1);
1740 
1741 	PG_RETURN_BOOL(result);
1742 }
1743 
1744 Datum
text_gt(PG_FUNCTION_ARGS)1745 text_gt(PG_FUNCTION_ARGS)
1746 {
1747 	text	   *arg1 = PG_GETARG_TEXT_PP(0);
1748 	text	   *arg2 = PG_GETARG_TEXT_PP(1);
1749 	bool		result;
1750 
1751 	result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0);
1752 
1753 	PG_FREE_IF_COPY(arg1, 0);
1754 	PG_FREE_IF_COPY(arg2, 1);
1755 
1756 	PG_RETURN_BOOL(result);
1757 }
1758 
1759 Datum
text_ge(PG_FUNCTION_ARGS)1760 text_ge(PG_FUNCTION_ARGS)
1761 {
1762 	text	   *arg1 = PG_GETARG_TEXT_PP(0);
1763 	text	   *arg2 = PG_GETARG_TEXT_PP(1);
1764 	bool		result;
1765 
1766 	result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) >= 0);
1767 
1768 	PG_FREE_IF_COPY(arg1, 0);
1769 	PG_FREE_IF_COPY(arg2, 1);
1770 
1771 	PG_RETURN_BOOL(result);
1772 }
1773 
1774 Datum
text_starts_with(PG_FUNCTION_ARGS)1775 text_starts_with(PG_FUNCTION_ARGS)
1776 {
1777 	Datum		arg1 = PG_GETARG_DATUM(0);
1778 	Datum		arg2 = PG_GETARG_DATUM(1);
1779 	bool		result;
1780 	Size		len1,
1781 				len2;
1782 
1783 	len1 = toast_raw_datum_size(arg1);
1784 	len2 = toast_raw_datum_size(arg2);
1785 	if (len2 > len1)
1786 		result = false;
1787 	else
1788 	{
1789 		text	   *targ1 = DatumGetTextPP(arg1);
1790 		text	   *targ2 = DatumGetTextPP(arg2);
1791 
1792 		result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
1793 						 VARSIZE_ANY_EXHDR(targ2)) == 0);
1794 
1795 		PG_FREE_IF_COPY(targ1, 0);
1796 		PG_FREE_IF_COPY(targ2, 1);
1797 	}
1798 
1799 	PG_RETURN_BOOL(result);
1800 }
1801 
1802 Datum
bttextcmp(PG_FUNCTION_ARGS)1803 bttextcmp(PG_FUNCTION_ARGS)
1804 {
1805 	text	   *arg1 = PG_GETARG_TEXT_PP(0);
1806 	text	   *arg2 = PG_GETARG_TEXT_PP(1);
1807 	int32		result;
1808 
1809 	result = text_cmp(arg1, arg2, PG_GET_COLLATION());
1810 
1811 	PG_FREE_IF_COPY(arg1, 0);
1812 	PG_FREE_IF_COPY(arg2, 1);
1813 
1814 	PG_RETURN_INT32(result);
1815 }
1816 
1817 Datum
bttextsortsupport(PG_FUNCTION_ARGS)1818 bttextsortsupport(PG_FUNCTION_ARGS)
1819 {
1820 	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
1821 	Oid			collid = ssup->ssup_collation;
1822 	MemoryContext oldcontext;
1823 
1824 	oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
1825 
1826 	/* Use generic string SortSupport */
1827 	varstr_sortsupport(ssup, collid, false);
1828 
1829 	MemoryContextSwitchTo(oldcontext);
1830 
1831 	PG_RETURN_VOID();
1832 }
1833 
1834 /*
1835  * Generic sortsupport interface for character type's operator classes.
1836  * Includes locale support, and support for BpChar semantics (i.e. removing
1837  * trailing spaces before comparison).
1838  *
1839  * Relies on the assumption that text, VarChar, BpChar, and bytea all have the
1840  * same representation.  Callers that always use the C collation (e.g.
1841  * non-collatable type callers like bytea) may have NUL bytes in their strings;
1842  * this will not work with any other collation, though.
1843  */
1844 void
varstr_sortsupport(SortSupport ssup,Oid collid,bool bpchar)1845 varstr_sortsupport(SortSupport ssup, Oid collid, bool bpchar)
1846 {
1847 	bool		abbreviate = ssup->abbreviate;
1848 	bool		collate_c = false;
1849 	VarStringSortSupport *sss;
1850 	pg_locale_t locale = 0;
1851 
1852 	/*
1853 	 * If possible, set ssup->comparator to a function which can be used to
1854 	 * directly compare two datums.  If we can do this, we'll avoid the
1855 	 * overhead of a trip through the fmgr layer for every comparison, which
1856 	 * can be substantial.
1857 	 *
1858 	 * Most typically, we'll set the comparator to varstrfastcmp_locale, which
1859 	 * uses strcoll() to perform comparisons and knows about the special
1860 	 * requirements of BpChar callers.  However, if LC_COLLATE = C, we can
1861 	 * make things quite a bit faster with varstrfastcmp_c or bpcharfastcmp_c,
1862 	 * both of which use memcmp() rather than strcoll().
1863 	 */
1864 	if (lc_collate_is_c(collid))
1865 	{
1866 		if (!bpchar)
1867 			ssup->comparator = varstrfastcmp_c;
1868 		else
1869 			ssup->comparator = bpcharfastcmp_c;
1870 
1871 		collate_c = true;
1872 	}
1873 	else
1874 	{
1875 		/*
1876 		 * We need a collation-sensitive comparison.  To make things faster,
1877 		 * we'll figure out the collation based on the locale id and cache the
1878 		 * result.
1879 		 */
1880 		if (collid != DEFAULT_COLLATION_OID)
1881 		{
1882 			if (!OidIsValid(collid))
1883 			{
1884 				/*
1885 				 * This typically means that the parser could not resolve a
1886 				 * conflict of implicit collations, so report it that way.
1887 				 */
1888 				ereport(ERROR,
1889 						(errcode(ERRCODE_INDETERMINATE_COLLATION),
1890 						 errmsg("could not determine which collation to use for string comparison"),
1891 						 errhint("Use the COLLATE clause to set the collation explicitly.")));
1892 			}
1893 			locale = pg_newlocale_from_collation(collid);
1894 		}
1895 
1896 		/*
1897 		 * There is a further exception on Windows.  When the database
1898 		 * encoding is UTF-8 and we are not using the C collation, complex
1899 		 * hacks are required.  We don't currently have a comparator that
1900 		 * handles that case, so we fall back on the slow method of having the
1901 		 * sort code invoke bttextcmp() (in the case of text) via the fmgr
1902 		 * trampoline.  ICU locales work just the same on Windows, however.
1903 		 */
1904 #ifdef WIN32
1905 		if (GetDatabaseEncoding() == PG_UTF8 &&
1906 			!(locale && locale->provider == COLLPROVIDER_ICU))
1907 			return;
1908 #endif
1909 
1910 		ssup->comparator = varstrfastcmp_locale;
1911 	}
1912 
1913 	/*
1914 	 * Unfortunately, it seems that abbreviation for non-C collations is
1915 	 * broken on many common platforms; testing of multiple versions of glibc
1916 	 * reveals that, for many locales, strcoll() and strxfrm() do not return
1917 	 * consistent results, which is fatal to this optimization.  While no
1918 	 * other libc other than Cygwin has so far been shown to have a problem,
1919 	 * we take the conservative course of action for right now and disable
1920 	 * this categorically.  (Users who are certain this isn't a problem on
1921 	 * their system can define TRUST_STRXFRM.)
1922 	 *
1923 	 * Even apart from the risk of broken locales, it's possible that there
1924 	 * are platforms where the use of abbreviated keys should be disabled at
1925 	 * compile time.  Having only 4 byte datums could make worst-case
1926 	 * performance drastically more likely, for example.  Moreover, macOS's
1927 	 * strxfrm() implementation is known to not effectively concentrate a
1928 	 * significant amount of entropy from the original string in earlier
1929 	 * transformed blobs.  It's possible that other supported platforms are
1930 	 * similarly encumbered.  So, if we ever get past disabling this
1931 	 * categorically, we may still want or need to disable it for particular
1932 	 * platforms.
1933 	 */
1934 #ifndef TRUST_STRXFRM
1935 	if (!collate_c && !(locale && locale->provider == COLLPROVIDER_ICU))
1936 		abbreviate = false;
1937 #endif
1938 
1939 	/*
1940 	 * If we're using abbreviated keys, or if we're using a locale-aware
1941 	 * comparison, we need to initialize a StringSortSupport object.  Both
1942 	 * cases will make use of the temporary buffers we initialize here for
1943 	 * scratch space (and to detect requirement for BpChar semantics from
1944 	 * caller), and the abbreviation case requires additional state.
1945 	 */
1946 	if (abbreviate || !collate_c)
1947 	{
1948 		sss = palloc(sizeof(VarStringSortSupport));
1949 		sss->buf1 = palloc(TEXTBUFLEN);
1950 		sss->buflen1 = TEXTBUFLEN;
1951 		sss->buf2 = palloc(TEXTBUFLEN);
1952 		sss->buflen2 = TEXTBUFLEN;
1953 		/* Start with invalid values */
1954 		sss->last_len1 = -1;
1955 		sss->last_len2 = -1;
1956 		/* Initialize */
1957 		sss->last_returned = 0;
1958 		sss->locale = locale;
1959 
1960 		/*
1961 		 * To avoid somehow confusing a strxfrm() blob and an original string,
1962 		 * constantly keep track of the variety of data that buf1 and buf2
1963 		 * currently contain.
1964 		 *
1965 		 * Comparisons may be interleaved with conversion calls.  Frequently,
1966 		 * conversions and comparisons are batched into two distinct phases,
1967 		 * but the correctness of caching cannot hinge upon this.  For
1968 		 * comparison caching, buffer state is only trusted if cache_blob is
1969 		 * found set to false, whereas strxfrm() caching only trusts the state
1970 		 * when cache_blob is found set to true.
1971 		 *
1972 		 * Arbitrarily initialize cache_blob to true.
1973 		 */
1974 		sss->cache_blob = true;
1975 		sss->collate_c = collate_c;
1976 		sss->bpchar = bpchar;
1977 		ssup->ssup_extra = sss;
1978 
1979 		/*
1980 		 * If possible, plan to use the abbreviated keys optimization.  The
1981 		 * core code may switch back to authoritative comparator should
1982 		 * abbreviation be aborted.
1983 		 */
1984 		if (abbreviate)
1985 		{
1986 			sss->prop_card = 0.20;
1987 			initHyperLogLog(&sss->abbr_card, 10);
1988 			initHyperLogLog(&sss->full_card, 10);
1989 			ssup->abbrev_full_comparator = ssup->comparator;
1990 			ssup->comparator = varstrcmp_abbrev;
1991 			ssup->abbrev_converter = varstr_abbrev_convert;
1992 			ssup->abbrev_abort = varstr_abbrev_abort;
1993 		}
1994 	}
1995 }
1996 
1997 /*
1998  * sortsupport comparison func (for C locale case)
1999  */
2000 static int
varstrfastcmp_c(Datum x,Datum y,SortSupport ssup)2001 varstrfastcmp_c(Datum x, Datum y, SortSupport ssup)
2002 {
2003 	VarString  *arg1 = DatumGetVarStringPP(x);
2004 	VarString  *arg2 = DatumGetVarStringPP(y);
2005 	char	   *a1p,
2006 			   *a2p;
2007 	int			len1,
2008 				len2,
2009 				result;
2010 
2011 	a1p = VARDATA_ANY(arg1);
2012 	a2p = VARDATA_ANY(arg2);
2013 
2014 	len1 = VARSIZE_ANY_EXHDR(arg1);
2015 	len2 = VARSIZE_ANY_EXHDR(arg2);
2016 
2017 	result = memcmp(a1p, a2p, Min(len1, len2));
2018 	if ((result == 0) && (len1 != len2))
2019 		result = (len1 < len2) ? -1 : 1;
2020 
2021 	/* We can't afford to leak memory here. */
2022 	if (PointerGetDatum(arg1) != x)
2023 		pfree(arg1);
2024 	if (PointerGetDatum(arg2) != y)
2025 		pfree(arg2);
2026 
2027 	return result;
2028 }
2029 
2030 /*
2031  * sortsupport comparison func (for BpChar C locale case)
2032  *
2033  * BpChar outsources its sortsupport to this module.  Specialization for the
2034  * varstr_sortsupport BpChar case, modeled on
2035  * internal_bpchar_pattern_compare().
2036  */
2037 static int
bpcharfastcmp_c(Datum x,Datum y,SortSupport ssup)2038 bpcharfastcmp_c(Datum x, Datum y, SortSupport ssup)
2039 {
2040 	BpChar	   *arg1 = DatumGetBpCharPP(x);
2041 	BpChar	   *arg2 = DatumGetBpCharPP(y);
2042 	char	   *a1p,
2043 			   *a2p;
2044 	int			len1,
2045 				len2,
2046 				result;
2047 
2048 	a1p = VARDATA_ANY(arg1);
2049 	a2p = VARDATA_ANY(arg2);
2050 
2051 	len1 = bpchartruelen(a1p, VARSIZE_ANY_EXHDR(arg1));
2052 	len2 = bpchartruelen(a2p, VARSIZE_ANY_EXHDR(arg2));
2053 
2054 	result = memcmp(a1p, a2p, Min(len1, len2));
2055 	if ((result == 0) && (len1 != len2))
2056 		result = (len1 < len2) ? -1 : 1;
2057 
2058 	/* We can't afford to leak memory here. */
2059 	if (PointerGetDatum(arg1) != x)
2060 		pfree(arg1);
2061 	if (PointerGetDatum(arg2) != y)
2062 		pfree(arg2);
2063 
2064 	return result;
2065 }
2066 
2067 /*
2068  * sortsupport comparison func (for locale case)
2069  */
2070 static int
varstrfastcmp_locale(Datum x,Datum y,SortSupport ssup)2071 varstrfastcmp_locale(Datum x, Datum y, SortSupport ssup)
2072 {
2073 	VarString  *arg1 = DatumGetVarStringPP(x);
2074 	VarString  *arg2 = DatumGetVarStringPP(y);
2075 	bool		arg1_match;
2076 	VarStringSortSupport *sss = (VarStringSortSupport *) ssup->ssup_extra;
2077 
2078 	/* working state */
2079 	char	   *a1p,
2080 			   *a2p;
2081 	int			len1,
2082 				len2,
2083 				result;
2084 
2085 	a1p = VARDATA_ANY(arg1);
2086 	a2p = VARDATA_ANY(arg2);
2087 
2088 	len1 = VARSIZE_ANY_EXHDR(arg1);
2089 	len2 = VARSIZE_ANY_EXHDR(arg2);
2090 
2091 	/* Fast pre-check for equality, as discussed in varstr_cmp() */
2092 	if (len1 == len2 && memcmp(a1p, a2p, len1) == 0)
2093 	{
2094 		/*
2095 		 * No change in buf1 or buf2 contents, so avoid changing last_len1 or
2096 		 * last_len2.  Existing contents of buffers might still be used by
2097 		 * next call.
2098 		 *
2099 		 * It's fine to allow the comparison of BpChar padding bytes here,
2100 		 * even though that implies that the memcmp() will usually be
2101 		 * performed for BpChar callers (though multibyte characters could
2102 		 * still prevent that from occurring).  The memcmp() is still very
2103 		 * cheap, and BpChar's funny semantics have us remove trailing spaces
2104 		 * (not limited to padding), so we need make no distinction between
2105 		 * padding space characters and "real" space characters.
2106 		 */
2107 		result = 0;
2108 		goto done;
2109 	}
2110 
2111 	if (sss->bpchar)
2112 	{
2113 		/* Get true number of bytes, ignoring trailing spaces */
2114 		len1 = bpchartruelen(a1p, len1);
2115 		len2 = bpchartruelen(a2p, len2);
2116 	}
2117 
2118 	if (len1 >= sss->buflen1)
2119 	{
2120 		pfree(sss->buf1);
2121 		sss->buflen1 = Max(len1 + 1, Min(sss->buflen1 * 2, MaxAllocSize));
2122 		sss->buf1 = MemoryContextAlloc(ssup->ssup_cxt, sss->buflen1);
2123 	}
2124 	if (len2 >= sss->buflen2)
2125 	{
2126 		pfree(sss->buf2);
2127 		sss->buflen2 = Max(len2 + 1, Min(sss->buflen2 * 2, MaxAllocSize));
2128 		sss->buf2 = MemoryContextAlloc(ssup->ssup_cxt, sss->buflen2);
2129 	}
2130 
2131 	/*
2132 	 * We're likely to be asked to compare the same strings repeatedly, and
2133 	 * memcmp() is so much cheaper than strcoll() that it pays to try to cache
2134 	 * comparisons, even though in general there is no reason to think that
2135 	 * that will work out (every string datum may be unique).  Caching does
2136 	 * not slow things down measurably when it doesn't work out, and can speed
2137 	 * things up by rather a lot when it does.  In part, this is because the
2138 	 * memcmp() compares data from cachelines that are needed in L1 cache even
2139 	 * when the last comparison's result cannot be reused.
2140 	 */
2141 	arg1_match = true;
2142 	if (len1 != sss->last_len1 || memcmp(sss->buf1, a1p, len1) != 0)
2143 	{
2144 		arg1_match = false;
2145 		memcpy(sss->buf1, a1p, len1);
2146 		sss->buf1[len1] = '\0';
2147 		sss->last_len1 = len1;
2148 	}
2149 
2150 	/*
2151 	 * If we're comparing the same two strings as last time, we can return the
2152 	 * same answer without calling strcoll() again.  This is more likely than
2153 	 * it seems (at least with moderate to low cardinality sets), because
2154 	 * quicksort compares the same pivot against many values.
2155 	 */
2156 	if (len2 != sss->last_len2 || memcmp(sss->buf2, a2p, len2) != 0)
2157 	{
2158 		memcpy(sss->buf2, a2p, len2);
2159 		sss->buf2[len2] = '\0';
2160 		sss->last_len2 = len2;
2161 	}
2162 	else if (arg1_match && !sss->cache_blob)
2163 	{
2164 		/* Use result cached following last actual strcoll() call */
2165 		result = sss->last_returned;
2166 		goto done;
2167 	}
2168 
2169 	if (sss->locale)
2170 	{
2171 		if (sss->locale->provider == COLLPROVIDER_ICU)
2172 		{
2173 #ifdef USE_ICU
2174 #ifdef HAVE_UCOL_STRCOLLUTF8
2175 			if (GetDatabaseEncoding() == PG_UTF8)
2176 			{
2177 				UErrorCode	status;
2178 
2179 				status = U_ZERO_ERROR;
2180 				result = ucol_strcollUTF8(sss->locale->info.icu.ucol,
2181 										  a1p, len1,
2182 										  a2p, len2,
2183 										  &status);
2184 				if (U_FAILURE(status))
2185 					ereport(ERROR,
2186 							(errmsg("collation failed: %s", u_errorName(status))));
2187 			}
2188 			else
2189 #endif
2190 			{
2191 				int32_t		ulen1,
2192 							ulen2;
2193 				UChar	   *uchar1,
2194 						   *uchar2;
2195 
2196 				ulen1 = icu_to_uchar(&uchar1, a1p, len1);
2197 				ulen2 = icu_to_uchar(&uchar2, a2p, len2);
2198 
2199 				result = ucol_strcoll(sss->locale->info.icu.ucol,
2200 									  uchar1, ulen1,
2201 									  uchar2, ulen2);
2202 
2203 				pfree(uchar1);
2204 				pfree(uchar2);
2205 			}
2206 #else							/* not USE_ICU */
2207 			/* shouldn't happen */
2208 			elog(ERROR, "unsupported collprovider: %c", sss->locale->provider);
2209 #endif							/* not USE_ICU */
2210 		}
2211 		else
2212 		{
2213 #ifdef HAVE_LOCALE_T
2214 			result = strcoll_l(sss->buf1, sss->buf2, sss->locale->info.lt);
2215 #else
2216 			/* shouldn't happen */
2217 			elog(ERROR, "unsupported collprovider: %c", sss->locale->provider);
2218 #endif
2219 		}
2220 	}
2221 	else
2222 		result = strcoll(sss->buf1, sss->buf2);
2223 
2224 	/*
2225 	 * In some locales strcoll() can claim that nonidentical strings are
2226 	 * equal. Believing that would be bad news for a number of reasons, so we
2227 	 * follow Perl's lead and sort "equal" strings according to strcmp().
2228 	 */
2229 	if (result == 0)
2230 		result = strcmp(sss->buf1, sss->buf2);
2231 
2232 	/* Cache result, perhaps saving an expensive strcoll() call next time */
2233 	sss->cache_blob = false;
2234 	sss->last_returned = result;
2235 done:
2236 	/* We can't afford to leak memory here. */
2237 	if (PointerGetDatum(arg1) != x)
2238 		pfree(arg1);
2239 	if (PointerGetDatum(arg2) != y)
2240 		pfree(arg2);
2241 
2242 	return result;
2243 }
2244 
2245 /*
2246  * Abbreviated key comparison func
2247  */
2248 static int
varstrcmp_abbrev(Datum x,Datum y,SortSupport ssup)2249 varstrcmp_abbrev(Datum x, Datum y, SortSupport ssup)
2250 {
2251 	/*
2252 	 * When 0 is returned, the core system will call varstrfastcmp_c()
2253 	 * (bpcharfastcmp_c() in BpChar case) or varstrfastcmp_locale().  Even a
2254 	 * strcmp() on two non-truncated strxfrm() blobs cannot indicate *equality*
2255 	 * authoritatively, for the same reason that there is a strcoll()
2256 	 * tie-breaker call to strcmp() in varstr_cmp().
2257 	 */
2258 	if (x > y)
2259 		return 1;
2260 	else if (x == y)
2261 		return 0;
2262 	else
2263 		return -1;
2264 }
2265 
2266 /*
2267  * Conversion routine for sortsupport.  Converts original to abbreviated key
2268  * representation.  Our encoding strategy is simple -- pack the first 8 bytes
2269  * of a strxfrm() blob into a Datum (on little-endian machines, the 8 bytes are
2270  * stored in reverse order), and treat it as an unsigned integer.  When the "C"
2271  * locale is used, or in case of bytea, just memcpy() from original instead.
2272  */
2273 static Datum
varstr_abbrev_convert(Datum original,SortSupport ssup)2274 varstr_abbrev_convert(Datum original, SortSupport ssup)
2275 {
2276 	VarStringSortSupport *sss = (VarStringSortSupport *) ssup->ssup_extra;
2277 	VarString  *authoritative = DatumGetVarStringPP(original);
2278 	char	   *authoritative_data = VARDATA_ANY(authoritative);
2279 
2280 	/* working state */
2281 	Datum		res;
2282 	char	   *pres;
2283 	int			len;
2284 	uint32		hash;
2285 
2286 	pres = (char *) &res;
2287 	/* memset(), so any non-overwritten bytes are NUL */
2288 	memset(pres, 0, sizeof(Datum));
2289 	len = VARSIZE_ANY_EXHDR(authoritative);
2290 
2291 	/* Get number of bytes, ignoring trailing spaces */
2292 	if (sss->bpchar)
2293 		len = bpchartruelen(authoritative_data, len);
2294 
2295 	/*
2296 	 * If we're using the C collation, use memcpy(), rather than strxfrm(), to
2297 	 * abbreviate keys.  The full comparator for the C locale is always
2298 	 * memcmp().  It would be incorrect to allow bytea callers (callers that
2299 	 * always force the C collation -- bytea isn't a collatable type, but this
2300 	 * approach is convenient) to use strxfrm().  This is because bytea
2301 	 * strings may contain NUL bytes.  Besides, this should be faster, too.
2302 	 *
2303 	 * More generally, it's okay that bytea callers can have NUL bytes in
2304 	 * strings because varstrcmp_abbrev() need not make a distinction between
2305 	 * terminating NUL bytes, and NUL bytes representing actual NULs in the
2306 	 * authoritative representation.  Hopefully a comparison at or past one
2307 	 * abbreviated key's terminating NUL byte will resolve the comparison
2308 	 * without consulting the authoritative representation; specifically, some
2309 	 * later non-NUL byte in the longer string can resolve the comparison
2310 	 * against a subsequent terminating NUL in the shorter string.  There will
2311 	 * usually be what is effectively a "length-wise" resolution there and
2312 	 * then.
2313 	 *
2314 	 * If that doesn't work out -- if all bytes in the longer string
2315 	 * positioned at or past the offset of the smaller string's (first)
2316 	 * terminating NUL are actually representative of NUL bytes in the
2317 	 * authoritative binary string (perhaps with some *terminating* NUL bytes
2318 	 * towards the end of the longer string iff it happens to still be small)
2319 	 * -- then an authoritative tie-breaker will happen, and do the right
2320 	 * thing: explicitly consider string length.
2321 	 */
2322 	if (sss->collate_c)
2323 		memcpy(pres, authoritative_data, Min(len, sizeof(Datum)));
2324 	else
2325 	{
2326 		Size		bsize;
2327 #ifdef USE_ICU
2328 		int32_t		ulen = -1;
2329 		UChar	   *uchar = NULL;
2330 #endif
2331 
2332 		/*
2333 		 * We're not using the C collation, so fall back on strxfrm or ICU
2334 		 * analogs.
2335 		 */
2336 
2337 		/* By convention, we use buffer 1 to store and NUL-terminate */
2338 		if (len >= sss->buflen1)
2339 		{
2340 			pfree(sss->buf1);
2341 			sss->buflen1 = Max(len + 1, Min(sss->buflen1 * 2, MaxAllocSize));
2342 			sss->buf1 = palloc(sss->buflen1);
2343 		}
2344 
2345 		/* Might be able to reuse strxfrm() blob from last call */
2346 		if (sss->last_len1 == len && sss->cache_blob &&
2347 			memcmp(sss->buf1, authoritative_data, len) == 0)
2348 		{
2349 			memcpy(pres, sss->buf2, Min(sizeof(Datum), sss->last_len2));
2350 			/* No change affecting cardinality, so no hashing required */
2351 			goto done;
2352 		}
2353 
2354 		memcpy(sss->buf1, authoritative_data, len);
2355 
2356 		/*
2357 		 * Just like strcoll(), strxfrm() expects a NUL-terminated string. Not
2358 		 * necessary for ICU, but doesn't hurt.
2359 		 */
2360 		sss->buf1[len] = '\0';
2361 		sss->last_len1 = len;
2362 
2363 #ifdef USE_ICU
2364 		/* When using ICU and not UTF8, convert string to UChar. */
2365 		if (sss->locale && sss->locale->provider == COLLPROVIDER_ICU &&
2366 			GetDatabaseEncoding() != PG_UTF8)
2367 			ulen = icu_to_uchar(&uchar, sss->buf1, len);
2368 #endif
2369 
2370 		/*
2371 		 * Loop: Call strxfrm() or ucol_getSortKey(), possibly enlarge buffer,
2372 		 * and try again.  Both of these functions have the result buffer
2373 		 * content undefined if the result did not fit, so we need to retry
2374 		 * until everything fits, even though we only need the first few bytes
2375 		 * in the end.  When using ucol_nextSortKeyPart(), however, we only
2376 		 * ask for as many bytes as we actually need.
2377 		 */
2378 		for (;;)
2379 		{
2380 #ifdef USE_ICU
2381 			if (sss->locale && sss->locale->provider == COLLPROVIDER_ICU)
2382 			{
2383 				/*
2384 				 * When using UTF8, use the iteration interface so we only
2385 				 * need to produce as many bytes as we actually need.
2386 				 */
2387 				if (GetDatabaseEncoding() == PG_UTF8)
2388 				{
2389 					UCharIterator iter;
2390 					uint32_t	state[2];
2391 					UErrorCode	status;
2392 
2393 					uiter_setUTF8(&iter, sss->buf1, len);
2394 					state[0] = state[1] = 0;	/* won't need that again */
2395 					status = U_ZERO_ERROR;
2396 					bsize = ucol_nextSortKeyPart(sss->locale->info.icu.ucol,
2397 												 &iter,
2398 												 state,
2399 												 (uint8_t *) sss->buf2,
2400 												 Min(sizeof(Datum), sss->buflen2),
2401 												 &status);
2402 					if (U_FAILURE(status))
2403 						ereport(ERROR,
2404 								(errmsg("sort key generation failed: %s",
2405 										u_errorName(status))));
2406 				}
2407 				else
2408 					bsize = ucol_getSortKey(sss->locale->info.icu.ucol,
2409 											uchar, ulen,
2410 											(uint8_t *) sss->buf2, sss->buflen2);
2411 			}
2412 			else
2413 #endif
2414 #ifdef HAVE_LOCALE_T
2415 			if (sss->locale && sss->locale->provider == COLLPROVIDER_LIBC)
2416 				bsize = strxfrm_l(sss->buf2, sss->buf1,
2417 								  sss->buflen2, sss->locale->info.lt);
2418 			else
2419 #endif
2420 				bsize = strxfrm(sss->buf2, sss->buf1, sss->buflen2);
2421 
2422 			sss->last_len2 = bsize;
2423 			if (bsize < sss->buflen2)
2424 				break;
2425 
2426 			/*
2427 			 * Grow buffer and retry.
2428 			 */
2429 			pfree(sss->buf2);
2430 			sss->buflen2 = Max(bsize + 1,
2431 							   Min(sss->buflen2 * 2, MaxAllocSize));
2432 			sss->buf2 = palloc(sss->buflen2);
2433 		}
2434 
2435 		/*
2436 		 * Every Datum byte is always compared.  This is safe because the
2437 		 * strxfrm() blob is itself NUL terminated, leaving no danger of
2438 		 * misinterpreting any NUL bytes not intended to be interpreted as
2439 		 * logically representing termination.
2440 		 *
2441 		 * (Actually, even if there were NUL bytes in the blob it would be
2442 		 * okay.  See remarks on bytea case above.)
2443 		 */
2444 		memcpy(pres, sss->buf2, Min(sizeof(Datum), bsize));
2445 
2446 #ifdef USE_ICU
2447 		if (uchar)
2448 			pfree(uchar);
2449 #endif
2450 	}
2451 
2452 	/*
2453 	 * Maintain approximate cardinality of both abbreviated keys and original,
2454 	 * authoritative keys using HyperLogLog.  Used as cheap insurance against
2455 	 * the worst case, where we do many string transformations for no saving
2456 	 * in full strcoll()-based comparisons.  These statistics are used by
2457 	 * varstr_abbrev_abort().
2458 	 *
2459 	 * First, Hash key proper, or a significant fraction of it.  Mix in length
2460 	 * in order to compensate for cases where differences are past
2461 	 * PG_CACHE_LINE_SIZE bytes, so as to limit the overhead of hashing.
2462 	 */
2463 	hash = DatumGetUInt32(hash_any((unsigned char *) authoritative_data,
2464 								   Min(len, PG_CACHE_LINE_SIZE)));
2465 
2466 	if (len > PG_CACHE_LINE_SIZE)
2467 		hash ^= DatumGetUInt32(hash_uint32((uint32) len));
2468 
2469 	addHyperLogLog(&sss->full_card, hash);
2470 
2471 	/* Hash abbreviated key */
2472 #if SIZEOF_DATUM == 8
2473 	{
2474 		uint32		lohalf,
2475 					hihalf;
2476 
2477 		lohalf = (uint32) res;
2478 		hihalf = (uint32) (res >> 32);
2479 		hash = DatumGetUInt32(hash_uint32(lohalf ^ hihalf));
2480 	}
2481 #else							/* SIZEOF_DATUM != 8 */
2482 	hash = DatumGetUInt32(hash_uint32((uint32) res));
2483 #endif
2484 
2485 	addHyperLogLog(&sss->abbr_card, hash);
2486 
2487 	/* Cache result, perhaps saving an expensive strxfrm() call next time */
2488 	sss->cache_blob = true;
2489 done:
2490 
2491 	/*
2492 	 * Byteswap on little-endian machines.
2493 	 *
2494 	 * This is needed so that varstrcmp_abbrev() (an unsigned integer 3-way
2495 	 * comparator) works correctly on all platforms.  If we didn't do this,
2496 	 * the comparator would have to call memcmp() with a pair of pointers to
2497 	 * the first byte of each abbreviated key, which is slower.
2498 	 */
2499 	res = DatumBigEndianToNative(res);
2500 
2501 	/* Don't leak memory here */
2502 	if (PointerGetDatum(authoritative) != original)
2503 		pfree(authoritative);
2504 
2505 	return res;
2506 }
2507 
2508 /*
2509  * Callback for estimating effectiveness of abbreviated key optimization, using
2510  * heuristic rules.  Returns value indicating if the abbreviation optimization
2511  * should be aborted, based on its projected effectiveness.
2512  */
2513 static bool
varstr_abbrev_abort(int memtupcount,SortSupport ssup)2514 varstr_abbrev_abort(int memtupcount, SortSupport ssup)
2515 {
2516 	VarStringSortSupport *sss = (VarStringSortSupport *) ssup->ssup_extra;
2517 	double		abbrev_distinct,
2518 				key_distinct;
2519 
2520 	Assert(ssup->abbreviate);
2521 
2522 	/* Have a little patience */
2523 	if (memtupcount < 100)
2524 		return false;
2525 
2526 	abbrev_distinct = estimateHyperLogLog(&sss->abbr_card);
2527 	key_distinct = estimateHyperLogLog(&sss->full_card);
2528 
2529 	/*
2530 	 * Clamp cardinality estimates to at least one distinct value.  While
2531 	 * NULLs are generally disregarded, if only NULL values were seen so far,
2532 	 * that might misrepresent costs if we failed to clamp.
2533 	 */
2534 	if (abbrev_distinct <= 1.0)
2535 		abbrev_distinct = 1.0;
2536 
2537 	if (key_distinct <= 1.0)
2538 		key_distinct = 1.0;
2539 
2540 	/*
2541 	 * In the worst case all abbreviated keys are identical, while at the same
2542 	 * time there are differences within full key strings not captured in
2543 	 * abbreviations.
2544 	 */
2545 #ifdef TRACE_SORT
2546 	if (trace_sort)
2547 	{
2548 		double		norm_abbrev_card = abbrev_distinct / (double) memtupcount;
2549 
2550 		elog(LOG, "varstr_abbrev: abbrev_distinct after %d: %f "
2551 			 "(key_distinct: %f, norm_abbrev_card: %f, prop_card: %f)",
2552 			 memtupcount, abbrev_distinct, key_distinct, norm_abbrev_card,
2553 			 sss->prop_card);
2554 	}
2555 #endif
2556 
2557 	/*
2558 	 * If the number of distinct abbreviated keys approximately matches the
2559 	 * number of distinct authoritative original keys, that's reason enough to
2560 	 * proceed.  We can win even with a very low cardinality set if most
2561 	 * tie-breakers only memcmp().  This is by far the most important
2562 	 * consideration.
2563 	 *
2564 	 * While comparisons that are resolved at the abbreviated key level are
2565 	 * considerably cheaper than tie-breakers resolved with memcmp(), both of
2566 	 * those two outcomes are so much cheaper than a full strcoll() once
2567 	 * sorting is underway that it doesn't seem worth it to weigh abbreviated
2568 	 * cardinality against the overall size of the set in order to more
2569 	 * accurately model costs.  Assume that an abbreviated comparison, and an
2570 	 * abbreviated comparison with a cheap memcmp()-based authoritative
2571 	 * resolution are equivalent.
2572 	 */
2573 	if (abbrev_distinct > key_distinct * sss->prop_card)
2574 	{
2575 		/*
2576 		 * When we have exceeded 10,000 tuples, decay required cardinality
2577 		 * aggressively for next call.
2578 		 *
2579 		 * This is useful because the number of comparisons required on
2580 		 * average increases at a linearithmic rate, and at roughly 10,000
2581 		 * tuples that factor will start to dominate over the linear costs of
2582 		 * string transformation (this is a conservative estimate).  The decay
2583 		 * rate is chosen to be a little less aggressive than halving -- which
2584 		 * (since we're called at points at which memtupcount has doubled)
2585 		 * would never see the cost model actually abort past the first call
2586 		 * following a decay.  This decay rate is mostly a precaution against
2587 		 * a sudden, violent swing in how well abbreviated cardinality tracks
2588 		 * full key cardinality.  The decay also serves to prevent a marginal
2589 		 * case from being aborted too late, when too much has already been
2590 		 * invested in string transformation.
2591 		 *
2592 		 * It's possible for sets of several million distinct strings with
2593 		 * mere tens of thousands of distinct abbreviated keys to still
2594 		 * benefit very significantly.  This will generally occur provided
2595 		 * each abbreviated key is a proxy for a roughly uniform number of the
2596 		 * set's full keys. If it isn't so, we hope to catch that early and
2597 		 * abort.  If it isn't caught early, by the time the problem is
2598 		 * apparent it's probably not worth aborting.
2599 		 */
2600 		if (memtupcount > 10000)
2601 			sss->prop_card *= 0.65;
2602 
2603 		return false;
2604 	}
2605 
2606 	/*
2607 	 * Abort abbreviation strategy.
2608 	 *
2609 	 * The worst case, where all abbreviated keys are identical while all
2610 	 * original strings differ will typically only see a regression of about
2611 	 * 10% in execution time for small to medium sized lists of strings.
2612 	 * Whereas on modern CPUs where cache stalls are the dominant cost, we can
2613 	 * often expect very large improvements, particularly with sets of strings
2614 	 * of moderately high to high abbreviated cardinality.  There is little to
2615 	 * lose but much to gain, which our strategy reflects.
2616 	 */
2617 #ifdef TRACE_SORT
2618 	if (trace_sort)
2619 		elog(LOG, "varstr_abbrev: aborted abbreviation at %d "
2620 			 "(abbrev_distinct: %f, key_distinct: %f, prop_card: %f)",
2621 			 memtupcount, abbrev_distinct, key_distinct, sss->prop_card);
2622 #endif
2623 
2624 	return true;
2625 }
2626 
2627 Datum
text_larger(PG_FUNCTION_ARGS)2628 text_larger(PG_FUNCTION_ARGS)
2629 {
2630 	text	   *arg1 = PG_GETARG_TEXT_PP(0);
2631 	text	   *arg2 = PG_GETARG_TEXT_PP(1);
2632 	text	   *result;
2633 
2634 	result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0) ? arg1 : arg2);
2635 
2636 	PG_RETURN_TEXT_P(result);
2637 }
2638 
2639 Datum
text_smaller(PG_FUNCTION_ARGS)2640 text_smaller(PG_FUNCTION_ARGS)
2641 {
2642 	text	   *arg1 = PG_GETARG_TEXT_PP(0);
2643 	text	   *arg2 = PG_GETARG_TEXT_PP(1);
2644 	text	   *result;
2645 
2646 	result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0) ? arg1 : arg2);
2647 
2648 	PG_RETURN_TEXT_P(result);
2649 }
2650 
2651 
2652 /*
2653  * The following operators support character-by-character comparison
2654  * of text datums, to allow building indexes suitable for LIKE clauses.
2655  * Note that the regular texteq/textne comparison operators, and regular
2656  * support functions 1 and 2 with "C" collation are assumed to be
2657  * compatible with these!
2658  */
2659 
2660 static int
internal_text_pattern_compare(text * arg1,text * arg2)2661 internal_text_pattern_compare(text *arg1, text *arg2)
2662 {
2663 	int			result;
2664 	int			len1,
2665 				len2;
2666 
2667 	len1 = VARSIZE_ANY_EXHDR(arg1);
2668 	len2 = VARSIZE_ANY_EXHDR(arg2);
2669 
2670 	result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
2671 	if (result != 0)
2672 		return result;
2673 	else if (len1 < len2)
2674 		return -1;
2675 	else if (len1 > len2)
2676 		return 1;
2677 	else
2678 		return 0;
2679 }
2680 
2681 
2682 Datum
text_pattern_lt(PG_FUNCTION_ARGS)2683 text_pattern_lt(PG_FUNCTION_ARGS)
2684 {
2685 	text	   *arg1 = PG_GETARG_TEXT_PP(0);
2686 	text	   *arg2 = PG_GETARG_TEXT_PP(1);
2687 	int			result;
2688 
2689 	result = internal_text_pattern_compare(arg1, arg2);
2690 
2691 	PG_FREE_IF_COPY(arg1, 0);
2692 	PG_FREE_IF_COPY(arg2, 1);
2693 
2694 	PG_RETURN_BOOL(result < 0);
2695 }
2696 
2697 
2698 Datum
text_pattern_le(PG_FUNCTION_ARGS)2699 text_pattern_le(PG_FUNCTION_ARGS)
2700 {
2701 	text	   *arg1 = PG_GETARG_TEXT_PP(0);
2702 	text	   *arg2 = PG_GETARG_TEXT_PP(1);
2703 	int			result;
2704 
2705 	result = internal_text_pattern_compare(arg1, arg2);
2706 
2707 	PG_FREE_IF_COPY(arg1, 0);
2708 	PG_FREE_IF_COPY(arg2, 1);
2709 
2710 	PG_RETURN_BOOL(result <= 0);
2711 }
2712 
2713 
2714 Datum
text_pattern_ge(PG_FUNCTION_ARGS)2715 text_pattern_ge(PG_FUNCTION_ARGS)
2716 {
2717 	text	   *arg1 = PG_GETARG_TEXT_PP(0);
2718 	text	   *arg2 = PG_GETARG_TEXT_PP(1);
2719 	int			result;
2720 
2721 	result = internal_text_pattern_compare(arg1, arg2);
2722 
2723 	PG_FREE_IF_COPY(arg1, 0);
2724 	PG_FREE_IF_COPY(arg2, 1);
2725 
2726 	PG_RETURN_BOOL(result >= 0);
2727 }
2728 
2729 
2730 Datum
text_pattern_gt(PG_FUNCTION_ARGS)2731 text_pattern_gt(PG_FUNCTION_ARGS)
2732 {
2733 	text	   *arg1 = PG_GETARG_TEXT_PP(0);
2734 	text	   *arg2 = PG_GETARG_TEXT_PP(1);
2735 	int			result;
2736 
2737 	result = internal_text_pattern_compare(arg1, arg2);
2738 
2739 	PG_FREE_IF_COPY(arg1, 0);
2740 	PG_FREE_IF_COPY(arg2, 1);
2741 
2742 	PG_RETURN_BOOL(result > 0);
2743 }
2744 
2745 
2746 Datum
bttext_pattern_cmp(PG_FUNCTION_ARGS)2747 bttext_pattern_cmp(PG_FUNCTION_ARGS)
2748 {
2749 	text	   *arg1 = PG_GETARG_TEXT_PP(0);
2750 	text	   *arg2 = PG_GETARG_TEXT_PP(1);
2751 	int			result;
2752 
2753 	result = internal_text_pattern_compare(arg1, arg2);
2754 
2755 	PG_FREE_IF_COPY(arg1, 0);
2756 	PG_FREE_IF_COPY(arg2, 1);
2757 
2758 	PG_RETURN_INT32(result);
2759 }
2760 
2761 
2762 Datum
bttext_pattern_sortsupport(PG_FUNCTION_ARGS)2763 bttext_pattern_sortsupport(PG_FUNCTION_ARGS)
2764 {
2765 	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
2766 	MemoryContext oldcontext;
2767 
2768 	oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
2769 
2770 	/* Use generic string SortSupport, forcing "C" collation */
2771 	varstr_sortsupport(ssup, C_COLLATION_OID, false);
2772 
2773 	MemoryContextSwitchTo(oldcontext);
2774 
2775 	PG_RETURN_VOID();
2776 }
2777 
2778 
2779 /*-------------------------------------------------------------
2780  * byteaoctetlen
2781  *
2782  * get the number of bytes contained in an instance of type 'bytea'
2783  *-------------------------------------------------------------
2784  */
2785 Datum
byteaoctetlen(PG_FUNCTION_ARGS)2786 byteaoctetlen(PG_FUNCTION_ARGS)
2787 {
2788 	Datum		str = PG_GETARG_DATUM(0);
2789 
2790 	/* We need not detoast the input at all */
2791 	PG_RETURN_INT32(toast_raw_datum_size(str) - VARHDRSZ);
2792 }
2793 
2794 /*
2795  * byteacat -
2796  *	  takes two bytea* and returns a bytea* that is the concatenation of
2797  *	  the two.
2798  *
2799  * Cloned from textcat and modified as required.
2800  */
2801 Datum
byteacat(PG_FUNCTION_ARGS)2802 byteacat(PG_FUNCTION_ARGS)
2803 {
2804 	bytea	   *t1 = PG_GETARG_BYTEA_PP(0);
2805 	bytea	   *t2 = PG_GETARG_BYTEA_PP(1);
2806 
2807 	PG_RETURN_BYTEA_P(bytea_catenate(t1, t2));
2808 }
2809 
2810 /*
2811  * bytea_catenate
2812  *	Guts of byteacat(), broken out so it can be used by other functions
2813  *
2814  * Arguments can be in short-header form, but not compressed or out-of-line
2815  */
2816 static bytea *
bytea_catenate(bytea * t1,bytea * t2)2817 bytea_catenate(bytea *t1, bytea *t2)
2818 {
2819 	bytea	   *result;
2820 	int			len1,
2821 				len2,
2822 				len;
2823 	char	   *ptr;
2824 
2825 	len1 = VARSIZE_ANY_EXHDR(t1);
2826 	len2 = VARSIZE_ANY_EXHDR(t2);
2827 
2828 	/* paranoia ... probably should throw error instead? */
2829 	if (len1 < 0)
2830 		len1 = 0;
2831 	if (len2 < 0)
2832 		len2 = 0;
2833 
2834 	len = len1 + len2 + VARHDRSZ;
2835 	result = (bytea *) palloc(len);
2836 
2837 	/* Set size of result string... */
2838 	SET_VARSIZE(result, len);
2839 
2840 	/* Fill data field of result string... */
2841 	ptr = VARDATA(result);
2842 	if (len1 > 0)
2843 		memcpy(ptr, VARDATA_ANY(t1), len1);
2844 	if (len2 > 0)
2845 		memcpy(ptr + len1, VARDATA_ANY(t2), len2);
2846 
2847 	return result;
2848 }
2849 
2850 #define PG_STR_GET_BYTEA(str_) \
2851 	DatumGetByteaPP(DirectFunctionCall1(byteain, CStringGetDatum(str_)))
2852 
2853 /*
2854  * bytea_substr()
2855  * Return a substring starting at the specified position.
2856  * Cloned from text_substr and modified as required.
2857  *
2858  * Input:
2859  *	- string
2860  *	- starting position (is one-based)
2861  *	- string length (optional)
2862  *
2863  * If the starting position is zero or less, then return from the start of the string
2864  * adjusting the length to be consistent with the "negative start" per SQL.
2865  * If the length is less than zero, an ERROR is thrown. If no third argument
2866  * (length) is provided, the length to the end of the string is assumed.
2867  */
2868 Datum
bytea_substr(PG_FUNCTION_ARGS)2869 bytea_substr(PG_FUNCTION_ARGS)
2870 {
2871 	PG_RETURN_BYTEA_P(bytea_substring(PG_GETARG_DATUM(0),
2872 									  PG_GETARG_INT32(1),
2873 									  PG_GETARG_INT32(2),
2874 									  false));
2875 }
2876 
2877 /*
2878  * bytea_substr_no_len -
2879  *	  Wrapper to avoid opr_sanity failure due to
2880  *	  one function accepting a different number of args.
2881  */
2882 Datum
bytea_substr_no_len(PG_FUNCTION_ARGS)2883 bytea_substr_no_len(PG_FUNCTION_ARGS)
2884 {
2885 	PG_RETURN_BYTEA_P(bytea_substring(PG_GETARG_DATUM(0),
2886 									  PG_GETARG_INT32(1),
2887 									  -1,
2888 									  true));
2889 }
2890 
2891 static bytea *
bytea_substring(Datum str,int S,int L,bool length_not_specified)2892 bytea_substring(Datum str,
2893 				int S,
2894 				int L,
2895 				bool length_not_specified)
2896 {
2897 	int32		S1;				/* adjusted start position */
2898 	int32		L1;				/* adjusted substring length */
2899 	int32		E;				/* end position */
2900 
2901 	/*
2902 	 * The logic here should generally match text_substring().
2903 	 */
2904 	S1 = Max(S, 1);
2905 
2906 	if (length_not_specified)
2907 	{
2908 		/*
2909 		 * Not passed a length - DatumGetByteaPSlice() grabs everything to the
2910 		 * end of the string if we pass it a negative value for length.
2911 		 */
2912 		L1 = -1;
2913 	}
2914 	else if (L < 0)
2915 	{
2916 		/* SQL99 says to throw an error for E < S, i.e., negative length */
2917 		ereport(ERROR,
2918 				(errcode(ERRCODE_SUBSTRING_ERROR),
2919 				 errmsg("negative substring length not allowed")));
2920 		L1 = -1;				/* silence stupider compilers */
2921 	}
2922 	else if (pg_add_s32_overflow(S, L, &E))
2923 	{
2924 		/*
2925 		 * L could be large enough for S + L to overflow, in which case the
2926 		 * substring must run to end of string.
2927 		 */
2928 		L1 = -1;
2929 	}
2930 	else
2931 	{
2932 		/*
2933 		 * A zero or negative value for the end position can happen if the
2934 		 * start was negative or one. SQL99 says to return a zero-length
2935 		 * string.
2936 		 */
2937 		if (E < 1)
2938 			return PG_STR_GET_BYTEA("");
2939 
2940 		L1 = E - S1;
2941 	}
2942 
2943 	/*
2944 	 * If the start position is past the end of the string, SQL99 says to
2945 	 * return a zero-length string -- DatumGetByteaPSlice() will do that for
2946 	 * us.  We need only convert S1 to zero-based starting position.
2947 	 */
2948 	return DatumGetByteaPSlice(str, S1 - 1, L1);
2949 }
2950 
2951 /*
2952  * byteaoverlay
2953  *	Replace specified substring of first string with second
2954  *
2955  * The SQL standard defines OVERLAY() in terms of substring and concatenation.
2956  * This code is a direct implementation of what the standard says.
2957  */
2958 Datum
byteaoverlay(PG_FUNCTION_ARGS)2959 byteaoverlay(PG_FUNCTION_ARGS)
2960 {
2961 	bytea	   *t1 = PG_GETARG_BYTEA_PP(0);
2962 	bytea	   *t2 = PG_GETARG_BYTEA_PP(1);
2963 	int			sp = PG_GETARG_INT32(2);	/* substring start position */
2964 	int			sl = PG_GETARG_INT32(3);	/* substring length */
2965 
2966 	PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
2967 }
2968 
2969 Datum
byteaoverlay_no_len(PG_FUNCTION_ARGS)2970 byteaoverlay_no_len(PG_FUNCTION_ARGS)
2971 {
2972 	bytea	   *t1 = PG_GETARG_BYTEA_PP(0);
2973 	bytea	   *t2 = PG_GETARG_BYTEA_PP(1);
2974 	int			sp = PG_GETARG_INT32(2);	/* substring start position */
2975 	int			sl;
2976 
2977 	sl = VARSIZE_ANY_EXHDR(t2); /* defaults to length(t2) */
2978 	PG_RETURN_BYTEA_P(bytea_overlay(t1, t2, sp, sl));
2979 }
2980 
2981 static bytea *
bytea_overlay(bytea * t1,bytea * t2,int sp,int sl)2982 bytea_overlay(bytea *t1, bytea *t2, int sp, int sl)
2983 {
2984 	bytea	   *result;
2985 	bytea	   *s1;
2986 	bytea	   *s2;
2987 	int			sp_pl_sl;
2988 
2989 	/*
2990 	 * Check for possible integer-overflow cases.  For negative sp, throw a
2991 	 * "substring length" error because that's what should be expected
2992 	 * according to the spec's definition of OVERLAY().
2993 	 */
2994 	if (sp <= 0)
2995 		ereport(ERROR,
2996 				(errcode(ERRCODE_SUBSTRING_ERROR),
2997 				 errmsg("negative substring length not allowed")));
2998 	if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
2999 		ereport(ERROR,
3000 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3001 				 errmsg("integer out of range")));
3002 
3003 	s1 = bytea_substring(PointerGetDatum(t1), 1, sp - 1, false);
3004 	s2 = bytea_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
3005 	result = bytea_catenate(s1, t2);
3006 	result = bytea_catenate(result, s2);
3007 
3008 	return result;
3009 }
3010 
3011 /*
3012  * byteapos -
3013  *	  Return the position of the specified substring.
3014  *	  Implements the SQL POSITION() function.
3015  * Cloned from textpos and modified as required.
3016  */
3017 Datum
byteapos(PG_FUNCTION_ARGS)3018 byteapos(PG_FUNCTION_ARGS)
3019 {
3020 	bytea	   *t1 = PG_GETARG_BYTEA_PP(0);
3021 	bytea	   *t2 = PG_GETARG_BYTEA_PP(1);
3022 	int			pos;
3023 	int			px,
3024 				p;
3025 	int			len1,
3026 				len2;
3027 	char	   *p1,
3028 			   *p2;
3029 
3030 	len1 = VARSIZE_ANY_EXHDR(t1);
3031 	len2 = VARSIZE_ANY_EXHDR(t2);
3032 
3033 	if (len2 <= 0)
3034 		PG_RETURN_INT32(1);		/* result for empty pattern */
3035 
3036 	p1 = VARDATA_ANY(t1);
3037 	p2 = VARDATA_ANY(t2);
3038 
3039 	pos = 0;
3040 	px = (len1 - len2);
3041 	for (p = 0; p <= px; p++)
3042 	{
3043 		if ((*p2 == *p1) && (memcmp(p1, p2, len2) == 0))
3044 		{
3045 			pos = p + 1;
3046 			break;
3047 		};
3048 		p1++;
3049 	};
3050 
3051 	PG_RETURN_INT32(pos);
3052 }
3053 
3054 /*-------------------------------------------------------------
3055  * byteaGetByte
3056  *
3057  * this routine treats "bytea" as an array of bytes.
3058  * It returns the Nth byte (a number between 0 and 255).
3059  *-------------------------------------------------------------
3060  */
3061 Datum
byteaGetByte(PG_FUNCTION_ARGS)3062 byteaGetByte(PG_FUNCTION_ARGS)
3063 {
3064 	bytea	   *v = PG_GETARG_BYTEA_PP(0);
3065 	int32		n = PG_GETARG_INT32(1);
3066 	int			len;
3067 	int			byte;
3068 
3069 	len = VARSIZE_ANY_EXHDR(v);
3070 
3071 	if (n < 0 || n >= len)
3072 		ereport(ERROR,
3073 				(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3074 				 errmsg("index %d out of valid range, 0..%d",
3075 						n, len - 1)));
3076 
3077 	byte = ((unsigned char *) VARDATA_ANY(v))[n];
3078 
3079 	PG_RETURN_INT32(byte);
3080 }
3081 
3082 /*-------------------------------------------------------------
3083  * byteaGetBit
3084  *
3085  * This routine treats a "bytea" type like an array of bits.
3086  * It returns the value of the Nth bit (0 or 1).
3087  *
3088  *-------------------------------------------------------------
3089  */
3090 Datum
byteaGetBit(PG_FUNCTION_ARGS)3091 byteaGetBit(PG_FUNCTION_ARGS)
3092 {
3093 	bytea	   *v = PG_GETARG_BYTEA_PP(0);
3094 	int32		n = PG_GETARG_INT32(1);
3095 	int			byteNo,
3096 				bitNo;
3097 	int			len;
3098 	int			byte;
3099 
3100 	len = VARSIZE_ANY_EXHDR(v);
3101 
3102 	/* Do comparison arithmetic in int64 in case len exceeds INT_MAX/8 */
3103 	if (n < 0 || n >= (int64) len * 8)
3104 		ereport(ERROR,
3105 				(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3106 				 errmsg("index %d out of valid range, 0..%d",
3107 						n, (int) Min((int64) len * 8 - 1, INT_MAX))));
3108 
3109 	byteNo = n / 8;
3110 	bitNo = n % 8;
3111 
3112 	byte = ((unsigned char *) VARDATA_ANY(v))[byteNo];
3113 
3114 	if (byte & (1 << bitNo))
3115 		PG_RETURN_INT32(1);
3116 	else
3117 		PG_RETURN_INT32(0);
3118 }
3119 
3120 /*-------------------------------------------------------------
3121  * byteaSetByte
3122  *
3123  * Given an instance of type 'bytea' creates a new one with
3124  * the Nth byte set to the given value.
3125  *
3126  *-------------------------------------------------------------
3127  */
3128 Datum
byteaSetByte(PG_FUNCTION_ARGS)3129 byteaSetByte(PG_FUNCTION_ARGS)
3130 {
3131 	bytea	   *res = PG_GETARG_BYTEA_P_COPY(0);
3132 	int32		n = PG_GETARG_INT32(1);
3133 	int32		newByte = PG_GETARG_INT32(2);
3134 	int			len;
3135 
3136 	len = VARSIZE(res) - VARHDRSZ;
3137 
3138 	if (n < 0 || n >= len)
3139 		ereport(ERROR,
3140 				(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3141 				 errmsg("index %d out of valid range, 0..%d",
3142 						n, len - 1)));
3143 
3144 	/*
3145 	 * Now set the byte.
3146 	 */
3147 	((unsigned char *) VARDATA(res))[n] = newByte;
3148 
3149 	PG_RETURN_BYTEA_P(res);
3150 }
3151 
3152 /*-------------------------------------------------------------
3153  * byteaSetBit
3154  *
3155  * Given an instance of type 'bytea' creates a new one with
3156  * the Nth bit set to the given value.
3157  *
3158  *-------------------------------------------------------------
3159  */
3160 Datum
byteaSetBit(PG_FUNCTION_ARGS)3161 byteaSetBit(PG_FUNCTION_ARGS)
3162 {
3163 	bytea	   *res = PG_GETARG_BYTEA_P_COPY(0);
3164 	int32		n = PG_GETARG_INT32(1);
3165 	int32		newBit = PG_GETARG_INT32(2);
3166 	int			len;
3167 	int			oldByte,
3168 				newByte;
3169 	int			byteNo,
3170 				bitNo;
3171 
3172 	len = VARSIZE(res) - VARHDRSZ;
3173 
3174 	/* Do comparison arithmetic in int64 in case len exceeds INT_MAX/8 */
3175 	if (n < 0 || n >= (int64) len * 8)
3176 		ereport(ERROR,
3177 				(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3178 				 errmsg("index %d out of valid range, 0..%d",
3179 						n, (int) Min((int64) len * 8 - 1, INT_MAX))));
3180 
3181 	byteNo = n / 8;
3182 	bitNo = n % 8;
3183 
3184 	/*
3185 	 * sanity check!
3186 	 */
3187 	if (newBit != 0 && newBit != 1)
3188 		ereport(ERROR,
3189 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3190 				 errmsg("new bit must be 0 or 1")));
3191 
3192 	/*
3193 	 * Update the byte.
3194 	 */
3195 	oldByte = ((unsigned char *) VARDATA(res))[byteNo];
3196 
3197 	if (newBit == 0)
3198 		newByte = oldByte & (~(1 << bitNo));
3199 	else
3200 		newByte = oldByte | (1 << bitNo);
3201 
3202 	((unsigned char *) VARDATA(res))[byteNo] = newByte;
3203 
3204 	PG_RETURN_BYTEA_P(res);
3205 }
3206 
3207 
3208 /* text_name()
3209  * Converts a text type to a Name type.
3210  */
3211 Datum
text_name(PG_FUNCTION_ARGS)3212 text_name(PG_FUNCTION_ARGS)
3213 {
3214 	text	   *s = PG_GETARG_TEXT_PP(0);
3215 	Name		result;
3216 	int			len;
3217 
3218 	len = VARSIZE_ANY_EXHDR(s);
3219 
3220 	/* Truncate oversize input */
3221 	if (len >= NAMEDATALEN)
3222 		len = pg_mbcliplen(VARDATA_ANY(s), len, NAMEDATALEN - 1);
3223 
3224 	/* We use palloc0 here to ensure result is zero-padded */
3225 	result = (Name) palloc0(NAMEDATALEN);
3226 	memcpy(NameStr(*result), VARDATA_ANY(s), len);
3227 
3228 	PG_RETURN_NAME(result);
3229 }
3230 
3231 /* name_text()
3232  * Converts a Name type to a text type.
3233  */
3234 Datum
name_text(PG_FUNCTION_ARGS)3235 name_text(PG_FUNCTION_ARGS)
3236 {
3237 	Name		s = PG_GETARG_NAME(0);
3238 
3239 	PG_RETURN_TEXT_P(cstring_to_text(NameStr(*s)));
3240 }
3241 
3242 
3243 /*
3244  * textToQualifiedNameList - convert a text object to list of names
3245  *
3246  * This implements the input parsing needed by nextval() and other
3247  * functions that take a text parameter representing a qualified name.
3248  * We split the name at dots, downcase if not double-quoted, and
3249  * truncate names if they're too long.
3250  */
3251 List *
textToQualifiedNameList(text * textval)3252 textToQualifiedNameList(text *textval)
3253 {
3254 	char	   *rawname;
3255 	List	   *result = NIL;
3256 	List	   *namelist;
3257 	ListCell   *l;
3258 
3259 	/* Convert to C string (handles possible detoasting). */
3260 	/* Note we rely on being able to modify rawname below. */
3261 	rawname = text_to_cstring(textval);
3262 
3263 	if (!SplitIdentifierString(rawname, '.', &namelist))
3264 		ereport(ERROR,
3265 				(errcode(ERRCODE_INVALID_NAME),
3266 				 errmsg("invalid name syntax")));
3267 
3268 	if (namelist == NIL)
3269 		ereport(ERROR,
3270 				(errcode(ERRCODE_INVALID_NAME),
3271 				 errmsg("invalid name syntax")));
3272 
3273 	foreach(l, namelist)
3274 	{
3275 		char	   *curname = (char *) lfirst(l);
3276 
3277 		result = lappend(result, makeString(pstrdup(curname)));
3278 	}
3279 
3280 	pfree(rawname);
3281 	list_free(namelist);
3282 
3283 	return result;
3284 }
3285 
3286 /*
3287  * SplitIdentifierString --- parse a string containing identifiers
3288  *
3289  * This is the guts of textToQualifiedNameList, and is exported for use in
3290  * other situations such as parsing GUC variables.  In the GUC case, it's
3291  * important to avoid memory leaks, so the API is designed to minimize the
3292  * amount of stuff that needs to be allocated and freed.
3293  *
3294  * Inputs:
3295  *	rawstring: the input string; must be overwritable!	On return, it's
3296  *			   been modified to contain the separated identifiers.
3297  *	separator: the separator punctuation expected between identifiers
3298  *			   (typically '.' or ',').  Whitespace may also appear around
3299  *			   identifiers.
3300  * Outputs:
3301  *	namelist: filled with a palloc'd list of pointers to identifiers within
3302  *			  rawstring.  Caller should list_free() this even on error return.
3303  *
3304  * Returns true if okay, false if there is a syntax error in the string.
3305  *
3306  * Note that an empty string is considered okay here, though not in
3307  * textToQualifiedNameList.
3308  */
3309 bool
SplitIdentifierString(char * rawstring,char separator,List ** namelist)3310 SplitIdentifierString(char *rawstring, char separator,
3311 					  List **namelist)
3312 {
3313 	char	   *nextp = rawstring;
3314 	bool		done = false;
3315 
3316 	*namelist = NIL;
3317 
3318 	while (scanner_isspace(*nextp))
3319 		nextp++;				/* skip leading whitespace */
3320 
3321 	if (*nextp == '\0')
3322 		return true;			/* allow empty string */
3323 
3324 	/* At the top of the loop, we are at start of a new identifier. */
3325 	do
3326 	{
3327 		char	   *curname;
3328 		char	   *endp;
3329 
3330 		if (*nextp == '"')
3331 		{
3332 			/* Quoted name --- collapse quote-quote pairs, no downcasing */
3333 			curname = nextp + 1;
3334 			for (;;)
3335 			{
3336 				endp = strchr(nextp + 1, '"');
3337 				if (endp == NULL)
3338 					return false;	/* mismatched quotes */
3339 				if (endp[1] != '"')
3340 					break;		/* found end of quoted name */
3341 				/* Collapse adjacent quotes into one quote, and look again */
3342 				memmove(endp, endp + 1, strlen(endp));
3343 				nextp = endp;
3344 			}
3345 			/* endp now points at the terminating quote */
3346 			nextp = endp + 1;
3347 		}
3348 		else
3349 		{
3350 			/* Unquoted name --- extends to separator or whitespace */
3351 			char	   *downname;
3352 			int			len;
3353 
3354 			curname = nextp;
3355 			while (*nextp && *nextp != separator &&
3356 				   !scanner_isspace(*nextp))
3357 				nextp++;
3358 			endp = nextp;
3359 			if (curname == nextp)
3360 				return false;	/* empty unquoted name not allowed */
3361 
3362 			/*
3363 			 * Downcase the identifier, using same code as main lexer does.
3364 			 *
3365 			 * XXX because we want to overwrite the input in-place, we cannot
3366 			 * support a downcasing transformation that increases the string
3367 			 * length.  This is not a problem given the current implementation
3368 			 * of downcase_truncate_identifier, but we'll probably have to do
3369 			 * something about this someday.
3370 			 */
3371 			len = endp - curname;
3372 			downname = downcase_truncate_identifier(curname, len, false);
3373 			Assert(strlen(downname) <= len);
3374 			strncpy(curname, downname, len);	/* strncpy is required here */
3375 			pfree(downname);
3376 		}
3377 
3378 		while (scanner_isspace(*nextp))
3379 			nextp++;			/* skip trailing whitespace */
3380 
3381 		if (*nextp == separator)
3382 		{
3383 			nextp++;
3384 			while (scanner_isspace(*nextp))
3385 				nextp++;		/* skip leading whitespace for next */
3386 			/* we expect another name, so done remains false */
3387 		}
3388 		else if (*nextp == '\0')
3389 			done = true;
3390 		else
3391 			return false;		/* invalid syntax */
3392 
3393 		/* Now safe to overwrite separator with a null */
3394 		*endp = '\0';
3395 
3396 		/* Truncate name if it's overlength */
3397 		truncate_identifier(curname, strlen(curname), false);
3398 
3399 		/*
3400 		 * Finished isolating current name --- add it to list
3401 		 */
3402 		*namelist = lappend(*namelist, curname);
3403 
3404 		/* Loop back if we didn't reach end of string */
3405 	} while (!done);
3406 
3407 	return true;
3408 }
3409 
3410 
3411 /*
3412  * SplitDirectoriesString --- parse a string containing file/directory names
3413  *
3414  * This works fine on file names too; the function name is historical.
3415  *
3416  * This is similar to SplitIdentifierString, except that the parsing
3417  * rules are meant to handle pathnames instead of identifiers: there is
3418  * no downcasing, embedded spaces are allowed, the max length is MAXPGPATH-1,
3419  * and we apply canonicalize_path() to each extracted string.  Because of the
3420  * last, the returned strings are separately palloc'd rather than being
3421  * pointers into rawstring --- but we still scribble on rawstring.
3422  *
3423  * Inputs:
3424  *	rawstring: the input string; must be modifiable!
3425  *	separator: the separator punctuation expected between directories
3426  *			   (typically ',' or ';').  Whitespace may also appear around
3427  *			   directories.
3428  * Outputs:
3429  *	namelist: filled with a palloc'd list of directory names.
3430  *			  Caller should list_free_deep() this even on error return.
3431  *
3432  * Returns true if okay, false if there is a syntax error in the string.
3433  *
3434  * Note that an empty string is considered okay here.
3435  */
3436 bool
SplitDirectoriesString(char * rawstring,char separator,List ** namelist)3437 SplitDirectoriesString(char *rawstring, char separator,
3438 					   List **namelist)
3439 {
3440 	char	   *nextp = rawstring;
3441 	bool		done = false;
3442 
3443 	*namelist = NIL;
3444 
3445 	while (scanner_isspace(*nextp))
3446 		nextp++;				/* skip leading whitespace */
3447 
3448 	if (*nextp == '\0')
3449 		return true;			/* allow empty string */
3450 
3451 	/* At the top of the loop, we are at start of a new directory. */
3452 	do
3453 	{
3454 		char	   *curname;
3455 		char	   *endp;
3456 
3457 		if (*nextp == '"')
3458 		{
3459 			/* Quoted name --- collapse quote-quote pairs */
3460 			curname = nextp + 1;
3461 			for (;;)
3462 			{
3463 				endp = strchr(nextp + 1, '"');
3464 				if (endp == NULL)
3465 					return false;	/* mismatched quotes */
3466 				if (endp[1] != '"')
3467 					break;		/* found end of quoted name */
3468 				/* Collapse adjacent quotes into one quote, and look again */
3469 				memmove(endp, endp + 1, strlen(endp));
3470 				nextp = endp;
3471 			}
3472 			/* endp now points at the terminating quote */
3473 			nextp = endp + 1;
3474 		}
3475 		else
3476 		{
3477 			/* Unquoted name --- extends to separator or end of string */
3478 			curname = endp = nextp;
3479 			while (*nextp && *nextp != separator)
3480 			{
3481 				/* trailing whitespace should not be included in name */
3482 				if (!scanner_isspace(*nextp))
3483 					endp = nextp + 1;
3484 				nextp++;
3485 			}
3486 			if (curname == endp)
3487 				return false;	/* empty unquoted name not allowed */
3488 		}
3489 
3490 		while (scanner_isspace(*nextp))
3491 			nextp++;			/* skip trailing whitespace */
3492 
3493 		if (*nextp == separator)
3494 		{
3495 			nextp++;
3496 			while (scanner_isspace(*nextp))
3497 				nextp++;		/* skip leading whitespace for next */
3498 			/* we expect another name, so done remains false */
3499 		}
3500 		else if (*nextp == '\0')
3501 			done = true;
3502 		else
3503 			return false;		/* invalid syntax */
3504 
3505 		/* Now safe to overwrite separator with a null */
3506 		*endp = '\0';
3507 
3508 		/* Truncate path if it's overlength */
3509 		if (strlen(curname) >= MAXPGPATH)
3510 			curname[MAXPGPATH - 1] = '\0';
3511 
3512 		/*
3513 		 * Finished isolating current name --- add it to list
3514 		 */
3515 		curname = pstrdup(curname);
3516 		canonicalize_path(curname);
3517 		*namelist = lappend(*namelist, curname);
3518 
3519 		/* Loop back if we didn't reach end of string */
3520 	} while (!done);
3521 
3522 	return true;
3523 }
3524 
3525 
3526 /*
3527  * SplitGUCList --- parse a string containing identifiers or file names
3528  *
3529  * This is used to split the value of a GUC_LIST_QUOTE GUC variable, without
3530  * presuming whether the elements will be taken as identifiers or file names.
3531  * We assume the input has already been through flatten_set_variable_args(),
3532  * so that we need never downcase (if appropriate, that was done already).
3533  * Nor do we ever truncate, since we don't know the correct max length.
3534  * We disallow embedded whitespace for simplicity (it shouldn't matter,
3535  * because any embedded whitespace should have led to double-quoting).
3536  * Otherwise the API is identical to SplitIdentifierString.
3537  *
3538  * XXX it's annoying to have so many copies of this string-splitting logic.
3539  * However, it's not clear that having one function with a bunch of option
3540  * flags would be much better.
3541  *
3542  * XXX there is a version of this function in src/bin/pg_dump/dumputils.c.
3543  * Be sure to update that if you have to change this.
3544  *
3545  * Inputs:
3546  *	rawstring: the input string; must be overwritable!	On return, it's
3547  *			   been modified to contain the separated identifiers.
3548  *	separator: the separator punctuation expected between identifiers
3549  *			   (typically '.' or ',').  Whitespace may also appear around
3550  *			   identifiers.
3551  * Outputs:
3552  *	namelist: filled with a palloc'd list of pointers to identifiers within
3553  *			  rawstring.  Caller should list_free() this even on error return.
3554  *
3555  * Returns true if okay, false if there is a syntax error in the string.
3556  */
3557 bool
SplitGUCList(char * rawstring,char separator,List ** namelist)3558 SplitGUCList(char *rawstring, char separator,
3559 			 List **namelist)
3560 {
3561 	char	   *nextp = rawstring;
3562 	bool		done = false;
3563 
3564 	*namelist = NIL;
3565 
3566 	while (scanner_isspace(*nextp))
3567 		nextp++;				/* skip leading whitespace */
3568 
3569 	if (*nextp == '\0')
3570 		return true;			/* allow empty string */
3571 
3572 	/* At the top of the loop, we are at start of a new identifier. */
3573 	do
3574 	{
3575 		char	   *curname;
3576 		char	   *endp;
3577 
3578 		if (*nextp == '"')
3579 		{
3580 			/* Quoted name --- collapse quote-quote pairs */
3581 			curname = nextp + 1;
3582 			for (;;)
3583 			{
3584 				endp = strchr(nextp + 1, '"');
3585 				if (endp == NULL)
3586 					return false;	/* mismatched quotes */
3587 				if (endp[1] != '"')
3588 					break;		/* found end of quoted name */
3589 				/* Collapse adjacent quotes into one quote, and look again */
3590 				memmove(endp, endp + 1, strlen(endp));
3591 				nextp = endp;
3592 			}
3593 			/* endp now points at the terminating quote */
3594 			nextp = endp + 1;
3595 		}
3596 		else
3597 		{
3598 			/* Unquoted name --- extends to separator or whitespace */
3599 			curname = nextp;
3600 			while (*nextp && *nextp != separator &&
3601 				   !scanner_isspace(*nextp))
3602 				nextp++;
3603 			endp = nextp;
3604 			if (curname == nextp)
3605 				return false;	/* empty unquoted name not allowed */
3606 		}
3607 
3608 		while (scanner_isspace(*nextp))
3609 			nextp++;			/* skip trailing whitespace */
3610 
3611 		if (*nextp == separator)
3612 		{
3613 			nextp++;
3614 			while (scanner_isspace(*nextp))
3615 				nextp++;		/* skip leading whitespace for next */
3616 			/* we expect another name, so done remains false */
3617 		}
3618 		else if (*nextp == '\0')
3619 			done = true;
3620 		else
3621 			return false;		/* invalid syntax */
3622 
3623 		/* Now safe to overwrite separator with a null */
3624 		*endp = '\0';
3625 
3626 		/*
3627 		 * Finished isolating current name --- add it to list
3628 		 */
3629 		*namelist = lappend(*namelist, curname);
3630 
3631 		/* Loop back if we didn't reach end of string */
3632 	} while (!done);
3633 
3634 	return true;
3635 }
3636 
3637 
3638 /*****************************************************************************
3639  *	Comparison Functions used for bytea
3640  *
3641  * Note: btree indexes need these routines not to leak memory; therefore,
3642  * be careful to free working copies of toasted datums.  Most places don't
3643  * need to be so careful.
3644  *****************************************************************************/
3645 
3646 Datum
byteaeq(PG_FUNCTION_ARGS)3647 byteaeq(PG_FUNCTION_ARGS)
3648 {
3649 	Datum		arg1 = PG_GETARG_DATUM(0);
3650 	Datum		arg2 = PG_GETARG_DATUM(1);
3651 	bool		result;
3652 	Size		len1,
3653 				len2;
3654 
3655 	/*
3656 	 * We can use a fast path for unequal lengths, which might save us from
3657 	 * having to detoast one or both values.
3658 	 */
3659 	len1 = toast_raw_datum_size(arg1);
3660 	len2 = toast_raw_datum_size(arg2);
3661 	if (len1 != len2)
3662 		result = false;
3663 	else
3664 	{
3665 		bytea	   *barg1 = DatumGetByteaPP(arg1);
3666 		bytea	   *barg2 = DatumGetByteaPP(arg2);
3667 
3668 		result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
3669 						 len1 - VARHDRSZ) == 0);
3670 
3671 		PG_FREE_IF_COPY(barg1, 0);
3672 		PG_FREE_IF_COPY(barg2, 1);
3673 	}
3674 
3675 	PG_RETURN_BOOL(result);
3676 }
3677 
3678 Datum
byteane(PG_FUNCTION_ARGS)3679 byteane(PG_FUNCTION_ARGS)
3680 {
3681 	Datum		arg1 = PG_GETARG_DATUM(0);
3682 	Datum		arg2 = PG_GETARG_DATUM(1);
3683 	bool		result;
3684 	Size		len1,
3685 				len2;
3686 
3687 	/*
3688 	 * We can use a fast path for unequal lengths, which might save us from
3689 	 * having to detoast one or both values.
3690 	 */
3691 	len1 = toast_raw_datum_size(arg1);
3692 	len2 = toast_raw_datum_size(arg2);
3693 	if (len1 != len2)
3694 		result = true;
3695 	else
3696 	{
3697 		bytea	   *barg1 = DatumGetByteaPP(arg1);
3698 		bytea	   *barg2 = DatumGetByteaPP(arg2);
3699 
3700 		result = (memcmp(VARDATA_ANY(barg1), VARDATA_ANY(barg2),
3701 						 len1 - VARHDRSZ) != 0);
3702 
3703 		PG_FREE_IF_COPY(barg1, 0);
3704 		PG_FREE_IF_COPY(barg2, 1);
3705 	}
3706 
3707 	PG_RETURN_BOOL(result);
3708 }
3709 
3710 Datum
bytealt(PG_FUNCTION_ARGS)3711 bytealt(PG_FUNCTION_ARGS)
3712 {
3713 	bytea	   *arg1 = PG_GETARG_BYTEA_PP(0);
3714 	bytea	   *arg2 = PG_GETARG_BYTEA_PP(1);
3715 	int			len1,
3716 				len2;
3717 	int			cmp;
3718 
3719 	len1 = VARSIZE_ANY_EXHDR(arg1);
3720 	len2 = VARSIZE_ANY_EXHDR(arg2);
3721 
3722 	cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3723 
3724 	PG_FREE_IF_COPY(arg1, 0);
3725 	PG_FREE_IF_COPY(arg2, 1);
3726 
3727 	PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 < len2)));
3728 }
3729 
3730 Datum
byteale(PG_FUNCTION_ARGS)3731 byteale(PG_FUNCTION_ARGS)
3732 {
3733 	bytea	   *arg1 = PG_GETARG_BYTEA_PP(0);
3734 	bytea	   *arg2 = PG_GETARG_BYTEA_PP(1);
3735 	int			len1,
3736 				len2;
3737 	int			cmp;
3738 
3739 	len1 = VARSIZE_ANY_EXHDR(arg1);
3740 	len2 = VARSIZE_ANY_EXHDR(arg2);
3741 
3742 	cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3743 
3744 	PG_FREE_IF_COPY(arg1, 0);
3745 	PG_FREE_IF_COPY(arg2, 1);
3746 
3747 	PG_RETURN_BOOL((cmp < 0) || ((cmp == 0) && (len1 <= len2)));
3748 }
3749 
3750 Datum
byteagt(PG_FUNCTION_ARGS)3751 byteagt(PG_FUNCTION_ARGS)
3752 {
3753 	bytea	   *arg1 = PG_GETARG_BYTEA_PP(0);
3754 	bytea	   *arg2 = PG_GETARG_BYTEA_PP(1);
3755 	int			len1,
3756 				len2;
3757 	int			cmp;
3758 
3759 	len1 = VARSIZE_ANY_EXHDR(arg1);
3760 	len2 = VARSIZE_ANY_EXHDR(arg2);
3761 
3762 	cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3763 
3764 	PG_FREE_IF_COPY(arg1, 0);
3765 	PG_FREE_IF_COPY(arg2, 1);
3766 
3767 	PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 > len2)));
3768 }
3769 
3770 Datum
byteage(PG_FUNCTION_ARGS)3771 byteage(PG_FUNCTION_ARGS)
3772 {
3773 	bytea	   *arg1 = PG_GETARG_BYTEA_PP(0);
3774 	bytea	   *arg2 = PG_GETARG_BYTEA_PP(1);
3775 	int			len1,
3776 				len2;
3777 	int			cmp;
3778 
3779 	len1 = VARSIZE_ANY_EXHDR(arg1);
3780 	len2 = VARSIZE_ANY_EXHDR(arg2);
3781 
3782 	cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3783 
3784 	PG_FREE_IF_COPY(arg1, 0);
3785 	PG_FREE_IF_COPY(arg2, 1);
3786 
3787 	PG_RETURN_BOOL((cmp > 0) || ((cmp == 0) && (len1 >= len2)));
3788 }
3789 
3790 Datum
byteacmp(PG_FUNCTION_ARGS)3791 byteacmp(PG_FUNCTION_ARGS)
3792 {
3793 	bytea	   *arg1 = PG_GETARG_BYTEA_PP(0);
3794 	bytea	   *arg2 = PG_GETARG_BYTEA_PP(1);
3795 	int			len1,
3796 				len2;
3797 	int			cmp;
3798 
3799 	len1 = VARSIZE_ANY_EXHDR(arg1);
3800 	len2 = VARSIZE_ANY_EXHDR(arg2);
3801 
3802 	cmp = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
3803 	if ((cmp == 0) && (len1 != len2))
3804 		cmp = (len1 < len2) ? -1 : 1;
3805 
3806 	PG_FREE_IF_COPY(arg1, 0);
3807 	PG_FREE_IF_COPY(arg2, 1);
3808 
3809 	PG_RETURN_INT32(cmp);
3810 }
3811 
3812 Datum
bytea_sortsupport(PG_FUNCTION_ARGS)3813 bytea_sortsupport(PG_FUNCTION_ARGS)
3814 {
3815 	SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
3816 	MemoryContext oldcontext;
3817 
3818 	oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
3819 
3820 	/* Use generic string SortSupport, forcing "C" collation */
3821 	varstr_sortsupport(ssup, C_COLLATION_OID, false);
3822 
3823 	MemoryContextSwitchTo(oldcontext);
3824 
3825 	PG_RETURN_VOID();
3826 }
3827 
3828 /*
3829  * appendStringInfoText
3830  *
3831  * Append a text to str.
3832  * Like appendStringInfoString(str, text_to_cstring(t)) but faster.
3833  */
3834 static void
appendStringInfoText(StringInfo str,const text * t)3835 appendStringInfoText(StringInfo str, const text *t)
3836 {
3837 	appendBinaryStringInfo(str, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
3838 }
3839 
3840 /*
3841  * replace_text
3842  * replace all occurrences of 'old_sub_str' in 'orig_str'
3843  * with 'new_sub_str' to form 'new_str'
3844  *
3845  * returns 'orig_str' if 'old_sub_str' == '' or 'orig_str' == ''
3846  * otherwise returns 'new_str'
3847  */
3848 Datum
replace_text(PG_FUNCTION_ARGS)3849 replace_text(PG_FUNCTION_ARGS)
3850 {
3851 	text	   *src_text = PG_GETARG_TEXT_PP(0);
3852 	text	   *from_sub_text = PG_GETARG_TEXT_PP(1);
3853 	text	   *to_sub_text = PG_GETARG_TEXT_PP(2);
3854 	int			src_text_len;
3855 	int			from_sub_text_len;
3856 	TextPositionState state;
3857 	text	   *ret_text;
3858 	int			start_posn;
3859 	int			curr_posn;
3860 	int			chunk_len;
3861 	char	   *start_ptr;
3862 	StringInfoData str;
3863 
3864 	text_position_setup(src_text, from_sub_text, &state);
3865 
3866 	/*
3867 	 * Note: we check the converted string length, not the original, because
3868 	 * they could be different if the input contained invalid encoding.
3869 	 */
3870 	src_text_len = state.len1;
3871 	from_sub_text_len = state.len2;
3872 
3873 	/* Return unmodified source string if empty source or pattern */
3874 	if (src_text_len < 1 || from_sub_text_len < 1)
3875 	{
3876 		text_position_cleanup(&state);
3877 		PG_RETURN_TEXT_P(src_text);
3878 	}
3879 
3880 	start_posn = 1;
3881 	curr_posn = text_position_next(1, &state);
3882 
3883 	/* When the from_sub_text is not found, there is nothing to do. */
3884 	if (curr_posn == 0)
3885 	{
3886 		text_position_cleanup(&state);
3887 		PG_RETURN_TEXT_P(src_text);
3888 	}
3889 
3890 	/* start_ptr points to the start_posn'th character of src_text */
3891 	start_ptr = VARDATA_ANY(src_text);
3892 
3893 	initStringInfo(&str);
3894 
3895 	do
3896 	{
3897 		CHECK_FOR_INTERRUPTS();
3898 
3899 		/* copy the data skipped over by last text_position_next() */
3900 		chunk_len = charlen_to_bytelen(start_ptr, curr_posn - start_posn);
3901 		appendBinaryStringInfo(&str, start_ptr, chunk_len);
3902 
3903 		appendStringInfoText(&str, to_sub_text);
3904 
3905 		start_posn = curr_posn;
3906 		start_ptr += chunk_len;
3907 		start_posn += from_sub_text_len;
3908 		start_ptr += charlen_to_bytelen(start_ptr, from_sub_text_len);
3909 
3910 		curr_posn = text_position_next(start_posn, &state);
3911 	}
3912 	while (curr_posn > 0);
3913 
3914 	/* copy trailing data */
3915 	chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
3916 	appendBinaryStringInfo(&str, start_ptr, chunk_len);
3917 
3918 	text_position_cleanup(&state);
3919 
3920 	ret_text = cstring_to_text_with_len(str.data, str.len);
3921 	pfree(str.data);
3922 
3923 	PG_RETURN_TEXT_P(ret_text);
3924 }
3925 
3926 /*
3927  * check_replace_text_has_escape_char
3928  *
3929  * check whether replace_text contains escape char.
3930  */
3931 static bool
check_replace_text_has_escape_char(const text * replace_text)3932 check_replace_text_has_escape_char(const text *replace_text)
3933 {
3934 	const char *p = VARDATA_ANY(replace_text);
3935 	const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
3936 
3937 	if (pg_database_encoding_max_length() == 1)
3938 	{
3939 		for (; p < p_end; p++)
3940 		{
3941 			if (*p == '\\')
3942 				return true;
3943 		}
3944 	}
3945 	else
3946 	{
3947 		for (; p < p_end; p += pg_mblen(p))
3948 		{
3949 			if (*p == '\\')
3950 				return true;
3951 		}
3952 	}
3953 
3954 	return false;
3955 }
3956 
3957 /*
3958  * appendStringInfoRegexpSubstr
3959  *
3960  * Append replace_text to str, substituting regexp back references for
3961  * \n escapes.  start_ptr is the start of the match in the source string,
3962  * at logical character position data_pos.
3963  */
3964 static void
appendStringInfoRegexpSubstr(StringInfo str,text * replace_text,regmatch_t * pmatch,char * start_ptr,int data_pos)3965 appendStringInfoRegexpSubstr(StringInfo str, text *replace_text,
3966 							 regmatch_t *pmatch,
3967 							 char *start_ptr, int data_pos)
3968 {
3969 	const char *p = VARDATA_ANY(replace_text);
3970 	const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
3971 	int			eml = pg_database_encoding_max_length();
3972 
3973 	for (;;)
3974 	{
3975 		const char *chunk_start = p;
3976 		int			so;
3977 		int			eo;
3978 
3979 		/* Find next escape char. */
3980 		if (eml == 1)
3981 		{
3982 			for (; p < p_end && *p != '\\'; p++)
3983 				 /* nothing */ ;
3984 		}
3985 		else
3986 		{
3987 			for (; p < p_end && *p != '\\'; p += pg_mblen(p))
3988 				 /* nothing */ ;
3989 		}
3990 
3991 		/* Copy the text we just scanned over, if any. */
3992 		if (p > chunk_start)
3993 			appendBinaryStringInfo(str, chunk_start, p - chunk_start);
3994 
3995 		/* Done if at end of string, else advance over escape char. */
3996 		if (p >= p_end)
3997 			break;
3998 		p++;
3999 
4000 		if (p >= p_end)
4001 		{
4002 			/* Escape at very end of input.  Treat same as unexpected char */
4003 			appendStringInfoChar(str, '\\');
4004 			break;
4005 		}
4006 
4007 		if (*p >= '1' && *p <= '9')
4008 		{
4009 			/* Use the back reference of regexp. */
4010 			int			idx = *p - '0';
4011 
4012 			so = pmatch[idx].rm_so;
4013 			eo = pmatch[idx].rm_eo;
4014 			p++;
4015 		}
4016 		else if (*p == '&')
4017 		{
4018 			/* Use the entire matched string. */
4019 			so = pmatch[0].rm_so;
4020 			eo = pmatch[0].rm_eo;
4021 			p++;
4022 		}
4023 		else if (*p == '\\')
4024 		{
4025 			/* \\ means transfer one \ to output. */
4026 			appendStringInfoChar(str, '\\');
4027 			p++;
4028 			continue;
4029 		}
4030 		else
4031 		{
4032 			/*
4033 			 * If escape char is not followed by any expected char, just treat
4034 			 * it as ordinary data to copy.  (XXX would it be better to throw
4035 			 * an error?)
4036 			 */
4037 			appendStringInfoChar(str, '\\');
4038 			continue;
4039 		}
4040 
4041 		if (so != -1 && eo != -1)
4042 		{
4043 			/*
4044 			 * Copy the text that is back reference of regexp.  Note so and eo
4045 			 * are counted in characters not bytes.
4046 			 */
4047 			char	   *chunk_start;
4048 			int			chunk_len;
4049 
4050 			Assert(so >= data_pos);
4051 			chunk_start = start_ptr;
4052 			chunk_start += charlen_to_bytelen(chunk_start, so - data_pos);
4053 			chunk_len = charlen_to_bytelen(chunk_start, eo - so);
4054 			appendBinaryStringInfo(str, chunk_start, chunk_len);
4055 		}
4056 	}
4057 }
4058 
4059 #define REGEXP_REPLACE_BACKREF_CNT		10
4060 
4061 /*
4062  * replace_text_regexp
4063  *
4064  * replace text that matches to regexp in src_text to replace_text.
4065  *
4066  * Note: to avoid having to include regex.h in builtins.h, we declare
4067  * the regexp argument as void *, but really it's regex_t *.
4068  */
4069 text *
replace_text_regexp(text * src_text,void * regexp,text * replace_text,bool glob)4070 replace_text_regexp(text *src_text, void *regexp,
4071 					text *replace_text, bool glob)
4072 {
4073 	text	   *ret_text;
4074 	regex_t    *re = (regex_t *) regexp;
4075 	int			src_text_len = VARSIZE_ANY_EXHDR(src_text);
4076 	StringInfoData buf;
4077 	regmatch_t	pmatch[REGEXP_REPLACE_BACKREF_CNT];
4078 	pg_wchar   *data;
4079 	size_t		data_len;
4080 	int			search_start;
4081 	int			data_pos;
4082 	char	   *start_ptr;
4083 	bool		have_escape;
4084 
4085 	initStringInfo(&buf);
4086 
4087 	/* Convert data string to wide characters. */
4088 	data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar));
4089 	data_len = pg_mb2wchar_with_len(VARDATA_ANY(src_text), data, src_text_len);
4090 
4091 	/* Check whether replace_text has escape char. */
4092 	have_escape = check_replace_text_has_escape_char(replace_text);
4093 
4094 	/* start_ptr points to the data_pos'th character of src_text */
4095 	start_ptr = (char *) VARDATA_ANY(src_text);
4096 	data_pos = 0;
4097 
4098 	search_start = 0;
4099 	while (search_start <= data_len)
4100 	{
4101 		int			regexec_result;
4102 
4103 		CHECK_FOR_INTERRUPTS();
4104 
4105 		regexec_result = pg_regexec(re,
4106 									data,
4107 									data_len,
4108 									search_start,
4109 									NULL,	/* no details */
4110 									REGEXP_REPLACE_BACKREF_CNT,
4111 									pmatch,
4112 									0);
4113 
4114 		if (regexec_result == REG_NOMATCH)
4115 			break;
4116 
4117 		if (regexec_result != REG_OKAY)
4118 		{
4119 			char		errMsg[100];
4120 
4121 			CHECK_FOR_INTERRUPTS();
4122 			pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
4123 			ereport(ERROR,
4124 					(errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
4125 					 errmsg("regular expression failed: %s", errMsg)));
4126 		}
4127 
4128 		/*
4129 		 * Copy the text to the left of the match position.  Note we are given
4130 		 * character not byte indexes.
4131 		 */
4132 		if (pmatch[0].rm_so - data_pos > 0)
4133 		{
4134 			int			chunk_len;
4135 
4136 			chunk_len = charlen_to_bytelen(start_ptr,
4137 										   pmatch[0].rm_so - data_pos);
4138 			appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4139 
4140 			/*
4141 			 * Advance start_ptr over that text, to avoid multiple rescans of
4142 			 * it if the replace_text contains multiple back-references.
4143 			 */
4144 			start_ptr += chunk_len;
4145 			data_pos = pmatch[0].rm_so;
4146 		}
4147 
4148 		/*
4149 		 * Copy the replace_text. Process back references when the
4150 		 * replace_text has escape characters.
4151 		 */
4152 		if (have_escape)
4153 			appendStringInfoRegexpSubstr(&buf, replace_text, pmatch,
4154 										 start_ptr, data_pos);
4155 		else
4156 			appendStringInfoText(&buf, replace_text);
4157 
4158 		/* Advance start_ptr and data_pos over the matched text. */
4159 		start_ptr += charlen_to_bytelen(start_ptr,
4160 										pmatch[0].rm_eo - data_pos);
4161 		data_pos = pmatch[0].rm_eo;
4162 
4163 		/*
4164 		 * When global option is off, replace the first instance only.
4165 		 */
4166 		if (!glob)
4167 			break;
4168 
4169 		/*
4170 		 * Advance search position.  Normally we start the next search at the
4171 		 * end of the previous match; but if the match was of zero length, we
4172 		 * have to advance by one character, or we'd just find the same match
4173 		 * again.
4174 		 */
4175 		search_start = data_pos;
4176 		if (pmatch[0].rm_so == pmatch[0].rm_eo)
4177 			search_start++;
4178 	}
4179 
4180 	/*
4181 	 * Copy the text to the right of the last match.
4182 	 */
4183 	if (data_pos < data_len)
4184 	{
4185 		int			chunk_len;
4186 
4187 		chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
4188 		appendBinaryStringInfo(&buf, start_ptr, chunk_len);
4189 	}
4190 
4191 	ret_text = cstring_to_text_with_len(buf.data, buf.len);
4192 	pfree(buf.data);
4193 	pfree(data);
4194 
4195 	return ret_text;
4196 }
4197 
4198 /*
4199  * split_text
4200  * parse input string
4201  * return ord item (1 based)
4202  * based on provided field separator
4203  */
4204 Datum
split_text(PG_FUNCTION_ARGS)4205 split_text(PG_FUNCTION_ARGS)
4206 {
4207 	text	   *inputstring = PG_GETARG_TEXT_PP(0);
4208 	text	   *fldsep = PG_GETARG_TEXT_PP(1);
4209 	int			fldnum = PG_GETARG_INT32(2);
4210 	int			inputstring_len;
4211 	int			fldsep_len;
4212 	TextPositionState state;
4213 	int			start_posn;
4214 	int			end_posn;
4215 	text	   *result_text;
4216 
4217 	/* field number is 1 based */
4218 	if (fldnum < 1)
4219 		ereport(ERROR,
4220 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4221 				 errmsg("field position must be greater than zero")));
4222 
4223 	text_position_setup(inputstring, fldsep, &state);
4224 
4225 	/*
4226 	 * Note: we check the converted string length, not the original, because
4227 	 * they could be different if the input contained invalid encoding.
4228 	 */
4229 	inputstring_len = state.len1;
4230 	fldsep_len = state.len2;
4231 
4232 	/* return empty string for empty input string */
4233 	if (inputstring_len < 1)
4234 	{
4235 		text_position_cleanup(&state);
4236 		PG_RETURN_TEXT_P(cstring_to_text(""));
4237 	}
4238 
4239 	/* empty field separator */
4240 	if (fldsep_len < 1)
4241 	{
4242 		text_position_cleanup(&state);
4243 		/* if first field, return input string, else empty string */
4244 		if (fldnum == 1)
4245 			PG_RETURN_TEXT_P(inputstring);
4246 		else
4247 			PG_RETURN_TEXT_P(cstring_to_text(""));
4248 	}
4249 
4250 	/* identify bounds of first field */
4251 	start_posn = 1;
4252 	end_posn = text_position_next(1, &state);
4253 
4254 	/* special case if fldsep not found at all */
4255 	if (end_posn == 0)
4256 	{
4257 		text_position_cleanup(&state);
4258 		/* if field 1 requested, return input string, else empty string */
4259 		if (fldnum == 1)
4260 			PG_RETURN_TEXT_P(inputstring);
4261 		else
4262 			PG_RETURN_TEXT_P(cstring_to_text(""));
4263 	}
4264 
4265 	while (end_posn > 0 && --fldnum > 0)
4266 	{
4267 		/* identify bounds of next field */
4268 		start_posn = end_posn + fldsep_len;
4269 		end_posn = text_position_next(start_posn, &state);
4270 	}
4271 
4272 	text_position_cleanup(&state);
4273 
4274 	if (fldnum > 0)
4275 	{
4276 		/* N'th field separator not found */
4277 		/* if last field requested, return it, else empty string */
4278 		if (fldnum == 1)
4279 			result_text = text_substring(PointerGetDatum(inputstring),
4280 										 start_posn,
4281 										 -1,
4282 										 true);
4283 		else
4284 			result_text = cstring_to_text("");
4285 	}
4286 	else
4287 	{
4288 		/* non-last field requested */
4289 		result_text = text_substring(PointerGetDatum(inputstring),
4290 									 start_posn,
4291 									 end_posn - start_posn,
4292 									 false);
4293 	}
4294 
4295 	PG_RETURN_TEXT_P(result_text);
4296 }
4297 
4298 /*
4299  * Convenience function to return true when two text params are equal.
4300  */
4301 static bool
text_isequal(text * txt1,text * txt2)4302 text_isequal(text *txt1, text *txt2)
4303 {
4304 	return DatumGetBool(DirectFunctionCall2(texteq,
4305 											PointerGetDatum(txt1),
4306 											PointerGetDatum(txt2)));
4307 }
4308 
4309 /*
4310  * text_to_array
4311  * parse input string and return text array of elements,
4312  * based on provided field separator
4313  */
4314 Datum
text_to_array(PG_FUNCTION_ARGS)4315 text_to_array(PG_FUNCTION_ARGS)
4316 {
4317 	return text_to_array_internal(fcinfo);
4318 }
4319 
4320 /*
4321  * text_to_array_null
4322  * parse input string and return text array of elements,
4323  * based on provided field separator and null string
4324  *
4325  * This is a separate entry point only to prevent the regression tests from
4326  * complaining about different argument sets for the same internal function.
4327  */
4328 Datum
text_to_array_null(PG_FUNCTION_ARGS)4329 text_to_array_null(PG_FUNCTION_ARGS)
4330 {
4331 	return text_to_array_internal(fcinfo);
4332 }
4333 
4334 /*
4335  * common code for text_to_array and text_to_array_null functions
4336  *
4337  * These are not strict so we have to test for null inputs explicitly.
4338  */
4339 static Datum
text_to_array_internal(PG_FUNCTION_ARGS)4340 text_to_array_internal(PG_FUNCTION_ARGS)
4341 {
4342 	text	   *inputstring;
4343 	text	   *fldsep;
4344 	text	   *null_string;
4345 	int			inputstring_len;
4346 	int			fldsep_len;
4347 	char	   *start_ptr;
4348 	text	   *result_text;
4349 	bool		is_null;
4350 	ArrayBuildState *astate = NULL;
4351 
4352 	/* when input string is NULL, then result is NULL too */
4353 	if (PG_ARGISNULL(0))
4354 		PG_RETURN_NULL();
4355 
4356 	inputstring = PG_GETARG_TEXT_PP(0);
4357 
4358 	/* fldsep can be NULL */
4359 	if (!PG_ARGISNULL(1))
4360 		fldsep = PG_GETARG_TEXT_PP(1);
4361 	else
4362 		fldsep = NULL;
4363 
4364 	/* null_string can be NULL or omitted */
4365 	if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
4366 		null_string = PG_GETARG_TEXT_PP(2);
4367 	else
4368 		null_string = NULL;
4369 
4370 	if (fldsep != NULL)
4371 	{
4372 		/*
4373 		 * Normal case with non-null fldsep.  Use the text_position machinery
4374 		 * to search for occurrences of fldsep.
4375 		 */
4376 		TextPositionState state;
4377 		int			fldnum;
4378 		int			start_posn;
4379 		int			end_posn;
4380 		int			chunk_len;
4381 
4382 		text_position_setup(inputstring, fldsep, &state);
4383 
4384 		/*
4385 		 * Note: we check the converted string length, not the original,
4386 		 * because they could be different if the input contained invalid
4387 		 * encoding.
4388 		 */
4389 		inputstring_len = state.len1;
4390 		fldsep_len = state.len2;
4391 
4392 		/* return empty array for empty input string */
4393 		if (inputstring_len < 1)
4394 		{
4395 			text_position_cleanup(&state);
4396 			PG_RETURN_ARRAYTYPE_P(construct_empty_array(TEXTOID));
4397 		}
4398 
4399 		/*
4400 		 * empty field separator: return the input string as a one-element
4401 		 * array
4402 		 */
4403 		if (fldsep_len < 1)
4404 		{
4405 			Datum		elems[1];
4406 			bool		nulls[1];
4407 			int			dims[1];
4408 			int			lbs[1];
4409 
4410 			text_position_cleanup(&state);
4411 			/* single element can be a NULL too */
4412 			is_null = null_string ? text_isequal(inputstring, null_string) : false;
4413 
4414 			elems[0] = PointerGetDatum(inputstring);
4415 			nulls[0] = is_null;
4416 			dims[0] = 1;
4417 			lbs[0] = 1;
4418 			/* XXX: this hardcodes assumptions about the text type */
4419 			PG_RETURN_ARRAYTYPE_P(construct_md_array(elems, nulls,
4420 													 1, dims, lbs,
4421 													 TEXTOID, -1, false, 'i'));
4422 		}
4423 
4424 		start_posn = 1;
4425 		/* start_ptr points to the start_posn'th character of inputstring */
4426 		start_ptr = VARDATA_ANY(inputstring);
4427 
4428 		for (fldnum = 1;; fldnum++) /* field number is 1 based */
4429 		{
4430 			CHECK_FOR_INTERRUPTS();
4431 
4432 			end_posn = text_position_next(start_posn, &state);
4433 
4434 			if (end_posn == 0)
4435 			{
4436 				/* fetch last field */
4437 				chunk_len = ((char *) inputstring + VARSIZE_ANY(inputstring)) - start_ptr;
4438 			}
4439 			else
4440 			{
4441 				/* fetch non-last field */
4442 				chunk_len = charlen_to_bytelen(start_ptr, end_posn - start_posn);
4443 			}
4444 
4445 			/* must build a temp text datum to pass to accumArrayResult */
4446 			result_text = cstring_to_text_with_len(start_ptr, chunk_len);
4447 			is_null = null_string ? text_isequal(result_text, null_string) : false;
4448 
4449 			/* stash away this field */
4450 			astate = accumArrayResult(astate,
4451 									  PointerGetDatum(result_text),
4452 									  is_null,
4453 									  TEXTOID,
4454 									  CurrentMemoryContext);
4455 
4456 			pfree(result_text);
4457 
4458 			if (end_posn == 0)
4459 				break;
4460 
4461 			start_posn = end_posn;
4462 			start_ptr += chunk_len;
4463 			start_posn += fldsep_len;
4464 			start_ptr += charlen_to_bytelen(start_ptr, fldsep_len);
4465 		}
4466 
4467 		text_position_cleanup(&state);
4468 	}
4469 	else
4470 	{
4471 		/*
4472 		 * When fldsep is NULL, each character in the inputstring becomes an
4473 		 * element in the result array.  The separator is effectively the
4474 		 * space between characters.
4475 		 */
4476 		inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
4477 
4478 		/* return empty array for empty input string */
4479 		if (inputstring_len < 1)
4480 			PG_RETURN_ARRAYTYPE_P(construct_empty_array(TEXTOID));
4481 
4482 		start_ptr = VARDATA_ANY(inputstring);
4483 
4484 		while (inputstring_len > 0)
4485 		{
4486 			int			chunk_len = pg_mblen(start_ptr);
4487 
4488 			CHECK_FOR_INTERRUPTS();
4489 
4490 			/* must build a temp text datum to pass to accumArrayResult */
4491 			result_text = cstring_to_text_with_len(start_ptr, chunk_len);
4492 			is_null = null_string ? text_isequal(result_text, null_string) : false;
4493 
4494 			/* stash away this field */
4495 			astate = accumArrayResult(astate,
4496 									  PointerGetDatum(result_text),
4497 									  is_null,
4498 									  TEXTOID,
4499 									  CurrentMemoryContext);
4500 
4501 			pfree(result_text);
4502 
4503 			start_ptr += chunk_len;
4504 			inputstring_len -= chunk_len;
4505 		}
4506 	}
4507 
4508 	PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate,
4509 										  CurrentMemoryContext));
4510 }
4511 
4512 /*
4513  * array_to_text
4514  * concatenate Cstring representation of input array elements
4515  * using provided field separator
4516  */
4517 Datum
array_to_text(PG_FUNCTION_ARGS)4518 array_to_text(PG_FUNCTION_ARGS)
4519 {
4520 	ArrayType  *v = PG_GETARG_ARRAYTYPE_P(0);
4521 	char	   *fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4522 
4523 	PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, NULL));
4524 }
4525 
4526 /*
4527  * array_to_text_null
4528  * concatenate Cstring representation of input array elements
4529  * using provided field separator and null string
4530  *
4531  * This version is not strict so we have to test for null inputs explicitly.
4532  */
4533 Datum
array_to_text_null(PG_FUNCTION_ARGS)4534 array_to_text_null(PG_FUNCTION_ARGS)
4535 {
4536 	ArrayType  *v;
4537 	char	   *fldsep;
4538 	char	   *null_string;
4539 
4540 	/* returns NULL when first or second parameter is NULL */
4541 	if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
4542 		PG_RETURN_NULL();
4543 
4544 	v = PG_GETARG_ARRAYTYPE_P(0);
4545 	fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
4546 
4547 	/* NULL null string is passed through as a null pointer */
4548 	if (!PG_ARGISNULL(2))
4549 		null_string = text_to_cstring(PG_GETARG_TEXT_PP(2));
4550 	else
4551 		null_string = NULL;
4552 
4553 	PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, null_string));
4554 }
4555 
4556 /*
4557  * common code for array_to_text and array_to_text_null functions
4558  */
4559 static text *
array_to_text_internal(FunctionCallInfo fcinfo,ArrayType * v,const char * fldsep,const char * null_string)4560 array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v,
4561 					   const char *fldsep, const char *null_string)
4562 {
4563 	text	   *result;
4564 	int			nitems,
4565 			   *dims,
4566 				ndims;
4567 	Oid			element_type;
4568 	int			typlen;
4569 	bool		typbyval;
4570 	char		typalign;
4571 	StringInfoData buf;
4572 	bool		printed = false;
4573 	char	   *p;
4574 	bits8	   *bitmap;
4575 	int			bitmask;
4576 	int			i;
4577 	ArrayMetaState *my_extra;
4578 
4579 	ndims = ARR_NDIM(v);
4580 	dims = ARR_DIMS(v);
4581 	nitems = ArrayGetNItems(ndims, dims);
4582 
4583 	/* if there are no elements, return an empty string */
4584 	if (nitems == 0)
4585 		return cstring_to_text_with_len("", 0);
4586 
4587 	element_type = ARR_ELEMTYPE(v);
4588 	initStringInfo(&buf);
4589 
4590 	/*
4591 	 * We arrange to look up info about element type, including its output
4592 	 * conversion proc, only once per series of calls, assuming the element
4593 	 * type doesn't change underneath us.
4594 	 */
4595 	my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
4596 	if (my_extra == NULL)
4597 	{
4598 		fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4599 													  sizeof(ArrayMetaState));
4600 		my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
4601 		my_extra->element_type = ~element_type;
4602 	}
4603 
4604 	if (my_extra->element_type != element_type)
4605 	{
4606 		/*
4607 		 * Get info about element type, including its output conversion proc
4608 		 */
4609 		get_type_io_data(element_type, IOFunc_output,
4610 						 &my_extra->typlen, &my_extra->typbyval,
4611 						 &my_extra->typalign, &my_extra->typdelim,
4612 						 &my_extra->typioparam, &my_extra->typiofunc);
4613 		fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc,
4614 					  fcinfo->flinfo->fn_mcxt);
4615 		my_extra->element_type = element_type;
4616 	}
4617 	typlen = my_extra->typlen;
4618 	typbyval = my_extra->typbyval;
4619 	typalign = my_extra->typalign;
4620 
4621 	p = ARR_DATA_PTR(v);
4622 	bitmap = ARR_NULLBITMAP(v);
4623 	bitmask = 1;
4624 
4625 	for (i = 0; i < nitems; i++)
4626 	{
4627 		Datum		itemvalue;
4628 		char	   *value;
4629 
4630 		/* Get source element, checking for NULL */
4631 		if (bitmap && (*bitmap & bitmask) == 0)
4632 		{
4633 			/* if null_string is NULL, we just ignore null elements */
4634 			if (null_string != NULL)
4635 			{
4636 				if (printed)
4637 					appendStringInfo(&buf, "%s%s", fldsep, null_string);
4638 				else
4639 					appendStringInfoString(&buf, null_string);
4640 				printed = true;
4641 			}
4642 		}
4643 		else
4644 		{
4645 			itemvalue = fetch_att(p, typbyval, typlen);
4646 
4647 			value = OutputFunctionCall(&my_extra->proc, itemvalue);
4648 
4649 			if (printed)
4650 				appendStringInfo(&buf, "%s%s", fldsep, value);
4651 			else
4652 				appendStringInfoString(&buf, value);
4653 			printed = true;
4654 
4655 			p = att_addlength_pointer(p, typlen, p);
4656 			p = (char *) att_align_nominal(p, typalign);
4657 		}
4658 
4659 		/* advance bitmap pointer if any */
4660 		if (bitmap)
4661 		{
4662 			bitmask <<= 1;
4663 			if (bitmask == 0x100)
4664 			{
4665 				bitmap++;
4666 				bitmask = 1;
4667 			}
4668 		}
4669 	}
4670 
4671 	result = cstring_to_text_with_len(buf.data, buf.len);
4672 	pfree(buf.data);
4673 
4674 	return result;
4675 }
4676 
4677 #define HEXBASE 16
4678 /*
4679  * Convert an int32 to a string containing a base 16 (hex) representation of
4680  * the number.
4681  */
4682 Datum
to_hex32(PG_FUNCTION_ARGS)4683 to_hex32(PG_FUNCTION_ARGS)
4684 {
4685 	uint32		value = (uint32) PG_GETARG_INT32(0);
4686 	char	   *ptr;
4687 	const char *digits = "0123456789abcdef";
4688 	char		buf[32];		/* bigger than needed, but reasonable */
4689 
4690 	ptr = buf + sizeof(buf) - 1;
4691 	*ptr = '\0';
4692 
4693 	do
4694 	{
4695 		*--ptr = digits[value % HEXBASE];
4696 		value /= HEXBASE;
4697 	} while (ptr > buf && value);
4698 
4699 	PG_RETURN_TEXT_P(cstring_to_text(ptr));
4700 }
4701 
4702 /*
4703  * Convert an int64 to a string containing a base 16 (hex) representation of
4704  * the number.
4705  */
4706 Datum
to_hex64(PG_FUNCTION_ARGS)4707 to_hex64(PG_FUNCTION_ARGS)
4708 {
4709 	uint64		value = (uint64) PG_GETARG_INT64(0);
4710 	char	   *ptr;
4711 	const char *digits = "0123456789abcdef";
4712 	char		buf[32];		/* bigger than needed, but reasonable */
4713 
4714 	ptr = buf + sizeof(buf) - 1;
4715 	*ptr = '\0';
4716 
4717 	do
4718 	{
4719 		*--ptr = digits[value % HEXBASE];
4720 		value /= HEXBASE;
4721 	} while (ptr > buf && value);
4722 
4723 	PG_RETURN_TEXT_P(cstring_to_text(ptr));
4724 }
4725 
4726 /*
4727  * Return the size of a datum, possibly compressed
4728  *
4729  * Works on any data type
4730  */
4731 Datum
pg_column_size(PG_FUNCTION_ARGS)4732 pg_column_size(PG_FUNCTION_ARGS)
4733 {
4734 	Datum		value = PG_GETARG_DATUM(0);
4735 	int32		result;
4736 	int			typlen;
4737 
4738 	/* On first call, get the input type's typlen, and save at *fn_extra */
4739 	if (fcinfo->flinfo->fn_extra == NULL)
4740 	{
4741 		/* Lookup the datatype of the supplied argument */
4742 		Oid			argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
4743 
4744 		typlen = get_typlen(argtypeid);
4745 		if (typlen == 0)		/* should not happen */
4746 			elog(ERROR, "cache lookup failed for type %u", argtypeid);
4747 
4748 		fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4749 													  sizeof(int));
4750 		*((int *) fcinfo->flinfo->fn_extra) = typlen;
4751 	}
4752 	else
4753 		typlen = *((int *) fcinfo->flinfo->fn_extra);
4754 
4755 	if (typlen == -1)
4756 	{
4757 		/* varlena type, possibly toasted */
4758 		result = toast_datum_size(value);
4759 	}
4760 	else if (typlen == -2)
4761 	{
4762 		/* cstring */
4763 		result = strlen(DatumGetCString(value)) + 1;
4764 	}
4765 	else
4766 	{
4767 		/* ordinary fixed-width type */
4768 		result = typlen;
4769 	}
4770 
4771 	PG_RETURN_INT32(result);
4772 }
4773 
4774 /*
4775  * string_agg - Concatenates values and returns string.
4776  *
4777  * Syntax: string_agg(value text, delimiter text) RETURNS text
4778  *
4779  * Note: Any NULL values are ignored. The first-call delimiter isn't
4780  * actually used at all, and on subsequent calls the delimiter precedes
4781  * the associated value.
4782  */
4783 
4784 /* subroutine to initialize state */
4785 static StringInfo
makeStringAggState(FunctionCallInfo fcinfo)4786 makeStringAggState(FunctionCallInfo fcinfo)
4787 {
4788 	StringInfo	state;
4789 	MemoryContext aggcontext;
4790 	MemoryContext oldcontext;
4791 
4792 	if (!AggCheckCallContext(fcinfo, &aggcontext))
4793 	{
4794 		/* cannot be called directly because of internal-type argument */
4795 		elog(ERROR, "string_agg_transfn called in non-aggregate context");
4796 	}
4797 
4798 	/*
4799 	 * Create state in aggregate context.  It'll stay there across subsequent
4800 	 * calls.
4801 	 */
4802 	oldcontext = MemoryContextSwitchTo(aggcontext);
4803 	state = makeStringInfo();
4804 	MemoryContextSwitchTo(oldcontext);
4805 
4806 	return state;
4807 }
4808 
4809 Datum
string_agg_transfn(PG_FUNCTION_ARGS)4810 string_agg_transfn(PG_FUNCTION_ARGS)
4811 {
4812 	StringInfo	state;
4813 
4814 	state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
4815 
4816 	/* Append the value unless null. */
4817 	if (!PG_ARGISNULL(1))
4818 	{
4819 		/* On the first time through, we ignore the delimiter. */
4820 		if (state == NULL)
4821 			state = makeStringAggState(fcinfo);
4822 		else if (!PG_ARGISNULL(2))
4823 			appendStringInfoText(state, PG_GETARG_TEXT_PP(2));	/* delimiter */
4824 
4825 		appendStringInfoText(state, PG_GETARG_TEXT_PP(1));	/* value */
4826 	}
4827 
4828 	/*
4829 	 * The transition type for string_agg() is declared to be "internal",
4830 	 * which is a pass-by-value type the same size as a pointer.
4831 	 */
4832 	PG_RETURN_POINTER(state);
4833 }
4834 
4835 Datum
string_agg_finalfn(PG_FUNCTION_ARGS)4836 string_agg_finalfn(PG_FUNCTION_ARGS)
4837 {
4838 	StringInfo	state;
4839 
4840 	/* cannot be called directly because of internal-type argument */
4841 	Assert(AggCheckCallContext(fcinfo, NULL));
4842 
4843 	state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
4844 
4845 	if (state != NULL)
4846 		PG_RETURN_TEXT_P(cstring_to_text_with_len(state->data, state->len));
4847 	else
4848 		PG_RETURN_NULL();
4849 }
4850 
4851 /*
4852  * Prepare cache with fmgr info for the output functions of the datatypes of
4853  * the arguments of a concat-like function, beginning with argument "argidx".
4854  * (Arguments before that will have corresponding slots in the resulting
4855  * FmgrInfo array, but we don't fill those slots.)
4856  */
4857 static FmgrInfo *
build_concat_foutcache(FunctionCallInfo fcinfo,int argidx)4858 build_concat_foutcache(FunctionCallInfo fcinfo, int argidx)
4859 {
4860 	FmgrInfo   *foutcache;
4861 	int			i;
4862 
4863 	/* We keep the info in fn_mcxt so it survives across calls */
4864 	foutcache = (FmgrInfo *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4865 												PG_NARGS() * sizeof(FmgrInfo));
4866 
4867 	for (i = argidx; i < PG_NARGS(); i++)
4868 	{
4869 		Oid			valtype;
4870 		Oid			typOutput;
4871 		bool		typIsVarlena;
4872 
4873 		valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
4874 		if (!OidIsValid(valtype))
4875 			elog(ERROR, "could not determine data type of concat() input");
4876 
4877 		getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
4878 		fmgr_info_cxt(typOutput, &foutcache[i], fcinfo->flinfo->fn_mcxt);
4879 	}
4880 
4881 	fcinfo->flinfo->fn_extra = foutcache;
4882 
4883 	return foutcache;
4884 }
4885 
4886 /*
4887  * Implementation of both concat() and concat_ws().
4888  *
4889  * sepstr is the separator string to place between values.
4890  * argidx identifies the first argument to concatenate (counting from zero);
4891  * note that this must be constant across any one series of calls.
4892  *
4893  * Returns NULL if result should be NULL, else text value.
4894  */
4895 static text *
concat_internal(const char * sepstr,int argidx,FunctionCallInfo fcinfo)4896 concat_internal(const char *sepstr, int argidx,
4897 				FunctionCallInfo fcinfo)
4898 {
4899 	text	   *result;
4900 	StringInfoData str;
4901 	FmgrInfo   *foutcache;
4902 	bool		first_arg = true;
4903 	int			i;
4904 
4905 	/*
4906 	 * concat(VARIADIC some-array) is essentially equivalent to
4907 	 * array_to_text(), ie concat the array elements with the given separator.
4908 	 * So we just pass the case off to that code.
4909 	 */
4910 	if (get_fn_expr_variadic(fcinfo->flinfo))
4911 	{
4912 		ArrayType  *arr;
4913 
4914 		/* Should have just the one argument */
4915 		Assert(argidx == PG_NARGS() - 1);
4916 
4917 		/* concat(VARIADIC NULL) is defined as NULL */
4918 		if (PG_ARGISNULL(argidx))
4919 			return NULL;
4920 
4921 		/*
4922 		 * Non-null argument had better be an array.  We assume that any call
4923 		 * context that could let get_fn_expr_variadic return true will have
4924 		 * checked that a VARIADIC-labeled parameter actually is an array.  So
4925 		 * it should be okay to just Assert that it's an array rather than
4926 		 * doing a full-fledged error check.
4927 		 */
4928 		Assert(OidIsValid(get_base_element_type(get_fn_expr_argtype(fcinfo->flinfo, argidx))));
4929 
4930 		/* OK, safe to fetch the array value */
4931 		arr = PG_GETARG_ARRAYTYPE_P(argidx);
4932 
4933 		/*
4934 		 * And serialize the array.  We tell array_to_text to ignore null
4935 		 * elements, which matches the behavior of the loop below.
4936 		 */
4937 		return array_to_text_internal(fcinfo, arr, sepstr, NULL);
4938 	}
4939 
4940 	/* Normal case without explicit VARIADIC marker */
4941 	initStringInfo(&str);
4942 
4943 	/* Get output function info, building it if first time through */
4944 	foutcache = (FmgrInfo *) fcinfo->flinfo->fn_extra;
4945 	if (foutcache == NULL)
4946 		foutcache = build_concat_foutcache(fcinfo, argidx);
4947 
4948 	for (i = argidx; i < PG_NARGS(); i++)
4949 	{
4950 		if (!PG_ARGISNULL(i))
4951 		{
4952 			Datum		value = PG_GETARG_DATUM(i);
4953 
4954 			/* add separator if appropriate */
4955 			if (first_arg)
4956 				first_arg = false;
4957 			else
4958 				appendStringInfoString(&str, sepstr);
4959 
4960 			/* call the appropriate type output function, append the result */
4961 			appendStringInfoString(&str,
4962 								   OutputFunctionCall(&foutcache[i], value));
4963 		}
4964 	}
4965 
4966 	result = cstring_to_text_with_len(str.data, str.len);
4967 	pfree(str.data);
4968 
4969 	return result;
4970 }
4971 
4972 /*
4973  * Concatenate all arguments. NULL arguments are ignored.
4974  */
4975 Datum
text_concat(PG_FUNCTION_ARGS)4976 text_concat(PG_FUNCTION_ARGS)
4977 {
4978 	text	   *result;
4979 
4980 	result = concat_internal("", 0, fcinfo);
4981 	if (result == NULL)
4982 		PG_RETURN_NULL();
4983 	PG_RETURN_TEXT_P(result);
4984 }
4985 
4986 /*
4987  * Concatenate all but first argument value with separators. The first
4988  * parameter is used as the separator. NULL arguments are ignored.
4989  */
4990 Datum
text_concat_ws(PG_FUNCTION_ARGS)4991 text_concat_ws(PG_FUNCTION_ARGS)
4992 {
4993 	char	   *sep;
4994 	text	   *result;
4995 
4996 	/* return NULL when separator is NULL */
4997 	if (PG_ARGISNULL(0))
4998 		PG_RETURN_NULL();
4999 	sep = text_to_cstring(PG_GETARG_TEXT_PP(0));
5000 
5001 	result = concat_internal(sep, 1, fcinfo);
5002 	if (result == NULL)
5003 		PG_RETURN_NULL();
5004 	PG_RETURN_TEXT_P(result);
5005 }
5006 
5007 /*
5008  * Return first n characters in the string. When n is negative,
5009  * return all but last |n| characters.
5010  */
5011 Datum
text_left(PG_FUNCTION_ARGS)5012 text_left(PG_FUNCTION_ARGS)
5013 {
5014 	text	   *str = PG_GETARG_TEXT_PP(0);
5015 	const char *p = VARDATA_ANY(str);
5016 	int			len = VARSIZE_ANY_EXHDR(str);
5017 	int			n = PG_GETARG_INT32(1);
5018 	int			rlen;
5019 
5020 	if (n < 0)
5021 		n = pg_mbstrlen_with_len(p, len) + n;
5022 	rlen = pg_mbcharcliplen(p, len, n);
5023 
5024 	PG_RETURN_TEXT_P(cstring_to_text_with_len(p, rlen));
5025 }
5026 
5027 /*
5028  * Return last n characters in the string. When n is negative,
5029  * return all but first |n| characters.
5030  */
5031 Datum
text_right(PG_FUNCTION_ARGS)5032 text_right(PG_FUNCTION_ARGS)
5033 {
5034 	text	   *str = PG_GETARG_TEXT_PP(0);
5035 	const char *p = VARDATA_ANY(str);
5036 	int			len = VARSIZE_ANY_EXHDR(str);
5037 	int			n = PG_GETARG_INT32(1);
5038 	int			off;
5039 
5040 	if (n < 0)
5041 		n = -n;
5042 	else
5043 		n = pg_mbstrlen_with_len(p, len) - n;
5044 	off = pg_mbcharcliplen(p, len, n);
5045 
5046 	PG_RETURN_TEXT_P(cstring_to_text_with_len(p + off, len - off));
5047 }
5048 
5049 /*
5050  * Return reversed string
5051  */
5052 Datum
text_reverse(PG_FUNCTION_ARGS)5053 text_reverse(PG_FUNCTION_ARGS)
5054 {
5055 	text	   *str = PG_GETARG_TEXT_PP(0);
5056 	const char *p = VARDATA_ANY(str);
5057 	int			len = VARSIZE_ANY_EXHDR(str);
5058 	const char *endp = p + len;
5059 	text	   *result;
5060 	char	   *dst;
5061 
5062 	result = palloc(len + VARHDRSZ);
5063 	dst = (char *) VARDATA(result) + len;
5064 	SET_VARSIZE(result, len + VARHDRSZ);
5065 
5066 	if (pg_database_encoding_max_length() > 1)
5067 	{
5068 		/* multibyte version */
5069 		while (p < endp)
5070 		{
5071 			int			sz;
5072 
5073 			sz = pg_mblen(p);
5074 			dst -= sz;
5075 			memcpy(dst, p, sz);
5076 			p += sz;
5077 		}
5078 	}
5079 	else
5080 	{
5081 		/* single byte version */
5082 		while (p < endp)
5083 			*(--dst) = *p++;
5084 	}
5085 
5086 	PG_RETURN_TEXT_P(result);
5087 }
5088 
5089 
5090 /*
5091  * Support macros for text_format()
5092  */
5093 #define TEXT_FORMAT_FLAG_MINUS	0x0001	/* is minus flag present? */
5094 
5095 #define ADVANCE_PARSE_POINTER(ptr,end_ptr) \
5096 	do { \
5097 		if (++(ptr) >= (end_ptr)) \
5098 			ereport(ERROR, \
5099 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE), \
5100 					 errmsg("unterminated format() type specifier"), \
5101 					 errhint("For a single \"%%\" use \"%%%%\"."))); \
5102 	} while (0)
5103 
5104 /*
5105  * Returns a formatted string
5106  */
5107 Datum
text_format(PG_FUNCTION_ARGS)5108 text_format(PG_FUNCTION_ARGS)
5109 {
5110 	text	   *fmt;
5111 	StringInfoData str;
5112 	const char *cp;
5113 	const char *start_ptr;
5114 	const char *end_ptr;
5115 	text	   *result;
5116 	int			arg;
5117 	bool		funcvariadic;
5118 	int			nargs;
5119 	Datum	   *elements = NULL;
5120 	bool	   *nulls = NULL;
5121 	Oid			element_type = InvalidOid;
5122 	Oid			prev_type = InvalidOid;
5123 	Oid			prev_width_type = InvalidOid;
5124 	FmgrInfo	typoutputfinfo;
5125 	FmgrInfo	typoutputinfo_width;
5126 
5127 	/* When format string is null, immediately return null */
5128 	if (PG_ARGISNULL(0))
5129 		PG_RETURN_NULL();
5130 
5131 	/* If argument is marked VARIADIC, expand array into elements */
5132 	if (get_fn_expr_variadic(fcinfo->flinfo))
5133 	{
5134 		ArrayType  *arr;
5135 		int16		elmlen;
5136 		bool		elmbyval;
5137 		char		elmalign;
5138 		int			nitems;
5139 
5140 		/* Should have just the one argument */
5141 		Assert(PG_NARGS() == 2);
5142 
5143 		/* If argument is NULL, we treat it as zero-length array */
5144 		if (PG_ARGISNULL(1))
5145 			nitems = 0;
5146 		else
5147 		{
5148 			/*
5149 			 * Non-null argument had better be an array.  We assume that any
5150 			 * call context that could let get_fn_expr_variadic return true
5151 			 * will have checked that a VARIADIC-labeled parameter actually is
5152 			 * an array.  So it should be okay to just Assert that it's an
5153 			 * array rather than doing a full-fledged error check.
5154 			 */
5155 			Assert(OidIsValid(get_base_element_type(get_fn_expr_argtype(fcinfo->flinfo, 1))));
5156 
5157 			/* OK, safe to fetch the array value */
5158 			arr = PG_GETARG_ARRAYTYPE_P(1);
5159 
5160 			/* Get info about array element type */
5161 			element_type = ARR_ELEMTYPE(arr);
5162 			get_typlenbyvalalign(element_type,
5163 								 &elmlen, &elmbyval, &elmalign);
5164 
5165 			/* Extract all array elements */
5166 			deconstruct_array(arr, element_type, elmlen, elmbyval, elmalign,
5167 							  &elements, &nulls, &nitems);
5168 		}
5169 
5170 		nargs = nitems + 1;
5171 		funcvariadic = true;
5172 	}
5173 	else
5174 	{
5175 		/* Non-variadic case, we'll process the arguments individually */
5176 		nargs = PG_NARGS();
5177 		funcvariadic = false;
5178 	}
5179 
5180 	/* Setup for main loop. */
5181 	fmt = PG_GETARG_TEXT_PP(0);
5182 	start_ptr = VARDATA_ANY(fmt);
5183 	end_ptr = start_ptr + VARSIZE_ANY_EXHDR(fmt);
5184 	initStringInfo(&str);
5185 	arg = 1;					/* next argument position to print */
5186 
5187 	/* Scan format string, looking for conversion specifiers. */
5188 	for (cp = start_ptr; cp < end_ptr; cp++)
5189 	{
5190 		int			argpos;
5191 		int			widthpos;
5192 		int			flags;
5193 		int			width;
5194 		Datum		value;
5195 		bool		isNull;
5196 		Oid			typid;
5197 
5198 		/*
5199 		 * If it's not the start of a conversion specifier, just copy it to
5200 		 * the output buffer.
5201 		 */
5202 		if (*cp != '%')
5203 		{
5204 			appendStringInfoCharMacro(&str, *cp);
5205 			continue;
5206 		}
5207 
5208 		ADVANCE_PARSE_POINTER(cp, end_ptr);
5209 
5210 		/* Easy case: %% outputs a single % */
5211 		if (*cp == '%')
5212 		{
5213 			appendStringInfoCharMacro(&str, *cp);
5214 			continue;
5215 		}
5216 
5217 		/* Parse the optional portions of the format specifier */
5218 		cp = text_format_parse_format(cp, end_ptr,
5219 									  &argpos, &widthpos,
5220 									  &flags, &width);
5221 
5222 		/*
5223 		 * Next we should see the main conversion specifier.  Whether or not
5224 		 * an argument position was present, it's known that at least one
5225 		 * character remains in the string at this point.  Experience suggests
5226 		 * that it's worth checking that that character is one of the expected
5227 		 * ones before we try to fetch arguments, so as to produce the least
5228 		 * confusing response to a mis-formatted specifier.
5229 		 */
5230 		if (strchr("sIL", *cp) == NULL)
5231 			ereport(ERROR,
5232 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5233 					 errmsg("unrecognized format() type specifier \"%c\"",
5234 							*cp),
5235 					 errhint("For a single \"%%\" use \"%%%%\".")));
5236 
5237 		/* If indirect width was specified, get its value */
5238 		if (widthpos >= 0)
5239 		{
5240 			/* Collect the specified or next argument position */
5241 			if (widthpos > 0)
5242 				arg = widthpos;
5243 			if (arg >= nargs)
5244 				ereport(ERROR,
5245 						(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5246 						 errmsg("too few arguments for format()")));
5247 
5248 			/* Get the value and type of the selected argument */
5249 			if (!funcvariadic)
5250 			{
5251 				value = PG_GETARG_DATUM(arg);
5252 				isNull = PG_ARGISNULL(arg);
5253 				typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
5254 			}
5255 			else
5256 			{
5257 				value = elements[arg - 1];
5258 				isNull = nulls[arg - 1];
5259 				typid = element_type;
5260 			}
5261 			if (!OidIsValid(typid))
5262 				elog(ERROR, "could not determine data type of format() input");
5263 
5264 			arg++;
5265 
5266 			/* We can treat NULL width the same as zero */
5267 			if (isNull)
5268 				width = 0;
5269 			else if (typid == INT4OID)
5270 				width = DatumGetInt32(value);
5271 			else if (typid == INT2OID)
5272 				width = DatumGetInt16(value);
5273 			else
5274 			{
5275 				/* For less-usual datatypes, convert to text then to int */
5276 				char	   *str;
5277 
5278 				if (typid != prev_width_type)
5279 				{
5280 					Oid			typoutputfunc;
5281 					bool		typIsVarlena;
5282 
5283 					getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
5284 					fmgr_info(typoutputfunc, &typoutputinfo_width);
5285 					prev_width_type = typid;
5286 				}
5287 
5288 				str = OutputFunctionCall(&typoutputinfo_width, value);
5289 
5290 				/* pg_atoi will complain about bad data or overflow */
5291 				width = pg_atoi(str, sizeof(int), '\0');
5292 
5293 				pfree(str);
5294 			}
5295 		}
5296 
5297 		/* Collect the specified or next argument position */
5298 		if (argpos > 0)
5299 			arg = argpos;
5300 		if (arg >= nargs)
5301 			ereport(ERROR,
5302 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5303 					 errmsg("too few arguments for format()")));
5304 
5305 		/* Get the value and type of the selected argument */
5306 		if (!funcvariadic)
5307 		{
5308 			value = PG_GETARG_DATUM(arg);
5309 			isNull = PG_ARGISNULL(arg);
5310 			typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
5311 		}
5312 		else
5313 		{
5314 			value = elements[arg - 1];
5315 			isNull = nulls[arg - 1];
5316 			typid = element_type;
5317 		}
5318 		if (!OidIsValid(typid))
5319 			elog(ERROR, "could not determine data type of format() input");
5320 
5321 		arg++;
5322 
5323 		/*
5324 		 * Get the appropriate typOutput function, reusing previous one if
5325 		 * same type as previous argument.  That's particularly useful in the
5326 		 * variadic-array case, but often saves work even for ordinary calls.
5327 		 */
5328 		if (typid != prev_type)
5329 		{
5330 			Oid			typoutputfunc;
5331 			bool		typIsVarlena;
5332 
5333 			getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
5334 			fmgr_info(typoutputfunc, &typoutputfinfo);
5335 			prev_type = typid;
5336 		}
5337 
5338 		/*
5339 		 * And now we can format the value.
5340 		 */
5341 		switch (*cp)
5342 		{
5343 			case 's':
5344 			case 'I':
5345 			case 'L':
5346 				text_format_string_conversion(&str, *cp, &typoutputfinfo,
5347 											  value, isNull,
5348 											  flags, width);
5349 				break;
5350 			default:
5351 				/* should not get here, because of previous check */
5352 				ereport(ERROR,
5353 						(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5354 						 errmsg("unrecognized format() type specifier \"%c\"",
5355 								*cp),
5356 						 errhint("For a single \"%%\" use \"%%%%\".")));
5357 				break;
5358 		}
5359 	}
5360 
5361 	/* Don't need deconstruct_array results anymore. */
5362 	if (elements != NULL)
5363 		pfree(elements);
5364 	if (nulls != NULL)
5365 		pfree(nulls);
5366 
5367 	/* Generate results. */
5368 	result = cstring_to_text_with_len(str.data, str.len);
5369 	pfree(str.data);
5370 
5371 	PG_RETURN_TEXT_P(result);
5372 }
5373 
5374 /*
5375  * Parse contiguous digits as a decimal number.
5376  *
5377  * Returns true if some digits could be parsed.
5378  * The value is returned into *value, and *ptr is advanced to the next
5379  * character to be parsed.
5380  *
5381  * Note parsing invariant: at least one character is known available before
5382  * string end (end_ptr) at entry, and this is still true at exit.
5383  */
5384 static bool
text_format_parse_digits(const char ** ptr,const char * end_ptr,int * value)5385 text_format_parse_digits(const char **ptr, const char *end_ptr, int *value)
5386 {
5387 	bool		found = false;
5388 	const char *cp = *ptr;
5389 	int			val = 0;
5390 
5391 	while (*cp >= '0' && *cp <= '9')
5392 	{
5393 		int8		digit = (*cp - '0');
5394 
5395 		if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
5396 			unlikely(pg_add_s32_overflow(val, digit, &val)))
5397 			ereport(ERROR,
5398 					(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5399 					 errmsg("number is out of range")));
5400 		ADVANCE_PARSE_POINTER(cp, end_ptr);
5401 		found = true;
5402 	}
5403 
5404 	*ptr = cp;
5405 	*value = val;
5406 
5407 	return found;
5408 }
5409 
5410 /*
5411  * Parse a format specifier (generally following the SUS printf spec).
5412  *
5413  * We have already advanced over the initial '%', and we are looking for
5414  * [argpos][flags][width]type (but the type character is not consumed here).
5415  *
5416  * Inputs are start_ptr (the position after '%') and end_ptr (string end + 1).
5417  * Output parameters:
5418  *	argpos: argument position for value to be printed.  -1 means unspecified.
5419  *	widthpos: argument position for width.  Zero means the argument position
5420  *			was unspecified (ie, take the next arg) and -1 means no width
5421  *			argument (width was omitted or specified as a constant).
5422  *	flags: bitmask of flags.
5423  *	width: directly-specified width value.  Zero means the width was omitted
5424  *			(note it's not necessary to distinguish this case from an explicit
5425  *			zero width value).
5426  *
5427  * The function result is the next character position to be parsed, ie, the
5428  * location where the type character is/should be.
5429  *
5430  * Note parsing invariant: at least one character is known available before
5431  * string end (end_ptr) at entry, and this is still true at exit.
5432  */
5433 static const char *
text_format_parse_format(const char * start_ptr,const char * end_ptr,int * argpos,int * widthpos,int * flags,int * width)5434 text_format_parse_format(const char *start_ptr, const char *end_ptr,
5435 						 int *argpos, int *widthpos,
5436 						 int *flags, int *width)
5437 {
5438 	const char *cp = start_ptr;
5439 	int			n;
5440 
5441 	/* set defaults for output parameters */
5442 	*argpos = -1;
5443 	*widthpos = -1;
5444 	*flags = 0;
5445 	*width = 0;
5446 
5447 	/* try to identify first number */
5448 	if (text_format_parse_digits(&cp, end_ptr, &n))
5449 	{
5450 		if (*cp != '$')
5451 		{
5452 			/* Must be just a width and a type, so we're done */
5453 			*width = n;
5454 			return cp;
5455 		}
5456 		/* The number was argument position */
5457 		*argpos = n;
5458 		/* Explicit 0 for argument index is immediately refused */
5459 		if (n == 0)
5460 			ereport(ERROR,
5461 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5462 					 errmsg("format specifies argument 0, but arguments are numbered from 1")));
5463 		ADVANCE_PARSE_POINTER(cp, end_ptr);
5464 	}
5465 
5466 	/* Handle flags (only minus is supported now) */
5467 	while (*cp == '-')
5468 	{
5469 		*flags |= TEXT_FORMAT_FLAG_MINUS;
5470 		ADVANCE_PARSE_POINTER(cp, end_ptr);
5471 	}
5472 
5473 	if (*cp == '*')
5474 	{
5475 		/* Handle indirect width */
5476 		ADVANCE_PARSE_POINTER(cp, end_ptr);
5477 		if (text_format_parse_digits(&cp, end_ptr, &n))
5478 		{
5479 			/* number in this position must be closed by $ */
5480 			if (*cp != '$')
5481 				ereport(ERROR,
5482 						(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5483 						 errmsg("width argument position must be ended by \"$\"")));
5484 			/* The number was width argument position */
5485 			*widthpos = n;
5486 			/* Explicit 0 for argument index is immediately refused */
5487 			if (n == 0)
5488 				ereport(ERROR,
5489 						(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5490 						 errmsg("format specifies argument 0, but arguments are numbered from 1")));
5491 			ADVANCE_PARSE_POINTER(cp, end_ptr);
5492 		}
5493 		else
5494 			*widthpos = 0;		/* width's argument position is unspecified */
5495 	}
5496 	else
5497 	{
5498 		/* Check for direct width specification */
5499 		if (text_format_parse_digits(&cp, end_ptr, &n))
5500 			*width = n;
5501 	}
5502 
5503 	/* cp should now be pointing at type character */
5504 	return cp;
5505 }
5506 
5507 /*
5508  * Format a %s, %I, or %L conversion
5509  */
5510 static void
text_format_string_conversion(StringInfo buf,char conversion,FmgrInfo * typOutputInfo,Datum value,bool isNull,int flags,int width)5511 text_format_string_conversion(StringInfo buf, char conversion,
5512 							  FmgrInfo *typOutputInfo,
5513 							  Datum value, bool isNull,
5514 							  int flags, int width)
5515 {
5516 	char	   *str;
5517 
5518 	/* Handle NULL arguments before trying to stringify the value. */
5519 	if (isNull)
5520 	{
5521 		if (conversion == 's')
5522 			text_format_append_string(buf, "", flags, width);
5523 		else if (conversion == 'L')
5524 			text_format_append_string(buf, "NULL", flags, width);
5525 		else if (conversion == 'I')
5526 			ereport(ERROR,
5527 					(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5528 					 errmsg("null values cannot be formatted as an SQL identifier")));
5529 		return;
5530 	}
5531 
5532 	/* Stringify. */
5533 	str = OutputFunctionCall(typOutputInfo, value);
5534 
5535 	/* Escape. */
5536 	if (conversion == 'I')
5537 	{
5538 		/* quote_identifier may or may not allocate a new string. */
5539 		text_format_append_string(buf, quote_identifier(str), flags, width);
5540 	}
5541 	else if (conversion == 'L')
5542 	{
5543 		char	   *qstr = quote_literal_cstr(str);
5544 
5545 		text_format_append_string(buf, qstr, flags, width);
5546 		/* quote_literal_cstr() always allocates a new string */
5547 		pfree(qstr);
5548 	}
5549 	else
5550 		text_format_append_string(buf, str, flags, width);
5551 
5552 	/* Cleanup. */
5553 	pfree(str);
5554 }
5555 
5556 /*
5557  * Append str to buf, padding as directed by flags/width
5558  */
5559 static void
text_format_append_string(StringInfo buf,const char * str,int flags,int width)5560 text_format_append_string(StringInfo buf, const char *str,
5561 						  int flags, int width)
5562 {
5563 	bool		align_to_left = false;
5564 	int			len;
5565 
5566 	/* fast path for typical easy case */
5567 	if (width == 0)
5568 	{
5569 		appendStringInfoString(buf, str);
5570 		return;
5571 	}
5572 
5573 	if (width < 0)
5574 	{
5575 		/* Negative width: implicit '-' flag, then take absolute value */
5576 		align_to_left = true;
5577 		/* -INT_MIN is undefined */
5578 		if (width <= INT_MIN)
5579 			ereport(ERROR,
5580 					(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5581 					 errmsg("number is out of range")));
5582 		width = -width;
5583 	}
5584 	else if (flags & TEXT_FORMAT_FLAG_MINUS)
5585 		align_to_left = true;
5586 
5587 	len = pg_mbstrlen(str);
5588 	if (align_to_left)
5589 	{
5590 		/* left justify */
5591 		appendStringInfoString(buf, str);
5592 		if (len < width)
5593 			appendStringInfoSpaces(buf, width - len);
5594 	}
5595 	else
5596 	{
5597 		/* right justify */
5598 		if (len < width)
5599 			appendStringInfoSpaces(buf, width - len);
5600 		appendStringInfoString(buf, str);
5601 	}
5602 }
5603 
5604 /*
5605  * text_format_nv - nonvariadic wrapper for text_format function.
5606  *
5607  * note: this wrapper is necessary to pass the sanity check in opr_sanity,
5608  * which checks that all built-in functions that share the implementing C
5609  * function take the same number of arguments.
5610  */
5611 Datum
text_format_nv(PG_FUNCTION_ARGS)5612 text_format_nv(PG_FUNCTION_ARGS)
5613 {
5614 	return text_format(fcinfo);
5615 }
5616 
5617 /*
5618  * Helper function for Levenshtein distance functions. Faster than memcmp(),
5619  * for this use case.
5620  */
5621 static inline bool
rest_of_char_same(const char * s1,const char * s2,int len)5622 rest_of_char_same(const char *s1, const char *s2, int len)
5623 {
5624 	while (len > 0)
5625 	{
5626 		len--;
5627 		if (s1[len] != s2[len])
5628 			return false;
5629 	}
5630 	return true;
5631 }
5632 
5633 /* Expand each Levenshtein distance variant */
5634 #include "levenshtein.c"
5635 #define LEVENSHTEIN_LESS_EQUAL
5636 #include "levenshtein.c"
5637