Skip to content

Commit 519c515

Browse files
committed
Correct parallel window function in CASE WHEN.
Previously, we attempted to disable window functions inside CASE WHEN expressions due to concerns about unstable parallel results. However, this was a misunderstanding. All expressions from the subquery are Var columns, not the original expressions. This issue was uncovered when we fixed the subquery row count estimation, causing the cost to change in the upper plan. EXPLAIN(COSTS OFF) SELECT empno, depname, salary, bonus, depadj, MIN(bonus) OVER (ORDER BY empno), MAX(depadj) OVER () FROM( SELECT *, CASE WHEN enroll_date < '2008-01-01' THEN 2008 - extract(YEAR FROM enroll_date) END * 500 AS bonus, CASE WHEN AVG(salary) OVER (PARTITION BY depname) < salary THEN 200 END AS depadj FROM empsalary )s; QUERY PLAN -------------------------------------------------------------------------- WindowAgg -> WindowAgg Order By: s.empno -> Gather Motion 6:1 (slice1; segments: 6) Merge Key: s.empno -> Sort Sort Key: s.empno -> Subquery Scan on s -> WindowAgg Partition By: empsalary.depname -> Sort Sort Key: empsalary.depname -> Redistribute Motion 6:6 (slice2; segments: 6) Hash Key: empsalary.depname Hash Module: 3 -> Parallel Seq Scan on empsalary Optimizer: Postgres query optimizer (17 rows) Authored-by: Zhang Mingli avamingli@gmail.com
1 parent 44195cc commit 519c515

File tree

4 files changed

+12
-70
lines changed

4 files changed

+12
-70
lines changed

src/backend/optimizer/plan/planner.c

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,6 @@ static split_rollup_data *make_new_rollups_for_hash_grouping_set(PlannerInfo *ro
300300
Path *path,
301301
grouping_sets_data *gd);
302302

