Re: Array: comparing first N elements? - Mailing list pgsql-sql

From Pavel Stehule
Subject Re: Array: comparing first N elements?
Date
Msg-id 162867790905120128h1dff1ba3mdc9c511af938e595@mail.gmail.com
Whole thread Raw
In response to Array: comparing first N elements?  (David Garamond <davidgaramond@gmail.com>)
List 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@gmail.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


pgsql-sql by date:

Previous
From: David Garamond
Date:
Subject: Re: Array: comparing first N elements?
Next
From: David Garamond
Date:
Subject: Re: Array: comparing first N elements?