agora inbox for postgres@postgres.berkeley.edu  
help / color / mirror / Atom feed
About "Queries over variable sized arrays" too!
4+ messages / 3 participants
[nested] [flat]

* About "Queries over variable sized arrays" too!
@ 1994-05-12 19:54 lmr <ren@math.ohio-state.edu>
  0 siblings, 0 replies; 4+ messages in thread

From: lmr @ 1994-05-12 19:54 UTC (permalink / raw)
  To: legacy

I worked on the exact same problem yesterday and thought about to post
yesterday but did not. 

I create a class by:

create manager (name=char16, employee=text[])\g
append manager (name="mike", employee="{"wei", "greg", "jeff"}")\g
append manager (name="tom", employee="{"bill", "joe", "ren", "jeff"}")\g

and want to write rule or C code to find the length of the array. The C code
I used:

int array_length(t)
TUPLE t;
{
  int i;
  char** names;
  bool isnull=FALSE;

  i=0;
  names=(char**)GetAttributeByName(t, "employee", &isnull);

  for(i=0; !isnull&& names[i]!=NULL; i++)
    {
      /*      printf("%s\n", names[i]);*/
    }
  return i;
}


/*
  define function array_length
  (language="c", returntype=int4)
  arg is (manager)
  as "/n/music/0/ren/postgres/array_length.o"\g
*/

It runs but gives incorrect result. 

*  retrieve (x=array_length(m)) from m in manager\g

Query sent to backend is "retrieve (x=array_length(m)) from m in manager"
---------------
| x           |
---------------
| 7           |
---------------
| 9           |
---------------

How does postgres encode the end of a variable array?
-- 
Liming Ren                       |  (614) 292-8989                   |  
Dept. of Mathematics             |  231 West 18th Avenue
The Ohio State University        |  Columbus, Ohio43210

===============================================================================
    To add/remove yourself from the POSTGRES mailing list: send mail with 
    the subject line ADD or DEL to "postgres-request@postgres.Berkeley.EDU"

    If this fails, send mail to "post_questions@postgres.Berkeley.EDU" and
    a human will deal with it.  DO NOT post to the "postgres" mailing list.
===============================================================================




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re:  About "Queries over variable sized arrays" too!
@ 1994-05-12 21:59 Keith Sklower <sklower@postgres.Berkeley.EDU>
  1994-05-13 17:43 ` Re: About "Queries over variable sized arrays" too! lmr <ren@math.ohio-state.edu>
  0 siblings, 1 reply; 4+ messages in thread

From: Keith Sklower @ 1994-05-12 21:59 UTC (permalink / raw)
  To: legacy; +Cc: ren@math.ohio-state.edu

This is a somewhat indirect response to Liming Ren's question
about encoding the length of a variable array.  I'll ask Sunita
to correct any outright mistakes, and see if we can sneak it into
the doc/implementation directory . . .



	       About Arrays in Postgres	v4.2

     The purpose of this note is to give a short overview of
the way	arrays are implemented,	and hopefully to yield
enough insight to writers of backend user-define adt code to
be able	to use direct access to	Postgres Multidimensional
Arrays.

     Arrays in Postgres	are implemented	in a few compatible
ways.  If the total size of the	array is small enough to be
fit on single page, the	an array attribute can be stored
like any other attribute.  However, many arrays	useful for
scientific computation exceed this limit, so array
attributes may be stored in Postgres large objects.

     Another enhancement is the	facility for rearranging the
way the	data is	stored to maximize access performance,
identifying subsections	or dimension frequently	accessed
together.  This	is called "Chunking", and is the subject of
Sunita Sarawagi's dissertation research, and is	discussed in
much greater detail elsewhere.

     All implementations share a common	header,	which is
defined	in src/backend/utils/adt/array.h.  There are some
utility	macros defined in this file, for convenient access.
Code implementing most array access is found in	arrayfuncs.c
and arrayutils.c, and the chunking code	is in chunk.c (also
in the utils/adt directory).

     The header	is a Postgres variable length object whose
initial	segment	is described by	the following C	structure
(remember, variable arrays don't work in C):

     typedef struct {
	     int     size;
	     int     ndim;	     /*	# of dimensions	*/
	     int     flags;	     /*	implementation flags */
     #ifdef for_show_only
     /*	     int     dims[ndim];     /*	size of	each dimension */
     /*	     int     lbounds[ndim];  /*	starting value for each	dim */
     #endif
     } ArrayType;

     Like any other VARLENA structure in postgres, the
4-byte size field is the total number of bytes to store
whatever portion of the	array is kept directly as a postgres
attribute in the classes themselves.  When the array
references a large object, the size field does not include
the number of bytes contained within the large object.	The
flags field encodes:

+    Whether the array data is stored externally in a large
     object.

+    The type of large object, if so.

+    Whether the array is chunked.

     There is no indication in the header of the underlying
type that the array is built on.  This must be obtained
through	the attribute structures.

     The common	header is followed by a	vector of 4-byte
integers giving	the size of each dimension, followed by	a
vector of 4-byte integers giving the lower bounds for each
dimension.  Unlike C, the default lower	bound is 1.

     In	the case where the data	is stored in a large object,
the vectors of information are followed	by a string name
giving the Postgres path name of the large object.
Otherwise the data directly follows the	object.

     One dimensional arrays of variable	size objects are
stored sequentially.  Thus, accessing individual elements of
an array of text may be	time consuming!	 For fixed size
data, the conventions are the same as C-language arrays,
with the last index cycling the	fastest.

An Example

     If	you wanted to compute the total	number of elements
in an array you	would write the	following C code:


#include "tmp/c.h"
#include "utils/log.h"
#include "utils/palloc.h"
#include "utils/adt/array.h"

int
ArEx(arg)
	ArrayType *arg;
{
	int ndims, *dimp, total;

	ndims =	ARR_NDIM(arg);
	dimp = ARR_DIMS(arg);

	for (total = *dimp++; --ndims >	0; )
		total *= dimp++;

	return (total);
}











===============================================================================
    To add/remove yourself from the POSTGRES mailing list: send mail with 
    the subject line ADD or DEL to "postgres-request@postgres.Berkeley.EDU"

    If this fails, send mail to "post_questions@postgres.Berkeley.EDU" and
    a human will deal with it.  DO NOT post to the "postgres" mailing list.
===============================================================================




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: About "Queries over variable sized arrays" too!
  1994-05-12 21:59 Re:  About "Queries over variable sized arrays" too! Keith Sklower <sklower@postgres.Berkeley.EDU>
@ 1994-05-13 17:43 ` lmr <ren@math.ohio-state.edu>
  0 siblings, 0 replies; 4+ messages in thread

