From 539bb8ee0eaa3fe800fb10020bb7dc2027f9da39 Mon Sep 17 00:00:00 2001 From: Shylock Hg <33566796+Shylock-Hg@users.noreply.github.com> Date: Mon, 12 Oct 2020 19:27:30 +0800 Subject: [PATCH] Move the ExpressionProps out of validator folder (#295) Co-authored-by: dutor <440396+dutor@users.noreply.github.com> Co-authored-by: Yee <2520865+yixinglu@users.noreply.github.com> --- src/validator/CMakeLists.txt | 1 - src/validator/ExpressionProps.cpp | 106 ----------------------------- src/validator/ExpressionProps.h | 85 ----------------------- src/validator/Validator.h | 2 +- src/visitor/DeducePropsVisitor.cpp | 97 +++++++++++++++++++++++++- src/visitor/DeducePropsVisitor.h | 62 ++++++++++++++++- 6 files changed, 158 insertions(+), 195 deletions(-) delete mode 100644 src/validator/ExpressionProps.cpp delete mode 100644 src/validator/ExpressionProps.h diff --git a/src/validator/CMakeLists.txt b/src/validator/CMakeLists.txt index 7ea8e730..ffe1da36 100644 --- a/src/validator/CMakeLists.txt +++ b/src/validator/CMakeLists.txt @@ -5,7 +5,6 @@ nebula_add_library( validator_obj OBJECT - ExpressionProps.cpp Validator.cpp AssignmentValidator.cpp GoValidator.cpp diff --git a/src/validator/ExpressionProps.cpp b/src/validator/ExpressionProps.cpp deleted file mode 100644 index b54a8ddc..00000000 --- a/src/validator/ExpressionProps.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/* Copyright (c) 2020 vesoft inc. All rights reserved. - * - * This source code is licensed under Apache 2.0 License, - * attached with Common Clause Condition 1.0, found in the LICENSES directory. - */ - -#include "validator/ExpressionProps.h" - -namespace nebula { -namespace graph { - -void ExpressionProps::insertVarProp(const std::string& outputVar, folly::StringPiece prop) { - auto& props = varProps_[outputVar]; - props.emplace(prop); -} - -void ExpressionProps::insertInputProp(folly::StringPiece prop) { - inputProps_.emplace(prop); -} - -void ExpressionProps::insertSrcTagProp(TagID tagId, folly::StringPiece prop) { - auto& props = srcTagProps_[tagId]; - props.emplace(prop); -} - -void ExpressionProps::insertDstTagProp(TagID tagId, folly::StringPiece prop) { - auto& props = dstTagProps_[tagId]; - props.emplace(prop); -} - -void ExpressionProps::insertEdgeProp(EdgeType edgeType, folly::StringPiece prop) { - auto& props = edgeProps_[edgeType]; - props.emplace(prop); -} - -void ExpressionProps::insertTagNameIds(const std::string &name, TagID tagId) { - tagNameIds_.emplace(name, tagId); -} - -void ExpressionProps::insertTagProp(TagID tagId, folly::StringPiece prop) { - auto& props = tagProps_[tagId]; - props.emplace(prop); -} - -bool ExpressionProps::isSubsetOfInput(const std::set<folly::StringPiece>& props) { - for (auto& prop : props) { - if (inputProps_.find(prop) == inputProps_.end()) { - return false; - } - } - return true; -} - -bool ExpressionProps::isSubsetOfVar(const VarPropMap& props) { - for (auto& iter : props) { - if (varProps_.find(iter.first) == varProps_.end()) { - return false; - } - for (auto& prop : iter.second) { - if (varProps_[iter.first].find(prop) == varProps_[iter.first].end()) { - return false; - } - } - } - return true; -} - -void ExpressionProps::unionProps(ExpressionProps exprProps) { - if (!exprProps.inputProps().empty()) { - inputProps_.insert(std::make_move_iterator(exprProps.inputProps().begin()), - std::make_move_iterator(exprProps.inputProps().end())); - } - if (!exprProps.srcTagProps().empty()) { - for (auto& iter : exprProps.srcTagProps()) { - srcTagProps_[iter.first].insert(std::make_move_iterator(iter.second.begin()), - std::make_move_iterator(iter.second.end())); - } - } - if (!exprProps.dstTagProps().empty()) { - for (auto& iter : exprProps.dstTagProps()) { - dstTagProps_[iter.first].insert(std::make_move_iterator(iter.second.begin()), - std::make_move_iterator(iter.second.end())); - } - } - if (!exprProps.tagProps().empty()) { - for (auto& iter : exprProps.tagProps()) { - tagProps_[iter.first].insert(std::make_move_iterator(iter.second.begin()), - std::make_move_iterator(iter.second.end())); - } - } - if (!exprProps.varProps().empty()) { - for (auto& iter : exprProps.varProps()) { - varProps_[iter.first].insert(std::make_move_iterator(iter.second.begin()), - std::make_move_iterator(iter.second.end())); - } - } - if (!exprProps.edgeProps().empty()) { - for (auto& iter : exprProps.edgeProps()) { - edgeProps_[iter.first].insert(std::make_move_iterator(iter.second.begin()), - std::make_move_iterator(iter.second.end())); - } - } -} - -} // namespace graph -} // namespace nebula diff --git a/src/validator/ExpressionProps.h b/src/validator/ExpressionProps.h deleted file mode 100644 index dd1bb63d..00000000 --- a/src/validator/ExpressionProps.h +++ /dev/null @@ -1,85 +0,0 @@ -/* Copyright (c) 2020 vesoft inc. All rights reserved. - * - * This source code is licensed under Apache 2.0 License, - * attached with Common Clause Condition 1.0, found in the LICENSES directory. - */ - -#ifndef VALIDATOR_EXPRESSIONPROPS_H_ -#define VALIDATOR_EXPRESSIONPROPS_H_ - -#include <set> -#include <string> -#include <unordered_map> - -#include <folly/String.h> - -#include "common/thrift/ThriftTypes.h" - -namespace nebula { -namespace graph { - -class ExpressionProps final { -public: - using TagNameIds = std::unordered_map<std::string, TagID>; - using TagIDPropsMap = std::unordered_map<TagID, std::set<folly::StringPiece>>; - using EdgePropMap = std::unordered_map<EdgeType, std::set<folly::StringPiece>>; - using VarPropMap = std::unordered_map<std::string, std::set<folly::StringPiece>>; - - ExpressionProps() = default; - ~ExpressionProps() = default; - - const std::set<folly::StringPiece>& inputProps() const { - return inputProps_; - } - const TagIDPropsMap& srcTagProps() const { - return srcTagProps_; - } - const TagIDPropsMap& dstTagProps() const { - return dstTagProps_; - } - const TagIDPropsMap& tagProps() const { - return tagProps_; - } - const TagNameIds& tagNameIds() const { - return tagNameIds_; - } - const EdgePropMap& edgeProps() const { - return edgeProps_; - } - const VarPropMap& varProps() const { - return varProps_; - } - - bool hasInputVarProperty() const { - return !inputProps_.empty() || !varProps_.empty(); - } - - bool hasSrcDstTagProperty() const { - return !srcTagProps_.empty() || !dstTagProps_.empty(); - } - - void insertInputProp(folly::StringPiece prop); - void insertVarProp(const std::string& outputVar, folly::StringPiece prop); - void insertSrcTagProp(TagID tagId, folly::StringPiece prop); - void insertDstTagProp(TagID tagId, folly::StringPiece prop); - void insertEdgeProp(EdgeType edgeType, folly::StringPiece prop); - void insertTagNameIds(const std::string &name, TagID tagId); - void insertTagProp(TagID tagId, folly::StringPiece prop); - bool isSubsetOfInput(const std::set<folly::StringPiece>& props); - bool isSubsetOfVar(const VarPropMap& props); - void unionProps(ExpressionProps exprProps); - -private: - std::set<folly::StringPiece> inputProps_; - VarPropMap varProps_; - TagIDPropsMap srcTagProps_; - TagIDPropsMap dstTagProps_; - EdgePropMap edgeProps_; - TagIDPropsMap tagProps_; - TagNameIds tagNameIds_; -}; - -} // namespace graph -} // namespace nebula - -#endif // VALIDATOR_EXPRESSIONPROPS_H_ diff --git a/src/validator/Validator.h b/src/validator/Validator.h index 2c507bf0..c67bf908 100644 --- a/src/validator/Validator.h +++ b/src/validator/Validator.h @@ -12,7 +12,7 @@ #include "parser/Sentence.h" #include "context/ValidateContext.h" #include "context/QueryContext.h" -#include "validator/ExpressionProps.h" +#include "visitor/DeducePropsVisitor.h" namespace nebula { diff --git a/src/visitor/DeducePropsVisitor.cpp b/src/visitor/DeducePropsVisitor.cpp index f007537c..0f547b80 100644 --- a/src/visitor/DeducePropsVisitor.cpp +++ b/src/visitor/DeducePropsVisitor.cpp @@ -9,11 +9,106 @@ #include <sstream> #include "context/QueryContext.h" -#include "validator/ExpressionProps.h" namespace nebula { namespace graph { +// Expression properties +void ExpressionProps::insertVarProp(const std::string& outputVar, folly::StringPiece prop) { + auto& props = varProps_[outputVar]; + props.emplace(prop); +} + +void ExpressionProps::insertInputProp(folly::StringPiece prop) { + inputProps_.emplace(prop); +} + +void ExpressionProps::insertSrcTagProp(TagID tagId, folly::StringPiece prop) { + auto& props = srcTagProps_[tagId]; + props.emplace(prop); +} + +void ExpressionProps::insertDstTagProp(TagID tagId, folly::StringPiece prop) { + auto& props = dstTagProps_[tagId]; + props.emplace(prop); +} + +void ExpressionProps::insertEdgeProp(EdgeType edgeType, folly::StringPiece prop) { + auto& props = edgeProps_[edgeType]; + props.emplace(prop); +} + +void ExpressionProps::insertTagNameIds(const std::string &name, TagID tagId) { + tagNameIds_.emplace(name, tagId); +} + +void ExpressionProps::insertTagProp(TagID tagId, folly::StringPiece prop) { + auto& props = tagProps_[tagId]; + props.emplace(prop); +} + +bool ExpressionProps::isSubsetOfInput(const std::set<folly::StringPiece>& props) { + for (auto& prop : props) { + if (inputProps_.find(prop) == inputProps_.end()) { + return false; + } + } + return true; +} + +bool ExpressionProps::isSubsetOfVar(const VarPropMap& props) { + for (auto& iter : props) { + if (varProps_.find(iter.first) == varProps_.end()) { + return false; + } + for (auto& prop : iter.second) { + if (varProps_[iter.first].find(prop) == varProps_[iter.first].end()) { + return false; + } + } + } + return true; +} + +void ExpressionProps::unionProps(ExpressionProps exprProps) { + if (!exprProps.inputProps().empty()) { + inputProps_.insert(std::make_move_iterator(exprProps.inputProps().begin()), + std::make_move_iterator(exprProps.inputProps().end())); + } + if (!exprProps.srcTagProps().empty()) { + for (auto& iter : exprProps.srcTagProps()) { + srcTagProps_[iter.first].insert(std::make_move_iterator(iter.second.begin()), + std::make_move_iterator(iter.second.end())); + } + } + if (!exprProps.dstTagProps().empty()) { + for (auto& iter : exprProps.dstTagProps()) { + dstTagProps_[iter.first].insert(std::make_move_iterator(iter.second.begin()), + std::make_move_iterator(iter.second.end())); + } + } + if (!exprProps.tagProps().empty()) { + for (auto& iter : exprProps.tagProps()) { + tagProps_[iter.first].insert(std::make_move_iterator(iter.second.begin()), + std::make_move_iterator(iter.second.end())); + } + } + if (!exprProps.varProps().empty()) { + for (auto& iter : exprProps.varProps()) { + varProps_[iter.first].insert(std::make_move_iterator(iter.second.begin()), + std::make_move_iterator(iter.second.end())); + } + } + if (!exprProps.edgeProps().empty()) { + for (auto& iter : exprProps.edgeProps()) { + edgeProps_[iter.first].insert(std::make_move_iterator(iter.second.begin()), + std::make_move_iterator(iter.second.end())); + } + } +} + + +// visitor DeducePropsVisitor::DeducePropsVisitor(QueryContext *qctx, GraphSpaceID space, ExpressionProps *exprProps) diff --git a/src/visitor/DeducePropsVisitor.h b/src/visitor/DeducePropsVisitor.h index 646e98fb..7b29bd6f 100644 --- a/src/visitor/DeducePropsVisitor.h +++ b/src/visitor/DeducePropsVisitor.h @@ -18,7 +18,67 @@ class Expression; namespace graph { class QueryContext; -class ExpressionProps; + +class ExpressionProps final { +public: + using TagNameIds = std::unordered_map<std::string, TagID>; + using TagIDPropsMap = std::unordered_map<TagID, std::set<folly::StringPiece>>; + using EdgePropMap = std::unordered_map<EdgeType, std::set<folly::StringPiece>>; + using VarPropMap = std::unordered_map<std::string, std::set<folly::StringPiece>>; + + ExpressionProps() = default; + ~ExpressionProps() = default; + + const std::set<folly::StringPiece>& inputProps() const { + return inputProps_; + } + const TagIDPropsMap& srcTagProps() const { + return srcTagProps_; + } + const TagIDPropsMap& dstTagProps() const { + return dstTagProps_; + } + const TagIDPropsMap& tagProps() const { + return tagProps_; + } + const TagNameIds& tagNameIds() const { + return tagNameIds_; + } + const EdgePropMap& edgeProps() const { + return edgeProps_; + } + const VarPropMap& varProps() const { + return varProps_; + } + + bool hasInputVarProperty() const { + return !inputProps_.empty() || !varProps_.empty(); + } + + bool hasSrcDstTagProperty() const { + return !srcTagProps_.empty() || !dstTagProps_.empty(); + } + + void insertInputProp(folly::StringPiece prop); + void insertVarProp(const std::string& outputVar, folly::StringPiece prop); + void insertSrcTagProp(TagID tagId, folly::StringPiece prop); + void insertDstTagProp(TagID tagId, folly::StringPiece prop); + void insertEdgeProp(EdgeType edgeType, folly::StringPiece prop); + void insertTagNameIds(const std::string &name, TagID tagId); + void insertTagProp(TagID tagId, folly::StringPiece prop); + bool isSubsetOfInput(const std::set<folly::StringPiece>& props); + bool isSubsetOfVar(const VarPropMap& props); + void unionProps(ExpressionProps exprProps); + +private: + std::set<folly::StringPiece> inputProps_; + VarPropMap varProps_; + TagIDPropsMap srcTagProps_; + TagIDPropsMap dstTagProps_; + EdgePropMap edgeProps_; + TagIDPropsMap tagProps_; + TagNameIds tagNameIds_; +}; class DeducePropsVisitor : public ExprVisitorImpl { public: -- GitLab