Skip to content

Commit

Permalink
Merge branch 'master' into disable-yield-var
Browse files Browse the repository at this point in the history
  • Loading branch information
yixinglu committed Nov 24, 2021
2 parents f2055f9 + b7c6901 commit 0112069
Show file tree
Hide file tree
Showing 37 changed files with 1,996 additions and 3,272 deletions.
14 changes: 6 additions & 8 deletions src/graph/optimizer/rule/IndexScanRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ StatusOr<OptRule::TransformResult> IndexScanRule::transform(OptContext* ctx,
FilterItems items;
ScanKind kind;
NG_RETURN_IF_ERROR(analyzeExpression(filter, &items, &kind, isEdge(groupNode)));
NG_RETURN_IF_ERROR(createIndexQueryCtx(iqctx, kind, items, qctx, groupNode));
auto status = createIndexQueryCtx(iqctx, kind, items, qctx, groupNode);
if (!status.ok()) {
NG_RETURN_IF_ERROR(createIndexQueryCtx(iqctx, qctx, groupNode));
}
}

const auto* oldIN = groupNode->node();
Expand Down Expand Up @@ -479,20 +482,15 @@ std::vector<IndexItem> IndexScanRule::findValidIndex(graph::QueryContext* qctx,
std::vector<IndexItem> validIndexes;
// Find indexes for match all fields by where condition.
for (const auto& index : indexes) {
bool allColsHint = true;
const auto& fields = index->get_fields();
for (const auto& item : items.items) {
auto it = std::find_if(fields.begin(), fields.end(), [item](const auto& field) {
return field.get_name() == item.col_;
});
if (it == fields.end()) {
allColsHint = false;
break;
if (it != fields.end()) {
validIndexes.emplace_back(index);
}
}
if (allColsHint) {
validIndexes.emplace_back(index);
}
}
// If the first field of the index does not match any condition, the index is
// invalid. remove it from validIndexes.
Expand Down
19 changes: 5 additions & 14 deletions src/graph/validator/GetSubgraphValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,30 +94,21 @@ Status GetSubgraphValidator::validateBothInOutBound(BothInOutClause* out) {
}

Status GetSubgraphValidator::validateYield(YieldClause* yield) {
auto pool = qctx_->objPool();
if (yield == nullptr) {
// version 3.0: return Status::SemanticError("No Yield Clause");
auto* yieldColumns = new YieldColumns();
auto* vertex = new YieldColumn(LabelExpression::make(pool, "_vertices"));
yieldColumns->addColumn(vertex);
if (subgraphCtx_->steps.steps() != 0) {
auto* edge = new YieldColumn(LabelExpression::make(pool, "_edges"));
yieldColumns->addColumn(edge);
}
yield = pool->add(new YieldClause(yieldColumns));
return Status::SemanticError("Missing yield clause.");
}
auto size = yield->columns().size();
outputs_.reserve(size);
auto pool = qctx_->objPool();
YieldColumns* newCols = pool->add(new YieldColumns());

for (const auto& col : yield->columns()) {
std::string lowerStr = col->expr()->toString();
folly::toLowerAscii(lowerStr);
if (lowerStr == "vertices" || lowerStr == "_vertices") {
const std::string& colStr = col->expr()->toString();
if (colStr == "VERTICES") {
subgraphCtx_->getVertexProp = true;
auto* newCol = new YieldColumn(InputPropertyExpression::make(pool, "VERTICES"), col->name());
newCols->addColumn(newCol);
} else if (lowerStr == "edges" || lowerStr == "_edges") {
} else if (colStr == "EDGES") {
if (subgraphCtx_->steps.steps() == 0) {
return Status::SemanticError("Get Subgraph 0 STEPS only support YIELD vertices");
}
Expand Down
2 changes: 1 addition & 1 deletion src/graph/validator/GroupByValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Status GroupByValidator::validateYield(const YieldClause* yieldClause) {
needGenProject_ = true;
}
if (!aggs.empty()) {
auto* colRewrited = ExpressionUtils::rewriteAgg2VarProp(colExpr);
auto* colRewrited = ExpressionUtils::rewriteAgg2VarProp(colExpr->clone());
projCols_->addColumn(new YieldColumn(colRewrited, colOldName));
continue;
}
Expand Down
29 changes: 7 additions & 22 deletions src/graph/validator/LookupValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,36 +160,21 @@ Status LookupValidator::validateYieldTag() {
}

Status LookupValidator::validateYield() {
auto pool = qctx_->objPool();
auto* newCols = pool->add(new YieldColumns());
lookupCtx_->yieldExpr = newCols;
if (lookupCtx_->isEdge) {
idxReturnCols_.emplace_back(kSrc);
idxReturnCols_.emplace_back(kDst);
idxReturnCols_.emplace_back(kRank);
idxReturnCols_.emplace_back(kType);
outputs_.emplace_back(kSrcVID, vidType_);
outputs_.emplace_back(kDstVID, vidType_);
outputs_.emplace_back(kRanking, Value::Type::INT);
newCols->addColumn(new YieldColumn(ColumnExpression::make(pool, 0), kSrcVID));
newCols->addColumn(new YieldColumn(ColumnExpression::make(pool, 1), kDstVID));
newCols->addColumn(new YieldColumn(ColumnExpression::make(pool, 2), kRanking));
} else {
idxReturnCols_.emplace_back(kVid);
outputs_.emplace_back(kVertexID, vidType_);
newCols->addColumn(new YieldColumn(ColumnExpression::make(pool, 0), kVertexID));
}

auto yieldClause = sentence()->yieldClause();
if (yieldClause == nullptr) {
extractExprProps();
return Status::OK();
return Status::SemanticError("Missing yield clause.");
}
lookupCtx_->dedup = yieldClause->isDistinct();
lookupCtx_->yieldExpr = qctx_->objPool()->add(new YieldColumns());

if (lookupCtx_->isEdge) {
idxReturnCols_.emplace_back(nebula::kSrc);
idxReturnCols_.emplace_back(nebula::kDst);
idxReturnCols_.emplace_back(nebula::kRank);
idxReturnCols_.emplace_back(nebula::kType);
NG_RETURN_IF_ERROR(validateYieldEdge());
} else {
idxReturnCols_.emplace_back(nebula::kVid);
NG_RETURN_IF_ERROR(validateYieldTag());
}
if (exprProps_.hasInputVarProperty()) {
Expand Down
4 changes: 2 additions & 2 deletions src/graph/validator/MatchValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,8 @@ Status MatchValidator::validateGroup(YieldClauseContext &yieldCtx) const {
yieldCtx.aggOutputColumnNames_.emplace_back(agg->toString());
}
if (!aggs.empty()) {
auto *rewrittenExpr = ExpressionUtils::rewriteAgg2VarProp(colExpr);
yieldCtx.projCols_->addColumn(new YieldColumn(rewrittenExpr, colOldName));
auto *rewritedExpr = ExpressionUtils::rewriteAgg2VarProp(colExpr->clone());
yieldCtx.projCols_->addColumn(new YieldColumn(rewritedExpr, colOldName));
yieldCtx.projOutputColumnNames_.emplace_back(colOldName);
continue;
}
Expand Down
39 changes: 23 additions & 16 deletions src/graph/validator/test/GetSubgraphValidatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using PK = nebula::graph::PlanNode::Kind;

TEST_F(GetSubgraphValidatorTest, Base) {
{
std::string query = "GET SUBGRAPH FROM \"1\"";
std::string query = "GET SUBGRAPH FROM \"1\" YIELD vertices as nodes";
std::vector<PlanNode::Kind> expected = {
PK::kProject,
PK::kDataCollect,
Expand All @@ -32,7 +32,7 @@ TEST_F(GetSubgraphValidatorTest, Base) {
EXPECT_TRUE(checkResult(query, expected));
}
{
std::string query = "GET SUBGRAPH WITH PROP 3 STEPS FROM \"1\"";
std::string query = "GET SUBGRAPH WITH PROP 3 STEPS FROM \"1\" YIELD edges as relationships";
std::vector<PlanNode::Kind> expected = {
PK::kProject,
PK::kDataCollect,
Expand All @@ -58,7 +58,8 @@ TEST_F(GetSubgraphValidatorTest, Base) {
EXPECT_TRUE(checkResult(query, expected));
}
{
std::string query = "GET SUBGRAPH WITH PROP FROM \"1\", \"2\" IN like";
std::string query =
"GET SUBGRAPH WITH PROP FROM \"1\", \"2\" IN like YIELD vertices as a, edges as b";
std::vector<PlanNode::Kind> expected = {
PK::kProject,
PK::kDataCollect,
Expand All @@ -76,7 +77,7 @@ TEST_F(GetSubgraphValidatorTest, Input) {
{
std::string query =
"GO FROM \"1\" OVER like YIELD like._src AS src | GET SUBGRAPH WITH "
"PROP FROM $-.src";
"PROP FROM $-.src YIELD vertices as a, edges as b";
std::vector<PlanNode::Kind> expected = {
PK::kProject,
PK::kDataCollect,
Expand All @@ -95,7 +96,7 @@ TEST_F(GetSubgraphValidatorTest, Input) {
{
std::string query =
"$a = GO FROM \"1\" OVER like YIELD like._src AS src; GET SUBGRAPH "
"FROM $a.src";
"FROM $a.src YIELD vertices as a, edges as b";
std::vector<PlanNode::Kind> expected = {
PK::kProject,
PK::kDataCollect,
Expand All @@ -112,7 +113,7 @@ TEST_F(GetSubgraphValidatorTest, Input) {
EXPECT_TRUE(checkResult(query, expected));
}
{
std::string query = "GET SUBGRAPH 0 STEPS FROM \"1\"";
std::string query = "GET SUBGRAPH 0 STEPS FROM \"1\" YIELD vertices as nodes";
std::vector<PlanNode::Kind> expected = {
PK::kAggregate,
PK::kGetVertices,
Expand All @@ -121,7 +122,8 @@ TEST_F(GetSubgraphValidatorTest, Input) {
EXPECT_TRUE(checkResult(query, expected));
}
{
std::string query = "GET SUBGRAPH WITH PROP 0 STEPS FROM \"1\", \"2\", \"3\"";
std::string query =
"GET SUBGRAPH WITH PROP 0 STEPS FROM \"1\", \"2\", \"3\" YIELD vertices as nodes";
std::vector<PlanNode::Kind> expected = {
PK::kAggregate,
PK::kGetVertices,
Expand All @@ -132,7 +134,7 @@ TEST_F(GetSubgraphValidatorTest, Input) {
{
std::string query =
"GO FROM \"1\" OVER like YIELD like._src AS src | GET SUBGRAPH WITH "
"PROP 0 STEPS FROM $-.src";
"PROP 0 STEPS FROM $-.src YIELD vertices as nodes";
std::vector<PlanNode::Kind> expected = {
PK::kAggregate,
PK::kGetVertices,
Expand All @@ -147,7 +149,7 @@ TEST_F(GetSubgraphValidatorTest, Input) {
{
std::string query =
"$a = GO FROM \"1\" OVER like YIELD like._src AS src; GET SUBGRAPH "
"WITH PROP 0 STEPS FROM $a.src";
"WITH PROP 0 STEPS FROM $a.src YIELD vertices as nodes";
std::vector<PlanNode::Kind> expected = {
PK::kAggregate,
PK::kGetVertices,
Expand All @@ -162,6 +164,11 @@ TEST_F(GetSubgraphValidatorTest, Input) {
}

TEST_F(GetSubgraphValidatorTest, invalidYield) {
{
std::string query = "GET SUBGRAPH WITH PROP FROM \"Tim Duncan\"";
auto result = checkResult(query);
EXPECT_EQ(std::string(result.message()), "SemanticError: Missing yield clause.");
}
{
std::string query = "GET SUBGRAPH WITH PROP FROM \"Tim Duncan\" YIELD vertice";
auto result = checkResult(query);
Expand Down Expand Up @@ -202,19 +209,19 @@ TEST_F(GetSubgraphValidatorTest, invalidYield) {

TEST_F(GetSubgraphValidatorTest, RefNotExist) {
{
std::string query = "GET SUBGRAPH WITH PROP FROM $-.id";
std::string query = "GET SUBGRAPH WITH PROP FROM $-.id YIELD edges as b";
auto result = checkResult(query);
EXPECT_EQ(std::string(result.message()), "SemanticError: `$-.id', not exist prop `id'");
}
{
std::string query = "GET SUBGRAPH WITH PROP FROM $a.id";
std::string query = "GET SUBGRAPH WITH PROP FROM $a.id YIELD edges as b";
auto result = checkResult(query);
EXPECT_EQ(std::string(result.message()), "SemanticError: `$a.id', not exist variable `a'");
}
{
std::string query =
"GO FROM \"1\" OVER like YIELD $$.person.age AS id | GET SUBGRAPH WITH "
"PROP FROM $-.id";
"PROP FROM $-.id YIELD vertices as nodes";
auto result = checkResult(query);
EXPECT_EQ(std::string(result.message()),
"SemanticError: `$-.id', the srcs should be type of "
Expand All @@ -223,7 +230,7 @@ TEST_F(GetSubgraphValidatorTest, RefNotExist) {
{
std::string query =
"$a = GO FROM \"1\" OVER like YIELD $$.person.age AS ID; GET SUBGRAPH "
"FROM $a.ID";
"FROM $a.ID YIELD edges as relationships";
auto result = checkResult(query);
EXPECT_EQ(std::string(result.message()),
"SemanticError: `$a.ID', the srcs should be type of "
Expand All @@ -232,21 +239,21 @@ TEST_F(GetSubgraphValidatorTest, RefNotExist) {
{
std::string query =
"$a = GO FROM \"1\" OVER like YIELD like._src AS src; GET SUBGRAPH "
"WITH PROP FROM $b.src";
"WITH PROP FROM $b.src YIELD vertices as nodes";
auto result = checkResult(query);
EXPECT_EQ(std::string(result.message()), "SemanticError: `$b.src', not exist variable `b'");
}
{
std::string query =
"GO FROM \"1\" OVER like YIELD like._dst AS id, like._src AS id | GET "
"SUBGRAPH FROM $-.id";
"SUBGRAPH FROM $-.id YIELD edges as relationships";
auto result = checkResult(query);
EXPECT_EQ(std::string(result.message()), "SemanticError: Duplicate Column Name : `id'");
}
{
std::string query =
"$a = GO FROM \"1\" OVER like YIELD like._dst AS id, like._src AS id; "
"GET SUBGRAPH WITH PROP FROM $a.id";
"GET SUBGRAPH WITH PROP FROM $a.id YIELD vertices as nodes";
auto result = checkResult(query);
EXPECT_EQ(std::string(result.message()), "SemanticError: Duplicate Column Name : `id'");
}
Expand Down
41 changes: 26 additions & 15 deletions src/graph/validator/test/LookupValidatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ TEST_F(LookupValidatorTest, InputOutput) {
// pipe
{
const std::string query =
"LOOKUP ON person where person.age == 35 | "
"FETCH PROP ON person $-.VertexID YIELD vertex as node";
"LOOKUP ON person where person.age == 35 YIELD id(vertex) as id | "
"FETCH PROP ON person $-.id YIELD vertex as node";
EXPECT_TRUE(checkResult(query,
{
PlanNode::Kind::kProject,
Expand Down Expand Up @@ -48,8 +48,8 @@ TEST_F(LookupValidatorTest, InputOutput) {
// variable
{
const std::string query =
"$a = LOOKUP ON person where person.age == 35; "
"FETCH PROP ON person $a.VertexID YIELD vertex as node";
"$a = LOOKUP ON person where person.age == 35 YIELD id(vertex) as id; "
"FETCH PROP ON person $a.id YIELD vertex as node";
EXPECT_TRUE(checkResult(query,
{
PlanNode::Kind::kProject,
Expand All @@ -63,8 +63,7 @@ TEST_F(LookupValidatorTest, InputOutput) {
// var with yield
{
const std::string query =
"$a = LOOKUP ON person where person.age == 35 YIELD person.name AS "
"name;"
"$a = LOOKUP ON person where person.age == 35 YIELD person.name AS name;"
"FETCH PROP ON person $a.name YIELD vertex as node";
EXPECT_TRUE(checkResult(query,
{
Expand All @@ -79,7 +78,10 @@ TEST_F(LookupValidatorTest, InputOutput) {
}

TEST_F(LookupValidatorTest, InvalidYieldExpression) {
// TODO(shylock)
{
const std::string query = "LOOKUP ON person where person.age > 20;";
EXPECT_FALSE(checkResult(query, {}));
}
{
const std::string query =
"LOOKUP ON person where person.age == 35 YIELD person.age + 1 AS age;";
Expand All @@ -95,40 +97,49 @@ TEST_F(LookupValidatorTest, InvalidYieldExpression) {

TEST_F(LookupValidatorTest, InvalidFilterExpression) {
{
const std::string query = "LOOKUP ON person where person.age == person.name;";
const std::string query =
"LOOKUP ON person where person.age == person.name YIELD vertex as node;";
EXPECT_FALSE(checkResult(query, {}));
}
{
const std::string query = "LOOKUP ON person where person.age > person.name;";
const std::string query =
"LOOKUP ON person where person.age > person.name YIELD vertex as node;";
EXPECT_FALSE(checkResult(query, {}));
}
{
const std::string query = "LOOKUP ON person where person.age != person.name;";
const std::string query =
"LOOKUP ON person where person.age != person.name YIELD vertex as node;";
EXPECT_FALSE(checkResult(query, {}));
}
{
const std::string query = "LOOKUP ON person where person.age + 1 > 5;";
const std::string query = "LOOKUP ON person where person.age + 1 > 5 YIELD person.age;";
EXPECT_FALSE(checkResult(query, {}));
}
{
const std::string query = "LOOKUP ON person where person.age > person.name + 5;";
const std::string query =
"LOOKUP ON person where person.age > person.name + 5 YIELD id(vertex);";
EXPECT_FALSE(checkResult(query, {}));
}
{
const std::string query = "LOOKUP ON person where 1 + 5 < person.age;";
const std::string query = "LOOKUP ON person where 1 + 5 < person.age YIELD vertex as node;";
EXPECT_TRUE(checkResult(query, {}));
}
{
const std::string query = "LOOKUP ON person where person.age > 1 + 5;";
const std::string query = "LOOKUP ON person where person.age > 1 + 5 YIELD vertex as node;";
EXPECT_TRUE(checkResult(query, {}));
}
{
const std::string query = "LOOKUP ON person where person.age > abs(-5);";
const std::string query = "LOOKUP ON person where person.age > abs(-5) YIELD id(vertex);";
EXPECT_TRUE(checkResult(query, {}));
}
}

TEST_F(LookupValidatorTest, wrongYield) {
{
std::string query = "LOOKUP ON person";
auto result = checkResult(query);
EXPECT_EQ(std::string(result.message()), "SemanticError: Missing yield clause.");
}
{
std::string query = "LOOKUP ON person YIELD vertex";
auto result = checkResult(query);
Expand Down
Loading

0 comments on commit 0112069

Please sign in to comment.