1 /*-------------------------------------------------------------------------
4 * Functions for the built-in types char(n) and varchar(n).
6 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/adt/varchar.c
13 *-------------------------------------------------------------------------
18 #include "access/hash.h"
19 #include "access/tuptoaster.h"
20 #include "catalog/pg_collation.h"
21 #include "libpq/pqformat.h"
22 #include "nodes/nodeFuncs.h"
23 #include "utils/array.h"
24 #include "utils/builtins.h"
25 #include "utils/varlena.h"
26 #include "mb/pg_wchar.h"
29 /* common code for bpchartypmodin and varchartypmodin */
31 anychar_typmodin(ArrayType *ta, const char *typename)
37 tl = ArrayGetIntegerTypmods(ta, &n);
40 * we're not too tense about good error message here because grammar
41 * shouldn't allow wrong number of modifiers for CHAR
45 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
46 errmsg("invalid type modifier")));
50 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
51 errmsg("length for type %s must be at least 1", typename)));
52 if (*tl > MaxAttrSize)
54 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
55 errmsg("length for type %s cannot exceed %d",
56 typename, MaxAttrSize)));
59 * For largely historical reasons, the typmod is VARHDRSZ plus the number
60 * of characters; there is enough client-side code that knows about that
61 * that we'd better not change it.
63 typmod = VARHDRSZ + *tl;
68 /* common code for bpchartypmodout and varchartypmodout */
70 anychar_typmodout(int32 typmod)
72 char *res = (char *) palloc(64);
74 if (typmod > VARHDRSZ)
75 snprintf(res, 64, "(%d)", (int) (typmod - VARHDRSZ));
84 * CHAR() and VARCHAR() types are part of the SQL standard. CHAR()
85 * is for blank-padded string whose length is specified in CREATE TABLE.
86 * VARCHAR is for storing string whose length is at most the length specified
87 * at CREATE TABLE time.
89 * It's hard to implement these types because we cannot figure out
90 * the length of the type from the type itself. I changed (hopefully all) the
91 * fmgr calls that invoke input functions of a data type to supply the
92 * length also. (eg. in INSERTs, we have the tupleDescriptor which contains
93 * the length of the attributes and hence the exact length of the char() or
94 * varchar(). We pass this to bpcharin() or varcharin().) In the case where
95 * we cannot determine the length, we pass in -1 instead and the input
96 * converter does not enforce any length check.
98 * We actually implement this as a varlena so that we don't have to pass in
99 * the length for the comparison functions. (The difference between these
100 * types and "text" is that we truncate and possibly blank-pad the string
101 * at insertion time.)
107 /*****************************************************************************
109 *****************************************************************************/
112 * bpchar_input -- common guts of bpcharin and bpcharrecv
114 * s is the input text of length len (may not be null-terminated)
115 * atttypmod is the typmod value to apply
117 * Note that atttypmod is measured in characters, which
118 * is not necessarily the same as the number of bytes.
120 * If the input string is too long, raise an error, unless the extra
121 * characters are spaces, in which case they're truncated. (per SQL)
124 bpchar_input(const char *s, size_t len, int32 atttypmod)
130 /* If typmod is -1 (or invalid), use the actual string length */
131 if (atttypmod < (int32) VARHDRSZ)
135 size_t charlen; /* number of CHARACTERS in the input */
137 maxlen = atttypmod - VARHDRSZ;
138 charlen = pg_mbstrlen_with_len(s, len);
139 if (charlen > maxlen)
141 /* Verify that extra characters are spaces, and clip them off */
142 size_t mbmaxlen = pg_mbcharcliplen(s, len, maxlen);
146 * at this point, len is the actual BYTE length of the input
147 * string, maxlen is the max number of CHARACTERS allowed for this
148 * bpchar type, mbmaxlen is the length in BYTES of those chars.
150 for (j = mbmaxlen; j < len; j++)
154 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
155 errmsg("value too long for type character(%d)",
160 * Now we set maxlen to the necessary byte length, not the number
163 maxlen = len = mbmaxlen;
168 * Now we set maxlen to the necessary byte length, not the number
171 maxlen = len + (maxlen - charlen);
175 result = (BpChar *) palloc(maxlen + VARHDRSZ);
176 SET_VARSIZE(result, maxlen + VARHDRSZ);
180 /* blank pad the string if necessary */
182 memset(r + len, ' ', maxlen - len);
188 * Convert a C string to CHARACTER internal representation. atttypmod
189 * is the declared length of the type plus VARHDRSZ.
192 bpcharin(PG_FUNCTION_ARGS)
194 char *s = PG_GETARG_CSTRING(0);
197 Oid typelem = PG_GETARG_OID(1);
199 int32 atttypmod = PG_GETARG_INT32(2);
202 result = bpchar_input(s, strlen(s), atttypmod);
203 PG_RETURN_BPCHAR_P(result);
208 * Convert a CHARACTER value to a C string.
210 * Uses the text conversion functions, which is only appropriate if BpChar
211 * and text are equivalent types.
214 bpcharout(PG_FUNCTION_ARGS)
216 Datum txt = PG_GETARG_DATUM(0);
218 PG_RETURN_CSTRING(TextDatumGetCString(txt));
222 * bpcharrecv - converts external binary format to bpchar
225 bpcharrecv(PG_FUNCTION_ARGS)
227 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
230 Oid typelem = PG_GETARG_OID(1);
232 int32 atttypmod = PG_GETARG_INT32(2);
237 str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
238 result = bpchar_input(str, nbytes, atttypmod);
240 PG_RETURN_BPCHAR_P(result);
244 * bpcharsend - converts bpchar to binary format
247 bpcharsend(PG_FUNCTION_ARGS)
249 /* Exactly the same as textsend, so share code */
250 return textsend(fcinfo);
255 * Converts a CHARACTER type to the specified size.
257 * maxlen is the typmod, ie, declared length plus VARHDRSZ bytes.
258 * isExplicit is true if this is for an explicit cast to char(N).
260 * Truncation rules: for an explicit cast, silently truncate to the given
261 * length; for an implicit cast, raise error unless extra characters are
262 * all spaces. (This is sort-of per SQL: the spec would actually have us
263 * raise a "completion condition" for the explicit cast case, but Postgres
264 * hasn't got such a concept.)
267 bpchar(PG_FUNCTION_ARGS)
269 BpChar *source = PG_GETARG_BPCHAR_PP(0);
270 int32 maxlen = PG_GETARG_INT32(1);
271 bool isExplicit = PG_GETARG_BOOL(2);
277 int charlen; /* number of characters in the input string +
280 /* No work if typmod is invalid */
281 if (maxlen < (int32) VARHDRSZ)
282 PG_RETURN_BPCHAR_P(source);
286 len = VARSIZE_ANY_EXHDR(source);
287 s = VARDATA_ANY(source);
289 charlen = pg_mbstrlen_with_len(s, len);
291 /* No work if supplied data matches typmod already */
292 if (charlen == maxlen)
293 PG_RETURN_BPCHAR_P(source);
295 if (charlen > maxlen)
297 /* Verify that extra characters are spaces, and clip them off */
300 maxmblen = pg_mbcharcliplen(s, len, maxlen);
304 for (i = maxmblen; i < len; i++)
307 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
308 errmsg("value too long for type character(%d)",
315 * At this point, maxlen is the necessary byte length, not the number
323 * At this point, maxlen is the necessary byte length, not the number
326 maxlen = len + (maxlen - charlen);
329 Assert(maxlen >= len);
331 result = palloc(maxlen + VARHDRSZ);
332 SET_VARSIZE(result, maxlen + VARHDRSZ);
337 /* blank pad the string if necessary */
339 memset(r + len, ' ', maxlen - len);
341 PG_RETURN_BPCHAR_P(result);
346 * Convert char to bpchar(1).
349 char_bpchar(PG_FUNCTION_ARGS)
351 char c = PG_GETARG_CHAR(0);
354 result = (BpChar *) palloc(VARHDRSZ + 1);
356 SET_VARSIZE(result, VARHDRSZ + 1);
357 *(VARDATA(result)) = c;
359 PG_RETURN_BPCHAR_P(result);
364 * Converts a bpchar() type to a NameData type.
367 bpchar_name(PG_FUNCTION_ARGS)
369 BpChar *s = PG_GETARG_BPCHAR_PP(0);
374 len = VARSIZE_ANY_EXHDR(s);
375 s_data = VARDATA_ANY(s);
377 /* Truncate oversize input */
378 if (len >= NAMEDATALEN)
379 len = pg_mbcliplen(s_data, len, NAMEDATALEN - 1);
381 /* Remove trailing blanks */
384 if (s_data[len - 1] != ' ')
389 /* We use palloc0 here to ensure result is zero-padded */
390 result = (Name) palloc0(NAMEDATALEN);
391 memcpy(NameStr(*result), s_data, len);
393 PG_RETURN_NAME(result);
397 * Converts a NameData type to a bpchar type.
399 * Uses the text conversion functions, which is only appropriate if BpChar
400 * and text are equivalent types.
403 name_bpchar(PG_FUNCTION_ARGS)
405 Name s = PG_GETARG_NAME(0);
408 result = (BpChar *) cstring_to_text(NameStr(*s));
409 PG_RETURN_BPCHAR_P(result);
413 bpchartypmodin(PG_FUNCTION_ARGS)
415 ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0);
417 PG_RETURN_INT32(anychar_typmodin(ta, "char"));
421 bpchartypmodout(PG_FUNCTION_ARGS)
423 int32 typmod = PG_GETARG_INT32(0);
425 PG_RETURN_CSTRING(anychar_typmodout(typmod));
429 /*****************************************************************************
430 * varchar - varchar(n)
432 * Note: varchar piggybacks on type text for most operations, and so has no
433 * C-coded functions except for I/O and typmod checking.
434 *****************************************************************************/
437 * varchar_input -- common guts of varcharin and varcharrecv
439 * s is the input text of length len (may not be null-terminated)
440 * atttypmod is the typmod value to apply
442 * Note that atttypmod is measured in characters, which
443 * is not necessarily the same as the number of bytes.
445 * If the input string is too long, raise an error, unless the extra
446 * characters are spaces, in which case they're truncated. (per SQL)
448 * Uses the C string to text conversion function, which is only appropriate
449 * if VarChar and text are equivalent types.
452 varchar_input(const char *s, size_t len, int32 atttypmod)
457 maxlen = atttypmod - VARHDRSZ;
459 if (atttypmod >= (int32) VARHDRSZ && len > maxlen)
461 /* Verify that extra characters are spaces, and clip them off */
462 size_t mbmaxlen = pg_mbcharcliplen(s, len, maxlen);
465 for (j = mbmaxlen; j < len; j++)
469 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
470 errmsg("value too long for type character varying(%d)",
477 result = (VarChar *) cstring_to_text_with_len(s, len);
482 * Convert a C string to VARCHAR internal representation. atttypmod
483 * is the declared length of the type plus VARHDRSZ.
486 varcharin(PG_FUNCTION_ARGS)
488 char *s = PG_GETARG_CSTRING(0);
491 Oid typelem = PG_GETARG_OID(1);
493 int32 atttypmod = PG_GETARG_INT32(2);
496 result = varchar_input(s, strlen(s), atttypmod);
497 PG_RETURN_VARCHAR_P(result);
502 * Convert a VARCHAR value to a C string.
504 * Uses the text to C string conversion function, which is only appropriate
505 * if VarChar and text are equivalent types.
508 varcharout(PG_FUNCTION_ARGS)
510 Datum txt = PG_GETARG_DATUM(0);
512 PG_RETURN_CSTRING(TextDatumGetCString(txt));
516 * varcharrecv - converts external binary format to varchar
519 varcharrecv(PG_FUNCTION_ARGS)
521 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
524 Oid typelem = PG_GETARG_OID(1);
526 int32 atttypmod = PG_GETARG_INT32(2);
531 str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
532 result = varchar_input(str, nbytes, atttypmod);
534 PG_RETURN_VARCHAR_P(result);
538 * varcharsend - converts varchar to binary format
541 varcharsend(PG_FUNCTION_ARGS)
543 /* Exactly the same as textsend, so share code */
544 return textsend(fcinfo);
549 * varchar_transform()
550 * Flatten calls to varchar's length coercion function that set the new maximum
551 * length >= the previous maximum length. We can ignore the isExplicit
552 * argument, since that only affects truncation cases.
555 varchar_transform(PG_FUNCTION_ARGS)
557 FuncExpr *expr = (FuncExpr *) PG_GETARG_POINTER(0);
561 Assert(IsA(expr, FuncExpr));
562 Assert(list_length(expr->args) >= 2);
564 typmod = (Node *) lsecond(expr->args);
566 if (IsA(typmod, Const) &&!((Const *) typmod)->constisnull)
568 Node *source = (Node *) linitial(expr->args);
569 int32 old_typmod = exprTypmod(source);
570 int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
571 int32 old_max = old_typmod - VARHDRSZ;
572 int32 new_max = new_typmod - VARHDRSZ;
574 if (new_typmod < 0 || (old_typmod >= 0 && old_max <= new_max))
575 ret = relabel_to_typmod(source, new_typmod);
578 PG_RETURN_POINTER(ret);
582 * Converts a VARCHAR type to the specified size.
584 * maxlen is the typmod, ie, declared length plus VARHDRSZ bytes.
585 * isExplicit is true if this is for an explicit cast to varchar(N).
587 * Truncation rules: for an explicit cast, silently truncate to the given
588 * length; for an implicit cast, raise error unless extra characters are
589 * all spaces. (This is sort-of per SQL: the spec would actually have us
590 * raise a "completion condition" for the explicit cast case, but Postgres
591 * hasn't got such a concept.)
594 varchar(PG_FUNCTION_ARGS)
596 VarChar *source = PG_GETARG_VARCHAR_PP(0);
597 int32 typmod = PG_GETARG_INT32(1);
598 bool isExplicit = PG_GETARG_BOOL(2);
605 len = VARSIZE_ANY_EXHDR(source);
606 s_data = VARDATA_ANY(source);
607 maxlen = typmod - VARHDRSZ;
609 /* No work if typmod is invalid or supplied data fits it already */
610 if (maxlen < 0 || len <= maxlen)
611 PG_RETURN_VARCHAR_P(source);
613 /* only reach here if string is too long... */
615 /* truncate multibyte string preserving multibyte boundary */
616 maxmblen = pg_mbcharcliplen(s_data, len, maxlen);
620 for (i = maxmblen; i < len; i++)
621 if (s_data[i] != ' ')
623 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
624 errmsg("value too long for type character varying(%d)",
628 PG_RETURN_VARCHAR_P((VarChar *) cstring_to_text_with_len(s_data,
633 varchartypmodin(PG_FUNCTION_ARGS)
635 ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0);
637 PG_RETURN_INT32(anychar_typmodin(ta, "varchar"));
641 varchartypmodout(PG_FUNCTION_ARGS)
643 int32 typmod = PG_GETARG_INT32(0);
645 PG_RETURN_CSTRING(anychar_typmodout(typmod));
649 /*****************************************************************************
651 *****************************************************************************/
653 /* "True" length (not counting trailing blanks) of a BpChar */
655 bcTruelen(BpChar *arg)
657 return bpchartruelen(VARDATA_ANY(arg), VARSIZE_ANY_EXHDR(arg));
661 bpchartruelen(char *s, int len)
666 * Note that we rely on the assumption that ' ' is a singleton unit on
667 * every supported multibyte server encoding.
669 for (i = len - 1; i >= 0; i--)
678 bpcharlen(PG_FUNCTION_ARGS)
680 BpChar *arg = PG_GETARG_BPCHAR_PP(0);
683 /* get number of bytes, ignoring trailing spaces */
684 len = bcTruelen(arg);
686 /* in multibyte encoding, convert to number of characters */
687 if (pg_database_encoding_max_length() != 1)
688 len = pg_mbstrlen_with_len(VARDATA_ANY(arg), len);
690 PG_RETURN_INT32(len);
694 bpcharoctetlen(PG_FUNCTION_ARGS)
696 Datum arg = PG_GETARG_DATUM(0);
698 /* We need not detoast the input at all */
699 PG_RETURN_INT32(toast_raw_datum_size(arg) - VARHDRSZ);
703 /*****************************************************************************
704 * Comparison Functions used for bpchar
706 * Note: btree indexes need these routines not to leak memory; therefore,
707 * be careful to free working copies of toasted datums. Most places don't
708 * need to be so careful.
709 *****************************************************************************/
712 bpchareq(PG_FUNCTION_ARGS)
714 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
715 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
720 len1 = bcTruelen(arg1);
721 len2 = bcTruelen(arg2);
724 * Since we only care about equality or not-equality, we can avoid all the
725 * expense of strcoll() here, and just do bitwise comparison.
730 result = (memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), len1) == 0);
732 PG_FREE_IF_COPY(arg1, 0);
733 PG_FREE_IF_COPY(arg2, 1);
735 PG_RETURN_BOOL(result);
739 bpcharne(PG_FUNCTION_ARGS)
741 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
742 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
747 len1 = bcTruelen(arg1);
748 len2 = bcTruelen(arg2);
751 * Since we only care about equality or not-equality, we can avoid all the
752 * expense of strcoll() here, and just do bitwise comparison.
757 result = (memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), len1) != 0);
759 PG_FREE_IF_COPY(arg1, 0);
760 PG_FREE_IF_COPY(arg2, 1);
762 PG_RETURN_BOOL(result);
766 bpcharlt(PG_FUNCTION_ARGS)
768 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
769 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
774 len1 = bcTruelen(arg1);
775 len2 = bcTruelen(arg2);
777 cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
780 PG_FREE_IF_COPY(arg1, 0);
781 PG_FREE_IF_COPY(arg2, 1);
783 PG_RETURN_BOOL(cmp < 0);
787 bpcharle(PG_FUNCTION_ARGS)
789 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
790 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
795 len1 = bcTruelen(arg1);
796 len2 = bcTruelen(arg2);
798 cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
801 PG_FREE_IF_COPY(arg1, 0);
802 PG_FREE_IF_COPY(arg2, 1);
804 PG_RETURN_BOOL(cmp <= 0);
808 bpchargt(PG_FUNCTION_ARGS)
810 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
811 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
816 len1 = bcTruelen(arg1);
817 len2 = bcTruelen(arg2);
819 cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
822 PG_FREE_IF_COPY(arg1, 0);
823 PG_FREE_IF_COPY(arg2, 1);
825 PG_RETURN_BOOL(cmp > 0);
829 bpcharge(PG_FUNCTION_ARGS)
831 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
832 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
837 len1 = bcTruelen(arg1);
838 len2 = bcTruelen(arg2);
840 cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
843 PG_FREE_IF_COPY(arg1, 0);
844 PG_FREE_IF_COPY(arg2, 1);
846 PG_RETURN_BOOL(cmp >= 0);
850 bpcharcmp(PG_FUNCTION_ARGS)
852 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
853 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
858 len1 = bcTruelen(arg1);
859 len2 = bcTruelen(arg2);
861 cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
864 PG_FREE_IF_COPY(arg1, 0);
865 PG_FREE_IF_COPY(arg2, 1);
867 PG_RETURN_INT32(cmp);
871 bpchar_sortsupport(PG_FUNCTION_ARGS)
873 SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
874 Oid collid = ssup->ssup_collation;
875 MemoryContext oldcontext;
877 oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
879 /* Use generic string SortSupport */
880 varstr_sortsupport(ssup, collid, true);
882 MemoryContextSwitchTo(oldcontext);
888 bpchar_larger(PG_FUNCTION_ARGS)
890 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
891 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
896 len1 = bcTruelen(arg1);
897 len2 = bcTruelen(arg2);
899 cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
902 PG_RETURN_BPCHAR_P((cmp >= 0) ? arg1 : arg2);
906 bpchar_smaller(PG_FUNCTION_ARGS)
908 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
909 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
914 len1 = bcTruelen(arg1);
915 len2 = bcTruelen(arg2);
917 cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
920 PG_RETURN_BPCHAR_P((cmp <= 0) ? arg1 : arg2);
925 * bpchar needs a specialized hash function because we want to ignore
926 * trailing blanks in comparisons.
928 * Note: currently there is no need for locale-specific behavior here,
929 * but if we ever change the semantics of bpchar comparison to trust
930 * strcoll() completely, we'd need to do something different in non-C locales.
933 hashbpchar(PG_FUNCTION_ARGS)
935 BpChar *key = PG_GETARG_BPCHAR_PP(0);
940 keydata = VARDATA_ANY(key);
941 keylen = bcTruelen(key);
943 result = hash_any((unsigned char *) keydata, keylen);
945 /* Avoid leaking memory for toasted inputs */
946 PG_FREE_IF_COPY(key, 0);
953 * The following operators support character-by-character comparison
954 * of bpchar datums, to allow building indexes suitable for LIKE clauses.
955 * Note that the regular bpchareq/bpcharne comparison operators, and
956 * regular support functions 1 and 2 with "C" collation are assumed to be
957 * compatible with these!
961 internal_bpchar_pattern_compare(BpChar *arg1, BpChar *arg2)
967 len1 = bcTruelen(arg1);
968 len2 = bcTruelen(arg2);
970 result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
973 else if (len1 < len2)
975 else if (len1 > len2)
983 bpchar_pattern_lt(PG_FUNCTION_ARGS)
985 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
986 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
989 result = internal_bpchar_pattern_compare(arg1, arg2);
991 PG_FREE_IF_COPY(arg1, 0);
992 PG_FREE_IF_COPY(arg2, 1);
994 PG_RETURN_BOOL(result < 0);
999 bpchar_pattern_le(PG_FUNCTION_ARGS)
1001 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1002 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1005 result = internal_bpchar_pattern_compare(arg1, arg2);
1007 PG_FREE_IF_COPY(arg1, 0);
1008 PG_FREE_IF_COPY(arg2, 1);
1010 PG_RETURN_BOOL(result <= 0);
1015 bpchar_pattern_ge(PG_FUNCTION_ARGS)
1017 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1018 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1021 result = internal_bpchar_pattern_compare(arg1, arg2);
1023 PG_FREE_IF_COPY(arg1, 0);
1024 PG_FREE_IF_COPY(arg2, 1);
1026 PG_RETURN_BOOL(result >= 0);
1031 bpchar_pattern_gt(PG_FUNCTION_ARGS)
1033 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1034 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1037 result = internal_bpchar_pattern_compare(arg1, arg2);
1039 PG_FREE_IF_COPY(arg1, 0);
1040 PG_FREE_IF_COPY(arg2, 1);
1042 PG_RETURN_BOOL(result > 0);
1047 btbpchar_pattern_cmp(PG_FUNCTION_ARGS)
1049 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1050 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1053 result = internal_bpchar_pattern_compare(arg1, arg2);
1055 PG_FREE_IF_COPY(arg1, 0);
1056 PG_FREE_IF_COPY(arg2, 1);
1058 PG_RETURN_INT32(result);
1063 btbpchar_pattern_sortsupport(PG_FUNCTION_ARGS)
1065 SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
1066 MemoryContext oldcontext;
1068 oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
1070 /* Use generic string SortSupport, forcing "C" collation */
1071 varstr_sortsupport(ssup, C_COLLATION_OID, true);
1073 MemoryContextSwitchTo(oldcontext);