Merge pull request #64940 from ClickHouse/backport/24.3/64766

Backport #64766 to 24.3: Fix OrderByLimitByDuplicateEliminationVisitor across subqueries
This commit is contained in:
robot-ch-test-poll1 2024-06-07 00:49:04 +04:00 committed by GitHub
commit fe7690ec74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 57 additions and 5 deletions

View File

@ -22,6 +22,7 @@ public:
if (query_node->hasOrderBy())
{
QueryTreeNodeConstRawPtrWithHashSet unique_expressions_nodes_set;
QueryTreeNodes result_nodes;
auto & query_order_by_nodes = query_node->getOrderBy().getNodes();
@ -45,10 +46,9 @@ public:
query_order_by_nodes = std::move(result_nodes);
}
unique_expressions_nodes_set.clear();
if (query_node->hasLimitBy())
{
QueryTreeNodeConstRawPtrWithHashSet unique_expressions_nodes_set;
QueryTreeNodes result_nodes;
auto & query_limit_by_nodes = query_node->getLimitBy().getNodes();
@ -63,9 +63,6 @@ public:
query_limit_by_nodes = std::move(result_nodes);
}
}
private:
QueryTreeNodeConstRawPtrWithHashSet unique_expressions_nodes_set;
};
}

View File

@ -0,0 +1,39 @@
QUERY id: 0
PROJECTION COLUMNS
id UInt64
PROJECTION
LIST id: 1, nodes: 1
COLUMN id: 2, column_name: id, result_type: UInt64, source_id: 3
JOIN TREE
TABLE id: 3, alias: __table1, table_name: default.test, final: 1
WHERE
FUNCTION id: 4, function_name: in, function_type: ordinary, result_type: UInt8
ARGUMENTS
LIST id: 5, nodes: 2
COLUMN id: 2, column_name: id, result_type: UInt64, source_id: 3
QUERY id: 6, is_subquery: 1, is_distinct: 1
PROJECTION COLUMNS
id UInt64
PROJECTION
LIST id: 7, nodes: 1
COLUMN id: 8, column_name: id, result_type: UInt64, source_id: 9
JOIN TREE
TABLE id: 9, alias: __table1, table_name: default.test, final: 1
ORDER BY
LIST id: 10, nodes: 1
SORT id: 11, sort_direction: ASCENDING, with_fill: 0
EXPRESSION
COLUMN id: 8, column_name: id, result_type: UInt64, source_id: 9
LIMIT
CONSTANT id: 12, constant_value: UInt64_4, constant_value_type: UInt64
ORDER BY
LIST id: 13, nodes: 1
SORT id: 14, sort_direction: ASCENDING, with_fill: 0
EXPRESSION
COLUMN id: 2, column_name: id, result_type: UInt64, source_id: 3
LIMIT BY LIMIT
CONSTANT id: 15, constant_value: UInt64_1, constant_value_type: UInt64
LIMIT BY
LIST id: 16, nodes: 1
COLUMN id: 2, column_name: id, result_type: UInt64, source_id: 3
SETTINGS allow_experimental_analyzer=1

View File

@ -0,0 +1,16 @@
CREATE TABLE test
ENGINE = ReplacingMergeTree
PRIMARY KEY id
AS SELECT number AS id FROM numbers(100);
EXPLAIN QUERY TREE SELECT id
FROM test FINAL
WHERE id IN (
SELECT DISTINCT id
FROM test FINAL
ORDER BY id ASC
LIMIT 4
)
ORDER BY id ASC
LIMIT 1 BY id
SETTINGS allow_experimental_analyzer = 1;