Add GROUP BY w/o aggrecation functions analyze + test

This commit is contained in:
Igor Nikonov 2022-12-02 21:21:15 +00:00
parent e59703abc6
commit 5a3abaad2a
3 changed files with 73 additions and 65 deletions

View File

@ -33,9 +33,10 @@ void tryRemoveRedundantOrderBy(QueryPlan::Node * root)
QueryPlan::Node * current_node = frame.node;
IQueryPlanStep * current_step = frame.node->step.get();
if (!steps_affect_order.empty())
{
if (SortingStep * ss = typeid_cast<SortingStep *>(current_step); ss)
/// if there is parent node which can affect order and current step is sorting
/// then check if we can remove the sorting step (and corresponding expression step)
if (!steps_affect_order.empty() && typeid_cast<SortingStep *>(current_step))
{
auto try_to_remove_sorting_step = [&]() -> bool
{
@ -49,7 +50,8 @@ void tryRemoveRedundantOrderBy(QueryPlan::Node * root)
/// (1) aggregation
if (const AggregatingStep * parent_aggr = typeid_cast<AggregatingStep *>(steps_affect_order.back()); parent_aggr)
{
/// check if it contains aggregation functions which depends on order
/// TODO: check if it contains aggregation functions which depends on order
remove_sorting = true;
}
/// (2) sorting
else if (SortingStep * parent_sorting = typeid_cast<SortingStep *>(steps_affect_order.back()); parent_sorting)
@ -73,14 +75,13 @@ void tryRemoveRedundantOrderBy(QueryPlan::Node * root)
};
if (try_to_remove_sorting_step())
{
/// current step was removed from plan, its parent has new children, need to visit them
/// current sorting step has been removed from plan, its parent has new children, need to visit them
for (auto * child : frame.parent_node->children)
stack.push_back({.node = child, .parent_node = frame.parent_node});
continue;
}
}
}
if (typeid_cast<LimitStep *>(current_step)
|| typeid_cast<LimitByStep *>(current_step) /// (1) if there are LIMITs on top of ORDER BY, the ORDER BY is non-removable
@ -111,5 +112,4 @@ void tryRemoveRedundantOrderBy(QueryPlan::Node * root)
}
}
}
}

View File

@ -1,5 +1,5 @@
-- Disable query_plan_remove_redundant_order_by
-- ORDER BY(s) in subqueries are untouched
-- ORDER BY clauses in subqueries are untouched
Expression (Project names)
Header: number UInt64
Sorting (Sorting for ORDER BY)
@ -17,7 +17,7 @@ Header: number UInt64
ReadFromStorage (SystemNumbers)
Header: number UInt64
-- Enable query_plan_remove_redundant_order_by
-- ORDER BY eliminates ORDER BY(s) in subqueries
-- ORDER BY removes ORDER BY clauses in subqueries
Expression (Project names)
Header: number UInt64
Sorting (Sorting for ORDER BY)
@ -26,7 +26,7 @@ Header: number UInt64
Header: number_0 UInt64
ReadFromStorage (SystemNumbers)
Header: number UInt64
-- ORDER BY cannot remove ORDER BY in subquery with ORDER BY WITH FILL
-- ORDER BY cannot remove ORDER BY in subquery WITH FILL
Expression (Project names)
Header: number UInt64
Sorting (Sorting for ORDER BY)
@ -58,3 +58,12 @@ Header: number UInt64
Header: number_1 UInt64
ReadFromStorage (SystemNumbers)
Header: number UInt64
-- GROUP BY removes ORDER BY in _all_ subqueries
Expression ((Project names + Projection))
Header: number UInt64
Aggregating
Header: number_0 UInt64
Expression ((Before GROUP BY + (Change column names to column identifiers + (Project names + (Before ORDER BY + (Projection + (Change column names to column identifiers + (Project names + (Before ORDER BY + (Projection + Change column names to column identifiers))))))))))
Header: number_0 UInt64
ReadFromStorage (SystemNumbers)
Header: number UInt64

View File

@ -4,7 +4,7 @@ SET optimize_duplicate_order_by_and_distinct=0;
SELECT '-- Disable query_plan_remove_redundant_order_by';
SET query_plan_remove_redundant_order_by=0;
SELECT '-- ORDER BY(s) in subqueries are untouched';
SELECT '-- ORDER BY clauses in subqueries are untouched';
EXPLAIN header=1
SELECT *
FROM
@ -23,7 +23,7 @@ ORDER BY number ASC;
SELECT '-- Enable query_plan_remove_redundant_order_by';
SET query_plan_remove_redundant_order_by=1;
SELECT '-- ORDER BY eliminates ORDER BY(s) in subqueries';
SELECT '-- ORDER BY removes ORDER BY clauses in subqueries';
EXPLAIN header=1
SELECT *
FROM
@ -39,7 +39,7 @@ FROM
)
ORDER BY number ASC;
SELECT '-- ORDER BY cannot remove ORDER BY in subquery with ORDER BY WITH FILL';
SELECT '-- ORDER BY cannot remove ORDER BY in subquery WITH FILL';
EXPLAIN header=1
SELECT *
FROM
@ -72,21 +72,21 @@ FROM
)
ORDER BY number ASC;
-- SELECT '-- GROUP BY eliminates ORDER BY in _all_ subqueries';
-- EXPLAIN
-- SELECT *
-- FROM
-- (
-- SELECT *
-- FROM
-- (
-- SELECT *
-- FROM numbers(3)
-- ORDER BY number ASC
-- )
-- ORDER BY number DESC
-- )
-- GROUP BY number;
SELECT '-- GROUP BY removes ORDER BY in _all_ subqueries';
EXPLAIN header=1
SELECT *
FROM
(
SELECT *
FROM
(
SELECT *
FROM numbers(3)
ORDER BY number ASC
)
ORDER BY number DESC
)
GROUP BY number;
-- SELECT '-- GROUP BY with aggregation function which does NOT depend on order -> eliminate ORDER BY(s) in _all_ subqueries';
-- EXPLAIN
@ -224,4 +224,3 @@ ORDER BY number ASC;
-- ORDER BY number DESC
-- ) AS t2
-- ORDER BY t1.number ASC;