Skip to content
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

Avoid out of bound InsertRangeFrom exception in MergingSortedBlockInputStream #8445

Merged
merged 5 commits into from
Dec 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions dbms/src/DataStreams/MergingSortedBlockInputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void MergingSortedBlockInputStream::merge(MutableColumns & merged_columns, std::
*/
auto count_row_and_check_limit = [&, this]() {
++total_merged_rows;
if (limit && total_merged_rows == limit)
if (limit && total_merged_rows >= limit)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this cause the number of rows output by MergingSortedBlockInputStream to be greater than limit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, the element is inserted before this function, although it won't affect query correctness, it do introduce tricky behavior. I've updated the code to finish execution immediately when limit is reached.

{
cancel(false);
finished = true;
Expand Down Expand Up @@ -216,14 +216,21 @@ void MergingSortedBlockInputStream::merge(MutableColumns & merged_columns, std::
merged_columns[i] = (*std::move(source_blocks[source_num]->getByPosition(i).column)).mutate();

size_t merged_rows = merged_columns.at(0)->size();

if (limit && total_merged_rows + merged_rows > limit)
if (limit && total_merged_rows + merged_rows >= limit)
{
RUNTIME_CHECK_MSG(
limit >= total_merged_rows,
"Unexpect limit and total_merged_rows {} {}",
limit,
total_merged_rows);
merged_rows = limit - total_merged_rows;
for (size_t i = 0; i < num_columns; ++i)
if likely (total_merged_rows + merged_rows > limit)
{
auto & column = merged_columns[i];
column = (*column->cut(0, merged_rows)).mutate();
for (size_t i = 0; i < num_columns; ++i)
{
auto & column = merged_columns[i];
column = (*column->cut(0, merged_rows)).mutate();
}
}

cancel(false);
Expand Down