Commit efcecd9e authored by Peter Eisentraut's avatar Peter Eisentraut

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.
parent c84c3d8f
<!-- <!--
$Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.54 2001/05/21 16:54:45 petere Exp $ $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.55 2001/05/22 16:37:15 petere Exp $
--> -->
<chapter id="datatype"> <chapter id="datatype">
...@@ -2276,30 +2276,52 @@ SELECT * FROM test1 WHERE a; ...@@ -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 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: or visualize bit masks. There are two SQL bit types:
<type>BIT(<replaceable>x</replaceable>)</type> and <type>BIT <type>BIT(<replaceable>x</replaceable>)</type> and <type>BIT
VARYING(<replaceable>x</replaceable>)</type>; the VARYING(<replaceable>x</replaceable>)</type>; where
<replaceable>x</replaceable> specifies the maximum length. <replaceable>x</replaceable> is a positive integer.
<type>BIT</type> type data is automatically padded with 0's on the </para>
right to the maximum length, <type>BIT VARYING</type> is of
variable length. <type>BIT</type> without length is equivalent <para>
to <literal>BIT(1)</literal>, <type>BIT VARYING</type> means <type>BIT</type> type data must match the length
unlimited length. Input data that is longer than the allowed <replaceable>x</replaceable> exactly; it is an error to attempt to
length will be truncated. Refer to <xref store shorter or longer bit strings. <type>BIT VARYING</type> is
of variable length up to the maximum length
<replaceable>x</replaceable>; longer strings will be rejected.
<type>BIT</type> without length is equivalent to
<literal>BIT(1)</literal>, <type>BIT VARYING</type> without length
specification means unlimited length.
</para>
<note>
<para>
Prior to PostgreSQL 7.2, <type>BIT</type> 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
<function>substring</function> function can be used.
</para>
</note>
<para>
Refer to <xref
linkend="sql-syntax-bit-strings"> for information about the syntax linkend="sql-syntax-bit-strings"> for information about the syntax
of bit string constants. Bit-logical operators and string of bit string constants. Bit-logical operators and string
manipulation functions are available; see <xref manipulation functions are available; see <xref
linkend="functions">. linkend="functions">.
</para> </para>
<informalexample> <example>
<para> <title>Using the bit string types</title>
Some examples:
<programlisting> <programlisting>
CREATE TABLE test (a BIT(3), b BIT VARYING(5)); CREATE TABLE test (a BIT(3), b BIT VARYING(5));
INSERT INTO test VALUES (B'101', B'00'); INSERT INTO test VALUES (B'101', B'00');
INSERT INTO test VALUES (B'10', B'101');
<computeroutput>
ERROR: bit string length does not match type bit(3)
</computeroutput>
SELECT SUBSTRING(b FROM 1 FOR 2) FROM test; SELECT SUBSTRING(b FROM 1 FOR 2) FROM test;
</programlisting> </programlisting>
</para> </example>
</informalexample>
</sect1> </sect1>
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * 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) ...@@ -3086,7 +3086,7 @@ transformColumnType(ParseState *pstate, ColumnDef *column)
typename->typmod = VARHDRSZ + typename->typmod = VARHDRSZ +
((NUMERIC_DEFAULT_PRECISION << 16) | NUMERIC_DEFAULT_SCALE); ((NUMERIC_DEFAULT_PRECISION << 16) | NUMERIC_DEFAULT_SCALE);
break; break;
case ZPBITOID: case BITOID:
/* 'bit' -> 'bit(1)' */ /* 'bit' -> 'bit(1)' */
typename->typmod = 1; typename->typmod = 1;
break; break;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -452,7 +452,7 @@ TypeCategory(Oid inType)
result = STRING_TYPE; result = STRING_TYPE;
break; break;
case (ZPBITOID): case (BITOID):
case (VARBITOID): case (VARBITOID):
result = BITSTRING_TYPE; result = BITSTRING_TYPE;
break; break;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -473,11 +473,11 @@ make_const(Value *value)
break; break;
case T_BitString: case T_BitString:
val = DirectFunctionCall3(zpbit_in, val = DirectFunctionCall3(bit_in,
CStringGetDatum(strVal(value)), CStringGetDatum(strVal(value)),
ObjectIdGetDatum(InvalidOid), ObjectIdGetDatum(InvalidOid),
Int32GetDatum(-1)); Int32GetDatum(-1));
typeid = ZPBITOID; typeid = BITOID;
typelen = -1; typelen = -1;
typebyval = false; typebyval = false;
break; break;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * 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) ...@@ -198,7 +198,7 @@ format_type_internal(Oid type_oid, int32 typemod)
buf = pstrdup("character varying"); buf = pstrdup("character varying");
break; break;
case ZPBITOID: case BITOID:
if (with_typemod) if (with_typemod)
buf = psnprintf(5 + MAX_INT32_LEN + 1, "bit(%d)", buf = psnprintf(5 + MAX_INT32_LEN + 1, "bit(%d)",
(int) typemod); (int) typemod);
...@@ -262,7 +262,7 @@ type_maximum_size(Oid type_oid, int32 typemod) ...@@ -262,7 +262,7 @@ type_maximum_size(Oid type_oid, int32 typemod)
break; break;
case VARBITOID: case VARBITOID:
case ZPBITOID: case BITOID:
/* typemod is the (max) number of bits */ /* typemod is the (max) number of bits */
return (typemod + (BITS_PER_BYTE - 1)) / BITS_PER_BYTE return (typemod + (BITS_PER_BYTE - 1)) / BITS_PER_BYTE
+ 2 * sizeof(int32); + 2 * sizeof(int32);
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * 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 @@ ...@@ -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 * attypmod -- contains the length of the bit string in bits, or for
* varying bits the maximum length. * varying bits the maximum length.
* *
...@@ -42,13 +38,13 @@ ...@@ -42,13 +38,13 @@
*/ */
/* /*
* zpbit_in - * bit_in -
* converts a char string to the internal representation of a bitstring. * converts a char string to the internal representation of a bitstring.
* The length is determined by the number of bits required plus * The length is determined by the number of bits required plus
* VARHDRSZ bytes or from atttypmod. * VARHDRSZ bytes or from atttypmod.
*/ */
Datum Datum
zpbit_in(PG_FUNCTION_ARGS) bit_in(PG_FUNCTION_ARGS)
{ {
char *input_string = PG_GETARG_CSTRING(0); char *input_string = PG_GETARG_CSTRING(0);
...@@ -64,8 +60,7 @@ zpbit_in(PG_FUNCTION_ARGS) ...@@ -64,8 +60,7 @@ zpbit_in(PG_FUNCTION_ARGS)
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 */
bool bit_not_hex; /* false = hex string true = bit string */ bool bit_not_hex; /* false = hex string true = bit string */
int bc, int bc;
ipad;
bits8 x = 0; bits8 x = 0;
/* Check that the first character is a b or an x */ /* Check that the first character is a b or an x */
...@@ -99,15 +94,12 @@ zpbit_in(PG_FUNCTION_ARGS) ...@@ -99,15 +94,12 @@ zpbit_in(PG_FUNCTION_ARGS)
/* /*
* Sometimes atttypmod is not supplied. If it is supplied we need to * Sometimes atttypmod is not supplied. If it is supplied we need to
* make sure that the bitstring fits. Note that the number of infered * make sure that the bitstring fits.
* 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
*/ */
if (atttypmod <= 0) if (atttypmod <= 0)
atttypmod = bitlen; atttypmod = bitlen;
else if (bit_not_hex ? (bitlen > atttypmod) : (bitlen > atttypmod + 3)) else if (bitlen != atttypmod)
elog(ERROR, "zpbit_in: bit string too long for bit(%d)", elog(ERROR, "bit string length does not match type bit(%d)",
atttypmod); atttypmod);
len = VARBITTOTALLEN(atttypmod); len = VARBITTOTALLEN(atttypmod);
...@@ -128,7 +120,7 @@ zpbit_in(PG_FUNCTION_ARGS) ...@@ -128,7 +120,7 @@ zpbit_in(PG_FUNCTION_ARGS)
if (*sp == '1') if (*sp == '1')
*r |= x; *r |= x;
else if (*sp != '0') 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; x >>= 1;
if (x == 0) if (x == 0)
{ {
...@@ -149,7 +141,7 @@ zpbit_in(PG_FUNCTION_ARGS) ...@@ -149,7 +141,7 @@ zpbit_in(PG_FUNCTION_ARGS)
else if (*sp >= 'a' && *sp <= 'f') else if (*sp >= 'a' && *sp <= 'f')
x = (bits8) (*sp - 'a') + 10; x = (bits8) (*sp - 'a') + 10;
else else
elog(ERROR, "Cannot parse %c as a hex digit", *sp); elog(ERROR, "cannot parse %c as a hex digit", *sp);
if (bc) if (bc)
{ {
*r++ |= x; *r++ |= x;
...@@ -163,31 +155,12 @@ zpbit_in(PG_FUNCTION_ARGS) ...@@ -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); 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 Datum
zpbit_out(PG_FUNCTION_ARGS) bit_out(PG_FUNCTION_ARGS)
{ {
#if 1 #if 1
/* same as varbit output */ /* same as varbit output */
...@@ -228,69 +201,59 @@ zpbit_out(PG_FUNCTION_ARGS) ...@@ -228,69 +201,59 @@ zpbit_out(PG_FUNCTION_ARGS)
#endif #endif
} }
/* zpbit() /* bit()
* Converts a bit() type to a specific internal length. * Converts a bit() type to a specific internal length.
* len is the bitlength specified in the column definition. * len is the bitlength specified in the column definition.
*/ */
Datum Datum
zpbit(PG_FUNCTION_ARGS) bit(PG_FUNCTION_ARGS)
{ {
VarBit *arg = PG_GETARG_VARBIT_P(0); VarBit *arg = PG_GETARG_VARBIT_P(0);
int32 len = PG_GETARG_INT32(1); int32 len = PG_GETARG_INT32(1);
VarBit *result;
int rlen;
/* No work if typmod is invalid or supplied data matches it already */ /* No work if typmod is invalid or supplied data matches it already */
if (len <= 0 || len == VARBITLEN(arg)) if (len <= 0 || len == VARBITLEN(arg))
PG_RETURN_VARBIT_P(arg); PG_RETURN_VARBIT_P(arg);
else
rlen = VARBITTOTALLEN(len); elog(ERROR, "bit string length does not match type bit(%d)",
result = (VarBit *) palloc(rlen); len);
/* set to 0 so that result is zero-padded if input is shorter */ return 0; /* quiet compiler */
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);
} }
/* _zpbit() /* _bit()
* Converts an array of bit() elements to a specific internal length. * Converts an array of bit() elements to a specific internal length.
* len is the bitlength specified in the column definition. * len is the bitlength specified in the column definition.
*/ */
Datum Datum
_zpbit(PG_FUNCTION_ARGS) _bit(PG_FUNCTION_ARGS)
{ {
ArrayType *v = (ArrayType *) PG_GETARG_VARLENA_P(0); ArrayType *v = (ArrayType *) PG_GETARG_VARLENA_P(0);
int32 len = PG_GETARG_INT32(1); int32 len = PG_GETARG_INT32(1);
FunctionCallInfoData locfcinfo; 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. * it up once per run.
*/ */
static FmgrInfo zpbit_finfo; static FmgrInfo bit_finfo;
if (zpbit_finfo.fn_oid == InvalidOid) if (bit_finfo.fn_oid == InvalidOid)
fmgr_info(F_ZPBIT, &zpbit_finfo); fmgr_info(F_BIT, &bit_finfo);
MemSet(&locfcinfo, 0, sizeof(locfcinfo)); MemSet(&locfcinfo, 0, sizeof(locfcinfo));
locfcinfo.flinfo = &zpbit_finfo; locfcinfo.flinfo = &bit_finfo;
locfcinfo.nargs = 2; locfcinfo.nargs = 2;
/* We assume we are "strict" and need not worry about null inputs */ /* We assume we are "strict" and need not worry about null inputs */
locfcinfo.arg[0] = PointerGetDatum(v); locfcinfo.arg[0] = PointerGetDatum(v);
locfcinfo.arg[1] = Int32GetDatum(len); locfcinfo.arg[1] = Int32GetDatum(len);
return array_map(&locfcinfo, ZPBITOID, ZPBITOID); return array_map(&locfcinfo, BITOID, BITOID);
} }
/* /*
* varbit_in - * varbit_in -
* converts a string to the internal representation of a bitstring. * 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. * the maximum length, not the exact length to force the bitstring to.
*/ */
Datum Datum
...@@ -310,8 +273,7 @@ varbit_in(PG_FUNCTION_ARGS) ...@@ -310,8 +273,7 @@ varbit_in(PG_FUNCTION_ARGS)
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 */
bool bit_not_hex; /* false = hex string true = bit string */ bool bit_not_hex; /* false = hex string true = bit string */
int bc, int bc;
ipad;
bits8 x = 0; bits8 x = 0;
/* Check that the first character is a b or an x */ /* Check that the first character is a b or an x */
...@@ -340,15 +302,12 @@ varbit_in(PG_FUNCTION_ARGS) ...@@ -340,15 +302,12 @@ varbit_in(PG_FUNCTION_ARGS)
/* /*
* Sometimes atttypmod is not supplied. If it is supplied we need to * Sometimes atttypmod is not supplied. If it is supplied we need to
* make sure that the bitstring fits. Note that the number of infered * make sure that the bitstring fits.
* 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
*/ */
if (atttypmod <= 0) if (atttypmod <= 0)
atttypmod = bitlen; atttypmod = bitlen;
else if (bit_not_hex ? (bitlen > atttypmod) : (bitlen > atttypmod + 3)) else if (bitlen > atttypmod)
elog(ERROR, "varbit_in: bit string too long for bit varying(%d)", elog(ERROR, "bit string too long for type bit varying(%d)",
atttypmod); atttypmod);
len = VARBITTOTALLEN(bitlen); len = VARBITTOTALLEN(bitlen);
...@@ -369,7 +328,7 @@ varbit_in(PG_FUNCTION_ARGS) ...@@ -369,7 +328,7 @@ varbit_in(PG_FUNCTION_ARGS)
if (*sp == '1') if (*sp == '1')
*r |= x; *r |= x;
else if (*sp != '0') 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; x >>= 1;
if (x == 0) if (x == 0)
{ {
...@@ -390,7 +349,7 @@ varbit_in(PG_FUNCTION_ARGS) ...@@ -390,7 +349,7 @@ varbit_in(PG_FUNCTION_ARGS)
else if (*sp >= 'a' && *sp <= 'f') else if (*sp >= 'a' && *sp <= 'f')
x = (bits8) (*sp - 'a') + 10; x = (bits8) (*sp - 'a') + 10;
else else
elog(ERROR, "Cannot parse %c as a hex digit", *sp); elog(ERROR, "cannot parse %c as a hex digit", *sp);
if (bc) if (bc)
{ {
*r++ |= x; *r++ |= x;
...@@ -404,21 +363,6 @@ varbit_in(PG_FUNCTION_ARGS) ...@@ -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); PG_RETURN_VARBIT_P(result);
} }
...@@ -477,6 +421,9 @@ varbit(PG_FUNCTION_ARGS) ...@@ -477,6 +421,9 @@ varbit(PG_FUNCTION_ARGS)
if (len <= 0 || len >= VARBITLEN(arg)) if (len <= 0 || len >= VARBITLEN(arg))
PG_RETURN_VARBIT_P(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); rlen = VARBITTOTALLEN(len);
result = (VarBit *) palloc(rlen); result = (VarBit *) palloc(rlen);
VARATT_SIZEP(result) = rlen; VARATT_SIZEP(result) = rlen;
...@@ -868,7 +815,7 @@ bitand(PG_FUNCTION_ARGS) ...@@ -868,7 +815,7 @@ bitand(PG_FUNCTION_ARGS)
bitlen1 = VARBITLEN(arg1); bitlen1 = VARBITLEN(arg1);
bitlen2 = VARBITLEN(arg2); bitlen2 = VARBITLEN(arg2);
if (bitlen1 != bitlen2) if (bitlen1 != bitlen2)
elog(ERROR, "bitand: Cannot AND bitstrings of different sizes"); elog(ERROR, "cannot AND bit strings of different sizes");
len = VARSIZE(arg1); len = VARSIZE(arg1);
result = (VarBit *) palloc(len); result = (VarBit *) palloc(len);
VARATT_SIZEP(result) = len; VARATT_SIZEP(result) = len;
...@@ -906,7 +853,7 @@ bitor(PG_FUNCTION_ARGS) ...@@ -906,7 +853,7 @@ bitor(PG_FUNCTION_ARGS)
bitlen1 = VARBITLEN(arg1); bitlen1 = VARBITLEN(arg1);
bitlen2 = VARBITLEN(arg2); bitlen2 = VARBITLEN(arg2);
if (bitlen1 != bitlen2) if (bitlen1 != bitlen2)
elog(ERROR, "bitor: Cannot OR bitstrings of different sizes"); elog(ERROR, "cannot OR bit strings of different sizes");
len = VARSIZE(arg1); len = VARSIZE(arg1);
result = (VarBit *) palloc(len); result = (VarBit *) palloc(len);
VARATT_SIZEP(result) = len; VARATT_SIZEP(result) = len;
...@@ -950,7 +897,7 @@ bitxor(PG_FUNCTION_ARGS) ...@@ -950,7 +897,7 @@ bitxor(PG_FUNCTION_ARGS)
bitlen1 = VARBITLEN(arg1); bitlen1 = VARBITLEN(arg1);
bitlen2 = VARBITLEN(arg2); bitlen2 = VARBITLEN(arg2);
if (bitlen1 != bitlen2) if (bitlen1 != bitlen2)
elog(ERROR, "bitxor: Cannot XOR bitstrings of different sizes"); elog(ERROR, "cannot XOR bit strings of different sizes");
len = VARSIZE(arg1); len = VARSIZE(arg1);
result = (VarBit *) palloc(len); result = (VarBit *) palloc(len);
VARATT_SIZEP(result) = len; VARATT_SIZEP(result) = len;
...@@ -1165,7 +1112,7 @@ bittoint4(PG_FUNCTION_ARGS) ...@@ -1165,7 +1112,7 @@ bittoint4(PG_FUNCTION_ARGS)
/* Check that the bit string is not too long */ /* Check that the bit string is not too long */
if (VARBITLEN(arg) > sizeof(int4) * BITS_PER_BYTE) 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; result = 0;
for (r = VARBITS(arg); r < VARBITEND(arg); r++) for (r = VARBITS(arg); r < VARBITEND(arg); r++)
{ {
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
* *
* *
* IDENTIFICATION * 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 * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
* *
...@@ -556,7 +556,7 @@ dumpClasses_dumpData(Archive *fout, char *oid, void *dctxv) ...@@ -556,7 +556,7 @@ dumpClasses_dumpData(Archive *fout, char *oid, void *dctxv)
archprintf(fout, "%s", archprintf(fout, "%s",
PQgetvalue(res, tuple, field)); PQgetvalue(res, tuple, field));
break; break;
case ZPBITOID: case BITOID:
case VARBITOID: case VARBITOID:
archprintf(fout, "B'%s'", archprintf(fout, "B'%s'",
PQgetvalue(res, tuple, field)); PQgetvalue(res, tuple, field));
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * 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 @@ ...@@ -53,6 +53,6 @@
*/ */
/* yyyymmddN */ /* yyyymmddN */
#define CATALOG_VERSION_NO 200105211 #define CATALOG_VERSION_NO 200105221
#endif #endif
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * 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 * NOTES
* The script catalog/genbki.sh reads this file and generates .bki * The script catalog/genbki.sh reads this file and generates .bki
...@@ -1975,9 +1975,9 @@ DESCR("# points in path"); ...@@ -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 - )); 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"); 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)"); 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)"); DESCR("(internal)");
DATA(insert OID = 1569 ( like PGUID 12 f t t t 2 f 16 "25 25" 100 0 0 100 textlike - )); 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"); ...@@ -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 - )); DATA(insert OID = 1684 ( bittoint4 PGUID 12 f t t t 1 f 23 "1560" 100 0 0 100 bittoint4 - ));
DESCR("bitstring to int4"); 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"); 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"); 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 - )); 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"); DESCR("adjust varbit() to typmod length");
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * 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 * NOTES
* the genbki.sh script reads this file and generates .bki * the genbki.sh script reads this file and generates .bki
...@@ -397,9 +397,9 @@ DESCR("hh:mm:ss, ANSI SQL time"); ...@@ -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_ )); 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 */ /* 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"); 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 = 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_ )); 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"); DESCR("variable-length bit string");
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * 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 ...@@ -69,7 +69,7 @@ typedef enum CATEGORY
|| ((t) == CIRCLEOID) \ || ((t) == CIRCLEOID) \
|| ((t) == INETOID) \ || ((t) == INETOID) \
|| ((t) == CIDROID) \ || ((t) == CIDROID) \
|| ((t) == ZPBITOID) \ || ((t) == BITOID) \
|| ((t) == VARBITOID) ) || ((t) == VARBITOID) )
...@@ -101,8 +101,8 @@ typedef enum CATEGORY ...@@ -101,8 +101,8 @@ typedef enum CATEGORY
|| ((a) == INT4OID && (b) == RELTIMEOID) \ || ((a) == INT4OID && (b) == RELTIMEOID) \
|| ((a) == INETOID && (b) == CIDROID) \ || ((a) == INETOID && (b) == CIDROID) \
|| ((a) == CIDROID && (b) == INETOID) \ || ((a) == CIDROID && (b) == INETOID) \
|| ((a) == ZPBITOID && (b) == VARBITOID) \ || ((a) == BITOID && (b) == VARBITOID) \
|| ((a) == VARBITOID && (b) == ZPBITOID)) || ((a) == VARBITOID && (b) == BITOID))
/* IS_HIGHER_TYPE() /* IS_HIGHER_TYPE()
* These types are the most general in each of the type categories. * These types are the most general in each of the type categories.
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * 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 ...@@ -61,12 +61,12 @@ typedef struct
#define BITHIGH 0x80 #define BITHIGH 0x80
extern Datum zpbit_in(PG_FUNCTION_ARGS); extern Datum bit_in(PG_FUNCTION_ARGS);
extern Datum zpbit_out(PG_FUNCTION_ARGS); extern Datum bit_out(PG_FUNCTION_ARGS);
extern Datum varbit_in(PG_FUNCTION_ARGS); extern Datum varbit_in(PG_FUNCTION_ARGS);
extern Datum varbit_out(PG_FUNCTION_ARGS); extern Datum varbit_out(PG_FUNCTION_ARGS);
extern Datum zpbit(PG_FUNCTION_ARGS); extern Datum bit(PG_FUNCTION_ARGS);
extern Datum _zpbit(PG_FUNCTION_ARGS); extern Datum _bit(PG_FUNCTION_ARGS);
extern Datum varbit(PG_FUNCTION_ARGS); extern Datum varbit(PG_FUNCTION_ARGS);
extern Datum _varbit(PG_FUNCTION_ARGS); extern Datum _varbit(PG_FUNCTION_ARGS);
extern Datum biteq(PG_FUNCTION_ARGS); extern Datum biteq(PG_FUNCTION_ARGS);
......
This diff is collapsed.
...@@ -6,17 +6,17 @@ ...@@ -6,17 +6,17 @@
-- Build tables for testing -- 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 BIT_TABLE VALUES (B'10'); -- too short
INSERT INTO ZPBIT_TABLE VALUES (B'0'); INSERT INTO BIT_TABLE VALUES (B'00000000000');
INSERT INTO ZPBIT_TABLE VALUES (B'11011'); INSERT INTO BIT_TABLE VALUES (B'11011000000');
INSERT INTO ZPBIT_TABLE VALUES (B'01010101010'); INSERT INTO BIT_TABLE VALUES (B'01010101010');
INSERT INTO ZPBIT_TABLE VALUES (B'101011111010'); -- too long INSERT INTO BIT_TABLE VALUES (B'101011111010'); -- too long
--INSERT INTO ZPBIT_TABLE VALUES ('X554'); --INSERT INTO BIT_TABLE VALUES ('X554');
--INSERT INTO ZPBIT_TABLE VALUES ('X555'); --INSERT INTO BIT_TABLE VALUES ('X555');
SELECT * FROM ZPBIT_TABLE; SELECT * FROM BIT_TABLE;
CREATE TABLE VARBIT_TABLE(v BIT VARYING(11)); CREATE TABLE VARBIT_TABLE(v BIT VARYING(11));
...@@ -32,12 +32,12 @@ SELECT * FROM VARBIT_TABLE; ...@@ -32,12 +32,12 @@ SELECT * FROM VARBIT_TABLE;
-- Concatenation -- Concatenation
SELECT v, b, (v || b) AS concat SELECT v, b, (v || b) AS concat
FROM ZPBIT_TABLE, VARBIT_TABLE FROM BIT_TABLE, VARBIT_TABLE
ORDER BY 3; ORDER BY 3;
-- Length -- Length
SELECT b, length(b) AS lb SELECT b, length(b) AS lb
FROM ZPBIT_TABLE; FROM BIT_TABLE;
SELECT v, length(v) AS lv SELECT v, length(v) AS lv
FROM VARBIT_TABLE; FROM VARBIT_TABLE;
...@@ -46,7 +46,7 @@ SELECT b, ...@@ -46,7 +46,7 @@ SELECT b,
SUBSTRING(b FROM 2 FOR 4) AS sub_2_4, SUBSTRING(b FROM 2 FOR 4) AS sub_2_4,
SUBSTRING(b FROM 7 FOR 13) AS sub_7_13, SUBSTRING(b FROM 7 FOR 13) AS sub_7_13,
SUBSTRING(b FROM 6) AS sub_6 SUBSTRING(b FROM 6) AS sub_6
FROM ZPBIT_TABLE; FROM BIT_TABLE;
SELECT v, SELECT v,
SUBSTRING(v FROM 2 FOR 4) AS sub_2_4, SUBSTRING(v FROM 2 FOR 4) AS sub_2_4,
SUBSTRING(v FROM 7 FOR 13) AS sub_7_13, 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; ...@@ -78,14 +78,14 @@ SELECT a,a<<4 AS "a<<4",b,b>>2 AS "b>>2" FROM varbit_table;
DROP TABLE varbit_table; DROP TABLE varbit_table;
--- Bit operations --- Bit operations
DROP TABLE zpbit_table; DROP TABLE bit_table;
CREATE TABLE zpbit_table (a BIT(16), b BIT(16)); CREATE TABLE bit_table (a BIT(16), b BIT(16));
COPY zpbit_table FROM stdin; COPY bit_table FROM stdin;
X0F X10 X0F00 X1000
X1F X11 X1F00 X1100
X2F X12 X2F00 X1200
X3F X13 X3F00 X1300
X8F X04 X8F00 X0400
X000F X0010 X000F X0010
X0123 XFFFF X0123 XFFFF
X2468 X2468 X2468 X2468
...@@ -94,12 +94,12 @@ X1234 XFFF5 ...@@ -94,12 +94,12 @@ X1234 XFFF5
\. \.
SELECT a,b,~a AS "~ a",a & b AS "a & b", 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", 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 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 zpbit_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 -- The following should fail
...@@ -158,29 +158,29 @@ SELECT POSITION(B'0000000000011101011111010110' IN B'000000000011101011111010110 ...@@ -158,29 +158,29 @@ SELECT POSITION(B'0000000000011101011111010110' IN B'000000000011101011111010110
-- Shifting -- Shifting
CREATE TABLE ZPBIT_SHIFT_TABLE(b BIT(16)); CREATE TABLE BIT_SHIFT_TABLE(b BIT(16));
INSERT INTO ZPBIT_SHIFT_TABLE VALUES (B'11011'); INSERT INTO BIT_SHIFT_TABLE VALUES (B'1101100000000000');
INSERT INTO ZPBIT_SHIFT_TABLE SELECT b>>1 FROM ZPBIT_SHIFT_TABLE; INSERT INTO BIT_SHIFT_TABLE SELECT b>>1 FROM BIT_SHIFT_TABLE;
INSERT INTO ZPBIT_SHIFT_TABLE SELECT b>>2 FROM ZPBIT_SHIFT_TABLE; INSERT INTO BIT_SHIFT_TABLE SELECT b>>2 FROM BIT_SHIFT_TABLE;
INSERT INTO ZPBIT_SHIFT_TABLE SELECT b>>4 FROM ZPBIT_SHIFT_TABLE; INSERT INTO BIT_SHIFT_TABLE SELECT b>>4 FROM BIT_SHIFT_TABLE;
INSERT INTO ZPBIT_SHIFT_TABLE SELECT b>>8 FROM ZPBIT_SHIFT_TABLE; INSERT INTO BIT_SHIFT_TABLE SELECT b>>8 FROM BIT_SHIFT_TABLE;
SELECT POSITION(B'1101'IN b), SELECT POSITION(B'1101' IN b),
POSITION(B'11011' IN b), POSITION(B'11011' IN b),
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 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 || B'0' AS BIT VARYING(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 || B'00' AS BIT VARYING(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 || B'0000' AS BIT VARYING(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'00000000' AS BIT VARYING(20)) >>8 FROM VARBIT_SHIFT_TABLE;
SELECT POSITION(B'1101' IN v), SELECT POSITION(B'1101' IN v),
POSITION(B'11011' IN v), POSITION(B'11011' IN v),
v v
FROM VARBIT_SHIFT_TABLE ; FROM VARBIT_SHIFT_TABLE ;
DROP TABLE ZPBIT_SHIFT_TABLE; DROP TABLE BIT_SHIFT_TABLE;
DROP TABLE VARBIT_SHIFT_TABLE; DROP TABLE VARBIT_SHIFT_TABLE;
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