Skip to content

Commit

Permalink
move labelTagPropertyExpresion to PropertyExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
nevermore3 committed Dec 3, 2021
1 parent 38ba829 commit 26e04d3
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 31 deletions.
1 change: 0 additions & 1 deletion src/common/expression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ nebula_add_library(
FunctionCallExpression.cpp
AggregateExpression.cpp
PropertyExpression.cpp
LabelTagPropertyExpression.cpp
UUIDExpression.cpp
VariableExpression.cpp
ContainerExpression.cpp
Expand Down
4 changes: 2 additions & 2 deletions src/common/expression/LabelTagPropertyExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "common/expression/ExprVisitor.h"

namespace nebula {

/*
const Value& LabelTagPropertyExpression::eval(ExpressionContext& ctx) {
const auto& var = label_->eval(ctx);
if (var.type() != Value::Type::VERTEX) {
Expand Down Expand Up @@ -51,5 +51,5 @@ void LabelTagPropertyExpression::resetFrom(Decoder& decoder) {
}
void LabelTagPropertyExpression::accept(ExprVisitor* visitor) { visitor->visit(this); }

*/
} // namespace nebula
3 changes: 2 additions & 1 deletion src/common/expression/LabelTagPropertyExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
namespace nebula {

/*
class LabelTagPropertyExpression final : public Expression {
public:
LabelTagPropertyExpression& operator=(const LabelTagPropertyExpression& rhs) = delete;
Expand Down Expand Up @@ -60,5 +61,5 @@ class LabelTagPropertyExpression final : public Expression {
std::string tag_;
std::string prop_;
};

*/
} // namespace nebula
41 changes: 41 additions & 0 deletions src/common/expression/PropertyExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,45 @@ std::string VariablePropertyExpression::toString() const {
return buf;
}

const Value& LabelTagPropertyExpression::eval(ExpressionContext& ctx) {
const auto& var = label_->eval(ctx);
if (var.type() != Value::Type::VERTEX) {
return Value::kNullBadType;
}
for (const auto& tag : var.getVertex().tags) {
if (tag.name == sym_) {
auto iter = tag.props.find(prop_);
if (iter != tag.props.end()) {
return iter->second;
}
}
}
return Value::kNullValue;
}

void LabelTagPropertyExpression::accept(ExprVisitor* visitor) { visitor->visit(this); }

std::string LabelTagPropertyExpression::toString() const {
std::string labelStr = label_->toString();
return labelStr.erase(0, 1) + "." + sym_ + "." + 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_ && sym_ == expr.sym_ && prop_ == expr.prop_;
}

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

void LabelTagPropertyExpression::resetFrom(Decoder& decoder) {
PropertyExpression::resetFrom(decoder);
label_ = decoder.readExpression(pool_);
}

} // namespace nebula
41 changes: 41 additions & 0 deletions src/common/expression/PropertyExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,47 @@ class TagPropertyExpression final : public PropertyExpression {
Value result_;
};

// label.tag_name.any_prop_name
class LabelTagPropertyExpression final : public PropertyExpression {
public:
LabelTagPropertyExpression& operator=(const LabelTagPropertyExpression& rhs) = delete;
LabelTagPropertyExpression& operator=(LabelTagPropertyExpression&&) = delete;

static LabelTagPropertyExpression* make(ObjectPool* pool,
Expression* label = nullptr,
const std::string& tag = "",
const std::string& prop = "") {
return pool->add(new LabelTagPropertyExpression(pool, label, tag, prop));
}

std::string toString() const override;

bool operator==(const Expression& rhs) const override;

const Value& eval(ExpressionContext& ctx) override;

void accept(ExprVisitor* visitor) override;

Expression* clone() const override {
return LabelTagPropertyExpression::make(pool_, label_, sym(), prop());
}

const Expression* label() const { return label_; }

private:
LabelTagPropertyExpression(ObjectPool* pool,
Expression* label = nullptr,
const std::string& tag = "",
const std::string& prop = "")
: PropertyExpression(pool, Kind::kLabelTagProperty, "", tag, prop), label_(label) {}

void writeTo(Encoder& encoder) const override;
void resetFrom(Decoder& decoder) override;

private:
Expression* label_{nullptr};
};

