mirror of
https://git.postgresql.org/git/postgresql.git
synced 2025-03-25 20:10:41 +08:00
Hi,
here is an updated version of the bit type with a bugfix and all the necessa ry SQL functions defined. This should replace what is currently in contrib. I'd appreciate any comments on what is there. Kind regards, Adriaan
This commit is contained in:
parent
5454b37921
commit
51cfdae50f
contrib/bit
src/bin/pgaccess/lib/languages
@ -1,10 +1,46 @@
|
||||
CFLAGS = -g
|
||||
ifndef PGDIR
|
||||
PGDIR= ../..
|
||||
PGDIR=/data/build/postgresql-7.0beta3
|
||||
endif
|
||||
|
||||
varbit: vartest.o varbit.o
|
||||
$(CC) $(CFLAGS) -o $@ $^
|
||||
SRCDIR= $(PGDIR)/src
|
||||
|
||||
varbit.o: varbit.c varbit.h
|
||||
vartest.o: vartest.c varbit.h
|
||||
include $(SRCDIR)/Makefile.global
|
||||
|
||||
TARGETS= varbit.sql varbit$(DLSUFFIX)
|
||||
# vartest
|
||||
SOURCE= varbit.c varbit_glue.c
|
||||
OBJ= $(SOURCE:.c=.o)
|
||||
CFLAGS += -g
|
||||
|
||||
all: $(TARGETS)
|
||||
|
||||
varbit$(DLSUFFIX): $(OBJ)
|
||||
$(CC) $(CFLAGS) -shared -o varbit$(DLSUFFIX) $(SOURCE) $(CLIBS)
|
||||
|
||||
vartest: varbit.o vartest.o
|
||||
$(CC) -o $@ varbit.o vartest.o
|
||||
|
||||
install:
|
||||
$(MAKE) all
|
||||
cp -p varbit$(DLSUFFIX) $(LIBDIR)/contrib
|
||||
chmod 555 $(LIBDIR)/contrib/varbit$(DLSUFFIX)
|
||||
|
||||
%.sql: %.source
|
||||
echo $(SRCDIR)
|
||||
if [ -z "$$USER" ]; then USER=$$LOGNAME; fi; \
|
||||
if [ -z "$$USER" ]; then USER=`whoami`; fi; \
|
||||
if [ -z "$$USER" ]; then echo 'Cannot deduce $$USER.'; exit 1; fi; \
|
||||
rm -f $@; \
|
||||
C=`pwd`; \
|
||||
O=$C; \
|
||||
if [ -d ${LIBDIR}/contrib ]; then O=${LIBDIR}/contrib; else \
|
||||
echo "contrib directory does not exist."; fi; \
|
||||
sed -e "s:_CWD_:$$C:g" \
|
||||
-e "s:_OBJWD_:$$O:g" \
|
||||
-e "s:_DLSUFFIX_:$(DLSUFFIX):g" \
|
||||
-e "s/_USER_/$$USER/g" < $< > $@
|
||||
|
||||
clean:
|
||||
rm -f $(TARGETS) varbit.o
|
||||
|
||||
clean:
|
||||
rm -f *.o varbit
|
||||
|
@ -7,9 +7,6 @@ make this code as independent as possible of the byte length, but it
|
||||
is quite possible that there may be problems on machines that don't
|
||||
have 8 bits/byte (are there still any around?).
|
||||
|
||||
In the input routines I have assumed that the parser eats the quotes
|
||||
in B'...' or X'...'.
|
||||
|
||||
The SQL standard only defines comparison, SUBSTR and concatenation
|
||||
operators, and these have been implemented. In addition all logical
|
||||
operators have been implemented, i.e. ~,|,&,^,<< and >>. This is
|
||||
|
@ -4,18 +4,14 @@
|
||||
* Functions for the built-in type bit() and varying bit().
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/contrib/bit/Attic/varbit.c,v 1.1 1999/11/29 22:34:36 momjian Exp $
|
||||
* $Header: /cvsroot/pgsql/contrib/bit/Attic/varbit.c,v 1.2 2000/04/03 20:56:40 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
#include "postgres.h"
|
||||
#include "varbit.h"
|
||||
/*
|
||||
#include "access/htup.h"
|
||||
#include "catalog/pg_type.h"
|
||||
#include "utils/builtins.h"
|
||||
*/
|
||||
|
||||
/*#include "catalog/pg_type.h" */
|
||||
/*#include "utils/builtins.h" */
|
||||
|
||||
|
||||
/*
|
||||
@ -43,22 +39,22 @@
|
||||
* (XXX dummy is here because we pass typelem as the second argument
|
||||
* for array_in. copied this, no idea what it means??)
|
||||
*/
|
||||
char *
|
||||
bits8 *
|
||||
zpbitin(char *s, int dummy, int32 atttypmod)
|
||||
{
|
||||
char *result,
|
||||
*sp; /* pointer into the character string */
|
||||
bits8 *result; /* the bits string that was read in */
|
||||
char *sp; /* pointer into the character string */
|
||||
bits8 *r;
|
||||
int len, /* Length of the whole data structure */
|
||||
bitlen, /* Number of bits in the bit string */
|
||||
slen; /* Length of the input string */
|
||||
int bit_not_hex; /* 0 = hex string 1=bit string */
|
||||
int i, bc, ipad;
|
||||
bits8 x, y;
|
||||
int bit_not_hex = 0; /* 0 = hex string 1=bit string */
|
||||
int bc, ipad;
|
||||
bits8 x = 0;
|
||||
|
||||
|
||||
if (s == NULL)
|
||||
return NULL;
|
||||
return (bits8 *) NULL;
|
||||
|
||||
/* Check that the first character is a b or an x */
|
||||
if (s[0]=='b' || s[0]=='B')
|
||||
@ -82,7 +78,8 @@ zpbitin(char *s, int dummy, int32 atttypmod)
|
||||
if (atttypmod == -1)
|
||||
atttypmod = bitlen;
|
||||
else
|
||||
if (bitlen>atttypmod && bit_not_hex || bitlen>atttypmod+3 && !bit_not_hex)
|
||||
if ((bitlen>atttypmod && bit_not_hex) ||
|
||||
(bitlen>atttypmod+3 && !bit_not_hex))
|
||||
elog(ERROR, "zpbitin: bit string of size %d cannot be written into bits(%d)",
|
||||
bitlen,atttypmod);
|
||||
|
||||
@ -90,10 +87,10 @@ zpbitin(char *s, int dummy, int32 atttypmod)
|
||||
len = VARBITDATALEN(atttypmod);
|
||||
|
||||
if (len > MaxAttrSize)
|
||||
elog(ERROR, "zpbitin: length of bit() must be less than %d",
|
||||
elog(ERROR, "zpbitin: length of bit() must be less than %ld",
|
||||
(MaxAttrSize-VARHDRSZ-VARBITHDRSZ)*BITSPERBYTE);
|
||||
|
||||
result = (char *) palloc(len);
|
||||
result = (bits8 *) palloc(len);
|
||||
/* set to 0 so that *r is always initialised and strin is zero-padded */
|
||||
memset(result, 0, len);
|
||||
VARSIZE(result) = len;
|
||||
@ -103,7 +100,7 @@ zpbitin(char *s, int dummy, int32 atttypmod)
|
||||
significant byte first. s points to the byte before the beginning
|
||||
of the bitstring */
|
||||
sp = s+1;
|
||||
r = (bits8 *) VARBITS(result);
|
||||
r = VARBITS(result);
|
||||
if (bit_not_hex)
|
||||
{
|
||||
/* Parse the bit representation of the string */
|
||||
@ -166,10 +163,10 @@ zpbitin(char *s, int dummy, int32 atttypmod)
|
||||
* for long strings
|
||||
*/
|
||||
char *
|
||||
zpbitout(char *s)
|
||||
zpbitout(bits8 *s)
|
||||
{
|
||||
char *result, *r;
|
||||
VarBit sp;
|
||||
bits8 *sp;
|
||||
int i, len, bitlen;
|
||||
|
||||
if (s == NULL)
|
||||
@ -183,7 +180,7 @@ zpbitout(char *s)
|
||||
bitlen = VARBITLEN(s);
|
||||
len = bitlen/4 + (bitlen%4>0 ? 1 : 0);
|
||||
result = (char *) palloc(len + 4);
|
||||
sp = (bits8 *) VARBITS(s);
|
||||
sp = VARBITS(s);
|
||||
r = result;
|
||||
*r++ = 'X';
|
||||
*r++ = '\'';
|
||||
@ -206,10 +203,10 @@ zpbitout(char *s)
|
||||
* Prints the string a bits
|
||||
*/
|
||||
char *
|
||||
zpbitsout(char *s)
|
||||
zpbitsout(bits8 *s)
|
||||
{
|
||||
char *result, *r;
|
||||
VarBit sp;
|
||||
bits8 *sp;
|
||||
bits8 x;
|
||||
int i, k, len;
|
||||
|
||||
@ -223,7 +220,7 @@ zpbitsout(char *s)
|
||||
{
|
||||
len = VARBITLEN(s);
|
||||
result = (char *) palloc(len + 4);
|
||||
sp = (bits8 *) VARBITS(s);
|
||||
sp = VARBITS(s);
|
||||
r = result;
|
||||
*r++ = 'B';
|
||||
*r++ = '\'';
|
||||
@ -252,22 +249,22 @@ zpbitsout(char *s)
|
||||
* varbitin -
|
||||
* converts a string to the internal representation of a bitstring.
|
||||
*/
|
||||
char *
|
||||
bits8 *
|
||||
varbitin(char *s, int dummy, int32 atttypmod)
|
||||
{
|
||||
char *result,
|
||||
*sp; /* pointer into the character string */
|
||||
bits8 *result; /* The resulting bit string */
|
||||
char *sp; /* pointer into the character string */
|
||||
bits8 *r;
|
||||
int len, /* Length of the whole data structure */
|
||||
bitlen, /* Number of bits in the bit string */
|
||||
slen; /* Length of the input string */
|
||||
int bit_not_hex;
|
||||
int i, bc, ipad;
|
||||
bits8 x, y;
|
||||
int bit_not_hex = 0;
|
||||
int bc, ipad;
|
||||
bits8 x = 0;
|
||||
|
||||
|
||||
if (s == NULL)
|
||||
return NULL;
|
||||
return (bits8 *) NULL;
|
||||
|
||||
/* Check that the first character is a b or an x */
|
||||
if (s[0]=='b' || s[0]=='B')
|
||||
@ -289,7 +286,8 @@ varbitin(char *s, int dummy, int32 atttypmod)
|
||||
reading a hex string and not by more than 3 bits, as a hex string gives
|
||||
and accurate length upto 4 bits */
|
||||
if (atttypmod > -1)
|
||||
if (bitlen>atttypmod && bit_not_hex || bitlen>atttypmod+3 && !bit_not_hex)
|
||||
if ((bitlen>atttypmod && bit_not_hex) ||
|
||||
(bitlen>atttypmod+3 && !bit_not_hex))
|
||||
elog(ERROR, "varbitin: bit string of size %d cannot be written into varying bits(%d)",
|
||||
bitlen,atttypmod);
|
||||
|
||||
@ -297,10 +295,10 @@ varbitin(char *s, int dummy, int32 atttypmod)
|
||||
len = VARBITDATALEN(bitlen);
|
||||
|
||||
if (len > MaxAttrSize)
|
||||
elog(ERROR, "varbitin: length of bit() must be less than %d",
|
||||
elog(ERROR, "varbitin: length of bit() must be less than %ld",
|
||||
(MaxAttrSize-VARHDRSZ-VARBITHDRSZ)*BITSPERBYTE);
|
||||
|
||||
result = (char *) palloc(len);
|
||||
result = (bits8 *) palloc(len);
|
||||
/* set to 0 so that *r is always initialised and strin is zero-padded */
|
||||
memset(result, 0, len);
|
||||
VARSIZE(result) = len;
|
||||
@ -310,7 +308,7 @@ varbitin(char *s, int dummy, int32 atttypmod)
|
||||
significant byte first. s points to the byte before the beginning
|
||||
of the bitstring */
|
||||
sp = s + 1;
|
||||
r = (VarBit) VARBITS(result);
|
||||
r = VARBITS(result);
|
||||
if (bit_not_hex)
|
||||
{
|
||||
/* Parse the bit representation of the string */
|
||||
@ -383,11 +381,10 @@ varbitin(char *s, int dummy, int32 atttypmod)
|
||||
*/
|
||||
|
||||
bool
|
||||
biteq (char *arg1, char *arg2)
|
||||
biteq (bits8 *arg1, bits8 *arg2)
|
||||
{
|
||||
int bitlen1,
|
||||
bitlen2;
|
||||
bits8 *p1, *p2;
|
||||
|
||||
if (!PointerIsValid(arg1) || !PointerIsValid(arg2))
|
||||
return (bool) 0;
|
||||
@ -402,11 +399,10 @@ biteq (char *arg1, char *arg2)
|
||||
}
|
||||
|
||||
bool
|
||||
bitne (char *arg1, char *arg2)
|
||||
bitne (bits8 *arg1, bits8 *arg2)
|
||||
{
|
||||
int bitlen1,
|
||||
bitlen2;
|
||||
bits8 *p1, *p2;
|
||||
|
||||
if (!PointerIsValid(arg1) || !PointerIsValid(arg2))
|
||||
return (bool) 0;
|
||||
@ -429,11 +425,10 @@ bitne (char *arg1, char *arg2)
|
||||
* Anything is equal to undefined.
|
||||
*/
|
||||
int
|
||||
bitcmp (char *arg1, char *arg2)
|
||||
bitcmp (bits8 *arg1, bits8 *arg2)
|
||||
{
|
||||
int bitlen1, bytelen1,
|
||||
bitlen2, bytelen2;
|
||||
bits8 *p1, *p2;
|
||||
int cmp;
|
||||
|
||||
if (!PointerIsValid(arg1) || !PointerIsValid(arg2))
|
||||
@ -452,25 +447,25 @@ bitcmp (char *arg1, char *arg2)
|
||||
}
|
||||
|
||||
bool
|
||||
bitlt (char *arg1, char *arg2)
|
||||
bitlt (bits8 *arg1, bits8 *arg2)
|
||||
{
|
||||
return (bool) (bitcmp(arg1,arg2) == -1);
|
||||
}
|
||||
|
||||
bool
|
||||
bitle (char *arg1, char *arg2)
|
||||
bitle (bits8 *arg1, bits8 *arg2)
|
||||
{
|
||||
return (bool) (bitcmp(arg1,arg2) <= 0);
|
||||
}
|
||||
|
||||
bool
|
||||
bitge (char *arg1, char *arg2)
|
||||
bitge (bits8 *arg1, bits8 *arg2)
|
||||
{
|
||||
return (bool) (bitcmp(arg1,arg2) >= 0);
|
||||
}
|
||||
|
||||
bool
|
||||
bitgt (char *arg1, char *arg2)
|
||||
bitgt (bits8 *arg1, bits8 *arg2)
|
||||
{
|
||||
return (bool) (bitcmp(arg1,arg2) == 1);
|
||||
}
|
||||
@ -478,11 +473,11 @@ bitgt (char *arg1, char *arg2)
|
||||
/* bitcat
|
||||
* Concatenation of bit strings
|
||||
*/
|
||||
char *
|
||||
bitcat (char *arg1, char *arg2)
|
||||
bits8 *
|
||||
bitcat (bits8 *arg1, bits8 *arg2)
|
||||
{
|
||||
int bitlen1, bitlen2, bytelen, bit1pad, bit2shift;
|
||||
char *result;
|
||||
bits8 *result;
|
||||
bits8 *pr, *pa;
|
||||
|
||||
if (!PointerIsValid(arg1) || !PointerIsValid(arg2))
|
||||
@ -493,7 +488,7 @@ bitcat (char *arg1, char *arg2)
|
||||
|
||||
bytelen = VARBITDATALEN(bitlen1+bitlen2);
|
||||
|
||||
result = (char *) palloc(bytelen*sizeof(bits8));
|
||||
result = (bits8 *) palloc(bytelen*sizeof(bits8));
|
||||
VARSIZE(result) = bytelen;
|
||||
VARBITLEN(result) = bitlen1+bitlen2;
|
||||
printf("%d %d %d \n",VARBITBYTES(arg1),VARBITLEN(arg1),VARBITPAD(arg1));
|
||||
@ -510,10 +505,10 @@ bitcat (char *arg1, char *arg2)
|
||||
{
|
||||
/* We need to shift all the results to fit */
|
||||
bit2shift = BITSPERBYTE - bit1pad;
|
||||
pa = (VarBit) VARBITS(arg2);
|
||||
pr = (VarBit) VARBITS(result)+VARBITBYTES(arg1)-1;
|
||||
pa = VARBITS(arg2);
|
||||
pr = VARBITS(result)+VARBITBYTES(arg1)-1;
|
||||
for ( ; pa < VARBITEND(arg2); pa++) {
|
||||
*pr = *pr | ((*pa >> bit2shift) & BITMASK);
|
||||
*pr |= ((*pa >> bit2shift) & BITMASK);
|
||||
pr++;
|
||||
if (pr < VARBITEND(result))
|
||||
*pr = (*pa << bit1pad) & BITMASK;
|
||||
@ -528,17 +523,17 @@ bitcat (char *arg1, char *arg2)
|
||||
* Note, s is 1-based.
|
||||
* SQL draft 6.10 9)
|
||||
*/
|
||||
char *
|
||||
bitsubstr (char *arg, int32 s, int32 l)
|
||||
bits8 *
|
||||
bitsubstr (bits8 *arg, int32 s, int32 l)
|
||||
{
|
||||
int bitlen,
|
||||
rbitlen,
|
||||
len,
|
||||
ipad,
|
||||
ipad = 0,
|
||||
ishift,
|
||||
i;
|
||||
int e, s1, e1;
|
||||
char * result;
|
||||
bits8 * result;
|
||||
bits8 mask, *r, *ps;
|
||||
|
||||
if (!PointerIsValid(arg))
|
||||
@ -552,7 +547,7 @@ bitsubstr (char *arg, int32 s, int32 l)
|
||||
{
|
||||
/* Need to return a null string */
|
||||
len = VARBITDATALEN(0);
|
||||
result = (char *) palloc(len);
|
||||
result = (bits8 *) palloc(len);
|
||||
VARBITLEN(result) = 0;
|
||||
VARSIZE(result) = len;
|
||||
}
|
||||
@ -562,22 +557,22 @@ bitsubstr (char *arg, int32 s, int32 l)
|
||||
ending at position e1-1 */
|
||||
rbitlen = e1-s1;
|
||||
len = VARBITDATALEN(rbitlen);
|
||||
result = (char *) palloc(len);
|
||||
result = (bits8 *) palloc(len);
|
||||
VARBITLEN(result) = rbitlen;
|
||||
VARSIZE(result) = len;
|
||||
len -= VARHDRSZ + VARBITHDRSZ;
|
||||
/* Are we copying from a byte boundary? */
|
||||
if ((s1-1)%BITSPERBYTE==0)
|
||||
{
|
||||
/* Yep, we are copying bytes */
|
||||
len -= VARHDRSZ + VARBITHDRSZ;
|
||||
memcpy(VARBITS(result),VARBITS(arg)+(s1-1)/BITSPERBYTE,len);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Figure out how much we need to shift the sequence by */
|
||||
ishift = (s1-1)%BITSPERBYTE;
|
||||
r = (VarBit) VARBITS(result);
|
||||
ps = (VarBit) VARBITS(arg) + (s1-1)/BITSPERBYTE;
|
||||
r = VARBITS(result);
|
||||
ps = VARBITS(arg) + (s1-1)/BITSPERBYTE;
|
||||
for (i=0; i<len; i++)
|
||||
{
|
||||
*r = (*ps <<ishift) & BITMASK;
|
||||
@ -602,12 +597,12 @@ bitsubstr (char *arg, int32 s, int32 l)
|
||||
* perform a logical AND on two bit strings. The result is automatically
|
||||
* truncated to the shorter bit string
|
||||
*/
|
||||
char *
|
||||
bitand (char * arg1, char * arg2)
|
||||
bits8 *
|
||||
bitand (bits8 * arg1, bits8 * arg2)
|
||||
{
|
||||
int len,
|
||||
i;
|
||||
char *result;
|
||||
bits8 *result;
|
||||
bits8 *p1,
|
||||
*p2,
|
||||
*r;
|
||||
@ -616,7 +611,7 @@ bitand (char * arg1, char * arg2)
|
||||
return (bool) 0;
|
||||
|
||||
len = Min(VARSIZE(arg1),VARSIZE(arg2));
|
||||
result = (char *) palloc(len);
|
||||
result = (bits8 *) palloc(len);
|
||||
VARSIZE(result) = len;
|
||||
VARBITLEN(result) = Min(VARBITLEN(arg1),VARBITLEN(arg2));
|
||||
|
||||
@ -635,12 +630,12 @@ bitand (char * arg1, char * arg2)
|
||||
* perform a logical OR on two bit strings. The result is automatically
|
||||
* truncated to the shorter bit string.
|
||||
*/
|
||||
char *
|
||||
bitor (char * arg1, char * arg2)
|
||||
bits8 *
|
||||
bitor (bits8 * arg1, bits8 * arg2)
|
||||
{
|
||||
int len,
|
||||
i;
|
||||
char *result;
|
||||
bits8 *result;
|
||||
bits8 *p1,
|
||||
*p2,
|
||||
*r;
|
||||
@ -650,7 +645,7 @@ bitor (char * arg1, char * arg2)
|
||||
return (bool) 0;
|
||||
|
||||
len = Min(VARSIZE(arg1),VARSIZE(arg2));
|
||||
result = (char *) palloc(len);
|
||||
result = (bits8 *) palloc(len);
|
||||
VARSIZE(result) = len;
|
||||
VARBITLEN(result) = Min(VARBITLEN(arg1),VARBITLEN(arg2));
|
||||
|
||||
@ -671,12 +666,12 @@ bitor (char * arg1, char * arg2)
|
||||
* perform a logical XOR on two bit strings. The result is automatically
|
||||
* truncated to the shorter bit string.
|
||||
*/
|
||||
char *
|
||||
bitxor (char * arg1, char * arg2)
|
||||
bits8 *
|
||||
bitxor (bits8 * arg1, bits8 * arg2)
|
||||
{
|
||||
int len,
|
||||
i;
|
||||
char *result;
|
||||
bits8 *result;
|
||||
bits8 *p1,
|
||||
*p2,
|
||||
*r;
|
||||
@ -686,7 +681,7 @@ bitxor (char * arg1, char * arg2)
|
||||
return (bool) 0;
|
||||
|
||||
len = Min(VARSIZE(arg1),VARSIZE(arg2));
|
||||
result = (char *) palloc(len);
|
||||
result = (bits8 *) palloc(len);
|
||||
VARSIZE(result) = len;
|
||||
VARBITLEN(result) = Min(VARBITLEN(arg1),VARBITLEN(arg2));
|
||||
|
||||
@ -708,11 +703,10 @@ bitxor (char * arg1, char * arg2)
|
||||
/* bitnot
|
||||
* perform a logical NOT on a bit strings.
|
||||
*/
|
||||
char *
|
||||
bitnot (char * arg)
|
||||
bits8 *
|
||||
bitnot (bits8 * arg)
|
||||
{
|
||||
int len;
|
||||
char *result;
|
||||
bits8 *result;
|
||||
bits8 *p,
|
||||
*r;
|
||||
bits8 mask;
|
||||
@ -720,7 +714,7 @@ bitnot (char * arg)
|
||||
if (!PointerIsValid(arg))
|
||||
return (bool) 0;
|
||||
|
||||
result = (char *) palloc(VARSIZE(arg));
|
||||
result = (bits8 *) palloc(VARSIZE(arg));
|
||||
VARSIZE(result) = VARSIZE(arg);
|
||||
VARBITLEN(result) = VARBITLEN(arg);
|
||||
|
||||
@ -739,11 +733,11 @@ bitnot (char * arg)
|
||||
/* bitshiftleft
|
||||
* do a left shift (i.e. to the beginning of the string) of the bit string
|
||||
*/
|
||||
char *
|
||||
bitshiftleft (char * arg, int shft)
|
||||
bits8 *
|
||||
bitshiftleft (bits8 * arg, int shft)
|
||||
{
|
||||
int byte_shift, ishift, len;
|
||||
char *result;
|
||||
bits8 *result;
|
||||
bits8 *p,
|
||||
*r;
|
||||
|
||||
@ -754,7 +748,7 @@ bitshiftleft (char * arg, int shft)
|
||||
if (shft < 0)
|
||||
return bitshiftright(arg, -shft);
|
||||
|
||||
result = (char *) palloc(VARSIZE(arg));
|
||||
result = (bits8 *) palloc(VARSIZE(arg));
|
||||
VARSIZE(result) = VARSIZE(arg);
|
||||
VARBITLEN(result) = VARBITLEN(arg);
|
||||
r = (bits8 *) VARBITS(result);
|
||||
@ -784,22 +778,22 @@ bitshiftleft (char * arg, int shft)
|
||||
/* bitshiftright
|
||||
* do a right shift (i.e. to the beginning of the string) of the bit string
|
||||
*/
|
||||
char *
|
||||
bitshiftright (char * arg, int shft)
|
||||
bits8 *
|
||||
bitshiftright (bits8 * arg, int shft)
|
||||
{
|
||||
int byte_shift, ishift, len;
|
||||
char *result;
|
||||
bits8 *result;
|
||||
bits8 *p,
|
||||
*r;
|
||||
|
||||
if (!PointerIsValid(arg))
|
||||
return (bool) 0;
|
||||
return (bits8 *) 0;
|
||||
|
||||
/* Negative shift is a shift to the left */
|
||||
if (shft < 0)
|
||||
return bitshiftleft(arg, -shft);
|
||||
|
||||
result = (char *) palloc(VARSIZE(arg));
|
||||
result = (bits8 *) palloc(VARSIZE(arg));
|
||||
VARSIZE(result) = VARSIZE(arg);
|
||||
VARBITLEN(result) = VARBITLEN(arg);
|
||||
r = (bits8 *) VARBITS(result);
|
||||
|
29
contrib/bit/varbit.demo.sql
Normal file
29
contrib/bit/varbit.demo.sql
Normal file
@ -0,0 +1,29 @@
|
||||
create table bit_example (a bits, b bits);
|
||||
copy bit_example from stdin;
|
||||
X0F X10
|
||||
X1F X11
|
||||
X2F X12
|
||||
X3F X13
|
||||
X8F X04
|
||||
X000F X0010
|
||||
X0123 XFFFF
|
||||
X2468 X2468
|
||||
XFA50 X05AF
|
||||
X12345 XFFF
|
||||
\.
|
||||
|
||||
select a,b,a||b as "a||b", bitsubstr(a,4,4) as "sub(a,4,4)",
|
||||
bitsubstr(b,2,4) as "sub(b,2,4)",
|
||||
bitsubstr(b,5,5) as "sub(b,5,5)"
|
||||
from bit_example;
|
||||
select a,b,~a as "~ a",~b as "~ b",a & b as "a & b",
|
||||
a|b as "a | b", a^b as "a ^ b" from bit_example;
|
||||
select a,b,a<b as "a<b",a<=b as "a<=b",a=b as "a=b",
|
||||
a>=b as "a>=b",a>b as "a>b",a<=>b as "a<=>b" from bit_example;
|
||||
select a,a<<4 as "a<<4",b,b>>2 as "b>>2" from bit_example;
|
||||
select a,b,a||b as "a||b", bitsubstr(a,4,4) as "sub(a,4,4)",
|
||||
bitsubstr(b,2,4) as "sub(b,2,4)",
|
||||
bitsubstr(b,5,5) as "sub(b,5,5)"
|
||||
from bit_example;
|
||||
|
||||
drop table bit_example;
|
36
contrib/bit/varbit.drop.sql
Normal file
36
contrib/bit/varbit.drop.sql
Normal file
@ -0,0 +1,36 @@
|
||||
DROP FUNCTION biteq(bits,bits);
|
||||
DROP OPERATOR = (bits,bits);
|
||||
DROP FUNCTION bitne(bits,bits);
|
||||
DROP OPERATOR <> (bits,bits);
|
||||
DROP FUNCTION bitlt(bits,bits);
|
||||
DROP OPERATOR < (bits,bits);
|
||||
DROP FUNCTION bitle(bits,bits);
|
||||
DROP OPERATOR <= (bits,bits);
|
||||
DROP FUNCTION bitgt(bits,bits);
|
||||
DROP OPERATOR > (bits,bits);
|
||||
DROP FUNCTION bitge(bits,bits);
|
||||
DROP OPERATOR >= (bits,bits);
|
||||
DROP FUNCTION bitcmp(bits,bits);
|
||||
DROP OPERATOR <=> (bits,bits);
|
||||
|
||||
DROP FUNCTION bitor(bits,bits);
|
||||
DROP OPERATOR | (bits,bits);
|
||||
DROP FUNCTION bitand(bits,bits);
|
||||
DROP OPERATOR & (bits,bits);
|
||||
DROP FUNCTION bitxor(bits,bits);
|
||||
DROP OPERATOR ^ (bits,bits);
|
||||
DROP FUNCTION bitnot(bits);
|
||||
DROP OPERATOR ~ (none,bits);
|
||||
|
||||
DROP FUNCTION bitshiftleft(bits,int4);
|
||||
DROP OPERATOR << (bits,int4);
|
||||
DROP FUNCTION bitshiftright(bits,int4);
|
||||
DROP OPERATOR >> (bits,int4);
|
||||
|
||||
DROP FUNCTION bitsubstr(bits,integer,integer);
|
||||
DROP OPERATOR || (bits,bits);
|
||||
DROP FUNCTION bitcat(bits,bits);
|
||||
|
||||
DROP FUNCTION varbit_in(opaque);
|
||||
DROP FUNCTION varbit_out(opaque);
|
||||
DROP TYPE bits;
|
@ -1,17 +1,21 @@
|
||||
#include "c.h"
|
||||
#include "postgres.h"
|
||||
|
||||
typedef bits8 *VarBit;
|
||||
typedef uint32 BitIndex;
|
||||
|
||||
#define HEXDIG(z) (z)<10 ? ((z)+'0') : ((z)-10+'A')
|
||||
|
||||
/* Modeled on struct varlena from postgres.h, bu data type is bits8 */
|
||||
struct varbita
|
||||
{
|
||||
int32 vl_len;
|
||||
bits8 vl_dat[1];
|
||||
};
|
||||
|
||||
#define BITSPERBYTE 8
|
||||
#define VARBITHDRSZ sizeof(int32)
|
||||
/* Number of bits in this bit string */
|
||||
#define VARBITLEN(PTR) (((struct varlena *)VARDATA(PTR))->vl_len)
|
||||
#define VARBITLEN(PTR) (((struct varbita *)VARDATA(PTR))->vl_len)
|
||||
/* Pointer tp the first byte containing bit string data */
|
||||
#define VARBITS(PTR) (((struct varlena *)VARDATA(PTR))->vl_dat)
|
||||
#define VARBITS(PTR) (((struct varbita *)VARDATA(PTR))->vl_dat)
|
||||
/* Number of bytes in the data section of a bit string */
|
||||
#define VARBITBYTES(PTR) (VARSIZE(PTR) - VARHDRSZ - VARBITHDRSZ)
|
||||
/* Padding of the bit string at the end */
|
||||
@ -27,22 +31,22 @@ typedef uint32 BitIndex;
|
||||
#define BITHIGH 0x80
|
||||
|
||||
|
||||
char * zpbitin(char *s, int dummy, int32 atttypmod);
|
||||
char * zpbitout(char *s);
|
||||
char * zpbitsout(char *s);
|
||||
char * varbitin(char *s, int dummy, int32 atttypmod);
|
||||
bool biteq (char *arg1, char *arg2);
|
||||
bool bitne (char *arg1, char *arg2);
|
||||
bool bitge (char *arg1, char *arg2);
|
||||
bool bitgt (char *arg1, char *arg2);
|
||||
bool bitle (char *arg1, char *arg2);
|
||||
bool bitlt (char *arg1, char *arg2);
|
||||
int bitcmp (char *arg1, char *arg2);
|
||||
char * bitand (char * arg1, char * arg2);
|
||||
char * bitor (char * arg1, char * arg2);
|
||||
char * bitxor (char * arg1, char * arg2);
|
||||
char * bitnot (char * arg);
|
||||
char * bitshiftright (char * arg, int shft);
|
||||
char * bitshiftleft (char * arg, int shft);
|
||||
char * bitcat (char *arg1, char *arg2);
|
||||
char * bitsubstr (char *arg, int32 s, int32 l);
|
||||
bits8 * zpbitin(char *s, int dummy, int32 atttypmod);
|
||||
char * zpbitout(bits8 *s);
|
||||
char * zpbitsout(bits8 *s);
|
||||
bits8 * varbitin(char *s, int dummy, int32 atttypmod);
|
||||
bool biteq (bits8 *arg1, bits8 *arg2);
|
||||
bool bitne (bits8 *arg1, bits8 *arg2);
|
||||
bool bitge (bits8 *arg1, bits8 *arg2);
|
||||
bool bitgt (bits8 *arg1, bits8 *arg2);
|
||||
bool bitle (bits8 *arg1, bits8 *arg2);
|
||||
bool bitlt (bits8 *arg1, bits8 *arg2);
|
||||
int bitcmp (bits8 *arg1, bits8 *arg2);
|
||||
bits8 * bitand (bits8 * arg1, bits8 * arg2);
|
||||
bits8 * bitor (bits8 * arg1, bits8 * arg2);
|
||||
bits8 * bitxor (bits8 * arg1, bits8 * arg2);
|
||||
bits8 * bitnot (bits8 * arg);
|
||||
bits8 * bitshiftright (bits8 * arg, int shft);
|
||||
bits8 * bitshiftleft (bits8 * arg, int shft);
|
||||
bits8 * bitcat (bits8 *arg1, bits8 *arg2);
|
||||
bits8 * bitsubstr (bits8 *arg, int32 s, int32 l);
|
||||
|
171
contrib/bit/varbit.source
Normal file
171
contrib/bit/varbit.source
Normal file
@ -0,0 +1,171 @@
|
||||
LOAD '_OBJWD_/varbit.so';
|
||||
|
||||
CREATE FUNCTION varbit_in(opaque)
|
||||
RETURNS bit
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'c';
|
||||
|
||||
CREATE FUNCTION varbit_out(opaque)
|
||||
RETURNS opaque
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'c';
|
||||
|
||||
CREATE TYPE bits (
|
||||
internallength = -1,
|
||||
input = varbit_in,
|
||||
output = varbit_out
|
||||
);
|
||||
|
||||
CREATE FUNCTION bitcat(bits,bits) RETURNS bits
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'C';
|
||||
|
||||
CREATE OPERATOR || (
|
||||
leftarg = bits,
|
||||
rightarg = bits,
|
||||
procedure = bitcat
|
||||
);
|
||||
|
||||
CREATE FUNCTION bitsubstr(bits,integer,integer) RETURNS bits
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'C';
|
||||
|
||||
CREATE FUNCTION biteq(bits,bits) RETURNS bool
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'C';
|
||||
|
||||
CREATE OPERATOR = (
|
||||
leftarg = bits,
|
||||
rightarg = bits,
|
||||
procedure = biteq,
|
||||
negator = <>,
|
||||
commutator = =
|
||||
);
|
||||
|
||||
CREATE FUNCTION bitne(bits,bits) RETURNS bool
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'C';
|
||||
|
||||
CREATE OPERATOR <> (
|
||||
leftarg = bits,
|
||||
rightarg = bits,
|
||||
procedure = bitne,
|
||||
negator = =,
|
||||
commutator = <>
|
||||
);
|
||||
|
||||
CREATE FUNCTION bitlt(bits,bits) RETURNS bool
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'C';
|
||||
|
||||
CREATE OPERATOR < (
|
||||
leftarg = bits,
|
||||
rightarg = bits,
|
||||
procedure = bitlt
|
||||
);
|
||||
|
||||
CREATE FUNCTION bitle(bits,bits) RETURNS bool
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'C';
|
||||
|
||||
CREATE OPERATOR <= (
|
||||
leftarg = bits,
|
||||
rightarg = bits,
|
||||
procedure = bitle
|
||||
);
|
||||
|
||||
CREATE FUNCTION bitgt(bits,bits) RETURNS bool
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'C';
|
||||
|
||||
CREATE OPERATOR > (
|
||||
leftarg = bits,
|
||||
rightarg = bits,
|
||||
procedure = bitgt,
|
||||
negator = <=,
|
||||
commutator = <
|
||||
);
|
||||
|
||||
CREATE FUNCTION bitge(bits,bits) RETURNS bool
|
||||
as '_OBJWD_/varbit.so'
|
||||
language 'C';
|
||||
|
||||
CREATE OPERATOR >= (
|
||||
leftarg = bits,
|
||||
rightarg = bits,
|
||||
procedure = bitge,
|
||||
negator = <,
|
||||
commutator = <=
|
||||
);
|
||||
|
||||
CREATE FUNCTION bitcmp(bits,bits) RETURNS int4
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'C';
|
||||
|
||||
CREATE OPERATOR <=> (
|
||||
leftarg = bits,
|
||||
rightarg = bits,
|
||||
procedure = bitcmp
|
||||
);
|
||||
|
||||
CREATE FUNCTION bitor(bits,bits) RETURNS bits
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'C';
|
||||
|
||||
CREATE OPERATOR | (
|
||||
leftarg = bits,
|
||||
rightarg = bits,
|
||||
procedure = bitor,
|
||||
commutator = |
|
||||
);
|
||||
|
||||
CREATE FUNCTION bitand(bits,bits) RETURNS bits
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'C';
|
||||
|
||||
CREATE OPERATOR & (
|
||||
leftarg = bits,
|
||||
rightarg = bits,
|
||||
procedure = bitand,
|
||||
commutator = &
|
||||
);
|
||||
|
||||
|
||||
CREATE FUNCTION bitxor(bits,bits) RETURNS bits
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'C';
|
||||
|
||||
CREATE OPERATOR ^ (
|
||||
leftarg = bits,
|
||||
rightarg = bits,
|
||||
procedure = bitxor
|
||||
);
|
||||
|
||||
CREATE FUNCTION bitnot(bits) RETURNS bits
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'C';
|
||||
|
||||
CREATE OPERATOR ~ (
|
||||
rightarg = bits,
|
||||
procedure = bitnot
|
||||
);
|
||||
|
||||
CREATE FUNCTION bitshiftleft(bits,int4) RETURNS bits
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'C';
|
||||
|
||||
CREATE OPERATOR << (
|
||||
leftarg = bits,
|
||||
rightarg = int4,
|
||||
procedure = bitshiftleft
|
||||
);
|
||||
|
||||
CREATE FUNCTION bitshiftright(bits,int4) RETURNS bits
|
||||
AS '_OBJWD_/varbit.so'
|
||||
LANGUAGE 'C';
|
||||
|
||||
CREATE OPERATOR >> (
|
||||
leftarg = bits,
|
||||
rightarg = int4,
|
||||
procedure = bitshiftright
|
||||
);
|
22
contrib/bit/varbit_glue.c
Normal file
22
contrib/bit/varbit_glue.c
Normal file
@ -0,0 +1,22 @@
|
||||
/* Glue file to use varbit before it is properly integrated with postgres */
|
||||
|
||||
#include "varbit.h"
|
||||
|
||||
bits8 * varbit_in (char * s);
|
||||
char * varbit_out (bits8 *s);
|
||||
|
||||
bits8 *
|
||||
varbit_in (char * s) {
|
||||
return varbitin (s, 0, -1);
|
||||
}
|
||||
|
||||
/*char *
|
||||
varbit_out (bits8 *s) {
|
||||
return zpbitout(s);
|
||||
}
|
||||
*/
|
||||
|
||||
char *
|
||||
varbit_out (bits8 *s) {
|
||||
return zpbitsout(s);
|
||||
}
|
@ -2,6 +2,8 @@
|
||||
#include "varbit.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void print_details (unsigned char *s);
|
||||
|
||||
const int numb = 8;
|
||||
/*
|
||||
const char *b[] = { "B0010", "B11011011", "B0001", "X3F12", "X27", "B",
|
||||
@ -23,13 +25,13 @@ void print_details (unsigned char *s)
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void
|
||||
int
|
||||
main ()
|
||||
{
|
||||
int i, j;
|
||||
char *s[numb];
|
||||
|
||||
for (i=0; i<numb; i++) {
|
||||
for (i=0; i<numb; i++) {
|
||||
printf ("Input: %s\n",b[i]);
|
||||
s[i] = zpbitin(b[i], 0, atttypmod[i]);
|
||||
//print_details(s[i]);
|
||||
@ -53,13 +55,13 @@ main ()
|
||||
zpbitsout(bitsubstr(s[3],1,8)));
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),9,8,
|
||||
zpbitsout(bitsubstr(s[3],9,8)));
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),1,9,
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),1,9,
|
||||
zpbitsout(bitsubstr(s[3],1,9)));
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),3,5,
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),3,5,
|
||||
zpbitsout(bitsubstr(s[3],3,5)));
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),3,9,
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),3,9,
|
||||
zpbitsout(bitsubstr(s[3],3,9)));
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),3,17,
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),3,17,
|
||||
zpbitsout(bitsubstr(s[3],3,17)));
|
||||
printf ("\nLOGICAL AND:\n");
|
||||
for (i=0; i<numb; i++)
|
||||
@ -124,14 +126,14 @@ main ()
|
||||
zpbitsout(bitsubstr(s[3],1,8)));
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),9,8,
|
||||
zpbitsout(bitsubstr(s[3],9,8)));
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),1,9,
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),1,9,
|
||||
zpbitsout(bitsubstr(s[3],1,9)));
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),3,5,
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),3,5,
|
||||
zpbitsout(bitsubstr(s[3],3,5)));
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),3,9,
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),3,9,
|
||||
zpbitsout(bitsubstr(s[3],3,9)));
|
||||
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),3,17,
|
||||
zpbitsout(bitsubstr(s[3],3,17)));
|
||||
printf("%s (%d,%d) => %s (%s)\n",zpbitsout(s[3]),3,17,
|
||||
zpbitsout(bitsubstr(s[3],3,17)),zpbitsout(bitsubstr(s[3],3,17)));
|
||||
printf ("\nLOGICAL AND:\n");
|
||||
for (i=0; i<numb; i++)
|
||||
for (j=i+1; j<numb; j++)
|
||||
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user