Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e2c9v-0008De-G2 for pgsql-bugs@arkaria.postgresql.org; Thu, 12 Oct 2017 12:00:15 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e2c9u-0001wd-Tf for pgsql-bugs@arkaria.postgresql.org; Thu, 12 Oct 2017 12:00:14 +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 1e2c9A-0000fN-TG for pgsql-bugs@postgresql.org; Thu, 12 Oct 2017 11:59:29 +0000 Received: from mail-lf0-x22c.google.com ([2a00:1450:4010:c07::22c]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1e2c97-00011e-5T for pgsql-bugs@postgresql.org; Thu, 12 Oct 2017 11:59:27 +0000 Received: by mail-lf0-x22c.google.com with SMTP id p184so5632313lfe.12 for ; Thu, 12 Oct 2017 04:59:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:subject:to:message-id:date:user-agent:mime-version :content-language; bh=+e1pf+MYgxlrkW+0OPWd/3Ua3k6sdyDARaF95Jf3q/Q=; b=uKPijxpHRQsdDU86ZG0XSmJCOJfmhFKtA/J3c0Ku1SNfy4hsvMcxXHQwvG5BbJRVeT yyx35bqyzoTJcpsa+1Crj3t2IK6hR7gOoYmrz3/y1CNoy93Z7fV5JRk54mDYHtWfpsiA Au9QWz25FsVQdqs61O+eoi2zhw1/Io/ot0DPz1OFL6x+DaWcs03584AtkNsvzZZiw9o5 3WSSSi+wcAQpZjp05Ox2pghRpbaPjLJaMIcBumpMK53VnQJxafRT8D5XNUy2/8hTVtcg HMnNUzjpExgS8FMGQHgp85TrQ5SXD8OZPkqmqH4mcphI64hCe/AtKe/fXuo8/gVk3rCN 7U1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:subject:to:message-id:date :user-agent:mime-version:content-language; bh=+e1pf+MYgxlrkW+0OPWd/3Ua3k6sdyDARaF95Jf3q/Q=; b=dz7j8o/+xel1MxXyU3am2dA8gAovJb7/eKafsJyfljlRwPrgyYxj/Fty3WhyYvQprs X69oHugqkcDc5EIVHUu7fD1PX2dnOQ1h07TaqMji/go4gwjcjzvnG05oS8zsKj2n2R7s nE3Pn4GxOya7tNhO4nBY9y2Cb4loD0uo6oJgTHWoIKgnVTpqEdd0wkaIdwI8Bh24a7lS 1nMQtqYwhJ2WJ5Q4/1oLoF6gLjrtz5j9FJuNw2X2mv/uJTyg897bgmekOQ+opZKRvfXC vvO2s32VZJDVFMjwbyCtGnZbvtABDMwRwyjeBJfHjZoyYwO8AdS2fDwSKtBxnWJW75r8 bV+w== X-Gm-Message-State: AMCzsaXs3fYQwKDhzdH23g/TyYKDsLbYPcIVyTatk7sKCO/No1A9EiMj RPmyFWec8i+vItL/WTVBgmR/+w== X-Google-Smtp-Source: ABhQp+RfHrofQeXujLRAr/hFJRrNb1k3MujnZHQ4p/ktLLb5lHHq9Uvjk+nFm+pWtyYcgoGx8sOrpg== X-Received: by 10.25.234.66 with SMTP id i63mr825867lfh.188.1507809562646; Thu, 12 Oct 2017 04:59:22 -0700 (PDT) Received: from [192.168.1.99] (dsl-hkibng32-54fb4e-103.dhcp.inet.fi. [84.251.78.103]) by smtp.gmail.com with ESMTPSA id u86sm1472523lje.79.2017.10.12.04.59.21 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Oct 2017 04:59:21 -0700 (PDT) From: Heikki Linnakangas Subject: Improper const-evaluation of HAVING with grouping sets and subquery pullup To: Pg Bugs Message-ID: <7dbdcf5c-b5a6-ef89-4958-da212fe10176@iki.fi> Date: Thu, 12 Oct 2017 14:59:12 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------7DE1DFF6AAFDC43C22B84B63" Content-Language: en-US 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 This is a multi-part message in MIME format. --------------7DE1DFF6AAFDC43C22B84B63 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit This query produces an incorrect result: regression=# select four, x from (select four, ten, 'foo'::text as x from tenk1 ) as t group by grouping sets(four, x) having x = 'foo' order by four; four | x ------+----- 0 | 1 | 2 | 3 | | foo (5 rows) The "having x = 'foo'" clause should've filtered out the rows where x is NULL, leaving only the last row as the result. Even though x is a constant 'foo' in the subquery, HAVING clause is supposed to be evaluated after grouping. What happens is that subquery pullup replaces x with the constant, and the "'foo' = 'foo'" qual is later const-evaluated to true. I propose the attached patch to fix that. It forces the use of PlaceHolderVars in subquery pullup, if the parent query has grouping sets and HAVING. I'm not 100% sure that's the right approach or a misuse of the placeholder system, so comments welcome. At first, I tried to set wrap_non_vars=true only when processing the havingQual, so that placeholders would only be there. But that didn't work out, I think because grouping sets planning would then put both the Const, and the PlaceHolderVar for the Const, in the Agg's targetlist, but only one of them would be set to NULL when doing the grouping. Another thing is that the check could be made much tighter, so that PlaceHolderVars were only used for expressions actually used in the HAVING. But it didn't seem worth the trouble to me. - Heikki --------------7DE1DFF6AAFDC43C22B84B63 Content-Type: text/x-patch; name="0001-Fix-subquery-pullup-into-a-query-containing-GROUPING.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Fix-subquery-pullup-into-a-query-containing-GROUPING.pa"; filename*1="tch"