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 261896bcd0f Fix errors on pathkeys on path
261896bcd0f is described below

commit 261896bcd0f4c08c68d76eee2992c79bab270fb9
Author: Jinbao Chen <[email protected]>
AuthorDate: Sun Dec 28 11:09:42 2025 +0800

    Fix errors on pathkeys on path
---
 src/backend/optimizer/util/pathnode.c            | 14 +----
 src/test/regress/expected/groupingsets.out       | 77 +++++++++++-------------
 src/test/regress/expected/select_distinct.out    | 20 +++---
 src/test/regress/expected/select_distinct_on.out | 48 ++++++---------
 src/test/regress/expected/subselect_gp.out       | 41 +++++++++----
 5 files changed, 93 insertions(+), 107 deletions(-)

diff --git a/src/backend/optimizer/util/pathnode.c 
b/src/backend/optimizer/util/pathnode.c
index ca3789da627..20f4f2e85c7 100644
--- a/src/backend/optimizer/util/pathnode.c
+++ b/src/backend/optimizer/util/pathnode.c
@@ -5093,19 +5093,7 @@ create_agg_path(PlannerInfo *root,
 
        if (aggstrategy == AGG_SORTED)
        {
-               /*
-                * 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);
-               else
-                       pathnode->path.pathkeys = subpath->pathkeys;    /* 
preserves order */
+               pathnode->path.pathkeys = subpath->pathkeys;    /* preserves 
order */
        }
        else
                pathnode->path.pathkeys = NIL;  /* output is unordered */
diff --git a/src/test/regress/expected/groupingsets.out 
b/src/test/regress/expected/groupingsets.out
index 239b30030c1..0844b33fd74 100644
--- a/src/test/regress/expected/groupingsets.out
+++ b/src/test/regress/expected/groupingsets.out
@@ -513,11 +513,12 @@ select * from (
 where x = 1 and q1 = 123;
                                         QUERY PLAN                             
           
 
------------------------------------------------------------------------------------------
- Gather Motion 3:1  (slice1; segments: 3)
+ Subquery Scan on ss
    Output: ss.x, ss.q1, ss.sum
-   ->  Subquery Scan on ss
-         Output: ss.x, ss.q1, ss.sum
-         Filter: ((ss.x = 1) AND (ss.q1 = 123))
+   Filter: ((ss.x = 1) AND (ss.q1 = 123))
+   ->  Gather Motion 3:1  (slice1; segments: 3)
+         Output: 1, i1.q1, (sum(i1.q2))
+         Merge Key: i1.q1
          ->  Finalize GroupAggregate
                Output: (1), i1.q1, sum(i1.q2)
                Group Key: 1, i1.q1, (GROUPINGSET_ID())
@@ -1677,26 +1678,22 @@ explain (costs off)
 BEGIN;
 SET LOCAL enable_hashagg = false;
 EXPLAIN (COSTS OFF) SELECT a, b, count(*), max(a), max(b) FROM gstest3 GROUP 
BY GROUPING SETS(a, b,()) ORDER BY a, b;
-                                  QUERY PLAN                                  
-------------------------------------------------------------------------------
- Gather Motion 3:1  (slice1; segments: 3)
-   Merge Key: a, b
-   ->  Sort
-         Sort Key: a, b
-         ->  Finalize GroupAggregate
-               Group Key: a, b, (GROUPINGSET_ID())
-               ->  Sort
-                     Sort Key: a, b, (GROUPINGSET_ID())
-                     ->  Redistribute Motion 3:3  (slice2; segments: 3)
-                           Hash Key: a, b, (GROUPINGSET_ID())
-                           ->  Partial GroupAggregate
-                                 Group Key: a
-                                 Group Key: ()
-                                 Sort Key: b
-                                   Group Key: b
-                                 ->  Index Scan using gstest3_pkey on gstest3
+                            QUERY PLAN                            
+------------------------------------------------------------------
+ Finalize GroupAggregate
+   Group Key: a, b, (GROUPINGSET_ID())
+   ->  Gather Motion 3:1  (slice1; segments: 3)
+         Merge Key: a, b, (GROUPINGSET_ID())
+         ->  Sort
+               Sort Key: a, b, (GROUPINGSET_ID())
+               ->  Partial GroupAggregate
+                     Group Key: a
+                     Group Key: ()
+                     Sort Key: b
+                       Group Key: b
+                     ->  Index Scan using gstest3_pkey on gstest3
  Optimizer: Postgres query optimizer
-(17 rows)
+(13 rows)
 
 SELECT a, b, count(*), max(a), max(b) FROM gstest3 GROUP BY GROUPING SETS(a, 
b,()) ORDER BY a, b;
  a | b | count | max | max 
@@ -1710,26 +1707,22 @@ SELECT a, b, count(*), max(a), max(b) FROM gstest3 
GROUP BY GROUPING SETS(a, b,(
 
 SET LOCAL enable_seqscan = false;
 EXPLAIN (COSTS OFF) SELECT a, b, count(*), max(a), max(b) FROM gstest3 GROUP 
BY GROUPING SETS(a, b,()) ORDER BY a, b;
-                                  QUERY PLAN                                  
-------------------------------------------------------------------------------
- Gather Motion 3:1  (slice1; segments: 3)
-   Merge Key: a, b
-   ->  Sort
-         Sort Key: a, b
-         ->  Finalize GroupAggregate
-               Group Key: a, b, (GROUPINGSET_ID())
-               ->  Sort
-                     Sort Key: a, b, (GROUPINGSET_ID())
-                     ->  Redistribute Motion 3:3  (slice2; segments: 3)
-                           Hash Key: a, b, (GROUPINGSET_ID())
-                           ->  Partial GroupAggregate
-                                 Group Key: a
-                                 Group Key: ()
-                                 Sort Key: b
-                                   Group Key: b
-                                 ->  Index Scan using gstest3_pkey on gstest3
+                            QUERY PLAN                            
+------------------------------------------------------------------
+ Finalize GroupAggregate
+   Group Key: a, b, (GROUPINGSET_ID())
+   ->  Gather Motion 3:1  (slice1; segments: 3)
+         Merge Key: a, b, (GROUPINGSET_ID())
+         ->  Sort
+               Sort Key: a, b, (GROUPINGSET_ID())
+               ->  Partial GroupAggregate
+                     Group Key: a
+                     Group Key: ()
+                     Sort Key: b
+                       Group Key: b
+                     ->  Index Scan using gstest3_pkey on gstest3
  Optimizer: Postgres query optimizer
-(17 rows)
+(13 rows)
 
 SELECT a, b, count(*), max(a), max(b) FROM gstest3 GROUP BY GROUPING SETS(a, 
b,()) ORDER BY a, b;
  a | b | count | max | max 
diff --git a/src/test/regress/expected/select_distinct.out 
b/src/test/regress/expected/select_distinct.out
index c5c85101d1c..79cae3885ff 100644
--- a/src/test/regress/expected/select_distinct.out
+++ b/src/test/regress/expected/select_distinct.out
@@ -188,22 +188,20 @@ SET enable_seqscan = 0;
 -- Check to see we get an incremental sort plan
 EXPLAIN (costs off)
 SELECT DISTINCT hundred, two FROM tenk1;
-                               QUERY PLAN                               
-------------------------------------------------------------------------
+                            QUERY PLAN                            
+------------------------------------------------------------------
  GroupAggregate
    Group Key: hundred, two
    ->  Gather Motion 3:1  (slice1; segments: 3)
          Merge Key: hundred, two
-         ->  Sort
-               Sort Key: hundred, two
-               ->  GroupAggregate
-                     Group Key: hundred, two
-                     ->  Sort
-                           Sort Key: hundred, two
-                           ->  Bitmap Heap Scan on tenk1
-                                 ->  Bitmap Index Scan on tenk1_hundred
+         ->  GroupAggregate
+               Group Key: hundred, two
+               ->  Sort
+                     Sort Key: hundred, two
+                     ->  Bitmap Heap Scan on tenk1
+                           ->  Bitmap Index Scan on tenk1_hundred
  Optimizer: Postgres query optimizer
-(13 rows)
+(11 rows)
 
 RESET enable_seqscan;
 SET enable_hashagg=TRUE;
diff --git a/src/test/regress/expected/select_distinct_on.out 
b/src/test/regress/expected/select_distinct_on.out
index 363591af133..f87fcee87d3 100644
--- a/src/test/regress/expected/select_distinct_on.out
+++ b/src/test/regress/expected/select_distinct_on.out
@@ -105,50 +105,38 @@ SELECT DISTINCT ON (four) four,two
 EXPLAIN (COSTS OFF)
 SELECT DISTINCT ON (four) four,two
    FROM tenk1 WHERE four = 0 ORDER BY 1,2;
-                               QUERY PLAN                               
-------------------------------------------------------------------------
- Gather Motion 3:1  (slice1; segments: 3)
-   Merge Key: two
-   ->  Sort
-         Sort Key: two
+                   QUERY PLAN                   
+------------------------------------------------
+ GroupAggregate
+   Group Key: four
+   ->  Gather Motion 3:1  (slice1; segments: 3)
+         Merge Key: two
          ->  GroupAggregate
                Group Key: four
                ->  Sort
                      Sort Key: two
-                     ->  Redistribute Motion 3:3  (slice2; segments: 3)
-                           Hash Key: four
-                           ->  GroupAggregate
-                                 Group Key: four
-                                 ->  Sort
-                                       Sort Key: two
-                                       ->  Seq Scan on tenk1
-                                             Filter: (four = 0)
+                     ->  Seq Scan on tenk1
+                           Filter: (four = 0)
  Optimizer: Postgres query optimizer
-(17 rows)
+(11 rows)
 
 -- Same again but use a column that is indexed so that we get an index scan
 -- then a limit
 EXPLAIN (COSTS OFF)
 SELECT DISTINCT ON (four) four,hundred
    FROM tenk1 WHERE four = 0 ORDER BY 1,2;
-                               QUERY PLAN                               
-------------------------------------------------------------------------
- Gather Motion 3:1  (slice1; segments: 3)
-   Merge Key: hundred
-   ->  Sort
-         Sort Key: hundred
+                   QUERY PLAN                   
+------------------------------------------------
+ GroupAggregate
+   Group Key: four
+   ->  Gather Motion 3:1  (slice1; segments: 3)
+         Merge Key: hundred
          ->  GroupAggregate
                Group Key: four
                ->  Sort
                      Sort Key: hundred
-                     ->  Redistribute Motion 3:3  (slice2; segments: 3)
-                           Hash Key: four
-                           ->  GroupAggregate
-                                 Group Key: four
-                                 ->  Sort
-                                       Sort Key: hundred
-                                       ->  Seq Scan on tenk1
-                                             Filter: (four = 0)
+                     ->  Seq Scan on tenk1
+                           Filter: (four = 0)
  Optimizer: Postgres query optimizer
-(17 rows)
+(11 rows)
 
diff --git a/src/test/regress/expected/subselect_gp.out 
b/src/test/regress/expected/subselect_gp.out
index 36f064c8003..5866e01393f 100644
--- a/src/test/regress/expected/subselect_gp.out
+++ b/src/test/regress/expected/subselect_gp.out
@@ -1132,21 +1132,40 @@ select * from t1 where a=1 and a=2 and a > (select t2.b 
from t2);
 (0 rows)
 
 explain select * from t1 where a=1 and a=2 and a > (select t2.b from t2);
-                   QUERY PLAN                   
-------------------------------------------------
- Result  (cost=1063.00..1063.01 rows=1 width=0)
-   One-Time Filter: false
- Settings:  optimizer_segments=3
- Optimizer status: Postgres query optimizer
-(4 rows)
+                                        QUERY PLAN                             
            
+-------------------------------------------------------------------------------------------
+ Gather Motion 1:1  (slice1; segments: 1)  (cost=1639.00..2154.52 rows=1 
width=4)
+   InitPlan 1 (returns $0)  (slice2)
+     ->  Gather Motion 3:1  (slice3; segments: 3)  (cost=0.00..1639.00 
rows=96300 width=4)
+           ->  Seq Scan on t2  (cost=0.00..355.00 rows=32100 width=4)
+   ->  Result  (cost=0.00..515.50 rows=1 width=4)
+         One-Time Filter: false
+         ->  Seq Scan on t1  (cost=0.00..515.50 rows=1 width=4)
+               Filter: ((a > $0) AND (a = 1))
+ Optimizer: Postgres query optimizer
+(9 rows)
 
 explain select * from t1 where a=1 and a=2 and a > (select t2.b from t2)
 union all
 select * from t1 where a=1 and a=2 and a > (select t2.b from t2);
-                QUERY PLAN                
-------------------------------------------
- Result  (cost=0.00..0.01 rows=1 width=0)
-   One-Time Filter: false
+                                              QUERY PLAN                       
                        
+-------------------------------------------------------------------------------------------------------
+ Gather Motion 1:1  (slice1; segments: 1)  (cost=1639.00..4309.09 rows=6 
width=4)
+   ->  Append  (cost=1639.00..4309.01 rows=2 width=4)
+         ->  Result  (cost=1639.00..2154.50 rows=1 width=4)
+               One-Time Filter: false
+               InitPlan 1 (returns $0)  (slice2)
+                 ->  Gather Motion 3:1  (slice3; segments: 3)  
(cost=0.00..1639.00 rows=96300 width=4)
+                       ->  Seq Scan on t2  (cost=0.00..355.00 rows=32100 
width=4)
+               ->  Seq Scan on t1  (cost=1639.00..2154.50 rows=1 width=4)
+                     Filter: ((a > $0) AND (a = 1))
+         ->  Result  (cost=1639.00..2154.50 rows=1 width=4)
+               One-Time Filter: false
+               InitPlan 2 (returns $1)  (slice4)
+                 ->  Gather Motion 3:1  (slice5; segments: 3)  
(cost=0.00..1639.00 rows=96300 width=4)
+                       ->  Seq Scan on t2 t2_1  (cost=0.00..355.00 rows=32100 
width=4)
+               ->  Seq Scan on t1 t1_1  (cost=1639.00..2154.50 rows=1 width=4)
+                     Filter: ((a > $1) AND (a = 1))
  Optimizer: Postgres query optimizer
 (3 rows)
 


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

Reply via email to