Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite729516

Browse files
committed
1
1 parent4712177 commite729516

File tree

6 files changed

+85
-19
lines changed

6 files changed

+85
-19
lines changed

‎src/backend/optimizer/path/equivclass.c

Lines changed: 1 addition & 12 deletions
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-
returncur_ec;
666-
}
655+
returncur_ec;/* Match! */
667656
}
668657
}
669658

‎src/backend/optimizer/path/pathkeys.c

Lines changed: 13 additions & 2 deletions
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
returnresult;
@@ -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
boolremove_redundant,
1388-
bool*sortable)
1391+
bool*sortable,
1392+
boolset_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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3400,7 +3400,8 @@ standard_qp_callback(PlannerInfo *root, void *extra)
34003400
&root->processed_groupClause,
34013401
tlist,
34023402
true,
3403-
&sortable);
3403+
&sortable,
3404+
true);
34043405
if (!sortable)
34053406
{
34063407
/* Can't sort; no point in considering aggregate ordering either */
@@ -3450,7 +3451,8 @@ standard_qp_callback(PlannerInfo *root, void *extra)
34503451
&root->processed_distinctClause,
34513452
tlist,
34523453
true,
3453-
&sortable);
3454+
&sortable,
3455+
false);
34543456
if (!sortable)
34553457
root->distinct_pathkeys=NIL;
34563458
}
@@ -3476,7 +3478,8 @@ standard_qp_callback(PlannerInfo *root, void *extra)
34763478
&groupClauses,
34773479
tlist,
34783480
false,
3479-
&sortable);
3481+
&sortable,
3482+
false);
34803483
if (!sortable)
34813484
root->setop_pathkeys=NIL;
34823485
}
@@ -6061,7 +6064,8 @@ make_pathkeys_for_window(PlannerInfo *root, WindowClause *wc,
60616064
&wc->partitionClause,
60626065
tlist,
60636066
true,
6064-
&sortable);
6067+
&sortable,
6068+
false);
60656069

60666070
Assert(sortable);
60676071
}

‎src/include/optimizer/paths.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ extern List *make_pathkeys_for_sortclauses_extended(PlannerInfo *root,
239239
List**sortclauses,
240240
List*tlist,
241241
boolremove_redundant,
242-
bool*sortable);
242+
bool*sortable,
243+
boolset_ec_sortref);
243244
externvoidinitialize_mergeclause_eclasses(PlannerInfo*root,
244245
RestrictInfo*restrictinfo);
245246
externvoidupdate_mergeclause_eclasses(PlannerInfo*root,

‎src/test/regress/expected/aggregates.out

Lines changed: 47 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
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 c1JOIN group_agg_pk c2ONc1.x=c2.x
12541254
GROUP BYc1.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+
SELECTc1.y,c1.xFROM group_agg_pk c1
1261+
JOIN group_agg_pk c2
1262+
ONc1.x=c2.x
1263+
GROUP BYc1.y,c1.x,c2.x;
1264+
EXPLAIN (COSTS OFF)
1265+
SELECTc1.y,c1.xFROM group_agg_pk c1
1266+
JOIN group_agg_pk c2
1267+
ONc1.x=c2.x
1268+
GROUP BYc1.y,c2.x,c1.x;
1269+
12561270
RESET enable_nestloop;
12571271
RESET enable_hashjoin;
12581272
DROPTABLE group_agg_pk;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp