Skip to content

Commit

Permalink
support properties in where clause
Browse files Browse the repository at this point in the history
  • Loading branch information
nevermore3 committed Dec 9, 2021
1 parent bc1aac6 commit 824f419
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 78 deletions.
84 changes: 18 additions & 66 deletions src/graph/validator/GoValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
namespace nebula {
namespace graph {

static const char* COLNAME_EDGE = "EDGE";
static const char* SRC_VERTEX = "$^";
static const char* DST_VERTEX = "$$";
// static const char* SRC_VERTEX = "$^";
// static const char* DST_VERTEX = "$$";

Status GoValidator::validateImpl() {
auto* goSentence = static_cast<GoSentence*>(sentence_);
Expand Down Expand Up @@ -140,17 +139,9 @@ Status GoValidator::validateYield(YieldClause* yield) {
return Status::SemanticError("`%s' is not support in go sentence.", col->toString().c_str());
}

const auto& vertexExprs = ExpressionUtils::collectAll(col->expr(), {Expression::Kind::kVertex});
for (const auto* expr : vertexExprs) {
const auto& colName = static_cast<const VertexExpression*>(expr)->name();
if (colName == SRC_VERTEX) {
NG_RETURN_IF_ERROR(extractVertexProp(exprProps, true));
} else if (colName == DST_VERTEX) {
NG_RETURN_IF_ERROR(extractVertexProp(exprProps, false));
} else {
return Status::SemanticError("`%s' is not support in go sentence.",
col->toString().c_str());
}
auto vertexExpr = ExpressionUtils::findAny(col->expr(), {Expression::Kind::kVertex});
if (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()));
Expand Down Expand Up @@ -180,25 +171,6 @@ Status GoValidator::validateYield(YieldClause* yield) {
return Status::OK();
}

Status GoValidator::extractVertexProp(ExpressionProps& exprProps, bool isSrc) {
const auto tagStatus = qctx_->schemaMng()->getAllLatestVerTagSchema(space_.id);
NG_RETURN_IF_ERROR(tagStatus);
for (const auto& tag : tagStatus.value()) {
auto tagID = tag.first;
const auto& tagSchema = tag.second;
if (isSrc) {
for (size_t i = 0; i < tagSchema->getNumFields(); ++i) {
exprProps.insertSrcTagProp(tagID, tagSchema->getFieldName(i));
}
} else {
for (size_t i = 0; i < tagSchema->getNumFields(); ++i) {
exprProps.insertDstTagProp(tagID, tagSchema->getFieldName(i));
}
}
}
return Status::OK();
}

Status GoValidator::extractEdgeProp(ExpressionProps& exprProps) {
const auto& edgeTypes = goCtx_->over.edgeTypes;
for (const auto& edgeType : edgeTypes) {
Expand All @@ -214,9 +186,14 @@ Status GoValidator::extractEdgeProp(ExpressionProps& exprProps) {
return Status::OK();
}

void GoValidator::extractPropExprs(const Expression* expr) {
ExtractPropExprVisitor visitor(
vctx_, goCtx_->srcEdgePropsExpr, goCtx_->dstPropsExpr, inputPropCols_, propExprColMap_);
void GoValidator::extractPropExprs(const Expression* expr,
std::unordered_set<std::string>& uniqueExpr) {
ExtractPropExprVisitor visitor(vctx_,
goCtx_->srcEdgePropsExpr,
goCtx_->dstPropsExpr,
inputPropCols_,
propExprColMap_,
uniqueExpr);
const_cast<Expression*>(expr)->accept(&visitor);
}

Expand Down Expand Up @@ -258,42 +235,17 @@ Status GoValidator::buildColumns() {
inputPropCols_ = pool->add(new YieldColumns());
}

std::unordered_set<std::string> uniqueEdgeVertexExpr;
auto filter = goCtx_->filter;
if (filter != nullptr) {
extractPropExprs(filter);
auto newFilter = filter->clone();
goCtx_->filter = rewrite2VarProp(newFilter);
extractPropExprs(filter, uniqueEdgeVertexExpr);
goCtx_->filter = rewrite2VarProp(filter);
}

std::unordered_set<std::string> existExpr;
auto* newYieldExpr = pool->add(new YieldColumns());
for (auto* col : goCtx_->yieldExpr->columns()) {
const auto& vertexExprs = ExpressionUtils::collectAll(col->expr(), {Expression::Kind::kVertex});
auto existEdge = ExpressionUtils::hasAny(col->expr(), {Expression::Kind::kEdge});
if (!existEdge && vertexExprs.empty()) {
extractPropExprs(col->expr());
newYieldExpr->addColumn(new YieldColumn(rewrite2VarProp(col->expr()), col->alias()));
} else {
if (existEdge) {
if (existExpr.emplace(COLNAME_EDGE).second) {
goCtx_->srcEdgePropsExpr->addColumn(
new YieldColumn(EdgeExpression::make(pool), COLNAME_EDGE));
}
}
for (const auto* expr : vertexExprs) {
const auto& colName = static_cast<const VertexExpression*>(expr)->name();
if (existExpr.emplace(colName).second) {
if (colName == SRC_VERTEX) {
goCtx_->srcEdgePropsExpr->addColumn(
new YieldColumn(VertexExpression::make(pool), SRC_VERTEX));
} else {
goCtx_->dstPropsExpr->addColumn(
new YieldColumn(VertexExpression::make(pool), DST_VERTEX));
}
}
}
newYieldExpr->addColumn(col->clone().release());
}
extractPropExprs(col->expr(), uniqueEdgeVertexExpr);
newYieldExpr->addColumn(new YieldColumn(rewrite2VarProp(col->expr()), col->alias()));
}
goCtx_->yieldExpr = newYieldExpr;
return Status::OK();
Expand Down
4 changes: 1 addition & 3 deletions src/graph/validator/GoValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ class GoValidator final : public Validator {

Status buildColumns();

void extractPropExprs(const Expression* expr);
void extractPropExprs(const Expression* expr, std::unordered_set<std::string>& uniqueCols);

Expression* rewrite2VarProp(const Expression* expr);

Status extractVertexProp(ExpressionProps& exprProps, bool isSrc);

Status extractEdgeProp(ExpressionProps& exprProps);

private:
Expand Down
24 changes: 21 additions & 3 deletions src/graph/visitor/DeducePropsVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,31 @@ void DeducePropsVisitor::visit(VersionedVariableExpression *expr) { reportError(

void DeducePropsVisitor::visit(LabelExpression *expr) { reportError(expr); }

void DeducePropsVisitor::visit(AttributeExpression *expr) { UNUSED(expr); }

void DeducePropsVisitor::visit(LabelAttributeExpression *expr) { reportError(expr); }

void DeducePropsVisitor::visit(ConstantExpression *expr) { UNUSED(expr); }

void DeducePropsVisitor::visit(VertexExpression *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;
}
for (const auto &tag : tagStatus.value()) {
auto tagID = tag.first;
const auto &tagSchema = tag.second;
if (colName == "$^") {
for (size_t i = 0; i < tagSchema->getNumFields(); ++i) {
exprProps_->insertSrcTagProp(tagID, tagSchema->getFieldName(i));
}
} else if (colName == "$$") {
for (size_t i = 0; i < tagSchema->getNumFields(); ++i) {
exprProps_->insertDstTagProp(tagID, tagSchema->getFieldName(i));
}
}
}
}

void DeducePropsVisitor::visit(EdgeExpression *expr) { UNUSED(expr); }

Expand Down
1 change: 0 additions & 1 deletion src/graph/visitor/DeducePropsVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class DeducePropsVisitor : public ExprVisitorImpl {
void visit(VariableExpression* expr) override;
void visit(VersionedVariableExpression* expr) override;
void visit(LabelExpression* expr) override;
void visit(AttributeExpression* expr) override;
void visit(LabelAttributeExpression* expr) override;
void visit(ConstantExpression* expr) override;
void visit(VertexExpression* expr) override;
Expand Down
23 changes: 19 additions & 4 deletions src/graph/visitor/ExtractPropExprVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,33 @@ ExtractPropExprVisitor::ExtractPropExprVisitor(
YieldColumns* srcAndEdgePropCols,
YieldColumns* dstPropCols,
YieldColumns* inputPropCols,
std::unordered_map<std::string, YieldColumn*>& propExprColMap)
std::unordered_map<std::string, YieldColumn*>& propExprColMap,
std::unordered_set<std::string>& uniqueEdgeVertexCol)
: vctx_(DCHECK_NOTNULL(vctx)),
srcAndEdgePropCols_(srcAndEdgePropCols),
dstPropCols_(dstPropCols),
inputPropCols_(inputPropCols),
propExprColMap_(propExprColMap) {}
propExprColMap_(propExprColMap),
uniqueEdgeVertexCol_(uniqueEdgeVertexCol) {}

void ExtractPropExprVisitor::visit(ConstantExpression* expr) { UNUSED(expr); }

void ExtractPropExprVisitor::visit(VertexExpression* expr) { UNUSED(expr); }
void ExtractPropExprVisitor::visit(VertexExpression* expr) {
const auto& colName = expr->name();
if (uniqueEdgeVertexCol_.emplace(colName).second) {
if (colName == "$^") {
srcAndEdgePropCols_->addColumn(new YieldColumn(expr->clone(), colName));
} else {
dstPropCols_->addColumn(new YieldColumn(expr->clone(), colName));
}
}
}

void ExtractPropExprVisitor::visit(EdgeExpression* expr) { UNUSED(expr); }
void ExtractPropExprVisitor::visit(EdgeExpression* expr) {
if (uniqueEdgeVertexCol_.emplace(expr->toString()).second) {
srcAndEdgePropCols_->addColumn(new YieldColumn(expr->clone(), expr->toString()));
}
}

void ExtractPropExprVisitor::visit(VariableExpression* expr) { UNUSED(expr); }

Expand Down
4 changes: 3 additions & 1 deletion src/graph/visitor/ExtractPropExprVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class ExtractPropExprVisitor final : public ExprVisitorImpl {
YieldColumns *srcAndEdgePropCols,
YieldColumns *dstPropCols,
YieldColumns *inputPropCols,
std::unordered_map<std::string, YieldColumn *> &propExprColMap);
std::unordered_map<std::string, YieldColumn *> &propExprColMap,
std::unordered_set<std::string> &uniqueEdgeVertexCol);

~ExtractPropExprVisitor() = default;

Expand Down Expand Up @@ -68,6 +69,7 @@ class ExtractPropExprVisitor final : public ExprVisitorImpl {
YieldColumns *dstPropCols_{nullptr};
YieldColumns *inputPropCols_{nullptr};
std::unordered_map<std::string, YieldColumn *> &propExprColMap_;
std::unordered_set<std::string> &uniqueEdgeVertexCol_;

Status status_;
};
Expand Down
1 change: 1 addition & 0 deletions tests/tck/features/go/GO.feature
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) 2021 vesoft inc. All rights reserved.
#
# This source code is licensed under Apache 2.0 License.
@jmq
Feature: Go Sentence

Background:
Expand Down
1 change: 1 addition & 0 deletions tests/tck/features/go/GoYieldVertexEdge.feature
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) 2021 vesoft inc. All rights reserved.
#
# This source code is licensed under Apache 2.0 License.
@jmq
Feature: Go Yield Vertex And Edge Sentence

Background:
Expand Down
1 change: 1 addition & 0 deletions tests/tck/features/go/GroupbyLimit.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@jmq
Feature: Groupby & limit Sentence

Background: Prepare space
Expand Down

0 comments on commit 824f419

Please sign in to comment.