#pragma once #include #include "binder/expression/expression.h" #include "processor/operator/physical_operator.h" #include "processor/result/factorized_table.h" namespace lbug { namespace processor { struct UnionAllScanPrintInfo final : OPPrintInfo { binder::expression_vector expressions; explicit UnionAllScanPrintInfo(binder::expression_vector expressions) : expressions(std::move(expressions)) {} std::string toString() const override; std::unique_ptr copy() const override { return std::unique_ptr(new UnionAllScanPrintInfo(*this)); } private: UnionAllScanPrintInfo(const UnionAllScanPrintInfo& other) : OPPrintInfo(other), expressions(other.expressions) {} }; struct UnionAllScanInfo { std::vector outputPositions; std::vector columnIndices; UnionAllScanInfo(std::vector outputPositions, std::vector columnIndices) : outputPositions{std::move(outputPositions)}, columnIndices{std::move(columnIndices)} {} EXPLICIT_COPY_DEFAULT_MOVE(UnionAllScanInfo); private: UnionAllScanInfo(const UnionAllScanInfo& other) : outputPositions{other.outputPositions}, columnIndices{other.columnIndices} {} }; struct UnionAllScanMorsel { FactorizedTable* table; uint64_t startTupleIdx; uint64_t numTuples; UnionAllScanMorsel(FactorizedTable* table, uint64_t startTupleIdx, uint64_t numTuples) : table{table}, startTupleIdx{startTupleIdx}, numTuples{numTuples} {} }; class UnionAllScanSharedState { public: UnionAllScanSharedState(std::vector> tables, uint64_t maxMorselSize) : tables{std::move(tables)}, maxMorselSize{maxMorselSize}, tableIdx{0}, nextTupleIdxToScan{0} {} std::unique_ptr getMorsel(); // Re-arm the state for another execution of a cached physical plan: scan from the // beginning. The child ResultCollectors are internal, so their tables are cleared in // place by ResultCollector::prepareForReuse() and stay valid. void resetForReuse() { tableIdx = 0; nextTupleIdxToScan = 0; } private: std::unique_ptr getMorselNoLock(FactorizedTable* table); private: std::mutex mtx; std::vector> tables; uint64_t maxMorselSize; uint64_t tableIdx; uint64_t nextTupleIdxToScan; }; class UnionAllScan : public PhysicalOperator { static constexpr PhysicalOperatorType type_ = PhysicalOperatorType::UNION_ALL_SCAN; public: UnionAllScan(UnionAllScanInfo info, std::shared_ptr sharedState, physical_op_id id, std::unique_ptr printInfo) : PhysicalOperator{type_, id, std::move(printInfo)}, info{std::move(info)}, sharedState{std::move(sharedState)} {} bool isSource() const final { return true; } void initGlobalStateInternal(ExecutionContext* context) override; void initLocalStateInternal(ResultSet* resultSet_, ExecutionContext* context) final; bool getNextTuplesInternal(ExecutionContext* context) final; std::unique_ptr copy() override { auto result = std::make_unique(info.copy(), sharedState, id, printInfo->copy()); for (auto i = 0u; i < children.size(); ++i) { result->addChild(children[i]->copy()); } return result; } private: UnionAllScanInfo info; std::shared_ptr sharedState; std::vector vectors; }; } // namespace processor } // namespace lbug