pgsql: Implement GROUP BY DISTINCT

From: Tomas Vondra <tomas(dot)vondra(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Implement GROUP BY DISTINCT
Date: 2021-03-18 17:23:04
Message-ID: E1lMwMS-0007AP-Qu@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Implement GROUP BY DISTINCT

With grouping sets, it's possible that some of the grouping sets are
duplicate. This is especially common with CUBE and ROLLUP clauses. For
example GROUP BY CUBE (a,b), CUBE (b,c) is equivalent to

GROUP BY GROUPING SETS (
(a, b, c),
(a, b, c),
(a, b, c),
(a, b),
(a, b),
(a, b),
(a),
(a),
(a),
(c, a),
(c, a),
(c, a),
(c),
(b, c),
(b),
()
)

Some of the grouping sets are calculated multiple times, which is mostly
unnecessary. This commit implements a new GROUP BY DISTINCT feature, as
defined in the SQL standard, which eliminates the duplicate sets.

Author: Vik Fearing
Reviewed-by: Erik Rijkers, Georgios Kokolatos, Tomas Vondra
Discussion: https://postgr.es/m/bf3805a8-d7d1-ae61-fece-761b7ff41ecc@postgresfriends.org

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/be45be9c33a85e72cdaeb9967e9f6d2d00199e09

Modified Files
--------------
doc/src/sgml/queries.sgml | 54 ++++++++++++++
doc/src/sgml/ref/select.sgml | 9 ++-
src/backend/catalog/sql_features.txt | 2 +-
src/backend/nodes/copyfuncs.c | 2 +
src/backend/nodes/equalfuncs.c | 2 +
src/backend/nodes/list.c | 16 +++++
src/backend/nodes/outfuncs.c | 2 +
src/backend/nodes/readfuncs.c | 1 +
src/backend/optimizer/plan/planner.c | 2 +-
src/backend/parser/analyze.c | 1 +
src/backend/parser/gram.y | 59 ++++++++++-----
src/backend/parser/parse_agg.c | 58 +++++++++++++--
src/backend/utils/adt/ruleutils.c | 2 +
src/include/nodes/parsenodes.h | 10 +++
src/include/nodes/pg_list.h | 1 +
src/include/parser/parse_agg.h | 2 +-
src/test/regress/expected/groupingsets.out | 111 +++++++++++++++++++++++++++++
src/test/regress/sql/groupingsets.sql | 26 +++++++
18 files changed, 333 insertions(+), 27 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2021-03-18 23:24:29 pgsql: Fix misuse of foreach_delete_current().
Previous Message Tomas Vondra 2021-03-18 16:39:05 pgsql: Remove temporary files after backend crash