#pragma once #include #include "binder/expression/expression.h" #include "join_hash_table.h" #include "processor/operator/physical_operator.h" #include "processor/operator/sink.h" #include "processor/result/factorized_table.h" #include "processor/result/result_set.h" namespace lbug { namespace processor { struct HashJoinBuildPrintInfo final : OPPrintInfo { binder::expression_vector keys; binder::expression_vector payloads; HashJoinBuildPrintInfo(binder::expression_vector keys, binder::expression_vector payloads) : keys{std::move(keys)}, payloads(std::move(payloads)) {} std::string toString() const override; std::unique_ptr copy() const override { return std::unique_ptr(new HashJoinBuildPrintInfo(*this)); } private: HashJoinBuildPrintInfo(const HashJoinBuildPrintInfo& other) : OPPrintInfo{other}, keys{other.keys}, payloads{other.payloads} {} }; class HashJoinBuild; // This is a shared state between HashJoinBuild and HashJoinProbe operators. // Each clone of these two operators will share the same state. // Inside the state, we keep the materialized tuples in factorizedTable, which are merged by each // HashJoinBuild thread when they finished materializing thread-local tuples. Also, the state holds // a global htDirectory, which will be updated by the last thread in the hash join build side // task/pipeline, and probed by the HashJoinProbe operators. class HashJoinSharedState { public: explicit HashJoinSharedState(std::unique_ptr hashTable) : hashTable{std::move(hashTable)} {}; void mergeLocalHashTable(JoinHashTable& localHashTable); JoinHashTable* getHashTable() { return hashTable.get(); } // Re-arm the shared state for another execution of a cached physical plan: drop the rows // and hash slots accumulated by the previous execution. Without this, later executions // probe stale rows from earlier executions. void resetForReuse() { hashTable->resetForReuse(); } protected: std::mutex mtx; std::unique_ptr hashTable; }; struct HashJoinBuildInfo { std::vector keysPos; std::vector fStateTypes; std::vector payloadsPos; FactorizedTableSchema tableSchema; HashJoinBuildInfo(std::vector keysPos, std::vector fStateTypes, std::vector payloadsPos, FactorizedTableSchema tableSchema) : keysPos{std::move(keysPos)}, fStateTypes{std::move(fStateTypes)}, payloadsPos{std::move(payloadsPos)}, tableSchema{std::move(tableSchema)} {} EXPLICIT_COPY_DEFAULT_MOVE(HashJoinBuildInfo); common::idx_t getNumKeys() const { return keysPos.size(); } private: HashJoinBuildInfo(const HashJoinBuildInfo& other) : keysPos{other.keysPos}, fStateTypes{other.fStateTypes}, payloadsPos{other.payloadsPos}, tableSchema{other.tableSchema.copy()} {} }; class HashJoinBuild : public Sink { public: HashJoinBuild(PhysicalOperatorType operatorType, std::shared_ptr sharedState, HashJoinBuildInfo info, std::unique_ptr child, uint32_t id, std::unique_ptr printInfo) : Sink{operatorType, std::move(child), id, std::move(printInfo)}, sharedState{std::move(sharedState)}, info{std::move(info)} {} std::shared_ptr getSharedState() const { return sharedState; } void initGlobalStateInternal(ExecutionContext* context) override; void initLocalStateInternal(ResultSet* resultSet, ExecutionContext* context) override; void executeInternal(ExecutionContext* context) override; void finalizeInternal(ExecutionContext* context) override; std::unique_ptr copy() override { return make_unique(operatorType, sharedState, info.copy(), children[0]->copy(), id, printInfo->copy()); } protected: virtual uint64_t appendVectors() { return hashTable->appendVectors(keyVectors, payloadVectors, keyState); } private: void setKeyState(common::DataChunkState* state); protected: std::shared_ptr sharedState; HashJoinBuildInfo info; std::vector keyVectors; // State of unFlat key(s). If all keys are flat, it points to any flat key state. common::DataChunkState* keyState = nullptr; std::vector payloadVectors; std::unique_ptr hashTable; // local state }; } // namespace processor } // namespace lbug