Skip to content

Commit

Permalink
refactor deduceProps
Browse files Browse the repository at this point in the history
  • Loading branch information
nevermore3 committed Dec 9, 2021
1 parent 824f419 commit cf57ea2
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 40 deletions.
16 changes: 8 additions & 8 deletions src/graph/validator/FetchEdgesValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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<EdgeType> edgeTypes{edgeType_};
NG_RETURN_IF_ERROR(deduceProps(colExpr, exprProps, nullptr, &edgeTypes));
}

if (exprProps.hasInputVarProperty()) {
Expand Down
10 changes: 6 additions & 4 deletions src/graph/validator/FetchVerticesValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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_) {
Expand Down
1 change: 1 addition & 0 deletions src/graph/validator/FetchVerticesValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class FetchVerticesValidator final : public Validator {

private:
std::map<TagID, std::shared_ptr<const meta::SchemaProviderIf>> tagsSchema_;
std::vector<TagID> tagIds_;

std::unique_ptr<FetchVerticesContext> fetchCtx_;
};
Expand Down
2 changes: 1 addition & 1 deletion src/graph/validator/FindPathValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
13 changes: 7 additions & 6 deletions src/graph/validator/GoValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -140,23 +140,24 @@ Status GoValidator::validateYield(YieldClause* yield) {
}

auto vertexExpr = ExpressionUtils::findAny(col->expr(), {Expression::Kind::kVertex});
if (static_cast<const VertexExpression*>(vertexExpr)->name() == "VERTEX") {
if (vertexExpr != nullptr &&
static_cast<const VertexExpression*>(vertexExpr)->name() == "VERTEX") {
return Status::SemanticError("`%s' is not support in go sentence.", col->toString().c_str());
}

col->setExpr(ExpressionUtils::rewriteLabelAttr2EdgeProp(col->expr()));
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;
Expand Down
11 changes: 8 additions & 3 deletions src/graph/validator/LookupValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}

Expand Down
1 change: 1 addition & 0 deletions src/graph/validator/LookupValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class LookupValidator final : public Validator {
std::vector<nebula::plugin::HttpClient> tsClients_;
ExpressionProps exprProps_;
std::vector<std::string> idxReturnCols_;
std::vector<int32_t> schemaIds_;
};

} // namespace graph
Expand Down
8 changes: 6 additions & 2 deletions src/graph/validator/Validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,12 @@ StatusOr<Value::Type> 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<TagID>* tagIds,
std::vector<EdgeType>* edgeTypes) {
DeducePropsVisitor visitor(
qctx_, space_.id, &exprProps, &userDefinedVarNameList_, tagIds, edgeTypes);
const_cast<Expression*>(expr)->accept(&visitor);
return std::move(visitor).status();
}
Expand Down
5 changes: 4 additions & 1 deletion src/graph/validator/Validator.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ class Validator {

StatusOr<Value::Type> deduceExprType(const Expression* expr) const;

Status deduceProps(const Expression* expr, ExpressionProps& exprProps);
Status deduceProps(const Expression* expr,
ExpressionProps& exprProps,
std::vector<TagID>* tagIds = nullptr,
std::vector<EdgeType>* edgeTypes = nullptr);

static StatusOr<size_t> checkPropNonexistOrDuplicate(const ColsDef& cols,
folly::StringPiece prop,
Expand Down
2 changes: 1 addition & 1 deletion src/graph/validator/YieldValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
58 changes: 45 additions & 13 deletions src/graph/visitor/DeducePropsVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,15 @@ void ExpressionProps::unionProps(ExpressionProps exprProps) {
DeducePropsVisitor::DeducePropsVisitor(QueryContext *qctx,
GraphSpaceID space,
ExpressionProps *exprProps,
std::set<std::string> *userDefinedVarNameList)
std::set<std::string> *userDefinedVarNameList,
std::vector<TagID> *tagIds,
std::vector<EdgeType> *edgeTypes)
: qctx_(qctx),
space_(space),
exprProps_(exprProps),
userDefinedVarNameList_(userDefinedVarNameList) {
userDefinedVarNameList_(userDefinedVarNameList),
tagIds_(tagIds),
edgeTypes_(edgeTypes) {
DCHECK(qctx != nullptr);
DCHECK(exprProps != nullptr);
DCHECK(userDefinedVarNameList != nullptr);
Expand Down Expand Up @@ -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<TagID> 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());
Expand Down
6 changes: 5 additions & 1 deletion src/graph/visitor/DeducePropsVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ class DeducePropsVisitor : public ExprVisitorImpl {
DeducePropsVisitor(QueryContext* qctx,
GraphSpaceID space,
ExpressionProps* exprProps,
std::set<std::string>* userDefinedVarNameList);
std::set<std::string>* userDefinedVarNameList,
std::vector<TagID>* tagIds,
std::vector<EdgeType>* edgeTypes);

bool ok() const override { return status_.ok(); }

Expand Down Expand Up @@ -106,6 +108,8 @@ class DeducePropsVisitor : public ExprVisitorImpl {
GraphSpaceID space_;
ExpressionProps* exprProps_{nullptr};
std::set<std::string>* userDefinedVarNameList_{nullptr};
std::vector<TagID>* tagIds_{nullptr};
std::vector<EdgeType>* edgeTypes_{nullptr};
Status status_;
};

Expand Down

0 comments on commit cf57ea2

Please sign in to comment.