mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-09-20 00:30:49 +00:00
Merge pull request #68099 from GrahamCampbell/patch-2
Do not apply redundant sorting removal when there's an offset
This commit is contained in:
commit
c1c8e6dd8d
@ -8,6 +8,7 @@
|
|||||||
#include <Processors/QueryPlan/JoinStep.h>
|
#include <Processors/QueryPlan/JoinStep.h>
|
||||||
#include <Processors/QueryPlan/LimitByStep.h>
|
#include <Processors/QueryPlan/LimitByStep.h>
|
||||||
#include <Processors/QueryPlan/LimitStep.h>
|
#include <Processors/QueryPlan/LimitStep.h>
|
||||||
|
#include <Processors/QueryPlan/OffsetStep.h>
|
||||||
#include <Processors/QueryPlan/Optimizations/Optimizations.h>
|
#include <Processors/QueryPlan/Optimizations/Optimizations.h>
|
||||||
#include <Processors/QueryPlan/QueryPlanVisitor.h>
|
#include <Processors/QueryPlan/QueryPlanVisitor.h>
|
||||||
#include <Processors/QueryPlan/ReadFromMergeTree.h>
|
#include <Processors/QueryPlan/ReadFromMergeTree.h>
|
||||||
@ -59,9 +60,10 @@ public:
|
|||||||
|
|
||||||
if (typeid_cast<LimitStep *>(current_step)
|
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
|
|| typeid_cast<LimitByStep *>(current_step) /// (1) if there are LIMITs on top of ORDER BY, the ORDER BY is non-removable
|
||||||
|| typeid_cast<FillingStep *>(current_step) /// (2) if ORDER BY is with FILL WITH, it is non-removable
|
|| typeid_cast<OffsetStep *>(current_step) /// (2) OFFSET on top of ORDER BY, the ORDER BY is non-removable
|
||||||
|| typeid_cast<SortingStep *>(current_step) /// (3) ORDER BY will change order of previous sorting
|
|| typeid_cast<FillingStep *>(current_step) /// (3) if ORDER BY is with FILL WITH, it is non-removable
|
||||||
|| typeid_cast<AggregatingStep *>(current_step)) /// (4) aggregation change order
|
|| typeid_cast<SortingStep *>(current_step) /// (4) ORDER BY will change order of previous sorting
|
||||||
|
|| typeid_cast<AggregatingStep *>(current_step)) /// (5) aggregation change order
|
||||||
{
|
{
|
||||||
logStep("nodes_affect_order/push", current_node);
|
logStep("nodes_affect_order/push", current_node);
|
||||||
nodes_affect_order.push_back(current_node);
|
nodes_affect_order.push_back(current_node);
|
||||||
|
@ -465,6 +465,37 @@ Expression ((Projection + Before ORDER BY))
|
|||||||
ReadFromStorage (SystemOne)
|
ReadFromStorage (SystemOne)
|
||||||
-- execute
|
-- execute
|
||||||
Float64 9007199254740994
|
Float64 9007199254740994
|
||||||
|
-- presence of an inner OFFSET retains the ORDER BY
|
||||||
|
-- query
|
||||||
|
WITH
|
||||||
|
t1 AS (
|
||||||
|
SELECT a, b
|
||||||
|
FROM
|
||||||
|
VALUES (
|
||||||
|
'b UInt32, a Int32',
|
||||||
|
(1, 1),
|
||||||
|
(2, 0)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
SUM(a)
|
||||||
|
FROM (
|
||||||
|
SELECT a, b
|
||||||
|
FROM t1
|
||||||
|
ORDER BY 1 DESC, 2
|
||||||
|
OFFSET 1
|
||||||
|
) t2
|
||||||
|
-- explain
|
||||||
|
Expression ((Projection + Before ORDER BY))
|
||||||
|
Aggregating
|
||||||
|
Expression (Before GROUP BY)
|
||||||
|
Offset
|
||||||
|
Expression (Projection)
|
||||||
|
Sorting (Sorting for ORDER BY)
|
||||||
|
Expression ((Before ORDER BY + (Projection + Before ORDER BY)))
|
||||||
|
ReadFromStorage (Values)
|
||||||
|
-- execute
|
||||||
|
0
|
||||||
-- disable common optimization to avoid functions to be lifted up (liftUpFunctions optimization), needed for testing with stateful function
|
-- disable common optimization to avoid functions to be lifted up (liftUpFunctions optimization), needed for testing with stateful function
|
||||||
-- neighbor() as stateful function prevents removing inner ORDER BY since its result depends on order
|
-- neighbor() as stateful function prevents removing inner ORDER BY since its result depends on order
|
||||||
-- query
|
-- query
|
||||||
|
@ -302,6 +302,27 @@ FROM
|
|||||||
)"
|
)"
|
||||||
run_query "$query"
|
run_query "$query"
|
||||||
|
|
||||||
|
echo "-- presence of an inner OFFSET retains the ORDER BY"
|
||||||
|
query="WITH
|
||||||
|
t1 AS (
|
||||||
|
SELECT a, b
|
||||||
|
FROM
|
||||||
|
VALUES (
|
||||||
|
'b UInt32, a Int32',
|
||||||
|
(1, 1),
|
||||||
|
(2, 0)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
SUM(a)
|
||||||
|
FROM (
|
||||||
|
SELECT a, b
|
||||||
|
FROM t1
|
||||||
|
ORDER BY 1 DESC, 2
|
||||||
|
OFFSET 1
|
||||||
|
) t2"
|
||||||
|
run_query "$query"
|
||||||
|
|
||||||
echo "-- disable common optimization to avoid functions to be lifted up (liftUpFunctions optimization), needed for testing with stateful function"
|
echo "-- disable common optimization to avoid functions to be lifted up (liftUpFunctions optimization), needed for testing with stateful function"
|
||||||
ENABLE_OPTIMIZATION="SET query_plan_enable_optimizations=0;$ENABLE_OPTIMIZATION"
|
ENABLE_OPTIMIZATION="SET query_plan_enable_optimizations=0;$ENABLE_OPTIMIZATION"
|
||||||
echo "-- neighbor() as stateful function prevents removing inner ORDER BY since its result depends on order"
|
echo "-- neighbor() as stateful function prevents removing inner ORDER BY since its result depends on order"
|
||||||
|
@ -464,6 +464,36 @@ Expression ((Project names + Projection))
|
|||||||
ReadFromStorage (SystemOne)
|
ReadFromStorage (SystemOne)
|
||||||
-- execute
|
-- execute
|
||||||
Float64 9007199254740994
|
Float64 9007199254740994
|
||||||
|
-- presence of an inner OFFSET retains the ORDER BY
|
||||||
|
-- query
|
||||||
|
WITH
|
||||||
|
t1 AS (
|
||||||
|
SELECT a, b
|
||||||
|
FROM
|
||||||
|
VALUES (
|
||||||
|
'b UInt32, a Int32',
|
||||||
|
(1, 1),
|
||||||
|
(2, 0)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
SUM(a)
|
||||||
|
FROM (
|
||||||
|
SELECT a, b
|
||||||
|
FROM t1
|
||||||
|
ORDER BY 1 DESC, 2
|
||||||
|
OFFSET 1
|
||||||
|
) t2
|
||||||
|
-- explain
|
||||||
|
Expression ((Project names + Projection))
|
||||||
|
Aggregating
|
||||||
|
Expression ((Before GROUP BY + (Change column names to column identifiers + Project names)))
|
||||||
|
Offset
|
||||||
|
Sorting (Sorting for ORDER BY)
|
||||||
|
Expression ((Before ORDER BY + (Projection + (Change column names to column identifiers + (Project names + (Projection + Change column names to column identifiers))))))
|
||||||
|
ReadFromStorage (Values)
|
||||||
|
-- execute
|
||||||
|
0
|
||||||
-- disable common optimization to avoid functions to be lifted up (liftUpFunctions optimization), needed for testing with stateful function
|
-- disable common optimization to avoid functions to be lifted up (liftUpFunctions optimization), needed for testing with stateful function
|
||||||
-- neighbor() as stateful function prevents removing inner ORDER BY since its result depends on order
|
-- neighbor() as stateful function prevents removing inner ORDER BY since its result depends on order
|
||||||
-- query
|
-- query
|
||||||
|
Loading…
Reference in New Issue
Block a user