Skip to content

Commit

Permalink
Merge pull request ClickHouse#32670 from CurtizJ/fix-reading-in-order
Browse files Browse the repository at this point in the history
Fix reading in order of sorting key from `Distributed` and `Merge` tables
  • Loading branch information
kssenii authored Dec 13, 2021
2 parents 6879f03 + 745fd4f commit dadaeab
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Interpreters/TreeOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,19 @@ void optimizeDuplicateDistinct(ASTSelectQuery & select)
/// has a single argument and not an aggregate functions.
void optimizeMonotonousFunctionsInOrderBy(ASTSelectQuery * select_query, ContextPtr context,
const TablesWithColumns & tables_with_columns,
const Names & sorting_key_columns)
const TreeRewriterResult & result)
{
auto order_by = select_query->orderBy();
if (!order_by)
return;

/// Do not apply optimization for Distributed and Merge storages,
/// because we can't get the sorting key of their undelying tables
/// and we can break the matching of the sorting key for `read_in_order`
/// optimization by removing monotonous functions from the prefix of key.
if (result.is_remote_storage || (result.storage && result.storage->getName() == "Merge"))
return;

for (const auto & child : order_by->children)
{
auto * order_by_element = child->as<ASTOrderByElement>();
Expand All @@ -438,6 +445,8 @@ void optimizeMonotonousFunctionsInOrderBy(ASTSelectQuery * select_query, Context
}
}

auto sorting_key_columns = result.metadata_snapshot ? result.metadata_snapshot->getSortingKeyColumns() : Names{};

bool is_sorting_key_prefix = true;
for (size_t i = 0; i < order_by->children.size(); ++i)
{
Expand Down Expand Up @@ -802,8 +811,7 @@ void TreeOptimizer::apply(ASTPtr & query, TreeRewriterResult & result,

/// Replace monotonous functions with its argument
if (settings.optimize_monotonous_functions_in_order_by)
optimizeMonotonousFunctionsInOrderBy(select_query, context, tables_with_columns,
result.metadata_snapshot ? result.metadata_snapshot->getSortingKeyColumns() : Names{});
optimizeMonotonousFunctionsInOrderBy(select_query, context, tables_with_columns, result);

/// Remove duplicate items from ORDER BY.
/// Execute it after all order by optimizations,
Expand Down
21 changes: 21 additions & 0 deletions tests/queries/0_stateless/02147_order_by_optimizations.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
SELECT
date,
v
FROM t_02147
ORDER BY
toStartOfHour(date) ASC,
v ASC
SELECT
date,
v
FROM t_02147_dist
ORDER BY
toStartOfHour(date) ASC,
v ASC
SELECT
date,
v
FROM t_02147_merge
ORDER BY
toStartOfHour(date) ASC,
v ASC
15 changes: 15 additions & 0 deletions tests/queries/0_stateless/02147_order_by_optimizations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
DROP TABLE IF EXISTS t_02147;
DROP TABLE IF EXISTS t_02147_dist;
DROP TABLE IF EXISTS t_02147_merge;

CREATE TABLE t_02147 (date DateTime, v UInt32)
ENGINE = MergeTree ORDER BY toStartOfHour(date);

CREATE TABLE t_02147_dist AS t_02147 ENGINE = Distributed(test_shard_localhost, currentDatabase(), t_02147);
CREATE TABLE t_02147_merge AS t_02147 ENGINE = Merge(currentDatabase(), 't_02147');

SET optimize_monotonous_functions_in_order_by = 1;

EXPLAIN SYNTAX SELECT * FROM t_02147 ORDER BY toStartOfHour(date), v;
EXPLAIN SYNTAX SELECT * FROM t_02147_dist ORDER BY toStartOfHour(date), v;
EXPLAIN SYNTAX SELECT * FROM t_02147_merge ORDER BY toStartOfHour(date), v;

0 comments on commit dadaeab

Please sign in to comment.