Skip to content

Commit

Permalink
Merge pull request #58759 from ClickHouse/cherrypick/23.10/3b1d728683…
Browse files Browse the repository at this point in the history
…98a5771aa32ec13e25437fe83062d8

Cherry pick #58739 to 23.10: Fix stream partitioning in parallel window functions
  • Loading branch information
robot-ch-test-poll3 committed Jan 12, 2024
2 parents 5bb71e1 + 3b1d728 commit 51933f1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/Interpreters/InterpreterSelectQuery.cpp
Expand Up @@ -2927,7 +2927,15 @@ void InterpreterSelectQuery::executeWindow(QueryPlan & query_plan)
// has suitable sorting. Also don't create sort steps when there are no
// columns to sort by, because the sort nodes are confused by this. It
// happens in case of `over ()`.
if (!window.full_sort_description.empty() && (i == 0 || !sortIsPrefix(window, *windows_sorted[i - 1])))
// Even if full_sort_description of both windows match, in case of different
// partitioning we need to add a SortingStep to reshuffle data in the streams.
bool need_sort = !window.full_sort_description.empty();
if (need_sort && i != 0)
{
need_sort = !sortIsPrefix(window, *windows_sorted[i - 1])
|| (settings.max_threads != 1 && window.partition_by.size() != windows_sorted[i - 1]->partition_by.size());
}
if (need_sort)
{
SortingStep::Settings sort_settings(*context);

Expand Down
12 changes: 10 additions & 2 deletions src/Planner/Planner.cpp
Expand Up @@ -889,9 +889,17 @@ void addWindowSteps(QueryPlan & query_plan,
* has suitable sorting. Also don't create sort steps when there are no
* columns to sort by, because the sort nodes are confused by this. It
* happens in case of `over ()`.
* Even if full_sort_description of both windows match, in case of different
* partitioning we need to add a SortingStep to reshuffle data in the streams.
*/
if (!window_description.full_sort_description.empty() &&
(i == 0 || !sortDescriptionIsPrefix(window_description.full_sort_description, window_descriptions[i - 1].full_sort_description)))

bool need_sort = !window_description.full_sort_description.empty();
if (need_sort && i != 0)
{
need_sort = !sortDescriptionIsPrefix(window_description.full_sort_description, window_descriptions[i - 1].full_sort_description)
|| (settings.max_threads != 1 && window_description.partition_by.size() != window_descriptions[i - 1].partition_by.size());
}
if (need_sort)
{
SortingStep::Settings sort_settings(*query_context);

Expand Down
@@ -0,0 +1,18 @@
sales 15000
sales 15000
sales 15000
sales 29400
sales 29400
sales 29400
sales 43800
sales 43800
sales 43800
sales 15000 5000
sales 15000 5000
sales 15000 5000
sales 29400 4800
sales 29400 4800
sales 29400 4800
sales 43800 4800
sales 43800 4800
sales 43800 4800
@@ -0,0 +1,32 @@
CREATE TABLE empsalary
(
`depname` LowCardinality(String),
`empno` UInt64,
`salary` Int32,
`enroll_date` Date
)
ENGINE = Memory;

insert into empsalary values ('sales',3,4800,'2007-08-01'), ('sales',1,5000,'2006-10-01'), ('sales',4,4800,'2007-08-08');


insert into empsalary values ('sales',3,4800,'2007-08-01'), ('sales',1,5000,'2006-10-01'), ('sales',4,4800,'2007-08-08');

insert into empsalary values ('sales',3,4800,'2007-08-01'), ('sales',1,5000,'2006-10-01'), ('sales',4,4800,'2007-08-08');

-- 1 window function

SELECT depname,
sum(salary) OVER (PARTITION BY depname order by empno) AS depsalary
FROM empsalary
order by depsalary;


-- 2 window functions with different window,
-- but result should be the same for depsalary

SELECT depname,
sum(salary) OVER (PARTITION BY depname order by empno) AS depsalary,
min(salary) OVER (PARTITION BY depname, empno order by enroll_date) AS depminsalary
FROM empsalary
order by depsalary;

0 comments on commit 51933f1

Please sign in to comment.