Commit 1637684a authored by Bruce Momjian's avatar Bruce Momjian

Cleanup getattr code. Make CHAR() use attcacheoff.

parent 60f54d62
/*------------------------------------------------------------------------- /*-------------------------------------------------------------------------
* *
* heaptuple.c-- * heaptuple.c--
* This file contains heap tuple accessor and mutator routines, as well * This file contains heap tuple accessor and mutator routines, as well
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/access/common/heaptuple.c,v 1.31 1998/01/31 04:38:02 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/access/common/heaptuple.c,v 1.32 1998/02/04 21:32:08 momjian Exp $
* *
* NOTES * NOTES
* The old interface functions have been converted to macros * The old interface functions have been converted to macros
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <access/htup.h> #include <access/htup.h>
#include <access/transam.h> #include <access/transam.h>
#include <access/tupmacs.h> #include <access/tupmacs.h>
#include <catalog/pg_type.h>
#include <storage/bufpage.h> #include <storage/bufpage.h>
#include <utils/memutils.h> #include <utils/memutils.h>
...@@ -435,7 +436,6 @@ nocachegetattr(HeapTuple tup, ...@@ -435,7 +436,6 @@ nocachegetattr(HeapTuple tup,
} }
else if (attnum == 0) else if (attnum == 0)
{ {
/* /*
* first attribute is always at position zero * first attribute is always at position zero
*/ */
...@@ -443,8 +443,6 @@ nocachegetattr(HeapTuple tup, ...@@ -443,8 +443,6 @@ nocachegetattr(HeapTuple tup,
} }
#endif #endif
tp = (char *) tup + tup->t_hoff;
slow = 0; slow = 0;
} }
else else
...@@ -478,18 +476,38 @@ nocachegetattr(HeapTuple tup, ...@@ -478,18 +476,38 @@ nocachegetattr(HeapTuple tup,
* Now check to see if any preceeding bits are null... * Now check to see if any preceeding bits are null...
* ---------------- * ----------------
*/ */
{ {
register int i = 0; /* current offset in bp */ register int i = 0; /* current offset in bp */
register int mask; /* bit in byte we're looking at */
register char n; /* current byte in bp */
register int byte,
finalbit;
for (i = 0; i < attnum && !slow; i++) byte = attnum >> 3;
finalbit = attnum & 0x07;
for (; i <= byte && !slow; i++)
{ {
if (att_isnull(i, bp)) n = bp[i];
slow = 1; if (i < byte)
{
/* check for nulls in any "earlier" bytes */
if ((~n) != 0)
slow=1;
}
else
{
/* check for nulls "before" final bit of last byte */
mask = (1 << finalbit) - 1;
if ((~n) & mask)
slow=1;
}
} }
} }
} }
tp = (char *) tup + tup->t_hoff;
/* /*
* now check for any non-fixed length attrs before our attribute * now check for any non-fixed length attrs before our attribute
*/ */
...@@ -497,21 +515,19 @@ nocachegetattr(HeapTuple tup, ...@@ -497,21 +515,19 @@ nocachegetattr(HeapTuple tup,
{ {
if (att[attnum]->attcacheoff > 0) if (att[attnum]->attcacheoff > 0)
{ {
return (Datum) return (Datum)fetchatt(&(att[attnum]),
fetchatt(&(att[attnum]), tp + att[attnum]->attcacheoff);
tp + att[attnum]->attcacheoff);
} }
else if (attnum == 0) else if (attnum == 0)
{ {
return (Datum) return ((Datum) fetchatt(&(att[0]), (char *) tp));
fetchatt(&(att[0]), (char *) tup + tup->t_hoff);
} }
else if (!HeapTupleAllFixed(tup)) else if (!HeapTupleAllFixed(tup))
{ {
register int j = 0; register int j = 0;
for (j = 0; j < attnum && !slow; j++) for (j = 0; j < attnum && !slow; j++)
if (att[j]->attlen < 1) if (att[j]->attlen < 1 && !VARLENA_FIXED_SIZE(att[j]))
slow = 1; slow = 1;
} }
} }
...@@ -535,10 +551,18 @@ nocachegetattr(HeapTuple tup, ...@@ -535,10 +551,18 @@ nocachegetattr(HeapTuple tup,
while (att[j]->attcacheoff > 0) while (att[j]->attcacheoff > 0)
j++; j++;
off = att[j - 1]->attcacheoff + att[j - 1]->attlen; if (!VARLENA_FIXED_SIZE(att[j]))
off = att[j - 1]->attcacheoff + att[j - 1]->attlen;
else
off = att[j - 1]->attcacheoff + att[j - 1]->atttypmod;
for (; j < attnum + 1; j++) for (; j < attnum + 1; j++)
{ {
/*
* Fix me when going to a machine with more than a four-byte
* word!
*/
switch (att[j]->attlen) switch (att[j]->attlen)
{ {
case -1: case -1:
...@@ -554,25 +578,28 @@ nocachegetattr(HeapTuple tup, ...@@ -554,25 +578,28 @@ nocachegetattr(HeapTuple tup,
off = INTALIGN(off); off = INTALIGN(off);
break; break;
default: default:
if (att[j]->attlen < sizeof(int32)) if (att[j]->attlen > sizeof(int32))
{ off = (att[j]->attalign == 'd') ?
elog(ERROR, DOUBLEALIGN(off) : LONGALIGN(off);
"nocachegetattr: attribute %d has len %d",
j, att[j]->attlen);
}
if (att[j]->attalign == 'd')
off = DOUBLEALIGN(off);
else else
off = LONGALIGN(off); elog(ERROR, "nocache_index_getattr: attribute %d has len %d",
j, att[j]->attlen);
break; break;
} }
att[j]->attcacheoff = off; att[j]->attcacheoff = off;
off += att[j]->attlen;
/* The only varlena/-1 length value to get here is this */
if (!VARLENA_FIXED_SIZE(att[j]))
off += att[j]->attlen;
else
{
Assert(att[j]->atttypmod == VARSIZE(tp + off));
off += att[j]->atttypmod;
}
} }
return return (Datum) fetchatt(&(att[attnum]), tp + att[attnum]->attcacheoff);
(Datum) fetchatt(&(att[attnum]), tp + att[attnum]->attcacheoff);
} }
else else
{ {
...@@ -600,41 +627,37 @@ nocachegetattr(HeapTuple tup, ...@@ -600,41 +627,37 @@ nocachegetattr(HeapTuple tup,
continue; continue;
} }
} }
switch (att[i]->attlen)
{ /* If we know the next offset, we can skip the rest */
case -1:
off = (att[i]->attalign == 'd') ?
DOUBLEALIGN(off) : INTALIGN(off);
break;
case sizeof(char):
break;
case sizeof(short):
off = SHORTALIGN(off);
break;
case sizeof(int32):
off = INTALIGN(off);
break;
default:
if (att[i]->attlen < sizeof(int32))
elog(ERROR,
"nocachegetattr2: attribute %d has len %d",
i, att[i]->attlen);
if (att[i]->attalign == 'd')
off = DOUBLEALIGN(off);
else
off = LONGALIGN(off);
break;
}
if (usecache && att[i]->attcacheoff > 0) if (usecache && att[i]->attcacheoff > 0)
{
off = att[i]->attcacheoff; off = att[i]->attcacheoff;
if (att[i]->attlen == -1)
{
usecache = false;
}
}
else else
{ {
switch (att[i]->attlen)
{
case -1:
off = (att[i]->attalign == 'd') ?
DOUBLEALIGN(off) : INTALIGN(off);
break;
case sizeof(char):
break;
case sizeof(short):
off = SHORTALIGN(off);
break;
case sizeof(int32):
off = INTALIGN(off);
break;
default:
if (att[i]->attlen < sizeof(int32))
elog(ERROR,
"nocachegetattr2: attribute %d has len %d",
i, att[i]->attlen);
if (att[i]->attalign == 'd')
off = DOUBLEALIGN(off);
else
off = LONGALIGN(off);
break;
}
if (usecache) if (usecache)
att[i]->attcacheoff = off; att[i]->attcacheoff = off;
} }
...@@ -644,21 +667,25 @@ nocachegetattr(HeapTuple tup, ...@@ -644,21 +667,25 @@ nocachegetattr(HeapTuple tup,
case sizeof(char): case sizeof(char):
off++; off++;
break; break;
case sizeof(int16): case sizeof(short):
off += sizeof(int16); off += sizeof(short);
break; break;
case sizeof(int32): case sizeof(int32):
off += sizeof(int32); off += sizeof(int32);
break; break;
case -1: case -1:
usecache = false; Assert(!VARLENA_FIXED_SIZE(att[i]) ||
att[i]->atttypmod == VARSIZE(tp + off));
off += VARSIZE(tp + off); off += VARSIZE(tp + off);
if (!VARLENA_FIXED_SIZE(att[i]))
usecache = false;
break; break;
default: default:
off += att[i]->attlen; off += att[i]->attlen;
break; break;
} }
} }
switch (att[attnum]->attlen) switch (att[attnum]->attlen)
{ {
case -1: case -1:
...@@ -683,7 +710,8 @@ nocachegetattr(HeapTuple tup, ...@@ -683,7 +710,8 @@ nocachegetattr(HeapTuple tup,
off = LONGALIGN(off); off = LONGALIGN(off);
break; break;
} }
return ((Datum) fetchatt(&(att[attnum]), tp + off));
return (Datum) fetchatt(&(att[attnum]), tp + off);
} }
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/access/common/indextuple.c,v 1.23 1998/01/31 04:38:03 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/access/common/indextuple.c,v 1.24 1998/02/04 21:32:09 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <access/ibit.h> #include <access/ibit.h>
#include <access/itup.h> #include <access/itup.h>
#include <access/tupmacs.h> #include <access/tupmacs.h>
#include <catalog/pg_type.h>
#ifndef HAVE_MEMMOVE #ifndef HAVE_MEMMOVE
#include <regex/utils.h> #include <regex/utils.h>
...@@ -188,8 +189,6 @@ nocache_index_getattr(IndexTuple tup, ...@@ -188,8 +189,6 @@ nocache_index_getattr(IndexTuple tup,
} }
#endif #endif
tp = (char *) tup + data_off;
slow = 0; slow = 0;
} }
else else
...@@ -224,35 +223,33 @@ nocache_index_getattr(IndexTuple tup, ...@@ -224,35 +223,33 @@ nocache_index_getattr(IndexTuple tup,
register int mask; /* bit in byte we're looking at */ register int mask; /* bit in byte we're looking at */
register char n; /* current byte in bp */ register char n; /* current byte in bp */
register int byte, register int byte,
finalbit; finalbit;
byte = attnum >> 3; byte = attnum >> 3;
finalbit = attnum & 0x07; finalbit = attnum & 0x07;
for (; i <= byte; i++) for (; i <= byte && !slow; i++)
{ {
n = bp[i]; n = bp[i];
if (i < byte) if (i < byte)
{ {
/* check for nulls in any "earlier" bytes */ /* check for nulls in any "earlier" bytes */
if ((~n) != 0) if ((~n) != 0)
{ slow=1;
slow++;
break;
}
} }
else else
{ {
/* check for nulls "before" final bit of last byte */ /* check for nulls "before" final bit of last byte */
mask = (finalbit << 1) - 1; mask = (1 << finalbit) - 1;
if ((~n) & mask) if ((~n) & mask)
slow++; slow=1;
} }
} }
} }
tp = (char *) tup + data_off;
} }
tp = (char *) tup + data_off;
/* now check for any non-fixed length attrs before our attribute */ /* now check for any non-fixed length attrs before our attribute */
if (!slow) if (!slow)
...@@ -262,12 +259,16 @@ nocache_index_getattr(IndexTuple tup, ...@@ -262,12 +259,16 @@ nocache_index_getattr(IndexTuple tup,
return (Datum) fetchatt(&(att[attnum]), return (Datum) fetchatt(&(att[attnum]),
tp + att[attnum]->attcacheoff); tp + att[attnum]->attcacheoff);
} }
else if (attnum == 0)
{
return ((Datum) fetchatt(&(att[0]), (char *) tp));
}
else if (!IndexTupleAllFixed(tup)) else if (!IndexTupleAllFixed(tup))
{ {
register int j = 0; register int j = 0;
for (j = 0; j < attnum && !slow; j++) for (j = 0; j < attnum && !slow; j++)
if (att[j]->attlen < 1) if (att[j]->attlen < 1 && !VARLENA_FIXED_SIZE(att[j]))
slow = 1; slow = 1;
} }
} }
...@@ -292,12 +293,13 @@ nocache_index_getattr(IndexTuple tup, ...@@ -292,12 +293,13 @@ nocache_index_getattr(IndexTuple tup,
while (att[j]->attcacheoff > 0) while (att[j]->attcacheoff > 0)
j++; j++;
off = att[j - 1]->attcacheoff + if (!VARLENA_FIXED_SIZE(att[j]))
att[j - 1]->attlen; off = att[j - 1]->attcacheoff + att[j - 1]->attlen;
else
off = att[j - 1]->attcacheoff + att[j - 1]->atttypmod;
for (; j < attnum + 1; j++) for (; j < attnum + 1; j++)
{ {
/* /*
* Fix me when going to a machine with more than a four-byte * Fix me when going to a machine with more than a four-byte
* word! * word!
...@@ -329,11 +331,18 @@ nocache_index_getattr(IndexTuple tup, ...@@ -329,11 +331,18 @@ nocache_index_getattr(IndexTuple tup,
} }
att[j]->attcacheoff = off; att[j]->attcacheoff = off;
off += att[j]->attlen;
/* The only varlena/-1 length value to get here is this */
if (!VARLENA_FIXED_SIZE(att[j]))
off += att[j]->attlen;
else
{
Assert(att[j]->atttypmod == VARSIZE(tp + off));
off += att[j]->atttypmod;
}
} }
return (Datum) fetchatt(&(att[attnum]), return (Datum) fetchatt(&(att[attnum]), tp + att[attnum]->attcacheoff);
tp + att[attnum]->attcacheoff);
} }
else else
{ {
...@@ -356,51 +365,64 @@ nocache_index_getattr(IndexTuple tup, ...@@ -356,51 +365,64 @@ nocache_index_getattr(IndexTuple tup,
} }
} }
/* If we know the next offset, we can skip the rest */
if (usecache && att[i]->attcacheoff > 0) if (usecache && att[i]->attcacheoff > 0)
{
off = att[i]->attcacheoff; off = att[i]->attcacheoff;
if (att[i]->attlen == -1) else
usecache = false; {
else switch (att[i]->attlen)
continue; {
case -1:
off = (att[i]->attalign == 'd') ?
DOUBLEALIGN(off) : INTALIGN(off);
break;
case sizeof(char):
break;
case sizeof(short):
off = SHORTALIGN(off);
break;
case sizeof(int32):
off = INTALIGN(off);
break;
default:
if (att[i]->attlen < sizeof(int32))
elog(ERROR,
"nocachegetiattr2: attribute %d has len %d",
i, att[i]->attlen);
if (att[i]->attalign == 'd')
off = DOUBLEALIGN(off);
else
off = LONGALIGN(off);
break;
}
if (usecache)
att[i]->attcacheoff = off;
} }
if (usecache)
att[i]->attcacheoff = off;
switch (att[i]->attlen) switch (att[i]->attlen)
{ {
case sizeof(char): case sizeof(char):
off++; off++;
break; break;
case sizeof(short): case sizeof(short):
off = SHORTALIGN(off) +sizeof(short); off += sizeof(short);
break; break;
case sizeof(int32): case sizeof(int32):
off = INTALIGN(off) +sizeof(int32); off += sizeof(int32);
break; break;
case -1: case -1:
usecache = false; Assert(!VARLENA_FIXED_SIZE(att[i]) ||
off = (att[i]->attalign == 'd') ? att[i]->atttypmod == VARSIZE(tp + off));
DOUBLEALIGN(off) : INTALIGN(off);
off += VARSIZE(tp + off); off += VARSIZE(tp + off);
if (!VARLENA_FIXED_SIZE(att[i]))
usecache = false;
break; break;
default: default:
if (att[i]->attlen > sizeof(int32)) off += att[i]->attlen;
off = (att[i]->attalign == 'd') ?
DOUBLEALIGN(off) + att[i]->attlen :
LONGALIGN(off) + att[i]->attlen;
else
elog(ERROR, "nocache_index_getattr2: attribute %d has len %d",
i, att[i]->attlen);
break; break;
} }
} }
/*
* I don't know why this code was missed here! I've got it from
* heaptuple.c:nocachegetattr(). - vadim 06/12/97
*/
switch (att[attnum]->attlen) switch (att[attnum]->attlen)
{ {
case -1: case -1:
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* $Id: pg_type.h,v 1.30 1998/02/03 19:27:17 momjian Exp $ * $Id: pg_type.h,v 1.31 1998/02/04 21:32:12 momjian Exp $
* *
* NOTES * NOTES
* the genbki.sh script reads this file and generates .bki * the genbki.sh script reads this file and generates .bki
...@@ -366,7 +366,7 @@ DESCR("limited-range ISO-format date and time"); ...@@ -366,7 +366,7 @@ DESCR("limited-range ISO-format date and time");
#define USE_ATTTYPMOD(typeid) ((typeid) == BPCHAROID || (typeid) == VARCHAROID) #define USE_ATTTYPMOD(typeid) ((typeid) == BPCHAROID || (typeid) == VARCHAROID)
#define VARLENA_FIXED_SIZE(attr) (false && (attr)->atttypid == BPCHAROID && (attr)->atttypmod > 0) #define VARLENA_FIXED_SIZE(attr) ((attr)->atttypid == BPCHAROID && (attr)->atttypmod > 0)
/* /*
* prototypes for functions in pg_type.c * prototypes for functions in pg_type.c
......
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