diff --git a/velox/exec/Window.cpp b/velox/exec/Window.cpp index b963fc059cbe..1f11c9591bf6 100644 --- a/velox/exec/Window.cpp +++ b/velox/exec/Window.cpp @@ -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; @@ -318,12 +318,11 @@ 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; } @@ -331,15 +330,13 @@ void Window::updateKRowsFrameBounds( 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);