From ad6447398f254152d1d77495a553931c44dab903 Mon Sep 17 00:00:00 2001 From: alesapin Date: Mon, 1 Jun 2020 22:58:11 +0300 Subject: [PATCH] Fix indices calculation during merge --- src/Storages/IndicesDescription.cpp | 11 +++++++++++ src/Storages/IndicesDescription.h | 3 +++ .../MergeTree/MergeTreeDataMergerMutator.cpp | 6 ++++-- ..._optimize_data_skip_idx_order_by_expr.reference | 1 + .../01292_optimize_data_skip_idx_order_by_expr.sql | 14 ++++++++++++++ 5 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/queries/0_stateless/01292_optimize_data_skip_idx_order_by_expr.reference create mode 100644 tests/queries/0_stateless/01292_optimize_data_skip_idx_order_by_expr.sql diff --git a/src/Storages/IndicesDescription.cpp b/src/Storages/IndicesDescription.cpp index 357a407168d..f3afe8c03a2 100644 --- a/src/Storages/IndicesDescription.cpp +++ b/src/Storages/IndicesDescription.cpp @@ -108,4 +108,15 @@ IndicesDescription IndicesDescription::parse(const String & str, const ColumnsDe return result; } +ExpressionActionsPtr IndicesDescription::getSingleExpressionForIndices(const ColumnsDescription & columns, const Context & context) const +{ + ASTPtr combined_expr_list = std::make_shared(); + for (const auto & index : *this) + for (const auto & index_expr : index.expression_list_ast->children) + combined_expr_list->children.push_back(index_expr->clone()); + + auto syntax_result = SyntaxAnalyzer(context).analyze(combined_expr_list, columns.getAllPhysical()); + return ExpressionAnalyzer(combined_expr_list, syntax_result, context).getActions(false); +} + } diff --git a/src/Storages/IndicesDescription.h b/src/Storages/IndicesDescription.h index 07613aa8252..16932dda6a2 100644 --- a/src/Storages/IndicesDescription.h +++ b/src/Storages/IndicesDescription.h @@ -59,6 +59,9 @@ struct IndicesDescription : public std::vector String toString() const; /// Parse description from string static IndicesDescription parse(const String & str, const ColumnsDescription & columns, const Context & context); + + /// Return common expression for all stored indices + ExpressionActionsPtr getSingleExpressionForIndices(const ColumnsDescription & columns, const Context & context) const; }; } diff --git a/src/Storages/MergeTree/MergeTreeDataMergerMutator.cpp b/src/Storages/MergeTree/MergeTreeDataMergerMutator.cpp index 369e90705a0..afa4d716d66 100644 --- a/src/Storages/MergeTree/MergeTreeDataMergerMutator.cpp +++ b/src/Storages/MergeTree/MergeTreeDataMergerMutator.cpp @@ -792,13 +792,15 @@ MergeTreeData::MutableDataPartPtr MergeTreeDataMergerMutator::mergePartsToTempor if (need_remove_expired_values) merged_stream = std::make_shared(merged_stream, data, new_data_part, time_of_merge, force_ttl); - const auto & index_factory = MergeTreeIndexFactory::instance(); + if (data.hasSecondaryIndices()) { - merged_stream = std::make_shared(merged_stream, data.getPrimaryKeyAndSkipIndicesExpression()); + const auto & indices = data.getSecondaryIndices(); + merged_stream = std::make_shared(merged_stream, indices.getSingleExpressionForIndices(data.getColumns(), data.global_context)); merged_stream = std::make_shared(merged_stream); } + const auto & index_factory = MergeTreeIndexFactory::instance(); MergedBlockOutputStream to{ new_data_part, merging_columns, diff --git a/tests/queries/0_stateless/01292_optimize_data_skip_idx_order_by_expr.reference b/tests/queries/0_stateless/01292_optimize_data_skip_idx_order_by_expr.reference new file mode 100644 index 00000000000..d00491fd7e5 --- /dev/null +++ b/tests/queries/0_stateless/01292_optimize_data_skip_idx_order_by_expr.reference @@ -0,0 +1 @@ +1 diff --git a/tests/queries/0_stateless/01292_optimize_data_skip_idx_order_by_expr.sql b/tests/queries/0_stateless/01292_optimize_data_skip_idx_order_by_expr.sql new file mode 100644 index 00000000000..cf823be1f79 --- /dev/null +++ b/tests/queries/0_stateless/01292_optimize_data_skip_idx_order_by_expr.sql @@ -0,0 +1,14 @@ +drop table if exists data_01292; + +create table data_01292 ( + key Int, + index key_idx (key) type minmax granularity 1 +) Engine=MergeTree() ORDER BY (key+0); + +insert into data_01292 values (1); + +optimize table data_01292 final; + +select * from data_01292 where key > 0; + +drop table if exists data_01292;