#include "common/constants.h"
#include "main/attached_database.h"
#include "main/database_manager.h"
#include "planner/operator/scan/logical_count_rel_table.h"
#include "processor/operator/scan/count_rel_table.h"
#include "processor/plan_mapper.h"
#include "storage/partition_storage_registry.h"
#include "storage/storage_manager.h"
using namespace lbug::common;
using namespace lbug::planner;
using namespace lbug::storage;
namespace lbug {
namespace processor {
std::unique_ptr<PhysicalOperator> PlanMapper::mapCountRelTable(
const LogicalOperator* logicalOperator) {
auto& logicalCountRelTable = logicalOperator->constCast<LogicalCountRelTable>();
auto outSchema = logicalCountRelTable.getSchema();
auto dbName = logicalCountRelTable.getDbName();
auto storageManager = StorageManager::Get(*clientContext);
if (!dbName.empty()) {
auto* attachedDB = main::DatabaseManager::Get(*clientContext)->getAttachedDatabase(dbName);
if (attachedDB->getDBType() == common::ATTACHED_LBUG_DB_TYPE) {
auto* attachedLbug = static_cast<main::AttachedLbugDatabase*>(attachedDB);
storageManager = attachedLbug->getStorageManager();
}
}
std::vector<NodeTable*> nodeTables;
auto* attachedLbugDB = static_cast<main::AttachedLbugDatabase*>(nullptr);
if (!dbName.empty()) {
auto* attachedDB = main::DatabaseManager::Get(*clientContext)->getAttachedDatabase(dbName);
if (attachedDB->getDBType() == common::ATTACHED_LBUG_DB_TYPE) {
attachedLbugDB = static_cast<main::AttachedLbugDatabase*>(attachedDB);
}
}
for (auto tableID : logicalCountRelTable.getBoundNodeTableIDs()) {
if (attachedLbugDB != nullptr) {
auto* attachedSM = attachedLbugDB->getStorageManager();
nodeTables.push_back(attachedSM->getTable(tableID)->ptrCast<NodeTable>());
} else {
nodeTables.push_back(
storage::PartitionStorageRegistry::resolveNodeTableByID(clientContext, tableID)
->ptrCast<NodeTable>());
}
}
std::vector<RelTable*> relTables;
for (auto tableID : logicalCountRelTable.getRelTableIDs()) {
relTables.push_back(storageManager->getTable(tableID)->ptrCast<RelTable>());
}
auto extendDirection = logicalCountRelTable.getDirection();
RelDataDirection relDirection;
if (extendDirection == ExtendDirection::FWD) {
relDirection = RelDataDirection::FWD;
} else if (extendDirection == ExtendDirection::BWD) {
relDirection = RelDataDirection::BWD;
} else {
relDirection = RelDataDirection::FWD;
}
auto countOutputPos = getDataPos(*logicalCountRelTable.getCountExpr(), *outSchema);
auto printInfo = std::make_unique<CountRelTablePrintInfo>(
logicalCountRelTable.getRelGroupEntry()->getName());
return std::make_unique<CountRelTable>(std::move(nodeTables), std::move(relTables),
relDirection, countOutputPos, logicalCountRelTable.getReturnNullOnZero(), getOperatorID(),
std::move(printInfo));
}
} }