303-
static bool
304-
contain_case_expr(Node *clause);
305-
306-
static bool
307-
contain_case_expr_walker(Node *node, void *context);
308-
309303
static void create_partial_window_path(PlannerInfo *root,
310304
RelOptInfo *window_rel,
311305
Path *path,
@@ -9139,25 +9133,6 @@ make_new_rollups_for_hash_grouping_set(PlannerInfo *root,
91399133
return srd;
91409134
}
91419135

9142-
static bool
9143-
contain_case_expr(Node *clause)
9144-
{
9145-
return contain_case_expr_walker(clause, NULL);
9146-
}
9147-
9148-
static bool
9149-
contain_case_expr_walker(Node *node, void *context)
9150-
{
9151-
if (node == NULL)
9152-
return false;
9153-
9154-
if (IsA(node, CaseExpr))
9155-
return true;
9156-
9157-
return expression_tree_walker(node, contain_case_expr_walker,
9158-
context);
9159-
}
9160-
91619136
/*
91629137
* Parallel processing of window functions.
91639138
*
@@ -9176,47 +9151,9 @@ create_partial_window_path(PlannerInfo *root,
91769151
{
91779152
PathTarget *window_target;
91789153
ListCell *l;
9179-
Bitmapset *sgrefs;
91809154

91819155
window_target = input_target;
91829156

9183-
sgrefs = NULL;
9184-
9185-
foreach(l, activeWindows)
9186-
{
9187-
WindowClause *wc = lfirst_node(WindowClause, l);
9188-
ListCell *lc2;
9189-
9190-
foreach(lc2, wc->partitionClause)
9191-
{
9192-
SortGroupClause *sortcl = lfirst_node(SortGroupClause, lc2);
9193-
9194-
sgrefs = bms_add_member(sgrefs, sortcl->tleSortGroupRef);
9195-
}
9196-
foreach(lc2, wc->orderClause)
9197-
{
9198-
SortGroupClause *sortcl = lfirst_node(SortGroupClause, lc2);
9199-
9200-
sgrefs = bms_add_member(sgrefs, sortcl->tleSortGroupRef);
9201-
}
9202-
}
9203-
9204-
int x = -1;
9205-
while ((x = bms_next_member(sgrefs, x)) >= 0)
9206-
{
9207-
Index sgref = get_pathtarget_sortgroupref(input_target, x);
9208-
if (sgref != 0)
9209-
{
9210-
ListCell *lc;
9211-
foreach(lc, input_target->exprs)
9212-
{
9213-
Expr *expr = (Expr *) lfirst(lc);
9214-
if (contain_case_expr((Node*)expr))
9215-
return;
9216-
}
9217-
}
9218-
}
9219-
92209157
foreach(l, activeWindows)
92219158
{
92229159
WindowClause *wc = lfirst_node(WindowClause, l);

src/test/regress/expected/window_parallel.out

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ SELECT sum(salary) OVER w, rank() OVER w FROM empsalary WINDOW w AS (PARTITION B
498498
(10 rows)
499499

500500
-- w8
501-
-- strict aggs
501+
-- window agg in CASE WHEN clause
502502
set enable_parallel = off;
503503
EXPLAIN(COSTS OFF)
504504
SELECT empno, depname, salary, bonus, depadj, MIN(bonus) OVER (ORDER BY empno), MAX(depadj) OVER () FROM(
@@ -556,12 +556,12 @@ SELECT empno, depname, salary, bonus, depadj, MIN(bonus) OVER (ORDER BY empno),
556556
AVG(salary) OVER (PARTITION BY depname) < salary
557557
THEN 200 END AS depadj FROM empsalary
558558
)s;
559-
QUERY PLAN
560-
-------------------------------------------------------------------
559+
QUERY PLAN
560+
------------------------------------------------------------------------------------------
561561
WindowAgg
562562
-> WindowAgg
563563
Order By: s.empno
564-
-> Gather Motion 3:1 (slice1; segments: 3)
564+
-> Gather Motion 6:1 (slice1; segments: 6)
565565
Merge Key: s.empno
566566
-> Sort
567567
Sort Key: s.empno
@@ -570,9 +570,12 @@ SELECT empno, depname, salary, bonus, depadj, MIN(bonus) OVER (ORDER BY empno),
570570
Partition By: empsalary.depname
571571
-> Sort
572572
Sort Key: empsalary.depname
573-
-> Seq Scan on empsalary
573+
-> Redistribute Motion 6:6 (slice2; segments: 6)
574+
Hash Key: empsalary.depname
575+
Hash Module: 3
576+
-> Parallel Seq Scan on empsalary
574577
Optimizer: Postgres query optimizer
575-
(14 rows)
578+
(17 rows)
576579

577580
SELECT empno, depname, salary, bonus, depadj, MIN(bonus) OVER (ORDER BY empno), MAX(depadj) OVER () FROM(
578581
SELECT *,

src/test/regress/sql/misc_jiras.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ set statement_mem to '1024kB';
2121

2222
set extra_float_digits=0; -- the last decimal digits are somewhat random
2323

24+
set enable_parallel = off;
2425
-- Inject fault at 'winagg_after_spool_tuples' to show that the tuplestore spills
2526
-- to disk.
2627
SELECT gp_inject_fault('winagg_after_spool_tuples', 'skip', dbid)
@@ -42,6 +43,7 @@ SELECT gp_inject_fault('winagg_after_spool_tuples', 'reset', dbid)
4243
FROM gp_segment_configuration WHERE role='p' AND content>=0;
4344

4445
reset statement_mem;
46+
reset enable_parallel;
4547

4648
-- non-ASCII multibyte character should show up correctly in error messages.
4749
select '' || (B'1');

src/test/regress/sql/window_parallel.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ SELECT sum(salary) OVER w, rank() OVER w FROM empsalary WINDOW w AS (PARTITION B
120120
SELECT sum(salary) OVER w, rank() OVER w FROM empsalary WINDOW w AS (PARTITION BY depname ORDER BY salary DESC);
121121

122122
-- w8
123-
-- strict aggs
123+
-- window agg in CASE WHEN clause
124124
set enable_parallel = off;
125125
EXPLAIN(COSTS OFF)
126126
SELECT empno, depname, salary, bonus, depadj, MIN(bonus) OVER (ORDER BY empno), MAX(depadj) OVER () FROM(

0 commit comments

Comments
 (0)