Skip to content

Commit

Permalink
Use int32_max that will be replaced by partition end index at last
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE committed Mar 13, 2024
1 parent c412300 commit d0c413b
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions velox/exec/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,20 @@ 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;
}
}
}

// 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<int32_t>::max()) {
FOLLY_ALWAYS_INLINE int32_t getOverflowStart(int64_t startValue) {
if (startValue > (int64_t)INT32_MAX) {
return 0;
} else {
return std::numeric_limits<int32_t>::max() - constantOffset + 1;
return INT32_MAX - startValue + 1;
}
}

Expand All @@ -318,23 +319,22 @@ void Window::updateKRowsFrameBounds(

if (isKPreceding) {
// Considers a very large int64 constantOffset is used.
if (startValue < std::numeric_limits<int32_t>::min()) {
std::fill_n(rawFrameBounds, numRows, startRow);
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(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.
Expand Down

0 comments on commit d0c413b

Please sign in to comment.