This is an automated email from the ASF dual-hosted git repository.

chenjinbao1989 pushed a commit to branch cbdb-postgres-merge
in repository https://gitbox.apache.org/repos/asf/cloudberry.git


The following commit(s) were added to refs/heads/cbdb-postgres-merge by this 
push:
     new 87ee46d8452 Add ORDER BY / DISTINCT columns to group keys
87ee46d8452 is described below

commit 87ee46d84528209bf32886c03e930769200b4d09
Author: Jinbao Chen <[email protected]>
AuthorDate: Mon Dec 29 21:34:08 2025 +0800

    Add ORDER BY / DISTINCT columns to group keys
---
 src/backend/cdb/cdbgroupingpaths.c                | 10 ++-
 src/backend/optimizer/plan/planner.c              |  6 --
 src/backend/optimizer/util/pathnode.c             | 15 ++++-
 src/test/regress/expected/aggregates.out          |  4 +-
 src/test/regress/expected/bitmap_index.out        | 75 ++++++++++++-----------
 src/test/regress/expected/gp_aggregates.out       |  2 +-
 src/test/regress/expected/gp_array_agg.out        |  2 +-
 src/test/regress/expected/gp_dqa.out              | 16 ++---
 src/test/regress/expected/partition_aggregate.out | 18 +++---
 src/test/regress/expected/tuplesort.out           | 52 ++++++++--------
 10 files changed, 110 insertions(+), 90 deletions(-)

diff --git a/src/backend/cdb/cdbgroupingpaths.c 
b/src/backend/cdb/cdbgroupingpaths.c
index d87af54f52b..f682b359ffe 100644
--- a/src/backend/cdb/cdbgroupingpaths.c
+++ b/src/backend/cdb/cdbgroupingpaths.c
@@ -432,15 +432,21 @@ cdb_create_multistage_grouping_paths(PlannerInfo *root,
                 * GROUPINGSET_ID() column.
                 */
                ctx.final_needed_pathkeys = make_pathkeys_for_sortclauses(root, 
gcls, tlist);
+               ctx.final_sort_pathkeys = ctx.final_needed_pathkeys;
        }
        else
        {
                ctx.partial_grouping_target = partial_grouping_target;
                ctx.final_groupClause = root->processed_groupClause;
-               ctx.final_needed_pathkeys = root->group_pathkeys;
+               if (list_length(root->group_pathkeys) > 
root->num_groupby_pathkeys)
+                       ctx.final_needed_pathkeys = 
list_copy_head(root->group_pathkeys,
+                                                                               
                           root->num_groupby_pathkeys);
+               else
+                       ctx.final_needed_pathkeys = root->group_pathkeys;    /* 
preserves order */
                ctx.gsetid_sortref = 0;
+               ctx.final_sort_pathkeys = root->group_pathkeys;
+
        }
-       ctx.final_sort_pathkeys = ctx.final_needed_pathkeys;
        ctx.final_group_tles = 
get_common_group_tles(ctx.partial_grouping_target,
                                                                                
                 ctx.final_groupClause,
                                                                                
                 NIL);
diff --git a/src/backend/optimizer/plan/planner.c 
b/src/backend/optimizer/plan/planner.c
index 5aaccef5423..071957b8a1a 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -3950,7 +3950,6 @@ reorder_grouping_sets(List *groupingSets, List 
*sortclause)
  *             Returns true if any PathKey in 'keys' has an EquivalenceClass
  *             containing a volatile function.  Otherwise returns false.
  */
-#if 0
 static bool
 has_volatile_pathkey(List *keys)
 {
@@ -3966,7 +3965,6 @@ has_volatile_pathkey(List *keys)
 
        return false;
 }
-#endif
 
 /*
  * adjust_group_pathkeys_for_groupagg
@@ -3997,7 +3995,6 @@ has_volatile_pathkey(List *keys)
  * query contains, we always force Aggrefs with volatile functions to perform
  * their own sorts.
  */
-#if 0
 static void
 adjust_group_pathkeys_for_groupagg(PlannerInfo *root)
 {
@@ -4218,7 +4215,6 @@ adjust_group_pathkeys_for_groupagg(PlannerInfo *root)
                }
        }
 }
