Commit 51cfdae5 authored by Bruce Momjian's avatar Bruce Momjian

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
parent 5454b379
CFLAGS = -g ifndef PGDIR
PGDIR= ../..
PGDIR=/data/build/postgresql-7.0beta3
endif
varbit: vartest.o varbit.o SRCDIR= $(PGDIR)/src
$(CC) $(CFLAGS) -o $@ $^
varbit.o: varbit.c varbit.h include $(SRCDIR)/Makefile.global
vartest.o: vartest.c varbit.h
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 ...@@ -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 is quite possible that there may be problems on machines that don't
have 8 bits/byte (are there still any around?). 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 The SQL standard only defines comparison, SUBSTR and concatenation
operators, and these have been implemented. In addition all logical operators, and these have been implemented. In addition all logical
operators have been implemented, i.e. ~,|,&,^,<< and >>. This is operators have been implemented, i.e. ~,|,&,^,<< and >>. This is
......
...@@ -4,18 +4,14 @@ ...@@ -4,18 +4,14 @@
* Functions for the built-in type bit() and varying bit(). * Functions for the built-in type bit() and varying bit().
* *
* IDENTIFICATION * 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 "varbit.h"
/*
#include "access/htup.h" #include "access/htup.h"
#include "catalog/pg_type.h" /*#include "catalog/pg_type.h" */
#include "utils/builtins.h" /*#include "utils/builtins.h" */
*/
/* /*
...@@ -43,22 +39,22 @@ ...@@ -43,22 +39,22 @@
* (XXX dummy is here because we pass typelem as the second argument * (XXX dummy is here because we pass typelem as the second argument
* for array_in. copied this, no idea what it means??) * for array_in. copied this, no idea what it means??)
*/ */
char * bits8 *
zpbitin(char *s, int dummy, int32 atttypmod) zpbitin(char *s, int dummy, int32 atttypmod)
{ {
char *result, bits8 *result; /* the bits string that was read in */
*sp; /* pointer into the character string */ char *sp; /* pointer into the character string */
bits8 *r; bits8 *r;
int len, /* Length of the whole data structure */ int len, /* Length of the whole data structure */
bitlen, /* Number of bits in the bit string */ bitlen, /* Number of bits in the bit string */
slen; /* Length of the input string */ slen; /* Length of the input string */
int bit_not_hex; /* 0 = hex string 1=bit string */ int bit_not_hex = 0; /* 0 = hex string 1=bit string */
int i, bc, ipad; int bc, ipad;
bits8 x, y; bits8 x = 0;
if (s == NULL) if (s == NULL)
return NULL; return (bits8 *) NULL;
/* Check that the first character is a b or an x */ /* Check that the first character is a b or an x */
if (s[0]=='b' || s[0]=='B') if (s[0]=='b' || s[0]=='B')
...@@ -82,7 +78,8 @@ zpbitin(char *s, int dummy, int32 atttypmod) ...@@ -82,7 +78,8 @@ zpbitin(char *s, int dummy, int32 atttypmod)
if (atttypmod == -1) if (atttypmod == -1)
atttypmod = bitlen; atttypmod = bitlen;
else 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)", elog(ERROR, "zpbitin: bit string of size %d cannot be written into bits(%d)",
bitlen,atttypmod); bitlen,atttypmod);
...@@ -90,10 +87,10 @@ zpbitin(char *s, int dummy, int32 atttypmod) ...@@ -90,10 +87,10 @@ zpbitin(char *s, int dummy, int32 atttypmod)
len = VARBITDATALEN(atttypmod); len = VARBITDATALEN(atttypmod);
if (len > MaxAttrSize) 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); (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 */ /* set to 0 so that *r is always initialised and strin is zero-padded */
memset(result, 0, len); memset(result, 0, len);
VARSIZE(result) = len; VARSIZE(result) = len;
...@@ -103,7 +100,7 @@ zpbitin(char *s, int dummy, int32 atttypmod) ...@@ -103,7 +100,7 @@ zpbitin(char *s, int dummy, int32 atttypmod)
significant byte first. s points to the byte before the beginning significant byte first. s points to the byte before the beginning
of the bitstring */ of the bitstring */
sp = s+1; sp = s+1;
r = (bits8 *) VARBITS(result); r = VARBITS(result);
if (bit_not_hex) if (bit_not_hex)
{ {
/* Parse the bit representation of the string */ /* Parse the bit representation of the string */
...@@ -166,10 +163,10 @@ zpbitin(char *s, int dummy, int32 atttypmod) ...@@ -166,10 +163,10 @@ zpbitin(char *s, int dummy, int32 atttypmod)
* for long strings * for long strings
*/ */
char * char *
zpbitout(char *s) zpbitout(bits8 *s)
{ {
char *result, *r; char *result, *r;
VarBit sp; bits8 *sp;
int i, len, bitlen; int i, len, bitlen;
if (s == NULL) if (s == NULL)
...@@ -183,7 +180,7 @@ zpbitout(char *s) ...@@ -183,7 +180,7 @@ zpbitout(char *s)
bitlen = VARBITLEN(s); bitlen = VARBITLEN(s);
len = bitlen/4 + (bitlen%4>0 ? 1 : 0); len = bitlen/4 + (bitlen%4>0 ? 1 : 0);
result = (char *) palloc(len + 4); result = (char *) palloc(len + 4);
sp = (bits8 *) VARBITS(s); sp = VARBITS(s);
r = result; r = result;
*r++ = 'X'; *r++ = 'X';
*r++ = '\''; *r++ = '\'';
...@@ -206,10 +203,10 @@ zpbitout(char *s) ...@@ -206,10 +203,10 @@ zpbitout(char *s)
* Prints the string a bits * Prints the string a bits
*/ */
char * char *
zpbitsout(char *s) zpbitsout(bits8 *s)
{ {
char *result, *r; char *result, *r;
VarBit sp; bits8 *sp;
bits8 x; bits8 x;
int i, k, len; int i, k, len;
...@@ -223,7 +220,7 @@ zpbitsout(char *s) ...@@ -223,7 +220,7 @@ zpbitsout(char *s)
{ {
len = VARBITLEN(s); len = VARBITLEN(s);
result = (char *) palloc(len + 4); result = (char *) palloc(len + 4);
sp = (bits8 *) VARBITS(s); sp = VARBITS(s);
r = result; r = result;
*r++ = 'B'; *r++ = 'B';
*r++ = '\''; *r++ = '\'';
...@@ -252,22 +249,22 @@ zpbitsout(char *s) ...@@ -252,22 +249,22 @@ zpbitsout(char *s)
* varbitin - * varbitin -
* converts a string to the internal representation of a bitstring. * converts a string to the internal representation of a bitstring.
*/ */
char * bits8 *
varbitin(char *s, int dummy, int32 atttypmod) varbitin(char *s, int dummy, int32 atttypmod)
{ {
char *result, bits8 *result; /* The resulting bit string */
*sp; /* pointer into the character string */ char *sp; /* pointer into the character string */
bits8 *r; bits8 *r;
int len, /* Length of the whole data structure */ int len, /* Length of the whole data structure */
bitlen, /* Number of bits in the bit string */ bitlen, /* Number of bits in the bit string */
slen; /* Length of the input string */ slen; /* Length of the input string */
int bit_not_hex; int bit_not_hex = 0;
int i, bc, ipad; int bc, ipad;
bits8 x, y; bits8 x = 0;
if (s == NULL) if (s == NULL)
return NULL; return (bits8 *) NULL;
/* Check that the first character is a b or an x */ /* Check that the first character is a b or an x */
if (s[0]=='b' || s[0]=='B') if (s[0]=='b' || s[0]=='B')
...@@ -289,7 +286,8 @@ varbitin(char *s, int dummy, int32 atttypmod) ...@@ -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 reading a hex string and not by more than 3 bits, as a hex string gives
and accurate length upto 4 bits */ and accurate length upto 4 bits */
if (atttypmod > -1) 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)", elog(ERROR, "varbitin: bit string of size %d cannot be written into varying bits(%d)",
bitlen,atttypmod); bitlen,atttypmod);
...@@ -297,10 +295,10 @@ varbitin(char *s, int dummy, int32 atttypmod) ...@@ -297,10 +295,10 @@ varbitin(char *s, int dummy, int32 atttypmod)
len = VARBITDATALEN(bitlen); len = VARBITDATALEN(bitlen);
if (len > MaxAttrSize) 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); (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 */ /* set to 0 so that *r is always initialised and strin is zero-padded */
memset(result, 0, len); memset(result, 0, len);
VARSIZE(result) = len; VARSIZE(result) = len;
...@@ -310,7 +308,7 @@ varbitin(char *s, int dummy, int32 atttypmod) ...@@ -310,7 +308,7 @@ varbitin(char *s, int dummy, int32 atttypmod)
significant byte first. s points to the byte before the beginning significant byte first. s points to the byte before the beginning
of the bitstring */ of the bitstring */
sp = s + 1; sp = s + 1;
r = (VarBit) VARBITS(result); r = VARBITS(result);
if (bit_not_hex) if (bit_not_hex)
{ {
/* Parse the bit representation of the string */ /* Parse the bit representation of the string */
...@@ -383,11 +381,10 @@ varbitin(char *s, int dummy, int32 atttypmod) ...@@ -383,11 +381,10 @@ varbitin(char *s, int dummy, int32 atttypmod)
*/ */
bool bool
biteq (char *arg1, char *arg2) biteq (bits8 *arg1, bits8 *arg2)
{ {
int bitlen1, int bitlen1,
bitlen2; bitlen2;
bits8 *p1, *p2;
if (!PointerIsValid(arg1) || !PointerIsValid(arg2)) if (!PointerIsValid(arg1) || !PointerIsValid(arg2))
return (bool) 0; return (bool) 0;
...@@ -402,11 +399,10 @@ biteq (char *arg1, char *arg2) ...@@ -402,11 +399,10 @@ biteq (char *arg1, char *arg2)
} }
bool bool
bitne (char *arg1, char *arg2) bitne (bits8 *arg1, bits8 *arg2)
{ {
int bitlen1, int bitlen1,
bitlen2; bitlen2;
bits8 *p1, *p2;
if (!PointerIsValid(arg1) || !PointerIsValid(arg2)) if (!PointerIsValid(arg1) || !PointerIsValid(arg2))
return (bool) 0; return (bool) 0;
...@@ -429,11 +425,10 @@ bitne (char *arg1, char *arg2) ...@@ -429,11 +425,10 @@ bitne (char *arg1, char *arg2)
* Anything is equal to undefined. * Anything is equal to undefined.
*/ */
int int
bitcmp (char *arg1, char *arg2) bitcmp (bits8 *arg1, bits8 *arg2)
{ {
int bitlen1, bytelen1, int bitlen1, bytelen1,
bitlen2, bytelen2; bitlen2, bytelen2;
bits8 *p1, *p2;
int cmp; int cmp;
if (!PointerIsValid(arg1) || !PointerIsValid(arg2)) if (!PointerIsValid(arg1) || !PointerIsValid(arg2))
...@@ -452,25 +447,25 @@ bitcmp (char *arg1, char *arg2) ...@@ -452,25 +447,25 @@ bitcmp (char *arg1, char *arg2)
} }
bool bool
bitlt (char *arg1, char *arg2) bitlt (bits8 *arg1, bits8 *arg2)
{ {
return (bool) (bitcmp(arg1,arg2) == -1); return (bool) (bitcmp(arg1,arg2) == -1);
} }
bool bool
bitle (char *arg1, char *arg2) bitle (bits8 *arg1, bits8 *arg2)
{ {
return (bool) (bitcmp(arg1,arg2) <= 0); return (bool) (bitcmp(arg1,arg2) <= 0);
} }
bool bool
bitge (char *arg1, char *arg2) bitge (bits8 *arg1, bits8 *arg2)
{ {
return (bool) (bitcmp(arg1,arg2) >= 0); return (bool) (bitcmp(arg1,arg2) >= 0);
} }
bool bool
bitgt (char *arg1, char *arg2) bitgt (bits8 *arg1, bits8 *arg2)
{ {
return (bool) (bitcmp(arg1,arg2) == 1); return (bool) (bitcmp(arg1,arg2) == 1);
} }
...@@ -478,11 +473,11 @@ bitgt (char *arg1, char *arg2) ...@@ -478,11 +473,11 @@ bitgt (char *arg1, char *arg2)
/* bitcat /* bitcat
* Concatenation of bit strings * Concatenation of bit strings
*/ */
char * bits8 *
bitcat (char *arg1, char *arg2) bitcat (bits8 *arg1, bits8 *arg2)
{ {
int bitlen1, bitlen2, bytelen, bit1pad, bit2shift; int bitlen1, bitlen2, bytelen, bit1pad, bit2shift;
char *result; bits8 *result;
bits8 *pr, *pa; bits8 *pr, *pa;
if (!PointerIsValid(arg1) || !PointerIsValid(arg2)) if (!PointerIsValid(arg1) || !PointerIsValid(arg2))
...@@ -493,7 +488,7 @@ bitcat (char *arg1, char *arg2) ...@@ -493,7 +488,7 @@ bitcat (char *arg1, char *arg2)
bytelen = VARBITDATALEN(bitlen1+bitlen2); bytelen = VARBITDATALEN(bitlen1+bitlen2);
result = (char *) palloc(bytelen*sizeof(bits8)); result = (bits8 *) palloc(bytelen*sizeof(bits8));
VARSIZE(result) = bytelen; VARSIZE(result) = bytelen;
VARBITLEN(result) = bitlen1+bitlen2; VARBITLEN(result) = bitlen1+bitlen2;
printf("%d %d %d \n",VARBITBYTES(arg1),VARBITLEN(arg1),VARBITPAD(arg1)); printf("%d %d %d \n",VARBITBYTES(arg1),VARBITLEN(arg1),VARBITPAD(arg1));
...@@ -510,10 +505,10 @@ bitcat (char *arg1, char *arg2) ...@@ -510,10 +505,10 @@ bitcat (char *arg1, char *arg2)
{ {
/* We need to shift all the results to fit */ /* We need to shift all the results to fit */
bit2shift = BITSPERBYTE - bit1pad; bit2shift = BITSPERBYTE - bit1pad;
pa = (VarBit) VARBITS(arg2); pa = VARBITS(arg2);
pr = (VarBit) VARBITS(result)+VARBITBYTES(arg1)-1; pr = VARBITS(result)+VARBITBYTES(arg1)-1;
for ( ; pa < VARBITEND(arg2); pa++) { for ( ; pa < VARBITEND(arg2); pa++) {
*pr = *pr | ((*pa >> bit2shift) & BITMASK); *pr |= ((*pa >> bit2shift) & BITMASK);
pr++; pr++;
if (pr < VARBITEND(result)) if (pr < VARBITEND(result))
*pr = (*pa << bit1pad) & BITMASK; *pr = (*pa << bit1pad) & BITMASK;
...@@ -528,17 +523,17 @@ bitcat (char *arg1, char *arg2) ...@@ -528,17 +523,17 @@ bitcat (char *arg1, char *arg2)
* Note, s is 1-based. * Note, s is 1-based.
* SQL draft 6.10 9) * SQL draft 6.10 9)
*/ */
char * bits8 *
bitsubstr (char *arg, int32 s, int32 l) bitsubstr (bits8 *arg, int32 s, int32 l)
{ {
int bitlen, int bitlen,
rbitlen, rbitlen,
len, len,
ipad, ipad = 0,
ishift, ishift,
i; i;
int e, s1, e1; int e, s1, e1;
char * result; bits8 * result;
bits8 mask, *r, *ps; bits8 mask, *r, *ps;
if (!PointerIsValid(arg)) if (!PointerIsValid(arg))
...@@ -552,7 +547,7 @@ bitsubstr (char *arg, int32 s, int32 l) ...@@ -552,7 +547,7 @@ bitsubstr (char *arg, int32 s, int32 l)
{ {
/* Need to return a null string */ /* Need to return a null string */
len = VARBITDATALEN(0); len = VARBITDATALEN(0);
result = (char *) palloc(len); result = (bits8 *) palloc(len);
VARBITLEN(result) = 0; VARBITLEN(result) = 0;
VARSIZE(result) = len; VARSIZE(result) = len;
} }
...@@ -562,22 +557,22 @@ bitsubstr (char *arg, int32 s, int32 l) ...@@ -562,22 +557,22 @@ bitsubstr (char *arg, int32 s, int32 l)
ending at position e1-1 */ ending at position e1-1 */
rbitlen = e1-s1; rbitlen = e1-s1;
len = VARBITDATALEN(rbitlen); len = VARBITDATALEN(rbitlen);
result = (char *) palloc(len); result = (bits8 *) palloc(len);
VARBITLEN(result) = rbitlen; VARBITLEN(result) = rbitlen;
VARSIZE(result) = len; VARSIZE(result) = len;
len -= VARHDRSZ + VARBITHDRSZ;
/* Are we copying from a byte boundary? */ /* Are we copying from a byte boundary? */
if ((s1-1)%BITSPERBYTE==0) if ((s1-1)%BITSPERBYTE==0)
{ {
/* Yep, we are copying bytes */ /* Yep, we are copying bytes */
len -= VARHDRSZ + VARBITHDRSZ;
memcpy(VARBITS(result),VARBITS(arg)+(s1-1)/BITSPERBYTE,len); memcpy(VARBITS(result),VARBITS(arg)+(s1-1)/BITSPERBYTE,len);
} }
else else
{ {
/* Figure out how much we need to shift the sequence by */ /* Figure out how much we need to shift the sequence by */
ishift = (s1-1)%BITSPERBYTE; ishift = (s1-1)%BITSPERBYTE;
r = (VarBit) VARBITS(result); r = VARBITS(result);
ps = (VarBit) VARBITS(arg) + (s1-1)/BITSPERBYTE; ps = VARBITS(arg) + (s1-1)/BITSPERBYTE;
for (i=0; i<len; i++) for (i=0; i<len; i++)
{ {
*r = (*ps <<ishift) & BITMASK; *r = (*ps <<ishift) & BITMASK;
...@@ -602,12 +597,12 @@ bitsubstr (char *arg, int32 s, int32 l) ...@@ -602,12 +597,12 @@ bitsubstr (char *arg, int32 s, int32 l)
* perform a logical AND on two bit strings. The result is automatically * perform a logical AND on two bit strings. The result is automatically
* truncated to the shorter bit string * truncated to the shorter bit string
*/ */
char * bits8 *
bitand (char * arg1, char * arg2) bitand (bits8 * arg1, bits8 * arg2)
{ {
int len, int len,
i; i;
char *result; bits8 *result;
bits8 *p1, bits8 *p1,
*p2, *p2,
*r; *r;
...@@ -616,7 +611,7 @@ bitand (char * arg1, char * arg2) ...@@ -616,7 +611,7 @@ bitand (char * arg1, char * arg2)
return (bool) 0; return (bool) 0;
len = Min(VARSIZE(arg1),VARSIZE(arg2)); len = Min(VARSIZE(arg1),VARSIZE(arg2));
result = (char *) palloc(len); result = (bits8 *) palloc(len);
VARSIZE(result) = len; VARSIZE(result) = len;
VARBITLEN(result) = Min(VARBITLEN(arg1),VARBITLEN(arg2)); VARBITLEN(result) = Min(VARBITLEN(arg1),VARBITLEN(arg2));
...@@ -635,12 +630,12 @@ bitand (char * arg1, char * arg2) ...@@ -635,12 +630,12 @@ bitand (char * arg1, char * arg2)
* perform a logical OR on two bit strings. The result is automatically * perform a logical OR on two bit strings. The result is automatically
* truncated to the shorter bit string. * truncated to the shorter bit string.
*/ */
char * bits8 *
bitor (char * arg1, char * arg2) bitor (bits8 * arg1, bits8 * arg2)
{ {
int len, int len,
i; i;
char *result; bits8 *result;
bits8 *p1, bits8 *p1,
*p2, *p2,
*r; *r;
...@@ -650,7 +645,7 @@ bitor (char * arg1, char * arg2) ...@@ -650,7 +645,7 @@ bitor (char * arg1, char * arg2)
return (bool) 0; return (bool) 0;
len = Min(VARSIZE(arg1),VARSIZE(arg2)); len = Min(VARSIZE(arg1),VARSIZE(arg2));
result = (char *) palloc(len); result = (bits8 *) palloc(len);
VARSIZE(result) = len; VARSIZE(result) = len;
VARBITLEN(result) = Min(VARBITLEN(arg1),VARBITLEN(arg2)); VARBITLEN(result) = Min(VARBITLEN(arg1),VARBITLEN(arg2));
...@@ -671,12 +666,12 @@ bitor (char * arg1, char * arg2) ...@@ -671,12 +666,12 @@ bitor (char * arg1, char * arg2)
* perform a logical XOR on two bit strings. The result is automatically * perform a logical XOR on two bit strings. The result is automatically
* truncated to the shorter bit string. * truncated to the shorter bit string.
*/ */
char * bits8 *
bitxor (char * arg1, char * arg2) bitxor (bits8 * arg1, bits8 * arg2)
{ {
int len, int len,
i; i;
char *result; bits8 *result;
bits8 *p1, bits8 *p1,
*p2, *p2,
*r; *r;
...@@ -686,7 +681,7 @@ bitxor (char * arg1, char * arg2) ...@@ -686,7 +681,7 @@ bitxor (char * arg1, char * arg2)
return (bool) 0; return (bool) 0;
len = Min(VARSIZE(arg1),VARSIZE(arg2)); len = Min(VARSIZE(arg1),VARSIZE(arg2));
result = (char *) palloc(len); result = (bits8 *) palloc(len);
VARSIZE(result) = len; VARSIZE(result) = len;
VARBITLEN(result) = Min(VARBITLEN(arg1),VARBITLEN(arg2)); VARBITLEN(result) = Min(VARBITLEN(arg1),VARBITLEN(arg2));
...@@ -708,11 +703,10 @@ bitxor (char * arg1, char * arg2) ...@@ -708,11 +703,10 @@ bitxor (char * arg1, char * arg2)
/* bitnot /* bitnot
* perform a logical NOT on a bit strings. * perform a logical NOT on a bit strings.
*/ */
char * bits8 *
bitnot (char * arg) bitnot (bits8 * arg)
{ {
int len; bits8 *result;
char *result;
bits8 *p, bits8 *p,
*r; *r;
bits8 mask; bits8 mask;
...@@ -720,7 +714,7 @@ bitnot (char * arg) ...@@ -720,7 +714,7 @@ bitnot (char * arg)
if (!PointerIsValid(arg)) if (!PointerIsValid(arg))
return (bool) 0; return (bool) 0;
result = (char *) palloc(VARSIZE(arg)); result = (bits8 *) palloc(VARSIZE(arg));
VARSIZE(result) = VARSIZE(arg); VARSIZE(result) = VARSIZE(arg);
VARBITLEN(result) = VARBITLEN(arg); VARBITLEN(result) = VARBITLEN(arg);
...@@ -739,11 +733,11 @@ bitnot (char * arg) ...@@ -739,11 +733,11 @@ bitnot (char * arg)
/* bitshiftleft /* bitshiftleft
* do a left shift (i.e. to the beginning of the string) of the bit string * do a left shift (i.e. to the beginning of the string) of the bit string
*/ */
char * bits8 *
bitshiftleft (char * arg, int shft) bitshiftleft (bits8 * arg, int shft)
{ {
int byte_shift, ishift, len; int byte_shift, ishift, len;
char *result; bits8 *result;
bits8 *p, bits8 *p,
*r; *r;
...@@ -754,7 +748,7 @@ bitshiftleft (char * arg, int shft) ...@@ -754,7 +748,7 @@ bitshiftleft (char * arg, int shft)
if (shft < 0) if (shft < 0)
return bitshiftright(arg, -shft); return bitshiftright(arg, -shft);
result = (char *) palloc(VARSIZE(arg)); result = (bits8 *) palloc(VARSIZE(arg));
VARSIZE(result) = VARSIZE(arg); VARSIZE(result) = VARSIZE(arg);
VARBITLEN(result) = VARBITLEN(arg); VARBITLEN(result) = VARBITLEN(arg);
r = (bits8 *) VARBITS(result); r = (bits8 *) VARBITS(result);
...@@ -784,22 +778,22 @@ bitshiftleft (char * arg, int shft) ...@@ -784,22 +778,22 @@ bitshiftleft (char * arg, int shft)
/* bitshiftright /* bitshiftright
* do a right shift (i.e. to the beginning of the string) of the bit string * do a right shift (i.e. to the beginning of the string) of the bit string
*/ */
char * bits8 *
bitshiftright (char * arg, int shft) bitshiftright (bits8 * arg, int shft)
{ {
int byte_shift, ishift, len; int byte_shift, ishift, len;
char *result; bits8 *result;
bits8 *p, bits8 *p,
*r; *r;
if (!PointerIsValid(arg)) if (!PointerIsValid(arg))
return (bool) 0; return (bits8 *) 0;
/* Negative shift is a shift to the left */ /* Negative shift is a shift to the left */
if (shft < 0) if (shft < 0)
return bitshiftleft(arg, -shft); return bitshiftleft(arg, -shft);
result = (char *) palloc(VARSIZE(arg)); result = (bits8 *) palloc(VARSIZE(arg));
VARSIZE(result) = VARSIZE(arg); VARSIZE(result) = VARSIZE(arg);
VARBITLEN(result) = VARBITLEN(arg); VARBITLEN(result) = VARBITLEN(arg);
r = (bits8 *) VARBITS(result); r = (bits8 *) VARBITS(result);
......
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;
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;
#include "c.h"
#include "postgres.h" #include "postgres.h"
typedef bits8 *VarBit;
typedef uint32 BitIndex;
#define HEXDIG(z) (z)<10 ? ((z)+'0') : ((z)-10+'A') #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 BITSPERBYTE 8
#define VARBITHDRSZ sizeof(int32) #define VARBITHDRSZ sizeof(int32)
/* Number of bits in this bit string */ /* 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 */ /* 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 */ /* Number of bytes in the data section of a bit string */
#define VARBITBYTES(PTR) (VARSIZE(PTR) - VARHDRSZ - VARBITHDRSZ) #define VARBITBYTES(PTR) (VARSIZE(PTR) - VARHDRSZ - VARBITHDRSZ)
/* Padding of the bit string at the end */ /* Padding of the bit string at the end */
...@@ -27,22 +31,22 @@ typedef uint32 BitIndex; ...@@ -27,22 +31,22 @@ typedef uint32 BitIndex;
#define BITHIGH 0x80 #define BITHIGH 0x80
char * zpbitin(char *s, int dummy, int32 atttypmod); bits8 * zpbitin(char *s, int dummy, int32 atttypmod);
char * zpbitout(char *s); char * zpbitout(bits8 *s);
char * zpbitsout(char *s); char * zpbitsout(bits8 *s);
char * varbitin(char *s, int dummy, int32 atttypmod); bits8 * varbitin(char *s, int dummy, int32 atttypmod);
bool biteq (char *arg1, char *arg2); bool biteq (bits8 *arg1, bits8 *arg2);
bool bitne (char *arg1, char *arg2); bool bitne (bits8 *arg1, bits8 *arg2);
bool bitge (char *arg1, char *arg2); bool bitge (bits8 *arg1, bits8 *arg2);
bool bitgt (char *arg1, char *arg2); bool bitgt (bits8 *arg1, bits8 *arg2);
bool bitle (char *arg1, char *arg2); bool bitle (bits8 *arg1, bits8 *arg2);
bool bitlt (char *arg1, char *arg2); bool bitlt (bits8 *arg1, bits8 *arg2);
int bitcmp (char *arg1, char *arg2); int bitcmp (bits8 *arg1, bits8 *arg2);
char * bitand (char * arg1, char * arg2); bits8 * bitand (bits8 * arg1, bits8 * arg2);
char * bitor (char * arg1, char * arg2); bits8 * bitor (bits8 * arg1, bits8 * arg2);
char * bitxor (char * arg1, char * arg2); bits8 * bitxor (bits8 * arg1, bits8 * arg2);
char * bitnot (char * arg); bits8 * bitnot (bits8 * arg);
char * bitshiftright (char * arg, int shft); bits8 * bitshiftright (bits8 * arg, int shft);
char * bitshiftleft (char * arg, int shft); bits8 * bitshiftleft (bits8 * arg, int shft);
char * bitcat (char *arg1, char *arg2); bits8 * bitcat (bits8 *arg1, bits8 *arg2);
char * bitsubstr (char *arg, int32 s, int32 l); bits8 * bitsubstr (bits8 *arg, int32 s, int32 l);
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
);
/* 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 @@ ...@@ -2,6 +2,8 @@
#include "varbit.h" #include "varbit.h"
#include <stdio.h> #include <stdio.h>
void print_details (unsigned char *s);
const int numb = 8; const int numb = 8;
/* /*
const char *b[] = { "B0010", "B11011011", "B0001", "X3F12", "X27", "B", const char *b[] = { "B0010", "B11011011", "B0001", "X3F12", "X27", "B",
...@@ -23,13 +25,13 @@ void print_details (unsigned char *s) ...@@ -23,13 +25,13 @@ void print_details (unsigned char *s)
printf("\n"); printf("\n");
} }
void int
main () main ()
{ {
int i, j; int i, j;
char *s[numb]; char *s[numb];
for (i=0; i<numb; i++) { for (i=0; i<numb; i++) {
printf ("Input: %s\n",b[i]); printf ("Input: %s\n",b[i]);
s[i] = zpbitin(b[i], 0, atttypmod[i]); s[i] = zpbitin(b[i], 0, atttypmod[i]);
//print_details(s[i]); //print_details(s[i]);
...@@ -53,13 +55,13 @@ main () ...@@ -53,13 +55,13 @@ main ()
zpbitsout(bitsubstr(s[3],1,8))); zpbitsout(bitsubstr(s[3],1,8)));
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),9,8, printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),9,8,
zpbitsout(bitsubstr(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))); 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))); 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))); 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))); zpbitsout(bitsubstr(s[3],3,17)));
printf ("\nLOGICAL AND:\n"); printf ("\nLOGICAL AND:\n");
for (i=0; i<numb; i++) for (i=0; i<numb; i++)
...@@ -124,14 +126,14 @@ main () ...@@ -124,14 +126,14 @@ main ()
zpbitsout(bitsubstr(s[3],1,8))); zpbitsout(bitsubstr(s[3],1,8)));
printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),9,8, printf("%s (%d,%d) => %s\n",zpbitsout(s[3]),9,8,
zpbitsout(bitsubstr(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))); 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))); 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))); zpbitsout(bitsubstr(s[3],3,9)));
printf("%s (%d,%d) => %s\n",zpbitsout(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)),zpbitsout(bitsubstr(s[3],3,17)));
printf ("\nLOGICAL AND:\n"); printf ("\nLOGICAL AND:\n");
for (i=0; i<numb; i++) for (i=0; i<numb; i++)
for (j=i+1; j<numb; j++) for (j=i+1; j<numb; j++)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment