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 91c7a2d06f2 Fix some error
91c7a2d06f2 is described below
commit 91c7a2d06f2a7a1f8c2dfba491f1d70d88575c74
Author: Jinbao Chen <[email protected]>
AuthorDate: Sat Nov 15 23:04:41 2025 +0800
Fix some error
---
src/backend/cdb/cdbgroupingpaths.c | 5 +-
src/backend/parser/parse_clause.c | 4 +-
src/test/regress/expected/incremental_sort.out | 75 +++----------
src/test/regress/expected/select_distinct.out | 140 +++++++++++++------------
src/test/regress/expected/select_into.out | 9 +-
src/test/regress/expected/union.out | 9 +-
src/test/regress/serial_schedule | 18 ++--
src/test/regress/sql/incremental_sort.sql | 3 -
8 files changed, 108 insertions(+), 155 deletions(-)
diff --git a/src/backend/cdb/cdbgroupingpaths.c
b/src/backend/cdb/cdbgroupingpaths.c
index b393882c9a2..623afd4a371 100644
--- a/src/backend/cdb/cdbgroupingpaths.c
+++ b/src/backend/cdb/cdbgroupingpaths.c
@@ -87,6 +87,7 @@ typedef struct
{
/* From the Query */
bool hasAggs;
+ bool hasDistinctOn;
List *groupClause; /* a list of SortGroupClause's */
List *groupingSets; /* a list of GroupingSet's if present */
List *group_tles;
@@ -312,6 +313,7 @@ cdb_create_multistage_grouping_paths(PlannerInfo *root,
ctx.strat = strat;
ctx.hasAggs = parse->hasAggs;
+ ctx.hasDistinctOn = parse->hasDistinctOn;
ctx.groupClause = root->processed_groupClause;
ctx.groupingSets = parse->groupingSets;
ctx.havingQual = havingQual;
@@ -612,6 +614,7 @@ cdb_create_twostage_distinct_paths(PlannerInfo *root,
* handled in the grouping stage.
*/
ctx.hasAggs = false;
+ ctx.hasDistinctOn = true;
ctx.groupingSets = NIL;
ctx.havingQual = NULL;
ctx.groupClause = root->processed_distinctClause;
@@ -1040,7 +1043,7 @@ add_first_stage_group_agg_path(PlannerInfo *root,
ctx->agg_partial_costs);
add_path(ctx->partial_rel, first_stage_agg_path, root);
}
- else if (ctx->hasAggs || ctx->groupClause)
+ else if (ctx->hasAggs || ctx->groupClause || ctx->hasDistinctOn)
{
add_path(ctx->partial_rel,
(Path *) create_agg_path(root,
diff --git a/src/backend/parser/parse_clause.c
b/src/backend/parser/parse_clause.c
index 4179aaad30c..8e4fd533b05 100644
--- a/src/backend/parser/parse_clause.c
+++ b/src/backend/parser/parse_clause.c
@@ -564,7 +564,9 @@ transformRangeSubselect(ParseState *pstate, RangeSubselect
*r)
* might still be required (if there is an all-tables locking clause).
*/
query = parse_sub_analyze(r->subquery, pstate, NULL,
-
getLockedRefname(pstate, r->alias->aliasname),
+
getLockedRefname(pstate,
+
r->alias == NULL ? NULL :
+
r->alias->aliasname),
true);
/* Restore state */
diff --git a/src/test/regress/expected/incremental_sort.out
b/src/test/regress/expected/incremental_sort.out
index 623ddedc6e5..55b93bc066d 100644
--- a/src/test/regress/expected/incremental_sort.out
+++ b/src/test/regress/expected/incremental_sort.out
@@ -1,4 +1,3 @@
-<<<<<<< HEAD
-- TODO: incremental sort is turned off by default, because it may have
-- wrong result for some core case. Turn it on to run the existing tests
-- and minimize the difference from upstream.
@@ -12,16 +11,15 @@ select * from (select * from tenk1 order by four) t order
by four, ten;
------------------------------------------
Gather Motion 3:1 (slice1; segments: 3)
Merge Key: tenk1.four, tenk1.ten
- -> Sort
+ -> Incremental Sort
Sort Key: tenk1.four, tenk1.ten
+ Presorted Key: tenk1.four
-> Sort
Sort Key: tenk1.four
-> Seq Scan on tenk1
Optimizer: Postgres query optimizer
-(8 rows)
+(9 rows)
-=======
->>>>>>> REL_16_9
-- When there is a LIMIT clause, incremental sort is beneficial because
-- it only has to sort some of the groups, and not the entire table.
explain (costs off)
@@ -51,13 +49,14 @@ select * from (select * from tenk1 order by four) t order
by four, ten;
------------------------------------------
Gather Motion 3:1 (slice1; segments: 3)
Merge Key: tenk1.four, tenk1.ten
- -> Sort
+ -> Incremental Sort
Sort Key: tenk1.four, tenk1.ten
+ Presorted Key: tenk1.four
-> Sort
Sort Key: tenk1.four
-> Seq Scan on tenk1
Optimizer: Postgres query optimizer
-(8 rows)
+(9 rows)
reset planner_work_mem;
create table t(a integer, b integer);
@@ -1578,22 +1577,17 @@ explain (costs off) select a,b,sum(c) from t group by
1,2 order by 1,2,3 limit 1
-> Gather Motion 3:1 (slice1; segments: 3)
Merge Key: a, b, (sum(c))
-> Limit
- -> Incremental Sort
+ -> Sort
Sort Key: a, b, (sum(c))
- Presorted Key: a, b
- -> GroupAggregate
+ -> HashAggregate
Group Key: a, b
- -> Incremental Sort
- Sort Key: a, b
- Presorted Key: a
- -> Index Scan using t_a_idx on t
+ -> Seq Scan on t
Optimizer: Postgres query optimizer
-(14 rows)
+(10 rows)
-- Incremental sort vs. set operations with varno 0
set enable_hashagg to off;
explain (costs off) select * from t union select * from t order by 1,3;
-<<<<<<< HEAD
QUERY PLAN
------------------------------------------------------------------------
Gather Motion 3:1 (slice1; segments: 3)
@@ -1611,27 +1605,10 @@ explain (costs off) select * from t union select * from
t order by 1,3;
-> Seq Scan on t
-> Seq Scan on t t_1
(15 rows)
-=======
- QUERY PLAN
-----------------------------------------------------------
- Incremental Sort
- Sort Key: t.a, t.c
- Presorted Key: t.a
- -> Unique
- -> Sort
- Sort Key: t.a, t.b, t.c
- -> Gather
- Workers Planned: 2
- -> Parallel Append
- -> Parallel Seq Scan on t
- -> Parallel Seq Scan on t t_1
-(11 rows)
->>>>>>> REL_16_9
-- Full sort, not just incremental sort can be pushed below a gather merge path
-- by generate_useful_gather_paths.
explain (costs off) select distinct a,b from t;
-<<<<<<< HEAD
QUERY PLAN
------------------------------------------
Gather Motion 3:1 (slice1; segments: 3)
@@ -1641,18 +1618,8 @@ explain (costs off) select distinct a,b from t;
-> Sort
Sort Key: a, b
-> Seq Scan on t
-=======
- QUERY PLAN
-------------------------------------------------
- Unique
- -> Gather Merge
- Workers Planned: 2
- -> Unique
- -> Sort
- Sort Key: a, b
- -> Parallel Seq Scan on t
->>>>>>> REL_16_9
-(7 rows)
+ Optimizer: Postgres query optimizer
+(8 rows)
drop table t;
-- Sort pushdown can't go below where expressions are part of the rel target.
@@ -1834,20 +1801,6 @@ order by 1, 2;
-> Nested Loop
-> Index Scan using tenk1_unique1 on tenk1
-> Function Scan on generate_series
-(7 rows)
-
-<<<<<<< HEAD
--- Disallow pushing down sort when pathkey is an SRF.
-explain (costs off) select unique1 from tenk1 order by unnest('{1,2}'::int[]);
- QUERY PLAN
--------------------------------------------------------------------------
- Gather Motion 3:1 (slice1; segments: 3)
- Merge Key: (unnest('{1,2}'::anyarray))
- -> Sort
- Sort Key: (unnest('{1,2}'::anyarray))
- -> ProjectSet
- -> Index Only Scan using tenk1_unique1 on tenk1
-(6 rows)
+ Optimizer: Postgres query optimizer
+(8 rows)
-=======
->>>>>>> REL_16_9
diff --git a/src/test/regress/expected/select_distinct.out
b/src/test/regress/expected/select_distinct.out
index 5db0c2ccbb7..660f03f3df5 100644
--- a/src/test/regress/expected/select_distinct.out
+++ b/src/test/regress/expected/select_distinct.out
@@ -134,33 +134,23 @@ SELECT count(*) FROM
--------------------------------------------------------------------------------
Finalize Aggregate
Output: count(*)
-<<<<<<< HEAD
-> Gather Motion 3:1 (slice1; segments: 3)
Output: (PARTIAL count(*))
-> Partial Aggregate
Output: PARTIAL count(*)
-> HashAggregate
Output: tenk1.two, tenk1.four, tenk1.two
- Group Key: tenk1.two, tenk1.four, tenk1.two
+ Group Key: tenk1.two, tenk1.four
-> Redistribute Motion 3:3 (slice2; segments: 3)
Output: tenk1.two, tenk1.four, tenk1.two
Hash Key: tenk1.two, tenk1.four, tenk1.two
-> HashAggregate
Output: tenk1.two, tenk1.four, tenk1.two
- Group Key: tenk1.two, tenk1.four, tenk1.two
+ Group Key: tenk1.two, tenk1.four
-> Seq Scan on public.tenk1
Output: tenk1.two, tenk1.four, tenk1.two
Optimizer: Postgres query optimizer
- Settings: optimizer=off
-(19 rows)
-=======
- -> HashAggregate
- Output: tenk1.two, tenk1.four, tenk1.two
- Group Key: tenk1.two, tenk1.four
- -> Seq Scan on public.tenk1
- Output: tenk1.two, tenk1.four, tenk1.two
-(7 rows)
->>>>>>> REL_16_9
+(18 rows)
SELECT count(*) FROM
(SELECT DISTINCT two, four, two FROM tenk1) ss;
@@ -198,14 +188,18 @@ SET enable_seqscan = 0;
-- Check to see we get an incremental sort plan
EXPLAIN (costs off)
SELECT DISTINCT hundred, two FROM tenk1;
- QUERY PLAN
------------------------------------------------------
- Unique
- -> Incremental Sort
- Sort Key: hundred, two
- Presorted Key: hundred
- -> Index Scan using tenk1_hundred on tenk1
-(5 rows)
+ QUERY PLAN
+------------------------------------------------------------
+ Gather Motion 3:1 (slice1; segments: 3)
+ Merge Key: hundred, two
+ -> Unique
+ Group Key: hundred, two
+ -> Sort
+ Sort Key: hundred, two
+ -> Bitmap Heap Scan on tenk1
+ -> Bitmap Index Scan on tenk1_hundred
+ Optimizer: Postgres query optimizer
+(9 rows)
RESET enable_seqscan;
SET enable_hashagg=TRUE;
@@ -256,17 +250,18 @@ SET max_parallel_workers_per_gather=2;
-- Ensure we get a parallel plan
EXPLAIN (costs off)
SELECT DISTINCT four FROM tenk1;
- QUERY PLAN
-----------------------------------------------------
- Unique
- -> Sort
- Sort Key: four
- -> Gather
- Workers Planned: 2
+ QUERY PLAN
+------------------------------------------------------------
+ Gather Motion 3:1 (slice1; segments: 3)
+ -> HashAggregate
+ Group Key: four
+ -> Redistribute Motion 3:3 (slice2; segments: 3)
+ Hash Key: four
-> HashAggregate
Group Key: four
- -> Parallel Seq Scan on tenk1
-(8 rows)
+ -> Seq Scan on tenk1
+ Optimizer: Postgres query optimizer
+(9 rows)
-- Ensure the parallel plan produces the correct results
SELECT DISTINCT four FROM tenk1;
@@ -286,13 +281,19 @@ $$ LANGUAGE plpgsql PARALLEL UNSAFE;
-- Ensure we don't do parallel distinct with a parallel unsafe function
EXPLAIN (COSTS OFF)
SELECT DISTINCT distinct_func(1) FROM tenk1;
- QUERY PLAN
-----------------------------------------------------------
- Unique
- -> Sort
- Sort Key: (distinct_func(1))
- -> Index Only Scan using tenk1_hundred on tenk1
-(4 rows)
+ QUERY PLAN
+------------------------------------------------------------------
+ Gather Motion 3:1 (slice1; segments: 3)
+ Merge Key: (distinct_func(1))
+ -> Unique
+ Group Key: (distinct_func(1))
+ -> Sort
+ Sort Key: (distinct_func(1))
+ -> Redistribute Motion 3:3 (slice2; segments: 3)
+ Hash Key: (distinct_func(1))
+ -> Seq Scan on tenk1
+ Optimizer: Postgres query optimizer
+(10 rows)
-- make the function parallel safe
CREATE OR REPLACE FUNCTION distinct_func(a INT) RETURNS INT AS $$
@@ -303,16 +304,19 @@ $$ LANGUAGE plpgsql PARALLEL SAFE;
-- Ensure we do parallel distinct now that the function is parallel safe
EXPLAIN (COSTS OFF)
SELECT DISTINCT distinct_func(1) FROM tenk1;
- QUERY PLAN
-----------------------------------------------------
- Unique
- -> Gather Merge
- Workers Planned: 2
- -> Unique
- -> Sort
- Sort Key: (distinct_func(1))
- -> Parallel Seq Scan on tenk1
-(7 rows)
+ QUERY PLAN
+------------------------------------------------------------------
+ Gather Motion 3:1 (slice1; segments: 3)
+ Merge Key: (distinct_func(1))
+ -> Unique
+ Group Key: (distinct_func(1))
+ -> Sort
+ Sort Key: (distinct_func(1))
+ -> Redistribute Motion 3:3 (slice2; segments: 3)
+ Hash Key: (distinct_func(1))
+ -> Seq Scan on tenk1
+ Optimizer: Postgres query optimizer
+(10 rows)
RESET max_parallel_workers_per_gather;
RESET min_parallel_table_scan_size;
@@ -325,12 +329,14 @@ RESET parallel_tuple_cost;
-- Ensure we get a plan with a Limit 1
EXPLAIN (COSTS OFF)
SELECT DISTINCT four FROM tenk1 WHERE four = 0;
- QUERY PLAN
-----------------------------
- Limit
- -> Seq Scan on tenk1
- Filter: (four = 0)
-(3 rows)
+ QUERY PLAN
+------------------------------------------
+ Gather Motion 3:1 (slice1; segments: 3)
+ -> Limit
+ -> Seq Scan on tenk1
+ Filter: (four = 0)
+ Optimizer: Postgres query optimizer
+(5 rows)
-- Ensure the above gives us the correct result
SELECT DISTINCT four FROM tenk1 WHERE four = 0;
@@ -342,12 +348,14 @@ SELECT DISTINCT four FROM tenk1 WHERE four = 0;
-- Ensure we get a plan with a Limit 1
EXPLAIN (COSTS OFF)
SELECT DISTINCT four FROM tenk1 WHERE four = 0 AND two <> 0;
- QUERY PLAN
----------------------------------------------
- Limit
- -> Seq Scan on tenk1
- Filter: ((two <> 0) AND (four = 0))
-(3 rows)
+ QUERY PLAN
+---------------------------------------------------
+ Gather Motion 3:1 (slice1; segments: 3)
+ -> Limit
+ -> Seq Scan on tenk1
+ Filter: ((two <> 0) AND (four = 0))
+ Optimizer: Postgres query optimizer
+(5 rows)
-- Ensure no rows are returned
SELECT DISTINCT four FROM tenk1 WHERE four = 0 AND two <> 0;
@@ -358,12 +366,14 @@ SELECT DISTINCT four FROM tenk1 WHERE four = 0 AND two <>
0;
-- Ensure we get a plan with a Limit 1 when the SELECT list contains constants
EXPLAIN (COSTS OFF)
SELECT DISTINCT four,1,2,3 FROM tenk1 WHERE four = 0;
- QUERY PLAN
-----------------------------
- Limit
- -> Seq Scan on tenk1
- Filter: (four = 0)
-(3 rows)
+ QUERY PLAN
+------------------------------------------
+ Gather Motion 3:1 (slice1; segments: 3)
+ -> Limit
+ -> Seq Scan on tenk1
+ Filter: (four = 0)
+ Optimizer: Postgres query optimizer
+(5 rows)
-- Ensure we only get 1 row
SELECT DISTINCT four,1,2,3 FROM tenk1 WHERE four = 0;
diff --git a/src/test/regress/expected/select_into.out
b/src/test/regress/expected/select_into.out
index 8c5ab55e710..0ad8b7eb236 100644
--- a/src/test/regress/expected/select_into.out
+++ b/src/test/regress/expected/select_into.out
@@ -208,9 +208,8 @@ CREATE VIEW foo AS SELECT 1 INTO int4_tbl;
ERROR: views must not contain SELECT INTO
INSERT INTO int4_tbl SELECT 1 INTO f;
ERROR: SELECT ... INTO is not allowed here
-<<<<<<< HEAD
-LINE 1: INSERT INTO b SELECT 1 INTO f;
- ^
+LINE 1: INSERT INTO int4_tbl SELECT 1 INTO f;
+ ^
--
-- Empty target list
--
@@ -228,10 +227,6 @@ select * from empty_tl;
(11 rows)
drop table empty_tl;
-=======
-LINE 1: INSERT INTO int4_tbl SELECT 1 INTO f;
- ^
->>>>>>> REL_16_9
-- Test CREATE TABLE AS ... IF NOT EXISTS
CREATE TABLE ctas_ine_tbl AS SELECT 1;
CREATE TABLE ctas_ine_tbl AS SELECT 1 / 0; -- error
diff --git a/src/test/regress/expected/union.out
b/src/test/regress/expected/union.out
index f5bc560e698..34febf92964 100644
--- a/src/test/regress/expected/union.out
+++ b/src/test/regress/expected/union.out
@@ -316,8 +316,7 @@ SELECT q1 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q2 FROM
int8_tbl ORDER BY 1;
------------------
123
4567890123456789
- 4567890123456789
-(3 rows)
+(2 rows)
SELECT q1 FROM int8_tbl EXCEPT ALL SELECT q1 FROM int8_tbl FOR NO KEY UPDATE;
ERROR: FOR NO KEY UPDATE is not allowed with UNION/INTERSECT/EXCEPT
@@ -685,10 +684,7 @@ select x from (values (row(1, 2)), (row(1, 3))) _(x) union
select x from (values
QUERY PLAN
-----------------------------------------------
Unique
-<<<<<<< HEAD
Group Key: "*VALUES*".column1
-=======
->>>>>>> REL_16_9
-> Sort
Sort Key: "*VALUES*".column1
-> Append
@@ -752,10 +748,7 @@ select x from (values (row(100::money)),
(row(200::money))) _(x) union select x
QUERY PLAN
-----------------------------------------------
Unique
-<<<<<<< HEAD
Group Key: "*VALUES*".column1
-=======
->>>>>>> REL_16_9
-> Sort
Sort Key: "*VALUES*".column1
-> Append
diff --git a/src/test/regress/serial_schedule b/src/test/regress/serial_schedule
index 27e4971bc83..713785ad73e 100644
--- a/src/test/regress/serial_schedule
+++ b/src/test/regress/serial_schedule
@@ -83,15 +83,15 @@ test: create_am
test: hash_func
test: errors
test: sanity_check
-# test: select_into
-# test: select_distinct
-# test: select_distinct_on
-# #test: select_implicit
-# test: select_having
-# test: subselect
-# test: incremental_sort
-# test: union
-# test: case
+test: select_into
+test: select_distinct
+test: select_distinct_on
+test: select_implicit
+test: select_having
+test: subselect
+test: incremental_sort
+test: union
+test: case
# test: join
# test: aggregates
# test: transactions
diff --git a/src/test/regress/sql/incremental_sort.sql
b/src/test/regress/sql/incremental_sort.sql
index be196f2dfae..632cc260347 100644
--- a/src/test/regress/sql/incremental_sort.sql
+++ b/src/test/regress/sql/incremental_sort.sql
@@ -1,4 +1,3 @@
-<<<<<<< HEAD
-- TODO: incremental sort is turned off by default, because it may have
-- wrong result for some core case. Turn it on to run the existing tests
-- and minimize the difference from upstream.
@@ -10,8 +9,6 @@ set enable_incremental_sort=on;
explain (costs off)
select * from (select * from tenk1 order by four) t order by four, ten;
-=======
->>>>>>> REL_16_9
-- When there is a LIMIT clause, incremental sort is beneficial because
-- it only has to sort some of the groups, and not the entire table.
explain (costs off)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]