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
$(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
......
This diff is collapsed.
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"
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);
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 @@
#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++)
......
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