#pragma once #include "binder/expression/rel_expression.h" #include "common/enums/extend_direction.h" #include "processor/operator/scan/scan_node_table.h" #include "processor/operator/scan/scan_table.h" #include "storage/predicate/column_predicate.h" #include "storage/table/rel_table.h" namespace lbug { namespace storage { class MemoryManager; class NodeTable; } // namespace storage namespace processor { struct ScanRelTableInfo : ScanTableInfo { common::RelDataDirection direction; ScanRelTableInfo(storage::Table* table, std::vector columnPredicates, common::RelDataDirection direction) : ScanTableInfo{table, std::move(columnPredicates)}, direction{direction} {} EXPLICIT_COPY_DEFAULT_MOVE(ScanRelTableInfo); void initScanState(storage::TableScanState& scanState, const std::vector& outVectors, main::ClientContext* context) override; private: ScanRelTableInfo(const ScanRelTableInfo& other) : ScanTableInfo{other}, direction{other.direction} {} }; struct ScanRelTablePrintInfo final : OPPrintInfo { std::vector tableNames; binder::expression_vector properties; std::shared_ptr boundNode; std::shared_ptr rel; std::shared_ptr nbrNode; common::ExtendDirection direction; std::string alias; ScanRelTablePrintInfo(std::vector tableNames, binder::expression_vector properties, std::shared_ptr boundNode, std::shared_ptr rel, std::shared_ptr nbrNode, common::ExtendDirection direction, std::string alias) : tableNames{std::move(tableNames)}, properties{std::move(properties)}, boundNode{std::move(boundNode)}, rel{std::move(rel)}, nbrNode{std::move(nbrNode)}, direction{direction}, alias{std::move(alias)} {} std::string toString() const override; std::unique_ptr copy() const override { return std::unique_ptr(new ScanRelTablePrintInfo(*this)); } private: ScanRelTablePrintInfo(const ScanRelTablePrintInfo& other) : OPPrintInfo{other}, tableNames{other.tableNames}, properties{other.properties}, boundNode{other.boundNode}, rel{other.rel}, nbrNode{other.nbrNode}, direction{other.direction}, alias{other.alias} {} }; class ScanRelTable final : public ScanTable { static constexpr PhysicalOperatorType type_ = PhysicalOperatorType::SCAN_REL_TABLE; public: ScanRelTable(ScanOpInfo info, ScanRelTableInfo tableInfo, std::unique_ptr child, physical_op_id id, std::unique_ptr printInfo, PhysicalOperatorType operatorType = type_) : ScanTable{operatorType, std::move(info), std::move(child), id, std::move(printInfo)}, tableInfo{std::move(tableInfo)}, sourceNodeScanInfo{DataPos::getInvalidPos(), {}} {} ScanRelTable(ScanOpInfo info, ScanRelTableInfo tableInfo, std::vector sourceNodeTables, physical_op_id id, std::unique_ptr printInfo, PhysicalOperatorType operatorType = type_) : ScanTable{operatorType, std::move(info), id, std::move(printInfo)}, tableInfo{std::move(tableInfo)}, sourceNodeTables{std::move(sourceNodeTables)}, sourceNodeScanInfo{DataPos::getInvalidPos(), {}}, sourceMode{true} {} ScanRelTable(ScanOpInfo info, ScanRelTableInfo tableInfo, std::vector sourceNodeTableInfos, std::vector> sourceNodeSharedStates, std::shared_ptr sourceNodeProgressSharedState, ScanOpInfo sourceNodeScanInfo, physical_op_id id, std::unique_ptr printInfo, PhysicalOperatorType operatorType = type_) : ScanTable{operatorType, std::move(info), id, std::move(printInfo)}, tableInfo{std::move(tableInfo)}, sourceNodeTableInfos{std::move(sourceNodeTableInfos)}, sourceNodeSharedStates{std::move(sourceNodeSharedStates)}, sourceNodeProgressSharedState{std::move(sourceNodeProgressSharedState)}, sourceNodeScanInfo{std::move(sourceNodeScanInfo)}, sourceMode{true}, sourceNodeScanMode{true} {} bool isSource() const override { return sourceMode; } bool isParallel() const override { return !sourceMode; } void initLocalStateInternal(ResultSet* resultSet, ExecutionContext* context) override; bool getNextTuplesInternal(ExecutionContext* context) override; std::unique_ptr copy() override { std::unique_ptr result; if (sourceMode) { if (sourceNodeScanMode) { result = std::make_unique(opInfo.copy(), tableInfo.copy(), copyVector(sourceNodeTableInfos), sourceNodeSharedStates, sourceNodeProgressSharedState, sourceNodeScanInfo.copy(), id, printInfo->copy(), operatorType); } else { result = std::make_unique(opInfo.copy(), tableInfo.copy(), sourceNodeTables, id, printInfo->copy(), operatorType); } } else { result = std::make_unique(opInfo.copy(), tableInfo.copy(), children[0]->copy(), id, printInfo->copy(), operatorType); } result->multiParentPackedScanEnabled = multiParentPackedScanEnabled; result->nbrNodeMaskMap = nbrNodeMaskMap; return result; } protected: void initGlobalStateInternal(ExecutionContext* context) override; bool fetchNextBoundNodeBatch(transaction::Transaction* transaction); void updatePackedChildSlices(common::sel_t outputSize) const; // Multi-parent packed batches (see docs/multi_parent_lifetime.md). Disabled by default: // standard consumers of the packed extend output rely on the one-parent-per-batch // factorization contract (bound vector flat with selSize 1). Only packed-aware consumers // (currently PackedFilteredCount, which reads the PackedChildSlices descriptor) enable // this, via setMultiParentPackedScanEnabled(), so the CSR scan may pack children of many // parents into one output batch. bool multiParentPackedScanEnabled = false; public: void setMultiParentPackedScanEnabled(bool enabled) { multiParentPackedScanEnabled = enabled; } // Semi mask on the neighbour node (outVectors[0]). Created at map time for every // extend that scans the nbr ID; enabled (populated) only when a hash-join SIP // SemiMasker targets this operator. Shared across plan copies so every copy filters // by the same masks, mirroring ScanNodeTableSharedState behaviour. void setNbrNodeMaskMap(std::shared_ptr maskMap) { nbrNodeMaskMap = std::move(maskMap); } common::table_id_map_t getNbrNodeMasks() const { if (nbrNodeMaskMap == nullptr) { return {}; } return nbrNodeMaskMap->getMasks(); } protected: // Thin wrappers over the shared ScanTable helpers; ScanRelTable additionally skips // filtering for multi-parent packed batches (packed output violates the one-parent- // per-batch contract the filter assumes). common::sel_t applyNbrNodeMask(); // Restores the shared output chunk to UNFLAT before neighbour filtering and returns // the filtered size. Storage scans only refill the selection vector; a downstream // Flatten may have left the chunk FLAT, which would make Filter dispatch scalar // (both-flat) comparison and leak non-matching rows. Mirrors ScanNodeTable. common::sel_t getFilteredOutputSize(); void refreshNbrMaskCache(); std::shared_ptr nbrNodeMaskMap; ScanRelTableInfo tableInfo; std::unique_ptr scanState; std::vector sourceNodeTables; std::vector sourceNodeTableInfos; std::vector> sourceNodeSharedStates; std::shared_ptr sourceNodeProgressSharedState; ScanOpInfo sourceNodeScanInfo; std::unique_ptr sourceNodeScanState; std::vector sourceNodeOutVectors; bool sourceMode = false; bool sourceNodeScanMode = false; common::idx_t currentSourceTableIdx = 0; common::offset_t nextSourceOffset = 0; common::row_idx_t currentSourceTableNumRows = 0; }; } // namespace processor } // namespace lbug