Skip to content

Commit

Permalink
Revise row-by-row check
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE committed Mar 14, 2024
1 parent d0c413b commit c08b1e7
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions velox/exec/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ void updateKRowsOffsetsColumn(
int precedingFactor = isKPreceding ? -1 : 1;
for (auto i = 0; i < numRows; i++) {
auto startValue = (int64_t)(startRow + i) + precedingFactor * offsets[i];
// Considers integer overflow case.
if (startValue != (int32_t)startValue) {
// computeValidFrames will handle INT32_MAX to compute a valid index.
if (startValue > INT32_MAX || startValue < INT32_MIN) {
// Integer overflow. computeValidFrames will replace INT32_MAX set here
// with partition's final row index.
rawFrameBounds[i] = startValue < 0 ? 0 : INT32_MAX;
} else {
rawFrameBounds[i] = startValue;
Expand Down Expand Up @@ -318,28 +318,25 @@ void Window::updateKRowsFrameBounds(
(isKPreceding ? -constantOffset : constantOffset) + startRow;

if (isKPreceding) {
// Considers a very large int64 constantOffset is used.
// Overflow.
if (startValue < INT32_MIN) {
std::fill_n(rawFrameBounds, numRows, 0);
return;
}
// Integer overflow cannot happen.
std::iota(rawFrameBounds, rawFrameBounds + numRows, startValue);
return;
}
// KFollowing.
auto overflowStart = getOverflowStart(startValue);
if (overflowStart >= 0 && overflowStart < numRows) {
std::iota(rawFrameBounds, rawFrameBounds + overflowStart, startValue);
// For remaining, use INT32_MAX, which will be converted to valid index
// by computeValidFrames.
// For remaining rows that overflow happens, use INT32_MAX.
// computeValidFrames will replace it with partition's final row index.
std::fill_n(
rawFrameBounds + overflowStart, numRows - overflowStart, INT32_MAX);
return;
}
// Integer overflow cannot happen.
std::iota(rawFrameBounds, rawFrameBounds + numRows, startValue);
return;
} else {
currentPartition_->extractColumn(
frameArg.index, partitionOffset_, numRows, 0, frameArg.value);
Expand Down

0 comments on commit c08b1e7

Please sign in to comment.