Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 62ecdfc

Browse files
committed
1
1 parent 7e44ac3 commit 62ecdfc

File tree

6 files changed

+85
-19
lines changed

6 files changed

+85
-19
lines changed

src/backend/optimizer/path/equivclass.c

+1-12
Original file line numberDiff line numberDiff line change
@@ -652,18 +652,7 @@ get_eclass_for_sort_expr(PlannerInfo *root,
652652

653653
if (opcintype == cur_em->em_datatype &&
654654
equal(expr, cur_em->em_expr))
655-
{
656-
/*
657-
* Match!
658-
*
659-
* Copy the sortref if it wasn't set yet. That may happen if
660-
* the ec was constructed from a WHERE clause, i.e. it doesn't
661-
* have a target reference at all.
662-
*/
663-
if (cur_ec->ec_sortref == 0 && sortref > 0)
664-
cur_ec->ec_sortref = sortref;
665-
return cur_ec;
666-
}
655+
return cur_ec; /* Match! */
667656
}
668657
}
669658

src/backend/optimizer/path/pathkeys.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,8 @@ make_pathkeys_for_sortclauses(PlannerInfo *root,
13551355
&sortclauses,
13561356
tlist,
13571357
false,
1358-
&sortable);
1358+
&sortable,
1359+
false);
13591360
/* It's caller error if not all clauses were sortable */
13601361
Assert(sortable);
13611362
return result;
@@ -1379,13 +1380,16 @@ make_pathkeys_for_sortclauses(PlannerInfo *root,
13791380
* to remove any clauses that can be proven redundant via the eclass logic.
13801381
* Even though we'll have to hash in that case, we might as well not hash
13811382
* redundant columns.)
1383+
* *set_ec_sortref is true if caller wants to set up ec_sortref field in
1384+
* the pathkey Equivalence Class, if it have not initialized beforehand.
13821385
*/
13831386
List *
13841387
make_pathkeys_for_sortclauses_extended(PlannerInfo *root,
13851388
List **sortclauses,
13861389
List *tlist,
13871390
bool remove_redundant,
1388-
bool *sortable)
1391+
bool *sortable,
1392+
bool set_ec_sortref)
13891393
{
13901394
List *pathkeys = NIL;
13911395
ListCell *l;
@@ -1409,6 +1413,13 @@ make_pathkeys_for_sortclauses_extended(PlannerInfo *root,
14091413
sortcl->nulls_first,
14101414
sortcl->tleSortGroupRef,
14111415
true);
1416+
if (pathkey->pk_eclass->ec_sortref == 0 && set_ec_sortref)
1417+
/*
1418+
* Copy the sortref if it wasn't set yet. That may happen if
1419+
* the ec was constructed from a WHERE clause, i.e. it doesn't
1420+
* have a target reference at all.
1421+
*/
1422+
pathkey->pk_eclass->ec_sortref = sortcl->tleSortGroupRef;
14121423

14131424
/* Canonical form eliminates redundant ordering keys */
14141425
if (!pathkey_is_redundant(pathkey, pathkeys))

src/backend/optimizer/plan/planner.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -3403,7 +3403,8 @@ standard_qp_callback(PlannerInfo *root, void *extra)
34033403
&root->processed_groupClause,
34043404
tlist,
34053405
true,
3406-
&sortable);
3406+
&sortable,
3407+
true);
34073408
if (!sortable)
34083409
{
34093410
/* Can't sort; no point in considering aggregate ordering either */
@@ -3453,7 +3454,8 @@ standard_qp_callback(PlannerInfo *root, void *extra)
34533454
&root->processed_distinctClause,
34543455
tlist,
34553456
true,
3456-
&sortable);
3457+
&sortable,
3458+
false);
34573459
if (!sortable)
34583460
root->distinct_pathkeys = NIL;
34593461
}
@@ -3479,7 +3481,8 @@ standard_qp_callback(PlannerInfo *root, void *extra)
34793481
&groupClauses,
34803482
tlist,
34813483
false,
3482-
&sortable);
3484+
&sortable,
3485+
false);
34833486
if (!sortable)
34843487
root->setop_pathkeys = NIL;
34853488
}
@@ -6027,7 +6030,8 @@ make_pathkeys_for_window(PlannerInfo *root, WindowClause *wc,
60276030
&wc->partitionClause,
60286031
tlist,
60296032
true,
6030-
&sortable);
6033+
&sortable,
6034+
false);
60316035

