-
Notifications
You must be signed in to change notification settings - Fork 409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FLASH-524] Optimize tmt read for single stream #254
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
dbms/src/DataStreams/TMTSingleSortedBlockInputStream.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,129 @@ | ||
#include <Columns/ColumnsCommon.h> | ||
#include <Columns/ColumnsNumber.h> | ||
#include <DataStreams/TMTSingleSortedBlockInputStream.h> | ||
#include <DataStreams/dedupUtils.h> | ||
|
||
namespace DB | ||
{ | ||
|
||
namespace ErrorCodes | ||
{ | ||
extern const int LOGICAL_ERROR; | ||
} | ||
|
||
static constexpr size_t SIMD_BYTES = 16; | ||
static constexpr Int32 STEP = SIMD_BYTES; | ||
#if __SSE2__ | ||
static const __m128i zero16 = _mm_setzero_si128(); | ||
#endif | ||
|
||
// pos in columns is made const in MergeTreeDataSelectExecutor::read. | ||
static const size_t pk_column_index = 0; | ||
static const size_t delmark_column_index = 2; | ||
|
||
/// tol_size > 0 | ||
static bool isPKDiffEachInSortedColumn(const UInt64 * data_start, size_t tol_size) | ||
solotzg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
tol_size -= 1; | ||
const UInt64 * data_pos = data_start; | ||
const UInt64 * data_end = data_start + tol_size; | ||
const UInt64 * data_end_sse = data_start + tol_size / STEP * STEP; | ||
|
||
#if __SSE2__ | ||
|
||
alignas(STEP) std::array<UInt8, STEP> step_data{}; | ||
|
||
for (; data_pos != data_end_sse; data_pos += STEP) | ||
{ | ||
for (size_t i = 0; i < STEP; ++i) | ||
step_data[i] = data_pos[i] == data_pos[i + 1]; | ||
int mask = _mm_movemask_epi8(_mm_cmpgt_epi8(_mm_loadu_si128(reinterpret_cast<const __m128i *>(step_data.data())), zero16)); | ||
if (mask) | ||
return false; | ||
} | ||
|
||
#endif | ||
|
||
for (; data_pos != data_end; ++data_pos) | ||
{ | ||
if (data_pos[0] == data_pos[1]) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
inline UInt64 getPkInBlockAt(const Block & block, size_t pos) | ||
{ | ||
return static_cast<const ColumnUInt64 *>(block.getByPosition(pk_column_index).column.get())->getData()[pos]; | ||
} | ||
|
||
void TMTSingleSortedBlockInputStream::updateNextBlock() | ||
{ | ||
if (!next_block) | ||
finish = true; | ||
cur_block = std::move(next_block); | ||
} | ||
|
||
Block TMTSingleSortedBlockInputStream::readImpl() | ||
{ | ||
while (true) | ||
{ | ||
if (finish) | ||
return Block(); | ||
|
||
{ | ||
Block tmp_block = input->read(); | ||
if (first) | ||
{ | ||
if (!tmp_block) | ||
return tmp_block; | ||
first = false; | ||
cur_block = std::move(tmp_block); | ||
continue; | ||
} | ||
next_block = std::move(tmp_block); | ||
} | ||
|
||
// cur_block is never empty. | ||
const size_t rows = cur_block.rows(); | ||
|
||
// if not exist, just make it diff from the last element. | ||
UInt64 next_block_first_pk = next_block ? getPkInBlockAt(next_block, 0) : getPkInBlockAt(cur_block, rows - 1) + 1; | ||
|
||
const UInt64 * pk_column | ||
= static_cast<const ColumnUInt64 *>(cur_block.getByPosition(pk_column_index).column.get())->getData().data(); | ||
const UInt8 * delmark_column | ||
= static_cast<const ColumnUInt8 *>(cur_block.getByPosition(delmark_column_index).column.get())->getData().data(); | ||
|
||
if (isPKDiffEachInSortedColumn(pk_column, rows) && pk_column[rows - 1] != next_block_first_pk && memoryIsZero(delmark_column, rows)) | ||
{ | ||
auto tmp_block = std::move(cur_block); | ||
updateNextBlock(); | ||
return tmp_block; | ||
} | ||
|
||
IColumn::Filter filter(rows, 0); | ||
for (size_t pos = 1; pos < rows; ++pos) | ||
{ | ||
if (pk_column[pos] != pk_column[pos - 1]) | ||
filter[pos - 1] = delmark_column[pos - 1] ^ (UInt8)1; | ||
} | ||
if (next_block_first_pk != pk_column[rows - 1]) | ||
filter[rows - 1] = delmark_column[rows - 1] ^ (UInt8)1; | ||
|
||
if (memoryIsZero(filter.data(), rows)) | ||
{ | ||
updateNextBlock(); | ||
continue; | ||
} | ||
else | ||
{ | ||
Block res = filterBlock(cur_block, filter); | ||
updateNextBlock(); | ||
return res; | ||
} | ||
} | ||
} | ||
|
||
} // namespace DB |
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,41 @@ | ||
#pragma once | ||
|
||
#include <DataStreams/IProfilingBlockInputStream.h> | ||
#include <common/logger_useful.h> | ||
|
||
namespace DB | ||
{ | ||
|
||
/// make it only can be used when pk is int64 or uint64 | ||
class TMTSingleSortedBlockInputStream : public IProfilingBlockInputStream | ||
{ | ||
public: | ||
TMTSingleSortedBlockInputStream(const BlockInputStreamPtr & input_) : input(input_) {} | ||
|
||
protected: | ||
Block getHeader() const override { return input->getHeader(); } | ||
|
||
bool isGroupedOutput() const override { return input->isGroupedOutput(); } | ||
|
||
bool isSortedOutput() const override { return input->isSortedOutput(); } | ||
|
||
const SortDescription & getSortDescription() const override { return input->getSortDescription(); } | ||
|
||
String getName() const override { return "TMTSingleSortedBlockInputStream"; } | ||
|
||
Block readImpl() override; | ||
|
||
private: | ||
void updateNextBlock(); | ||
|
||
private: | ||
BlockInputStreamPtr input; | ||
|
||
bool first = true; | ||
bool finish = false; | ||
Block cur_block; | ||
Block next_block; | ||
Logger * log = &Logger::get("TMTSingleSortedBlockInputStream"); | ||
}; | ||
|
||
} // namespace DB |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No guarantee of the columns‘s order, getting indexes dynamically is necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/pingcap/tics/blob/0f3ea5f214a0dde72229c0fc72bdf28462ccbd98/dbms/src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp#L223
By now, column: pk version del is definitely at first 3.