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

Optimize getPartitionEndRow for window function #6625

Merged
Merged
Changes from 2 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
22 changes: 16 additions & 6 deletions dbms/src/DataStreams/WindowBlockInputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ Block WindowBlockInputStream::readImpl()
// Judge whether current_partition_row is end row of partition in current block
bool WindowTransformAction::isDifferentFromPrevPartition(UInt64 current_partition_row)
{
const auto reference_columns = inputAt(prev_frame_start);
const auto compared_columns = inputAt(partition_end);
const Columns & reference_columns = inputAt(prev_frame_start);
const Columns & compared_columns = inputAt(partition_end);

for (size_t i = 0; i < partition_column_indices.size(); ++i)
{
const auto reference_column = reference_columns[partition_column_indices[i]];
const auto * compared_column = compared_columns[partition_column_indices[i]].get();
const ColumnPtr & reference_column = reference_columns[partition_column_indices[i]];
const ColumnPtr & compared_column = compared_columns[partition_column_indices[i]];
if (window_description.partition_by[i].collator)
{
if (compared_column->compareAt(current_partition_row,
Expand Down Expand Up @@ -240,9 +240,19 @@ Int64 WindowTransformAction::getPartitionEndRow(size_t block_rows)
Int64 left = partition_end.row;
Int64 right = block_rows - 1;

while (left <= right)
// Compare several times first.
// It will benefit if the partition end is very close.
for (; left <= std::min(left + 2, right); ++left)
Copy link
Contributor

Choose a reason for hiding this comment

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

Better add a comment to explain why + 2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A magic number

{
Int64 middle = left + (right - left) / 2;
if (isDifferentFromPrevPartition(left))
{
return left;
}
}

while (left < right)
{
Int64 middle = (left + right) << 1;
Copy link
Contributor

@mengxin9014 mengxin9014 Jan 11, 2023

Choose a reason for hiding this comment

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

Suggested change
Int64 middle = (left + right) << 1;
Int64 middle = left + (right - left) << 1;

if (isDifferentFromPrevPartition(middle))
{
right = middle - 1;
Expand Down