From efcecd9eca884776137b156a3f1f93c23b98a648 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Tue, 22 May 2001 16:37:17 +0000 Subject: [PATCH] Make bit and bit varying types reject too long input. (They already tried to do that, but inconsistently.) Make bit type reject too short input, too, per SQL. Since it no longer zero pads, 'zpbit*' has been renamed to 'bit*' in the source, hence initdb. --- doc/src/sgml/datatype.sgml | 50 +++++++++---- src/backend/parser/analyze.c | 4 +- src/backend/parser/parse_coerce.c | 4 +- src/backend/parser/parse_node.c | 6 +- src/backend/utils/adt/format_type.c | 6 +- src/backend/utils/adt/varbit.c | 131 ++++++++++------------------------ src/bin/pg_dump/pg_dump.c | 4 +- src/include/catalog/catversion.h | 4 +- src/include/catalog/pg_proc.h | 10 +-- src/include/catalog/pg_type.h | 6 +- src/include/parser/parse_coerce.h | 8 +-- src/include/utils/varbit.h | 10 +-- src/test/regress/expected/bit.out | 137 ++++++++++++++++-------------------- src/test/regress/sql/bit.sql | 76 ++++++++++---------- 14 files changed, 203 insertions(+), 253 deletions(-) diff --git a/doc/src/sgml/datatype.sgml b/doc/src/sgml/datatype.sgml index bbbc85197b..e86614f2ef 100644 --- a/doc/src/sgml/datatype.sgml +++ b/doc/src/sgml/datatype.sgml @@ -1,5 +1,5 @@ @@ -2276,30 +2276,52 @@ SELECT * FROM test1 WHERE a; Bit strings are strings of 1's and 0's. They can be used to store or visualize bit masks. There are two SQL bit types: BIT(x) and BIT - VARYING(x); the - x specifies the maximum length. - BIT type data is automatically padded with 0's on the - right to the maximum length, BIT VARYING is of - variable length. BIT without length is equivalent - to BIT(1), BIT VARYING means - unlimited length. Input data that is longer than the allowed - length will be truncated. Refer to x); where + x is a positive integer. + + + + BIT type data must match the length + x exactly; it is an error to attempt to + store shorter or longer bit strings. BIT VARYING is + of variable length up to the maximum length + x; longer strings will be rejected. + BIT without length is equivalent to + BIT(1), BIT VARYING without length + specification means unlimited length. + + + + + Prior to PostgreSQL 7.2, BIT type data was + zero-padded on the right. This was changed to comply with the + SQL standard. To implement zero-padded bit strings, a + combination of the concatenation operator and the + substring function can be used. + + + + + Refer to for information about the syntax of bit string constants. Bit-logical operators and string manipulation functions are available; see . - - - Some examples: + + Using the bit string types + CREATE TABLE test (a BIT(3), b BIT VARYING(5)); INSERT INTO test VALUES (B'101', B'00'); +INSERT INTO test VALUES (B'10', B'101'); + +ERROR: bit string length does not match type bit(3) + SELECT SUBSTRING(b FROM 1 FOR 2) FROM test; - - + diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index 4afd395f5a..0b6faf6422 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.186 2001/05/18 21:24:19 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.187 2001/05/22 16:37:15 petere Exp $ * *------------------------------------------------------------------------- */ @@ -3086,7 +3086,7 @@ transformColumnType(ParseState *pstate, ColumnDef *column) typename->typmod = VARHDRSZ + ((NUMERIC_DEFAULT_PRECISION << 16) | NUMERIC_DEFAULT_SCALE); break; - case ZPBITOID: + case BITOID: /* 'bit' -> 'bit(1)' */ typename->typmod = 1; break; diff --git a/src/backend/parser/parse_coerce.c b/src/backend/parser/parse_coerce.c index 1e7c8af7b0..38f044217e 100644 --- a/src/backend/parser/parse_coerce.c +++ b/src/backend/parser/parse_coerce.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.56 2001/03/22 03:59:41 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.57 2001/05/22 16:37:16 petere Exp $ * *------------------------------------------------------------------------- */ @@ -452,7 +452,7 @@ TypeCategory(Oid inType) result = STRING_TYPE; break; - case (ZPBITOID): + case (BITOID): case (VARBITOID): result = BITSTRING_TYPE; break; diff --git a/src/backend/parser/parse_node.c b/src/backend/parser/parse_node.c index b23dd7f1b9..1c9933c2f1 100644 --- a/src/backend/parser/parse_node.c +++ b/src/backend/parser/parse_node.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/parser/parse_node.c,v 1.53 2001/03/22 03:59:41 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/parser/parse_node.c,v 1.54 2001/05/22 16:37:16 petere Exp $ * *------------------------------------------------------------------------- */ @@ -473,11 +473,11 @@ make_const(Value *value) break; case T_BitString: - val = DirectFunctionCall3(zpbit_in, + val = DirectFunctionCall3(bit_in, CStringGetDatum(strVal(value)), ObjectIdGetDatum(InvalidOid), Int32GetDatum(-1)); - typeid = ZPBITOID; + typeid = BITOID; typelen = -1; typebyval = false; break; diff --git a/src/backend/utils/adt/format_type.c b/src/backend/utils/adt/format_type.c index 5a860ebbb1..ca46bccf5b 100644 --- a/src/backend/utils/adt/format_type.c +++ b/src/backend/utils/adt/format_type.c @@ -8,7 +8,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/format_type.c,v 1.12 2001/05/18 22:54:23 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/format_type.c,v 1.13 2001/05/22 16:37:16 petere Exp $ * *------------------------------------------------------------------------- */ @@ -198,7 +198,7 @@ format_type_internal(Oid type_oid, int32 typemod) buf = pstrdup("character varying"); break; - case ZPBITOID: + case BITOID: if (with_typemod) buf = psnprintf(5 + MAX_INT32_LEN + 1, "bit(%d)", (int) typemod); @@ -262,7 +262,7 @@ type_maximum_size(Oid type_oid, int32 typemod) break; case VARBITOID: - case ZPBITOID: + case BITOID: /* typemod is the (max) number of bits */ return (typemod + (BITS_PER_BYTE - 1)) / BITS_PER_BYTE + 2 * sizeof(int32); diff --git a/src/backend/utils/adt/varbit.c b/src/backend/utils/adt/varbit.c index 5d03683dd6..d75d05309d 100644 --- a/src/backend/utils/adt/varbit.c +++ b/src/backend/utils/adt/varbit.c @@ -9,7 +9,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/varbit.c,v 1.17 2001/05/03 19:00:36 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/varbit.c,v 1.18 2001/05/22 16:37:16 petere Exp $ * *------------------------------------------------------------------------- */ @@ -25,10 +25,6 @@ /*---------- - * Prefixes: - * zp -- zero-padded fixed length bit string - * var -- varying bit string - * * attypmod -- contains the length of the bit string in bits, or for * varying bits the maximum length. * @@ -42,13 +38,13 @@ */ /* - * zpbit_in - + * bit_in - * converts a char string to the internal representation of a bitstring. * The length is determined by the number of bits required plus * VARHDRSZ bytes or from atttypmod. */ Datum -zpbit_in(PG_FUNCTION_ARGS) +bit_in(PG_FUNCTION_ARGS) { char *input_string = PG_GETARG_CSTRING(0); @@ -64,8 +60,7 @@ zpbit_in(PG_FUNCTION_ARGS) bitlen, /* Number of bits in the bit string */ slen; /* Length of the input string */ bool bit_not_hex; /* false = hex string true = bit string */ - int bc, - ipad; + int bc; bits8 x = 0; /* Check that the first character is a b or an x */ @@ -99,15 +94,12 @@ zpbit_in(PG_FUNCTION_ARGS) /* * Sometimes atttypmod is not supplied. If it is supplied we need to - * make sure that the bitstring fits. Note that the number of infered - * bits can be larger than the number of actual bits needed, but only - * if we are reading a hex string and not by more than 3 bits, as a - * hex string gives an accurate length up to 4 bits + * make sure that the bitstring fits. */ if (atttypmod <= 0) atttypmod = bitlen; - else if (bit_not_hex ? (bitlen > atttypmod) : (bitlen > atttypmod + 3)) - elog(ERROR, "zpbit_in: bit string too long for bit(%d)", + else if (bitlen != atttypmod) + elog(ERROR, "bit string length does not match type bit(%d)", atttypmod); len = VARBITTOTALLEN(atttypmod); @@ -128,7 +120,7 @@ zpbit_in(PG_FUNCTION_ARGS) if (*sp == '1') *r |= x; else if (*sp != '0') - elog(ERROR, "Cannot parse %c as a binary digit", *sp); + elog(ERROR, "cannot parse %c as a binary digit", *sp); x >>= 1; if (x == 0) { @@ -149,7 +141,7 @@ zpbit_in(PG_FUNCTION_ARGS) else if (*sp >= 'a' && *sp <= 'f') x = (bits8) (*sp - 'a') + 10; else - elog(ERROR, "Cannot parse %c as a hex digit", *sp); + elog(ERROR, "cannot parse %c as a hex digit", *sp); if (bc) { *r++ |= x; @@ -163,31 +155,12 @@ zpbit_in(PG_FUNCTION_ARGS) } } - if (bitlen > atttypmod) - { - /* Check that this fitted */ - r = VARBITEND(result) - 1; - ipad = VARBITPAD(result); - - /* - * The bottom ipad bits of the byte pointed to by r need to be - * zero - */ - if (((*r << (BITS_PER_BYTE - ipad)) & BITMASK) != 0) - elog(ERROR, "zpbit_in: bit string too long for bit(%d)", - atttypmod); - } - PG_RETURN_VARBIT_P(result); } -/* zpbit_out - - * for the time being we print everything as hex strings, as this is likely - * to be more compact than bit strings, and consequently much more efficient - * for long strings - */ + Datum -zpbit_out(PG_FUNCTION_ARGS) +bit_out(PG_FUNCTION_ARGS) { #if 1 /* same as varbit output */ @@ -228,69 +201,59 @@ zpbit_out(PG_FUNCTION_ARGS) #endif } -/* zpbit() +/* bit() * Converts a bit() type to a specific internal length. * len is the bitlength specified in the column definition. */ Datum -zpbit(PG_FUNCTION_ARGS) +bit(PG_FUNCTION_ARGS) { VarBit *arg = PG_GETARG_VARBIT_P(0); int32 len = PG_GETARG_INT32(1); - VarBit *result; - int rlen; /* No work if typmod is invalid or supplied data matches it already */ if (len <= 0 || len == VARBITLEN(arg)) PG_RETURN_VARBIT_P(arg); - - rlen = VARBITTOTALLEN(len); - result = (VarBit *) palloc(rlen); - /* set to 0 so that result is zero-padded if input is shorter */ - memset(result, 0, rlen); - VARATT_SIZEP(result) = rlen; - VARBITLEN(result) = len; - - memcpy(VARBITS(result), VARBITS(arg), - Min(VARBITBYTES(result), VARBITBYTES(arg))); - - PG_RETURN_VARBIT_P(result); + else + elog(ERROR, "bit string length does not match type bit(%d)", + len); + return 0; /* quiet compiler */ } -/* _zpbit() +/* _bit() * Converts an array of bit() elements to a specific internal length. * len is the bitlength specified in the column definition. */ Datum -_zpbit(PG_FUNCTION_ARGS) +_bit(PG_FUNCTION_ARGS) { ArrayType *v = (ArrayType *) PG_GETARG_VARLENA_P(0); int32 len = PG_GETARG_INT32(1); FunctionCallInfoData locfcinfo; /* - * Since zpbit() is a built-in function, we should only need to look + * Since bit() is a built-in function, we should only need to look * it up once per run. */ - static FmgrInfo zpbit_finfo; + static FmgrInfo bit_finfo; - if (zpbit_finfo.fn_oid == InvalidOid) - fmgr_info(F_ZPBIT, &zpbit_finfo); + if (bit_finfo.fn_oid == InvalidOid) + fmgr_info(F_BIT, &bit_finfo); MemSet(&locfcinfo, 0, sizeof(locfcinfo)); - locfcinfo.flinfo = &zpbit_finfo; + locfcinfo.flinfo = &bit_finfo; locfcinfo.nargs = 2; /* We assume we are "strict" and need not worry about null inputs */ locfcinfo.arg[0] = PointerGetDatum(v); locfcinfo.arg[1] = Int32GetDatum(len); - return array_map(&locfcinfo, ZPBITOID, ZPBITOID); + return array_map(&locfcinfo, BITOID, BITOID); } /* * varbit_in - * converts a string to the internal representation of a bitstring. - * This is the same as zpbit_in except that atttypmod is taken as + * This is the same as bit_in except that atttypmod is taken as * the maximum length, not the exact length to force the bitstring to. */ Datum @@ -310,8 +273,7 @@ varbit_in(PG_FUNCTION_ARGS) bitlen, /* Number of bits in the bit string */ slen; /* Length of the input string */ bool bit_not_hex; /* false = hex string true = bit string */ - int bc, - ipad; + int bc; bits8 x = 0; /* Check that the first character is a b or an x */ @@ -340,15 +302,12 @@ varbit_in(PG_FUNCTION_ARGS) /* * Sometimes atttypmod is not supplied. If it is supplied we need to - * make sure that the bitstring fits. Note that the number of infered - * bits can be larger than the number of actual bits needed, but only - * if we are reading a hex string and not by more than 3 bits, as a - * hex string gives an accurate length up to 4 bits + * make sure that the bitstring fits. */ if (atttypmod <= 0) atttypmod = bitlen; - else if (bit_not_hex ? (bitlen > atttypmod) : (bitlen > atttypmod + 3)) - elog(ERROR, "varbit_in: bit string too long for bit varying(%d)", + else if (bitlen > atttypmod) + elog(ERROR, "bit string too long for type bit varying(%d)", atttypmod); len = VARBITTOTALLEN(bitlen); @@ -369,7 +328,7 @@ varbit_in(PG_FUNCTION_ARGS) if (*sp == '1') *r |= x; else if (*sp != '0') - elog(ERROR, "Cannot parse %c as a binary digit", *sp); + elog(ERROR, "cannot parse %c as a binary digit", *sp); x >>= 1; if (x == 0) { @@ -390,7 +349,7 @@ varbit_in(PG_FUNCTION_ARGS) else if (*sp >= 'a' && *sp <= 'f') x = (bits8) (*sp - 'a') + 10; else - elog(ERROR, "Cannot parse %c as a hex digit", *sp); + elog(ERROR, "cannot parse %c as a hex digit", *sp); if (bc) { *r++ |= x; @@ -404,21 +363,6 @@ varbit_in(PG_FUNCTION_ARGS) } } - if (bitlen > atttypmod) - { - /* Check that this fitted */ - r = VARBITEND(result) - 1; - ipad = VARBITPAD(result); - - /* - * The bottom ipad bits of the byte pointed to by r need to be - * zero - */ - if (((*r << (BITS_PER_BYTE - ipad)) & BITMASK) != 0) - elog(ERROR, "varbit_in: bit string too long for bit varying(%d)", - atttypmod); - } - PG_RETURN_VARBIT_P(result); } @@ -477,6 +421,9 @@ varbit(PG_FUNCTION_ARGS) if (len <= 0 || len >= VARBITLEN(arg)) PG_RETURN_VARBIT_P(arg); + if (len < VARBITLEN(arg)) + elog(ERROR, "bit string too long for type bit varying(%d)", len); + rlen = VARBITTOTALLEN(len); result = (VarBit *) palloc(rlen); VARATT_SIZEP(result) = rlen; @@ -868,7 +815,7 @@ bitand(PG_FUNCTION_ARGS) bitlen1 = VARBITLEN(arg1); bitlen2 = VARBITLEN(arg2); if (bitlen1 != bitlen2) - elog(ERROR, "bitand: Cannot AND bitstrings of different sizes"); + elog(ERROR, "cannot AND bit strings of different sizes"); len = VARSIZE(arg1); result = (VarBit *) palloc(len); VARATT_SIZEP(result) = len; @@ -906,7 +853,7 @@ bitor(PG_FUNCTION_ARGS) bitlen1 = VARBITLEN(arg1); bitlen2 = VARBITLEN(arg2); if (bitlen1 != bitlen2) - elog(ERROR, "bitor: Cannot OR bitstrings of different sizes"); + elog(ERROR, "cannot OR bit strings of different sizes"); len = VARSIZE(arg1); result = (VarBit *) palloc(len); VARATT_SIZEP(result) = len; @@ -950,7 +897,7 @@ bitxor(PG_FUNCTION_ARGS) bitlen1 = VARBITLEN(arg1); bitlen2 = VARBITLEN(arg2); if (bitlen1 != bitlen2) - elog(ERROR, "bitxor: Cannot XOR bitstrings of different sizes"); + elog(ERROR, "cannot XOR bit strings of different sizes"); len = VARSIZE(arg1); result = (VarBit *) palloc(len); VARATT_SIZEP(result) = len; @@ -1165,7 +1112,7 @@ bittoint4(PG_FUNCTION_ARGS) /* Check that the bit string is not too long */ if (VARBITLEN(arg) > sizeof(int4) * BITS_PER_BYTE) - elog(ERROR, "Bit string is too large to fit in an int4"); + elog(ERROR, "bit string is too large to fit in type integer"); result = 0; for (r = VARBITS(arg); r < VARBITEND(arg); r++) { diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index f18d0972ed..428c4931a0 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -22,7 +22,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.208 2001/05/17 21:12:48 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.209 2001/05/22 16:37:16 petere Exp $ * * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb * @@ -556,7 +556,7 @@ dumpClasses_dumpData(Archive *fout, char *oid, void *dctxv) archprintf(fout, "%s", PQgetvalue(res, tuple, field)); break; - case ZPBITOID: + case BITOID: case VARBITOID: archprintf(fout, "B'%s'", PQgetvalue(res, tuple, field)); diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h index 2616b4af7d..c38f27c955 100644 --- a/src/include/catalog/catversion.h +++ b/src/include/catalog/catversion.h @@ -37,7 +37,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: catversion.h,v 1.80 2001/05/21 14:22:17 wieck Exp $ + * $Id: catversion.h,v 1.81 2001/05/22 16:37:16 petere Exp $ * *------------------------------------------------------------------------- */ @@ -53,6 +53,6 @@ */ /* yyyymmddN */ -#define CATALOG_VERSION_NO 200105211 +#define CATALOG_VERSION_NO 200105221 #endif diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index f905a063c6..6b31f2a4b2 100644 --- a/src/include/catalog/pg_proc.h +++ b/src/include/catalog/pg_proc.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: pg_proc.h,v 1.186 2001/05/20 20:28:19 tgl Exp $ + * $Id: pg_proc.h,v 1.187 2001/05/22 16:37:16 petere Exp $ * * NOTES * The script catalog/genbki.sh reads this file and generates .bki @@ -1975,9 +1975,9 @@ DESCR("# points in path"); DATA(insert OID = 1556 ( npoints PGUID 12 f t t t 1 f 23 "604" 100 0 0 100 poly_npoints - )); DESCR("number of points in polygon"); -DATA(insert OID = 1564 ( zpbit_in PGUID 12 f t t t 1 f 1560 "0" 100 0 0 100 zpbit_in - )); +DATA(insert OID = 1564 ( bit_in PGUID 12 f t t t 1 f 1560 "0" 100 0 0 100 bit_in - )); DESCR("(internal)"); -DATA(insert OID = 1565 ( zpbit_out PGUID 12 f t t t 1 f 23 "0" 100 0 0 100 zpbit_out - )); +DATA(insert OID = 1565 ( bit_out PGUID 12 f t t t 1 f 23 "0" 100 0 0 100 bit_out - )); DESCR("(internal)"); DATA(insert OID = 1569 ( like PGUID 12 f t t t 2 f 16 "25 25" 100 0 0 100 textlike - )); @@ -2214,9 +2214,9 @@ DESCR("int4 to bitstring"); DATA(insert OID = 1684 ( bittoint4 PGUID 12 f t t t 1 f 23 "1560" 100 0 0 100 bittoint4 - )); DESCR("bitstring to int4"); -DATA(insert OID = 1685 ( bit PGUID 12 f t t t 2 f 1560 "1560 23" 100 0 0 100 zpbit - )); +DATA(insert OID = 1685 ( bit PGUID 12 f t t t 2 f 1560 "1560 23" 100 0 0 100 bit - )); DESCR("adjust bit() to typmod length"); -DATA(insert OID = 1686 ( _bit PGUID 12 f t t t 2 f 1561 "1561 23" 100 0 0 100 _zpbit - )); +DATA(insert OID = 1686 ( _bit PGUID 12 f t t t 2 f 1561 "1561 23" 100 0 0 100 _bit - )); DESCR("adjust bit()[] to typmod length"); DATA(insert OID = 1687 ( varbit PGUID 12 f t t t 2 f 1562 "1562 23" 100 0 0 100 varbit - )); DESCR("adjust varbit() to typmod length"); diff --git a/src/include/catalog/pg_type.h b/src/include/catalog/pg_type.h index 5b330d9ea3..89e5027858 100644 --- a/src/include/catalog/pg_type.h +++ b/src/include/catalog/pg_type.h @@ -8,7 +8,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: pg_type.h,v 1.106 2001/05/21 14:22:18 wieck Exp $ + * $Id: pg_type.h,v 1.107 2001/05/22 16:37:17 petere Exp $ * * NOTES * the genbki.sh script reads this file and generates .bki @@ -397,9 +397,9 @@ DESCR("hh:mm:ss, ANSI SQL time"); DATA(insert OID = 1270 ( _timetz PGUID -1 -1 f b t \054 0 1266 array_in array_out array_in array_out d x _null_ )); /* OIDS 1500 - 1599 */ -DATA(insert OID = 1560 ( bit PGUID -1 -1 f b t \054 0 0 zpbit_in zpbit_out zpbit_in zpbit_out i x _null_ )); +DATA(insert OID = 1560 ( bit PGUID -1 -1 f b t \054 0 0 bit_in bit_out bit_in bit_out i x _null_ )); DESCR("fixed-length bit string"); -#define ZPBITOID 1560 +#define BITOID 1560 DATA(insert OID = 1561 ( _bit PGUID -1 -1 f b t \054 0 1560 array_in array_out array_in array_out i x _null_ )); DATA(insert OID = 1562 ( varbit PGUID -1 -1 f b t \054 0 0 varbit_in varbit_out varbit_in varbit_out i x _null_ )); DESCR("variable-length bit string"); diff --git a/src/include/parser/parse_coerce.h b/src/include/parser/parse_coerce.h index d8c7e430cd..f81a3be830 100644 --- a/src/include/parser/parse_coerce.h +++ b/src/include/parser/parse_coerce.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: parse_coerce.h,v 1.27 2001/03/22 04:00:57 momjian Exp $ + * $Id: parse_coerce.h,v 1.28 2001/05/22 16:37:17 petere Exp $ * *------------------------------------------------------------------------- */ @@ -69,7 +69,7 @@ typedef enum CATEGORY || ((t) == CIRCLEOID) \ || ((t) == INETOID) \ || ((t) == CIDROID) \ - || ((t) == ZPBITOID) \ + || ((t) == BITOID) \ || ((t) == VARBITOID) ) @@ -101,8 +101,8 @@ typedef enum CATEGORY || ((a) == INT4OID && (b) == RELTIMEOID) \ || ((a) == INETOID && (b) == CIDROID) \ || ((a) == CIDROID && (b) == INETOID) \ - || ((a) == ZPBITOID && (b) == VARBITOID) \ - || ((a) == VARBITOID && (b) == ZPBITOID)) + || ((a) == BITOID && (b) == VARBITOID) \ + || ((a) == VARBITOID && (b) == BITOID)) /* IS_HIGHER_TYPE() * These types are the most general in each of the type categories. diff --git a/src/include/utils/varbit.h b/src/include/utils/varbit.h index 3a1b3f07a0..35172de7be 100644 --- a/src/include/utils/varbit.h +++ b/src/include/utils/varbit.h @@ -8,7 +8,7 @@ * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: varbit.h,v 1.10 2001/03/22 04:01:15 momjian Exp $ + * $Id: varbit.h,v 1.11 2001/05/22 16:37:17 petere Exp $ * *------------------------------------------------------------------------- */ @@ -61,12 +61,12 @@ typedef struct #define BITHIGH 0x80 -extern Datum zpbit_in(PG_FUNCTION_ARGS); -extern Datum zpbit_out(PG_FUNCTION_ARGS); +extern Datum bit_in(PG_FUNCTION_ARGS); +extern Datum bit_out(PG_FUNCTION_ARGS); extern Datum varbit_in(PG_FUNCTION_ARGS); extern Datum varbit_out(PG_FUNCTION_ARGS); -extern Datum zpbit(PG_FUNCTION_ARGS); -extern Datum _zpbit(PG_FUNCTION_ARGS); +extern Datum bit(PG_FUNCTION_ARGS); +extern Datum _bit(PG_FUNCTION_ARGS); extern Datum varbit(PG_FUNCTION_ARGS); extern Datum _varbit(PG_FUNCTION_ARGS); extern Datum biteq(PG_FUNCTION_ARGS); diff --git a/src/test/regress/expected/bit.out b/src/test/regress/expected/bit.out index 552b528d2a..4821a1d3da 100644 --- a/src/test/regress/expected/bit.out +++ b/src/test/regress/expected/bit.out @@ -4,23 +4,23 @@ -- -- Build tables for testing -- -CREATE TABLE ZPBIT_TABLE(b BIT(11)); -INSERT INTO ZPBIT_TABLE VALUES (B''); -INSERT INTO ZPBIT_TABLE VALUES (B'0'); -INSERT INTO ZPBIT_TABLE VALUES (B'11011'); -INSERT INTO ZPBIT_TABLE VALUES (B'01010101010'); -INSERT INTO ZPBIT_TABLE VALUES (B'101011111010'); -- too long ---INSERT INTO ZPBIT_TABLE VALUES ('X554'); ---INSERT INTO ZPBIT_TABLE VALUES ('X555'); -SELECT * FROM ZPBIT_TABLE; +CREATE TABLE BIT_TABLE(b BIT(11)); +INSERT INTO BIT_TABLE VALUES (B'10'); -- too short +ERROR: bit string length does not match type bit(11) +INSERT INTO BIT_TABLE VALUES (B'00000000000'); +INSERT INTO BIT_TABLE VALUES (B'11011000000'); +INSERT INTO BIT_TABLE VALUES (B'01010101010'); +INSERT INTO BIT_TABLE VALUES (B'101011111010'); -- too long +ERROR: bit string length does not match type bit(11) +--INSERT INTO BIT_TABLE VALUES ('X554'); +--INSERT INTO BIT_TABLE VALUES ('X555'); +SELECT * FROM BIT_TABLE; b ------------- 00000000000 - 00000000000 11011000000 01010101010 - 10101111101 -(5 rows) +(3 rows) CREATE TABLE VARBIT_TABLE(v BIT VARYING(11)); INSERT INTO VARBIT_TABLE VALUES (B''); @@ -28,6 +28,7 @@ INSERT INTO VARBIT_TABLE VALUES (B'0'); INSERT INTO VARBIT_TABLE VALUES (B'010101'); INSERT INTO VARBIT_TABLE VALUES (B'01010101010'); INSERT INTO VARBIT_TABLE VALUES (B'101011111010'); -- too long +ERROR: bit string too long for type bit varying(11) --INSERT INTO VARBIT_TABLE VALUES ('X554'); --INSERT INTO VARBIT_TABLE VALUES ('X555'); SELECT * FROM VARBIT_TABLE; @@ -37,53 +38,37 @@ SELECT * FROM VARBIT_TABLE; 0 010101 01010101010 - 10101111101 -(5 rows) +(4 rows) -- Concatenation SELECT v, b, (v || b) AS concat - FROM ZPBIT_TABLE, VARBIT_TABLE + FROM BIT_TABLE, VARBIT_TABLE ORDER BY 3; v | b | concat -------------+-------------+------------------------ | 00000000000 | 00000000000 - | 00000000000 | 00000000000 - 0 | 00000000000 | 000000000000 0 | 00000000000 | 000000000000 0 | 01010101010 | 001010101010 010101 | 00000000000 | 01010100000000000 - 010101 | 00000000000 | 01010100000000000 | 01010101010 | 01010101010 01010101010 | 00000000000 | 0101010101000000000000 - 01010101010 | 00000000000 | 0101010101000000000000 01010101010 | 01010101010 | 0101010101001010101010 010101 | 01010101010 | 01010101010101010 - 01010101010 | 10101111101 | 0101010101010101111101 01010101010 | 11011000000 | 0101010101011011000000 - 010101 | 10101111101 | 01010110101111101 010101 | 11011000000 | 01010111011000000 - 0 | 10101111101 | 010101111101 0 | 11011000000 | 011011000000 - | 10101111101 | 10101111101 - 10101111101 | 00000000000 | 1010111110100000000000 - 10101111101 | 00000000000 | 1010111110100000000000 - 10101111101 | 01010101010 | 1010111110101010101010 - 10101111101 | 10101111101 | 1010111110110101111101 - 10101111101 | 11011000000 | 1010111110111011000000 | 11011000000 | 11011000000 -(25 rows) +(12 rows) -- Length SELECT b, length(b) AS lb - FROM ZPBIT_TABLE; + FROM BIT_TABLE; b | lb -------------+---- 00000000000 | 11 - 00000000000 | 11 11011000000 | 11 01010101010 | 11 - 10101111101 | 11 -(5 rows) +(3 rows) SELECT v, length(v) AS lv FROM VARBIT_TABLE; @@ -93,23 +78,20 @@ SELECT v, length(v) AS lv 0 | 1 010101 | 6 01010101010 | 11 - 10101111101 | 11 -(5 rows) +(4 rows) -- Substring SELECT b, SUBSTRING(b FROM 2 FOR 4) AS sub_2_4, SUBSTRING(b FROM 7 FOR 13) AS sub_7_13, SUBSTRING(b FROM 6) AS sub_6 - FROM ZPBIT_TABLE; + FROM BIT_TABLE; b | sub_2_4 | sub_7_13 | sub_6 -------------+---------+----------+-------- 00000000000 | 0000 | 00000 | 000000 - 00000000000 | 0000 | 00000 | 000000 11011000000 | 1011 | 00000 | 000000 01010101010 | 1010 | 01010 | 101010 - 10101111101 | 0101 | 11101 | 111101 -(5 rows) +(3 rows) SELECT v, SUBSTRING(v FROM 2 FOR 4) AS sub_2_4, @@ -122,8 +104,7 @@ SELECT v, 0 | | | 010101 | 1010 | | 1 01010101010 | 1010 | 01010 | 101010 - 10101111101 | 0101 | 11101 | 111101 -(5 rows) +(4 rows) --- Bit operations DROP TABLE varbit_table; @@ -178,11 +159,11 @@ SELECT a,a<<4 AS "a<<4",b,b>>2 AS "b>>2" FROM varbit_table; DROP TABLE varbit_table; --- Bit operations -DROP TABLE zpbit_table; -CREATE TABLE zpbit_table (a BIT(16), b BIT(16)); -COPY zpbit_table FROM stdin; +DROP TABLE bit_table; +CREATE TABLE bit_table (a BIT(16), b BIT(16)); +COPY bit_table FROM stdin; SELECT a,b,~a AS "~ a",a & b AS "a & b", - a|b AS "a | b", a # b AS "a # b" FROM zpbit_table; + a|b AS "a | b", a # b AS "a # b" FROM bit_table; a | b | ~ a | a & b | a | b | a # b ------------------+------------------+------------------+------------------+------------------+------------------ 0000111100000000 | 0001000000000000 | 1111000011111111 | 0000000000000000 | 0001111100000000 | 0001111100000000 @@ -198,7 +179,7 @@ SELECT a,b,~a AS "~ a",a & b AS "a & b", (10 rows) SELECT a,b,a=b AS "a>=b",a>b AS "a>b",a<>b AS "a<>b" FROM zpbit_table; + a>=b AS "a>=b",a>b AS "a>b",a<>b AS "a<>b" FROM bit_table; a | b | a=b | a>b | a<>b ------------------+------------------+-----+------+-----+------+-----+------ 0000111100000000 | 0001000000000000 | t | t | f | f | f | t @@ -213,7 +194,7 @@ SELECT a,b,a>2 AS "b>>2" FROM zpbit_table; +SELECT a,a<<4 AS "a<<4",b,b>>2 AS "b>>2" FROM bit_table; a | a<<4 | b | b>>2 ------------------+------------------+------------------+------------------ 0000111100000000 | 1111000000000000 | 0001000000000000 | 0000010000000000 @@ -228,14 +209,14 @@ SELECT a,a<<4 AS "a<<4",b,b>>2 AS "b>>2" FROM zpbit_table; 0001001000110100 | 0010001101000000 | 1111111111110101 | 0011111111111101 (10 rows) -DROP TABLE zpbit_table; +DROP TABLE bit_table; -- The following should fail select B'001' & B'10'; -ERROR: bitand: Cannot AND bitstrings of different sizes +ERROR: cannot AND bit strings of different sizes select B'0111' | B'011'; -ERROR: bitor: Cannot OR bitstrings of different sizes +ERROR: cannot OR bit strings of different sizes select B'0010' # B'011101'; -ERROR: bitxor: Cannot XOR bitstrings of different sizes +ERROR: cannot XOR bit strings of different sizes -- More position tests, checking all the boundary cases SELECT POSITION(B'1010' IN B'0000101'); -- 0 position @@ -466,16 +447,16 @@ SELECT POSITION(B'0000000000011101011111010110' IN B'000000000011101011111010110 (1 row) -- Shifting -CREATE TABLE ZPBIT_SHIFT_TABLE(b BIT(16)); -INSERT INTO ZPBIT_SHIFT_TABLE VALUES (B'11011'); -INSERT INTO ZPBIT_SHIFT_TABLE SELECT b>>1 FROM ZPBIT_SHIFT_TABLE; -INSERT INTO ZPBIT_SHIFT_TABLE SELECT b>>2 FROM ZPBIT_SHIFT_TABLE; -INSERT INTO ZPBIT_SHIFT_TABLE SELECT b>>4 FROM ZPBIT_SHIFT_TABLE; -INSERT INTO ZPBIT_SHIFT_TABLE SELECT b>>8 FROM ZPBIT_SHIFT_TABLE; -SELECT POSITION(B'1101'IN b), +CREATE TABLE BIT_SHIFT_TABLE(b BIT(16)); +INSERT INTO BIT_SHIFT_TABLE VALUES (B'1101100000000000'); +INSERT INTO BIT_SHIFT_TABLE SELECT b>>1 FROM BIT_SHIFT_TABLE; +INSERT INTO BIT_SHIFT_TABLE SELECT b>>2 FROM BIT_SHIFT_TABLE; +INSERT INTO BIT_SHIFT_TABLE SELECT b>>4 FROM BIT_SHIFT_TABLE; +INSERT INTO BIT_SHIFT_TABLE SELECT b>>8 FROM BIT_SHIFT_TABLE; +SELECT POSITION(B'1101' IN b), POSITION(B'11011' IN b), b - FROM ZPBIT_SHIFT_TABLE ; + FROM BIT_SHIFT_TABLE ; position | position | b ----------+----------+------------------ 1 | 1 | 1101100000000000 @@ -496,35 +477,35 @@ SELECT POSITION(B'1101'IN b), 0 | 0 | 0000000000000001 (16 rows) -CREATE TABLE VARBIT_SHIFT_TABLE(v BIT VARYING(18)); +CREATE TABLE VARBIT_SHIFT_TABLE(v BIT VARYING(20)); INSERT INTO VARBIT_SHIFT_TABLE VALUES (B'11011'); -INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v AS BIT(6)) >>1 FROM VARBIT_SHIFT_TABLE; -INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v AS BIT(8)) >>2 FROM VARBIT_SHIFT_TABLE; -INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v AS BIT(12)) >>4 FROM VARBIT_SHIFT_TABLE; -INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v AS BIT(20)) >>8 FROM VARBIT_SHIFT_TABLE; +INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v || B'0' AS BIT VARYING(6)) >>1 FROM VARBIT_SHIFT_TABLE; +INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v || B'00' AS BIT VARYING(8)) >>2 FROM VARBIT_SHIFT_TABLE; +INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v || B'0000' AS BIT VARYING(12)) >>4 FROM VARBIT_SHIFT_TABLE; +INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v || B'00000000' AS BIT VARYING(20)) >>8 FROM VARBIT_SHIFT_TABLE; SELECT POSITION(B'1101' IN v), POSITION(B'11011' IN v), v FROM VARBIT_SHIFT_TABLE ; - position | position | v -----------+----------+-------------------- + position | position | v +----------+----------+---------------------- 1 | 1 | 11011 2 | 2 | 011011 - 3 | 3 | 00110110 + 3 | 3 | 0011011 4 | 4 | 00011011 - 5 | 5 | 000011011000 - 6 | 6 | 000001101100 - 7 | 7 | 000000110110 + 5 | 5 | 000011011 + 6 | 6 | 0000011011 + 7 | 7 | 00000011011 8 | 8 | 000000011011 - 9 | 9 | 000000001101100000 - 10 | 10 | 000000000110110000 - 11 | 11 | 000000000011011000 - 12 | 12 | 000000000001101100 - 13 | 13 | 000000000000110110 + 9 | 9 | 0000000011011 + 10 | 10 | 00000000011011 + 11 | 11 | 000000000011011 + 12 | 12 | 0000000000011011 + 13 | 13 | 00000000000011011 14 | 14 | 000000000000011011 - 15 | 0 | 000000000000001101 - 0 | 0 | 000000000000000110 + 15 | 15 | 0000000000000011011 + 16 | 16 | 00000000000000011011 (16 rows) -DROP TABLE ZPBIT_SHIFT_TABLE; +DROP TABLE BIT_SHIFT_TABLE; DROP TABLE VARBIT_SHIFT_TABLE; diff --git a/src/test/regress/sql/bit.sql b/src/test/regress/sql/bit.sql index 41eee3f886..f178991d8f 100644 --- a/src/test/regress/sql/bit.sql +++ b/src/test/regress/sql/bit.sql @@ -6,17 +6,17 @@ -- Build tables for testing -- -CREATE TABLE ZPBIT_TABLE(b BIT(11)); +CREATE TABLE BIT_TABLE(b BIT(11)); -INSERT INTO ZPBIT_TABLE VALUES (B''); -INSERT INTO ZPBIT_TABLE VALUES (B'0'); -INSERT INTO ZPBIT_TABLE VALUES (B'11011'); -INSERT INTO ZPBIT_TABLE VALUES (B'01010101010'); -INSERT INTO ZPBIT_TABLE VALUES (B'101011111010'); -- too long ---INSERT INTO ZPBIT_TABLE VALUES ('X554'); ---INSERT INTO ZPBIT_TABLE VALUES ('X555'); +INSERT INTO BIT_TABLE VALUES (B'10'); -- too short +INSERT INTO BIT_TABLE VALUES (B'00000000000'); +INSERT INTO BIT_TABLE VALUES (B'11011000000'); +INSERT INTO BIT_TABLE VALUES (B'01010101010'); +INSERT INTO BIT_TABLE VALUES (B'101011111010'); -- too long +--INSERT INTO BIT_TABLE VALUES ('X554'); +--INSERT INTO BIT_TABLE VALUES ('X555'); -SELECT * FROM ZPBIT_TABLE; +SELECT * FROM BIT_TABLE; CREATE TABLE VARBIT_TABLE(v BIT VARYING(11)); @@ -32,12 +32,12 @@ SELECT * FROM VARBIT_TABLE; -- Concatenation SELECT v, b, (v || b) AS concat - FROM ZPBIT_TABLE, VARBIT_TABLE + FROM BIT_TABLE, VARBIT_TABLE ORDER BY 3; -- Length SELECT b, length(b) AS lb - FROM ZPBIT_TABLE; + FROM BIT_TABLE; SELECT v, length(v) AS lv FROM VARBIT_TABLE; @@ -46,7 +46,7 @@ SELECT b, SUBSTRING(b FROM 2 FOR 4) AS sub_2_4, SUBSTRING(b FROM 7 FOR 13) AS sub_7_13, SUBSTRING(b FROM 6) AS sub_6 - FROM ZPBIT_TABLE; + FROM BIT_TABLE; SELECT v, SUBSTRING(v FROM 2 FOR 4) AS sub_2_4, SUBSTRING(v FROM 7 FOR 13) AS sub_7_13, @@ -78,14 +78,14 @@ SELECT a,a<<4 AS "a<<4",b,b>>2 AS "b>>2" FROM varbit_table; DROP TABLE varbit_table; --- Bit operations -DROP TABLE zpbit_table; -CREATE TABLE zpbit_table (a BIT(16), b BIT(16)); -COPY zpbit_table FROM stdin; -X0F X10 -X1F X11 -X2F X12 -X3F X13 -X8F X04 +DROP TABLE bit_table; +CREATE TABLE bit_table (a BIT(16), b BIT(16)); +COPY bit_table FROM stdin; +X0F00 X1000 +X1F00 X1100 +X2F00 X1200 +X3F00 X1300 +X8F00 X0400 X000F X0010 X0123 XFFFF X2468 X2468 @@ -94,12 +94,12 @@ X1234 XFFF5 \. SELECT a,b,~a AS "~ a",a & b AS "a & b", - a|b AS "a | b", a # b AS "a # b" FROM zpbit_table; + a|b AS "a | b", a # b AS "a # b" FROM bit_table; SELECT a,b,a=b AS "a>=b",a>b AS "a>b",a<>b AS "a<>b" FROM zpbit_table; -SELECT a,a<<4 AS "a<<4",b,b>>2 AS "b>>2" FROM zpbit_table; + a>=b AS "a>=b",a>b AS "a>b",a<>b AS "a<>b" FROM bit_table; +SELECT a,a<<4 AS "a<<4",b,b>>2 AS "b>>2" FROM bit_table; -DROP TABLE zpbit_table; +DROP TABLE bit_table; -- The following should fail @@ -158,29 +158,29 @@ SELECT POSITION(B'0000000000011101011111010110' IN B'000000000011101011111010110 -- Shifting -CREATE TABLE ZPBIT_SHIFT_TABLE(b BIT(16)); -INSERT INTO ZPBIT_SHIFT_TABLE VALUES (B'11011'); -INSERT INTO ZPBIT_SHIFT_TABLE SELECT b>>1 FROM ZPBIT_SHIFT_TABLE; -INSERT INTO ZPBIT_SHIFT_TABLE SELECT b>>2 FROM ZPBIT_SHIFT_TABLE; -INSERT INTO ZPBIT_SHIFT_TABLE SELECT b>>4 FROM ZPBIT_SHIFT_TABLE; -INSERT INTO ZPBIT_SHIFT_TABLE SELECT b>>8 FROM ZPBIT_SHIFT_TABLE; -SELECT POSITION(B'1101'IN b), +CREATE TABLE BIT_SHIFT_TABLE(b BIT(16)); +INSERT INTO BIT_SHIFT_TABLE VALUES (B'1101100000000000'); +INSERT INTO BIT_SHIFT_TABLE SELECT b>>1 FROM BIT_SHIFT_TABLE; +INSERT INTO BIT_SHIFT_TABLE SELECT b>>2 FROM BIT_SHIFT_TABLE; +INSERT INTO BIT_SHIFT_TABLE SELECT b>>4 FROM BIT_SHIFT_TABLE; +INSERT INTO BIT_SHIFT_TABLE SELECT b>>8 FROM BIT_SHIFT_TABLE; +SELECT POSITION(B'1101' IN b), POSITION(B'11011' IN b), b - FROM ZPBIT_SHIFT_TABLE ; + FROM BIT_SHIFT_TABLE ; -CREATE TABLE VARBIT_SHIFT_TABLE(v BIT VARYING(18)); +CREATE TABLE VARBIT_SHIFT_TABLE(v BIT VARYING(20)); INSERT INTO VARBIT_SHIFT_TABLE VALUES (B'11011'); -INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v AS BIT(6)) >>1 FROM VARBIT_SHIFT_TABLE; -INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v AS BIT(8)) >>2 FROM VARBIT_SHIFT_TABLE; -INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v AS BIT(12)) >>4 FROM VARBIT_SHIFT_TABLE; -INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v AS BIT(20)) >>8 FROM VARBIT_SHIFT_TABLE; +INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v || B'0' AS BIT VARYING(6)) >>1 FROM VARBIT_SHIFT_TABLE; +INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v || B'00' AS BIT VARYING(8)) >>2 FROM VARBIT_SHIFT_TABLE; +INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v || B'0000' AS BIT VARYING(12)) >>4 FROM VARBIT_SHIFT_TABLE; +INSERT INTO VARBIT_SHIFT_TABLE SELECT CAST(v || B'00000000' AS BIT VARYING(20)) >>8 FROM VARBIT_SHIFT_TABLE; SELECT POSITION(B'1101' IN v), POSITION(B'11011' IN v), v FROM VARBIT_SHIFT_TABLE ; -DROP TABLE ZPBIT_SHIFT_TABLE; +DROP TABLE BIT_SHIFT_TABLE; DROP TABLE VARBIT_SHIFT_TABLE; -- 2.11.0