114 #define TEXTBUFLEN 1024
116 #define DatumGetVarStringP(X) ((VarString *) PG_DETOAST_DATUM(X))
117 #define DatumGetVarStringPP(X) ((VarString *) PG_DETOAST_DATUM_PACKED(X))
132 bool length_not_specified);
147 bool length_not_specified);
156 const char *fldsep,
const char *null_string);
162 int *argpos,
int *widthpos,
163 int *flags,
int *width);
167 int flags,
int width);
169 int flags,
int width);
257 if (dst_len >= src_len)
265 if (srcunpacked != src)
275 #define VAL(CH) ((CH) - '0')
276 #define DIG(VAL) ((VAL) + '0')
293 Node *escontext = fcinfo->context;
300 if (inputText[0] ==
'\\' && inputText[1] ==
'x')
302 size_t len = strlen(inputText);
314 for (bc = 0, tp = inputText; *tp !=
'\0'; bc++)
318 else if ((tp[0] ==
'\\') &&
319 (tp[1] >=
'0' && tp[1] <=
'3') &&
320 (tp[2] >=
'0' && tp[2] <=
'7') &&
321 (tp[3] >=
'0' && tp[3] <=
'7'))
323 else if ((tp[0] ==
'\\') &&
332 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
333 errmsg(
"invalid input syntax for type %s",
"bytea")));
348 else if ((tp[0] ==
'\\') &&
349 (tp[1] >=
'0' && tp[1] <=
'3') &&
350 (tp[2] >=
'0' && tp[2] <=
'7') &&
351 (tp[3] >=
'0' && tp[3] <=
'7'))
357 *rp++ = bc +
VAL(tp[3]);
361 else if ((tp[0] ==
'\\') &&
373 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
374 errmsg(
"invalid input syntax for type %s",
"bytea")));
415 else if ((
unsigned char) *vp < 0x20 || (
unsigned char) *vp > 0x7e)
428 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
440 else if ((
unsigned char) *vp < 0x20 || (
unsigned char) *vp > 0x7e)
459 elog(
ERROR,
"unrecognized \"bytea_output\" setting: %d",
477 nbytes =
buf->len -
buf->cursor;
508 bool isfirst =
false;
817 for (s = p; n > 0; n--)
903 if (length_not_specified)
910 (
errcode(ERRCODE_SUBSTRING_ERROR),
911 errmsg(
"negative substring length not allowed")));
966 if (length_not_specified)
968 slice_size = L1 = -1;
973 (
errcode(ERRCODE_SUBSTRING_ERROR),
974 errmsg(
"negative substring length not allowed")));
975 slice_size = L1 = -1;
983 slice_size = L1 = -1;
1036 if (S1 > slice_strlen)
1048 E1 =
Min(S1 + L1, slice_start + 1 + slice_strlen);
1050 E1 = slice_start + 1 + slice_strlen;
1056 for (
i = 0;
i < S1 - 1;
i++)
1066 for (
i = S1;
i < E1;
i++)
1071 memcpy(
VARDATA(ret), s, (p - s));
1079 elog(
ERROR,
"invalid backend encoding: encoding max length < 1");
1130 (
errcode(ERRCODE_SUBSTRING_ERROR),
1131 errmsg(
"negative substring length not allowed")));
1134 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1135 errmsg(
"integer out of range")));
1228 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1229 errmsg(
"nondeterministic collations are not supported for substring searches")));
1244 state->is_multibyte_char_in_char =
false;
1246 state->is_multibyte_char_in_char =
false;
1248 state->is_multibyte_char_in_char =
true;
1254 state->last_match = NULL;
1268 if (len1 >= len2 && len2 > 1)
1270 int searchlength = len1 - len2;
1274 const char *str2 =
state->str2;
1288 if (searchlength < 16)
1290 else if (searchlength < 64)
1292 else if (searchlength < 128)
1294 else if (searchlength < 512)
1296 else if (searchlength < 2048)
1298 else if (searchlength < 4096)
1299 skiptablemask = 127;
1301 skiptablemask = 255;
1302 state->skiptablemask = skiptablemask;
1309 for (
i = 0;
i <= skiptablemask;
i++)
1310 state->skiptable[
i] = len2;
1321 for (
i = 0;
i < last;
i++)
1322 state->skiptable[(
unsigned char) str2[
i] & skiptablemask] = last -
i;
1337 int needle_len =
state->len2;
1341 if (needle_len <= 0)
1345 if (
state->last_match)
1346 start_ptr =
state->last_match + needle_len;
1348 start_ptr =
state->str1;
1362 if (
state->is_multibyte_char_in_char)
1369 while (
state->refpoint < matchptr)
1381 if (
state->refpoint > matchptr)
1383 start_ptr =
state->refpoint;
1389 state->last_match = matchptr;
1401 int haystack_len =
state->len1;
1402 int needle_len =
state->len2;
1403 int skiptablemask =
state->skiptablemask;
1404 const char *haystack =
state->str1;
1405 const char *needle =
state->str2;
1406 const char *haystack_end = &haystack[haystack_len];
1409 Assert(start_ptr >= haystack && start_ptr <= haystack_end);
1411 if (needle_len == 1)
1414 char nchar = *needle;
1417 while (hptr < haystack_end)
1420 return (
char *) hptr;
1426 const char *needle_last = &needle[needle_len - 1];
1429 hptr = start_ptr + needle_len - 1;
1430 while (hptr < haystack_end)
1454 hptr +=
state->skiptable[(
unsigned char) *hptr & skiptablemask];
1469 return state->last_match;
1484 return state->refpos + 1;
1496 state->last_match = NULL;
1518 (
errcode(ERRCODE_INDETERMINATE_COLLATION),
1519 errmsg(
"could not determine which collation to use for string comparison"),
1520 errhint(
"Use the COLLATE clause to set the collation explicitly.")));
1549 result = memcmp(arg1, arg2,
Min(len1, len2));
1550 if ((result == 0) && (len1 != len2))
1551 result = (len1 < len2) ? -1 : 1;
1564 if (len1 == len2 && memcmp(arg1, arg2, len1) == 0)
1567 result =
pg_strncoll(arg1, len1, arg2, len2, mylocale);
1572 result = memcmp(arg1, arg2,
Min(len1, len2));
1573 if ((result == 0) && (len1 != len2))
1574 result = (len1 < len2) ? -1 : 1;
1791 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1792 errmsg(
"nondeterministic collations are not supported for substring searches")));
1859 bool collate_c =
false;
1880 if (
locale->collate_is_c)
1882 if (typid == BPCHAROID)
1884 else if (typid == NAMEOID)
1900 if (typid == NAMEOID)
1935 if (abbreviate || !collate_c)
2009 result = memcmp(a1p, a2p,
Min(len1, len2));
2010 if ((result == 0) && (len1 != len2))
2011 result = (len1 < len2) ? -1 : 1;
2046 result = memcmp(a1p, a2p,
Min(len1, len2));
2047 if ((result == 0) && (len1 != len2))
2048 result = (len1 < len2) ? -1 : 1;
2127 if (len1 == len2 && memcmp(a1p, a2p, len1) == 0)
2145 if (sss->
typid == BPCHAROID)
2174 if (len1 != sss->
last_len1 || memcmp(sss->
buf1, a1p, len1) != 0)
2177 memcpy(sss->
buf1, a1p, len1);
2178 sss->
buf1[len1] =
'\0';
2188 if (len2 != sss->
last_len2 || memcmp(sss->
buf2, a2p, len2) != 0)
2190 memcpy(sss->
buf2, a2p, len2);
2191 sss->
buf2[len2] =
'\0';
2204 result = strcmp(sss->
buf1, sss->
buf2);
2222 const size_t max_prefix_bytes =
sizeof(
Datum);
2225 char *authoritative_data =
VARDATA_ANY(authoritative);
2233 pres = (
char *) &
res;
2235 memset(pres, 0, max_prefix_bytes);
2239 if (sss->
typid == BPCHAROID)
2270 memcpy(pres, authoritative_data,
Min(
len, max_prefix_bytes));
2289 memcmp(sss->
buf1, authoritative_data,
len) == 0)
2296 memcpy(sss->
buf1, authoritative_data,
len);
2306 if (sss->
buflen2 < max_prefix_bytes)
2314 max_prefix_bytes, sss->
locale);
2332 if (bsize < sss->buflen2)
2353 memcpy(pres, sss->
buf2,
Min(max_prefix_bytes, bsize));
2376 #if SIZEOF_DATUM == 8
2403 res = DatumBigEndianToNative(
res);
2407 pfree(authoritative);
2421 double abbrev_distinct,
2427 if (memtupcount < 100)
2438 if (abbrev_distinct <= 1.0)
2439 abbrev_distinct = 1.0;
2441 if (key_distinct <= 1.0)
2451 double norm_abbrev_card = abbrev_distinct / (double) memtupcount;
2453 elog(
LOG,
"varstr_abbrev: abbrev_distinct after %d: %f "
2454 "(key_distinct: %f, norm_abbrev_card: %f, prop_card: %f)",
2455 memtupcount, abbrev_distinct, key_distinct, norm_abbrev_card,
2475 if (abbrev_distinct > key_distinct * sss->
prop_card)
2502 if (memtupcount > 10000)
2520 elog(
LOG,
"varstr_abbrev: aborted abbreviation at %d "
2521 "(abbrev_distinct: %f, key_distinct: %f, prop_card: %f)",
2522 memtupcount, abbrev_distinct, key_distinct, sss->
prop_card);
2579 size_t len1 = strlen(
NameStr(*arg1));
2586 if (
collid == C_COLLATION_OID)
2587 result = (len1 == len2 &&
2605 size_t len2 = strlen(
NameStr(*arg2));
2611 if (
collid == C_COLLATION_OID)
2612 result = (len1 == len2 &&
2629 size_t len1 = strlen(
NameStr(*arg1));
2636 if (
collid == C_COLLATION_OID)
2637 result = !(len1 == len2 &&
2655 size_t len2 = strlen(
NameStr(*arg2));
2661 if (
collid == C_COLLATION_OID)
2662 result = !(len1 == len2 &&
2706 #define CmpCall(cmpfunc) \
2707 DatumGetInt32(DirectFunctionCall2Coll(cmpfunc, \
2708 PG_GET_COLLATION(), \
2709 PG_GETARG_DATUM(0), \
2710 PG_GETARG_DATUM(1)))
2784 else if (len1 < len2)
2786 else if (len1 > len2)
2961 #define PG_STR_GET_BYTEA(str_) \
2962 DatumGetByteaPP(DirectFunctionCall1(byteain, CStringGetDatum(str_)))
3006 bool length_not_specified)
3017 if (length_not_specified)
3029 (
errcode(ERRCODE_SUBSTRING_ERROR),
3030 errmsg(
"negative substring length not allowed")));
3107 (
errcode(ERRCODE_SUBSTRING_ERROR),
3108 errmsg(
"negative substring length not allowed")));
3111 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3112 errmsg(
"integer out of range")));
3163 for (p = 0; p <=
px; p++)
3165 if ((*
p2 == *p1) && (memcmp(p1,
p2, len2) == 0))
3193 if (n < 0 || n >=
len)
3195 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3196 errmsg(
"index %d out of valid range, 0..%d",
3224 if (n < 0 || n >= (int64)
len * 8)
3226 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3227 errmsg(
"index %lld out of valid range, 0..%lld",
3228 (
long long) n, (
long long)
len * 8 - 1)));
3231 byteNo = (int) (n / 8);
3232 bitNo = (int) (n % 8);
3234 byte = ((
unsigned char *)
VARDATA_ANY(v))[byteNo];
3236 if (
byte & (1 << bitNo))
3260 if (n < 0 || n >=
len)
3262 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3263 errmsg(
"index %d out of valid range, 0..%d",
3269 ((
unsigned char *)
VARDATA(
res))[n] = newByte;
3296 if (n < 0 || n >= (int64)
len * 8)
3298 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3299 errmsg(
"index %lld out of valid range, 0..%lld",
3300 (
long long) n, (
long long)
len * 8 - 1)));
3303 byteNo = (int) (n / 8);
3304 bitNo = (int) (n % 8);
3309 if (newBit != 0 && newBit != 1)
3311 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3312 errmsg(
"new bit must be 0 or 1")));
3317 oldByte = ((
unsigned char *)
VARDATA(
res))[byteNo];
3320 newByte = oldByte & (~(1 << bitNo));
3322 newByte = oldByte | (1 << bitNo);
3324 ((
unsigned char *)
VARDATA(
res))[byteNo] = newByte;
3387 (
errcode(ERRCODE_INVALID_NAME),
3388 errmsg(
"invalid name syntax")));
3390 if (namelist ==
NIL)
3392 (
errcode(ERRCODE_INVALID_NAME),
3393 errmsg(
"invalid name syntax")));
3395 foreach(l, namelist)
3397 char *curname = (
char *)
lfirst(l);
3435 char *nextp = rawstring;
3455 curname = nextp + 1;
3458 endp = strchr(nextp + 1,
'"');
3464 memmove(endp, endp + 1, strlen(endp));
3481 if (curname == nextp)
3493 len = endp - curname;
3496 strncpy(curname, downname,
len);
3510 else if (*nextp ==
'\0')
3524 *namelist =
lappend(*namelist, curname);
3562 char *nextp = rawstring;
3582 curname = nextp + 1;
3585 endp = strchr(nextp + 1,
'"');
3591 memmove(endp, endp + 1, strlen(endp));
3600 curname = endp = nextp;
3608 if (curname == endp)
3622 else if (*nextp ==
'\0')
3639 *namelist =
lappend(*namelist, curname);
3683 char *nextp = rawstring;
3703 curname = nextp + 1;
3706 endp = strchr(nextp + 1,
'"');
3712 memmove(endp, endp + 1, strlen(endp));
3726 if (curname == nextp)
3740 else if (*nextp ==
'\0')
3751 *namelist =
lappend(*namelist, curname);
3925 if ((
cmp == 0) && (len1 != len2))
3926 cmp = (len1 < len2) ? -1 : 1;
3977 int from_sub_text_len;
3990 if (src_text_len < 1 || from_sub_text_len < 1)
4015 chunk_len = curr_ptr - start_ptr;
4020 start_ptr = curr_ptr + from_sub_text_len;
4029 chunk_len = ((
char *) src_text +
VARSIZE_ANY(src_text)) - start_ptr;
4057 p = memchr(p,
'\\', p_end - p);
4064 if (*p >=
'1' && *p <=
'9')
4083 char *start_ptr,
int data_pos)
4090 const char *chunk_start = p;
4095 p = memchr(p,
'\\', p_end - p);
4100 if (p > chunk_start)
4115 if (*p >=
'1' && *p <=
'9')
4120 so = pmatch[
idx].rm_so;
4121 eo = pmatch[
idx].rm_eo;
4127 so = pmatch[0].rm_so;
4128 eo = pmatch[0].rm_eo;
4131 else if (*p ==
'\\')
4149 if (so >= 0 && eo >= 0)
4159 chunk_start = start_ptr;
4183 int cflags,
Oid collation,
4184 int search_start,
int n)
4209 if (escape_status < 2)
4223 while (search_start <= data_len)
4245 pg_regerror(regexec_result, re, errMsg,
sizeof(errMsg));
4247 (
errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
4248 errmsg(
"regular expression failed: %s", errMsg)));
4255 if (n > 0 && nmatches != n)
4262 search_start = pmatch[0].rm_eo;
4263 if (pmatch[0].rm_so == pmatch[0].rm_eo)
4272 if (pmatch[0].rm_so - data_pos > 0)
4277 pmatch[0].rm_so - data_pos);
4284 start_ptr += chunk_len;
4285 data_pos = pmatch[0].rm_so;
4291 if (escape_status > 0)
4293 start_ptr, data_pos);
4299 pmatch[0].rm_eo - data_pos);
4300 data_pos = pmatch[0].rm_eo;
4314 search_start = data_pos;
4315 if (pmatch[0].rm_so == pmatch[0].rm_eo)
4322 if (data_pos < data_len)
4326 chunk_len = ((
char *) src_text +
VARSIZE_ANY(src_text)) - start_ptr;
4348 int inputstring_len;
4359 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4360 errmsg(
"field position must not be zero")));
4366 if (inputstring_len < 1)
4373 if (fldnum == 1 || fldnum == -1)
4389 if (fldnum == 1 || fldnum == -1)
4411 end_ptr =
VARDATA_ANY(inputstring) + inputstring_len;
4414 end_ptr - start_ptr));
4418 fldnum += numfields + 1;
4437 while (found && --fldnum > 0)
4440 start_ptr = end_ptr + fldsep_len;
4454 int last_len = start_ptr -
VARDATA_ANY(inputstring);
4457 inputstring_len - last_len);
4494 memset(&tstate, 0,
sizeof(tstate));
4499 if (tstate.
astate == NULL)
4534 tstate.tupdesc = rsi->
setDesc;
4572 int inputstring_len;
4607 if (inputstring_len < 1)
4614 null_string, collation);
4634 chunk_len = ((
char *) inputstring +
VARSIZE_ANY(inputstring)) - start_ptr;
4641 chunk_len = end_ptr - start_ptr;
4649 null_string, collation);
4656 start_ptr = end_ptr + fldsep_len;
4672 while (inputstring_len > 0)
4674 int chunk_len =
pg_mblen(start_ptr);
4683 null_string, collation);
4687 start_ptr += chunk_len;
4688 inputstring_len -= chunk_len;
4707 bool is_null =
false;
4709 if (null_string &&
text_isequal(field_value, null_string, collation))
4784 const char *fldsep,
const char *null_string)
4795 bool printed =
false;
4819 if (my_extra == NULL)
4840 typlen = my_extra->
typlen;
4854 if (bitmap && (*bitmap & bitmask) == 0)
4857 if (null_string != NULL)
4868 itemvalue =
fetch_att(p, typbyval, typlen);
4886 if (bitmask == 0x100)
4904 static inline text *
4907 const char *
digits =
"0123456789abcdef";
4911 char *
const end =
buf +
sizeof(
buf);
4996 if (fcinfo->flinfo->fn_extra == NULL)
5003 elog(
ERROR,
"cache lookup failed for type %u", argtypeid);
5007 *((
int *) fcinfo->flinfo->fn_extra) = typlen;
5010 typlen = *((
int *) fcinfo->flinfo->fn_extra);
5017 else if (typlen == -2)
5043 if (fcinfo->flinfo->fn_extra == NULL)
5050 elog(
ERROR,
"cache lookup failed for type %u", argtypeid);
5054 *((
int *) fcinfo->flinfo->fn_extra) = typlen;
5057 typlen = *((
int *) fcinfo->flinfo->fn_extra);
5078 elog(
ERROR,
"invalid compression method id %d", cmid);
5096 if (fcinfo->flinfo->fn_extra == NULL)
5103 elog(
ERROR,
"cache lookup failed for type %u", argtypeid);
5107 *((
int *) fcinfo->flinfo->fn_extra) = typlen;
5110 typlen = *((
int *) fcinfo->flinfo->fn_extra);
5146 elog(
ERROR,
"string_agg_transfn called in non-aggregate context");
5171 bool isfirst =
false;
5223 elog(
ERROR,
"aggregate function called in non-aggregate context");
5250 else if (state2->
len > 0)
5376 elog(
ERROR,
"could not determine data type of concat() input");
5403 bool first_arg =
true;
5446 if (foutcache == NULL)
5563 const char *endp = p +
len;
5598 #define TEXT_FORMAT_FLAG_MINUS 0x0001
5600 #define ADVANCE_PARSE_POINTER(ptr,end_ptr) \
5602 if (++(ptr) >= (end_ptr)) \
5604 (errcode(ERRCODE_INVALID_PARAMETER_VALUE), \
5605 errmsg("unterminated format() type specifier"), \
5606 errhint("For a single \"%%\" use \"%%%%\"."))); \
5618 const char *start_ptr;
5619 const char *end_ptr;
5624 Datum *elements = NULL;
5668 &elmlen, &elmbyval, &elmalign);
5672 &elements, &nulls, &
nitems);
5676 funcvariadic =
true;
5682 funcvariadic =
false;
5693 for (cp = start_ptr; cp < end_ptr; cp++)
5735 if (strchr(
"sIL", *cp) == NULL)
5737 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5738 errmsg(
"unrecognized format() type specifier \"%.*s\"",
5740 errhint(
"For a single \"%%\" use \"%%%%\".")));
5750 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5751 errmsg(
"too few arguments for format()")));
5763 isNull = nulls[
arg - 1];
5764 typid = element_type;
5767 elog(
ERROR,
"could not determine data type of format() input");
5774 else if (typid == INT4OID)
5776 else if (typid == INT2OID)
5783 if (typid != prev_width_type)
5789 fmgr_info(typoutputfunc, &typoutputinfo_width);
5790 prev_width_type = typid;
5807 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5808 errmsg(
"too few arguments for format()")));
5820 isNull = nulls[
arg - 1];
5821 typid = element_type;
5824 elog(
ERROR,
"could not determine data type of format() input");
5833 if (typid != prev_type)
5839 fmgr_info(typoutputfunc, &typoutputfinfo);
5858 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5859 errmsg(
"unrecognized format() type specifier \"%.*s\"",
5861 errhint(
"For a single \"%%\" use \"%%%%\".")));
5867 if (elements != NULL)
5893 const char *cp = *ptr;
5896 while (*cp >=
'0' && *cp <=
'9')
5898 int8 digit = (*cp -
'0');
5903 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5904 errmsg(
"number is out of range")));
5940 int *argpos,
int *widthpos,
5941 int *flags,
int *width)
5943 const char *cp = start_ptr;
5966 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5967 errmsg(
"format specifies argument 0, but arguments are numbered from 1")));
5987 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5988 errmsg(
"width argument position must be ended by \"$\"")));
5994 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5995 errmsg(
"format specifies argument 0, but arguments are numbered from 1")));