From 00951771e6b9e7f3c9c92a7ea5d9d38a32bf57d1 Mon Sep 17 00:00:00 2001 From: jimingquan Date: Mon, 16 Jan 2023 14:24:57 +0800 Subject: [PATCH] address comment --- src/graph/executor/Executor.cpp | 65 +++++++++++++++ src/graph/executor/Executor.h | 79 ++++--------------- .../executor/query/PatternApplyExecutor.cpp | 16 ++-- .../executor/query/PatternApplyExecutor.h | 14 ++-- .../executor/query/RollUpApplyExecutor.cpp | 17 ++-- .../executor/query/RollUpApplyExecutor.h | 29 +++---- src/parser/MatchPath.cpp | 3 +- 7 files changed, 115 insertions(+), 108 deletions(-) diff --git a/src/graph/executor/Executor.cpp b/src/graph/executor/Executor.cpp index 313d8cd7f59..645566674cf 100644 --- a/src/graph/executor/Executor.cpp +++ b/src/graph/executor/Executor.cpp @@ -765,3 +765,68 @@ size_t Executor::getBatchSize(size_t totalSize) const { } // namespace graph } // namespace nebula + +namespace std { +std::size_t hash::operator()( + const nebula::graph::ListWrapper &wrapper) const noexcept { + size_t seed = 0; + for (const auto &pair : wrapper.pairs()) { + if (pair.second) { + seed ^= std::hash()(pair.first); + } else { + const auto &values = pair.first.getList().values; + for (const auto &v : values) { + seed ^= std::hash()(v); + } + } + } + DLOG(ERROR) << "hashValue : " << wrapper.toString() << " values : " << seed; + return seed; +} + +bool equal_to::operator()( + const nebula::graph::ListWrapper &lhs, const nebula::graph::ListWrapper &rhs) const noexcept { + const auto &pairs = lhs.pairs(); + size_t size = pairs.size(); + const auto &otherPairs = rhs.pairs(); + if (size != otherPairs.size()) { + return false; + } + for (size_t i = 0; i < size; ++i) { + auto type = pairs[i].second; + if (type != otherPairs[i].second) { + return false; + } + if (type) { + if (pairs[i].first != otherPairs[i].first) { + return false; + } else { + continue; + } + } + const auto &values = pairs[i].first.getList().values; + const auto &otherValues = otherPairs[i].first.getList().values; + size_t listSize = values.size(); + if (listSize != otherValues.size()) { + return false; + } + if (values.front() == otherValues.front()) { + for (size_t j = 1; j < listSize; ++j) { + if (values[j] != otherValues[j]) { + return false; + } + } + } else if (values.front() == otherValues.back()) { + for (size_t j = 1; j < listSize; ++j) { + if (values[j] != otherValues[listSize - 1 - j]) { + return false; + } + } + } else { + return false; + } + } + return true; +} + +} // namespace std diff --git a/src/graph/executor/Executor.h b/src/graph/executor/Executor.h index 8c2c3aceddd..a8e151f0c81 100644 --- a/src/graph/executor/Executor.h +++ b/src/graph/executor/Executor.h @@ -45,70 +45,6 @@ class ListWrapper { std::vector> pairs_; }; -struct WrapperHash { - std::size_t operator()(const ListWrapper &wrapper) const { - size_t seed = 0; - for (const auto &pair : wrapper.pairs()) { - if (pair.second) { - seed ^= std::hash()(pair.first); - } else { - const auto &values = pair.first.getList().values; - for (const auto &v : values) { - seed ^= std::hash()(v); - } - } - } - DLOG(ERROR) << "hashValue : " << wrapper.toString() << " values : " << seed; - return seed; - } -}; - -struct WrapperEqual { - bool operator()(const ListWrapper &lhs, const ListWrapper &rhs) const { - const auto &pairs = lhs.pairs(); - size_t size = pairs.size(); - const auto &otherPairs = rhs.pairs(); - if (size != otherPairs.size()) { - return false; - } - for (size_t i = 0; i < size; ++i) { - auto type = pairs[i].second; - if (type != otherPairs[i].second) { - return false; - } - if (type) { - if (pairs[i].first != otherPairs[i].first) { - return false; - } else { - continue; - } - } - const auto &values = pairs[i].first.getList().values; - const auto &otherValues = otherPairs[i].first.getList().values; - size_t listSize = values.size(); - if (listSize != otherValues.size()) { - return false; - } - if (values.front() == otherValues.front()) { - for (size_t j = 1; j < listSize; ++j) { - if (values[j] != otherValues[j]) { - return false; - } - } - } else if (values.front() == otherValues.back()) { - for (size_t j = 1; j < listSize; ++j) { - if (values[j] != otherValues[listSize - 1 - j]) { - return false; - } - } - } else { - return false; - } - } - return true; - } -}; - class Executor : private boost::noncopyable, private cpp::NonMovable { public: // Create executor according to plan node @@ -271,4 +207,19 @@ auto Executor::runMultiJobs(ScatterFunc &&scatter, GatherFunc &&gather, Iterator } // namespace graph } // namespace nebula +namespace std { +// Inject a customized hash function +template <> +struct hash { + std::size_t operator()(const nebula::graph::ListWrapper &wrapper) const noexcept; +}; + +template <> +struct equal_to { + bool operator()(const nebula::graph::ListWrapper &lhs, + const nebula::graph::ListWrapper &rhs) const noexcept; +}; + +} // namespace std + #endif // GRAPH_EXECUTOR_EXECUTOR_H_ diff --git a/src/graph/executor/query/PatternApplyExecutor.cpp b/src/graph/executor/query/PatternApplyExecutor.cpp index a5aa30c14d6..e2ba26e27cd 100644 --- a/src/graph/executor/query/PatternApplyExecutor.cpp +++ b/src/graph/executor/query/PatternApplyExecutor.cpp @@ -38,10 +38,9 @@ Status PatternApplyExecutor::checkBiInputDataSets() { return Status::OK(); } -void PatternApplyExecutor::collectValidKeys( - const std::vector>& pairs, - Iterator* iter, - std::unordered_set& validKeys) const { +void PatternApplyExecutor::collectValidKeys(const std::vector>& pairs, + Iterator* iter, + std::unordered_set& validKeys) const { QueryExpressionContext ctx(ectx_); for (; iter->valid(); iter->next()) { ListWrapper listWrapper; @@ -93,10 +92,9 @@ DataSet PatternApplyExecutor::applySingleKey(Expression* appliedKey, return ds; } -DataSet PatternApplyExecutor::applyMultiKey( - std::vector> pairs, - Iterator* appliedIter, - const std::unordered_set& validKeys) { +DataSet PatternApplyExecutor::applyMultiKey(std::vector> pairs, + Iterator* appliedIter, + const std::unordered_set& validKeys) { DataSet ds; ds.rows.reserve(appliedIter->size()); QueryExpressionContext ctx(ectx_); @@ -141,7 +139,7 @@ folly::Future PatternApplyExecutor::patternApply() { return applyColsCopy; }; - std::unordered_set validKeys; + std::unordered_set validKeys; collectValidKeys(cloneExpr(keyCols), rhsIter_.get(), validKeys); result = applyMultiKey(cloneExpr(keyCols), lhsIter_.get(), validKeys); } diff --git a/src/graph/executor/query/PatternApplyExecutor.h b/src/graph/executor/query/PatternApplyExecutor.h index 11f6f61df4e..372d52a0dbb 100644 --- a/src/graph/executor/query/PatternApplyExecutor.h +++ b/src/graph/executor/query/PatternApplyExecutor.h @@ -20,10 +20,9 @@ class PatternApplyExecutor : public Executor { protected: Status checkBiInputDataSets(); - void collectValidKeys( - const std::vector>& keyCols, - Iterator* iter, - std::unordered_set& validKeys) const; + void collectValidKeys(const std::vector>& keyCols, + Iterator* iter, + std::unordered_set& validKeys) const; void collectValidKey(Expression* keyCol, Iterator* iter, @@ -35,10 +34,9 @@ class PatternApplyExecutor : public Executor { Iterator* appliedIter, const std::unordered_set& validKey); - DataSet applyMultiKey( - std::vector> appliedKeys, - Iterator* appliedIter, - const std::unordered_set& validKeys); + DataSet applyMultiKey(std::vector> appliedKeys, + Iterator* appliedIter, + const std::unordered_set& validKeys); folly::Future patternApply(); std::unique_ptr lhsIter_; diff --git a/src/graph/executor/query/RollUpApplyExecutor.cpp b/src/graph/executor/query/RollUpApplyExecutor.cpp index ba41e089d51..3839e0147e5 100644 --- a/src/graph/executor/query/RollUpApplyExecutor.cpp +++ b/src/graph/executor/query/RollUpApplyExecutor.cpp @@ -41,7 +41,7 @@ void RollUpApplyExecutor::buildHashTable( const std::vector>& compareCols, const InputPropertyExpression* collectCol, Iterator* iter, - std::unordered_map& hashTable) const { + std::unordered_map& hashTable) const { QueryExpressionContext ctx(ectx_); for (; iter->valid(); iter->next()) { @@ -61,7 +61,7 @@ void RollUpApplyExecutor::buildSingleKeyHashTable( const std::pair& compareCol, const InputPropertyExpression* collectCol, Iterator* iter, - std::unordered_map& hashTable) const { + std::unordered_map& hashTable) const { QueryExpressionContext ctx(ectx_); for (; iter->valid(); iter->next()) { auto& val = compareCol.first->eval(ctx(iter)); @@ -97,7 +97,7 @@ DataSet RollUpApplyExecutor::probeZeroKey(Iterator* probeIter, const List& hashT DataSet RollUpApplyExecutor::probeSingleKey( const std::pair& probeKey, Iterator* probeIter, - const std::unordered_map& hashTable) { + const std::unordered_map& hashTable) { DataSet ds; ds.rows.reserve(probeIter->size()); QueryExpressionContext ctx(ectx_); @@ -117,10 +117,9 @@ DataSet RollUpApplyExecutor::probeSingleKey( return ds; } -DataSet RollUpApplyExecutor::probe( - std::vector> probeKeys, - Iterator* probeIter, - const std::unordered_map& hashTable) { +DataSet RollUpApplyExecutor::probe(std::vector> probeKeys, + Iterator* probeIter, + const std::unordered_map& hashTable) { DataSet ds; ds.rows.reserve(probeIter->size()); QueryExpressionContext ctx(ectx_); @@ -158,7 +157,7 @@ folly::Future RollUpApplyExecutor::rollUpApply() { buildZeroKeyHashTable(rollUpApplyNode->collectCol(), rhsIter_.get(), hashTable); result = probeZeroKey(lhsIter_.get(), hashTable); } else if (compareCols.size() == 1) { - std::unordered_map hashTable; + std::unordered_map hashTable; std::pair pair(compareCols[0].first->clone(), compareCols[0].second); buildSingleKeyHashTable(pair, rollUpApplyNode->collectCol(), rhsIter_.get(), hashTable); std::pair newPair(compareCols[0].first->clone(), compareCols[0].second); @@ -174,7 +173,7 @@ folly::Future RollUpApplyExecutor::rollUpApply() { return collectColsCopy; }; - std::unordered_map hashTable; + std::unordered_map hashTable; buildHashTable( cloneExpr(compareCols), rollUpApplyNode->collectCol(), rhsIter_.get(), hashTable); diff --git a/src/graph/executor/query/RollUpApplyExecutor.h b/src/graph/executor/query/RollUpApplyExecutor.h index 51c8770b188..3770b2c75e3 100644 --- a/src/graph/executor/query/RollUpApplyExecutor.h +++ b/src/graph/executor/query/RollUpApplyExecutor.h @@ -20,17 +20,15 @@ class RollUpApplyExecutor : public Executor { protected: Status checkBiInputDataSets(); - void buildHashTable( - const std::vector>& compareCols, - const InputPropertyExpression* collectCol, - Iterator* iter, - std::unordered_map& hashTable) const; - - void buildSingleKeyHashTable( - const std::pair& compareCol, - const InputPropertyExpression* collectCol, - Iterator* iter, - std::unordered_map& hashTable) const; + void buildHashTable(const std::vector>& compareCols, + const InputPropertyExpression* collectCol, + Iterator* iter, + std::unordered_map& hashTable) const; + + void buildSingleKeyHashTable(const std::pair& compareCol, + const InputPropertyExpression* collectCol, + Iterator* iter, + std::unordered_map& hashTable) const; void buildZeroKeyHashTable(const InputPropertyExpression* collectCol, Iterator* iter, @@ -38,14 +36,13 @@ class RollUpApplyExecutor : public Executor { DataSet probeZeroKey(Iterator* probeIter, const List& hashTable); - DataSet probeSingleKey( - const std::pair& probeKey, - Iterator* probeIter, - const std::unordered_map& hashTable); + DataSet probeSingleKey(const std::pair& probeKey, + Iterator* probeIter, + const std::unordered_map& hashTable); DataSet probe(std::vector> probeKeys, Iterator* probeIter, - const std::unordered_map& hashTable); + const std::unordered_map& hashTable); folly::Future rollUpApply(); diff --git a/src/parser/MatchPath.cpp b/src/parser/MatchPath.cpp index 9731f12067c..ab65fa20b5c 100644 --- a/src/parser/MatchPath.cpp +++ b/src/parser/MatchPath.cpp @@ -92,8 +92,7 @@ std::string MatchPath::toString() const { buf.reserve(256); if (alias_ != nullptr) { - buf += *alias_; - buf += " = "; + return *alias_; } buf += node(0)->toString();