Skip to content

Commit

Permalink
fix gv colnames
Browse files Browse the repository at this point in the history
  • Loading branch information
nevermore3 committed Aug 30, 2021
1 parent 056c32a commit 501b1f8
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/graph/context/ast/QueryAstContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ struct FetchVerticesContext final : public AstContext {
bool distinct{false};
YieldColumns* yieldExpr{nullptr};
ExpressionProps exprProps;
std::vector<std::string> colNames;
std::vector<std::string> gvColNames;

// store the result of the previous sentence
std::string inputVarName;
Expand Down
2 changes: 1 addition & 1 deletion src/graph/planner/ngql/FetchVerticesPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ StatusOr<SubPlan> FetchVerticesPlanner::transform(AstContext* astCtx) {
{},
fetchCtx_->distinct);
getVertices->setInputVar(vidsVar);
getVertices->setColNames(fetchCtx_->colNames);
// getVertices->setColNames(fetchCtx_->gvColNames);

subPlan.root = Project::make(qctx, getVertices, fetchCtx_->yieldExpr);
if (fetchCtx_->distinct) {
Expand Down
77 changes: 57 additions & 20 deletions src/graph/validator/FetchVerticesValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include "graph/planner/plan/Query.h"
#include "graph/util/ExpressionUtils.h"
#include "graph/util/SchemaUtil.h"
#include "graph/util/ValidateUtil.h"
#include "graph/visitor/DeducePropsVisitor.h"

Expand All @@ -27,6 +26,21 @@ Status FetchVerticesValidator::validateImpl() {
return Status::OK();
}

Expression *FetchVerticesValidator::rewriteIDVertex2Vid(const Expression *expr) {
auto *pool = qctx_->objPool();
auto matcher = [](const Expression *e) -> bool {
std::string lowerStr = e->toString();
folly::toLowerAscii(lowerStr);
return e->kind() == Expression::Kind::kFunctionCall && lowerStr == "id(vertex)";
};
auto rewriter = [pool](const Expression *e) -> Expression * {
UNUSED(e);
return InputPropertyExpression::make(pool, nebula::kVid);
};

return RewriteVisitor::transform(expr, std::move(matcher), std::move(rewriter));
}

Status FetchVerticesValidator::validateTag(const NameLabelList *nameLabels) {
if (nameLabels == nullptr) {
// all tag
Expand Down Expand Up @@ -54,32 +68,55 @@ Status FetchVerticesValidator::validateTag(const NameLabelList *nameLabels) {

Status FetchVerticesValidator::validateYield(YieldClause *yield) {
auto pool = qctx_->objPool();
bool existVertex = false;
if (yield == nullptr) {
// version 3.0: return Status::SemanticError("No YIELD Clause");
auto *yieldColumns = new YieldColumns();
auto *vertex = new YieldColumn(VertexExpression::make(pool));
yieldColumns->addColumn(vertex);
yield = pool->add(new YieldClause(yieldColumns));
existVertex = true;
}
for (const auto &col : yield->columns()) {
if (col->expr()->kind() == Expression::Kind::kVertex) {
existVertex = true;
break;
}
}
fetchCtx_->distinct = yield->isDistinct();

fetchCtx_->distinct = yield->isDistinct();
auto size = yield->columns().size();
outputs_.reserve(size + 1); // VertexID
outputs_.emplace_back(VertexID, vidType_);
outputs_.reserve(size + 1);

auto &exprProps = fetchCtx_->exprProps;
auto *newCols = pool->add(new YieldColumns());
if (!existVertex) {
outputs_.emplace_back(VertexID, vidType_);
auto *vidCol = new YieldColumn(InputPropertyExpression::make(pool, nebula::kVid), VertexID);
newCols->addColumn(vidCol);
} else {
extractVertexProp(exprProps);
}
for (auto col : yield->columns()) {
// yield vertex or id(vertex)
col->setExpr(ExpressionUtils::rewriteLabelAttr2TagProp(col->expr()));
NG_RETURN_IF_ERROR(ValidateUtil::invalidLabelIdentifiers(col->expr()));

auto colExpr = col->expr();
auto typeStatus = deduceExprType(colExpr);
NG_RETURN_IF_ERROR(typeStatus);
outputs_.emplace_back(col->name(), typeStatus.value());
col->setAlias(col->name());
col->setExpr(rewriteIDVertex2Vid(colExpr));
newCols->addColumn(col->clone().release());

NG_RETURN_IF_ERROR(deduceProps(colExpr, exprProps));
}
if (exprProps.tagProps().empty()) {
for (const auto &tagSchema : tagsSchema_) {
exprProps.insertTagProp(tagSchema.first, nebula::kTag);
}
}
fetchCtx_->yieldExpr = newCols;

if (exprProps.hasInputVarProperty()) {
return Status::SemanticError("Unsupported input/variable property expression in yield.");
Expand All @@ -88,24 +125,24 @@ Status FetchVerticesValidator::validateYield(YieldClause *yield) {
return Status::SemanticError("Unsupported src/dst property expression in yield.");
}

// for (const auto &tag : exprProps.tagNameIds()) {
// if (tagsSchema_.find(tag.first) == tagsSchema_.end()) {
// return Status::SemanticError("Mismatched tag `%s'", tag);
// }
// }
auto *newCols = pool->add(new YieldColumns());
// TODO (will be deleted in version 3.0)
auto *vidCol = new YieldColumn(InputPropertyExpression::make(pool, nebula::kVid), VertexID);
newCols->addColumn(vidCol);
for (const auto &col : yield->columns()) {
newCols->addColumn(col->clone().release());
for (const auto &tag : exprProps.tagNameIds()) {
if (tagsSchema_.find(tag.second) == tagsSchema_.end()) {
return Status::SemanticError("Mismatched tag `%s'", tag.first.c_str());
}
}
fetchCtx_->yieldExpr = newCols;
auto colNames = getOutColNames();
colNames.insert(colNames.begin(), nebula::kVid);
fetchCtx_->colNames = std::move(colNames);
return Status::OK();
}

void FetchVerticesValidator::extractVertexProp(ExpressionProps &exprProps) {
for (const auto &tagSchema : tagsSchema_) {
auto tagID = tagSchema.first;
exprProps.insertTagProp(tagID, nebula::kTag);
for (std::size_t i = 0; i < tagSchema.second->getNumFields(); ++i) {
const auto propName = tagSchema.second->getFieldName(i);
exprProps.insertTagProp(tagID, propName);
}
}
}

} // namespace graph
} // namespace nebula
6 changes: 4 additions & 2 deletions src/graph/validator/FetchVerticesValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ class FetchVerticesValidator final : public Validator {

AstContext* getAstContext() override { return fetchCtx_.get(); }

private:
std::unordered_map<std::string, TagID> tags_;
void extractVertexProp(ExpressionProps& exprProps);

Expression* rewriteIDVertex2Vid(const Expression* expr);

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

std::unique_ptr<FetchVerticesContext> fetchCtx_;
Expand Down
18 changes: 6 additions & 12 deletions src/graph/validator/test/FetchVerticesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class FetchVerticesValidatorTest : public ValidatorTestBase {
};

TEST_F(FetchVerticesValidatorTest, FetchVerticesProp) {
auto src = VariablePropertyExpression::make(pool_.get(), "_VARNAME_", "VertexID");
// auto src = VariablePropertyExpression::make(pool_.get(), "_VARNAME_", "VertexID");
auto src = ColumnExpression::make(pool_.get(), 0);
{
auto qctx = getQCtx("FETCH PROP ON person \"1\"");
auto *pool = qctx->objPool();
Expand All @@ -40,9 +41,8 @@ TEST_F(FetchVerticesValidatorTest, FetchVerticesProp) {
gv->setColNames({nebula::kVid, "person.name", "person.age"});
// project
auto yieldColumns = std::make_unique<YieldColumns>();
yieldColumns->addColumn(new YieldColumn(VertexExpression::make(pool), "VERTEX"));
yieldColumns->addColumn(new YieldColumn(VertexExpression::make(pool)));
auto *project = Project::make(qctx, gv, yieldColumns.get());
project->setColNames({"VERTEX"});
auto result = Eq(qctx->plan()->root(), project);
ASSERT_TRUE(result.ok()) << result;
}
Expand Down Expand Up @@ -71,9 +71,8 @@ TEST_F(FetchVerticesValidatorTest, FetchVerticesProp) {
gv->setColNames({nebula::kVid, "person.name", "person.age", "book.name"});
// project
auto yieldColumns = std::make_unique<YieldColumns>();
yieldColumns->addColumn(new YieldColumn(VertexExpression::make(pool), "vertices_"));
yieldColumns->addColumn(new YieldColumn(VertexExpression::make(pool)));
auto *project = Project::make(qctx, gv, yieldColumns.get());
project->setColNames({"vertices_"});
auto result = Eq(qctx->plan()->root(), project);
ASSERT_TRUE(result.ok()) << result;
}
Expand Down Expand Up @@ -405,9 +404,8 @@ TEST_F(FetchVerticesValidatorTest, FetchVerticesProp) {
gv->setColNames({nebula::kVid, "person.name", "person.age"});
// project
auto yieldColumns = std::make_unique<YieldColumns>();
yieldColumns->addColumn(new YieldColumn(VertexExpression::make(pool), "vertices_"));
yieldColumns->addColumn(new YieldColumn(VertexExpression::make(pool)));
auto *project = Project::make(qctx, gv, yieldColumns.get());
project->setColNames({"vertices_"});
auto result = Eq(qctx->plan()->root(), project);
ASSERT_TRUE(result.ok()) << result;
}
Expand All @@ -420,9 +418,8 @@ TEST_F(FetchVerticesValidatorTest, FetchVerticesProp) {
gv->setColNames({nebula::kVid, "person.name", "person.age"});
// project
auto yieldColumns = std::make_unique<YieldColumns>();
yieldColumns->addColumn(new YieldColumn(VertexExpression::make(pool), "vertices_"));
yieldColumns->addColumn(new YieldColumn(VertexExpression::make(pool)));
auto *project = Project::make(qctx, gv, yieldColumns.get());
project->setColNames({"vertices_"});
auto result = Eq(qctx->plan()->root(), project);
ASSERT_TRUE(result.ok()) << result;
}
Expand All @@ -443,7 +440,6 @@ TEST_F(FetchVerticesValidatorTest, FetchVerticesProp) {
new YieldColumn(InputPropertyExpression::make(pool, nebula::kVid), "VertexID"));
yieldColumns->addColumn(new YieldColumn(TagPropertyExpression::make(pool, "person", "name")));
auto *project = Project::make(qctx, gv, yieldColumns.get());
project->setColNames(colNames);

auto result = Eq(qctx->plan()->root(), project);
ASSERT_TRUE(result.ok()) << result;
Expand All @@ -465,7 +461,6 @@ TEST_F(FetchVerticesValidatorTest, FetchVerticesProp) {
yieldColumns->addColumn(new YieldColumn(TagPropertyExpression::make(pool, "person", "name")));
yieldColumns->addColumn(new YieldColumn(TagPropertyExpression::make(pool, "person", "age")));
auto *project = Project::make(qctx, gv, yieldColumns.get());
project->setColNames(colNames);

auto result = Eq(qctx->plan()->root(), project);
ASSERT_TRUE(result.ok()) << result;
Expand All @@ -488,7 +483,6 @@ TEST_F(FetchVerticesValidatorTest, FetchVerticesProp) {
yieldColumns->addColumn(new YieldColumn(TagPropertyExpression::make(pool, "person", "name")));
yieldColumns->addColumn(new YieldColumn(TagPropertyExpression::make(pool, "person", "age")));
auto *project = Project::make(qctx, gv, yieldColumns.get());
project->setColNames({"VertexID", "(1+1)", "person.name", "person.age"});

auto result = Eq(qctx->plan()->root(), project);
ASSERT_TRUE(result.ok()) << result;
Expand Down
3 changes: 3 additions & 0 deletions src/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,9 @@ expression
| reduce_expression {
$$ = $1;
}
| KW_VERTEX {
$$ = VertexExpression::make(qctx->objPool());
}
;

constant_expression
Expand Down
31 changes: 17 additions & 14 deletions tests/tck/features/fetch/FetchVertices.intVid.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Feature: Fetch Int Vid Vertices
FETCH PROP ON player hash('Boris Diaw')
"""
Then the result should be, in any order:
| vertices_ |
| VERTEX |
| (hash('Boris Diaw'):player{age:36,name:"Boris Diaw"}) |
# Fetch prop on not existing vertex
When executing query:
Expand Down Expand Up @@ -104,7 +104,8 @@ Feature: Fetch Int Vid Vertices
Scenario: Fetch from pipe
When executing query:
"""
GO FROM hash('Boris Diaw') over like YIELD like._dst as id | FETCH PROP ON player $-.id YIELD player.name, player.age
GO FROM hash('Boris Diaw') over like YIELD like._dst as id |
FETCH PROP ON player $-.id YIELD player.name, player.age
"""
Then the result should be, in any order, and the columns 0 should be hashed:
| VertexID | player.name | player.age |
Expand All @@ -113,31 +114,33 @@ Feature: Fetch Int Vid Vertices
# empty input
When executing query:
"""
GO FROM hash('NON EXIST VERTEX ID') over like YIELD like._dst as id | FETCH PROP ON player $-.id yield player.name
GO FROM hash('NON EXIST VERTEX ID') over like YIELD like._dst as id |
FETCH PROP ON player $-.id yield player.name
"""
Then the result should be, in any order:
| VertexID | player.name |
When executing query:
"""
GO FROM hash('NON EXIST VERTEX ID') over serve YIELD serve._dst as id, serve.start_year as start
| YIELD $-.id as id WHERE $-.start > 20000 | FETCH PROP ON player $-.id yield player.name
GO FROM hash('NON EXIST VERTEX ID') over serve YIELD serve._dst as id, serve.start_year as start |
YIELD $-.id as id WHERE $-.start > 20000 |
FETCH PROP ON player $-.id yield player.name
"""
Then the result should be, in any order:
| VertexID | player.name |
# Fetch prop on multi tags of vertices from pipe
When executing query:
"""
GO FROM hash("Boris Diaw") over like YIELD like._dst as id
| FETCH PROP ON player, team, bachelor $-.id YIELD player.name, player.age, team.name, bachelor.name, bachelor.speciality
GO FROM hash("Boris Diaw") over like YIELD like._dst as id |
FETCH PROP ON player, team, bachelor $-.id YIELD player.name, player.age, team.name, bachelor.name, bachelor.speciality
"""
Then the result should be, in any order, and the columns 0 should be hashed:
| VertexID | player.name | player.age | team.name | bachelor.name | bachelor.speciality |
| "Tim Duncan" | "Tim Duncan" | 42 | EMPTY | "Tim Duncan" | "psychology" |
| "Tony Parker" | "Tony Parker" | 36 | EMPTY | EMPTY | EMPTY |
When executing query:
"""
GO FROM hash('Boris Diaw') over like YIELD like._dst as id
| FETCH PROP ON player, bachelor $-.id YIELD player.name, player.age, bachelor.name, bachelor.speciality
GO FROM hash('Boris Diaw') over like YIELD like._dst as id |
FETCH PROP ON player, bachelor $-.id YIELD player.name, player.age, bachelor.name, bachelor.speciality
"""
Then the result should be, in any order, and the columns 0 should be hashed:
| VertexID | player.name | player.age | bachelor.name | bachelor.speciality |
Expand Down Expand Up @@ -204,7 +207,7 @@ Feature: Fetch Int Vid Vertices
FETCH PROP ON * hash('NON EXIST VERTEX ID')
"""
Then the result should be, in any order:
| vertices_ |
| VERTEX |
# on existing vertex
When executing query:
"""
Expand Down Expand Up @@ -250,13 +253,13 @@ Feature: Fetch Int Vid Vertices
FETCH PROP ON * hash('Tim Duncan')
"""
Then the result should be, in any order, with relax comparison:
| vertices_ |
| VERTEX |
| ("Tim Duncan":player:bachelor) |

Scenario: Fetch and Yield id(v)
When executing query:
"""
FETCH PROP ON player hash('Boris Diaw'), hash('Tony Parker') | YIELD id($-.vertices_) as id
FETCH PROP ON player hash('Boris Diaw'), hash('Tony Parker') | YIELD id($-.VERTEX) as id
"""
Then the result should be, in any order, and the columns 0 should be hashed:
| id |
Expand All @@ -266,8 +269,8 @@ Feature: Fetch Int Vid Vertices
Scenario: Fetch vertices and then GO
When executing query:
"""
FETCH PROP ON player hash('Tony Parker') YIELD player.name as Name
| GO FROM $-.VertexID OVER like
FETCH PROP ON player hash('Tony Parker') YIELD player.name as Name |
GO FROM $-.VertexID OVER like
"""
Then the result should be, in any order, and the columns 0 should be hashed:
| like._dst |
Expand Down
Loading

0 comments on commit 501b1f8

Please sign in to comment.