diff --git a/velox/exec/WindowPartition.cpp b/velox/exec/WindowPartition.cpp index 6f06dd713a6f..b8205b694675 100644 --- a/velox/exec/WindowPartition.cpp +++ b/velox/exec/WindowPartition.cpp @@ -365,7 +365,8 @@ void WindowPartition::updateKRangeFrameBounds( vector_size_t start = 0; vector_size_t end; - vector_size_t lastFoundIndex = isPreceding ? 0 : -1; + // vector_size_t lastFoundIndex = isPreceding ? 0 : -1; + vector_size_t lastFoundIndex = 0; // frameColumn is a column index into the original input rows, while // orderByColumn and mappedFrameColumn are column indices into rows in data_ // after the columns are reordered as per inputMapping_. @@ -413,12 +414,24 @@ void WindowPartition::updateKRangeFrameBounds( // [0, currentRow] are examined. For following bounds, rows between // [currentRow, numRows()) are checked. if (isPreceding) { - start = - lastFoundIndex == -1 ? 0 : std::min(lastFoundIndex, numRows - 1); + // start = + // lastFoundIndex == -1 ? 0 : std::min(lastFoundIndex, numRows - 1); + if (i == 0) { + start = 0; + } else { + auto compareResult = data_->compare( + partition_[currentRow - 1], partition_[currentRow], mappedFrameColumn, mappedFrameColumn, flags); + if (compareResult <= 0) { + start = lastFoundIndex; + } else { + start = 0; + } + } end = currentRow + 1; } else { - start = lastFoundIndex == -1 ? currentRow - : std::min(lastFoundIndex, numRows - 1); + // start = lastFoundIndex == -1 ? currentRow + // : std::min(lastFoundIndex, numRows - 1); + start = currentRow; end = partition_.size(); } rawFrameBounds[i] = searchFrameValue(