diff --git a/velox/exec/Window.cpp b/velox/exec/Window.cpp index 3ef2181c99d3..b963fc059cbe 100644 --- a/velox/exec/Window.cpp +++ b/velox/exec/Window.cpp @@ -287,7 +287,8 @@ void updateKRowsOffsetsColumn( auto startValue = (int64_t)(startRow + i) + precedingFactor * offsets[i]; // Considers integer overflow case. if (startValue != (int32_t)startValue) { - rawFrameBounds[i] = startValue < 0 ? 0 : numRows - 1; + // computeValidFrames will handle INT32_MAX to compute a valid index. + rawFrameBounds[i] = startValue < 0 ? 0 : INT32_MAX; } else { rawFrameBounds[i] = startValue; } @@ -295,11 +296,11 @@ void updateKRowsOffsetsColumn( } // Get the start index that integer overflow can happen for kFollowing. -FOLLY_ALWAYS_INLINE int32_t getOverflowStart(int64_t constantOffset) { - if (constantOffset > (int64_t)std::numeric_limits::max()) { +FOLLY_ALWAYS_INLINE int32_t getOverflowStart(int64_t startValue) { + if (startValue > (int64_t)INT32_MAX) { return 0; } else { - return std::numeric_limits::max() - constantOffset + 1; + return INT32_MAX - startValue + 1; } } @@ -318,8 +319,8 @@ void Window::updateKRowsFrameBounds( if (isKPreceding) { // Considers a very large int64 constantOffset is used. - if (startValue < std::numeric_limits::min()) { - std::fill_n(rawFrameBounds, numRows, startRow); + if (startValue < INT32_MIN) { + std::fill_n(rawFrameBounds, numRows, 0); return; } // Integer overflow cannot happen. @@ -327,14 +328,13 @@ void Window::updateKRowsFrameBounds( return; } // KFollowing. - auto overflowStart = getOverflowStart(constantOffset); + auto overflowStart = getOverflowStart(startValue); if (overflowStart >= 0 && overflowStart < numRows) { std::iota(rawFrameBounds, rawFrameBounds + overflowStart, startValue); - // For remaining, set with the largest index for this partition. + // For remaining, use INT32_MAX, which will be converted to valid index + // by computeValidFrames. std::fill_n( - rawFrameBounds + overflowStart, - numRows - overflowStart, - startRow + numRows - 1); + rawFrameBounds + overflowStart, numRows - overflowStart, INT32_MAX); return; } // Integer overflow cannot happen.