-#endif
 
 /*
  * Compute query_pathkeys and other pathkeys during plan generation
@@ -4297,10 +4293,8 @@ standard_qp_callback(PlannerInfo *root, void *extra)
                         * adjust_group_pathkeys_for_groupagg add distinct key 
to group path key,
                         * It should cause error for muti-stage aggregate.
                         */
-#if 0
                        if (root->numOrderedAggs > 0)
                                adjust_group_pathkeys_for_groupagg(root);
-#endif
                }
        }
        else
diff --git a/src/backend/optimizer/util/pathnode.c 
b/src/backend/optimizer/util/pathnode.c
index 20f4f2e85c7..7b0e25e87d5 100644
--- a/src/backend/optimizer/util/pathnode.c
+++ b/src/backend/optimizer/util/pathnode.c
@@ -5093,7 +5093,20 @@ create_agg_path(PlannerInfo *root,
 
        if (aggstrategy == AGG_SORTED)
        {
-               pathnode->path.pathkeys = subpath->pathkeys;    /* preserves 
order */
+               /*
+                * Attempt to preserve the order of the subpath.  Additional 
pathkeys
+                * may have been added in adjust_group_pathkeys_for_groupagg() 
to
+                * support ORDER BY / DISTINCT aggregates.  Pathkeys added there
+                * belong to columns within the aggregate function, so we must 
strip
+                * these additional pathkeys off as those columns are 
unavailable
+                * above the aggregate node.
+                */
+               if (list_length(subpath->pathkeys) > root->num_groupby_pathkeys)
+                       pathnode->path.pathkeys = 
list_copy_head(subpath->pathkeys,
+                               root->num_groupby_pathkeys > 
list_length(groupClause) ? 
+                                root->num_groupby_pathkeys : 
list_length(groupClause));
+               else
+                       pathnode->path.pathkeys = subpath->pathkeys;    /* 
preserves order */
        }
        else
                pathnode->path.pathkeys = NIL;  /* output is unordered */
diff --git a/src/test/regress/expected/aggregates.out 
b/src/test/regress/expected/aggregates.out
index aad886480f7..46bc187644e 100644
--- a/src/test/regress/expected/aggregates.out
+++ b/src/test/regress/expected/aggregates.out
@@ -1597,7 +1597,7 @@ group by ten;
    ->  GroupAggregate
          Group Key: ten
          ->  Sort
-               Sort Key: ten
+               Sort Key: ten, two, four
                ->  Redistribute Motion 3:3  (slice2; segments: 3)
                      Hash Key: ten
                      ->  Seq Scan on tenk1
@@ -1620,7 +1620,7 @@ group by ten;
    ->  GroupAggregate
          Group Key: ten
          ->  Sort
-               Sort Key: ten
+               Sort Key: ten, four, two
                ->  Redistribute Motion 3:3  (slice2; segments: 3)
                      Hash Key: ten
                      ->  Seq Scan on tenk1
diff --git a/src/test/regress/expected/bitmap_index.out 
b/src/test/regress/expected/bitmap_index.out
index 37ac2505490..61f8e7323ef 100644
--- a/src/test/regress/expected/bitmap_index.out
+++ b/src/test/regress/expected/bitmap_index.out
@@ -811,22 +811,23 @@ CREATE INDEX ON test_bmselec USING bitmap(type);
 ANALYZE test_bmselec;
 -- it used to choose bitmap index over seq scan, which not right.
 explain (analyze, verbose) select * from test_bmselec where type < 500;
-                                                          QUERY PLAN           
                                                
--------------------------------------------------------------------------------------------------------------------------------
- Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..562.63 rows=5097 
width=41) (actual time=0.325..5.944 rows=5000 loops=1)
+                                                               QUERY PLAN      
                                                          
+-----------------------------------------------------------------------------------------------------------------------------------------
+ Gather Motion 3:1  (slice1; segments: 3)  (cost=4.43..170.34 rows=5023 
width=41) (actual time=22.223..63.521 rows=5000 loops=1)
    Output: id, type, msg
-   ->  Seq Scan on public.test_bmselec  (cost=0.00..494.67 rows=1699 width=41) 
(actual time=0.029..5.094 rows=1693 loops=1)
+   ->  Bitmap Heap Scan on public.test_bmselec  (cost=4.43..103.36 rows=1674 
width=41) (actual time=20.060..58.812 rows=1693 loops=1)
          Output: id, type, msg
