-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RankLikeWindowBuild to optimize rank functions
- Loading branch information
Showing
9 changed files
with
292 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "velox/exec/RankLikeWindowBuild.h" | ||
|
||
namespace facebook::velox::exec { | ||
|
||
RankLikeWindowBuild::RankLikeWindowBuild( | ||
const std::shared_ptr<const core::WindowNode>& windowNode, | ||
velox::memory::MemoryPool* pool, | ||
const common::SpillConfig* spillConfig, | ||
tsan_atomic<bool>* nonReclaimableSection) | ||
: WindowBuild(windowNode, pool, spillConfig, nonReclaimableSection) { | ||
partitionOffsets_.push_back(0); | ||
} | ||
|
||
void RankLikeWindowBuild::addInput(RowVectorPtr input) { | ||
for (auto i = 0; i < inputChannels_.size(); ++i) { | ||
decodedInputVectors_[i].decode(*input->childAt(inputChannels_[i])); | ||
} | ||
|
||
for (auto row = 0; row < input->size(); ++row) { | ||
char* newRow = data_->newRow(); | ||
|
||
for (auto col = 0; col < input->childrenSize(); ++col) { | ||
data_->store(decodedInputVectors_[col], row, newRow, col); | ||
} | ||
|
||
if (previousRow_ != nullptr && | ||
compareRowsWithKeys(previousRow_, newRow, partitionKeyInfo_)) { | ||
sortedRows_.push_back(inputRows_); | ||
partitionOffsets_.push_back(0); | ||
inputRows_.clear(); | ||
} | ||
|
||
inputRows_.push_back(newRow); | ||
previousRow_ = newRow; | ||
} | ||
partitionOffsets_.push_back(inputRows_.size()); | ||
sortedRows_.push_back(inputRows_); | ||
inputRows_.clear(); | ||
} | ||
|
||
void RankLikeWindowBuild::noMoreInput() { | ||
isFinished_ = true; | ||
inputRows_.clear(); | ||
} | ||
|
||
std::unique_ptr<WindowPartition> RankLikeWindowBuild::nextPartition() { | ||
currentPartition_++; | ||
|
||
if (currentPartition_ > 0) { | ||
// Erase data_ and sortedRows; | ||
data_->eraseRows(folly::Range<char**>( | ||
sortedRows_[currentPartition_ - 1].data(), | ||
sortedRows_[currentPartition_ - 1].size())); | ||
sortedRows_[currentPartition_ - 1].clear(); | ||
} | ||
|
||
auto partition = folly::Range( | ||
sortedRows_[currentPartition_].data(), | ||
sortedRows_[currentPartition_].size()); | ||
|
||
auto offset = 0; | ||
for (auto i = currentPartition_; partitionOffsets_[i] != 0; i--) { | ||
offset += partitionOffsets_[i]; | ||
} | ||
return std::make_unique<WindowPartition>( | ||
data_.get(), partition, inputColumns_, sortKeyInfo_, offset); | ||
} | ||
|
||
bool RankLikeWindowBuild::hasNextPartition() { | ||
return sortedRows_.size() > 0 && currentPartition_ != sortedRows_.size() - 1; | ||
} | ||
|
||
} // namespace facebook::velox::exec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "velox/exec/WindowBuild.h" | ||
|
||
namespace facebook::velox::exec { | ||
|
||
/// If the data has already been sorted according to the partition key and sort | ||
/// key, there is no need to hold all the data of a partition in memory for Rank | ||
/// and row_number functions. RankWindowBuild adopts a streaming method to | ||
/// construct WindowPartition, which can reduce the occurrence of Out Of Memory | ||
/// (OOM). | ||
class RankLikeWindowBuild : public WindowBuild { | ||
public: | ||
RankLikeWindowBuild( | ||
const std::shared_ptr<const core::WindowNode>& windowNode, | ||
velox::memory::MemoryPool* pool, | ||
const common::SpillConfig* spillConfig, | ||
tsan_atomic<bool>* nonReclaimableSection); | ||
|
||
void addInput(RowVectorPtr input) override; | ||
|
||
void spill() override { | ||
VELOX_UNREACHABLE(); | ||
} | ||
|
||
std::optional<common::SpillStats> spilledStats() const override { | ||
return std::nullopt; | ||
} | ||
|
||
void noMoreInput() override; | ||
|
||
bool hasNextPartition() override; | ||
|
||
std::unique_ptr<WindowPartition> nextPartition() override; | ||
|
||
bool needsInput() override { | ||
return !isFinished_; | ||
} | ||
|
||
private: | ||
// Vector of pointers to each input row in the data_ RowContainer. | ||
// Rows are erased from data_ when they are output from the | ||
// Window operator. | ||
std::vector<std::vector<char*>> sortedRows_; | ||
|
||
// Holds input rows within the current partition. | ||
std::vector<char*> inputRows_; | ||
|
||
// Indices of the start row (in sortedRows_) of each partition in | ||
// the RowContainer data_. This auxiliary structure helps demarcate | ||
// partitions. | ||
std::vector<vector_size_t> partitionOffsets_; | ||
|
||
// Used to compare rows based on partitionKeys. | ||
char* previousRow_ = nullptr; | ||
|
||
// Current partition being output. Used to construct WindowPartitions | ||
// during resetPartition. | ||
vector_size_t currentPartition_ = -1; | ||
|
||
bool isFinished_ = false; | ||
}; | ||
|
||
} // namespace facebook::velox::exec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.