Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
nevermore3 committed Jan 16, 2023
1 parent 47dad67 commit 0095177
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 108 deletions.
65 changes: 65 additions & 0 deletions src/graph/executor/Executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,68 @@ size_t Executor::getBatchSize(size_t totalSize) const {

} // namespace graph
} // namespace nebula

namespace std {
std::size_t hash<nebula::graph::ListWrapper>::operator()(
const nebula::graph::ListWrapper &wrapper) const noexcept {
size_t seed = 0;
for (const auto &pair : wrapper.pairs()) {
if (pair.second) {
seed ^= std::hash<nebula::Value>()(pair.first);
} else {
const auto &values = pair.first.getList().values;
for (const auto &v : values) {
seed ^= std::hash<nebula::Value>()(v);
}
}
}
DLOG(ERROR) << "hashValue : " << wrapper.toString() << " values : " << seed;
return seed;
}

bool equal_to<nebula::graph::ListWrapper>::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
79 changes: 15 additions & 64 deletions src/graph/executor/Executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,70 +45,6 @@ class ListWrapper {
std::vector<std::pair<Value, bool>> 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<nebula::Value>()(pair.first);
} else {
const auto &values = pair.first.getList().values;
for (const auto &v : values) {
seed ^= std::hash<nebula::Value>()(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
Expand Down Expand Up @@ -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<nebula::graph::ListWrapper> {
std::size_t operator()(const nebula::graph::ListWrapper &wrapper) const noexcept;
};

template <>
struct equal_to<nebula::graph::ListWrapper> {
bool operator()(const nebula::graph::ListWrapper &lhs,
const nebula::graph::ListWrapper &rhs) const noexcept;
};

} // namespace std

#endif // GRAPH_EXECUTOR_EXECUTOR_H_
16 changes: 7 additions & 9 deletions src/graph/executor/query/PatternApplyExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ Status PatternApplyExecutor::checkBiInputDataSets() {
return Status::OK();
}

void PatternApplyExecutor::collectValidKeys(
const std::vector<std::pair<Expression*, bool>>& pairs,
Iterator* iter,
std::unordered_set<ListWrapper, WrapperHash, WrapperEqual>& validKeys) const {
void PatternApplyExecutor::collectValidKeys(const std::vector<std::pair<Expression*, bool>>& pairs,
Iterator* iter,
std::unordered_set<ListWrapper>& validKeys) const {
QueryExpressionContext ctx(ectx_);
for (; iter->valid(); iter->next()) {
ListWrapper listWrapper;
Expand Down Expand Up @@ -93,10 +92,9 @@ DataSet PatternApplyExecutor::applySingleKey(Expression* appliedKey,
return ds;
}

DataSet PatternApplyExecutor::applyMultiKey(
std::vector<std::pair<Expression*, bool>> pairs,
Iterator* appliedIter,
const std::unordered_set<ListWrapper, WrapperHash, WrapperEqual>& validKeys) {
DataSet PatternApplyExecutor::applyMultiKey(std::vector<std::pair<Expression*, bool>> pairs,
Iterator* appliedIter,
const std::unordered_set<ListWrapper>& validKeys) {
DataSet ds;
ds.rows.reserve(appliedIter->size());
QueryExpressionContext ctx(ectx_);
Expand Down Expand Up @@ -141,7 +139,7 @@ folly::Future<Status> PatternApplyExecutor::patternApply() {
return applyColsCopy;
};

std::unordered_set<ListWrapper, WrapperHash, WrapperEqual> validKeys;
std::unordered_set<ListWrapper> validKeys;
collectValidKeys(cloneExpr(keyCols), rhsIter_.get(), validKeys);
result = applyMultiKey(cloneExpr(keyCols), lhsIter_.get(), validKeys);
}
Expand Down
14 changes: 6 additions & 8 deletions src/graph/executor/query/PatternApplyExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ class PatternApplyExecutor : public Executor {
protected:
Status checkBiInputDataSets();

void collectValidKeys(
const std::vector<std::pair<Expression*, bool>>& keyCols,
Iterator* iter,
std::unordered_set<ListWrapper, WrapperHash, WrapperEqual>& validKeys) const;
void collectValidKeys(const std::vector<std::pair<Expression*, bool>>& keyCols,
Iterator* iter,
std::unordered_set<ListWrapper>& validKeys) const;

void collectValidKey(Expression* keyCol,
Iterator* iter,
Expand All @@ -35,10 +34,9 @@ class PatternApplyExecutor : public Executor {
Iterator* appliedIter,
const std::unordered_set<Value>& validKey);

DataSet applyMultiKey(
std::vector<std::pair<Expression*, bool>> appliedKeys,
Iterator* appliedIter,
const std::unordered_set<ListWrapper, WrapperHash, WrapperEqual>& validKeys);
DataSet applyMultiKey(std::vector<std::pair<Expression*, bool>> appliedKeys,
Iterator* appliedIter,
const std::unordered_set<ListWrapper>& validKeys);

folly::Future<Status> patternApply();
std::unique_ptr<Iterator> lhsIter_;
Expand Down
17 changes: 8 additions & 9 deletions src/graph/executor/query/RollUpApplyExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void RollUpApplyExecutor::buildHashTable(
const std::vector<std::pair<Expression*, bool>>& compareCols,
const InputPropertyExpression* collectCol,
Iterator* iter,
std::unordered_map<ListWrapper, List, WrapperHash, WrapperEqual>& hashTable) const {
std::unordered_map<ListWrapper, List>& hashTable) const {
QueryExpressionContext ctx(ectx_);

for (; iter->valid(); iter->next()) {
Expand All @@ -61,7 +61,7 @@ void RollUpApplyExecutor::buildSingleKeyHashTable(
const std::pair<Expression*, bool>& compareCol,
const InputPropertyExpression* collectCol,
Iterator* iter,
std::unordered_map<ListWrapper, List, WrapperHash, WrapperEqual>& hashTable) const {
std::unordered_map<ListWrapper, List>& hashTable) const {
QueryExpressionContext ctx(ectx_);
for (; iter->valid(); iter->next()) {
auto& val = compareCol.first->eval(ctx(iter));
Expand Down Expand Up @@ -97,7 +97,7 @@ DataSet RollUpApplyExecutor::probeZeroKey(Iterator* probeIter, const List& hashT
DataSet RollUpApplyExecutor::probeSingleKey(
const std::pair<Expression*, bool>& probeKey,
Iterator* probeIter,
const std::unordered_map<ListWrapper, List, WrapperHash, WrapperEqual>& hashTable) {
const std::unordered_map<ListWrapper, List>& hashTable) {
DataSet ds;
ds.rows.reserve(probeIter->size());
QueryExpressionContext ctx(ectx_);
Expand All @@ -117,10 +117,9 @@ DataSet RollUpApplyExecutor::probeSingleKey(
return ds;
}

DataSet RollUpApplyExecutor::probe(
std::vector<std::pair<Expression*, bool>> probeKeys,
Iterator* probeIter,
const std::unordered_map<ListWrapper, List, WrapperHash, WrapperEqual>& hashTable) {
DataSet RollUpApplyExecutor::probe(std::vector<std::pair<Expression*, bool>> probeKeys,
Iterator* probeIter,
const std::unordered_map<ListWrapper, List>& hashTable) {
DataSet ds;
ds.rows.reserve(probeIter->size());
QueryExpressionContext ctx(ectx_);
Expand Down Expand Up @@ -158,7 +157,7 @@ folly::Future<Status> RollUpApplyExecutor::rollUpApply() {
buildZeroKeyHashTable(rollUpApplyNode->collectCol(), rhsIter_.get(), hashTable);
result = probeZeroKey(lhsIter_.get(), hashTable);
} else if (compareCols.size() == 1) {
std::unordered_map<ListWrapper, List, WrapperHash, WrapperEqual> hashTable;
std::unordered_map<ListWrapper, List> hashTable;
std::pair<Expression*, bool> pair(compareCols[0].first->clone(), compareCols[0].second);
buildSingleKeyHashTable(pair, rollUpApplyNode->collectCol(), rhsIter_.get(), hashTable);
std::pair<Expression*, bool> newPair(compareCols[0].first->clone(), compareCols[0].second);
Expand All @@ -174,7 +173,7 @@ folly::Future<Status> RollUpApplyExecutor::rollUpApply() {
return collectColsCopy;
};

std::unordered_map<ListWrapper, List, WrapperHash, WrapperEqual> hashTable;
std::unordered_map<ListWrapper, List> hashTable;
buildHashTable(
cloneExpr(compareCols), rollUpApplyNode->collectCol(), rhsIter_.get(), hashTable);

Expand Down
29 changes: 13 additions & 16 deletions src/graph/executor/query/RollUpApplyExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,29 @@ class RollUpApplyExecutor : public Executor {
protected:
Status checkBiInputDataSets();

void buildHashTable(
const std::vector<std::pair<Expression*, bool>>& compareCols,
const InputPropertyExpression* collectCol,
Iterator* iter,
std::unordered_map<ListWrapper, List, WrapperHash, WrapperEqual>& hashTable) const;

void buildSingleKeyHashTable(
const std::pair<Expression*, bool>& compareCol,
const InputPropertyExpression* collectCol,
Iterator* iter,
std::unordered_map<ListWrapper, List, WrapperHash, WrapperEqual>& hashTable) const;
void buildHashTable(const std::vector<std::pair<Expression*, bool>>& compareCols,
const InputPropertyExpression* collectCol,
Iterator* iter,
std::unordered_map<ListWrapper, List>& hashTable) const;

void buildSingleKeyHashTable(const std::pair<Expression*, bool>& compareCol,
const InputPropertyExpression* collectCol,
Iterator* iter,
std::unordered_map<ListWrapper, List>& hashTable) const;

void buildZeroKeyHashTable(const InputPropertyExpression* collectCol,
Iterator* iter,
List& hashTable) const;

DataSet probeZeroKey(Iterator* probeIter, const List& hashTable);

DataSet probeSingleKey(
const std::pair<Expression*, bool>& probeKey,
Iterator* probeIter,
const std::unordered_map<ListWrapper, List, WrapperHash, WrapperEqual>& hashTable);
DataSet probeSingleKey(const std::pair<Expression*, bool>& probeKey,
Iterator* probeIter,
const std::unordered_map<ListWrapper, List>& hashTable);

DataSet probe(std::vector<std::pair<Expression*, bool>> probeKeys,
Iterator* probeIter,
const std::unordered_map<ListWrapper, List, WrapperHash, WrapperEqual>& hashTable);
const std::unordered_map<ListWrapper, List>& hashTable);

folly::Future<Status> rollUpApply();

Expand Down
3 changes: 1 addition & 2 deletions src/parser/MatchPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ std::string MatchPath::toString() const {
buf.reserve(256);

if (alias_ != nullptr) {
buf += *alias_;
buf += " = ";
return *alias_;
}

buf += node(0)->toString();
Expand Down

0 comments on commit 0095177

Please sign in to comment.