Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tSLAX-00FoQa-Em for pgsql-bugs@arkaria.postgresql.org; Mon, 30 Dec 2024 19:11:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tSLAW-00EFmn-6E for pgsql-bugs@arkaria.postgresql.org; Mon, 30 Dec 2024 19:11:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tSLAV-00EFmf-Ts for pgsql-bugs@lists.postgresql.org; Mon, 30 Dec 2024 19:11:11 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tSLAS-001IHw-26 for pgsql-bugs@lists.postgresql.org; Mon, 30 Dec 2024 19:11:10 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 4BUJB6mQ3879325; Mon, 30 Dec 2024 14:11:06 -0500 From: Tom Lane To: vectorplanck@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18758: Incorrect query result caused by ROLLUP operation In-reply-to: <18758-7bbeea9719044a29@postgresql.org> References: <18758-7bbeea9719044a29@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Mon, 30 Dec 2024 16:03:58 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3879323.1735585866.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 30 Dec 2024 14:11:06 -0500 Message-ID: <3879324.1735585866@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > create table t1 (c1 char(50)); > create table t2 (c1 integer); > select t1.c1 from t2, t1 group by rollup(t1.c1); -- actual: {null}, > expected: {} This is the correct result AFAICS. Per [1], ROLLUP(t1.c1) is a shorthand for GROUPING SETS ( ( t1.c1 ), ( ) ) So first you get the output that would correspond to GROUP BY t1.c1, and with no values of t1.c1 that output is indeed empty. But then you get the output for an empty grouping set, which is defined in the same place as An empty grouping set means that all rows are aggregated down to a single group (which is output even if no input rows were present), as described above for the case of aggregate functions with no GROUP BY clause. So a single row with a null value for t1.c1 is expected, even when the FROM clause produces no rows. regards, tom lane [1] https://www.postgresql.org/docs/current/queries-table-expressions.html= #QUERIES-GROUPING-SETS