From cf57ea2a0d3b6ec06f581753ef390bc38dff8788 Mon Sep 17 00:00:00 2001 From: jimingquan Date: Thu, 9 Dec 2021 15:47:47 +0800 Subject: [PATCH] refactor deduceProps --- src/graph/validator/FetchEdgesValidator.cpp | 16 ++--- .../validator/FetchVerticesValidator.cpp | 10 ++-- src/graph/validator/FetchVerticesValidator.h | 1 + src/graph/validator/FindPathValidator.cpp | 2 +- src/graph/validator/GoValidator.cpp | 13 +++-- src/graph/validator/LookupValidator.cpp | 11 +++- src/graph/validator/LookupValidator.h | 1 + src/graph/validator/Validator.cpp | 8 ++- src/graph/validator/Validator.h | 5 +- src/graph/validator/YieldValidator.cpp | 2 +- src/graph/visitor/DeducePropsVisitor.cpp | 58 ++++++++++++++----- src/graph/visitor/DeducePropsVisitor.h | 6 +- 12 files changed, 93 insertions(+), 40 deletions(-) diff --git a/src/graph/validator/FetchEdgesValidator.cpp b/src/graph/validator/FetchEdgesValidator.cpp index d0f5c0a2b4e..8b03d949e7c 100644 --- a/src/graph/validator/FetchEdgesValidator.cpp +++ b/src/graph/validator/FetchEdgesValidator.cpp @@ -158,12 +158,12 @@ Status FetchEdgesValidator::validateYield(const YieldClause *yield) { exprProps.insertEdgeProp(edgeType_, nebula::kDst); exprProps.insertEdgeProp(edgeType_, nebula::kRank); - for (const auto &col : yield->columns()) { - if (ExpressionUtils::hasAny(col->expr(), {Expression::Kind::kEdge})) { - extractEdgeProp(exprProps); - break; - } - } + // for (const auto &col : yield->columns()) { + // if (ExpressionUtils::hasAny(col->expr(), {Expression::Kind::kEdge})) { + // extractEdgeProp(exprProps); + // break; + // } + // } auto size = yield->columns().size(); outputs_.reserve(size); @@ -182,8 +182,8 @@ Status FetchEdgesValidator::validateYield(const YieldClause *yield) { NG_RETURN_IF_ERROR(typeStatus); outputs_.emplace_back(col->name(), typeStatus.value()); newCols->addColumn(col->clone().release()); - - NG_RETURN_IF_ERROR(deduceProps(colExpr, exprProps)); + std::vector edgeTypes{edgeType_}; + NG_RETURN_IF_ERROR(deduceProps(colExpr, exprProps, nullptr, &edgeTypes)); } if (exprProps.hasInputVarProperty()) { diff --git a/src/graph/validator/FetchVerticesValidator.cpp b/src/graph/validator/FetchVerticesValidator.cpp index dd902d79002..a80dffdbeea 100644 --- a/src/graph/validator/FetchVerticesValidator.cpp +++ b/src/graph/validator/FetchVerticesValidator.cpp @@ -30,6 +30,7 @@ Status FetchVerticesValidator::validateTag(const NameLabelList *nameLabels) { NG_RETURN_IF_ERROR(tagStatus); for (const auto &tag : tagStatus.value()) { tagsSchema_.emplace(tag.first, tag.second); + tagIds_.emplace_back(tag.first); } } else { auto labels = nameLabels->labels(); @@ -43,6 +44,7 @@ Status FetchVerticesValidator::validateTag(const NameLabelList *nameLabels) { return Status::SemanticError("no schema found for `%s'", label->c_str()); } tagsSchema_.emplace(tagID, tagSchema); + tagIds_.emplace_back(tagID); } } return Status::OK(); @@ -76,12 +78,12 @@ Status FetchVerticesValidator::validateYield(YieldClause *yield) { col->setAlias(col->name()); col->setExpr(InputPropertyExpression::make(pool, nebula::kVid)); } - if (ExpressionUtils::hasAny(colExpr, {Expression::Kind::kVertex})) { - extractVertexProp(exprProps); - } + // if (ExpressionUtils::hasAny(colExpr, {Expression::Kind::kVertex})) { + // extractVertexProp(exprProps); + // } newCols->addColumn(col->clone().release()); - NG_RETURN_IF_ERROR(deduceProps(colExpr, exprProps)); + NG_RETURN_IF_ERROR(deduceProps(colExpr, exprProps, &tagIds_)); } if (exprProps.tagProps().empty()) { for (const auto &tagSchema : tagsSchema_) { diff --git a/src/graph/validator/FetchVerticesValidator.h b/src/graph/validator/FetchVerticesValidator.h index 88f87f6762a..cee2c498ad3 100644 --- a/src/graph/validator/FetchVerticesValidator.h +++ b/src/graph/validator/FetchVerticesValidator.h @@ -31,6 +31,7 @@ class FetchVerticesValidator final : public Validator { private: std::map> tagsSchema_; + std::vector tagIds_; std::unique_ptr fetchCtx_; }; diff --git a/src/graph/validator/FindPathValidator.cpp b/src/graph/validator/FindPathValidator.cpp index bfd39c137d6..0d230fc55a6 100644 --- a/src/graph/validator/FindPathValidator.cpp +++ b/src/graph/validator/FindPathValidator.cpp @@ -58,7 +58,7 @@ Status FindPathValidator::validateWhere(WhereClause* where) { return Status::SemanticError(ss.str()); } - NG_RETURN_IF_ERROR(deduceProps(filter, pathCtx_->exprProps)); + NG_RETURN_IF_ERROR(deduceProps(filter, pathCtx_->exprProps, {}, {})); pathCtx_->filter = filter; return Status::OK(); } diff --git a/src/graph/validator/GoValidator.cpp b/src/graph/validator/GoValidator.cpp index 200b4bfbc97..202542b6006 100644 --- a/src/graph/validator/GoValidator.cpp +++ b/src/graph/validator/GoValidator.cpp @@ -89,7 +89,7 @@ Status GoValidator::validateWhere(WhereClause* where) { return Status::SemanticError(ss.str()); } - NG_RETURN_IF_ERROR(deduceProps(filter, goCtx_->exprProps)); + NG_RETURN_IF_ERROR(deduceProps(filter, goCtx_->exprProps, nullptr, &goCtx_->over.edgeTypes)); goCtx_->filter = filter; return Status::OK(); } @@ -140,7 +140,8 @@ Status GoValidator::validateYield(YieldClause* yield) { } auto vertexExpr = ExpressionUtils::findAny(col->expr(), {Expression::Kind::kVertex}); - if (static_cast(vertexExpr)->name() == "VERTEX") { + if (vertexExpr != nullptr && + static_cast(vertexExpr)->name() == "VERTEX") { return Status::SemanticError("`%s' is not support in go sentence.", col->toString().c_str()); } @@ -148,15 +149,15 @@ Status GoValidator::validateYield(YieldClause* yield) { NG_RETURN_IF_ERROR(ValidateUtil::invalidLabelIdentifiers(col->expr())); auto* colExpr = col->expr(); - if (ExpressionUtils::hasAny(colExpr, {Expression::Kind::kEdge})) { - extractEdgeProp(exprProps); - } + // if (ExpressionUtils::hasAny(colExpr, {Expression::Kind::kEdge})) { + // extractEdgeProp(exprProps); + // } auto typeStatus = deduceExprType(colExpr); NG_RETURN_IF_ERROR(typeStatus); auto type = typeStatus.value(); outputs_.emplace_back(col->name(), type); - NG_RETURN_IF_ERROR(deduceProps(colExpr, exprProps)); + NG_RETURN_IF_ERROR(deduceProps(colExpr, exprProps, nullptr, &goCtx_->over.edgeTypes)); } const auto& over = goCtx_->over; diff --git a/src/graph/validator/LookupValidator.cpp b/src/graph/validator/LookupValidator.cpp index 6426b8975fc..7da955281b5 100644 --- a/src/graph/validator/LookupValidator.cpp +++ b/src/graph/validator/LookupValidator.cpp @@ -53,6 +53,7 @@ Status LookupValidator::validateFrom() { NG_RETURN_IF_ERROR(ret); lookupCtx_->isEdge = ret.value().first; lookupCtx_->schemaId = ret.value().second; + schemaIds_.emplace_back(ret.value().second); return Status::OK(); } @@ -124,7 +125,7 @@ Status LookupValidator::validateYieldEdge() { NG_RETURN_IF_ERROR(typeStatus); outputs_.emplace_back(col->name(), typeStatus.value()); yieldExpr->addColumn(col->clone().release()); - NG_RETURN_IF_ERROR(deduceProps(colExpr, exprProps_)); + NG_RETURN_IF_ERROR(deduceProps(colExpr, exprProps_, nullptr, &schemaIds_)); } return Status::OK(); } @@ -154,7 +155,7 @@ Status LookupValidator::validateYieldTag() { NG_RETURN_IF_ERROR(typeStatus); outputs_.emplace_back(col->name(), typeStatus.value()); yieldExpr->addColumn(col->clone().release()); - NG_RETURN_IF_ERROR(deduceProps(colExpr, exprProps_)); + NG_RETURN_IF_ERROR(deduceProps(colExpr, exprProps_, &schemaIds_)); } return Status::OK(); } @@ -211,7 +212,11 @@ Status LookupValidator::validateFilter() { // Make sure the type of the rewritted filter expr is right NG_RETURN_IF_ERROR(deduceExprType(lookupCtx_->filter)); } - NG_RETURN_IF_ERROR(deduceProps(lookupCtx_->filter, exprProps_)); + if (lookupCtx_->isEdge) { + NG_RETURN_IF_ERROR(deduceProps(lookupCtx_->filter, exprProps_, nullptr, &schemaIds_)); + } else { + NG_RETURN_IF_ERROR(deduceProps(lookupCtx_->filter, exprProps_, &schemaIds_)); + } return Status::OK(); } diff --git a/src/graph/validator/LookupValidator.h b/src/graph/validator/LookupValidator.h index 54713d46ef4..56236bdd92c 100644 --- a/src/graph/validator/LookupValidator.h +++ b/src/graph/validator/LookupValidator.h @@ -60,6 +60,7 @@ class LookupValidator final : public Validator { std::vector tsClients_; ExpressionProps exprProps_; std::vector idxReturnCols_; + std::vector schemaIds_; }; } // namespace graph diff --git a/src/graph/validator/Validator.cpp b/src/graph/validator/Validator.cpp index 687d4731492..3b1edb7f443 100644 --- a/src/graph/validator/Validator.cpp +++ b/src/graph/validator/Validator.cpp @@ -352,8 +352,12 @@ StatusOr Validator::deduceExprType(const Expression* expr) const { return visitor.type(); } -Status Validator::deduceProps(const Expression* expr, ExpressionProps& exprProps) { - DeducePropsVisitor visitor(qctx_, space_.id, &exprProps, &userDefinedVarNameList_); +Status Validator::deduceProps(const Expression* expr, + ExpressionProps& exprProps, + std::vector* tagIds, + std::vector* edgeTypes) { + DeducePropsVisitor visitor( + qctx_, space_.id, &exprProps, &userDefinedVarNameList_, tagIds, edgeTypes); const_cast(expr)->accept(&visitor); return std::move(visitor).status(); } diff --git a/src/graph/validator/Validator.h b/src/graph/validator/Validator.h index 0568d0eb054..686ec654b17 100644 --- a/src/graph/validator/Validator.h +++ b/src/graph/validator/Validator.h @@ -107,7 +107,10 @@ class Validator { StatusOr deduceExprType(const Expression* expr) const; - Status deduceProps(const Expression* expr, ExpressionProps& exprProps); + Status deduceProps(const Expression* expr, + ExpressionProps& exprProps, + std::vector* tagIds = nullptr, + std::vector* edgeTypes = nullptr); static StatusOr checkPropNonexistOrDuplicate(const ColsDef& cols, folly::StringPiece prop, diff --git a/src/graph/validator/YieldValidator.cpp b/src/graph/validator/YieldValidator.cpp index 901774e8697..e622eda743d 100644 --- a/src/graph/validator/YieldValidator.cpp +++ b/src/graph/validator/YieldValidator.cpp @@ -65,7 +65,7 @@ Status YieldValidator::makeOutputColumn(YieldColumn *column) { DCHECK(colExpr != nullptr); auto expr = colExpr->clone(); - NG_RETURN_IF_ERROR(deduceProps(expr, exprProps_)); + NG_RETURN_IF_ERROR(deduceProps(expr, exprProps_, {}, {})); auto status = deduceExprType(expr); NG_RETURN_IF_ERROR(status); diff --git a/src/graph/visitor/DeducePropsVisitor.cpp b/src/graph/visitor/DeducePropsVisitor.cpp index 8991932a95d..d7c494913d2 100644 --- a/src/graph/visitor/DeducePropsVisitor.cpp +++ b/src/graph/visitor/DeducePropsVisitor.cpp @@ -108,11 +108,15 @@ void ExpressionProps::unionProps(ExpressionProps exprProps) { DeducePropsVisitor::DeducePropsVisitor(QueryContext *qctx, GraphSpaceID space, ExpressionProps *exprProps, - std::set *userDefinedVarNameList) + std::set *userDefinedVarNameList, + std::vector *tagIds, + std::vector *edgeTypes) : qctx_(qctx), space_(space), exprProps_(exprProps), - userDefinedVarNameList_(userDefinedVarNameList) { + userDefinedVarNameList_(userDefinedVarNameList), + tagIds_(tagIds), + edgeTypes_(edgeTypes) { DCHECK(qctx != nullptr); DCHECK(exprProps != nullptr); DCHECK(userDefinedVarNameList != nullptr); @@ -177,31 +181,59 @@ void DeducePropsVisitor::visit(LabelAttributeExpression *expr) { reportError(exp void DeducePropsVisitor::visit(ConstantExpression *expr) { UNUSED(expr); } +void DeducePropsVisitor::visit(ColumnExpression *expr) { UNUSED(expr); } + void DeducePropsVisitor::visit(VertexExpression *expr) { - const auto &colName = expr->name(); - auto tagStatus = qctx_->schemaMng()->getAllLatestVerTagSchema(space_); - if (!tagStatus.ok()) { - status_ = std::move(tagStatus).status(); - return; + std::vector tagIds; + if (tagIds_ == nullptr) { + auto tagStatus = qctx_->schemaMng()->getAllLatestVerTagSchema(space_); + if (!tagStatus.ok()) { + status_ = std::move(tagStatus).status(); + return; + } + for (const auto &tag : tagStatus.value()) { + tagIds.emplace_back(tag.first); + } + tagIds_ = &tagIds; } - for (const auto &tag : tagStatus.value()) { - auto tagID = tag.first; - const auto &tagSchema = tag.second; + const auto &colName = expr->name(); + for (const auto &tagID : *tagIds_) { + const auto &tagSchema = qctx_->schemaMng()->getTagSchema(space_, tagID); if (colName == "$^") { + exprProps_->insertSrcTagProp(tagID, nebula::kTag); for (size_t i = 0; i < tagSchema->getNumFields(); ++i) { exprProps_->insertSrcTagProp(tagID, tagSchema->getFieldName(i)); } } else if (colName == "$$") { + exprProps_->insertDstTagProp(tagID, nebula::kTag); for (size_t i = 0; i < tagSchema->getNumFields(); ++i) { exprProps_->insertDstTagProp(tagID, tagSchema->getFieldName(i)); } + } else { + exprProps_->insertTagProp(tagID, nebula::kTag); + for (size_t i = 0; i < tagSchema->getNumFields(); ++i) { + exprProps_->insertTagProp(tagID, tagSchema->getFieldName(i)); + } } } } -void DeducePropsVisitor::visit(EdgeExpression *expr) { UNUSED(expr); } - -void DeducePropsVisitor::visit(ColumnExpression *expr) { UNUSED(expr); } +void DeducePropsVisitor::visit(EdgeExpression *expr) { + if (edgeTypes_ == nullptr) { + UNUSED(expr); + return; + } + for (const auto &edgeType : *edgeTypes_) { + const auto &edgeSchema = qctx_->schemaMng()->getEdgeSchema(space_, std::abs(edgeType)); + exprProps_->insertEdgeProp(edgeType, kType); + exprProps_->insertEdgeProp(edgeType, kSrc); + exprProps_->insertEdgeProp(edgeType, kDst); + exprProps_->insertEdgeProp(edgeType, kRank); + for (size_t i = 0; i < edgeSchema->getNumFields(); ++i) { + exprProps_->insertEdgeProp(edgeType, edgeSchema->getFieldName(i)); + } + } +} void DeducePropsVisitor::visitEdgePropExpr(PropertyExpression *expr) { auto status = qctx_->schemaMng()->toEdgeType(space_, expr->sym()); diff --git a/src/graph/visitor/DeducePropsVisitor.h b/src/graph/visitor/DeducePropsVisitor.h index 05cdb5e4a4d..ef8a603d1f0 100644 --- a/src/graph/visitor/DeducePropsVisitor.h +++ b/src/graph/visitor/DeducePropsVisitor.h @@ -71,7 +71,9 @@ class DeducePropsVisitor : public ExprVisitorImpl { DeducePropsVisitor(QueryContext* qctx, GraphSpaceID space, ExpressionProps* exprProps, - std::set* userDefinedVarNameList); + std::set* userDefinedVarNameList, + std::vector* tagIds, + std::vector* edgeTypes); bool ok() const override { return status_.ok(); } @@ -106,6 +108,8 @@ class DeducePropsVisitor : public ExprVisitorImpl { GraphSpaceID space_; ExpressionProps* exprProps_{nullptr}; std::set* userDefinedVarNameList_{nullptr}; + std::vector* tagIds_{nullptr}; + std::vector* edgeTypes_{nullptr}; Status status_; };