|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "velox/exec/RowsStreamingWindowBuild.h" |
| 18 | +#include "velox/exec/RowsStreamingWindowPartition.h" |
| 19 | + |
| 20 | +namespace facebook::velox::exec { |
| 21 | + |
| 22 | +RowsStreamingWindowBuild::RowsStreamingWindowBuild( |
| 23 | + const std::shared_ptr<const core::WindowNode>& windowNode, |
| 24 | + velox::memory::MemoryPool* pool, |
| 25 | + const common::SpillConfig* spillConfig, |
| 26 | + tsan_atomic<bool>* nonReclaimableSection) |
| 27 | + : WindowBuild(windowNode, pool, spillConfig, nonReclaimableSection) {} |
| 28 | + |
| 29 | +void RowsStreamingWindowBuild::buildNextInputOrPartition(bool isFinished) { |
| 30 | + if (windowPartitions_.size() <= inputCurrentPartition_) { |
| 31 | + windowPartitions_.push_back(std::make_shared<RowsStreamingWindowPartition>( |
| 32 | + data_.get(), |
| 33 | + folly::Range<char**>(nullptr, nullptr), |
| 34 | + inversedInputChannels_, |
| 35 | + sortKeyInfo_)); |
| 36 | + } |
| 37 | + |
| 38 | + windowPartitions_[inputCurrentPartition_]->addNewRows(inputRows_); |
| 39 | + |
| 40 | + if (isFinished) { |
| 41 | + windowPartitions_[inputCurrentPartition_]->setInputRowsFinished(); |
| 42 | + inputCurrentPartition_++; |
| 43 | + } |
| 44 | + |
| 45 | + inputRows_.clear(); |
| 46 | +} |
| 47 | + |
| 48 | +void RowsStreamingWindowBuild::addInput(RowVectorPtr input) { |
| 49 | + for (auto i = 0; i < inputChannels_.size(); ++i) { |
| 50 | + decodedInputVectors_[i].decode(*input->childAt(inputChannels_[i])); |
| 51 | + } |
| 52 | + |
| 53 | + for (auto row = 0; row < input->size(); ++row) { |
| 54 | + char* newRow = data_->newRow(); |
| 55 | + |
| 56 | + for (auto col = 0; col < input->childrenSize(); ++col) { |
| 57 | + data_->store(decodedInputVectors_[col], row, newRow, col); |
| 58 | + } |
| 59 | + |
| 60 | + if (previousRow_ != nullptr && |
| 61 | + compareRowsWithKeys(previousRow_, newRow, partitionKeyInfo_)) { |
| 62 | + buildNextInputOrPartition(true); |
| 63 | + } |
| 64 | + |
| 65 | + // Wait for the peers to be ready in single partition; these peers are the |
| 66 | + // rows that have identical values in the ORDER BY clause. |
| 67 | + if (previousRow_ != nullptr && inputRows_.size() >= numRowsPerOutput_ && |
| 68 | + compareRowsWithKeys(previousRow_, newRow, sortKeyInfo_)) { |
| 69 | + buildNextInputOrPartition(false); |
| 70 | + } |
| 71 | + |
| 72 | + inputRows_.push_back(newRow); |
| 73 | + previousRow_ = newRow; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +void RowsStreamingWindowBuild::noMoreInput() { |
| 78 | + buildNextInputOrPartition(true); |
| 79 | +} |
| 80 | + |
| 81 | +std::shared_ptr<WindowPartition> RowsStreamingWindowBuild::nextPartition() { |
| 82 | + if (outputCurrentPartition_ > 0) { |
| 83 | + windowPartitions_[outputCurrentPartition_].reset(); |
| 84 | + } |
| 85 | + |
| 86 | + return windowPartitions_[++outputCurrentPartition_]; |
| 87 | +} |
| 88 | + |
| 89 | +bool RowsStreamingWindowBuild::hasNextPartition() { |
| 90 | + return windowPartitions_.size() > 0 && |
| 91 | + outputCurrentPartition_ <= int(windowPartitions_.size() - 2); |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace facebook::velox::exec |
0 commit comments