From: | Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> |
---|---|
To: | David Garamond <davidgaramond(at)gmail(dot)com> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: Array: comparing first N elements? |
Date: | 2009-05-12 08:28:57 |
Message-ID: | 162867790905120128h1dff1ba3mdc9c511af938e595@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
Hello
create or replace function eqn(anyarray, anyarray, int)
returns boolean as $$
select not exists(select $1[i] from generate_series(1,$3) g(i)
except
select $2[i] from generate_series(1,$3) g(i))
$$ language sql immutable strict;
postgres=# select eqn(array[1,2,3,4,5], array[1,2,3,5,6], 3);
eqn
-----
t
(1 row)
Time: 1,590 ms
postgres=# select eqn(array[1,2,3,4,5], array[1,2,3,5,6], 4);
eqn
-----
f
(1 row)
regards
Pavel Stehule
2009/5/12 David Garamond <davidgaramond(at)gmail(dot)com>:
> I have a "materialized path" tree table like this (simplified):
> CREATE TABLE product (
> id SERIAL PRIMARY KEY,
> parents INT[] NOT NULL,
> name TEXT NOT NULL,
> UNIQUE (parents, name)
> );
> CREATE INDEX name ON product(name);
>
> Previously I use TEXT column for parents, but arrays look interesting and
> convenient so I'm considering migrating to arrays. However, how do I rewrite
> this using arrays?
> SELECT * FROM product
> WHERE parents LIKE '0001/0010/%';
> In other words, testing against the first N elements in an array.
> Regards,
> Dave
From | Date | Subject | |
---|---|---|---|
Next Message | Glenn Maynard | 2009-05-12 08:37:24 | Re: Array: comparing first N elements? |
Previous Message | David Garamond | 2009-05-12 08:05:28 | Array: comparing first N elements? |