112 #define TEXTBUFLEN 1024
114 #define DatumGetVarStringP(X) ((VarString *) PG_DETOAST_DATUM(X))
115 #define DatumGetVarStringPP(X) ((VarString *) PG_DETOAST_DATUM_PACKED(X))
130 bool length_not_specified);
145 bool length_not_specified);
154 const char *fldsep,
const char *null_string);
160 int *argpos,
int *widthpos,
161 int *flags,
int *width);
165 int flags,
int width);
167 int flags,
int width);
255 if (dst_len >= src_len)
263 if (srcunpacked != src)
273 #define VAL(CH) ((CH) - '0')
274 #define DIG(VAL) ((VAL) + '0')
291 Node *escontext = fcinfo->context;
298 if (inputText[0] ==
'\\' && inputText[1] ==
'x')
300 size_t len = strlen(inputText);
312 for (bc = 0, tp = inputText; *tp !=
'\0'; bc++)
316 else if ((tp[0] ==
'\\') &&
317 (tp[1] >=
'0' && tp[1] <=
'3') &&
318 (tp[2] >=
'0' && tp[2] <=
'7') &&
319 (tp[3] >=
'0' && tp[3] <=
'7'))
321 else if ((tp[0] ==
'\\') &&
330 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
331 errmsg(
"invalid input syntax for type %s",
"bytea")));
346 else if ((tp[0] ==
'\\') &&
347 (tp[1] >=
'0' && tp[1] <=
'3') &&
348 (tp[2] >=
'0' && tp[2] <=
'7') &&
349 (tp[3] >=
'0' && tp[3] <=
'7'))
355 *rp++ = bc +
VAL(tp[3]);
359 else if ((tp[0] ==
'\\') &&
371 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
372 errmsg(
"invalid input syntax for type %s",
"bytea")));
413 else if ((
unsigned char) *vp < 0x20 || (
unsigned char) *vp > 0x7e)
426 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
438 else if ((
unsigned char) *vp < 0x20 || (
unsigned char) *vp > 0x7e)
457 elog(
ERROR,
"unrecognized bytea_output setting: %d",
475 nbytes =
buf->len -
buf->cursor;
506 bool isfirst =
false;
815 for (s = p; n > 0; n--)
900 if (length_not_specified)
907 (
errcode(ERRCODE_SUBSTRING_ERROR),
908 errmsg(
"negative substring length not allowed")));
963 if (length_not_specified)
965 slice_size = L1 = -1;
970 (
errcode(ERRCODE_SUBSTRING_ERROR),
971 errmsg(
"negative substring length not allowed")));
972 slice_size = L1 = -1;
980 slice_size = L1 = -1;
1033 if (S1 > slice_strlen)
1045 E1 =
Min(S1 + L1, slice_start + 1 + slice_strlen);
1047 E1 = slice_start + 1 + slice_strlen;
1053 for (
i = 0;
i < S1 - 1;
i++)
1063 for (
i = S1;
i < E1;
i++)
1068 memcpy(
VARDATA(ret), s, (p - s));
1076 elog(
ERROR,
"invalid backend encoding: encoding max length < 1");
1127 (
errcode(ERRCODE_SUBSTRING_ERROR),
1128 errmsg(
"negative substring length not allowed")));
1131 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1132 errmsg(
"integer out of range")));
1226 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1227 errmsg(
"nondeterministic collations are not supported for substring searches")));
1242 state->is_multibyte_char_in_char =
false;
1244 state->is_multibyte_char_in_char =
false;
1246 state->is_multibyte_char_in_char =
true;
1252 state->last_match = NULL;
1266 if (len1 >= len2 && len2 > 1)
1268 int searchlength = len1 - len2;
1272 const char *str2 =
state->str2;
1286 if (searchlength < 16)
1288 else if (searchlength < 64)
1290 else if (searchlength < 128)
1292 else if (searchlength < 512)
1294 else if (searchlength < 2048)
1296 else if (searchlength < 4096)
1297 skiptablemask = 127;
1299 skiptablemask = 255;
1300 state->skiptablemask = skiptablemask;
1307 for (
i = 0;
i <= skiptablemask;
i++)
1308 state->skiptable[
i] = len2;
1319 for (
i = 0;
i < last;
i++)
1320 state->skiptable[(
unsigned char) str2[
i] & skiptablemask] = last -
i;
1335 int needle_len =
state->len2;
1339 if (needle_len <= 0)
1343 if (
state->last_match)
1344 start_ptr =
state->last_match + needle_len;
1346 start_ptr =
state->str1;
1360 if (
state->is_multibyte_char_in_char)
1367 while (
state->refpoint < matchptr)
1379 if (
state->refpoint > matchptr)
1381 start_ptr =
state->refpoint;
1387 state->last_match = matchptr;
1399 int haystack_len =
state->len1;
1400 int needle_len =
state->len2;
1401 int skiptablemask =
state->skiptablemask;
1402 const char *haystack =
state->str1;
1403 const char *needle =
state->str2;
1404 const char *haystack_end = &haystack[haystack_len];
1407 Assert(start_ptr >= haystack && start_ptr <= haystack_end);
1409 if (needle_len == 1)
1412 char nchar = *needle;
1415 while (hptr < haystack_end)
1418 return (
char *) hptr;
1424 const char *needle_last = &needle[needle_len - 1];
1427 hptr = start_ptr + needle_len - 1;
1428 while (hptr < haystack_end)
1452 hptr +=
state->skiptable[(
unsigned char) *hptr & skiptablemask];
1467 return state->last_match;
1482 return state->refpos + 1;
1494 state->last_match = NULL;
1516 (
errcode(ERRCODE_INDETERMINATE_COLLATION),
1517 errmsg(
"could not determine which collation to use for string comparison"),
1518 errhint(
"Use the COLLATE clause to set the collation explicitly.")));
1550 result = memcmp(arg1, arg2,
Min(len1, len2));
1551 if ((result == 0) && (len1 != len2))
1552 result = (len1 < len2) ? -1 : 1;
1569 if (len1 == len2 && memcmp(arg1, arg2, len1) == 0)
1572 result =
pg_strncoll(arg1, len1, arg2, len2, mylocale);
1577 result = memcmp(arg1, arg2,
Min(len1, len2));
1578 if ((result == 0) && (len1 != len2))
1579 result = (len1 < len2) ? -1 : 1;
1619 bool locale_is_c =
false;
1678 bool locale_is_c =
false;
1805 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1806 errmsg(
"nondeterministic collations are not supported for substring searches")));
1873 bool collate_c =
false;
1894 if (typid == BPCHAROID)
1896 else if (typid == NAMEOID)
1919 if (typid == NAMEOID)
1954 if (abbreviate || !collate_c)
2025 result = memcmp(a1p, a2p,
Min(len1, len2));
2026 if ((result == 0) && (len1 != len2))
2027 result = (len1 < len2) ? -1 : 1;
2062 result = memcmp(a1p, a2p,
Min(len1, len2));
2063 if ((result == 0) && (len1 != len2))
2064 result = (len1 < len2) ? -1 : 1;
2143 if (len1 == len2 && memcmp(a1p, a2p, len1) == 0)
2161 if (sss->
typid == BPCHAROID)
2190 if (len1 != sss->
last_len1 || memcmp(sss->
buf1, a1p, len1) != 0)
2193 memcpy(sss->
buf1, a1p, len1);
2194 sss->
buf1[len1] =
'\0';
2204 if (len2 != sss->
last_len2 || memcmp(sss->
buf2, a2p, len2) != 0)
2206 memcpy(sss->
buf2, a2p, len2);
2207 sss->
buf2[len2] =
'\0';
2220 result = strcmp(sss->
buf1, sss->
buf2);
2238 const size_t max_prefix_bytes =
sizeof(
Datum);
2241 char *authoritative_data =
VARDATA_ANY(authoritative);
2249 pres = (
char *) &
res;
2251 memset(pres, 0, max_prefix_bytes);
2255 if (sss->
typid == BPCHAROID)
2286 memcpy(pres, authoritative_data,
Min(
len, max_prefix_bytes));
2305 memcmp(sss->
buf1, authoritative_data,
len) == 0)
2312 memcpy(sss->
buf1, authoritative_data,
len);
2322 if (sss->
buflen2 < max_prefix_bytes)
2330 max_prefix_bytes, sss->
locale);
2348 if (bsize < sss->buflen2)
2369 memcpy(pres, sss->
buf2,
Min(max_prefix_bytes, bsize));
2392 #if SIZEOF_DATUM == 8
2419 res = DatumBigEndianToNative(
res);
2423 pfree(authoritative);
2437 double abbrev_distinct,
2443 if (memtupcount < 100)
2454 if (abbrev_distinct <= 1.0)
2455 abbrev_distinct = 1.0;
2457 if (key_distinct <= 1.0)
2468 double norm_abbrev_card = abbrev_distinct / (double) memtupcount;
2470 elog(
LOG,
"varstr_abbrev: abbrev_distinct after %d: %f "
2471 "(key_distinct: %f, norm_abbrev_card: %f, prop_card: %f)",
2472 memtupcount, abbrev_distinct, key_distinct, norm_abbrev_card,
2493 if (abbrev_distinct > key_distinct * sss->
prop_card)
2520 if (memtupcount > 10000)
2539 elog(
LOG,
"varstr_abbrev: aborted abbreviation at %d "
2540 "(abbrev_distinct: %f, key_distinct: %f, prop_card: %f)",
2541 memtupcount, abbrev_distinct, key_distinct, sss->
prop_card);
2560 collid == DEFAULT_COLLATION_OID ||
2601 size_t len1 = strlen(
NameStr(*arg1));
2608 if (
collid == C_COLLATION_OID)
2609 result = (len1 == len2 &&
2627 size_t len2 = strlen(
NameStr(*arg2));
2633 if (
collid == C_COLLATION_OID)
2634 result = (len1 == len2 &&
2651 size_t len1 = strlen(
NameStr(*arg1));
2658 if (
collid == C_COLLATION_OID)
2659 result = !(len1 == len2 &&
2677 size_t len2 = strlen(
NameStr(*arg2));
2683 if (
collid == C_COLLATION_OID)
2684 result = !(len1 == len2 &&
2728 #define CmpCall(cmpfunc) \
2729 DatumGetInt32(DirectFunctionCall2Coll(cmpfunc, \
2730 PG_GET_COLLATION(), \
2731 PG_GETARG_DATUM(0), \
2732 PG_GETARG_DATUM(1)))
2806 else if (len1 < len2)
2808 else if (len1 > len2)
2983 #define PG_STR_GET_BYTEA(str_) \
2984 DatumGetByteaPP(DirectFunctionCall1(byteain, CStringGetDatum(str_)))
3028 bool length_not_specified)
3039 if (length_not_specified)
3051 (
errcode(ERRCODE_SUBSTRING_ERROR),
3052 errmsg(
"negative substring length not allowed")));
3129 (
errcode(ERRCODE_SUBSTRING_ERROR),
3130 errmsg(
"negative substring length not allowed")));
3133 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3134 errmsg(
"integer out of range")));
3185 for (p = 0; p <=
px; p++)
3187 if ((*
p2 == *p1) && (memcmp(p1,
p2, len2) == 0))
3215 if (n < 0 || n >=
len)
3217 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3218 errmsg(
"index %d out of valid range, 0..%d",
3246 if (n < 0 || n >= (int64)
len * 8)
3248 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3249 errmsg(
"index %lld out of valid range, 0..%lld",
3250 (
long long) n, (
long long)
len * 8 - 1)));
3253 byteNo = (int) (n / 8);
3254 bitNo = (int) (n % 8);
3256 byte = ((
unsigned char *)
VARDATA_ANY(v))[byteNo];
3258 if (
byte & (1 << bitNo))
3282 if (n < 0 || n >=
len)
3284 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3285 errmsg(
"index %d out of valid range, 0..%d",
3291 ((
unsigned char *)
VARDATA(
res))[n] = newByte;
3318 if (n < 0 || n >= (int64)
len * 8)
3320 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3321 errmsg(
"index %lld out of valid range, 0..%lld",
3322 (
long long) n, (
long long)
len * 8 - 1)));
3325 byteNo = (int) (n / 8);
3326 bitNo = (int) (n % 8);
3331 if (newBit != 0 && newBit != 1)
3333 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3334 errmsg(
"new bit must be 0 or 1")));
3339 oldByte = ((
unsigned char *)
VARDATA(
res))[byteNo];
3342 newByte = oldByte & (~(1 << bitNo));
3344 newByte = oldByte | (1 << bitNo);
3346 ((
unsigned char *)
VARDATA(
res))[byteNo] = newByte;
3409 (
errcode(ERRCODE_INVALID_NAME),
3410 errmsg(
"invalid name syntax")));
3412 if (namelist ==
NIL)
3414 (
errcode(ERRCODE_INVALID_NAME),
3415 errmsg(
"invalid name syntax")));
3417 foreach(l, namelist)
3419 char *curname = (
char *)
lfirst(l);
3457 char *nextp = rawstring;
3477 curname = nextp + 1;
3480 endp = strchr(nextp + 1,
'"');
3486 memmove(endp, endp + 1, strlen(endp));
3503 if (curname == nextp)
3515 len = endp - curname;
3518 strncpy(curname, downname,
len);
3532 else if (*nextp ==
'\0')
3546 *namelist =
lappend(*namelist, curname);
3584 char *nextp = rawstring;
3604 curname = nextp + 1;
3607 endp = strchr(nextp + 1,
'"');
3613 memmove(endp, endp + 1, strlen(endp));
3622 curname = endp = nextp;
3630 if (curname == endp)
3644 else if (*nextp ==
'\0')
3661 *namelist =
lappend(*namelist, curname);
3705 char *nextp = rawstring;
3725 curname = nextp + 1;
3728 endp = strchr(nextp + 1,
'"');
3734 memmove(endp, endp + 1, strlen(endp));
3748 if (curname == nextp)
3762 else if (*nextp ==
'\0')
3773 *namelist =
lappend(*namelist, curname);
3947 if ((
cmp == 0) && (len1 != len2))
3948 cmp = (len1 < len2) ? -1 : 1;
3999 int from_sub_text_len;
4012 if (src_text_len < 1 || from_sub_text_len < 1)
4037 chunk_len = curr_ptr - start_ptr;
4042 start_ptr = curr_ptr + from_sub_text_len;
4051 chunk_len = ((
char *) src_text +
VARSIZE_ANY(src_text)) - start_ptr;
4079 p = memchr(p,
'\\', p_end - p);
4086 if (*p >=
'1' && *p <=
'9')
4105 char *start_ptr,
int data_pos)
4112 const char *chunk_start = p;
4117 p = memchr(p,
'\\', p_end - p);
4122 if (p > chunk_start)
4137 if (*p >=
'1' && *p <=
'9')
4149 so = pmatch[0].
rm_so;
4150 eo = pmatch[0].
rm_eo;
4153 else if (*p ==
'\\')
4171 if (so >= 0 && eo >= 0)
4181 chunk_start = start_ptr;
4205 int cflags,
Oid collation,
4206 int search_start,
int n)
4231 if (escape_status < 2)
4245 while (search_start <= data_len)
4267 pg_regerror(regexec_result, re, errMsg,
sizeof(errMsg));
4269 (
errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
4270 errmsg(
"regular expression failed: %s", errMsg)));
4277 if (n > 0 && nmatches != n)
4284 search_start = pmatch[0].
rm_eo;
4285 if (pmatch[0].rm_so == pmatch[0].rm_eo)
4294 if (pmatch[0].rm_so - data_pos > 0)
4299 pmatch[0].rm_so - data_pos);
4306 start_ptr += chunk_len;
4307 data_pos = pmatch[0].
rm_so;
4313 if (escape_status > 0)
4315 start_ptr, data_pos);
4321 pmatch[0].rm_eo - data_pos);
4322 data_pos = pmatch[0].
rm_eo;
4336 search_start = data_pos;
4337 if (pmatch[0].rm_so == pmatch[0].rm_eo)
4344 if (data_pos < data_len)
4348 chunk_len = ((
char *) src_text +
VARSIZE_ANY(src_text)) - start_ptr;
4370 int inputstring_len;
4381 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4382 errmsg(
"field position must not be zero")));
4388 if (inputstring_len < 1)
4395 if (fldnum == 1 || fldnum == -1)
4411 if (fldnum == 1 || fldnum == -1)
4433 end_ptr =
VARDATA_ANY(inputstring) + inputstring_len;
4436 end_ptr - start_ptr));
4440 fldnum += numfields + 1;
4459 while (found && --fldnum > 0)
4462 start_ptr = end_ptr + fldsep_len;
4476 int last_len = start_ptr -
VARDATA_ANY(inputstring);
4479 inputstring_len - last_len);
4516 memset(&tstate, 0,
sizeof(tstate));
4521 if (tstate.
astate == NULL)
4556 tstate.tupdesc = rsi->
setDesc;
4594 int inputstring_len;
4629 if (inputstring_len < 1)
4636 null_string, collation);
4656 chunk_len = ((
char *) inputstring +
VARSIZE_ANY(inputstring)) - start_ptr;
4663 chunk_len = end_ptr - start_ptr;
4671 null_string, collation);
4678 start_ptr = end_ptr + fldsep_len;
4694 while (inputstring_len > 0)
4696 int chunk_len =
pg_mblen(start_ptr);
4705 null_string, collation);
4709 start_ptr += chunk_len;
4710 inputstring_len -= chunk_len;
4729 bool is_null =
false;
4731 if (null_string &&
text_isequal(field_value, null_string, collation))
4806 const char *fldsep,
const char *null_string)
4817 bool printed =
false;
4841 if (my_extra == NULL)
4862 typlen = my_extra->
typlen;
4876 if (bitmap && (*bitmap & bitmask) == 0)
4879 if (null_string != NULL)
4890 itemvalue =
fetch_att(p, typbyval, typlen);
4908 if (bitmask == 0x100)
4926 static inline text *
4929 const char *
digits =
"0123456789abcdef";
4933 char *
const end =
buf +
sizeof(
buf);
5018 if (fcinfo->flinfo->fn_extra == NULL)
5025 elog(
ERROR,
"cache lookup failed for type %u", argtypeid);
5029 *((
int *) fcinfo->flinfo->fn_extra) = typlen;
5032 typlen = *((
int *) fcinfo->flinfo->fn_extra);
5039 else if (typlen == -2)
5065 if (fcinfo->flinfo->fn_extra == NULL)
5072 elog(
ERROR,
"cache lookup failed for type %u", argtypeid);
5076 *((
int *) fcinfo->flinfo->fn_extra) = typlen;
5079 typlen = *((
int *) fcinfo->flinfo->fn_extra);
5100 elog(
ERROR,
"invalid compression method id %d", cmid);
5127 elog(
ERROR,
"string_agg_transfn called in non-aggregate context");
5152 bool isfirst =
false;
5204 elog(
ERROR,
"aggregate function called in non-aggregate context");
5231 else if (state2->
len > 0)
5359 elog(
ERROR,
"could not determine data type of concat() input");
5386 bool first_arg =
true;
5429 if (foutcache == NULL)
5546 const char *endp = p +
len;
5581 #define TEXT_FORMAT_FLAG_MINUS 0x0001
5583 #define ADVANCE_PARSE_POINTER(ptr,end_ptr) \
5585 if (++(ptr) >= (end_ptr)) \
5587 (errcode(ERRCODE_INVALID_PARAMETER_VALUE), \
5588 errmsg("unterminated format() type specifier"), \
5589 errhint("For a single \"%%\" use \"%%%%\"."))); \
5601 const char *start_ptr;
5602 const char *end_ptr;
5607 Datum *elements = NULL;
5651 &elmlen, &elmbyval, &elmalign);
5655 &elements, &nulls, &
nitems);
5659 funcvariadic =
true;
5665 funcvariadic =
false;
5676 for (cp = start_ptr; cp < end_ptr; cp++)
5718 if (strchr(
"sIL", *cp) == NULL)
5720 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5721 errmsg(
"unrecognized format() type specifier \"%.*s\"",
5723 errhint(
"For a single \"%%\" use \"%%%%\".")));
5733 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5734 errmsg(
"too few arguments for format()")));
5746 isNull = nulls[
arg - 1];
5747 typid = element_type;
5750 elog(
ERROR,
"could not determine data type of format() input");
5757 else if (typid == INT4OID)
5759 else if (typid == INT2OID)
5766 if (typid != prev_width_type)
5772 fmgr_info(typoutputfunc, &typoutputinfo_width);
5773 prev_width_type = typid;
5790 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5791 errmsg(
"too few arguments for format()")));
5803 isNull = nulls[
arg - 1];
5804 typid = element_type;
5807 elog(
ERROR,
"could not determine data type of format() input");
5816 if (typid != prev_type)
5822 fmgr_info(typoutputfunc, &typoutputfinfo);
5841 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5842 errmsg(
"unrecognized format() type specifier \"%.*s\"",
5844 errhint(
"For a single \"%%\" use \"%%%%\".")));
5850 if (elements != NULL)
5876 const char *cp = *ptr;
5879 while (*cp >=
'0' && *cp <=
'9')
5881 int8 digit = (*cp -
'0');
5886 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5887 errmsg(
"number is out of range")));
5923 int *argpos,
int *widthpos,
5924 int *flags,
int *width)
5926 const char *cp = start_ptr;
5949 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5950 errmsg(
"format specifies argument 0, but arguments are numbered from 1")));
5970 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5971 errmsg(
"width argument position must be ended by \"$\"")));
5977 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5978 errmsg(
"format specifies argument 0, but arguments are numbered from 1")));
6002 int flags,
int width)