Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d9CmO-0003tj-HW for pgsql-bugs@arkaria.postgresql.org; Fri, 12 May 2017 15:46:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1d9CmN-0002dL-Nl for pgsql-bugs@arkaria.postgresql.org; Fri, 12 May 2017 15:46:55 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1d9Clh-0001Qm-1E for pgsql-bugs@postgresql.org; Fri, 12 May 2017 15:46:13 +0000 Received: from mail-oi0-x22c.google.com ([2607:f8b0:4003:c06::22c]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1d9ClZ-0006WM-By for pgsql-bugs@postgresql.org; Fri, 12 May 2017 15:46:11 +0000 Received: by mail-oi0-x22c.google.com with SMTP id h4so70337343oib.3 for ; Fri, 12 May 2017 08:46:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=8bfIMZ+uhssyua6tPZA2dCILS6xwARcnIRfGGKUN9xw=; b=JRGgzRLVnVcDAvWXVuGvuPRL7KgIwX5VLeKHCvri7O8DmPaz36z/JmySZiOKwBMU8K cBV2JG+h5bKMQk+KBY/XSAVweHcxcuhF+Ftg39EOSIj5VOoK0F3tmtj5CvvV3TR1kJ16 wJAk8j43FUeZB4+qJjUuqWqrCPcfwMiGnlupjAdqnsm3Ecz7t64DNhyun2ptQy1BlUz6 83Dt2FFlt7LKoQhP22he/yejqnFVz1DJBDcWWKQ021GobDduTz3ByFfgtveBsaqtbTIX i50d2DztWLM3n2OYMD3tldOYC2XmeUkfAO96VJZpEyFqYwhiNJ6Y5K9ijV1T+WfIMH24 NzkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=8bfIMZ+uhssyua6tPZA2dCILS6xwARcnIRfGGKUN9xw=; b=g6ooEKKwH4+xsGGS6956RKwF7jbJGy/mUpygcufeX8xorwC+i4adkwCjqIDcCBRjZZ OipTkJpgcbgLD0LnmqkjGGODNg00oy9ACOYugDJXacISOZb295YwOGZXPSDZIWtZ0ifC uUJp57sNpFLRSDjBK8LA9pJDi3NoBK+jcKGx24gjveTr5Y1SFAPoFNDohO0Pv3NowZE7 tHOZJjdXUQfvQ85sv4UFri3j0qaYnnUaShsCa737ngXWWXg3wHjxUTrxFdxe0/7U6bVZ jPJ6cAI8EyUM3rK8+ltipOphke7/mGGJJGzg8SjPGMWXj5WIoEKwut9IQGS4QnCQIqtP eOAw== X-Gm-Message-State: AODbwcCtora6byfmGX6BKWFwf2KBpCAGpJVGMhH8qwa/WS65WyCkFk4x weUm+g877957hu67vbAbvf14Yp5l8A== X-Received: by 10.157.89.201 with SMTP id u9mr2675722otg.32.1494603964925; Fri, 12 May 2017 08:46:04 -0700 (PDT) MIME-Version: 1.0 Received: by 10.157.46.205 with HTTP; Fri, 12 May 2017 08:46:04 -0700 (PDT) In-Reply-To: References: <20170512130033.1796.93117@wrigleys.postgresql.org> <18166.1494598847@sss.pgh.pa.us> From: "David G. Johnston" Date: Fri, 12 May 2017 08:46:04 -0700 Message-ID: Subject: Re: BUG #14648: counts for queries using array unnesting is incorrect To: Sebastion Calbaza Cc: "pgsql-bugs@postgresql.org" Content-Type: multipart/alternative; boundary="f4030435b39068cf33054f559ac5" X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --f4030435b39068cf33054f559ac5 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, May 12, 2017 at 7:55 AM, Sebastion Calbaza < sebastian.calbaza@hgdata.com> wrote: > Thanks for replying.... I know it's complicated to follow it (even if > formatted ) , but probably I did not emphasize enough that by using > unnest(ids) in the second query, the distinct count for company is not > computed correctly anymore. > Focusing on the used select clauses is important, they are just distinct > counts, it would have been logical to stay the same for both queries, > ignoring the unnest(ids)(the data set is in the milions of rows so you ne= ed > to take my word that the first query return the correct values :) ). > =E2=80=8BIn the first query the number of distinct companies =E2=80=8Bis gr= eater than the number of matched companies. In the second the numbers are equal. If the only difference between the two is the "unnest(ids)" then its presence is causing every unmatched company to be discarded from the result. Since "SELECT unnest(null::text[])" is the empty set it would remove the corresponding row from your subquery output. You might try writing the following which will convert the empty set to a NULL and thus not discard records. (SELECT unnest(ids)) AS id, F.urlx as company ... David J. --f4030435b39068cf33054f559ac5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Fri, Ma= y 12, 2017 at 7:55 AM, Sebastion Calbaza <sebastian.calbaza@hgdata.com> wrote:
Thanks for replying.... I know i= t's complicated to follow it (even if formatted ) , but probably I did = not emphasize enough that by using unnest(ids) in the second query, the dis= tinct count for company is not computed correctly anymore.
Focusing on t= he used select clauses=C2=A0 is important, they are just distinct counts, i= t would have been logical to stay the same for both queries, ignoring the u= nnest(ids)(the data set is in the milions of rows so you need to take my wo= rd that the first query return the correct values :) ).

=E2=80=8BIn the first query the number of = distinct companies =E2=80=8Bis greater than the number of matched companies= .=C2=A0 In the second the numbers are equal.=C2=A0 If the only difference b= etween the two is the "unnest(ids)" then its presence is causing = every unmatched company to be discarded from the result.=C2=A0 Since "= SELECT unnest(null::text[])" is the empty set it would remove the corr= esponding row from your subquery output.=C2=A0 You might try writing the fo= llowing which will convert the empty set to a NULL and thus not discard rec= ords.

(SELECT unnest(ids)) AS id,=C2=A0F.urlx as company ...=

=
David J.

--f4030435b39068cf33054f559ac5--