pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Pavel Stehule <pavel.stehule@gmail.com>
To: David Garamond <davidgaramond@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Array: comparing first N elements?
Date: Tue, 12 May 2009 10:28:57 +0200
Message-ID: <162867790905120128h1dff1ba3mdc9c511af938e595@mail.gmail.com> (raw)
In-Reply-To: <7c33d060905120105y74b0483eyf5d94c07912a0702@mail.gmail.com>
References: <7c33d060905120105y74b0483eyf5d94c07912a0702@mail.gmail.com>

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



view thread (6+ messages)  latest in thread

Message-ID: <162867790905120128h1dff1ba3mdc9c511af938e595@mail.gmail.com>
Permalink:  ../162867790905120128h1dff1ba3mdc9c511af938e595@mail.gmail.com/
Also on:    postgresql.org/message-id/162867790905120128h1dff1ba3mdc9c511af938e595@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: pavel.stehule@gmail.com, davidgaramond@gmail.com
  Subject: Re: Array: comparing first N elements?
  In-Reply-To: <162867790905120128h1dff1ba3mdc9c511af938e595@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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