forked from oap-project/velox
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
434 additions
and
31 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
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,99 @@ | ||
/* | ||
* 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/RowLevelStreamingWindowBuild.h" | ||
|
||
namespace facebook::velox::exec { | ||
|
||
RowLevelStreamingWindowBuild::RowLevelStreamingWindowBuild( | ||
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) {} | ||
|
||
void RowLevelStreamingWindowBuild::buildNextInputOrPartition(bool isFinished) { | ||
sortedRows_.push_back(inputRows_); | ||
if (windowPartitions_.size() <= inputCurrentPartition_) { | ||
auto partition = | ||
folly::Range(sortedRows_.back().data(), sortedRows_.back().size()); | ||
|
||
windowPartitions_.push_back(std::make_shared<WindowPartition>( | ||
data_.get(), | ||
partition, | ||
inputColumns_, | ||
sortKeyInfo_, | ||
ProcessingUnit::kRow)); | ||
} | ||
|
||
windowPartitions_[inputCurrentPartition_]->insertNewBatch(sortedRows_.back()); | ||
|
||
if (isFinished) { | ||
windowPartitions_[inputCurrentPartition_]->setTotalNum( | ||
currentPartitionNum_ - 1); | ||
|
||
inputCurrentPartition_++; | ||
currentPartitionNum_ = 1; | ||
} | ||
|
||
inputRows_.clear(); | ||
} | ||
|
||
void RowLevelStreamingWindowBuild::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) { | ||
currentPartitionNum_++; | ||
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_)) { | ||
buildNextInputOrPartition(true); | ||
} | ||
|
||
// Wait for the peers to be ready in single partition; these peers are the | ||
// rows that have identical values in the ORDER BY clause. | ||
if (previousRow_ != nullptr && inputRows_.size() > 0 && | ||
compareRowsWithKeys(previousRow_, newRow, sortKeyInfo_)) { | ||
buildNextInputOrPartition(false); | ||
} | ||
|
||
inputRows_.push_back(newRow); | ||
previousRow_ = newRow; | ||
} | ||
} | ||
|
||
void RowLevelStreamingWindowBuild::noMoreInput() { | ||
isFinished_ = true; | ||
buildNextInputOrPartition(true); | ||
} | ||
|
||
std::shared_ptr<WindowPartition> RowLevelStreamingWindowBuild::nextPartition() { | ||
return windowPartitions_[++outputCurrentPartition_]; | ||
} | ||
|
||
bool RowLevelStreamingWindowBuild::hasNextPartition() { | ||
return windowPartitions_.size() > 0 && | ||
outputCurrentPartition_ <= int(windowPartitions_.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,88 @@ | ||
/* | ||
* 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 { | ||
|
||
/// Unlike StreamingWindowBuild, RowLevelStreamingWindowBuild is capable of | ||
/// processing window functions as rows arrive within a single partition, | ||
/// without the need to wait for the entire partition to be ready. This approach | ||
/// can significantly reduce memory usage, especially when a single partition | ||
/// contains a large amount of data. It is particularly suited for optimizing | ||
/// rank and row_number functions, as well as aggregate window functions with a | ||
/// default frame. | ||
class RowLevelStreamingWindowBuild : public WindowBuild { | ||
public: | ||
RowLevelStreamingWindowBuild( | ||
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::shared_ptr<WindowPartition> nextPartition() override; | ||
|
||
bool needsInput() override { | ||
// No partitions are available or the currentPartition is the last available | ||
// one, so can consume input rows. | ||
return windowPartitions_.size() == 0 || | ||
outputCurrentPartition_ == windowPartitions_.size() - 1; | ||
} | ||
|
||
private: | ||
void buildNextInputOrPartition(bool isFinished); | ||
|
||
/// Vector of pointers to each input row in the data_ RowContainer. | ||
/// Rows are erased from data_ when they are processed in WindowPartition. | ||
std::vector<std::vector<char*>> sortedRows_; | ||
|
||
// Holds input rows within the current partition. | ||
std::vector<char*> inputRows_; | ||
|
||
// Used to compare rows based on partitionKeys. | ||
char* previousRow_ = nullptr; | ||
|
||
// Current partition being output. Used to return the WidnowPartitions. | ||
vector_size_t outputCurrentPartition_ = -1; | ||
|
||
bool isFinished_ = false; | ||
|
||
// Current partition when adding input. Used to construct WindowPartitions. | ||
vector_size_t inputCurrentPartition_ = 0; | ||
|
||
std::vector<std::shared_ptr<WindowPartition>> windowPartitions_; | ||
|
||
// Records the total rows number in each partition. | ||
vector_size_t currentPartitionNum_ = 0; | ||
}; | ||
|
||
} // 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
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.