Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 268B0634129 for ; Tue, 12 May 2009 05:41:54 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 67615-04 for ; Tue, 12 May 2009 05:41:45 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from yx-out-2324.google.com (yx-out-2324.google.com [74.125.44.29]) by mail.postgresql.org (Postfix) with ESMTP id 2AE0C6340C7 for ; Tue, 12 May 2009 05:36:21 -0300 (ADT) Received: by yx-out-2324.google.com with SMTP id 8so1947613yxg.73 for ; Tue, 12 May 2009 01:36:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=kkyvv5OkmF9/fSjgA1AQlaCm40kFZolaYEc6HqtI+hQ=; b=xoRifkhwZr20vzSVrCy3CWSup6eNXXSgozRzmQswjd/YFZUfnJ2qqOmVeg97uzmyGf wUIWYFFzV+2AK56Oumk9jtk84cfMJAK/UpQ03Gq+ONruVOXNIj+kbDFknRpeKM/6JQHI Uug3XAnPnnOPGL0GDq1GSY5DVUruba8yAypI0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=tkF5TYdZcgDtZYqlyPCiWXCnp+A/YDXB7vcKmcRkliUOzefxnSA5u8MMIJ1jYo5kfP nrQw/fs1/Lxe+HZY/TNsemVPnDEUXSEYef0luIEBZ6bISoCfR6aPRtDz/QUL1VrPL4rZ cIBqg5/QmNm8KBL9PAsPkilSnQcgA5CXtmxZk= MIME-Version: 1.0 Received: by 10.151.129.8 with SMTP id g8mr15056076ybn.104.1242116937101; Tue, 12 May 2009 01:28:57 -0700 (PDT) In-Reply-To: <7c33d060905120105y74b0483eyf5d94c07912a0702@mail.gmail.com> References: <7c33d060905120105y74b0483eyf5d94c07912a0702@mail.gmail.com> Date: Tue, 12 May 2009 10:28:57 +0200 Message-ID: <162867790905120128h1dff1ba3mdc9c511af938e595@mail.gmail.com> Subject: Re: Array: comparing first N elements? From: Pavel Stehule To: David Garamond Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.166 tagged_above=0 required=5 tests=AWL=0.166 X-Spam-Level: X-Archive-Number: 200905/54 X-Sequence-Number: 32546 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=3D# select eqn(array[1,2,3,4,5], array[1,2,3,5,6], 3); eqn ----- t (1 row) Time: 1,590 ms postgres=3D# 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 : > I have a "materialized path" tree table like this (simplified): > CREATE TABLE product ( > =C2=A0 =C2=A0 id SERIAL PRIMARY KEY, > =C2=A0 =C2=A0 parents INT[] NOT NULL, > =C2=A0 =C2=A0 name TEXT NOT NULL, > =C2=A0 =C2=A0 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 rewr= ite > 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