-         Filter: (test_bmselec.type < 500)
-         Rows Removed by Filter: 31769
- Planning Time: 0.620 ms
-   (slice0)    Executor memory: 36K bytes.
-   (slice1)    Executor memory: 36K bytes avg x 3 workers, 36K bytes max 
(seg0).
+         Recheck Cond: (test_bmselec.type < 500)
+         ->  Bitmap Index Scan on test_bmselec_type_idx  (cost=0.00..4.01 
rows=1674 width=0) (actual time=12.196..12.196 rows=1 loops=1)
+               Index Cond: (test_bmselec.type < 500)
+ Settings: enable_seqscan = 'on', enable_indexscan = 'on', enable_bitmapscan = 
'on'
+ Planning Time: 0.360 ms
+   (slice0)    Executor memory: 118K bytes.
+   (slice1)    Executor memory: 63172K bytes avg x 3x(0) workers, 63382K bytes 
max (seg0).
  Memory used:  128000kB
  Optimizer: Postgres query optimizer
- Settings: enable_bitmapscan=on, enable_indexscan=on, enable_seqscan=on
- Execution Time: 6.606 ms
-(13 rows)
+ Execution Time: 66.514 ms
+(14 rows)
 
 SET enable_seqscan = OFF;
 SET enable_bitmapscan = OFF;
@@ -860,22 +861,23 @@ CREATE INDEX ON test_bmsparse USING bitmap(type);
 ANALYZE test_bmsparse;
 -- select lots of rows but on small part of distinct values, should use seq 
scan
 explain (analyze, verbose) select * from test_bmsparse where type < 200;
-                                                            QUERY PLAN         
                                                    
------------------------------------------------------------------------------------------------------------------------------------
- Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..1559.93 rows=79895 
width=41) (actual time=0.846..27.219 rows=80400 loops=1)
+                                                                QUERY PLAN     
                                                            
+-------------------------------------------------------------------------------------------------------------------------------------------
+ Gather Motion 3:1  (slice1; segments: 3)  (cost=10.67..1486.92 rows=79900 
width=41) (actual time=24.016..242.976 rows=80400 loops=1)
    Output: id, type, msg
-   ->  Seq Scan on public.test_bmsparse  (cost=0.00..494.67 rows=26632 
width=41) (actual time=0.029..7.373 rows=26975 loops=1)
+   ->  Bitmap Heap Scan on public.test_bmsparse  (cost=10.67..421.58 
rows=26633 width=41) (actual time=11.006..231.034 rows=26975 loops=1)
          Output: id, type, msg
-         Filter: (test_bmsparse.type < 200)
-         Rows Removed by Filter: 6596
- Planning Time: 0.549 ms
-   (slice0)    Executor memory: 36K bytes.
-   (slice1)    Executor memory: 36K bytes avg x 3 workers, 36K bytes max 
(seg0).
+         Recheck Cond: (test_bmsparse.type < 200)
+         ->  Bitmap Index Scan on test_bmsparse_type_idx  (cost=0.00..4.01 
rows=26633 width=0) (actual time=8.447..8.448 rows=1 loops=1)
+               Index Cond: (test_bmsparse.type < 200)
+ Settings: enable_seqscan = 'on', enable_indexscan = 'on', enable_bitmapscan = 
'on'
+ Planning Time: 0.369 ms
+   (slice0)    Executor memory: 118K bytes.
+   (slice1)    Executor memory: 26238K bytes avg x 3x(0) workers, 26238K bytes 
max (seg0).
  Memory used:  128000kB
  Optimizer: Postgres query optimizer
- Settings: enable_bitmapscan=on, enable_indexscan=on, enable_seqscan=on
- Execution Time: 31.714 ms
-(13 rows)
+ Execution Time: 246.986 ms
+(14 rows)
 
 SET enable_seqscan = OFF;
 SET enable_bitmapscan = OFF;
@@ -900,22 +902,23 @@ SET enable_seqscan = ON;
 SET enable_bitmapscan = ON;
 -- select small part of table but on lots of distinct values, should use seq 
