1 /*-------------------------------------------------------------------------
4 * Functions for the SQL datatypes BIT() and BIT VARYING().
6 * Code originally contributed by Adriaan Joubert.
8 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
12 * src/backend/utils/adt/varbit.c
14 *-------------------------------------------------------------------------
19 #include "access/htup_details.h"
20 #include "libpq/pqformat.h"
21 #include "nodes/nodeFuncs.h"
22 #include "utils/array.h"
23 #include "utils/builtins.h"
24 #include "utils/varbit.h"
26 #define HEXDIG(z) ((z)<10 ? ((z)+'0') : ((z)-10+'A'))
28 static VarBit *bit_catenate(VarBit *arg1, VarBit *arg2);
29 static VarBit *bitsubstring(VarBit *arg, int32 s, int32 l,
30 bool length_not_specified);
31 static VarBit *bit_overlay(VarBit *t1, VarBit *t2, int sp, int sl);
35 * common code for bittypmodin and varbittypmodin
38 anybit_typmodin(ArrayType *ta, const char *typename)
44 tl = ArrayGetIntegerTypmods(ta, &n);
47 * we're not too tense about good error message here because grammar
48 * shouldn't allow wrong number of modifiers for BIT
52 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
53 errmsg("invalid type modifier")));
57 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
58 errmsg("length for type %s must be at least 1",
60 if (*tl > (MaxAttrSize * BITS_PER_BYTE))
62 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
63 errmsg("length for type %s cannot exceed %d",
64 typename, MaxAttrSize * BITS_PER_BYTE)));
72 * common code for bittypmodout and varbittypmodout
75 anybit_typmodout(int32 typmod)
77 char *res = (char *) palloc(64);
80 snprintf(res, 64, "(%d)", typmod);
89 * attypmod -- contains the length of the bit string in bits, or for
90 * varying bits the maximum length.
92 * The data structure contains the following elements:
93 * header -- length of the whole data structure (incl header)
94 * in bytes. (as with all varying length datatypes)
95 * data section -- private data section for the bits data structures
96 * bitlength -- length of the bit string in bits
97 * bitdata -- bit string, most significant byte first
99 * The length of the bitdata vector should always be exactly as many
100 * bytes as are needed for the given bitlength. If the bitlength is
101 * not a multiple of 8, the extra low-order padding bits of the last
102 * byte must be zeroes.
108 * converts a char string to the internal representation of a bitstring.
109 * The length is determined by the number of bits required plus
110 * VARHDRSZ bytes or from atttypmod.
113 bit_in(PG_FUNCTION_ARGS)
115 char *input_string = PG_GETARG_CSTRING(0);
118 Oid typelem = PG_GETARG_OID(1);
120 int32 atttypmod = PG_GETARG_INT32(2);
121 VarBit *result; /* The resulting bit string */
122 char *sp; /* pointer into the character string */
123 bits8 *r; /* pointer into the result */
124 int len, /* Length of the whole data structure */
125 bitlen, /* Number of bits in the bit string */
126 slen; /* Length of the input string */
127 bool bit_not_hex; /* false = hex string true = bit string */
131 /* Check that the first character is a b or an x */
132 if (input_string[0] == 'b' || input_string[0] == 'B')
135 sp = input_string + 1;
137 else if (input_string[0] == 'x' || input_string[0] == 'X')
140 sp = input_string + 1;
145 * Otherwise it's binary. This allows things like cast('1001' as bit)
146 * to work transparently.
153 * Determine bitlength from input string. MaxAllocSize ensures a regular
154 * input is small enough, but we must check hex input.
161 if (slen > VARBITMAXLEN / 4)
163 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
164 errmsg("bit string length exceeds the maximum allowed (%d)",
170 * Sometimes atttypmod is not supplied. If it is supplied we need to make
171 * sure that the bitstring fits.
175 else if (bitlen != atttypmod)
177 (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
178 errmsg("bit string length %d does not match type bit(%d)",
179 bitlen, atttypmod)));
181 len = VARBITTOTALLEN(atttypmod);
182 /* set to 0 so that *r is always initialised and string is zero-padded */
183 result = (VarBit *) palloc0(len);
184 SET_VARSIZE(result, len);
185 VARBITLEN(result) = atttypmod;
190 /* Parse the bit representation of the string */
191 /* We know it fits, as bitlen was compared to atttypmod */
199 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
200 errmsg("\"%c\" is not a valid binary digit",
213 /* Parse the hex representation of the string */
214 for (bc = 0; *sp; sp++)
216 if (*sp >= '0' && *sp <= '9')
217 x = (bits8) (*sp - '0');
218 else if (*sp >= 'A' && *sp <= 'F')
219 x = (bits8) (*sp - 'A') + 10;
220 else if (*sp >= 'a' && *sp <= 'f')
221 x = (bits8) (*sp - 'a') + 10;
224 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
225 errmsg("\"%c\" is not a valid hexadecimal digit",
241 PG_RETURN_VARBIT_P(result);
246 bit_out(PG_FUNCTION_ARGS)
249 /* same as varbit output */
250 return varbit_out(fcinfo);
254 * This is how one would print a hex string, in case someone wants to
255 * write a formatting function.
257 VarBit *s = PG_GETARG_VARBIT_P(0);
265 bitlen = VARBITLEN(s);
266 len = (bitlen + 3) / 4;
267 result = (char *) palloc(len + 2);
271 /* we cheat by knowing that we store full bytes zero padded */
272 for (i = 0; i < len; i += 2, sp++)
274 *r++ = HEXDIG((*sp) >> 4);
275 *r++ = HEXDIG((*sp) & 0xF);
279 * Go back one step if we printed a hex number that was not part of the
286 PG_RETURN_CSTRING(result);
291 * bit_recv - converts external binary format to bit
294 bit_recv(PG_FUNCTION_ARGS)
296 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
299 Oid typelem = PG_GETARG_OID(1);
301 int32 atttypmod = PG_GETARG_INT32(2);
308 bitlen = pq_getmsgint(buf, sizeof(int32));
309 if (bitlen < 0 || bitlen > VARBITMAXLEN)
311 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
312 errmsg("invalid length in external bit string")));
315 * Sometimes atttypmod is not supplied. If it is supplied we need to make
316 * sure that the bitstring fits.
318 if (atttypmod > 0 && bitlen != atttypmod)
320 (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
321 errmsg("bit string length %d does not match type bit(%d)",
322 bitlen, atttypmod)));
324 len = VARBITTOTALLEN(bitlen);
325 result = (VarBit *) palloc(len);
326 SET_VARSIZE(result, len);
327 VARBITLEN(result) = bitlen;
329 pq_copymsgbytes(buf, (char *) VARBITS(result), VARBITBYTES(result));
331 /* Make sure last byte is zero-padded if needed */
332 ipad = VARBITPAD(result);
335 mask = BITMASK << ipad;
336 *(VARBITS(result) + VARBITBYTES(result) - 1) &= mask;
339 PG_RETURN_VARBIT_P(result);
343 * bit_send - converts bit to binary format
346 bit_send(PG_FUNCTION_ARGS)
348 /* Exactly the same as varbit_send, so share code */
349 return varbit_send(fcinfo);
354 * Converts a bit() type to a specific internal length.
355 * len is the bitlength specified in the column definition.
357 * If doing implicit cast, raise error when source data is wrong length.
358 * If doing explicit cast, silently truncate or zero-pad to specified length.
361 bit(PG_FUNCTION_ARGS)
363 VarBit *arg = PG_GETARG_VARBIT_P(0);
364 int32 len = PG_GETARG_INT32(1);
365 bool isExplicit = PG_GETARG_BOOL(2);
371 /* No work if typmod is invalid or supplied data matches it already */
372 if (len <= 0 || len > VARBITMAXLEN || len == VARBITLEN(arg))
373 PG_RETURN_VARBIT_P(arg);
377 (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
378 errmsg("bit string length %d does not match type bit(%d)",
379 VARBITLEN(arg), len)));
381 rlen = VARBITTOTALLEN(len);
382 /* set to 0 so that string is zero-padded */
383 result = (VarBit *) palloc0(rlen);
384 SET_VARSIZE(result, rlen);
385 VARBITLEN(result) = len;
387 memcpy(VARBITS(result), VARBITS(arg),
388 Min(VARBITBYTES(result), VARBITBYTES(arg)));
391 * Make sure last byte is zero-padded if needed. This is useless but safe
392 * if source data was shorter than target length (we assume the last byte
393 * of the source data was itself correctly zero-padded).
395 ipad = VARBITPAD(result);
398 mask = BITMASK << ipad;
399 *(VARBITS(result) + VARBITBYTES(result) - 1) &= mask;
402 PG_RETURN_VARBIT_P(result);
406 bittypmodin(PG_FUNCTION_ARGS)
408 ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0);
410 PG_RETURN_INT32(anybit_typmodin(ta, "bit"));
414 bittypmodout(PG_FUNCTION_ARGS)
416 int32 typmod = PG_GETARG_INT32(0);
418 PG_RETURN_CSTRING(anybit_typmodout(typmod));
424 * converts a string to the internal representation of a bitstring.
425 * This is the same as bit_in except that atttypmod is taken as
426 * the maximum length, not the exact length to force the bitstring to.
429 varbit_in(PG_FUNCTION_ARGS)
431 char *input_string = PG_GETARG_CSTRING(0);
434 Oid typelem = PG_GETARG_OID(1);
436 int32 atttypmod = PG_GETARG_INT32(2);
437 VarBit *result; /* The resulting bit string */
438 char *sp; /* pointer into the character string */
439 bits8 *r; /* pointer into the result */
440 int len, /* Length of the whole data structure */
441 bitlen, /* Number of bits in the bit string */
442 slen; /* Length of the input string */
443 bool bit_not_hex; /* false = hex string true = bit string */
447 /* Check that the first character is a b or an x */
448 if (input_string[0] == 'b' || input_string[0] == 'B')
451 sp = input_string + 1;
453 else if (input_string[0] == 'x' || input_string[0] == 'X')
456 sp = input_string + 1;
465 * Determine bitlength from input string. MaxAllocSize ensures a regular
466 * input is small enough, but we must check hex input.
473 if (slen > VARBITMAXLEN / 4)
475 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
476 errmsg("bit string length exceeds the maximum allowed (%d)",
482 * Sometimes atttypmod is not supplied. If it is supplied we need to make
483 * sure that the bitstring fits.
487 else if (bitlen > atttypmod)
489 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
490 errmsg("bit string too long for type bit varying(%d)",
493 len = VARBITTOTALLEN(bitlen);
494 /* set to 0 so that *r is always initialised and string is zero-padded */
495 result = (VarBit *) palloc0(len);
496 SET_VARSIZE(result, len);
497 VARBITLEN(result) = Min(bitlen, atttypmod);
502 /* Parse the bit representation of the string */
503 /* We know it fits, as bitlen was compared to atttypmod */
511 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
512 errmsg("\"%c\" is not a valid binary digit",
525 /* Parse the hex representation of the string */
526 for (bc = 0; *sp; sp++)
528 if (*sp >= '0' && *sp <= '9')
529 x = (bits8) (*sp - '0');
530 else if (*sp >= 'A' && *sp <= 'F')
531 x = (bits8) (*sp - 'A') + 10;
532 else if (*sp >= 'a' && *sp <= 'f')
533 x = (bits8) (*sp - 'a') + 10;
536 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
537 errmsg("\"%c\" is not a valid hexadecimal digit",
553 PG_RETURN_VARBIT_P(result);
558 * Prints the string as bits to preserve length accurately
560 * XXX varbit_recv() and hex input to varbit_in() can load a value that this
561 * cannot emit. Consider using hex output for such values.
564 varbit_out(PG_FUNCTION_ARGS)
566 VarBit *s = PG_GETARG_VARBIT_P(0);
576 result = (char *) palloc(len + 1);
579 for (i = 0; i <= len - BITS_PER_BYTE; i += BITS_PER_BYTE, sp++)
581 /* print full bytes */
583 for (k = 0; k < BITS_PER_BYTE; k++)
585 *r++ = IS_HIGHBIT_SET(x) ? '1' : '0';
591 /* print the last partial byte */
593 for (k = i; k < len; k++)
595 *r++ = IS_HIGHBIT_SET(x) ? '1' : '0';
601 PG_RETURN_CSTRING(result);
605 * varbit_recv - converts external binary format to varbit
607 * External format is the bitlen as an int32, then the byte array.
610 varbit_recv(PG_FUNCTION_ARGS)
612 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
615 Oid typelem = PG_GETARG_OID(1);
617 int32 atttypmod = PG_GETARG_INT32(2);
624 bitlen = pq_getmsgint(buf, sizeof(int32));
625 if (bitlen < 0 || bitlen > VARBITMAXLEN)
627 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
628 errmsg("invalid length in external bit string")));
631 * Sometimes atttypmod is not supplied. If it is supplied we need to make
632 * sure that the bitstring fits.
634 if (atttypmod > 0 && bitlen > atttypmod)
636 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
637 errmsg("bit string too long for type bit varying(%d)",
640 len = VARBITTOTALLEN(bitlen);
641 result = (VarBit *) palloc(len);
642 SET_VARSIZE(result, len);
643 VARBITLEN(result) = bitlen;
645 pq_copymsgbytes(buf, (char *) VARBITS(result), VARBITBYTES(result));
647 /* Make sure last byte is zero-padded if needed */
648 ipad = VARBITPAD(result);
651 mask = BITMASK << ipad;
652 *(VARBITS(result) + VARBITBYTES(result) - 1) &= mask;
655 PG_RETURN_VARBIT_P(result);
659 * varbit_send - converts varbit to binary format
662 varbit_send(PG_FUNCTION_ARGS)
664 VarBit *s = PG_GETARG_VARBIT_P(0);
667 pq_begintypsend(&buf);
668 pq_sendint(&buf, VARBITLEN(s), sizeof(int32));
669 pq_sendbytes(&buf, (char *) VARBITS(s), VARBITBYTES(s));
670 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
675 * Flatten calls to varbit's length coercion function that set the new maximum
676 * length >= the previous maximum length. We can ignore the isExplicit
677 * argument, since that only affects truncation cases.
680 varbit_transform(PG_FUNCTION_ARGS)
682 FuncExpr *expr = castNode(FuncExpr, PG_GETARG_POINTER(0));
686 Assert(list_length(expr->args) >= 2);
688 typmod = (Node *) lsecond(expr->args);
690 if (IsA(typmod, Const) &&!((Const *) typmod)->constisnull)
692 Node *source = (Node *) linitial(expr->args);
693 int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
694 int32 old_max = exprTypmod(source);
695 int32 new_max = new_typmod;
697 /* Note: varbit() treats typmod 0 as invalid, so we do too */
698 if (new_max <= 0 || (old_max > 0 && old_max <= new_max))
699 ret = relabel_to_typmod(source, new_typmod);
702 PG_RETURN_POINTER(ret);
707 * Converts a varbit() type to a specific internal length.
708 * len is the maximum bitlength specified in the column definition.
710 * If doing implicit cast, raise error when source data is too long.
711 * If doing explicit cast, silently truncate to max length.
714 varbit(PG_FUNCTION_ARGS)
716 VarBit *arg = PG_GETARG_VARBIT_P(0);
717 int32 len = PG_GETARG_INT32(1);
718 bool isExplicit = PG_GETARG_BOOL(2);
724 /* No work if typmod is invalid or supplied data matches it already */
725 if (len <= 0 || len >= VARBITLEN(arg))
726 PG_RETURN_VARBIT_P(arg);
730 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
731 errmsg("bit string too long for type bit varying(%d)",
734 rlen = VARBITTOTALLEN(len);
735 result = (VarBit *) palloc(rlen);
736 SET_VARSIZE(result, rlen);
737 VARBITLEN(result) = len;
739 memcpy(VARBITS(result), VARBITS(arg), VARBITBYTES(result));
741 /* Make sure last byte is zero-padded if needed */
742 ipad = VARBITPAD(result);
745 mask = BITMASK << ipad;
746 *(VARBITS(result) + VARBITBYTES(result) - 1) &= mask;
749 PG_RETURN_VARBIT_P(result);
753 varbittypmodin(PG_FUNCTION_ARGS)
755 ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0);
757 PG_RETURN_INT32(anybit_typmodin(ta, "varbit"));
761 varbittypmodout(PG_FUNCTION_ARGS)
763 int32 typmod = PG_GETARG_INT32(0);
765 PG_RETURN_CSTRING(anybit_typmodout(typmod));
770 * Comparison operators
772 * We only need one set of comparison operators for bitstrings, as the lengths
773 * are stored in the same way for zero-padded and varying bit strings.
775 * Note that the standard is not unambiguous about the comparison between
776 * zero-padded bit strings and varying bitstrings. If the same value is written
777 * into a zero padded bitstring as into a varying bitstring, but the zero
778 * padded bitstring has greater length, it will be bigger.
780 * Zeros from the beginning of a bitstring cannot simply be ignored, as they
781 * may be part of a bit string and may be significant.
783 * Note: btree indexes need these routines not to leak memory; therefore,
784 * be careful to free working copies of toasted datums. Most places don't
785 * need to be so careful.
791 * Compares two bitstrings and returns <0, 0, >0 depending on whether the first
792 * string is smaller, equal, or bigger than the second. All bits are considered
793 * and additional zero bits may make one string smaller/larger than the other,
794 * even if their zero-padded values would be the same.
797 bit_cmp(VarBit *arg1, VarBit *arg2)
805 bytelen1 = VARBITBYTES(arg1);
806 bytelen2 = VARBITBYTES(arg2);
808 cmp = memcmp(VARBITS(arg1), VARBITS(arg2), Min(bytelen1, bytelen2));
811 bitlen1 = VARBITLEN(arg1);
812 bitlen2 = VARBITLEN(arg2);
813 if (bitlen1 != bitlen2)
814 cmp = (bitlen1 < bitlen2) ? -1 : 1;
820 biteq(PG_FUNCTION_ARGS)
822 VarBit *arg1 = PG_GETARG_VARBIT_P(0);
823 VarBit *arg2 = PG_GETARG_VARBIT_P(1);
828 bitlen1 = VARBITLEN(arg1);
829 bitlen2 = VARBITLEN(arg2);
831 /* fast path for different-length inputs */
832 if (bitlen1 != bitlen2)
835 result = (bit_cmp(arg1, arg2) == 0);
837 PG_FREE_IF_COPY(arg1, 0);
838 PG_FREE_IF_COPY(arg2, 1);
840 PG_RETURN_BOOL(result);
844 bitne(PG_FUNCTION_ARGS)
846 VarBit *arg1 = PG_GETARG_VARBIT_P(0);
847 VarBit *arg2 = PG_GETARG_VARBIT_P(1);
852 bitlen1 = VARBITLEN(arg1);
853 bitlen2 = VARBITLEN(arg2);
855 /* fast path for different-length inputs */
856 if (bitlen1 != bitlen2)
859 result = (bit_cmp(arg1, arg2) != 0);
861 PG_FREE_IF_COPY(arg1, 0);
862 PG_FREE_IF_COPY(arg2, 1);
864 PG_RETURN_BOOL(result);
868 bitlt(PG_FUNCTION_ARGS)
870 VarBit *arg1 = PG_GETARG_VARBIT_P(0);
871 VarBit *arg2 = PG_GETARG_VARBIT_P(1);
874 result = (bit_cmp(arg1, arg2) < 0);
876 PG_FREE_IF_COPY(arg1, 0);
877 PG_FREE_IF_COPY(arg2, 1);
879 PG_RETURN_BOOL(result);
883 bitle(PG_FUNCTION_ARGS)
885 VarBit *arg1 = PG_GETARG_VARBIT_P(0);
886 VarBit *arg2 = PG_GETARG_VARBIT_P(1);
889 result = (bit_cmp(arg1, arg2) <= 0);
891 PG_FREE_IF_COPY(arg1, 0);
892 PG_FREE_IF_COPY(arg2, 1);
894 PG_RETURN_BOOL(result);
898 bitgt(PG_FUNCTION_ARGS)
900 VarBit *arg1 = PG_GETARG_VARBIT_P(0);
901 VarBit *arg2 = PG_GETARG_VARBIT_P(1);
904 result = (bit_cmp(arg1, arg2) > 0);
906 PG_FREE_IF_COPY(arg1, 0);
907 PG_FREE_IF_COPY(arg2, 1);
909 PG_RETURN_BOOL(result);
913 bitge(PG_FUNCTION_ARGS)
915 VarBit *arg1 = PG_GETARG_VARBIT_P(0);
916 VarBit *arg2 = PG_GETARG_VARBIT_P(1);
919 result = (bit_cmp(arg1, arg2) >= 0);
921 PG_FREE_IF_COPY(arg1, 0);
922 PG_FREE_IF_COPY(arg2, 1);
924 PG_RETURN_BOOL(result);
928 bitcmp(PG_FUNCTION_ARGS)
930 VarBit *arg1 = PG_GETARG_VARBIT_P(0);
931 VarBit *arg2 = PG_GETARG_VARBIT_P(1);
934 result = bit_cmp(arg1, arg2);
936 PG_FREE_IF_COPY(arg1, 0);
937 PG_FREE_IF_COPY(arg2, 1);
939 PG_RETURN_INT32(result);
944 * Concatenation of bit strings
947 bitcat(PG_FUNCTION_ARGS)
949 VarBit *arg1 = PG_GETARG_VARBIT_P(0);
950 VarBit *arg2 = PG_GETARG_VARBIT_P(1);
952 PG_RETURN_VARBIT_P(bit_catenate(arg1, arg2));
956 bit_catenate(VarBit *arg1, VarBit *arg2)
967 bitlen1 = VARBITLEN(arg1);
968 bitlen2 = VARBITLEN(arg2);
970 if (bitlen1 > VARBITMAXLEN - bitlen2)
972 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
973 errmsg("bit string length exceeds the maximum allowed (%d)",
975 bytelen = VARBITTOTALLEN(bitlen1 + bitlen2);
977 result = (VarBit *) palloc(bytelen);
978 SET_VARSIZE(result, bytelen);
979 VARBITLEN(result) = bitlen1 + bitlen2;
981 /* Copy the first bitstring in */
982 memcpy(VARBITS(result), VARBITS(arg1), VARBITBYTES(arg1));
984 /* Copy the second bit string */
985 bit1pad = VARBITPAD(arg1);
988 memcpy(VARBITS(result) + VARBITBYTES(arg1), VARBITS(arg2),
991 else if (bitlen2 > 0)
993 /* We need to shift all the bits to fit */
994 bit2shift = BITS_PER_BYTE - bit1pad;
995 pr = VARBITS(result) + VARBITBYTES(arg1) - 1;
996 for (pa = VARBITS(arg2); pa < VARBITEND(arg2); pa++)
998 *pr |= ((*pa >> bit2shift) & BITMASK);
1000 if (pr < VARBITEND(result))
1001 *pr = (*pa << bit1pad) & BITMASK;
1010 * retrieve a substring from the bit string.
1011 * Note, s is 1-based.
1015 bitsubstr(PG_FUNCTION_ARGS)
1017 PG_RETURN_VARBIT_P(bitsubstring(PG_GETARG_VARBIT_P(0),
1024 bitsubstr_no_len(PG_FUNCTION_ARGS)
1026 PG_RETURN_VARBIT_P(bitsubstring(PG_GETARG_VARBIT_P(0),
1032 bitsubstring(VarBit *arg, int32 s, int32 l, bool length_not_specified)
1048 bitlen = VARBITLEN(arg);
1050 /* If we do not have an upper bound, use end of string */
1051 if (length_not_specified)
1060 * A negative value for L is the only way for the end position to be
1061 * before the start. SQL99 says to throw an error.
1065 (errcode(ERRCODE_SUBSTRING_ERROR),
1066 errmsg("negative substring length not allowed")));
1067 e1 = Min(e, bitlen + 1);
1069 if (s1 > bitlen || e1 <= s1)
1071 /* Need to return a zero-length bitstring */
1072 len = VARBITTOTALLEN(0);
1073 result = (VarBit *) palloc(len);
1074 SET_VARSIZE(result, len);
1075 VARBITLEN(result) = 0;
1080 * OK, we've got a true substring starting at position s1-1 and ending
1084 len = VARBITTOTALLEN(rbitlen);
1085 result = (VarBit *) palloc(len);
1086 SET_VARSIZE(result, len);
1087 VARBITLEN(result) = rbitlen;
1088 len -= VARHDRSZ + VARBITHDRSZ;
1089 /* Are we copying from a byte boundary? */
1090 if ((s1 - 1) % BITS_PER_BYTE == 0)
1092 /* Yep, we are copying bytes */
1093 memcpy(VARBITS(result), VARBITS(arg) + (s1 - 1) / BITS_PER_BYTE,
1098 /* Figure out how much we need to shift the sequence by */
1099 ishift = (s1 - 1) % BITS_PER_BYTE;
1100 r = VARBITS(result);
1101 ps = VARBITS(arg) + (s1 - 1) / BITS_PER_BYTE;
1102 for (i = 0; i < len; i++)
1104 *r = (*ps << ishift) & BITMASK;
1105 if ((++ps) < VARBITEND(arg))
1106 *r |= *ps >> (BITS_PER_BYTE - ishift);
1110 /* Do we need to pad at the end? */
1111 ipad = VARBITPAD(result);
1114 mask = BITMASK << ipad;
1115 *(VARBITS(result) + len - 1) &= mask;
1124 * Replace specified substring of first string with second
1126 * The SQL standard defines OVERLAY() in terms of substring and concatenation.
1127 * This code is a direct implementation of what the standard says.
1130 bitoverlay(PG_FUNCTION_ARGS)
1132 VarBit *t1 = PG_GETARG_VARBIT_P(0);
1133 VarBit *t2 = PG_GETARG_VARBIT_P(1);
1134 int sp = PG_GETARG_INT32(2); /* substring start position */
1135 int sl = PG_GETARG_INT32(3); /* substring length */
1137 PG_RETURN_VARBIT_P(bit_overlay(t1, t2, sp, sl));
1141 bitoverlay_no_len(PG_FUNCTION_ARGS)
1143 VarBit *t1 = PG_GETARG_VARBIT_P(0);
1144 VarBit *t2 = PG_GETARG_VARBIT_P(1);
1145 int sp = PG_GETARG_INT32(2); /* substring start position */
1148 sl = VARBITLEN(t2); /* defaults to length(t2) */
1149 PG_RETURN_VARBIT_P(bit_overlay(t1, t2, sp, sl));
1153 bit_overlay(VarBit *t1, VarBit *t2, int sp, int sl)
1161 * Check for possible integer-overflow cases. For negative sp, throw a
1162 * "substring length" error because that's what should be expected
1163 * according to the spec's definition of OVERLAY().
1167 (errcode(ERRCODE_SUBSTRING_ERROR),
1168 errmsg("negative substring length not allowed")));
1172 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1173 errmsg("integer out of range")));
1175 s1 = bitsubstring(t1, 1, sp - 1, false);
1176 s2 = bitsubstring(t1, sp_pl_sl, -1, true);
1177 result = bit_catenate(s1, t2);
1178 result = bit_catenate(result, s2);
1184 * bitlength, bitoctetlength
1185 * Return the length of a bit string
1188 bitlength(PG_FUNCTION_ARGS)
1190 VarBit *arg = PG_GETARG_VARBIT_P(0);
1192 PG_RETURN_INT32(VARBITLEN(arg));
1196 bitoctetlength(PG_FUNCTION_ARGS)
1198 VarBit *arg = PG_GETARG_VARBIT_P(0);
1200 PG_RETURN_INT32(VARBITBYTES(arg));
1205 * perform a logical AND on two bit strings.
1208 bit_and(PG_FUNCTION_ARGS)
1210 VarBit *arg1 = PG_GETARG_VARBIT_P(0);
1211 VarBit *arg2 = PG_GETARG_VARBIT_P(1);
1221 bitlen1 = VARBITLEN(arg1);
1222 bitlen2 = VARBITLEN(arg2);
1223 if (bitlen1 != bitlen2)
1225 (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
1226 errmsg("cannot AND bit strings of different sizes")));
1228 len = VARSIZE(arg1);
1229 result = (VarBit *) palloc(len);
1230 SET_VARSIZE(result, len);
1231 VARBITLEN(result) = bitlen1;
1235 r = VARBITS(result);
1236 for (i = 0; i < VARBITBYTES(arg1); i++)
1237 *r++ = *p1++ & *p2++;
1239 /* Padding is not needed as & of 0 pad is 0 */
1241 PG_RETURN_VARBIT_P(result);
1246 * perform a logical OR on two bit strings.
1249 bit_or(PG_FUNCTION_ARGS)
1251 VarBit *arg1 = PG_GETARG_VARBIT_P(0);
1252 VarBit *arg2 = PG_GETARG_VARBIT_P(1);
1263 bitlen1 = VARBITLEN(arg1);
1264 bitlen2 = VARBITLEN(arg2);
1265 if (bitlen1 != bitlen2)
1267 (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
1268 errmsg("cannot OR bit strings of different sizes")));
1269 len = VARSIZE(arg1);
1270 result = (VarBit *) palloc(len);
1271 SET_VARSIZE(result, len);
1272 VARBITLEN(result) = bitlen1;
1276 r = VARBITS(result);
1277 for (i = 0; i < VARBITBYTES(arg1); i++)
1278 *r++ = *p1++ | *p2++;
1280 /* Pad the result */
1281 mask = BITMASK << VARBITPAD(result);
1288 PG_RETURN_VARBIT_P(result);
1293 * perform a logical XOR on two bit strings.
1296 bitxor(PG_FUNCTION_ARGS)
1298 VarBit *arg1 = PG_GETARG_VARBIT_P(0);
1299 VarBit *arg2 = PG_GETARG_VARBIT_P(1);
1310 bitlen1 = VARBITLEN(arg1);
1311 bitlen2 = VARBITLEN(arg2);
1312 if (bitlen1 != bitlen2)
1314 (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
1315 errmsg("cannot XOR bit strings of different sizes")));
1317 len = VARSIZE(arg1);
1318 result = (VarBit *) palloc(len);
1319 SET_VARSIZE(result, len);
1320 VARBITLEN(result) = bitlen1;
1324 r = VARBITS(result);
1325 for (i = 0; i < VARBITBYTES(arg1); i++)
1326 *r++ = *p1++ ^ *p2++;
1328 /* Pad the result */
1329 mask = BITMASK << VARBITPAD(result);
1336 PG_RETURN_VARBIT_P(result);
1341 * perform a logical NOT on a bit string.
1344 bitnot(PG_FUNCTION_ARGS)
1346 VarBit *arg = PG_GETARG_VARBIT_P(0);
1352 result = (VarBit *) palloc(VARSIZE(arg));
1353 SET_VARSIZE(result, VARSIZE(arg));
1354 VARBITLEN(result) = VARBITLEN(arg);
1357 r = VARBITS(result);
1358 for (; p < VARBITEND(arg); p++)
1361 /* Pad the result */
1362 mask = BITMASK << VARBITPAD(result);
1369 PG_RETURN_VARBIT_P(result);
1374 * do a left shift (i.e. towards the beginning of the string)
1377 bitshiftleft(PG_FUNCTION_ARGS)
1379 VarBit *arg = PG_GETARG_VARBIT_P(0);
1380 int32 shft = PG_GETARG_INT32(1);
1388 /* Negative shift is a shift to the right */
1391 /* Prevent integer overflow in negation */
1392 if (shft < -VARBITMAXLEN)
1393 shft = -VARBITMAXLEN;
1394 PG_RETURN_DATUM(DirectFunctionCall2(bitshiftright,
1395 VarBitPGetDatum(arg),
1396 Int32GetDatum(-shft)));
1399 result = (VarBit *) palloc(VARSIZE(arg));
1400 SET_VARSIZE(result, VARSIZE(arg));
1401 VARBITLEN(result) = VARBITLEN(arg);
1402 r = VARBITS(result);
1404 /* If we shifted all the bits out, return an all-zero string */
1405 if (shft >= VARBITLEN(arg))
1407 MemSet(r, 0, VARBITBYTES(arg));
1408 PG_RETURN_VARBIT_P(result);
1411 byte_shift = shft / BITS_PER_BYTE;
1412 ishift = shft % BITS_PER_BYTE;
1413 p = VARBITS(arg) + byte_shift;
1417 /* Special case: we can do a memcpy */
1418 len = VARBITBYTES(arg) - byte_shift;
1420 MemSet(r + len, 0, byte_shift);
1424 for (; p < VARBITEND(arg); r++)
1427 if ((++p) < VARBITEND(arg))
1428 *r |= *p >> (BITS_PER_BYTE - ishift);
1430 for (; r < VARBITEND(result); r++)
1434 PG_RETURN_VARBIT_P(result);
1439 * do a right shift (i.e. towards the end of the string)
1442 bitshiftright(PG_FUNCTION_ARGS)
1444 VarBit *arg = PG_GETARG_VARBIT_P(0);
1445 int32 shft = PG_GETARG_INT32(1);
1453 /* Negative shift is a shift to the left */
1456 /* Prevent integer overflow in negation */
1457 if (shft < -VARBITMAXLEN)
1458 shft = -VARBITMAXLEN;
1459 PG_RETURN_DATUM(DirectFunctionCall2(bitshiftleft,
1460 VarBitPGetDatum(arg),
1461 Int32GetDatum(-shft)));
1464 result = (VarBit *) palloc(VARSIZE(arg));
1465 SET_VARSIZE(result, VARSIZE(arg));
1466 VARBITLEN(result) = VARBITLEN(arg);
1467 r = VARBITS(result);
1469 /* If we shifted all the bits out, return an all-zero string */
1470 if (shft >= VARBITLEN(arg))
1472 MemSet(r, 0, VARBITBYTES(arg));
1473 PG_RETURN_VARBIT_P(result);
1476 byte_shift = shft / BITS_PER_BYTE;
1477 ishift = shft % BITS_PER_BYTE;
1480 /* Set the first part of the result to 0 */
1481 MemSet(r, 0, byte_shift);
1486 /* Special case: we can do a memcpy */
1487 len = VARBITBYTES(arg) - byte_shift;
1492 if (r < VARBITEND(result))
1493 *r = 0; /* initialize first byte */
1494 for (; r < VARBITEND(result); p++)
1497 if ((++r) < VARBITEND(result))
1498 *r = (*p << (BITS_PER_BYTE - ishift)) & BITMASK;
1502 PG_RETURN_VARBIT_P(result);
1506 * This is not defined in any standard. We retain the natural ordering of
1507 * bits here, as it just seems more intuitive.
1510 bitfromint4(PG_FUNCTION_ARGS)
1512 int32 a = PG_GETARG_INT32(0);
1513 int32 typmod = PG_GETARG_INT32(1);
1520 if (typmod <= 0 || typmod > VARBITMAXLEN)
1521 typmod = 1; /* default bit length */
1523 rlen = VARBITTOTALLEN(typmod);
1524 result = (VarBit *) palloc(rlen);
1525 SET_VARSIZE(result, rlen);
1526 VARBITLEN(result) = typmod;
1528 r = VARBITS(result);
1529 destbitsleft = typmod;
1531 /* drop any input bits that don't fit */
1532 srcbitsleft = Min(srcbitsleft, destbitsleft);
1533 /* sign-fill any excess bytes in output */
1534 while (destbitsleft >= srcbitsleft + 8)
1536 *r++ = (bits8) ((a < 0) ? BITMASK : 0);
1539 /* store first fractional byte */
1540 if (destbitsleft > srcbitsleft)
1542 int val = (int) (a >> (destbitsleft - 8));
1544 /* Force sign-fill in case the compiler implements >> as zero-fill */
1546 val |= (-1) << (srcbitsleft + 8 - destbitsleft);
1547 *r++ = (bits8) (val & BITMASK);
1550 /* Now srcbitsleft and destbitsleft are the same, need not track both */
1551 /* store whole bytes */
1552 while (destbitsleft >= 8)
1554 *r++ = (bits8) ((a >> (destbitsleft - 8)) & BITMASK);
1557 /* store last fractional byte */
1558 if (destbitsleft > 0)
1559 *r = (bits8) ((a << (8 - destbitsleft)) & BITMASK);
1561 PG_RETURN_VARBIT_P(result);
1565 bittoint4(PG_FUNCTION_ARGS)
1567 VarBit *arg = PG_GETARG_VARBIT_P(0);
1571 /* Check that the bit string is not too long */
1572 if (VARBITLEN(arg) > sizeof(result) * BITS_PER_BYTE)
1574 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1575 errmsg("integer out of range")));
1578 for (r = VARBITS(arg); r < VARBITEND(arg); r++)
1580 result <<= BITS_PER_BYTE;
1583 /* Now shift the result to take account of the padding at the end */
1584 result >>= VARBITPAD(arg);
1586 PG_RETURN_INT32(result);
1590 bitfromint8(PG_FUNCTION_ARGS)
1592 int64 a = PG_GETARG_INT64(0);
1593 int32 typmod = PG_GETARG_INT32(1);
1600 if (typmod <= 0 || typmod > VARBITMAXLEN)
1601 typmod = 1; /* default bit length */
1603 rlen = VARBITTOTALLEN(typmod);
1604 result = (VarBit *) palloc(rlen);
1605 SET_VARSIZE(result, rlen);
1606 VARBITLEN(result) = typmod;
1608 r = VARBITS(result);
1609 destbitsleft = typmod;
1611 /* drop any input bits that don't fit */
1612 srcbitsleft = Min(srcbitsleft, destbitsleft);
1613 /* sign-fill any excess bytes in output */
1614 while (destbitsleft >= srcbitsleft + 8)
1616 *r++ = (bits8) ((a < 0) ? BITMASK : 0);
1619 /* store first fractional byte */
1620 if (destbitsleft > srcbitsleft)
1622 int val = (int) (a >> (destbitsleft - 8));
1624 /* Force sign-fill in case the compiler implements >> as zero-fill */
1626 val |= (-1) << (srcbitsleft + 8 - destbitsleft);
1627 *r++ = (bits8) (val & BITMASK);
1630 /* Now srcbitsleft and destbitsleft are the same, need not track both */
1631 /* store whole bytes */
1632 while (destbitsleft >= 8)
1634 *r++ = (bits8) ((a >> (destbitsleft - 8)) & BITMASK);
1637 /* store last fractional byte */
1638 if (destbitsleft > 0)
1639 *r = (bits8) ((a << (8 - destbitsleft)) & BITMASK);
1641 PG_RETURN_VARBIT_P(result);
1645 bittoint8(PG_FUNCTION_ARGS)
1647 VarBit *arg = PG_GETARG_VARBIT_P(0);
1651 /* Check that the bit string is not too long */
1652 if (VARBITLEN(arg) > sizeof(result) * BITS_PER_BYTE)
1654 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1655 errmsg("bigint out of range")));
1658 for (r = VARBITS(arg); r < VARBITEND(arg); r++)
1660 result <<= BITS_PER_BYTE;
1663 /* Now shift the result to take account of the padding at the end */
1664 result >>= VARBITPAD(arg);
1666 PG_RETURN_INT64(result);
1671 * Determines the position of S2 in the bitstring S1 (1-based string).
1672 * If S2 does not appear in S1 this function returns 0.
1673 * If S2 is of length 0 this function returns 1.
1674 * Compatible in usage with POSITION() functions for other data types.
1677 bitposition(PG_FUNCTION_ARGS)
1679 VarBit *str = PG_GETARG_VARBIT_P(0);
1680 VarBit *substr = PG_GETARG_VARBIT_P(1);
1685 bits8 *s, /* pointer into substring */
1686 *p; /* pointer into str */
1687 bits8 cmp, /* shifted substring byte to compare */
1688 mask1, /* mask for substring byte shifted right */
1689 mask2, /* mask for substring byte shifted left */
1690 end_mask, /* pad mask for last substring byte */
1691 str_mask; /* pad mask for last string byte */
1694 /* Get the substring length */
1695 substr_length = VARBITLEN(substr);
1696 str_length = VARBITLEN(str);
1698 /* String has zero length or substring longer than string, return 0 */
1699 if ((str_length == 0) || (substr_length > str_length))
1702 /* zero-length substring means return 1 */
1703 if (substr_length == 0)
1706 /* Initialise the padding masks */
1707 end_mask = BITMASK << VARBITPAD(substr);
1708 str_mask = BITMASK << VARBITPAD(str);
1709 for (i = 0; i < VARBITBYTES(str) - VARBITBYTES(substr) + 1; i++)
1711 for (is = 0; is < BITS_PER_BYTE; is++)
1714 p = VARBITS(str) + i;
1715 mask1 = BITMASK >> is;
1717 for (s = VARBITS(substr);
1718 is_match && s < VARBITEND(substr); s++)
1721 if (s == VARBITEND(substr) - 1)
1723 mask1 &= end_mask >> is;
1724 if (p == VARBITEND(str) - 1)
1726 /* Check that there is enough of str left */
1727 if (mask1 & ~str_mask)
1735 is_match = ((cmp ^ *p) & mask1) == 0;
1738 /* Move on to the next byte */
1740 if (p == VARBITEND(str))
1742 mask2 = end_mask << (BITS_PER_BYTE - is);
1743 is_match = mask2 == 0;
1745 elog(DEBUG4, "S. %d %d em=%2x sm=%2x r=%d",
1746 i, is, end_mask, mask2, is_match);
1750 cmp = *s << (BITS_PER_BYTE - is);
1751 if (s == VARBITEND(substr) - 1)
1753 mask2 &= end_mask << (BITS_PER_BYTE - is);
1754 if (p == VARBITEND(str) - 1)
1756 if (mask2 & ~str_mask)
1764 is_match = ((cmp ^ *p) & mask2) == 0;
1766 /* Have we found a match? */
1768 PG_RETURN_INT32(i * BITS_PER_BYTE + is + 1);
1778 * Given an instance of type 'bit' creates a new one with
1779 * the Nth bit set to the given value.
1781 * The bit location is specified left-to-right in a zero-based fashion
1782 * consistent with the other get_bit and set_bit functions, but
1783 * inconsistent with the standard substring, position, overlay functions
1786 bitsetbit(PG_FUNCTION_ARGS)
1788 VarBit *arg1 = PG_GETARG_VARBIT_P(0);
1789 int32 n = PG_GETARG_INT32(1);
1790 int32 newBit = PG_GETARG_INT32(2);
1799 bitlen = VARBITLEN(arg1);
1800 if (n < 0 || n >= bitlen)
1802 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1803 errmsg("bit index %d out of valid range (0..%d)",
1809 if (newBit != 0 && newBit != 1)
1811 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1812 errmsg("new bit must be 0 or 1")));
1814 len = VARSIZE(arg1);
1815 result = (VarBit *) palloc(len);
1816 SET_VARSIZE(result, len);
1817 VARBITLEN(result) = bitlen;
1820 r = VARBITS(result);
1822 memcpy(r, p, VARBITBYTES(arg1));
1824 byteNo = n / BITS_PER_BYTE;
1825 bitNo = BITS_PER_BYTE - 1 - (n % BITS_PER_BYTE);
1831 r[byteNo] &= (~(1 << bitNo));
1833 r[byteNo] |= (1 << bitNo);
1835 PG_RETURN_VARBIT_P(result);
1841 * returns the value of the Nth bit of a bit array (0 or 1).
1843 * The bit location is specified left-to-right in a zero-based fashion
1844 * consistent with the other get_bit and set_bit functions, but
1845 * inconsistent with the standard substring, position, overlay functions
1848 bitgetbit(PG_FUNCTION_ARGS)
1850 VarBit *arg1 = PG_GETARG_VARBIT_P(0);
1851 int32 n = PG_GETARG_INT32(1);
1857 bitlen = VARBITLEN(arg1);
1858 if (n < 0 || n >= bitlen)
1860 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
1861 errmsg("bit index %d out of valid range (0..%d)",
1866 byteNo = n / BITS_PER_BYTE;
1867 bitNo = BITS_PER_BYTE - 1 - (n % BITS_PER_BYTE);
1869 if (p[byteNo] & (1 << bitNo))