60326036
Assert(sortable);
60336037
}

src/include/optimizer/paths.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ extern List *make_pathkeys_for_sortclauses_extended(PlannerInfo *root,
242242
List **sortclauses,
243243
List *tlist,
244244
bool remove_redundant,
245-
bool *sortable);
245+
bool *sortable,
246+
bool set_ec_sortref);
246247
extern void initialize_mergeclause_eclasses(PlannerInfo *root,
247248
RestrictInfo *restrictinfo);
248249
extern void update_mergeclause_eclasses(PlannerInfo *root,

src/test/regress/expected/aggregates.out

+47
Original file line numberDiff line numberDiff line change
@@ -2886,6 +2886,53 @@ GROUP BY c1.w, c1.z;
28862886
5.0000000000000000
28872887
(2 rows)
28882888

2889+
-- Pathkeys, built in subtree, can be used to optimize GROUP-BY clause ordering.
2890+
-- Also, here we check that it doesn't depend on the initial clause order in the
2891+
-- GROUP-BY list.
2892+
EXPLAIN (COSTS OFF)
2893+
SELECT c1.y,c1.x FROM group_agg_pk c1
2894+
JOIN group_agg_pk c2
2895+
ON c1.x = c2.x
2896+
GROUP BY c1.y,c1.x,c2.x;
2897+
QUERY PLAN
2898+
-----------------------------------------------------
2899+
Group
2900+
Group Key: c1.x, c1.y
2901+
-> Incremental Sort
2902+
Sort Key: c1.x, c1.y
2903+
Presorted Key: c1.x
2904+
-> Merge Join
2905+
Merge Cond: (c1.x = c2.x)
2906+
-> Sort
2907+
Sort Key: c1.x
2908+
-> Seq Scan on group_agg_pk c1
2909+
-> Sort
2910+
Sort Key: c2.x
2911+
-> Seq Scan on group_agg_pk c2
2912+
(13 rows)
2913+
2914+
EXPLAIN (COSTS OFF)
2915+
SELECT c1.y,c1.x FROM group_agg_pk c1
2916+
JOIN group_agg_pk c2
2917+
ON c1.x = c2.x
2918+
GROUP BY c1.y,c2.x,c1.x;
2919+
QUERY PLAN
2920+
-----------------------------------------------------
2921+
Group
2922+
Group Key: c2.x, c1.y
2923+
-> Incremental Sort
2924+
Sort Key: c2.x, c1.y
2925+
Presorted Key: c2.x
2926+
-> Merge Join
2927+
Merge Cond: (c1.x = c2.x)
2928+
-> Sort
2929+
Sort Key: c1.x
2930+
-> Seq Scan on group_agg_pk c1
2931+
-> Sort
2932+
Sort Key: c2.x
2933+
-> Seq Scan on group_agg_pk c2
2934+
(13 rows)
2935+
28892936
RESET enable_nestloop;
28902937
RESET enable_hashjoin;
28912938
DROP TABLE group_agg_pk;

src/test/regress/sql/aggregates.sql

+14
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,20 @@ SELECT avg(c1.f ORDER BY c1.x, c1.y)
12531253
FROM group_agg_pk c1 JOIN group_agg_pk c2 ON c1.x = c2.x
12541254
GROUP BY c1.w, c1.z;
12551255

1256+
-- Pathkeys, built in subtree, can be used to optimize GROUP-BY clause ordering.
1257+
-- Also, here we check that it doesn't depend on the initial clause order in the
1258+
-- GROUP-BY list.
1259+
EXPLAIN (COSTS OFF)
1260+
SELECT c1.y,c1.x FROM group_agg_pk c1
1261+
JOIN group_agg_pk c2
1262+
ON c1.x = c2.x
1263+
GROUP BY c1.y,c1.x,c2.x;
1264+
EXPLAIN (COSTS OFF)
1265+
SELECT c1.y,c1.x FROM group_agg_pk c1
1266+
JOIN group_agg_pk c2
1267+
ON c1.x = c2.x
1268+
GROUP BY c1.y,c2.x,c1.x;
1269+
12561270
RESET enable_nestloop;
12571271
RESET enable_hashjoin;
12581272
DROP TABLE group_agg_pk;

0 commit comments

Comments
 (0)