scan
 explain (analyze, verbose) select * from test_bmsparse where type > 500;
-                                                           QUERY PLAN          
                                                  
----------------------------------------------------------------------------------------------------------------------------------
- Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..748.40 rows=19030 
width=41) (actual time=0.327..9.309 rows=18998 loops=1)
+                                                                 QUERY PLAN    
                                                             
+--------------------------------------------------------------------------------------------------------------------------------------------
+ Gather Motion 3:1  (slice1; segments: 3)  (cost=5.59..416.92 rows=19047 
width=41) (actual time=357.472..921.232 rows=18998 loops=1)
    Output: id, type, msg
-   ->  Seq Scan on public.test_bmsparse  (cost=0.00..494.67 rows=6343 
width=41) (actual time=0.042..5.347 rows=6448 loops=1)
+   ->  Bitmap Heap Scan on public.test_bmsparse  (cost=5.59..162.96 rows=6349 
width=41) (actual time=360.199..915.270 rows=6448 loops=1)
          Output: id, type, msg
-         Filter: (test_bmsparse.type > 500)
-         Rows Removed by Filter: 26979
- Planning Time: 0.330 ms
-   (slice0)    Executor memory: 36K bytes.
-   (slice1)    Executor memory: 36K bytes avg x 3 workers, 36K bytes max 
(seg0).
+         Recheck Cond: (test_bmsparse.type > 500)
+         ->  Bitmap Index Scan on test_bmsparse_type_idx  (cost=0.00..4.01 
rows=6349 width=0) (actual time=317.088..317.089 rows=1 loops=1)
+               Index Cond: (test_bmsparse.type > 500)
+ Settings: enable_seqscan = 'on', enable_indexscan = 'on', enable_bitmapscan = 
'on'
+ Planning Time: 0.338 ms
+   (slice0)    Executor memory: 118K bytes.
+   (slice1)    Executor memory: 401129K bytes avg x 3x(0) workers, 408418K 
bytes max (seg1).
  Memory used:  128000kB
  Optimizer: Postgres query optimizer
- Settings: enable_bitmapscan=on, enable_indexscan=on, enable_seqscan=on
- Execution Time: 10.469 ms
-(13 rows)
+ Execution Time: 942.066 ms
+(14 rows)
 
 SET enable_seqscan = OFF;
 SET enable_bitmapscan = OFF;
