Return-Path: owner-postman 
Delivery-Date: Wed, 11 May 94 23:32:10 -0700
Return-Path: owner-postman
Received: from localhost (localhost [127.0.0.1]) by nobozo.CS.Berkeley.EDU (8.6.4/8.6.3) with SMTP id TAA27569 for postgres-redist; Wed, 11 May 1994 19:54:14 -0700
Resent-From: POSTGRES mailing list <postman@postgres.Berkeley.EDU>
Resent-Message-Id: <199405120254.TAA27569@nobozo.CS.Berkeley.EDU>
X-Authentication-Warning: nobozo.CS.Berkeley.EDU: Host localhost didn't use HELO protocol
Sender: owner-postman@postgres.Berkeley.EDU
X-Return-Path: owner-postman
Received: from kaukau.comp.vuw.ac.nz (kaukau.comp.vuw.ac.nz [130.195.5.20]) by nobozo.CS.Berkeley.EDU (8.6.4/8.6.3) with ESMTP id TAA27559 for <postgres@postgres.Berkeley.EDU>; Wed, 11 May 1994 19:54:07 -0700
Received: from downstage.comp.vuw.ac.nz (downstage.comp.vuw.ac.nz [130.195.6.10]) by kaukau.comp.vuw.ac.nz (8.6.8/8.6.6) with ESMTP id OAA07529 for <postgres@postgres.Berkeley.EDU>; Thu, 12 May 1994 14:53:46 +1200
From: Aaron Roydhouse <Aaron.Roydhouse@Comp.VUW.AC.NZ>
Received: from localhost (aaron@localhost) by downstage.comp.vuw.ac.nz (8.6.8/8.6.6) with SMTP id OAA11670 for <postgres@postgres.Berkeley.EDU>; Thu, 12 May 1994 14:54:02 +1200
Message-Id: <199405120254.OAA11670@downstage.comp.vuw.ac.nz>
To: postgres@postgres.Berkeley.EDU
Subject: Queries over variable sized arrays
Date: Thu, 12 May 1994 14:54:01 +1200
Resent-To: postgres-redist@postgres.Berkeley.EDU
Resent-Date: Wed, 11 May 94 19:54:14 -0700
Resent-XMts: smtp

Variable sized arrays as implemented in Postgres 4.2 are good and
wonderful things for storage and retrieval, especially with the handy
sub-array query syntax.

However, I am finding it quite challenging to construct queries over
the contents of arrays. Consider the following simple example

----------------------------------------------------------------------
* create things (items = int4[])\g

Query sent to backend is "create things (items = int4[])"
CREATE
Go 
* append things (items = "{7,4,2}")\g

Query sent to backend is "append things (items = "{7,4,2}")"
APPEND 657000
Go 
* append things (items = "{3,9}")\g

Query sent to backend is "append things (items = "{3,9}")"
APPEND 657001
Go 
* append things (items = "{9,2,3,4}")\g

Query sent to backend is "append things (items = "{9,2,3,4}")"
APPEND 657002
Go 
* retrieve (things.all)\g

Query sent to backend is "retrieve (things.all)"
---------------
| items       |
---------------
| {7,4,2}     |
---------------
| {3,9}       |
---------------
| {9,2,3,4}   |
---------------
----------------------------------------------------------------------

Suppose I want to find instances with items greater than seven. I can
issue the following query.

----------------------------------------------------------------------
* retrieve (t.all) from t in things 
where t.items[1] > 7 
or t.items[2] > 7 
or t.items[3] > 7 
or t.items[4] > 7\g

Query sent to backend is "retrieve (t.all) from t in things where t.items[1] > 7 or t.items[2] > 7 or t.items[3] > 7 or t.items[4] > 7"
---------------
| items       |
---------------
| {3,9}       |
---------------
| {9,2,3,4}   |
---------------
----------------------------------------------------------------------

But this required as many conditions as the maximum number of items in
any array, and required me to know the maximum number of items in any
array (how can I find that out?).

I really want to map a test over the array, but I can't see how this
is possible. I guess I could write an operator in C that manually
conducts a test over the array. Sometimes, though, I'd want to issue
sub-queries for items in the array. Is there an (easy or documented or
example) method to issue internal sub-queries from inside postgres
functions defined in C? Has anyone done something similar?

Well, I'm just full of questions today. Thanks for your help Paul,
even if many of the answers have negative :-)

Aaron.



===============================================================================
    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.
===============================================================================

