diff --git a/dbms/src/Storages/MergeTree/MergeTreeRangeReader.cpp b/dbms/src/Storages/MergeTree/MergeTreeRangeReader.cpp index 6e887a0bf9c..7718ed642d3 100644 --- a/dbms/src/Storages/MergeTree/MergeTreeRangeReader.cpp +++ b/dbms/src/Storages/MergeTree/MergeTreeRangeReader.cpp @@ -317,6 +317,9 @@ void MergeTreeRangeReader::ReadResult::optimize(bool can_read_incomplete_granule } num_rows_to_skip_in_last_granule += rows_per_granule_original.back() - rows_per_granule.back(); + filter_original = filter; + filter_holder_original = std::move(filter_holder); + /// Check if const 1 after shrink if (countBytesInResultFilter(filter->getData()) + total_zero_rows_in_tails == total_rows_per_granule) { @@ -333,12 +336,10 @@ void MergeTreeRangeReader::ReadResult::optimize(bool can_read_incomplete_granule collapseZeroTails(filter->getData(), new_data); total_rows_per_granule = new_filter->size(); num_rows = total_rows_per_granule; - filter_original = filter; - filter_holder_original = std::move(filter_holder); filter = new_filter.get(); filter_holder = std::move(new_filter); - need_filter = true; } + need_filter = true; } /// Another guess, if it's worth filtering at PREWHERE else if (countBytesInResultFilter(filter->getData()) < 0.6 * filter->size()) @@ -631,7 +632,7 @@ MergeTreeRangeReader::ReadResult MergeTreeRangeReader::read(size_t max_rows, Mar if (read_result.need_filter) { auto old_columns = block_before_prewhere.getColumns(); - filterColumns(old_columns, read_result.getFilter()->getData()); + filterColumns(old_columns, read_result.getFilterOriginal()->getData()); block_before_prewhere.setColumns(std::move(old_columns)); } @@ -875,7 +876,7 @@ void MergeTreeRangeReader::executePrewhereActionsAndFilterColumns(ReadResult & r if (result.getFilter()) { /// filter might be shrinked while columns not - auto result_filter = result.getFilterOriginal() ? result.getFilterOriginal() : result.getFilter(); + auto result_filter = result.getFilterOriginal(); filterColumns(result.columns, result_filter->getData()); result.need_filter = true; diff --git a/dbms/src/Storages/MergeTree/MergeTreeRangeReader.h b/dbms/src/Storages/MergeTree/MergeTreeRangeReader.h index 4309076931d..affdb00147a 100644 --- a/dbms/src/Storages/MergeTree/MergeTreeRangeReader.h +++ b/dbms/src/Storages/MergeTree/MergeTreeRangeReader.h @@ -144,7 +144,7 @@ public: /// The number of bytes read from disk. size_t numBytesRead() const { return num_bytes_read; } /// Filter you need to apply to newly-read columns in order to add them to block. - const ColumnUInt8 * getFilterOriginal() const { return filter_original; } + const ColumnUInt8 * getFilterOriginal() const { return filter_original ? filter_original : filter; } const ColumnUInt8 * getFilter() const { return filter; } ColumnPtr & getFilterHolder() { return filter_holder; } diff --git a/dbms/tests/queries/0_stateless/01097_one_more_range_reader_test.reference b/dbms/tests/queries/0_stateless/01097_one_more_range_reader_test.reference new file mode 100644 index 00000000000..b4dfe343bbe --- /dev/null +++ b/dbms/tests/queries/0_stateless/01097_one_more_range_reader_test.reference @@ -0,0 +1,3 @@ +foo +foo +foo diff --git a/dbms/tests/queries/0_stateless/01097_one_more_range_reader_test.sql b/dbms/tests/queries/0_stateless/01097_one_more_range_reader_test.sql new file mode 100644 index 00000000000..53eab0dc52e --- /dev/null +++ b/dbms/tests/queries/0_stateless/01097_one_more_range_reader_test.sql @@ -0,0 +1,17 @@ +drop table if exists t; + +create table t (id UInt32, a Int) engine = MergeTree order by id; + +insert into t values (1, 0) (2, 1) (3, 0) (4, 0) (5, 0); +alter table t add column s String default 'foo'; +select s from t prewhere a = 1; + +drop table t; + +create table t (id UInt32, a Int) engine = MergeTree order by id; + +insert into t values (1, 1) (2, 1) (3, 0) (4, 0) (5, 0); +alter table t add column s String default 'foo'; +select s from t prewhere a = 1; + +drop table t;