From: lmr @ 1994-05-13 17:43 UTC (permalink / raw)
  To: Keith Sklower <sklower@postgres.Berkeley.EDU>; +Cc: postgres@

> This is a somewhat indirect response to Liming Ren's question
> about encoding the length of a variable array.  I'll ask Sunita
> to correct any outright mistakes, and see if we can sneak it into
> the doc/implementation directory . . .
> 
> 
> 
> 	       About Arrays in Postgres	v4.2


Thank you for your time and post. It did not solve my problem completely.
I am using v4.1. How do I do the same thing with v4.1? Or maybe v4.1 does
not support this?


Many thanks!
-- 
Liming Ren                       | 
Dept. of Mathematics             |  231 West 18th Avenue
The Ohio State University        |  Columbus, Ohio43210

===============================================================================
    To add/remove yourself from the POSTGRES mailing list: send mail with 
    the subject line ADD or DEL to "postgres-request@postgres.Berkeley.EDU"

    If this fails, send mail to "post_questions@postgres.Berkeley.EDU" and
    a human will deal with it.  DO NOT post to the "postgres" mailing list.
===============================================================================




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: About "Queries over variable sized arrays" too!
@ 1994-05-13 20:03 Keith Sklower <sklower@vangogh.CS.Berkeley.EDU>
  0 siblings, 0 replies; 4+ messages in thread

From: Keith Sklower @ 1994-05-13 20:03 UTC (permalink / raw)
  To: ren@math.ohio-state.edu; +Cc: legacy

I shoulda looked before I leaped.  I spent 3 hours which I didn't
have writing the document .... sorry.

I also now just spent 20 minutes looking at the 4.1 code.
4.1 arrays have to fit in a page, aren't multidimensional, and can't
be backed by large objects.
It appears, from looking at the older version of arrayfuncs.c
that as before the first 4 bytes is a 32 bit integer telling you
the total size of the array (as with any other varlena).
The array elements may be a fixed size of either 1,2,or4 bytes in
which case you can do the calculation easy enough, or it assumed
to be variable length, in which case you have to walk through
the array (as a sequence of varlena's).
I urge you in the strongest possible terms to upgrade to 4.2;
very large numbers of bugs were fixed, and the support for arrays
is much better and more flexible.

===============================================================================
    To add/remove yourself from the POSTGRES mailing list: send mail with 
    the subject line ADD or DEL to "postgres-request@postgres.Berkeley.EDU"

    If this fails, send mail to "post_questions@postgres.Berkeley.EDU" and
    a human will deal with it.  DO NOT post to the "postgres" mailing list.
===============================================================================




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~1994-05-13 20:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1994-05-12 19:54 About "Queries over variable sized arrays" too! lmr <ren@math.ohio-state.edu>
1994-05-12 21:59 Re:  About "Queries over variable sized arrays" too! Keith Sklower <sklower@postgres.Berkeley.EDU>
1994-05-13 17:43 ` lmr <ren@math.ohio-state.edu>
1994-05-13 20:03 Re: About "Queries over variable sized arrays" too! Keith Sklower <sklower@vangogh.CS.Berkeley.EDU>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox