mirror of
https://github.com/postgres/postgres.git
synced 2025-11-29 23:43:17 +03:00
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
This commit is contained in:
@@ -3135,6 +3135,7 @@ _copyQuery(const Query *from)
|
||||
COPY_NODE_FIELD(onConflict);
|
||||
COPY_NODE_FIELD(returningList);
|
||||
COPY_NODE_FIELD(groupClause);
|
||||
COPY_SCALAR_FIELD(groupDistinct);
|
||||
COPY_NODE_FIELD(groupingSets);
|
||||
COPY_NODE_FIELD(havingQual);
|
||||
COPY_NODE_FIELD(windowClause);
|
||||
@@ -3221,6 +3222,7 @@ _copySelectStmt(const SelectStmt *from)
|
||||
COPY_NODE_FIELD(fromClause);
|
||||
COPY_NODE_FIELD(whereClause);
|
||||
COPY_NODE_FIELD(groupClause);
|
||||
COPY_SCALAR_FIELD(groupDistinct);
|
||||
COPY_NODE_FIELD(havingClause);
|
||||
COPY_NODE_FIELD(windowClause);
|
||||
COPY_NODE_FIELD(valuesLists);
|
||||
|
||||
@@ -977,6 +977,7 @@ _equalQuery(const Query *a, const Query *b)
|
||||
COMPARE_NODE_FIELD(onConflict);
|
||||
COMPARE_NODE_FIELD(returningList);
|
||||
COMPARE_NODE_FIELD(groupClause);
|
||||
COMPARE_SCALAR_FIELD(groupDistinct);
|
||||
COMPARE_NODE_FIELD(groupingSets);
|
||||
COMPARE_NODE_FIELD(havingQual);
|
||||
COMPARE_NODE_FIELD(windowClause);
|
||||
@@ -1053,6 +1054,7 @@ _equalSelectStmt(const SelectStmt *a, const SelectStmt *b)
|
||||
COMPARE_NODE_FIELD(fromClause);
|
||||
COMPARE_NODE_FIELD(whereClause);
|
||||
COMPARE_NODE_FIELD(groupClause);
|
||||
COMPARE_SCALAR_FIELD(groupDistinct);
|
||||
COMPARE_NODE_FIELD(havingClause);
|
||||
COMPARE_NODE_FIELD(windowClause);
|
||||
COMPARE_NODE_FIELD(valuesLists);
|
||||
|
||||
@@ -1506,6 +1506,22 @@ list_sort(List *list, list_sort_comparator cmp)
|
||||
qsort(list->elements, len, sizeof(ListCell), (qsort_comparator) cmp);
|
||||
}
|
||||
|
||||
/*
|
||||
* list_sort comparator for sorting a list into ascending int order.
|
||||
*/
|
||||
int
|
||||
list_int_cmp(const ListCell *p1, const ListCell *p2)
|
||||
{
|
||||
int v1 = lfirst_int(p1);
|
||||
int v2 = lfirst_int(p2);
|
||||
|
||||
if (v1 < v2)
|
||||
return -1;
|
||||
if (v1 > v2)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* list_sort comparator for sorting a list into ascending OID order.
|
||||
*/
|
||||
|
||||
@@ -2771,6 +2771,7 @@ _outSelectStmt(StringInfo str, const SelectStmt *node)
|
||||
WRITE_NODE_FIELD(fromClause);
|
||||
WRITE_NODE_FIELD(whereClause);
|
||||
WRITE_NODE_FIELD(groupClause);
|
||||
WRITE_BOOL_FIELD(groupDistinct);
|
||||
WRITE_NODE_FIELD(havingClause);
|
||||
WRITE_NODE_FIELD(windowClause);
|
||||
WRITE_NODE_FIELD(valuesLists);
|
||||
@@ -2996,6 +2997,7 @@ _outQuery(StringInfo str, const Query *node)
|
||||
WRITE_NODE_FIELD(onConflict);
|
||||
WRITE_NODE_FIELD(returningList);
|
||||
WRITE_NODE_FIELD(groupClause);
|
||||
WRITE_BOOL_FIELD(groupDistinct);
|
||||
WRITE_NODE_FIELD(groupingSets);
|
||||
WRITE_NODE_FIELD(havingQual);
|
||||
WRITE_NODE_FIELD(windowClause);
|
||||
|
||||
@@ -271,6 +271,7 @@ _readQuery(void)
|
||||
READ_NODE_FIELD(onConflict);
|
||||
READ_NODE_FIELD(returningList);
|
||||
READ_NODE_FIELD(groupClause);
|
||||
READ_BOOL_FIELD(groupDistinct);
|
||||
READ_NODE_FIELD(groupingSets);
|
||||
READ_NODE_FIELD(havingQual);
|
||||
READ_NODE_FIELD(windowClause);
|
||||
|
||||
Reference in New Issue
Block a user