1 /*-------------------------------------------------------------------------
4 * Functions for the SQL datatypes BIT() and BIT VARYING().
6 * The data structure contains the following elements:
7 * header -- length of the whole data structure (incl header)
8 * in bytes (as with all varying length datatypes)
9 * data section -- private data section for the bits data structures
10 * bitlength -- length of the bit string in bits
11 * bitdata -- bit string, most significant byte first
13 * The length of the bitdata vector should always be exactly as many
14 * bytes as are needed for the given bitlength. If the bitlength is
15 * not a multiple of 8, the extra low-order padding bits of the last
16 * byte must be zeroes.
18 * attypmod is defined as the length of the bit string in bits, or for
19 * varying bits the maximum length.
21 * Code originally contributed by Adriaan Joubert.
23 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
24 * Portions Copyright (c) 1994, Regents of the University of California
27 * src/backend/utils/adt/varbit.c
29 *-------------------------------------------------------------------------
34 #include "access/htup_details.h"
35 #include "common/int.h"
36 #include "libpq/pqformat.h"
37 #include "nodes/nodeFuncs.h"
38 #include "nodes/supportnodes.h"
39 #include "port/pg_bitutils.h"
40 #include "utils/array.h"
41 #include "utils/builtins.h"
42 #include "utils/varbit.h"
44 #define HEXDIG(z) ((z)<10 ? ((z)+'0') : ((z)-10+'A'))
46 /* Mask off any bits that should be zero in the last byte of a bitstring */
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_; \
56 * Many functions work byte-by-byte, so they have a pointer handy to the
57 * last-plus-one byte, which saves a cycle or two.
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_; \
67 /* Assert proper padding of a bitstring */
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)
80 static VarBit
*bit_catenate(VarBit
*arg1
, VarBit
*arg2
);
81 static VarBit
*bitsubstring(VarBit
*arg
, int32 s
, int32 l
,
82 bool length_not_specified
);
83 static VarBit
*bit_overlay(VarBit
*t1
, VarBit
*t2
, int sp
, int sl
);
87 * common code for bittypmodin and varbittypmodin
90 anybit_typmodin(ArrayType
*ta
, const char *typename
)
96 tl
= ArrayGetIntegerTypmods(ta
, &n
);
99 * we're not too tense about good error message here because grammar
100 * shouldn't allow wrong number of modifiers for BIT
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",
112 if (*tl
> (MaxAttrSize
* BITS_PER_BYTE
))
114 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
115 errmsg("length for type %s cannot exceed %d",
116 typename
, MaxAttrSize
* BITS_PER_BYTE
)));
124 * common code for bittypmodout and varbittypmodout
127 anybit_typmodout(int32 typmod
)
129 char *res
= (char *) palloc(64);
132 snprintf(res
, 64, "(%d)", typmod
);
142 * converts a char string to the internal representation of a bitstring.
143 * The length is determined by the number of bits required plus
144 * VARHDRSZ bytes or from atttypmod.
147 bit_in(PG_FUNCTION_ARGS
)
149 char *input_string
= PG_GETARG_CSTRING(0);
152 Oid typelem
= PG_GETARG_OID(1);
154 int32 atttypmod
= PG_GETARG_INT32(2);
155 VarBit
*result
; /* The resulting bit string */
156 char *sp
; /* pointer into the character string */
157 bits8
*r
; /* pointer into the result */
158 int len
, /* Length of the whole data structure */
159 bitlen
, /* Number of bits in the bit string */
160 slen
; /* Length of the input string */
161 bool bit_not_hex
; /* false = hex string true = bit string */
165 /* Check that the first character is a b or an x */
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;
179 * Otherwise it's binary. This allows things like cast('1001' as bit)
180 * to work transparently.
187 * Determine bitlength from input string. MaxAllocSize ensures a regular
188 * input is small enough, but we must check hex input.
195 if (slen
> VARBITMAXLEN
/ 4)
197 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED
),
198 errmsg("bit string length exceeds the maximum allowed (%d)",
204 * Sometimes atttypmod is not supplied. If it is supplied we need to make
205 * sure that the bitstring fits.
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
)));
215 len
= VARBITTOTALLEN(atttypmod
);
216 /* set to 0 so that *r is always initialised and string is zero-padded */
217 result
= (VarBit
*) palloc0(len
);
218 SET_VARSIZE(result
, len
);
219 VARBITLEN(result
) = atttypmod
;
224 /* Parse the bit representation of the string */
225 /* We know it fits, as bitlen was compared to atttypmod */
233 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION
),
234 errmsg("\"%.*s\" is not a valid binary digit",
247 /* Parse the hex representation of the string */
248 for (bc
= 0; *sp
; sp
++)
250 if (*sp
>= '0' && *sp
<= '9')
251 x
= (bits8
) (*sp
- '0');
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",
275 PG_RETURN_VARBIT_P(result
);
280 bit_out(PG_FUNCTION_ARGS
)
283 /* same as varbit output */
284 return varbit_out(fcinfo
);
288 * This is how one would print a hex string, in case someone wants to
289 * write a formatting function.
291 VarBit
*s
= PG_GETARG_VARBIT_P(0);
299 /* Assertion to help catch any bit functions that don't pad correctly */
300 VARBIT_CORRECTLY_PADDED(s
);
302 bitlen
= VARBITLEN(s
);
303 len
= (bitlen
+ 3) / 4;
304 result
= (char *) palloc(len
+ 2);
308 /* we cheat by knowing that we store full bytes zero padded */
309 for (i
= 0; i
< len
; i
+= 2, sp
++)
311 *r
++ = HEXDIG((*sp
) >> 4);
312 *r
++ = HEXDIG((*sp
) & 0xF);
316 * Go back one step if we printed a hex number that was not part of the
323 PG_RETURN_CSTRING(result
);
328 * bit_recv - converts external binary format to bit
331 bit_recv(PG_FUNCTION_ARGS
)
333 StringInfo buf
= (StringInfo
) PG_GETARG_POINTER(0);
336 Oid typelem
= PG_GETARG_OID(1);
338 int32 atttypmod
= PG_GETARG_INT32(2);
343 bitlen
= pq_getmsgint(buf
, sizeof(int32
));
344 if (bitlen
< 0 || bitlen
> VARBITMAXLEN
)
346 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION
),
347 errmsg("invalid length in external bit string")));
350 * Sometimes atttypmod is not supplied. If it is supplied we need to make
351 * sure that the bitstring fits.
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
)));
359 len
= VARBITTOTALLEN(bitlen
);
360 result
= (VarBit
*) palloc(len
);
361 SET_VARSIZE(result
, len
);
362 VARBITLEN(result
) = bitlen
;
364 pq_copymsgbytes(buf
, (char *) VARBITS(result
), VARBITBYTES(result
));
366 /* Make sure last byte is correctly zero-padded */
369 PG_RETURN_VARBIT_P(result
);
373 * bit_send - converts bit to binary format
376 bit_send(PG_FUNCTION_ARGS
)
378 /* Exactly the same as varbit_send, so share code */
379 return varbit_send(fcinfo
);
384 * Converts a bit() type to a specific internal length.
385 * len is the bitlength specified in the column definition.
387 * If doing implicit cast, raise error when source data is wrong length.
388 * If doing explicit cast, silently truncate or zero-pad to specified length.
391 bit(PG_FUNCTION_ARGS
)
393 VarBit
*arg
= PG_GETARG_VARBIT_P(0);
394 int32 len
= PG_GETARG_INT32(1);
395 bool isExplicit
= PG_GETARG_BOOL(2);
399 /* No work if typmod is invalid or supplied data matches it already */
400 if (len
<= 0 || len
> VARBITMAXLEN
|| len
== VARBITLEN(arg
))
401 PG_RETURN_VARBIT_P(arg
);
405 (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH
),
406 errmsg("bit string length %d does not match type bit(%d)",
407 VARBITLEN(arg
), len
)));
409 rlen
= VARBITTOTALLEN(len
);
410 /* set to 0 so that string is zero-padded */
411 result
= (VarBit
*) palloc0(rlen
);
412 SET_VARSIZE(result
, rlen
);
413 VARBITLEN(result
) = len
;
415 memcpy(VARBITS(result
), VARBITS(arg
),
416 Min(VARBITBYTES(result
), VARBITBYTES(arg
)));
419 * Make sure last byte is zero-padded if needed. This is useless but safe
420 * if source data was shorter than target length (we assume the last byte
421 * of the source data was itself correctly zero-padded).
425 PG_RETURN_VARBIT_P(result
);
429 bittypmodin(PG_FUNCTION_ARGS
)
431 ArrayType
*ta
= PG_GETARG_ARRAYTYPE_P(0);
433 PG_RETURN_INT32(anybit_typmodin(ta
, "bit"));
437 bittypmodout(PG_FUNCTION_ARGS
)
439 int32 typmod
= PG_GETARG_INT32(0);
441 PG_RETURN_CSTRING(anybit_typmodout(typmod
));
447 * converts a string to the internal representation of a bitstring.
448 * This is the same as bit_in except that atttypmod is taken as
449 * the maximum length, not the exact length to force the bitstring to.
452 varbit_in(PG_FUNCTION_ARGS
)
454 char *input_string
= PG_GETARG_CSTRING(0);
457 Oid typelem
= PG_GETARG_OID(1);
459 int32 atttypmod
= PG_GETARG_INT32(2);
460 VarBit
*result
; /* The resulting bit string */
461 char *sp
; /* pointer into the character string */
462 bits8
*r
; /* pointer into the result */
463 int len
, /* Length of the whole data structure */
464 bitlen
, /* Number of bits in the bit string */
465 slen
; /* Length of the input string */
466 bool bit_not_hex
; /* false = hex string true = bit string */
470 /* Check that the first character is a b or an x */
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;
488 * Determine bitlength from input string. MaxAllocSize ensures a regular
489 * input is small enough, but we must check hex input.
496 if (slen
> VARBITMAXLEN
/ 4)
498 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED
),
499 errmsg("bit string length exceeds the maximum allowed (%d)",
505 * Sometimes atttypmod is not supplied. If it is supplied we need to make
506 * sure that the bitstring fits.
510 else if (bitlen
> atttypmod
)
512 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION
),
513 errmsg("bit string too long for type bit varying(%d)",
516 len
= VARBITTOTALLEN(bitlen
);
517 /* set to 0 so that *r is always initialised and string is zero-padded */
518 result
= (VarBit
*) palloc0(len
);
519 SET_VARSIZE(result
, len
);
520 VARBITLEN(result
) = Min(bitlen
, atttypmod
);
525 /* Parse the bit representation of the string */
526 /* We know it fits, as bitlen was compared to atttypmod */
534 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION
),
535 errmsg("\"%.*s\" is not a valid binary digit",
548 /* Parse the hex representation of the string */
549 for (bc
= 0; *sp
; sp
++)
551 if (*sp
>= '0' && *sp
<= '9')
552 x
= (bits8
) (*sp
- '0');
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",
576 PG_RETURN_VARBIT_P(result
);
581 * Prints the string as bits to preserve length accurately
583 * XXX varbit_recv() and hex input to varbit_in() can load a value that this
584 * cannot emit. Consider using hex output for such values.
587 varbit_out(PG_FUNCTION_ARGS
)
589 VarBit
*s
= PG_GETARG_VARBIT_P(0);
598 /* Assertion to help catch any bit functions that don't pad correctly */
599 VARBIT_CORRECTLY_PADDED(s
);
602 result
= (char *) palloc(len
+ 1);
605 for (i
= 0; i
<= len
- BITS_PER_BYTE
; i
+= BITS_PER_BYTE
, sp
++)
607 /* print full bytes */
609 for (k
= 0; k
< BITS_PER_BYTE
; k
++)
611 *r
++ = IS_HIGHBIT_SET(x
) ? '1' : '0';
617 /* print the last partial byte */
619 for (k
= i
; k
< len
; k
++)
621 *r
++ = IS_HIGHBIT_SET(x
) ? '1' : '0';
627 PG_RETURN_CSTRING(result
);
631 * varbit_recv - converts external binary format to varbit
633 * External format is the bitlen as an int32, then the byte array.
636 varbit_recv(PG_FUNCTION_ARGS
)
638 StringInfo buf
= (StringInfo
) PG_GETARG_POINTER(0);
641 Oid typelem
= PG_GETARG_OID(1);
643 int32 atttypmod
= PG_GETARG_INT32(2);
648 bitlen
= pq_getmsgint(buf
, sizeof(int32
));
649 if (bitlen
< 0 || bitlen
> VARBITMAXLEN
)
651 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION
),
652 errmsg("invalid length in external bit string")));
655 * Sometimes atttypmod is not supplied. If it is supplied we need to make
656 * sure that the bitstring fits.
658 if (atttypmod
> 0 && bitlen
> atttypmod
)
660 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION
),
661 errmsg("bit string too long for type bit varying(%d)",
664 len
= VARBITTOTALLEN(bitlen
);
665 result
= (VarBit
*) palloc(len
);
666 SET_VARSIZE(result
, len
);
667 VARBITLEN(result
) = bitlen
;
669 pq_copymsgbytes(buf
, (char *) VARBITS(result
), VARBITBYTES(result
));
671 /* Make sure last byte is correctly zero-padded */
674 PG_RETURN_VARBIT_P(result
);
678 * varbit_send - converts varbit to binary format
681 varbit_send(PG_FUNCTION_ARGS
)
683 VarBit
*s
= PG_GETARG_VARBIT_P(0);
686 pq_begintypsend(&buf
);
687 pq_sendint32(&buf
, VARBITLEN(s
));
688 pq_sendbytes(&buf
, (char *) VARBITS(s
), VARBITBYTES(s
));
689 PG_RETURN_BYTEA_P(pq_endtypsend(&buf
));
695 * Planner support function for the varbit() length coercion function.
697 * Currently, the only interesting thing we can do is flatten calls that set
698 * the new maximum length >= the previous maximum length. We can ignore the
699 * isExplicit argument, since that only affects truncation cases.
702 varbit_support(PG_FUNCTION_ARGS
)
704 Node
*rawreq
= (Node
*) PG_GETARG_POINTER(0);
707 if (IsA(rawreq
, SupportRequestSimplify
))
709 SupportRequestSimplify
*req
= (SupportRequestSimplify
*) rawreq
;
710 FuncExpr
*expr
= req
->fcall
;
713 Assert(list_length(expr
->args
) >= 2);
715 typmod
= (Node
*) lsecond(expr
->args
);
717 if (IsA(typmod
, Const
) && !((Const
*) typmod
)->constisnull
)
719 Node
*source
= (Node
*) linitial(expr
->args
);
720 int32 new_typmod
= DatumGetInt32(((Const
*) typmod
)->constvalue
);
721 int32 old_max
= exprTypmod(source
);
722 int32 new_max
= new_typmod
;
724 /* Note: varbit() treats typmod 0 as invalid, so we do too */
725 if (new_max
<= 0 || (old_max
> 0 && old_max
<= new_max
))
726 ret
= relabel_to_typmod(source
, new_typmod
);
730 PG_RETURN_POINTER(ret
);
735 * Converts a varbit() type to a specific internal length.
736 * len is the maximum bitlength specified in the column definition.
738 * If doing implicit cast, raise error when source data is too long.
739 * If doing explicit cast, silently truncate to max length.
742 varbit(PG_FUNCTION_ARGS
)
744 VarBit
*arg
= PG_GETARG_VARBIT_P(0);
745 int32 len
= PG_GETARG_INT32(1);
746 bool isExplicit
= PG_GETARG_BOOL(2);
750 /* No work if typmod is invalid or supplied data matches it already */
751 if (len
<= 0 || len
>= VARBITLEN(arg
))
752 PG_RETURN_VARBIT_P(arg
);
756 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION
),
757 errmsg("bit string too long for type bit varying(%d)",
760 rlen
= VARBITTOTALLEN(len
);
761 result
= (VarBit
*) palloc(rlen
);
762 SET_VARSIZE(result
, rlen
);
763 VARBITLEN(result
) = len
;
765 memcpy(VARBITS(result
), VARBITS(arg
), VARBITBYTES(result
));
767 /* Make sure last byte is correctly zero-padded */
770 PG_RETURN_VARBIT_P(result
);
774 varbittypmodin(PG_FUNCTION_ARGS
)
776 ArrayType
*ta
= PG_GETARG_ARRAYTYPE_P(0);
778 PG_RETURN_INT32(anybit_typmodin(ta
, "varbit"));
782 varbittypmodout(PG_FUNCTION_ARGS
)
784 int32 typmod
= PG_GETARG_INT32(0);
786 PG_RETURN_CSTRING(anybit_typmodout(typmod
));
791 * Comparison operators
793 * We only need one set of comparison operators for bitstrings, as the lengths
794 * are stored in the same way for zero-padded and varying bit strings.
796 * Note that the standard is not unambiguous about the comparison between
797 * zero-padded bit strings and varying bitstrings. If the same value is written
798 * into a zero padded bitstring as into a varying bitstring, but the zero
799 * padded bitstring has greater length, it will be bigger.
801 * Zeros from the beginning of a bitstring cannot simply be ignored, as they
802 * may be part of a bit string and may be significant.
804 * Note: btree indexes need these routines not to leak memory; therefore,
805 * be careful to free working copies of toasted datums. Most places don't
806 * need to be so careful.
812 * Compares two bitstrings and returns <0, 0, >0 depending on whether the first
813 * string is smaller, equal, or bigger than the second. All bits are considered
814 * and additional zero bits may make one string smaller/larger than the other,
815 * even if their zero-padded values would be the same.
818 bit_cmp(VarBit
*arg1
, VarBit
*arg2
)
826 bytelen1
= VARBITBYTES(arg1
);
827 bytelen2
= VARBITBYTES(arg2
);
829 cmp
= memcmp(VARBITS(arg1
), VARBITS(arg2
), Min(bytelen1
, bytelen2
));
832 bitlen1
= VARBITLEN(arg1
);
833 bitlen2
= VARBITLEN(arg2
);
834 if (bitlen1
!= bitlen2
)
835 cmp
= (bitlen1
< bitlen2
) ? -1 : 1;
841 biteq(PG_FUNCTION_ARGS
)
843 VarBit
*arg1
= PG_GETARG_VARBIT_P(0);
844 VarBit
*arg2
= PG_GETARG_VARBIT_P(1);
849 bitlen1
= VARBITLEN(arg1
);
850 bitlen2
= VARBITLEN(arg2
);
852 /* fast path for different-length inputs */
853 if (bitlen1
!= bitlen2
)
856 result
= (bit_cmp(arg1
, arg2
) == 0);
858 PG_FREE_IF_COPY(arg1
, 0);
859 PG_FREE_IF_COPY(arg2
, 1);
861 PG_RETURN_BOOL(result
);
865 bitne(PG_FUNCTION_ARGS
)
867 VarBit
*arg1
= PG_GETARG_VARBIT_P(0);
868 VarBit
*arg2
= PG_GETARG_VARBIT_P(1);
873 bitlen1
= VARBITLEN(arg1
);
874 bitlen2
= VARBITLEN(arg2
);
876 /* fast path for different-length inputs */
877 if (bitlen1
!= bitlen2
)
880 result
= (bit_cmp(arg1
, arg2
) != 0);
882 PG_FREE_IF_COPY(arg1
, 0);
883 PG_FREE_IF_COPY(arg2
, 1);
885 PG_RETURN_BOOL(result
);
889 bitlt(PG_FUNCTION_ARGS
)
891 VarBit
*arg1
= PG_GETARG_VARBIT_P(0);
892 VarBit
*arg2
= PG_GETARG_VARBIT_P(1);
895 result
= (bit_cmp(arg1
, arg2
) < 0);
897 PG_FREE_IF_COPY(arg1
, 0);
898 PG_FREE_IF_COPY(arg2
, 1);
900 PG_RETURN_BOOL(result
);
904 bitle(PG_FUNCTION_ARGS
)
906 VarBit
*arg1
= PG_GETARG_VARBIT_P(0);
907 VarBit
*arg2
= PG_GETARG_VARBIT_P(1);
910 result
= (bit_cmp(arg1
, arg2
) <= 0);
912 PG_FREE_IF_COPY(arg1
, 0);
913 PG_FREE_IF_COPY(arg2
, 1);
915 PG_RETURN_BOOL(result
);
919 bitgt(PG_FUNCTION_ARGS
)
921 VarBit
*arg1
= PG_GETARG_VARBIT_P(0);
922 VarBit
*arg2
= PG_GETARG_VARBIT_P(1);
925 result
= (bit_cmp(arg1
, arg2
) > 0);
927 PG_FREE_IF_COPY(arg1
, 0);
928 PG_FREE_IF_COPY(arg2
, 1);
930 PG_RETURN_BOOL(result
);
934 bitge(PG_FUNCTION_ARGS
)
936 VarBit
*arg1
= PG_GETARG_VARBIT_P(0);
937 VarBit
*arg2
= PG_GETARG_VARBIT_P(1);
940 result
= (bit_cmp(arg1
, arg2
) >= 0);
942 PG_FREE_IF_COPY(arg1
, 0);
943 PG_FREE_IF_COPY(arg2
, 1);
945 PG_RETURN_BOOL(result
);
949 bitcmp(PG_FUNCTION_ARGS
)
951 VarBit
*arg1
= PG_GETARG_VARBIT_P(0);
952 VarBit
*arg2
= PG_GETARG_VARBIT_P(1);
955 result
= bit_cmp(arg1
, arg2
);
957 PG_FREE_IF_COPY(arg1
, 0);
958 PG_FREE_IF_COPY(arg2
, 1);
960 PG_RETURN_INT32(result
);
965 * Concatenation of bit strings
968 bitcat(PG_FUNCTION_ARGS
)
970 VarBit
*arg1
= PG_GETARG_VARBIT_P(0);
971 VarBit
*arg2
= PG_GETARG_VARBIT_P(1);
973 PG_RETURN_VARBIT_P(bit_catenate(arg1
, arg2
));
977 bit_catenate(VarBit
*arg1
, VarBit
*arg2
)
988 bitlen1
= VARBITLEN(arg1
);
989 bitlen2
= VARBITLEN(arg2
);
991 if (bitlen1
> VARBITMAXLEN
- bitlen2
)
993 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED
),
994 errmsg("bit string length exceeds the maximum allowed (%d)",
996 bytelen
= VARBITTOTALLEN(bitlen1
+ bitlen2
);
998 result
= (VarBit
*) palloc(bytelen
);
999 SET_VARSIZE(result
, bytelen
);
1000 VARBITLEN(result
) = bitlen1
+ bitlen2
;
1002 /* Copy the first bitstring in */
1003 memcpy(VARBITS(result
), VARBITS(arg1
), VARBITBYTES(arg1
));
1005 /* Copy the second bit string */
1006 bit1pad
= VARBITPAD(arg1
);
1009 memcpy(VARBITS(result
) + VARBITBYTES(arg1
), VARBITS(arg2
),
1012 else if (bitlen2
> 0)
1014 /* We need to shift all the bits to fit */
1015 bit2shift
= BITS_PER_BYTE
- bit1pad
;
1016 pr
= VARBITS(result
) + VARBITBYTES(arg1
) - 1;
1017 for (pa
= VARBITS(arg2
); pa
< VARBITEND(arg2
); pa
++)
1019 *pr
|= ((*pa
>> bit2shift
) & BITMASK
);
1021 if (pr
< VARBITEND(result
))
1022 *pr
= (*pa
<< bit1pad
) & BITMASK
;
1026 /* The pad bits should be already zero at this point */
1033 * retrieve a substring from the bit string.
1034 * Note, s is 1-based.
1038 bitsubstr(PG_FUNCTION_ARGS
)
1040 PG_RETURN_VARBIT_P(bitsubstring(PG_GETARG_VARBIT_P(0),
1047 bitsubstr_no_len(PG_FUNCTION_ARGS
)
1049 PG_RETURN_VARBIT_P(bitsubstring(PG_GETARG_VARBIT_P(0),
1055 bitsubstring(VarBit
*arg
, int32 s
, int32 l
, bool length_not_specified
)
1069 bitlen
= VARBITLEN(arg
);
1071 /* If we do not have an upper bound, use end of string */
1072 if (length_not_specified
)
1078 /* SQL99 says to throw an error for E < S, i.e., negative length */
1080 (errcode(ERRCODE_SUBSTRING_ERROR
),
1081 errmsg("negative substring length not allowed")));
1082 e1
= -1; /* silence stupider compilers */
1084 else if (pg_add_s32_overflow(s
, l
, &e
))
1087 * L could be large enough for S + L to overflow, in which case the
1088 * substring must run to end of string.
1094 e1
= Min(e
, bitlen
+ 1);
1096 if (s1
> bitlen
|| e1
<= s1
)
1098 /* Need to return a zero-length bitstring */
1099 len
= VARBITTOTALLEN(0);
1100 result
= (VarBit
*) palloc(len
);
1101 SET_VARSIZE(result
, len
);
1102 VARBITLEN(result
) = 0;
1107 * OK, we've got a true substring starting at position s1-1 and ending
1111 len
= VARBITTOTALLEN(rbitlen
);
1112 result
= (VarBit
*) palloc(len
);
1113 SET_VARSIZE(result
, len
);
1114 VARBITLEN(result
) = rbitlen
;
1115 len
-= VARHDRSZ
+ VARBITHDRSZ
;
1116 /* Are we copying from a byte boundary? */
1117 if ((s1
- 1) % BITS_PER_BYTE
== 0)
1119 /* Yep, we are copying bytes */
1120 memcpy(VARBITS(result
), VARBITS(arg
) + (s1
- 1) / BITS_PER_BYTE
,
1125 /* Figure out how much we need to shift the sequence by */
1126 ishift
= (s1
- 1) % BITS_PER_BYTE
;
1127 r
= VARBITS(result
);
1128 ps
= VARBITS(arg
) + (s1
- 1) / BITS_PER_BYTE
;
1129 for (i
= 0; i
< len
; i
++)
1131 *r
= (*ps
<< ishift
) & BITMASK
;
1132 if ((++ps
) < VARBITEND(arg
))
1133 *r
|= *ps
>> (BITS_PER_BYTE
- ishift
);
1138 /* Make sure last byte is correctly zero-padded */
1147 * Replace specified substring of first string with second
1149 * The SQL standard defines OVERLAY() in terms of substring and concatenation.
1150 * This code is a direct implementation of what the standard says.
1153 bitoverlay(PG_FUNCTION_ARGS
)
1155 VarBit
*t1
= PG_GETARG_VARBIT_P(0);
1156 VarBit
*t2
= PG_GETARG_VARBIT_P(1);
1157 int sp
= PG_GETARG_INT32(2); /* substring start position */
1158 int sl
= PG_GETARG_INT32(3); /* substring length */
1160 PG_RETURN_VARBIT_P(bit_overlay(t1
, t2
, sp
, sl
));
1164 bitoverlay_no_len(PG_FUNCTION_ARGS
)
1166 VarBit
*t1
= PG_GETARG_VARBIT_P(0);
1167 VarBit
*t2
= PG_GETARG_VARBIT_P(1);
1168 int sp
= PG_GETARG_INT32(2); /* substring start position */
1171 sl
= VARBITLEN(t2
); /* defaults to length(t2) */
1172 PG_RETURN_VARBIT_P(bit_overlay(t1
, t2
, sp
, sl
));
1176 bit_overlay(VarBit
*t1
, VarBit
*t2
, int sp
, int sl
)
1184 * Check for possible integer-overflow cases. For negative sp, throw a
1185 * "substring length" error because that's what should be expected
1186 * according to the spec's definition of OVERLAY().
1190 (errcode(ERRCODE_SUBSTRING_ERROR
),
1191 errmsg("negative substring length not allowed")));
1192 if (pg_add_s32_overflow(sp
, sl
, &sp_pl_sl
))
1194 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1195 errmsg("integer out of range")));
1197 s1
= bitsubstring(t1
, 1, sp
- 1, false);
1198 s2
= bitsubstring(t1
, sp_pl_sl
, -1, true);
1199 result
= bit_catenate(s1
, t2
);
1200 result
= bit_catenate(result
, s2
);
1208 * Returns the number of bits set in a bit string.
1211 bit_bit_count(PG_FUNCTION_ARGS
)
1213 VarBit
*arg
= PG_GETARG_VARBIT_P(0);
1215 PG_RETURN_INT64(pg_popcount((char *) VARBITS(arg
), VARBITBYTES(arg
)));
1219 * bitlength, bitoctetlength
1220 * Return the length of a bit string
1223 bitlength(PG_FUNCTION_ARGS
)
1225 VarBit
*arg
= PG_GETARG_VARBIT_P(0);
1227 PG_RETURN_INT32(VARBITLEN(arg
));
1231 bitoctetlength(PG_FUNCTION_ARGS
)
1233 VarBit
*arg
= PG_GETARG_VARBIT_P(0);
1235 PG_RETURN_INT32(VARBITBYTES(arg
));
1240 * perform a logical AND on two bit strings.
1243 bit_and(PG_FUNCTION_ARGS
)
1245 VarBit
*arg1
= PG_GETARG_VARBIT_P(0);
1246 VarBit
*arg2
= PG_GETARG_VARBIT_P(1);
1256 bitlen1
= VARBITLEN(arg1
);
1257 bitlen2
= VARBITLEN(arg2
);
1258 if (bitlen1
!= bitlen2
)
1260 (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH
),
1261 errmsg("cannot AND bit strings of different sizes")));
1263 len
= VARSIZE(arg1
);
1264 result
= (VarBit
*) palloc(len
);
1265 SET_VARSIZE(result
, len
);
1266 VARBITLEN(result
) = bitlen1
;
1270 r
= VARBITS(result
);
1271 for (i
= 0; i
< VARBITBYTES(arg1
); i
++)
1272 *r
++ = *p1
++ & *p2
++;
1274 /* Padding is not needed as & of 0 pads is 0 */
1276 PG_RETURN_VARBIT_P(result
);
1281 * perform a logical OR on two bit strings.
1284 bit_or(PG_FUNCTION_ARGS
)
1286 VarBit
*arg1
= PG_GETARG_VARBIT_P(0);
1287 VarBit
*arg2
= PG_GETARG_VARBIT_P(1);
1297 bitlen1
= VARBITLEN(arg1
);
1298 bitlen2
= VARBITLEN(arg2
);
1299 if (bitlen1
!= bitlen2
)
1301 (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH
),
1302 errmsg("cannot OR bit strings of different sizes")));
1303 len
= VARSIZE(arg1
);
1304 result
= (VarBit
*) palloc(len
);
1305 SET_VARSIZE(result
, len
);
1306 VARBITLEN(result
) = bitlen1
;
1310 r
= VARBITS(result
);
1311 for (i
= 0; i
< VARBITBYTES(arg1
); i
++)
1312 *r
++ = *p1
++ | *p2
++;
1314 /* Padding is not needed as | of 0 pads is 0 */
1316 PG_RETURN_VARBIT_P(result
);
1321 * perform a logical XOR on two bit strings.
1324 bitxor(PG_FUNCTION_ARGS
)
1326 VarBit
*arg1
= PG_GETARG_VARBIT_P(0);
1327 VarBit
*arg2
= PG_GETARG_VARBIT_P(1);
1337 bitlen1
= VARBITLEN(arg1
);
1338 bitlen2
= VARBITLEN(arg2
);
1339 if (bitlen1
!= bitlen2
)
1341 (errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH
),
1342 errmsg("cannot XOR bit strings of different sizes")));
1344 len
= VARSIZE(arg1
);
1345 result
= (VarBit
*) palloc(len
);
1346 SET_VARSIZE(result
, len
);
1347 VARBITLEN(result
) = bitlen1
;
1351 r
= VARBITS(result
);
1352 for (i
= 0; i
< VARBITBYTES(arg1
); i
++)
1353 *r
++ = *p1
++ ^ *p2
++;
1355 /* Padding is not needed as ^ of 0 pads is 0 */
1357 PG_RETURN_VARBIT_P(result
);
1362 * perform a logical NOT on a bit string.
1365 bitnot(PG_FUNCTION_ARGS
)
1367 VarBit
*arg
= PG_GETARG_VARBIT_P(0);
1372 result
= (VarBit
*) palloc(VARSIZE(arg
));
1373 SET_VARSIZE(result
, VARSIZE(arg
));
1374 VARBITLEN(result
) = VARBITLEN(arg
);
1377 r
= VARBITS(result
);
1378 for (; p
< VARBITEND(arg
); p
++)
1381 /* Must zero-pad the result, because extra bits are surely 1's here */
1382 VARBIT_PAD_LAST(result
, r
);
1384 PG_RETURN_VARBIT_P(result
);
1389 * do a left shift (i.e. towards the beginning of the string)
1392 bitshiftleft(PG_FUNCTION_ARGS
)
1394 VarBit
*arg
= PG_GETARG_VARBIT_P(0);
1395 int32 shft
= PG_GETARG_INT32(1);
1403 /* Negative shift is a shift to the right */
1406 /* Prevent integer overflow in negation */
1407 if (shft
< -VARBITMAXLEN
)
1408 shft
= -VARBITMAXLEN
;
1409 PG_RETURN_DATUM(DirectFunctionCall2(bitshiftright
,
1410 VarBitPGetDatum(arg
),
1411 Int32GetDatum(-shft
)));
1414 result
= (VarBit
*) palloc(VARSIZE(arg
));
1415 SET_VARSIZE(result
, VARSIZE(arg
));
1416 VARBITLEN(result
) = VARBITLEN(arg
);
1417 r
= VARBITS(result
);
1419 /* If we shifted all the bits out, return an all-zero string */
1420 if (shft
>= VARBITLEN(arg
))
1422 MemSet(r
, 0, VARBITBYTES(arg
));
1423 PG_RETURN_VARBIT_P(result
);
1426 byte_shift
= shft
/ BITS_PER_BYTE
;
1427 ishift
= shft
% BITS_PER_BYTE
;
1428 p
= VARBITS(arg
) + byte_shift
;
1432 /* Special case: we can do a memcpy */
1433 len
= VARBITBYTES(arg
) - byte_shift
;
1435 MemSet(r
+ len
, 0, byte_shift
);
1439 for (; p
< VARBITEND(arg
); r
++)
1442 if ((++p
) < VARBITEND(arg
))
1443 *r
|= *p
>> (BITS_PER_BYTE
- ishift
);
1445 for (; r
< VARBITEND(result
); r
++)
1449 /* The pad bits should be already zero at this point */
1451 PG_RETURN_VARBIT_P(result
);
1456 * do a right shift (i.e. towards the end of the string)
1459 bitshiftright(PG_FUNCTION_ARGS
)
1461 VarBit
*arg
= PG_GETARG_VARBIT_P(0);
1462 int32 shft
= PG_GETARG_INT32(1);
1470 /* Negative shift is a shift to the left */
1473 /* Prevent integer overflow in negation */
1474 if (shft
< -VARBITMAXLEN
)
1475 shft
= -VARBITMAXLEN
;
1476 PG_RETURN_DATUM(DirectFunctionCall2(bitshiftleft
,
1477 VarBitPGetDatum(arg
),
1478 Int32GetDatum(-shft
)));
1481 result
= (VarBit
*) palloc(VARSIZE(arg
));
1482 SET_VARSIZE(result
, VARSIZE(arg
));
1483 VARBITLEN(result
) = VARBITLEN(arg
);
1484 r
= VARBITS(result
);
1486 /* If we shifted all the bits out, return an all-zero string */
1487 if (shft
>= VARBITLEN(arg
))
1489 MemSet(r
, 0, VARBITBYTES(arg
));
1490 PG_RETURN_VARBIT_P(result
);
1493 byte_shift
= shft
/ BITS_PER_BYTE
;
1494 ishift
= shft
% BITS_PER_BYTE
;
1497 /* Set the first part of the result to 0 */
1498 MemSet(r
, 0, byte_shift
);
1503 /* Special case: we can do a memcpy */
1504 len
= VARBITBYTES(arg
) - byte_shift
;
1510 if (r
< VARBITEND(result
))
1511 *r
= 0; /* initialize first byte */
1512 for (; r
< VARBITEND(result
); p
++)
1515 if ((++r
) < VARBITEND(result
))
1516 *r
= (*p
<< (BITS_PER_BYTE
- ishift
)) & BITMASK
;
1520 /* We may have shifted 1's into the pad bits, so fix that */
1521 VARBIT_PAD_LAST(result
, r
);
1523 PG_RETURN_VARBIT_P(result
);
1527 * This is not defined in any standard. We retain the natural ordering of
1528 * bits here, as it just seems more intuitive.
1531 bitfromint4(PG_FUNCTION_ARGS
)
1533 int32 a
= PG_GETARG_INT32(0);
1534 int32 typmod
= PG_GETARG_INT32(1);
1541 if (typmod
<= 0 || typmod
> VARBITMAXLEN
)
1542 typmod
= 1; /* default bit length */
1544 rlen
= VARBITTOTALLEN(typmod
);
1545 result
= (VarBit
*) palloc(rlen
);
1546 SET_VARSIZE(result
, rlen
);
1547 VARBITLEN(result
) = typmod
;
1549 r
= VARBITS(result
);
1550 destbitsleft
= typmod
;
1552 /* drop any input bits that don't fit */
1553 srcbitsleft
= Min(srcbitsleft
, destbitsleft
);
1554 /* sign-fill any excess bytes in output */
1555 while (destbitsleft
>= srcbitsleft
+ 8)
1557 *r
++ = (bits8
) ((a
< 0) ? BITMASK
: 0);
1560 /* store first fractional byte */
1561 if (destbitsleft
> srcbitsleft
)
1563 unsigned int val
= (unsigned int) (a
>> (destbitsleft
- 8));
1565 /* Force sign-fill in case the compiler implements >> as zero-fill */
1567 val
|= ((unsigned int) -1) << (srcbitsleft
+ 8 - destbitsleft
);
1568 *r
++ = (bits8
) (val
& BITMASK
);
1571 /* Now srcbitsleft and destbitsleft are the same, need not track both */
1572 /* store whole bytes */
1573 while (destbitsleft
>= 8)
1575 *r
++ = (bits8
) ((a
>> (destbitsleft
- 8)) & BITMASK
);
1578 /* store last fractional byte */
1579 if (destbitsleft
> 0)
1580 *r
= (bits8
) ((a
<< (8 - destbitsleft
)) & BITMASK
);
1582 PG_RETURN_VARBIT_P(result
);
1586 bittoint4(PG_FUNCTION_ARGS
)
1588 VarBit
*arg
= PG_GETARG_VARBIT_P(0);
1592 /* Check that the bit string is not too long */
1593 if (VARBITLEN(arg
) > sizeof(result
) * BITS_PER_BYTE
)
1595 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1596 errmsg("integer out of range")));
1599 for (r
= VARBITS(arg
); r
< VARBITEND(arg
); r
++)
1601 result
<<= BITS_PER_BYTE
;
1604 /* Now shift the result to take account of the padding at the end */
1605 result
>>= VARBITPAD(arg
);
1607 PG_RETURN_INT32(result
);
1611 bitfromint8(PG_FUNCTION_ARGS
)
1613 int64 a
= PG_GETARG_INT64(0);
1614 int32 typmod
= PG_GETARG_INT32(1);
1621 if (typmod
<= 0 || typmod
> VARBITMAXLEN
)
1622 typmod
= 1; /* default bit length */
1624 rlen
= VARBITTOTALLEN(typmod
);
1625 result
= (VarBit
*) palloc(rlen
);
1626 SET_VARSIZE(result
, rlen
);
1627 VARBITLEN(result
) = typmod
;
1629 r
= VARBITS(result
);
1630 destbitsleft
= typmod
;
1632 /* drop any input bits that don't fit */
1633 srcbitsleft
= Min(srcbitsleft
, destbitsleft
);
1634 /* sign-fill any excess bytes in output */
1635 while (destbitsleft
>= srcbitsleft
+ 8)
1637 *r
++ = (bits8
) ((a
< 0) ? BITMASK
: 0);
1640 /* store first fractional byte */
1641 if (destbitsleft
> srcbitsleft
)
1643 unsigned int val
= (unsigned int) (a
>> (destbitsleft
- 8));
1645 /* Force sign-fill in case the compiler implements >> as zero-fill */
1647 val
|= ((unsigned int) -1) << (srcbitsleft
+ 8 - destbitsleft
);
1648 *r
++ = (bits8
) (val
& BITMASK
);
1651 /* Now srcbitsleft and destbitsleft are the same, need not track both */
1652 /* store whole bytes */
1653 while (destbitsleft
>= 8)
1655 *r
++ = (bits8
) ((a
>> (destbitsleft
- 8)) & BITMASK
);
1658 /* store last fractional byte */
1659 if (destbitsleft
> 0)
1660 *r
= (bits8
) ((a
<< (8 - destbitsleft
)) & BITMASK
);
1662 PG_RETURN_VARBIT_P(result
);
1666 bittoint8(PG_FUNCTION_ARGS
)
1668 VarBit
*arg
= PG_GETARG_VARBIT_P(0);
1672 /* Check that the bit string is not too long */
1673 if (VARBITLEN(arg
) > sizeof(result
) * BITS_PER_BYTE
)
1675 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE
),
1676 errmsg("bigint out of range")));
1679 for (r
= VARBITS(arg
); r
< VARBITEND(arg
); r
++)
1681 result
<<= BITS_PER_BYTE
;
1684 /* Now shift the result to take account of the padding at the end */
1685 result
>>= VARBITPAD(arg
);
1687 PG_RETURN_INT64(result
);
1692 * Determines the position of S2 in the bitstring S1 (1-based string).
1693 * If S2 does not appear in S1 this function returns 0.
1694 * If S2 is of length 0 this function returns 1.
1695 * Compatible in usage with POSITION() functions for other data types.
1698 bitposition(PG_FUNCTION_ARGS
)
1700 VarBit
*str
= PG_GETARG_VARBIT_P(0);
1701 VarBit
*substr
= PG_GETARG_VARBIT_P(1);
1706 bits8
*s
, /* pointer into substring */
1707 *p
; /* pointer into str */
1708 bits8 cmp
, /* shifted substring byte to compare */
1709 mask1
, /* mask for substring byte shifted right */
1710 mask2
, /* mask for substring byte shifted left */
1711 end_mask
, /* pad mask for last substring byte */
1712 str_mask
; /* pad mask for last string byte */
1715 /* Get the substring length */
1716 substr_length
= VARBITLEN(substr
);
1717 str_length
= VARBITLEN(str
);
1719 /* String has zero length or substring longer than string, return 0 */
1720 if ((str_length
== 0) || (substr_length
> str_length
))
1723 /* zero-length substring means return 1 */
1724 if (substr_length
== 0)
1727 /* Initialise the padding masks */
1728 end_mask
= BITMASK
<< VARBITPAD(substr
);
1729 str_mask
= BITMASK
<< VARBITPAD(str
);
1730 for (i
= 0; i
< VARBITBYTES(str
) - VARBITBYTES(substr
) + 1; i
++)
1732 for (is
= 0; is
< BITS_PER_BYTE
; is
++)
1735 p
= VARBITS(str
) + i
;
1736 mask1
= BITMASK
>> is
;
1738 for (s
= VARBITS(substr
);
1739 is_match
&& s
< VARBITEND(substr
); s
++)
1742 if (s
== VARBITEND(substr
) - 1)
1744 mask1
&= end_mask
>> is
;
1745 if (p
== VARBITEND(str
) - 1)
1747 /* Check that there is enough of str left */
1748 if (mask1
& ~str_mask
)
1756 is_match
= ((cmp
^ *p
) & mask1
) == 0;
1759 /* Move on to the next byte */
1761 if (p
== VARBITEND(str
))
1763 mask2
= end_mask
<< (BITS_PER_BYTE
- is
);
1764 is_match
= mask2
== 0;
1766 elog(DEBUG4
, "S. %d %d em=%2x sm=%2x r=%d",
1767 i
, is
, end_mask
, mask2
, is_match
);
1771 cmp
= *s
<< (BITS_PER_BYTE
- is
);
1772 if (s
== VARBITEND(substr
) - 1)
1774 mask2
&= end_mask
<< (BITS_PER_BYTE
- is
);
1775 if (p
== VARBITEND(str
) - 1)
1777 if (mask2
& ~str_mask
)
1785 is_match
= ((cmp
^ *p
) & mask2
) == 0;
1787 /* Have we found a match? */
1789 PG_RETURN_INT32(i
* BITS_PER_BYTE
+ is
+ 1);
1799 * Given an instance of type 'bit' creates a new one with
1800 * the Nth bit set to the given value.
1802 * The bit location is specified left-to-right in a zero-based fashion
1803 * consistent with the other get_bit and set_bit functions, but
1804 * inconsistent with the standard substring, position, overlay functions
1807 bitsetbit(PG_FUNCTION_ARGS
)
1809 VarBit
*arg1
= PG_GETARG_VARBIT_P(0);
1810 int32 n
= PG_GETARG_INT32(1);
1811 int32 newBit
= PG_GETARG_INT32(2);
1820 bitlen
= VARBITLEN(arg1
);
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")));
1835 len
= VARSIZE(arg1
);
1836 result
= (VarBit
*) palloc(len
);
1837 SET_VARSIZE(result
, len
);
1838 VARBITLEN(result
) = bitlen
;
1841 r
= VARBITS(result
);
1843 memcpy(r
, p
, VARBITBYTES(arg1
));
1845 byteNo
= n
/ BITS_PER_BYTE
;
1846 bitNo
= BITS_PER_BYTE
- 1 - (n
% BITS_PER_BYTE
);
1852 r
[byteNo
] &= (~(1 << bitNo
));
1854 r
[byteNo
] |= (1 << bitNo
);
1856 PG_RETURN_VARBIT_P(result
);
1862 * returns the value of the Nth bit of a bit array (0 or 1).
1864 * The bit location is specified left-to-right in a zero-based fashion
1865 * consistent with the other get_bit and set_bit functions, but
1866 * inconsistent with the standard substring, position, overlay functions
1869 bitgetbit(PG_FUNCTION_ARGS
)
1871 VarBit
*arg1
= PG_GETARG_VARBIT_P(0);
1872 int32 n
= PG_GETARG_INT32(1);
1878 bitlen
= VARBITLEN(arg1
);
1879 if (n
< 0 || n
>= bitlen
)
1881 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR
),
1882 errmsg("bit index %d out of valid range (0..%d)",
1887 byteNo
= n
/ BITS_PER_BYTE
;
1888 bitNo
= BITS_PER_BYTE
- 1 - (n
% BITS_PER_BYTE
);
1890 if (p
[byteNo
] & (1 << bitNo
))