#include "processor/operator/scan/scan_rel_table.h" #include #include "binder/expression/expression_util.h" #include "common/system_config.h" #include "processor/execution_context.h" #include "storage/buffer_manager/memory_manager.h" #include "storage/local_storage/local_rel_table.h" #include "storage/table/arrow_node_table.h" #include "storage/table/arrow_rel_table.h" #include "storage/table/external_table_hooks.h" #include "storage/table/foreign_rel_table.h" #include "storage/table/ice_disk_node_table.h" #include "storage/table/ice_disk_rel_table.h" #include "storage/table/node_table.h" using namespace lbug::common; using namespace lbug::storage; namespace lbug { namespace processor { static std::unique_ptr createSourceNodeTableScanState(NodeTable* table, ValueVector* nodeIDVector, const std::vector& outVectors, MemoryManager* memoryManager) { // Extension-backed columnar node tables create their state through registered hooks, // mirroring createNodeTableScanState. if (auto hooks = storage::ColumnarTableHookRegistry::getTableHooks(table->getStorageManager(), table->getTableID())) { DASSERT(hooks->createNodeScanState); return hooks->createNodeScanState(table, nodeIDVector, outVectors, nodeIDVector->state, memoryManager); } if (dynamic_cast(table) != nullptr) { return std::make_unique(*memoryManager, nodeIDVector, outVectors, nodeIDVector->state); } if (dynamic_cast(table) != nullptr) { return std::make_unique(*memoryManager, nodeIDVector, outVectors, nodeIDVector->state); } return std::make_unique(nodeIDVector, outVectors, nodeIDVector->state); } std::string ScanRelTablePrintInfo::toString() const { std::string result = "Tables: "; for (auto& tableName : tableNames) { result += tableName; if (tableName != tableNames.back()) { result += ", "; } } if (!alias.empty()) { result += ",Alias: "; result += alias; } result += ",Direction: ("; result += boundNode->toString(); result += ")"; switch (direction) { case ExtendDirection::FWD: { result += "-["; result += rel->detailsToString(); result += "]->"; } break; case ExtendDirection::BWD: { result += "<-["; result += rel->detailsToString(); result += "]-"; } break; case ExtendDirection::BOTH: { result += "<-["; result += rel->detailsToString(); result += "]->"; } break; default: UNREACHABLE_CODE; } result += "("; result += nbrNode->toString(); result += ")"; if (!properties.empty()) { result += ",Properties: "; result += binder::ExpressionUtil::toString(properties); } return result; } void ScanRelTableInfo::initScanState(TableScanState& scanState, const std::vector& outVectors, main::ClientContext* context) { auto transaction = transaction::Transaction::Get(*context); scanState.setToTable(transaction, table, columnIDs, copyVector(columnPredicates), direction); initScanStateVectors(scanState, outVectors, MemoryManager::Get(*context)); } void ScanRelTable::refreshNbrMaskCache() { refreshNbrMasks(nbrNodeMaskMap.get()); } common::sel_t ScanRelTable::applyNbrNodeMask() { if (multiParentPackedScanEnabled) { return scanState->outState->getSelVector().getSelSize(); } return applyNbrMaskFilter(scanState->outState->getSelVectorUnsafe(), outVectors[0]); } common::sel_t ScanRelTable::getFilteredOutputSize() { scanState->outState->setToUnflat(); return applyNbrNodeMask(); } void ScanRelTable::initLocalStateInternal(ResultSet* resultSet, ExecutionContext* context) { ScanTable::initLocalStateInternal(resultSet, context); refreshNbrMaskCache(); auto clientContext = context->clientContext; auto boundNodeIDVector = resultSet->getValueVector(opInfo.nodeIDPos).get(); auto nbrNodeIDVector = outVectors[0]; // Extension-backed columnar rel tables create their state through registered hooks. auto tableHooks = storage::ColumnarTableHookRegistry::getTableHooks( tableInfo.table->getStorageManager(), tableInfo.table->getTableID()); auto* arrowTable = dynamic_cast(tableInfo.table); auto* iceDiskTable = dynamic_cast(tableInfo.table); auto* foreignTable = dynamic_cast(tableInfo.table); if (tableHooks) { DASSERT(tableHooks->createRelScanState); scanState = tableHooks->createRelScanState(tableInfo.table->ptrCast(), boundNodeIDVector, outVectors, nbrNodeIDVector->state, storage::MemoryManager::Get(*clientContext)); } else if (arrowTable) { scanState = std::make_unique(*MemoryManager::Get(*clientContext), boundNodeIDVector, outVectors, nbrNodeIDVector->state); } else if (iceDiskTable) { scanState = std::make_unique(*MemoryManager::Get(*clientContext), boundNodeIDVector, outVectors, nbrNodeIDVector->state); } else if (foreignTable) { scanState = std::make_unique(*MemoryManager::Get(*clientContext), boundNodeIDVector, outVectors, nbrNodeIDVector->state); } else { scanState = std::make_unique(*MemoryManager::Get(*clientContext), boundNodeIDVector, outVectors, nbrNodeIDVector->state); } scanState->packedMultiParentScan = multiParentPackedScanEnabled; tableInfo.initScanState(*scanState, outVectors, clientContext); if (sourceNodeScanMode) { sourceNodeOutVectors.clear(); for (auto& pos : sourceNodeScanInfo.outVectorsPos) { sourceNodeOutVectors.push_back(resultSet->getValueVector(pos).get()); } } if (sourceMode) { currentSourceTableIdx = 0; nextSourceOffset = 0; currentSourceTableNumRows = 0; } } void ScanRelTable::initGlobalStateInternal(ExecutionContext* context) { if (!sourceNodeScanMode) { return; } DASSERT(sourceNodeTableInfos.size() == sourceNodeSharedStates.size()); // The progress state is shared across cached physical plan clones, so reset it per // execution (ScanNodeTableSharedState::initialize only accumulates into it). sourceNodeProgressSharedState->numMorsels = 0; sourceNodeProgressSharedState->numMorselsScanned = 0; for (auto i = 0u; i < sourceNodeTableInfos.size(); ++i) { sourceNodeSharedStates[i]->initialize( transaction::Transaction::Get(*context->clientContext), sourceNodeTableInfos[i].table->ptrCast(), *sourceNodeProgressSharedState, context->clientContext); } } static void initSourceNodeScanState(ScanNodeTableInfo& sourceInfo, std::unique_ptr& sourceScanState, ValueVector* boundNodeIDVector, const std::vector& sourceNodeOutVectors, main::ClientContext* context) { sourceScanState = createSourceNodeTableScanState(sourceInfo.table->ptrCast(), boundNodeIDVector, sourceNodeOutVectors, MemoryManager::Get(*context)); sourceInfo.initScanState(*sourceScanState, sourceNodeOutVectors, context); if (dynamic_cast(sourceInfo.table) || dynamic_cast(sourceInfo.table)) { sourceInfo.table->initScanState(transaction::Transaction::Get(*context), *sourceScanState); } } bool ScanRelTable::fetchNextBoundNodeBatch(transaction::Transaction* transaction) { if (sourceNodeScanMode) { auto* boundNodeIDVector = scanState->nodeIDVector; auto context = transaction->getClientContext(); while (currentSourceTableIdx < sourceNodeTableInfos.size()) { auto& sourceInfo = sourceNodeTableInfos[currentSourceTableIdx]; if (!sourceNodeScanState) { initSourceNodeScanState(sourceInfo, sourceNodeScanState, boundNodeIDVector, sourceNodeOutVectors, context); } while (sourceInfo.table->scan(transaction, *sourceNodeScanState)) { const auto outputSize = sourceNodeScanState->outState->getSelVector().getSelSize(); if (outputSize > 0) { sourceInfo.castColumns(); sourceNodeScanState->outState->setToUnflat(); tableInfo.table->initScanState(transaction, *scanState); return true; } } sourceNodeSharedStates[currentSourceTableIdx]->nextMorsel(*sourceNodeScanState, *sourceNodeProgressSharedState); if (sourceNodeScanState->source == TableScanSource::NONE) { currentSourceTableIdx++; sourceNodeScanState = nullptr; } else { sourceInfo.table->initScanState(transaction, *sourceNodeScanState); } } return false; } auto* boundNodeIDVector = scanState->nodeIDVector; while (currentSourceTableIdx < sourceNodeTables.size()) { auto* nodeTable = sourceNodeTables[currentSourceTableIdx]; if (currentSourceTableNumRows == 0) { currentSourceTableNumRows = nodeTable->getNumTotalRows(transaction); } if (nextSourceOffset >= currentSourceTableNumRows) { currentSourceTableIdx++; nextSourceOffset = 0; currentSourceTableNumRows = 0; continue; } const auto numToGenerate = std::min(DEFAULT_VECTOR_CAPACITY, currentSourceTableNumRows - nextSourceOffset); boundNodeIDVector->state->setToUnflat(); boundNodeIDVector->state->getSelVectorUnsafe().setToUnfiltered(numToGenerate); for (auto i = 0u; i < numToGenerate; ++i) { boundNodeIDVector->setValue(i, nodeID_t{nextSourceOffset + i, nodeTable->getTableID()}); } nextSourceOffset += numToGenerate; tableInfo.table->initScanState(transaction, *scanState); return true; } return false; } void ScanRelTable::updatePackedChildSlices(sel_t outputSize) const { if (operatorType != PhysicalOperatorType::PACKED_EXTEND) { scanState->outState->clearPackedChildSlices(); return; } // Attach the PackedChildSlices descriptor to the output (nbr/child) chunk state. See // docs/multi_parent_lifetime.md for the representation/lifetime contract: the descriptor // aliases the bound chunk state's selection vector via shared_ptr and is valid only for // synchronous consumption of this output batch. const auto boundSelVector = scanState->nodeIDVector->state->getSelVectorShared(); if (boundSelVector->getSelSize() > 1) { // Multi-parent packed batch: the CSR scan served the children of several parents and // recorded the prefix-sum offsets over the served parents in packedChildOffsets (the // bound vector's chunk state was switched to unflat holding exactly those parents). DASSERT(!scanState->packedChildOffsets.empty()); DASSERT(scanState->packedChildOffsets.back() == outputSize); scanState->outState->setPackedChildSlices(boundSelVector, std::move(scanState->packedChildOffsets)); } else { // Single-parent batch: the bound vector is flat pointing its selVector[0] at the actual // parent whose children are currently materialized in the output vector (see // RelTableScanState::setNodeIDVectorToFlat). DASSERT(boundSelVector->getSelSize() == 1); scanState->outState->setSingleParentPackedChildSlice(boundSelVector, outputSize); } } bool ScanRelTable::getNextTuplesInternal(ExecutionContext* context) { const auto transaction = transaction::Transaction::Get(*context->clientContext); if (sourceMode) { while (true) { while (scanState->source != TableScanSource::NONE && tableInfo.table->scan(transaction, *scanState)) { const auto outputSize = scanState->outState->getSelVector().getSelSize(); if (outputSize > 0) { const auto filteredSize = getFilteredOutputSize(); if (filteredSize == 0) { continue; } updatePackedChildSlices(filteredSize); tableInfo.castColumns(); metrics->numOutputTuple.increase(filteredSize); return true; } } if (!fetchNextBoundNodeBatch(transaction)) { scanState->releaseScanPin(); return false; } } } while (true) { while (scanState->source != TableScanSource::NONE && tableInfo.table->scan(transaction, *scanState)) { const auto outputSize = scanState->outState->getSelVector().getSelSize(); if (outputSize > 0) { const auto filteredSize = getFilteredOutputSize(); if (filteredSize == 0) { continue; } updatePackedChildSlices(filteredSize); tableInfo.castColumns(); metrics->numOutputTuple.increase(filteredSize); return true; } } scanState->restoreBoundNodeBatch(); if (!children[0]->getNextTuple(context)) { scanState->releaseScanPin(); return false; } tableInfo.table->initScanState(transaction, *scanState); } } } // namespace processor } // namespace lbug