Skip to content

Commit

Permalink
Optimize getPartitionEndRow for window function (#6625)
Browse files Browse the repository at this point in the history
* optimize getPartitionEndRow if the partition end is very close

Signed-off-by: gengliqi <[email protected]>

* fix copy of columns

Signed-off-by: gengliqi <[email protected]>

* fix

Signed-off-by: gengliqi <[email protected]>

Signed-off-by: gengliqi <[email protected]>
  • Loading branch information
gengliqi authored Jan 11, 2023
1 parent f5021e1 commit 9e1b4a1
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 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;

// Compare several times first.
// It will speed up if the partition end is very close.
for (; left <= std::min(left + 2, right); ++left)
{
if (isDifferentFromPrevPartition(left))
{
return left;
}
}

while (left <= right)
{
Int64 middle = left + (right - left) / 2;
Int64 middle = (left + right) >> 1;
if (isDifferentFromPrevPartition(middle))
{
right = middle - 1;
Expand Down

0 comments on commit 9e1b4a1

Please sign in to comment.