-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Storage: row ID generation for MVCC bitmap filter (#6458)
ref #6296
- Loading branch information
Showing
39 changed files
with
1,802 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
dbms/src/Storages/DeltaMerge/BitmapFilter/BitmapFilter.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright 2022 PingCAP, Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <Storages/DeltaMerge/BitmapFilter/BitmapFilter.h> | ||
#include <Storages/DeltaMerge/DeltaMergeHelpers.h> | ||
#include <Storages/DeltaMerge/Segment.h> | ||
|
||
namespace DB::DM | ||
{ | ||
BitmapFilter::BitmapFilter(UInt32 size_, bool default_value) | ||
: filter(size_, default_value) | ||
, all_match(default_value) | ||
{} | ||
|
||
void BitmapFilter::set(BlockInputStreamPtr & stream) | ||
{ | ||
stream->readPrefix(); | ||
for (;;) | ||
{ | ||
FilterPtr f = nullptr; | ||
auto blk = stream->read(f, /*res_filter*/ true); | ||
if (likely(blk)) | ||
{ | ||
set(blk.segmentRowIdCol(), f); | ||
} | ||
else | ||
{ | ||
break; | ||
} | ||
} | ||
stream->readSuffix(); | ||
} | ||
|
||
void BitmapFilter::set(const ColumnPtr & col, const FilterPtr & f) | ||
{ | ||
const auto * v = toColumnVectorDataPtr<UInt32>(col); | ||
set(v->data(), v->size(), f); | ||
} | ||
|
||
void BitmapFilter::set(const UInt32 * data, UInt32 size, const FilterPtr & f) | ||
{ | ||
if (size == 0) | ||
{ | ||
return; | ||
} | ||
if (!f) | ||
{ | ||
for (UInt32 i = 0; i < size; i++) | ||
{ | ||
UInt32 row_id = *(data + i); | ||
filter[row_id] = true; | ||
} | ||
} | ||
else | ||
{ | ||
RUNTIME_CHECK(size == f->size(), size, f->size()); | ||
for (UInt32 i = 0; i < size; i++) | ||
{ | ||
UInt32 row_id = *(data + i); | ||
filter[row_id] = (*f)[i]; | ||
} | ||
} | ||
} | ||
|
||
void BitmapFilter::set(UInt32 start, UInt32 limit) | ||
{ | ||
RUNTIME_CHECK(start + limit <= filter.size(), start, limit, filter.size()); | ||
std::fill(filter.begin() + start, filter.begin() + start + limit, true); | ||
} | ||
|
||
bool BitmapFilter::get(IColumn::Filter & f, UInt32 start, UInt32 limit) const | ||
{ | ||
RUNTIME_CHECK(start + limit <= filter.size(), start, limit, filter.size()); | ||
auto begin = filter.cbegin() + start; | ||
auto end = filter.cbegin() + start + limit; | ||
if (all_match || std::find(begin, end, false) == end) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
std::copy(begin, end, f.begin()); | ||
return false; | ||
} | ||
} | ||
|
||
void BitmapFilter::runOptimize() | ||
{ | ||
all_match = std::find(filter.begin(), filter.end(), false) == filter.end(); | ||
} | ||
|
||
String BitmapFilter::toDebugString() const | ||
{ | ||
String s(filter.size(), '1'); | ||
for (UInt32 i = 0; i < filter.size(); i++) | ||
{ | ||
if (!filter[i]) | ||
{ | ||
s[i] = '0'; | ||
} | ||
} | ||
return fmt::format("{}", s); | ||
} | ||
|
||
size_t BitmapFilter::count() const | ||
{ | ||
return std::count(filter.cbegin(), filter.cend(), true); | ||
} | ||
} // namespace DB::DM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2022 PingCAP, Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include <Columns/IColumn.h> | ||
#include <DataStreams/IBlockInputStream.h> | ||
|
||
namespace DB::DM | ||
{ | ||
|
||
class BitmapFilter | ||
{ | ||
public: | ||
BitmapFilter(UInt32 size_, bool default_value); | ||
|
||
void set(BlockInputStreamPtr & stream); | ||
void set(const ColumnPtr & col, const FilterPtr & f); | ||
void set(const UInt32 * data, UInt32 size, const FilterPtr & f); | ||
void set(UInt32 start, UInt32 limit); | ||
// If return true, all data is match and do not fill the filter. | ||
bool get(IColumn::Filter & f, UInt32 start, UInt32 limit) const; | ||
|
||
void runOptimize(); | ||
|
||
String toDebugString() const; | ||
size_t count() const; | ||
|
||
private: | ||
std::vector<bool> filter; | ||
bool all_match; | ||
}; | ||
|
||
using BitmapFilterPtr = std::shared_ptr<BitmapFilter>; | ||
} // namespace DB::DM |
99 changes: 99 additions & 0 deletions
99
dbms/src/Storages/DeltaMerge/BitmapFilter/BitmapFilterBlockInputStream.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2022 PingCAP, Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <Storages/DeltaMerge/BitmapFilter/BitmapFilter.h> | ||
#include <Storages/DeltaMerge/BitmapFilter/BitmapFilterBlockInputStream.h> | ||
#include <Storages/DeltaMerge/DeltaMergeHelpers.h> | ||
|
||
namespace DB::DM | ||
{ | ||
BitmapFilterBlockInputStream::BitmapFilterBlockInputStream( | ||
const ColumnDefines & columns_to_read, | ||
BlockInputStreamPtr stable_, | ||
BlockInputStreamPtr delta_, | ||
size_t stable_rows_, | ||
const BitmapFilterPtr & bitmap_filter_, | ||
const String & req_id_) | ||
: header(toEmptyBlock(columns_to_read)) | ||
, stable(stable_) | ||
, delta(delta_) | ||
, stable_rows(stable_rows_) | ||
, bitmap_filter(bitmap_filter_) | ||
, log(Logger::get(NAME, req_id_)) | ||
{} | ||
|
||
Block BitmapFilterBlockInputStream::readImpl(FilterPtr & res_filter, bool return_filter) | ||
{ | ||
auto [block, from_delta] = readBlock(); | ||
if (block) | ||
{ | ||
if (from_delta) | ||
{ | ||
block.setStartOffset(block.startOffset() + stable_rows); | ||
} | ||
|
||
filter.resize(block.rows()); | ||
bool all_match = bitmap_filter->get(filter, block.startOffset(), block.rows()); | ||
if (!all_match) | ||
{ | ||
if (return_filter) | ||
{ | ||
res_filter = &filter; | ||
} | ||
else | ||
{ | ||
for (auto & col : block) | ||
{ | ||
col.column = col.column->filter(filter, block.rows()); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
res_filter = nullptr; | ||
} | ||
} | ||
return block; | ||
} | ||
|
||
// <Block, from_delta> | ||
std::pair<Block, bool> BitmapFilterBlockInputStream::readBlock() | ||
{ | ||
if (stable == nullptr && delta == nullptr) | ||
{ | ||
return {{}, false}; | ||
} | ||
|
||
if (stable == nullptr) | ||
{ | ||
return {delta->read(), true}; | ||
} | ||
|
||
auto block = stable->read(); | ||
if (block) | ||
{ | ||
return {block, false}; | ||
} | ||
else | ||
{ | ||
stable = nullptr; | ||
if (delta != nullptr) | ||
{ | ||
block = delta->read(); | ||
} | ||
return {block, true}; | ||
} | ||
} | ||
|
||
} // namespace DB::DM |
Oops, something went wrong.