Skip to content

Commit

Permalink
rewrite yield clause
Browse files Browse the repository at this point in the history
  • Loading branch information
nevermore3 committed Nov 11, 2021
1 parent cfbd4bb commit fa692a9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
14 changes: 7 additions & 7 deletions src/common/expression/LabelTagPropertyExpression.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
* This source code is licensed under Apache 2.0 License.
*/
#include "common/expression/LabelTagPropertyExpression.h"

Expand All @@ -10,7 +9,7 @@
namespace nebula {

const Value& LabelTagPropertyExpression::eval(ExpressionContext& ctx) {
const auto& var = ctx.getVar(label_);
const auto& var = label_->eval(ctx);
if (var.type() != Value::Type::VERTEX) {
return Value::kNullBadType;
}
Expand All @@ -26,26 +25,27 @@ const Value& LabelTagPropertyExpression::eval(ExpressionContext& ctx) {
}

std::string LabelTagPropertyExpression::toString() const {
return label_ + "." + tag_ + "." + prop_;
std::string labelStr = label_->toString();
return labelStr.erase(0, 1) + "." + tag_ + "." + prop_;
}

bool LabelTagPropertyExpression::operator==(const Expression& rhs) const {
if (kind_ != rhs.kind()) {
return false;
}
const auto& expr = static_cast<const LabelTagPropertyExpression&>(rhs);
return label_ == expr.label_ && tag_ == expr.tag_ && prop_ == expr.prop_;
return *label_ == *expr.label_ && tag_ == expr.tag_ && prop_ == expr.prop_;
}

void LabelTagPropertyExpression::writeTo(Encoder& encoder) const {
encoder << kind_;
encoder << label_;
encoder << *label_;
encoder << tag_;
encoder << prop_;
}

void LabelTagPropertyExpression::resetFrom(Decoder& decoder) {
label_ = decoder.readStr();
label_ = decoder.readExpression(pool_);
tag_ = decoder.readStr();
prop_ = decoder.readStr();
}
Expand Down
11 changes: 5 additions & 6 deletions src/common/expression/LabelTagPropertyExpression.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* Copyright (c) 2021 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
* This source code is licensed under Apache 2.0 License.
*/

#pragma once
Expand All @@ -22,7 +21,7 @@ class LabelTagPropertyExpression final : public Expression {
LabelTagPropertyExpression& operator=(LabelTagPropertyExpression&&) = delete;

static LabelTagPropertyExpression* make(ObjectPool* pool,
const std::string& label = "",
Expression* label = nullptr,
const std::string& tag = "",
const std::string& prop = "") {
return pool->add(new LabelTagPropertyExpression(pool, label, tag, prop));
Expand All @@ -44,11 +43,11 @@ class LabelTagPropertyExpression final : public Expression {

const std::string& tag() const { return tag_; }

const std::string& label() const { return label_; }
const Expression* label() const { return label_; }

private:
LabelTagPropertyExpression(ObjectPool* pool,
const std::string& label = "",
Expression* label = nullptr,
const std::string& tag = "",
const std::string& prop = "")
: Expression(pool, Kind::kLabelTagProperty), label_(label), tag_(tag), prop_(prop) {}
Expand All @@ -57,7 +56,7 @@ class LabelTagPropertyExpression final : public Expression {
void resetFrom(Decoder& decoder) override;

private:
std::string label_;
Expression* label_{nullptr};
std::string tag_;
std::string prop_;
};
Expand Down
7 changes: 4 additions & 3 deletions src/graph/util/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Expression *ExpressionUtils::rewriteAttr2LabelTagProp(const Expression *expr) {
if (e->kind() == Expression::Kind::kAttribute) {
auto attrExpr = static_cast<const AttributeExpression *>(e);
if (attrExpr->left()->kind() == Expression::Kind::kLabelAttribute &&
attrExpr->right()->kind() == Expression::Kind::kLabel) {
attrExpr->right()->kind() == Expression::Kind::kConstant) {
return true;
}
}
Expand All @@ -128,10 +128,11 @@ Expression *ExpressionUtils::rewriteAttr2LabelTagProp(const Expression *expr) {
auto tagExpr = const_cast<ConstantExpression *>(labelAttrExpr->right());
auto propExpr = static_cast<const LabelExpression *>(attrExpr->right());
QueryExpressionContext ctx(nullptr);
const auto &label = labelExpr->eval(ctx);
const auto &labelVal = labelExpr->eval(ctx);
auto label = VariablePropertyExpression::make(pool, "", labelVal.getStr());
const auto &tag = tagExpr->eval(ctx);
const auto &prop = const_cast<LabelExpression *>(propExpr)->eval(ctx);
return LabelTagPropertyExpression::make(pool, label.getStr(), tag.getStr(), prop.getStr());
return LabelTagPropertyExpression::make(pool, label, tag.getStr(), prop.getStr());
};

return RewriteVisitor::transform(expr, std::move(matcher), std::move(rewriter));
Expand Down
27 changes: 15 additions & 12 deletions src/graph/validator/MatchValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,30 +349,28 @@ Status MatchValidator::validateReturn(MatchReturn *ret,
return Status::SemanticError("RETURN * is not allowed when there are no variables in scope");
}
}
std::vector<const Expression *> exprs;
if (ret->returnItems()->columns()) {
exprs.reserve(ret->returnItems()->columns()->size());
for (auto *column : ret->returnItems()->columns()->columns()) {
if (ExpressionUtils::hasAny(column->expr(),
{Expression::Kind::kVertex, Expression::Kind::kEdge})) {
return Status::SemanticError(
"keywords: vertex and edge are not supported in return clause `%s'",
column->toString().c_str());
}
if (!retClauseCtx.yield->hasAgg_ &&
ExpressionUtils::hasAny(column->expr(), {Expression::Kind::kAggregate})) {
retClauseCtx.yield->hasAgg_ = true;
}
column->setExpr(ExpressionUtils::rewriteAttr2LabelTagProp(column->expr()));
exprs.push_back(column->expr());
columns->addColumn(column->clone().release());
}
}
DCHECK(!columns->empty());
retClauseCtx.yield->yieldColumns = columns;

// Check all referencing expressions are valid
std::vector<const Expression *> exprs;
exprs.reserve(retClauseCtx.yield->yieldColumns->size());
for (auto *col : retClauseCtx.yield->yieldColumns->columns()) {
if (!retClauseCtx.yield->hasAgg_ &&
ExpressionUtils::hasAny(col->expr(), {Expression::Kind::kAggregate})) {
retClauseCtx.yield->hasAgg_ = true;
}
exprs.push_back(col->expr());
}
NG_RETURN_IF_ERROR(validateAliases(exprs, retClauseCtx.yield->aliasesUsed));
NG_RETURN_IF_ERROR(validateYield(*retClauseCtx.yield));

Expand Down Expand Up @@ -772,18 +770,23 @@ Status MatchValidator::checkAlias(
return Status::OK();
}
case Expression::Kind::kLabelTagProperty: {
auto name = static_cast<const LabelTagPropertyExpression *>(refExpr)->label();
auto labelExpr = static_cast<const LabelTagPropertyExpression *>(refExpr)->label();
auto name = static_cast<const VariablePropertyExpression *>(labelExpr)->prop();
auto res = getAliasType(aliasesUsed, name);
NG_RETURN_IF_ERROR(res);
if (res.value() != AliasType::kNode) {
return Status::SemanticError("Alias: %s 's type must be vertex", name.c_str());
return Status::SemanticError("The type of `%s' must be tag", name.c_str());
}
return Status::OK();
}
case Expression::Kind::kLabelAttribute: {
auto name = static_cast<const LabelAttributeExpression *>(refExpr)->left()->name();
auto res = getAliasType(aliasesUsed, name);
NG_RETURN_IF_ERROR(res);
if (res.value() == AliasType::kNode) {
return Status::SemanticError(
"To get the attribute of the vertex, use alias.tagname.propname format");
}
return Status::OK();
}
case Expression::Kind::kEdgeSrc: {
Expand Down

0 comments on commit fa692a9

Please sign in to comment.