From 125bb145d8353277ad3c0369a49354ca12515be3 Mon Sep 17 00:00:00 2001 From: Igor Markelov Date: Mon, 27 May 2024 19:28:14 +0000 Subject: [PATCH] Fix cardinalities usage --- src/Storages/MergeTree/RowOrderOptimizer.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Storages/MergeTree/RowOrderOptimizer.cpp b/src/Storages/MergeTree/RowOrderOptimizer.cpp index 5e3d0df4c52..34f9fed4500 100644 --- a/src/Storages/MergeTree/RowOrderOptimizer.cpp +++ b/src/Storages/MergeTree/RowOrderOptimizer.cpp @@ -110,7 +110,8 @@ std::vector getCardinalitiesInPermutedRange( std::vector cardinalities(other_column_indexes.size()); for (size_t i = 0; i < other_column_indexes.size(); ++i) { - const ColumnPtr & column = block.getByPosition(i).column; + const size_t column_id = other_column_indexes[i]; + const ColumnPtr & column = block.getByPosition(column_id).column; cardinalities[i] = column->estimateCardinalityInPermutedRange(permutation, equal_range); } return cardinalities; @@ -123,19 +124,27 @@ void updatePermutationInEqualRange( const EqualRange & equal_range, const std::vector & cardinalities) { + LoggerPtr log = getLogger("RowOrderOptimizer"); + + LOG_TRACE(log, "Starting optimization in equal range"); + std::vector column_order(other_column_indexes.size()); iota(column_order.begin(), column_order.end(), 0); auto cmp = [&](size_t lhs, size_t rhs) -> bool { return cardinalities[lhs] < cardinalities[rhs]; }; - ::sort(column_order.begin(), column_order.end(), cmp); + stable_sort(column_order.begin(), column_order.end(), cmp); std::vector ranges = {equal_range}; + LOG_TRACE(log, "equal_range: .from: {}, .to: {}", equal_range.from, equal_range.to); for (size_t i : column_order) { const size_t column_id = other_column_indexes[i]; const ColumnPtr & column = block.getByPosition(column_id).column; + LOG_TRACE(log, "i: {}, column_id: {}, column->getName(): {}, cardinality: {}", i, column_id, column->getName(), cardinalities[i]); column->updatePermutation( - IColumn::PermutationSortDirection::Ascending, IColumn::PermutationSortStability::Unstable, 0, 1, permutation, ranges); + IColumn::PermutationSortDirection::Ascending, IColumn::PermutationSortStability::Stable, 0, 1, permutation, ranges); } + + LOG_TRACE(log, "Finish optimization in equal range"); } }