Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Interpreters/ExpressionAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ void ExpressionAnalyzer::makeWindowDescriptionFromAST(const Context & context_,
with_alias->getColumnName(), 1 /* direction */,
1 /* nulls_direction */));

auto actions_dag = std::make_shared<ActionsDAG>(columns_after_join);
auto actions_dag = std::make_shared<ActionsDAG>(aggregated_columns);
getRootActions(column_ast, false, actions_dag);
desc.partition_by_actions.push_back(std::move(actions_dag));
}
Expand All @@ -698,7 +698,7 @@ void ExpressionAnalyzer::makeWindowDescriptionFromAST(const Context & context_,
order_by_element.direction,
order_by_element.nulls_direction));

auto actions_dag = std::make_shared<ActionsDAG>(columns_after_join);
auto actions_dag = std::make_shared<ActionsDAG>(aggregated_columns);
getRootActions(column_ast, false, actions_dag);
desc.order_by_actions.push_back(std::move(actions_dag));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ size_t tryReuseStorageOrderingForWindowFunctions(QueryPlan::Node * parent_node,
{
/// Find the following sequence of steps, add InputOrderInfo and apply prefix sort description to
/// SortingStep:
/// WindowStep <- SortingStep <- [Expression] <- [SettingQuotaAndLimits] <- ReadFromMergeTree
/// WindowStep <- SortingStep <- [Expression] <- ReadFromMergeTree

auto * window_node = parent_node;
auto * window = typeid_cast<WindowStep *>(window_node->step.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ No sorting plan
optimize_read_in_window_order=1
Prefix sort description: n ASC, x ASC
Result sort description: n ASC, x ASC
Complex ORDER BY
optimize_read_in_window_order=0
3 3 1
4 5 2
5 7 3
optimize_read_in_window_order=1
3 3 1
4 5 2
5 7 3
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ $CLICKHOUSE_CLIENT -q "explain plan actions=1, description=1 select n, sum(x) OV
echo ' optimize_read_in_window_order=1'
$CLICKHOUSE_CLIENT -q "explain plan actions=1, description=1 select n, sum(x) OVER (ORDER BY n, x ROWS BETWEEN 100 PRECEDING AND CURRENT ROW) from ${name}_n_x SETTINGS optimize_read_in_window_order=1" | grep -i "sort description"

echo 'Complex ORDER BY'
$CLICKHOUSE_CLIENT -q "CREATE TABLE ${name}_complex (unique1 Int32, unique2 Int32, ten Int32) ENGINE=MergeTree ORDER BY tuple() SETTINGS index_granularity = 8192"
$CLICKHOUSE_CLIENT -q "INSERT INTO ${name}_complex VALUES (1, 2, 3), (2, 3, 4), (3, 4, 5)"
echo ' optimize_read_in_window_order=0'
$CLICKHOUSE_CLIENT -q "SELECT ten, sum(unique1) + sum(unique2) AS res, rank() OVER (ORDER BY sum(unique1) + sum(unique2) ASC) AS rank FROM ${name}_complex GROUP BY ten ORDER BY ten ASC SETTINGS optimize_read_in_window_order=0"
echo ' optimize_read_in_window_order=1'
$CLICKHOUSE_CLIENT -q "SELECT ten, sum(unique1) + sum(unique2) AS res, rank() OVER (ORDER BY sum(unique1) + sum(unique2) ASC) AS rank FROM ${name}_complex GROUP BY ten ORDER BY ten ASC SETTINGS optimize_read_in_window_order=1"

$CLICKHOUSE_CLIENT -q "drop table ${name}"
$CLICKHOUSE_CLIENT -q "drop table ${name}_n"
$CLICKHOUSE_CLIENT -q "drop table ${name}_n_x"
$CLICKHOUSE_CLIENT -q "drop table ${name}_complex"