Merge pull request #9612 from CurtizJ/fix-range-reader

Fix MergeTreeRangeReader
This commit is contained in:
alexey-milovidov 2020-03-12 23:31:50 +03:00 committed by GitHub
commit dc9725a757
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 6 deletions

View File

@ -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(); 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 /// Check if const 1 after shrink
if (countBytesInResultFilter(filter->getData()) + total_zero_rows_in_tails == total_rows_per_granule) 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); collapseZeroTails(filter->getData(), new_data);
total_rows_per_granule = new_filter->size(); total_rows_per_granule = new_filter->size();
num_rows = total_rows_per_granule; num_rows = total_rows_per_granule;
filter_original = filter;
filter_holder_original = std::move(filter_holder);
filter = new_filter.get(); filter = new_filter.get();
filter_holder = std::move(new_filter); filter_holder = std::move(new_filter);
need_filter = true;
} }
need_filter = true;
} }
/// Another guess, if it's worth filtering at PREWHERE /// Another guess, if it's worth filtering at PREWHERE
else if (countBytesInResultFilter(filter->getData()) < 0.6 * filter->size()) 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) if (read_result.need_filter)
{ {
auto old_columns = block_before_prewhere.getColumns(); 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)); block_before_prewhere.setColumns(std::move(old_columns));
} }
@ -875,7 +876,7 @@ void MergeTreeRangeReader::executePrewhereActionsAndFilterColumns(ReadResult & r
if (result.getFilter()) if (result.getFilter())
{ {
/// filter might be shrinked while columns not /// 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()); filterColumns(result.columns, result_filter->getData());
result.need_filter = true; result.need_filter = true;

View File

@ -144,7 +144,7 @@ public:
/// The number of bytes read from disk. /// The number of bytes read from disk.
size_t numBytesRead() const { return num_bytes_read; } size_t numBytesRead() const { return num_bytes_read; }
/// Filter you need to apply to newly-read columns in order to add them to block. /// 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; } const ColumnUInt8 * getFilter() const { return filter; }
ColumnPtr & getFilterHolder() { return filter_holder; } ColumnPtr & getFilterHolder() { return filter_holder; }

View File

@ -0,0 +1,3 @@
foo
foo
foo

View File

@ -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;