diff --git a/src/test/regress/expected/gp_aggregates.out 
b/src/test/regress/expected/gp_aggregates.out
index ceea42befa4..b4760a78540 100644
--- a/src/test/regress/expected/gp_aggregates.out
+++ b/src/test/regress/expected/gp_aggregates.out
@@ -379,7 +379,7 @@ select count(distinct j), count(distinct k), count(distinct 
m) from (select j,k,
    ->  GroupAggregate
          Group Key: multiagg_with_subquery.j
          ->  Sort
-               Sort Key: multiagg_with_subquery.j
+               Sort Key: multiagg_with_subquery.j, multiagg_with_subquery.k
                ->  Redistribute Motion 3:3  (slice2; segments: 3)
                      Hash Key: multiagg_with_subquery.j
                      ->  HashAggregate
diff --git a/src/test/regress/expected/gp_array_agg.out 
b/src/test/regress/expected/gp_array_agg.out
index b1ead57022d..4ae1f4b0b7d 100644
--- a/src/test/regress/expected/gp_array_agg.out
+++ b/src/test/regress/expected/gp_array_agg.out
@@ -210,7 +210,7 @@ explain (costs off) select * from v_pagg_test order by y;
    ->  GroupAggregate
          Group Key: pagg_test.y
          ->  Sort
-               Sort Key: pagg_test.y
+               Sort Key: pagg_test.y, 
(((unnest(regexp_split_to_array((string_agg((pagg_test.x)::text, ','::text)), 
','::text))))::integer)
                ->  Result
                      ->  ProjectSet
                            ->  Finalize HashAggregate
diff --git a/src/test/regress/expected/gp_dqa.out 
b/src/test/regress/expected/gp_dqa.out
index e5d42dad5a7..be2bcf591c3 100644
--- a/src/test/regress/expected/gp_dqa.out
+++ b/src/test/regress/expected/gp_dqa.out
@@ -1937,7 +1937,7 @@ explain (costs off) select count(distinct d) from dqa_t1 
group by i;
          ->  Partial GroupAggregate
                Group Key: i
                ->  Sort
-                     Sort Key: i
+                     Sort Key: i, d
                      ->  Seq Scan on dqa_t1
  Optimizer: Postgres query optimizer
 (10 rows)
@@ -2691,7 +2691,7 @@ explain (verbose on, costs off)select sum(Distinct a), 
count(b), sum(c) from dqa
          Group Key: dqa_f3.e
          ->  Sort
                Output: e, a, b, c
-               Sort Key: dqa_f3.e
+               Sort Key: dqa_f3.e, dqa_f3.a
                ->  Redistribute Motion 3:3  (slice2; segments: 3)
                      Output: e, a, b, c
                      Hash Key: e
@@ -2723,7 +2723,7 @@ explain (verbose on, costs off) select sum(Distinct e), 
count(b), sum(c) from dq
          Group Key: dqa_f3.a
          ->  Sort
                Output: a, e, b, c
-               Sort Key: dqa_f3.a
+               Sort Key: dqa_f3.a, dqa_f3.e
                ->  Seq Scan on public.dqa_f3
                      Output: a, e, b, c
  Settings: enable_groupagg = 'off', enable_hashagg = 'on', enable_parallel = 
'off', gp_motion_cost_per_row = '2', optimizer = 'off'
@@ -2772,7 +2772,7 @@ explain (verbose on, costs off) select sum(Distinct c), 
count(a), sum(d) from dq
          Group Key: dqa_f3.b
          ->  Sort
                Output: b, c, a, d
-               Sort Key: dqa_f3.b
+               Sort Key: dqa_f3.b, dqa_f3.c
                ->  Redistribute Motion 3:3  (slice2; segments: 3)
                      Output: b, c, a, d
                      Hash Key: b
@@ -2803,7 +2803,7 @@ explain (verbose on, costs off) select sum(Distinct c), 
count(a), sum(d) from dq
          Group Key: dqa_f3.b
          ->  Sort
                Output: b, c, a, d
-               Sort Key: dqa_f3.b
+               Sort Key: dqa_f3.b, dqa_f3.c
                ->  Redistribute Motion 3:3  (slice2; segments: 3)
                      Output: b, c, a, d
                      Hash Key: b
@@ -2839,7 +2839,7 @@ explain (verbose on, costs off) select distinct 
sum(Distinct c), count(a), sum(d
                      Group Key: dqa_f3.b
                      ->  Sort
                            Output: b, c, a, d
-                           Sort Key: dqa_f3.b
+                           Sort Key: dqa_f3.b, dqa_f3.c
                            ->  Redistribute Motion 3:3  (slice2; segments: 3)
                                  Output: b, c, a, d
                                  Hash Key: b
@@ -2870,7 +2870,7 @@ explain (verbose on, costs off) select sum(Distinct c), 
count(a), sum(d) from dq
          Filter: (avg(dqa_f3.e) > '3'::numeric)
          ->  Sort
                Output: b, c, a, d, e
-               Sort Key: dqa_f3.b
+               Sort Key: dqa_f3.b, dqa_f3.c
                ->  Redistribute Motion 3:3  (slice2; segments: 3)
                      Output: b, c, a, d, e
                      Hash Key: b
@@ -2908,7 +2908,7 @@ explain (verbose on, costs off) select sum(Distinct c), 
count(a), sum(d) from dq
          Group Key: dqa_f3.b
          ->  Sort
                Output: b, c, a, d
-               Sort Key: dqa_f3.b
+               Sort Key: dqa_f3.b, dqa_f3.c
                ->  Redistribute Motion 3:3  (slice2; segments: 3)
                      Output: b, c, a, d
                      Hash Key: b
diff --git a/src/test/regress/expected/partition_aggregate.out 
b/src/test/regress/expected/partition_aggregate.out
index 8722556d78c..9d0d1d4f9c1 100644
--- a/src/test/regress/expected/partition_aggregate.out
+++ b/src/test/regress/expected/partition_aggregate.out
@@ -435,21 +435,21 @@ SELECT c, sum(b order by a) FROM pagg_tab GROUP BY c 
ORDER BY 1, 2;
                ->  GroupAggregate
                      Group Key: pagg_tab.c
                      ->  Sort
-                           Sort Key: pagg_tab.c
+                           Sort Key: pagg_tab.c, pagg_tab.a
                            ->  Redistribute Motion 3:3  (slice2; segments: 3)
                                  Hash Key: pagg_tab.c
                                  ->  Seq Scan on pagg_tab_p1 pagg_tab
                ->  GroupAggregate
                      Group Key: pagg_tab_1.c
                      ->  Sort
-                           Sort Key: pagg_tab_1.c
+                           Sort Key: pagg_tab_1.c, pagg_tab_1.a
                            ->  Redistribute Motion 3:3  (slice3; segments: 3)
                                  Hash Key: pagg_tab_1.c
                                  ->  Seq Scan on pagg_tab_p2 pagg_tab_1
                ->  GroupAggregate
                      Group Key: pagg_tab_2.c
                      ->  Sort
-                           Sort Key: pagg_tab_2.c
+                           Sort Key: pagg_tab_2.c, pagg_tab_2.a
                            ->  Redistribute Motion 3:3  (slice4; segments: 3)
                                  Hash Key: pagg_tab_2.c
                                  ->  Seq Scan on pagg_tab_p3 pagg_tab_2
@@ -1127,13 +1127,13 @@ SELECT a, sum(b), array_agg(distinct c), count(*) FROM 
pagg_tab_ml GROUP BY a HA
                      Group Key: pagg_tab_ml.a
                      Filter: (avg(pagg_tab_ml.b) < '3'::numeric)
                      ->  Sort
-                           Sort Key: pagg_tab_ml.a
+                           Sort Key: pagg_tab_ml.a, pagg_tab_ml.c
                            ->  Seq Scan on pagg_tab_ml_p1 pagg_tab_ml
                ->  GroupAggregate
                      Group Key: pagg_tab_ml_2.a
                      Filter: (avg(pagg_tab_ml_2.b) < '3'::numeric)
                      ->  Sort
-                           Sort Key: pagg_tab_ml_2.a
+                           Sort Key: pagg_tab_ml_2.a, pagg_tab_ml_2.c
                            ->  Append
                                  ->  Seq Scan on pagg_tab_ml_p2_s1 
pagg_tab_ml_2
                                  ->  Seq Scan on pagg_tab_ml_p2_s2 
pagg_tab_ml_3
@@ -1141,7 +1141,7 @@ SELECT a, sum(b), array_agg(distinct c), count(*) FROM 
pagg_tab_ml GROUP BY a HA
                      Group Key: pagg_tab_ml_5.a
                      Filter: (avg(pagg_tab_ml_5.b) < '3'::numeric)
                      ->  Sort
-                           Sort Key: pagg_tab_ml_5.a
+                           Sort Key: pagg_tab_ml_5.a, pagg_tab_ml_5.c
                            ->  Append
                                  ->  Seq Scan on pagg_tab_ml_p3_s1 
pagg_tab_ml_5
                                  ->  Seq Scan on pagg_tab_ml_p3_s2 
pagg_tab_ml_6
@@ -1173,13 +1173,13 @@ SELECT a, sum(b), array_agg(distinct c), count(*) FROM 
pagg_tab_ml GROUP BY a HA
                Group Key: pagg_tab_ml.a
                Filter: (avg(pagg_tab_ml.b) < '3'::numeric)
                ->  Sort
-                     Sort Key: pagg_tab_ml.a
+                     Sort Key: pagg_tab_ml.a, pagg_tab_ml.c
                      ->  Seq Scan on pagg_tab_ml_p1 pagg_tab_ml
          ->  GroupAggregate
                Group Key: pagg_tab_ml_2.a
                Filter: (avg(pagg_tab_ml_2.b) < '3'::numeric)
                ->  Sort
-                     Sort Key: pagg_tab_ml_2.a
+                     Sort Key: pagg_tab_ml_2.a, pagg_tab_ml_2.c
                      ->  Append
                            ->  Seq Scan on pagg_tab_ml_p2_s1 pagg_tab_ml_2
                            ->  Seq Scan on pagg_tab_ml_p2_s2 pagg_tab_ml_3
@@ -1187,7 +1187,7 @@ SELECT a, sum(b), array_agg(distinct c), count(*) FROM 
pagg_tab_ml GROUP BY a HA
                Group Key: pagg_tab_ml_5.a
                Filter: (avg(pagg_tab_ml_5.b) < '3'::numeric)
                ->  Sort
-                     Sort Key: pagg_tab_ml_5.a
+                     Sort Key: pagg_tab_ml_5.a, pagg_tab_ml_5.c
                      ->  Append
                            ->  Seq Scan on pagg_tab_ml_p3_s1 pagg_tab_ml_5
                            ->  Seq Scan on pagg_tab_ml_p3_s2 pagg_tab_ml_6
diff --git a/src/test/regress/expected/tuplesort.out 
b/src/test/regress/expected/tuplesort.out
index 47b5a8ddb21..d172dd85c2a 100644
--- a/src/test/regress/expected/tuplesort.out
+++ b/src/test/regress/expected/tuplesort.out
@@ -546,18 +546,20 @@ EXPLAIN (COSTS OFF) :qry;
                      ->  GroupAggregate
                            Group Key: a.col12
                            Filter: (count(*) > 1)
-                           ->  Merge Join
-                                 Merge Cond: (a.col12 = b.col12)
-                                 ->  Sort
-                                       Sort Key: a.col12 DESC
-                                       ->  Redistribute Motion 3:3  (slice2; 
segments: 3)
-                                             Hash Key: a.col12
-                                             ->  Seq Scan on test_mark_restore 
a
-                                 ->  Sort
-                                       Sort Key: b.col12 DESC
-                                       ->  Redistribute Motion 3:3  (slice3; 
segments: 3)
-                                             Hash Key: b.col12
-                                             ->  Seq Scan on test_mark_restore 
b
+                           ->  Sort
+                                 Sort Key: a.col12 DESC, a.col1
+                                 ->  Merge Join
+                                       Merge Cond: (a.col12 = b.col12)
+                                       ->  Sort
+                                             Sort Key: a.col12 DESC
+                                             ->  Redistribute Motion 3:3  
(slice2; segments: 3)
+                                                   Hash Key: a.col12
+                                                   ->  Seq Scan on 
test_mark_restore a
+                                       ->  Sort
+                                             Sort Key: b.col12 DESC
+                                             ->  Redistribute Motion 3:3  
(slice3; segments: 3)
+                                                   Hash Key: b.col12
+                                                   ->  Seq Scan on 
test_mark_restore b
  Optimizer: Postgres query optimizer
 (22 rows)
 
@@ -590,18 +592,20 @@ EXPLAIN (COSTS OFF) :qry;
                      ->  GroupAggregate
                            Group Key: a.col12
                            Filter: (count(*) > 1)
-                           ->  Merge Join
-                                 Merge Cond: (a.col12 = b.col12)
-                                 ->  Sort
-                                       Sort Key: a.col12 DESC
-                                       ->  Redistribute Motion 3:3  (slice2; 
segments: 3)
-                                             Hash Key: a.col12
-                                             ->  Seq Scan on test_mark_restore 
a
-                                 ->  Sort
-                                       Sort Key: b.col12 DESC
-                                       ->  Redistribute Motion 3:3  (slice3; 
segments: 3)
-                                             Hash Key: b.col12
-                                             ->  Seq Scan on test_mark_restore 
b
+                           ->  Sort
+                                 Sort Key: a.col12 DESC, a.col1
+                                 ->  Merge Join
+                                       Merge Cond: (a.col12 = b.col12)
+                                       ->  Sort
+                                             Sort Key: a.col12 DESC
+                                             ->  Redistribute Motion 3:3  
(slice2; segments: 3)
+                                                   Hash Key: a.col12
+                                                   ->  Seq Scan on 
test_mark_restore a
+                                       ->  Sort
+                                             Sort Key: b.col12 DESC
+                                             ->  Redistribute Motion 3:3  
(slice3; segments: 3)
+                                                   Hash Key: b.col12
+                                                   ->  Seq Scan on 
test_mark_restore b
  Optimizer: Postgres query optimizer
 (22 rows)
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to