Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor scan rel table #1749

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 30 additions & 36 deletions src/include/processor/operator/scan/generic_scan_rel_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,54 @@
namespace kuzu {
namespace processor {

class RelTableDataCollection {
class RelTableCollectionScanner {
public:
RelTableDataCollection(std::vector<storage::DirectedRelTableData*> relTableDatas,
std::vector<std::unique_ptr<storage::RelTableScanState>> tableScanStates)
: relTableDatas{std::move(relTableDatas)}, tableScanStates{std::move(tableScanStates)} {}
RelTableCollectionScanner(std::vector<std::unique_ptr<RelTableScanInfo>> scanInfos)
: scanInfos{std::move(scanInfos)} {}

void resetState();
inline uint32_t getNumTablesInCollection() { return relTableDatas.size(); }
inline void resetState() {
currentTableIdx = 0;
nextTableIdx = 0;
}

void init();
bool scan(common::ValueVector* inVector, const std::vector<common::ValueVector*>& outputVectors,
transaction::Transaction* transaction);

std::unique_ptr<RelTableDataCollection> clone() const;
std::unique_ptr<RelTableCollectionScanner> clone() const;

private:
std::vector<storage::DirectedRelTableData*> relTableDatas;
std::vector<std::unique_ptr<storage::RelTableScanState>> tableScanStates;

uint32_t currentRelTableIdxToScan = UINT32_MAX;
uint32_t nextRelTableIdxToScan = 0;
std::vector<std::unique_ptr<RelTableScanInfo>> scanInfos;
std::vector<std::unique_ptr<storage::RelTableScanState>> scanStates;
uint32_t currentTableIdx = UINT32_MAX;
uint32_t nextTableIdx = 0;
};

class GenericScanRelTables : public ScanRelTable {
class ScanMultiRelTable : public ScanRelTable {
using node_table_id_scanner_map_t =
std::unordered_map<common::table_id_t, std::unique_ptr<RelTableCollectionScanner>>;

public:
GenericScanRelTables(const DataPos& inNodeIDVectorPos, std::vector<DataPos> outputVectorsPos,
std::unordered_map<common::table_id_t, std::unique_ptr<RelTableDataCollection>>
relTableCollectionPerNodeTable,
std::unique_ptr<PhysicalOperator> child, uint32_t id, const std::string& paramsString)
: ScanRelTable{inNodeIDVectorPos, std::move(outputVectorsPos),
PhysicalOperatorType::GENERIC_SCAN_REL_TABLES, std::move(child), id, paramsString},
relTableCollectionPerNodeTable{std::move(relTableCollectionPerNodeTable)} {}
ScanMultiRelTable(std::unique_ptr<ScanRelTalePosInfo> posInfo,
node_table_id_scanner_map_t scannerPerNodeTable, std::unique_ptr<PhysicalOperator> child,
uint32_t id, const std::string& paramsString)
: ScanRelTable{std::move(posInfo), PhysicalOperatorType::GENERIC_SCAN_REL_TABLES,
std::move(child), id, paramsString},
scannerPerNodeTable{std::move(scannerPerNodeTable)} {}

void initLocalStateInternal(ResultSet* resultSet, ExecutionContext* context) override;
void initLocalStateInternal(ResultSet* resultSet, ExecutionContext* context) final;

bool getNextTuplesInternal(ExecutionContext* context) override;
bool getNextTuplesInternal(ExecutionContext* context) final;

std::unique_ptr<PhysicalOperator> clone() override {
std::unordered_map<common::table_id_t, std::unique_ptr<RelTableDataCollection>>
clonedCollections;
for (auto& [tableID, propertyCollection] : relTableCollectionPerNodeTable) {
clonedCollections.insert({tableID, propertyCollection->clone()});
}
return make_unique<GenericScanRelTables>(inNodeIDVectorPos, outputVectorsPos,
std::move(clonedCollections), children[0]->clone(), id, paramsString);
}
std::unique_ptr<PhysicalOperator> clone() final;

private:
bool scanCurrentRelTableDataCollection();
void initCurrentRelTableDataCollection(const common::nodeID_t& nodeID);
void resetState();
void initCurrentScanner(const common::nodeID_t& nodeID);

private:
std::unordered_map<common::table_id_t, std::unique_ptr<RelTableDataCollection>>
relTableCollectionPerNodeTable;
RelTableDataCollection* currentRelTableDataCollection = nullptr;
node_table_id_scanner_map_t scannerPerNodeTable;
RelTableCollectionScanner* currentScanner = nullptr;
};

} // namespace processor
Expand Down
58 changes: 47 additions & 11 deletions src/include/processor/operator/scan/scan_rel_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,59 @@
namespace kuzu {
namespace processor {

struct ScanRelTalePosInfo {
DataPos inNodeVectorPos;
DataPos outNodeVectorPos;
std::vector<DataPos> outVectorsPos;

ScanRelTalePosInfo(const DataPos& inNodeVectorPos, const DataPos& outNodeVectorPos,
std::vector<DataPos> outVectorsPos)
: inNodeVectorPos{inNodeVectorPos}, outNodeVectorPos{outNodeVectorPos},
outVectorsPos{std::move(outVectorsPos)} {}
ScanRelTalePosInfo(const ScanRelTalePosInfo& other)
: inNodeVectorPos{other.inNodeVectorPos}, outNodeVectorPos{other.outNodeVectorPos},
outVectorsPos{other.outVectorsPos} {}

inline std::unique_ptr<ScanRelTalePosInfo> copy() const {
return std::make_unique<ScanRelTalePosInfo>(*this);
}
};

struct RelTableScanInfo {
storage::RelTableDataType relTableDataType;
storage::DirectedRelTableData* tableData;
storage::RelStatistics* relStats;
std::vector<common::property_id_t> propertyIds;

RelTableScanInfo(storage::RelTableDataType relTableDataType,
storage::DirectedRelTableData* tableData, storage::RelStatistics* relStats,
std::vector<common::property_id_t> propertyIds)
: relTableDataType{relTableDataType}, tableData{tableData}, relStats{relStats},
propertyIds{std::move(propertyIds)} {}
RelTableScanInfo(const RelTableScanInfo& other)
: relTableDataType{other.relTableDataType}, tableData{other.tableData},
relStats{other.relStats}, propertyIds{other.propertyIds} {}

inline std::unique_ptr<RelTableScanInfo> copy() const {
return std::make_unique<RelTableScanInfo>(*this);
}
};

class ScanRelTable : public PhysicalOperator {
protected:
ScanRelTable(const DataPos& inNodeIDVectorPos, std::vector<DataPos> outputVectorsPos,
PhysicalOperatorType operatorType, std::unique_ptr<PhysicalOperator> child, uint32_t id,
const std::string& paramsString)
: PhysicalOperator{operatorType, std::move(child), id, paramsString},
inNodeIDVectorPos{inNodeIDVectorPos}, outputVectorsPos{std::move(outputVectorsPos)} {}
ScanRelTable(std::unique_ptr<ScanRelTalePosInfo> posInfo, PhysicalOperatorType operatorType,
std::unique_ptr<PhysicalOperator> child, uint32_t id, const std::string& paramsString)
: PhysicalOperator{operatorType, std::move(child), id, paramsString}, posInfo{std::move(
posInfo)} {}

void initLocalStateInternal(ResultSet* resultSet, ExecutionContext* context) override;

protected:
// vector positions
DataPos inNodeIDVectorPos;
std::vector<DataPos> outputVectorsPos;
// vectors
common::ValueVector* inNodeIDVector;
std::vector<common::ValueVector*> outputVectors;
std::unique_ptr<ScanRelTalePosInfo> posInfo;

common::ValueVector* inNodeVector;
common::ValueVector* outNodeVector;
std::vector<common::ValueVector*> outVectors;
};

} // namespace processor
Expand Down
27 changes: 12 additions & 15 deletions src/include/processor/operator/scan/scan_rel_table_columns.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,24 @@ namespace processor {

class ScanRelTableColumns : public ScanRelTable, public SelVectorOverWriter {
public:
ScanRelTableColumns(storage::DirectedRelTableData* tableData, storage::RelStatistics* relStats,
std::vector<uint32_t> propertyIds, const DataPos& inNodeIDVectorPos,
std::vector<DataPos> outputVectorsPos, std::unique_ptr<PhysicalOperator> child, uint32_t id,
const std::string& paramsString)
: ScanRelTable{inNodeIDVectorPos, std::move(outputVectorsPos),
PhysicalOperatorType::SCAN_REL_TABLE_COLUMNS, std::move(child), id, paramsString},
tableData{tableData} {
scanState = std::make_unique<storage::RelTableScanState>(
relStats, std::move(propertyIds), storage::RelTableDataType::COLUMNS);
}
ScanRelTableColumns(std::unique_ptr<RelTableScanInfo> scanInfo,
std::unique_ptr<ScanRelTalePosInfo> posInfo, std::unique_ptr<PhysicalOperator> child,
uint32_t id, const std::string& paramsString)
: ScanRelTable{std::move(posInfo), PhysicalOperatorType::SCAN_REL_TABLE_COLUMNS,
std::move(child), id, paramsString},
scanInfo{std::move(scanInfo)} {}

void initLocalStateInternal(ResultSet* resultSet, ExecutionContext* context) final;

bool getNextTuplesInternal(ExecutionContext* context) override;
bool getNextTuplesInternal(ExecutionContext* context) final;

inline std::unique_ptr<PhysicalOperator> clone() override {
return std::make_unique<ScanRelTableColumns>(tableData, scanState->relStats,
scanState->propertyIds, inNodeIDVectorPos, outputVectorsPos, children[0]->clone(), id,
paramsString);
return std::make_unique<ScanRelTableColumns>(
scanInfo->copy(), posInfo->copy(), children[0]->clone(), id, paramsString);
}

private:
storage::DirectedRelTableData* tableData;
std::unique_ptr<RelTableScanInfo> scanInfo;
std::unique_ptr<storage::RelTableScanState> scanState;
};

Expand Down
27 changes: 12 additions & 15 deletions src/include/processor/operator/scan/scan_rel_table_lists.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,24 @@ namespace processor {

class ScanRelTableLists : public ScanRelTable {
public:
ScanRelTableLists(storage::DirectedRelTableData* tableData, storage::RelStatistics* relStats,
std::vector<uint32_t> propertyIds, const DataPos& inNodeIDVectorPos,
std::vector<DataPos> outputVectorsPos, std::unique_ptr<PhysicalOperator> child, uint32_t id,
const std::string& paramsString)
: ScanRelTable{inNodeIDVectorPos, std::move(outputVectorsPos),
PhysicalOperatorType::SCAN_REL_TABLE_LISTS, std::move(child), id, paramsString},
tableData{tableData} {
scanState = std::make_unique<storage::RelTableScanState>(
relStats, std::move(propertyIds), storage::RelTableDataType::LISTS);
}
ScanRelTableLists(std::unique_ptr<RelTableScanInfo> scanInfo,
std::unique_ptr<ScanRelTalePosInfo> posInfo, std::unique_ptr<PhysicalOperator> child,
uint32_t id, const std::string& paramsString)
: ScanRelTable{std::move(posInfo), PhysicalOperatorType::SCAN_REL_TABLE_LISTS,
std::move(child), id, paramsString},
scanInfo{std::move(scanInfo)} {}

void initLocalStateInternal(ResultSet* resultSet, ExecutionContext* context) final;

bool getNextTuplesInternal(ExecutionContext* context) override;
bool getNextTuplesInternal(ExecutionContext* context) final;

inline std::unique_ptr<PhysicalOperator> clone() override {
return make_unique<ScanRelTableLists>(tableData, scanState->relStats,
scanState->propertyIds, inNodeIDVectorPos, outputVectorsPos, children[0]->clone(), id,
paramsString);
return make_unique<ScanRelTableLists>(
scanInfo->copy(), posInfo->copy(), children[0]->clone(), id, paramsString);
}

private:
storage::DirectedRelTableData* tableData;
std::unique_ptr<RelTableScanInfo> scanInfo;
std::unique_ptr<storage::RelTableScanState> scanState;
};

Expand Down
Loading
Loading