41 #include "utils/fmgrprotos.h"
44 #define HEXDIG(z) ((z)<10 ? ((z)+'0') : ((z)-10+'A'))
47 #define VARBIT_PAD(vb) \
49 int32 pad_ = VARBITPAD(vb); \
50 Assert(pad_ >= 0 && pad_ < BITS_PER_BYTE); \
52 *(VARBITS(vb) + VARBITBYTES(vb) - 1) &= BITMASK << pad_; \
59 #define VARBIT_PAD_LAST(vb, ptr) \
61 int32 pad_ = VARBITPAD(vb); \
62 Assert(pad_ >= 0 && pad_ < BITS_PER_BYTE); \
64 *((ptr) - 1) &= BITMASK << pad_; \
68 #ifdef USE_ASSERT_CHECKING
69 #define VARBIT_CORRECTLY_PADDED(vb) \
71 int32 pad_ = VARBITPAD(vb); \
72 Assert(pad_ >= 0 && pad_ < BITS_PER_BYTE); \
74 (*(VARBITS(vb) + VARBITBYTES(vb) - 1) & ~(BITMASK << pad_)) == 0); \
77 #define VARBIT_CORRECTLY_PADDED(vb) ((void) 0)
82 bool length_not_specified);
104 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
105 errmsg(
"invalid type modifier")));
109 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
110 errmsg(
"length for type %s must be at least 1",
114 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
115 errmsg(
"length for type %s cannot exceed %d",
154 Node *escontext = fcinfo->context;
166 if (input_string[0] ==
'b' || input_string[0] ==
'B')
169 sp = input_string + 1;
171 else if (input_string[0] ==
'x' || input_string[0] ==
'X')
174 sp = input_string + 1;
197 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
198 errmsg(
"bit string length exceeds the maximum allowed (%d)",
209 else if (bitlen != atttypmod)
211 (
errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
212 errmsg(
"bit string length %d does not match type bit(%d)",
213 bitlen, atttypmod)));
233 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
234 errmsg(
"\"%.*s\" is not a valid binary digit",
248 for (bc = 0; *sp; sp++)
250 if (*sp >=
'0' && *sp <=
'9')
252 else if (*sp >=
'A' && *sp <=
'F')
253 x = (
bits8) (*sp -
'A') + 10;
254 else if (*sp >=
'a' && *sp <=
'f')
255 x = (
bits8) (*sp -
'a') + 10;
258 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
259 errmsg(
"\"%.*s\" is not a valid hexadecimal digit",
303 len = (bitlen + 3) / 4;
309 for (
i = 0;
i <
len;
i += 2, sp++)
311 *r++ =
HEXDIG((*sp) >> 4);
312 *r++ =
HEXDIG((*sp) & 0xF);
346 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
347 errmsg(
"invalid length in external bit string")));
353 if (atttypmod > 0 && bitlen != atttypmod)
355 (
errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
356 errmsg(
"bit string length %d does not match type bit(%d)",
357 bitlen, atttypmod)));
405 (
errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
406 errmsg(
"bit string length %d does not match type bit(%d)",
459 Node *escontext = fcinfo->context;
471 if (input_string[0] ==
'b' || input_string[0] ==
'B')
474 sp = input_string + 1;
476 else if (input_string[0] ==
'x' || input_string[0] ==
'X')
479 sp = input_string + 1;
498 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
499 errmsg(
"bit string length exceeds the maximum allowed (%d)",
510 else if (bitlen > atttypmod)
512 (
errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
513 errmsg(
"bit string too long for type bit varying(%d)",
534 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
535 errmsg(
"\"%.*s\" is not a valid binary digit",
549 for (bc = 0; *sp; sp++)
551 if (*sp >=
'0' && *sp <=
'9')
553 else if (*sp >=
'A' && *sp <=
'F')
554 x = (
bits8) (*sp -
'A') + 10;
555 else if (*sp >=
'a' && *sp <=
'f')
556 x = (
bits8) (*sp -
'a') + 10;
559 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
560 errmsg(
"\"%.*s\" is not a valid hexadecimal digit",
619 for (k =
i; k <
len; k++)
651 (
errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
652 errmsg(
"invalid length in external bit string")));
658 if (atttypmod > 0 && bitlen > atttypmod)
660 (
errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
661 errmsg(
"bit string too long for type bit varying(%d)",
722 int32 new_max = new_typmod;
725 if (new_max <= 0 || (old_max > 0 && old_max <= new_max))
756 (
errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
757 errmsg(
"bit string too long for type bit varying(%d)",
834 if (bitlen1 != bitlen2)
835 cmp = (bitlen1 < bitlen2) ? -1 : 1;
853 if (bitlen1 != bitlen2)
856 result = (
bit_cmp(arg1, arg2) == 0);
877 if (bitlen1 != bitlen2)
880 result = (
bit_cmp(arg1, arg2) != 0);
895 result = (
bit_cmp(arg1, arg2) < 0);
910 result = (
bit_cmp(arg1, arg2) <= 0);
925 result = (
bit_cmp(arg1, arg2) > 0);
940 result = (
bit_cmp(arg1, arg2) >= 0);
993 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
994 errmsg(
"bit string length exceeds the maximum allowed (%d)",
1012 else if (bitlen2 > 0)
1019 *pr |= ((*pa >> bit2shift) &
BITMASK);
1022 *pr = (*pa << bit1pad) &
BITMASK;
1072 if (length_not_specified)
1080 (
errcode(ERRCODE_SUBSTRING_ERROR),
1081 errmsg(
"negative substring length not allowed")));
1094 e1 =
Min(
e, bitlen + 1);
1096 if (
s1 > bitlen || e1 <=
s1)
1129 for (
i = 0;
i <
len;
i++)
1190 (
errcode(ERRCODE_SUBSTRING_ERROR),
1191 errmsg(
"negative substring length not allowed")));
1194 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1195 errmsg(
"integer out of range")));
1258 if (bitlen1 != bitlen2)
1260 (
errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
1261 errmsg(
"cannot AND bit strings of different sizes")));
1272 *r++ = *p1++ & *
p2++;
1299 if (bitlen1 != bitlen2)
1301 (
errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
1302 errmsg(
"cannot OR bit strings of different sizes")));
1312 *r++ = *p1++ | *
p2++;
1339 if (bitlen1 != bitlen2)
1341 (
errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
1342 errmsg(
"cannot XOR bit strings of different sizes")));
1353 *r++ = *p1++ ^ *
p2++;
1498 MemSet(r, 0, byte_shift);
1550 destbitsleft = typmod;
1553 srcbitsleft =
Min(srcbitsleft, destbitsleft);
1555 while (destbitsleft >= srcbitsleft + 8)
1561 if (destbitsleft > srcbitsleft)
1563 unsigned int val = (
unsigned int) (
a >> (destbitsleft - 8));
1567 val |= ((
unsigned int) -1) << (srcbitsleft + 8 - destbitsleft);
1573 while (destbitsleft >= 8)
1579 if (destbitsleft > 0)
1595 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1596 errmsg(
"integer out of range")));
1630 destbitsleft = typmod;
1633 srcbitsleft =
Min(srcbitsleft, destbitsleft);
1635 while (destbitsleft >= srcbitsleft + 8)
1641 if (destbitsleft > srcbitsleft)
1643 unsigned int val = (
unsigned int) (
a >> (destbitsleft - 8));
1647 val |= ((
unsigned int) -1) << (srcbitsleft + 8 - destbitsleft);
1653 while (destbitsleft >= 8)
1659 if (destbitsleft > 0)
1675 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1676 errmsg(
"bigint out of range")));
1720 if ((str_length == 0) || (substr_length > str_length))
1724 if (substr_length == 0)
1744 mask1 &= end_mask >> is;
1748 if (mask1 & ~str_mask)
1756 is_match = ((
cmp ^ *p) & mask1) == 0;
1764 is_match = mask2 == 0;
1767 i, is, end_mask, mask2, is_match);
1777 if (mask2 & ~str_mask)
1785 is_match = ((
cmp ^ *p) & mask2) == 0;
1821 if (n < 0 || n >= bitlen)
1823 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1824 errmsg(
"bit index %d out of valid range (0..%d)",
1830 if (newBit != 0 && newBit != 1)
1832 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1833 errmsg(
"new bit must be 0 or 1")));
1852 r[byteNo] &= (~(1 << bitNo));
1854 r[byteNo] |= (1 << bitNo);
1879 if (n < 0 || n >= bitlen)
1881 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1882 errmsg(
"bit index %d out of valid range (0..%d)",
1890 if (p[byteNo] & (1 << bitNo))
#define PG_GETARG_ARRAYTYPE_P(n)
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
#define IS_HIGHBIT_SET(ch)
#define Assert(condition)
#define MemSet(start, val, len)
static void PGresult const char * p2
static void PGresult * res
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereturn(context, dummy_value,...)
#define ereport(elevel,...)
#define PG_FREE_IF_COPY(ptr, n)
#define PG_RETURN_BYTEA_P(x)
#define DirectFunctionCall2(func, arg1, arg2)
#define PG_GETARG_POINTER(n)
#define PG_RETURN_CSTRING(x)
#define PG_RETURN_INT64(x)
#define PG_GETARG_CSTRING(n)
#define PG_GETARG_INT64(n)
#define PG_RETURN_INT32(x)
#define PG_GETARG_INT32(n)
#define PG_GETARG_BOOL(n)
#define PG_RETURN_DATUM(x)
#define PG_RETURN_POINTER(x)
#define PG_RETURN_BOOL(x)
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
int pg_mblen(const char *mbstr)
void * palloc0(Size size)
int32 exprTypmod(const Node *expr)
Node * relabel_to_typmod(Node *expr, int32 typmod)
#define IsA(nodeptr, _type_)
static uint64 pg_popcount(const char *buf, int bytes)
static int list_length(const List *l)
static rewind_source * source
static Datum Int32GetDatum(int32 X)
static int32 DatumGetInt32(Datum X)
static int cmp(const chr *x, const chr *y, size_t len)
StringInfoData * StringInfo
#define SET_VARSIZE(PTR, len)
Datum bitoverlay(PG_FUNCTION_ARGS)
Datum bitfromint4(PG_FUNCTION_ARGS)
Datum bitxor(PG_FUNCTION_ARGS)
Datum bitcmp(PG_FUNCTION_ARGS)
Datum bitcat(PG_FUNCTION_ARGS)
Datum bittoint4(PG_FUNCTION_ARGS)
Datum varbit_send(PG_FUNCTION_ARGS)
Datum bitfromint8(PG_FUNCTION_ARGS)
#define VARBIT_CORRECTLY_PADDED(vb)
Datum varbittypmodin(PG_FUNCTION_ARGS)
Datum bitoverlay_no_len(PG_FUNCTION_ARGS)
Datum bit(PG_FUNCTION_ARGS)
Datum bit_send(PG_FUNCTION_ARGS)
Datum varbit(PG_FUNCTION_ARGS)
Datum bitne(PG_FUNCTION_ARGS)
Datum bittypmodout(PG_FUNCTION_ARGS)
Datum varbit_out(PG_FUNCTION_ARGS)
Datum varbit_support(PG_FUNCTION_ARGS)
Datum bit_out(PG_FUNCTION_ARGS)
static char * anybit_typmodout(int32 typmod)
static int32 anybit_typmodin(ArrayType *ta, const char *typename)
Datum bitgetbit(PG_FUNCTION_ARGS)
Datum bitsetbit(PG_FUNCTION_ARGS)
Datum bit_bit_count(PG_FUNCTION_ARGS)
Datum bit_and(PG_FUNCTION_ARGS)
Datum varbit_recv(PG_FUNCTION_ARGS)
Datum bitge(PG_FUNCTION_ARGS)
static VarBit * bit_catenate(VarBit *arg1, VarBit *arg2)
Datum bitle(PG_FUNCTION_ARGS)
static VarBit * bit_overlay(VarBit *t1, VarBit *t2, int sp, int sl)
#define VARBIT_PAD_LAST(vb, ptr)
Datum biteq(PG_FUNCTION_ARGS)
Datum bitsubstr_no_len(PG_FUNCTION_ARGS)
static VarBit * bitsubstring(VarBit *arg, int32 s, int32 l, bool length_not_specified)
Datum bitlt(PG_FUNCTION_ARGS)
Datum bittoint8(PG_FUNCTION_ARGS)
Datum bit_or(PG_FUNCTION_ARGS)
Datum bitgt(PG_FUNCTION_ARGS)
Datum bittypmodin(PG_FUNCTION_ARGS)
Datum bitposition(PG_FUNCTION_ARGS)
Datum bitoctetlength(PG_FUNCTION_ARGS)
Datum bit_in(PG_FUNCTION_ARGS)
Datum bit_recv(PG_FUNCTION_ARGS)
Datum bitsubstr(PG_FUNCTION_ARGS)
Datum varbittypmodout(PG_FUNCTION_ARGS)
Datum bitshiftright(PG_FUNCTION_ARGS)
Datum bitnot(PG_FUNCTION_ARGS)
Datum bitshiftleft(PG_FUNCTION_ARGS)
Datum bitlength(PG_FUNCTION_ARGS)
Datum varbit_in(PG_FUNCTION_ARGS)
static int32 bit_cmp(VarBit *arg1, VarBit *arg2)
#define PG_RETURN_VARBIT_P(x)
#define PG_GETARG_VARBIT_P(n)
static Datum VarBitPGetDatum(const VarBit *X)
#define VARBITTOTALLEN(BITLEN)