// $-.any_prop_name
class InputPropertyExpression final : public PropertyExpression {
public:
Expand Down
2 changes: 1 addition & 1 deletion src/graph/optimizer/rule/IndexScanRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ std::vector<IndexItem> IndexScanRule::findValidIndex(graph::QueryContext* qctx,
return item.col_ == fields[0].get_name();
});
if (it == items.items.end()) {
validIndexes.erase(index);
index = validIndexes.erase(index);
} else {
index++;
}
Expand Down
1 change: 1 addition & 0 deletions src/graph/optimizer/rule/PushFilterDownProjectRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ StatusOr<OptRule::TransformResult> PushFilterDownProjectRule::transform(
auto picker = [&projColumns, &projColNames, &rewriteMap](const Expression* e) -> bool {
auto varProps = graph::ExpressionUtils::collectAll(e,
{Expression::Kind::kTagProperty,
Expression::Kind::kLabelTagProperty,
Expression::Kind::kEdgeProperty,
Expression::Kind::kInputProperty,
Expression::Kind::kVarProperty,
Expand Down
2 changes: 1 addition & 1 deletion src/graph/planner/match/LabelIndexSeek.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ StatusOr<SubPlan> LabelIndexSeek::transformNode(NodeContext* nodeCtx) {
}
}
if (canBeEmbedded2IndexScan) {
auto* srcFilter = ExpressionUtils::rewriteLabelAttr2TagProp(flattenFilter);
auto* srcFilter = ExpressionUtils::rewriteAttr2LabelTagProp(flattenFilter);
storage::cpp2::IndexQueryContext ctx;
ctx.set_filter(Expression::encode(*srcFilter));
scan->setIndexQueryContext({ctx});
Expand Down
24 changes: 14 additions & 10 deletions src/graph/planner/match/MatchSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ Expression* MatchSolver::doRewrite(QueryContext* qctx,
auto alias = aliases.find(labelExpr->name());
DCHECK(alias != aliases.end());
}

return rewriteLabel2VarProp(qctx, expr);
auto res = ExpressionUtils::rewriteAttr2LabelTagProp(expr);
return rewriteLabel2VarProp(qctx, res);
}

Expression* MatchSolver::makeIndexFilter(const std::string& label,
Expand Down Expand Up @@ -162,23 +162,27 @@ Expression* MatchSolver::makeIndexFilter(const std::string& label,
}
propName = la->right()->value().getStr();
} else {
const LabelTagPropertyExpression* la = nullptr;
if (left->kind() == Expression::Kind::kLabelTagProperty &&
const AttributeExpression* la = nullptr;
if (left->kind() == Expression::Kind::kAttribute &&
right->kind() == Expression::Kind::kConstant) {
la = static_cast<const LabelTagPropertyExpression*>(left);
la = static_cast<const AttributeExpression*>(left);
constant = static_cast<const ConstantExpression*>(right);
} else if (right->kind() == Expression::Kind::kLabelTagProperty &&
} else if (right->kind() == Expression::Kind::kAttribute &&
left->kind() == Expression::Kind::kConstant) {
la = static_cast<const LabelTagPropertyExpression*>(right);
la = static_cast<const AttributeExpression*>(right);
constant = static_cast<const ConstantExpression*>(left);
} else {
continue;
}
if (static_cast<const PropertyExpression*>(la->label())->prop() != alias ||
la->tag() != label) {
if (la->left()->kind() != Expression::Kind::kLabelAttribute) {
continue;
}
auto labelAttExpr = static_cast<const LabelAttributeExpression*>(la->left());
if (labelAttExpr->left()->name() != alias ||
labelAttExpr->right()->value().getStr() != label) {
continue;
}
propName = la->prop();
propName = static_cast<const ConstantExpression*>(la->right())->value().getStr();
}

auto* tpExpr =
Expand Down
3 changes: 3 additions & 0 deletions src/graph/util/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bool ExpressionUtils::isPropertyExpr(const Expression *expr) {
auto kind = expr->kind();

return std::unordered_set<Expression::Kind>{Expression::Kind::kTagProperty,
Expression::Kind::kLabelTagProperty,
Expression::Kind::kEdgeProperty,
Expression::Kind::kInputProperty,
Expression::Kind::kVarProperty,
Expand Down Expand Up @@ -76,6 +77,7 @@ bool ExpressionUtils::checkVarExprIfExist(const Expression *expr) {
std::vector<const Expression *> ExpressionUtils::findAllStorage(const Expression *expr) {
return collectAll(expr,
{Expression::Kind::kTagProperty,
Expression::Kind::kLabelTagProperty,
Expression::Kind::kEdgeProperty,
Expression::Kind::kDstProperty,
Expression::Kind::kSrcProperty,
Expand All @@ -98,6 +100,7 @@ bool ExpressionUtils::isConstExpr(const Expression *expr) {
Expression::Kind::kVar,
Expression::Kind::kVersionedVar,
Expression::Kind::kLabelAttribute,
Expression::Kind::kLabelTagProperty,
Expression::Kind::kTagProperty,
Expression::Kind::kEdgeProperty,
Expression::Kind::kDstProperty,
Expand Down
34 changes: 24 additions & 10 deletions src/graph/validator/MatchValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ Status MatchValidator::validateFilter(const Expression *filter,
auto transformRes = ExpressionUtils::filterTransform(filter);
NG_RETURN_IF_ERROR(transformRes);
// rewrite Attribute to LabelTagProperty
whereClauseCtx.filter = ExpressionUtils::rewriteAttr2LabelTagProp(transformRes.value());
whereClauseCtx.filter = transformRes.value();

auto typeStatus = deduceExprType(whereClauseCtx.filter);
NG_RETURN_IF_ERROR(typeStatus);
Expand Down Expand Up @@ -364,7 +364,7 @@ Status MatchValidator::validateReturn(MatchReturn *ret,
ExpressionUtils::hasAny(column->expr(), {Expression::Kind::kAggregate})) {
retClauseCtx.yield->hasAgg_ = true;
}
column->setExpr(ExpressionUtils::rewriteAttr2LabelTagProp(column->expr()));
// column->setExpr(ExpressionUtils::rewriteAttr2LabelTagProp(column->expr()));
exprs.push_back(column->expr());
columns->addColumn(column->clone().release());
}
Expand Down Expand Up @@ -397,6 +397,7 @@ Status MatchValidator::validateAliases(
static const std::unordered_set<Expression::Kind> kinds = {Expression::Kind::kLabel,
Expression::Kind::kLabelAttribute,
Expression::Kind::kLabelTagProperty,
Expression::Kind::kAttribute,
// primitive props
Expression::Kind::kEdgeSrc,
Expression::Kind::kEdgeDst,
Expand Down Expand Up @@ -794,13 +795,25 @@ Status MatchValidator::checkAlias(
NG_RETURN_IF_ERROR(res);
return Status::OK();
}
case Expression::Kind::kLabelTagProperty: {
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("The type of `%s' must be tag", name.c_str());
// case Expression::Kind::kLabelTagProperty: {
// 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("The type of `%s' must be tag", name.c_str());
// }
// return Status::OK();
// }
case Expression::Kind::kAttribute: {
auto leftExpr = static_cast<const AttributeExpression *>(refExpr)->left();
if (leftExpr->kind() == Expression::Kind::kLabelAttribute) {
auto name = static_cast<const LabelAttributeExpression *>(leftExpr)->left()->name();
auto res = getAliasType(aliasesUsed, name);
NG_RETURN_IF_ERROR(res);
if (res.value() != AliasType::kNode) {
return Status::SemanticError("The type of `%s' should be tag", name.c_str());
}
}
return Status::OK();
}
Expand All @@ -810,7 +823,8 @@ Status MatchValidator::checkAlias(
NG_RETURN_IF_ERROR(res);
if (res.value() == AliasType::kNode) {
return Status::SemanticError(
"To get the property of the vertex, use the format `var.tag.prop'");
"To get the property of the vertex in `%s', should use the format `var.tag.prop'",
refExpr->toString().c_str());
}
return Status::OK();
}
Expand Down
4 changes: 2 additions & 2 deletions src/graph/visitor/DeducePropsVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ void DeducePropsVisitor::visit(TagPropertyExpression *expr) {
}

void DeducePropsVisitor::visit(LabelTagPropertyExpression *expr) {
auto status = qctx_->schemaMng()->toTagID(space_, expr->tag());
auto status = qctx_->schemaMng()->toTagID(space_, expr->sym());
if (!status.ok()) {
status_ = std::move(status).status();
return;
}
exprProps_->insertTagNameIds(expr->tag(), status.value());
exprProps_->insertTagNameIds(expr->sym(), status.value());
exprProps_->insertTagProp(status.value(), expr->prop());
}

Expand Down
6 changes: 3 additions & 3 deletions tests/tck/features/geo/GeoBase.feature
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ Feature: Geo base
MATCH (v:any_shape) RETURN ST_ASText(v.any_shape.geo);
"""
Then the result should be, in any order:
| ST_ASText(v.geo) |
| ST_ASText(v.any_shape.geo) |
| "POINT(3 8)" |
| "LINESTRING(3 8, 4.7 73.23)" |
| "POLYGON((0 1, 1 2, 2 3, 0 1))" |
Expand Down Expand Up @@ -474,10 +474,10 @@ Feature: Geo base
# Match with geo predicate
When executing query:
"""
MATCH (v:any_shape) WHERE ST_Intersects(v.geo, ST_GeogFromText('POINT(3 8)')) RETURN ST_ASText(v.geo);
MATCH (v:any_shape) WHERE ST_Intersects(v.any_shape.geo, ST_GeogFromText('POINT(3 8)')) RETURN ST_ASText(v.any_shape.geo);
"""
Then the result should be, in any order:
| ST_ASText(v.geo) |
| ST_ASText(v.any_shape.geo) |
| "POINT(3 8)" |
| "LINESTRING(3 8, 4.7 73.23)" |
# ST_Distance
Expand Down

0 comments on commit 26e04d3

Please sign in to comment.