Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hfYPZ-0005B7-Ul for pgsql-general@arkaria.postgresql.org; Mon, 24 Jun 2019 23:30:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hfYOY-0005GV-Ib for pgsql-general@arkaria.postgresql.org; Mon, 24 Jun 2019 23:29:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hfYOX-0005GI-Ve for pgsql-general@lists.postgresql.org; Mon, 24 Jun 2019 23:29:06 +0000 Received: from mail-pg1-x531.google.com ([2607:f8b0:4864:20::531]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hfYOR-00072C-6z for pgsql-general@postgresql.org; Mon, 24 Jun 2019 23:29:04 +0000 Received: by mail-pg1-x531.google.com with SMTP id k13so1737805pgq.9 for ; Mon, 24 Jun 2019 16:28:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=u3oDF9tFNZ7GdWs8Y5S4VIfqDZlg4RPYCapES61dmEI=; b=CXAwYucx+6KWRzARO28ecMxFZisVPxhWYJ5fGnC2vsQwIgJbMvVK1NyKpTaK9Z0WDR 4XHv0BKqex+65/BkKDTbiycyxDHjXRuYk708TIq3fDe4JdXkhiBk4xBQFmZHhi+YXIjL ULD6VxPmGY93W4p8h0lk4Pdy4O+peVvm8wNa63WRiHWcGegTOkljjB/ETimqMSU6jUo0 A8dFEBcfCGr9DQ8eti4Qqq8UmFov6rJ6xfwOYb2prAQMvssgNQbo2HPasp7xhLC0CIcV 0EG/2NunZLEwR7MS1z2zdi4yCYs+XWYZ1AZVbuEfboSNC88sFUIGEhNY0tnPP62T0zJ5 lWBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=u3oDF9tFNZ7GdWs8Y5S4VIfqDZlg4RPYCapES61dmEI=; b=uPJNBSzYpNL/EEpXsu0fh8UAJrwrIW27he7rBbOlR2JBwjfwtyiy3vCbWsIGDK2seO 8ZxWVTVHsmiLpzL29S1B+d3Bq9ZthJTmFQoY1G/ObUlGOSl9yyEn1Zt4Yaxxavr7aRkq V76zI2TZF88ehJ6/RLUPRih9anx5IgZ8PiL6zyptvBOFwGc+lpmAYJEv4Nuug/tooLNG MPlY5OpC8C+dEEKIJYgazZxdD9yrKGikWyeRLLaWxYw2xmzK3edS7Xnttlmxf1s8DeNL hfgFWKVNl/aYAoZt6KOLaLfAhrBNNAVYwFHhKqM53xEVV9uDkx6TDHzn01wpIC225frp ziLw== X-Gm-Message-State: APjAAAWEg0ftKT+qNityNpmK/31781uL67mBsKAgulF5SQz9agljyVzr 7R4jeNqqEsYa2SrVA2ckYnhnxDkSjuU= X-Google-Smtp-Source: APXvYqytGAwzv1AOlsBHwSNP+5+ULJ8b5aFrLDNMRQMFmvAB1WfYF/oZu8TMC5RrWPcTIbhvClazcQ== X-Received: by 2002:a17:90a:a410:: with SMTP id y16mr28145867pjp.62.1561418937621; Mon, 24 Jun 2019 16:28:57 -0700 (PDT) Received: from [10.104.134.41] ([155.100.47.1]) by smtp.gmail.com with ESMTPSA id v9sm16578305pgj.69.2019.06.24.16.28.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Jun 2019 16:28:57 -0700 (PDT) Subject: Re: Coalesce 2 Arrays To: "David G. Johnston" Cc: Alex Magnum , "pgsql-general@postgresql.org" References: <163793B6-8981-4618-9770-0AFD4D499728@gmail.com> <53ea4b16-4a57-e8c9-a27c-5de130375e7c@gmail.com> From: Rob Sargent Message-ID: <951467c4-229b-2bf1-5fe6-b2695b65c152@gmail.com> Date: Mon, 24 Jun 2019 17:28:55 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------184B4A44A8CEBB5D759B7838" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------184B4A44A8CEBB5D759B7838 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 6/24/19 5:19 PM, David G. Johnston wrote: > On Mon, Jun 24, 2019 at 4:11 PM Rob Sargent > wrote: > > > On 6/24/19 4:46 PM, Alex Magnum wrote: >> Yes, they are. >> >> On Tue, Jun 25, 2019 at 4:33 AM Rob Sargent >> > wrote: >> >> >> >>> On Jun 24, 2019, at 2:31 PM, Alex Magnum >>> > wrote: >>> >>> Hi, >>> I have two arrays which I need to combine based on the >>> individual values; >>> i could do a coalesce for each field but was wondering if >>> there is an easier way >>> >>> array_a{a,  null,c,   d,null,f,null}  primary >>> array_b{null,2  ,null,4,5   ,6,null}  secondary >>> >>> result {a,   2,   c,   d,5,   f,null) >>> >>> Any advice would be appreciated >>> >> Are the inputs always of fixed dimensions eg. 1 by 7? >> > create or replace function tt( a1 int[], a2 int[]) > returns int[] as $$ > declare >         aret int[]; >         asize int; > begin >      select array_length(a1,1) into asize; >      for i in 1..asize loop >           aret[i] = coalesce(a1[i], a2[i]); >      end loop; >      return aret; > end; > > $$ language plpgsql; > > select * from tt(array[3,null], array[null,4]); >   tt > ------- >  {3,4} > (1 row) > > Plain SQL variant: > SELECT array_agg(COALESCE(a, b)) > FROM ( > SELECT > unnest(ARRAY[null, 2]::int[]), > unnest(ARRAY[1,null]::int[]) > ) vals (a, b); > > Even if they aren't the same length the above should work, I think, as > extra rows for the shorter array will contribute padded nulls. > > David J. > Brilliant of course.  Maybe not as easy to stick in another query select a.name, b.name, tt(a.intarray, b.intarray) as coalesced_array from table a join table b on a. = b.; Any guess at the performance differences? --------------184B4A44A8CEBB5D759B7838 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit


On 6/24/19 5:19 PM, David G. Johnston wrote:
On Mon, Jun 24, 2019 at 4:11 PM Rob Sargent <robjsargent@gmail.com> wrote:


On 6/24/19 4:46 PM, Alex Magnum wrote:
Yes, they are. 

On Tue, Jun 25, 2019 at 4:33 AM Rob Sargent <robjsargent@gmail.com> wrote:


On Jun 24, 2019, at 2:31 PM, Alex Magnum <magnum11200@gmail.com> wrote:

Hi,
I have two arrays which I need to combine based on the individual values;
i could do a coalesce for each field but was wondering if there is an easier way

array_a{a,   null,c,   d,null,f,null}  primary
array_b{null,2   ,null,4,5   ,6,null}  secondary
 
result {a,   2,   c,   d,5,   f,null)

Any advice would be appreciated

Are the inputs always of fixed dimensions eg. 1 by 7?

create or replace function tt( a1 int[], a2 int[])
returns int[] as $$
declare
        aret int[];
        asize int;
begin
     select array_length(a1,1) into asize;
     for i in 1..asize loop
          aret[i] = coalesce(a1[i], a2[i]);
     end loop;
     return aret;
end;

$$ language plpgsql;

select * from tt(array[3,null], array[null,4]);
  tt  
-------
 {3,4}
(1 row)

Plain SQL variant:
 
SELECT array_agg(COALESCE(a, b))
FROM (
SELECT 
unnest(ARRAY[null, 2]::int[]),
unnest(ARRAY[1,null]::int[])
) vals (a, b);

Even if they aren't the same length the above should work, I think, as extra rows for the shorter array will contribute padded nulls.

David J.

Brilliant of course.  Maybe not as easy to stick in another query

select a.name, b.name, tt(a.intarray, b.intarray) as coalesced_array from table a join table b on a.<something> = b.<something>;

Any guess at the performance differences?


--------------184B4A44A8CEBB5D759B7838--