-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/pattern expression ref local variable (#4498)
* Initial. * Get pattern expression value by expression. * Filter path by local variable. * Add tests. Co-authored-by: Sophie <[email protected]>
- Loading branch information
1 parent
2767a00
commit 02d6380
Showing
18 changed files
with
426 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) 2022 vesoft inc. All rights reserved. | ||
// | ||
// This source code is licensed under Apache 2.0 License. | ||
#include "graph/visitor/ValidatePatternExpressionVisitor.h" | ||
|
||
#include "ExprVisitorImpl.h" | ||
#include "graph/context/ValidateContext.h" | ||
|
||
namespace nebula { | ||
namespace graph { | ||
|
||
void ValidatePatternExpressionVisitor::visit(ListComprehensionExpression *expr) { | ||
DCHECK(ok()) << expr->toString(); | ||
// Store current available variables in expression | ||
localVariables_.push_front(expr->innerVar()); | ||
SCOPE_EXIT { | ||
localVariables_.pop_front(); | ||
}; | ||
ExprVisitorImpl::visit(expr); | ||
} | ||
|
||
void ValidatePatternExpressionVisitor::visit(MatchPathPatternExpression *expr) { | ||
DCHECK(ok()) << expr->toString(); | ||
// don't need to process sub-expression | ||
const auto &matchPath = expr->matchPath(); | ||
std::vector<Expression *> nodeFilters; | ||
auto *pathList = InputPropertyExpression::make(pool_, matchPath.toString()); | ||
auto listElementVar = vctx_->anonVarGen()->getVar(); | ||
for (std::size_t i = 0; i < matchPath.nodes().size(); ++i) { | ||
const auto &node = matchPath.nodes()[i]; | ||
if (!node->alias().empty()) { | ||
const auto find = std::find(localVariables_.begin(), localVariables_.end(), node->alias()); | ||
if (find != localVariables_.end()) { | ||
// TODO we should check variable is Node type | ||
// from local variable | ||
node->setVariableDefinedSource(MatchNode::VariableDefinedSource::kExpression); | ||
auto *listElement = VariableExpression::make(pool_, listElementVar); | ||
// Note: this require build path by node pattern order | ||
auto *listElementId = FunctionCallExpression::make( | ||
pool_, | ||
"_nodeid", | ||
{listElement, ConstantExpression::make(pool_, static_cast<int64_t>(i))}); | ||
auto *nodeValue = VariableExpression::make(pool_, node->alias()); | ||
auto *nodeId = FunctionCallExpression::make(pool_, "id", {nodeValue}); | ||
auto *equal = RelationalExpression::makeEQ(pool_, listElementId, nodeId); | ||
nodeFilters.emplace_back(equal); | ||
} | ||
} | ||
} | ||
if (!nodeFilters.empty()) { | ||
auto genList = ListComprehensionExpression::make( | ||
pool_, listElementVar, pathList, andAll(nodeFilters), nullptr); | ||
expr->setGenList(genList); | ||
} | ||
} | ||
|
||
Expression *ValidatePatternExpressionVisitor::andAll(const std::vector<Expression *> &exprs) const { | ||
CHECK(!exprs.empty()); | ||
if (exprs.size() == 1) { | ||
return exprs[0]; | ||
} | ||
auto *expr = exprs[0]; | ||
for (std::size_t i = 1; i < exprs.size(); ++i) { | ||
expr = LogicalExpression::makeAnd(pool_, expr, exprs[i]); | ||
} | ||
return expr; | ||
} | ||
|
||
} // namespace graph | ||
} // namespace nebula |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) 2022 vesoft inc. All rights reserved. | ||
// | ||
// This source code is licensed under Apache 2.0 License. | ||
|
||
#pragma once | ||
|
||
#include "common/expression/Expression.h" | ||
#include "graph/visitor/ExprVisitorImpl.h" | ||
|
||
namespace nebula { | ||
namespace graph { | ||
|
||
class ValidateContext; | ||
|
||
class ValidatePatternExpressionVisitor final : public ExprVisitorImpl { | ||
public: | ||
explicit ValidatePatternExpressionVisitor(ObjectPool *pool, ValidateContext *vctx) | ||
: pool_(pool), vctx_(vctx) {} | ||
|
||
bool ok() const override { | ||
// TODO: delete this interface | ||
return true; | ||
} | ||
|
||
private: | ||
using ExprVisitorImpl::visit; | ||
void visit(ConstantExpression *) override {} | ||
void visit(LabelExpression *) override {} | ||
void visit(UUIDExpression *) override {} | ||
void visit(VariableExpression *) override {} | ||
void visit(VersionedVariableExpression *) override {} | ||
void visit(TagPropertyExpression *) override {} | ||
void visit(LabelTagPropertyExpression *) override {} | ||
void visit(EdgePropertyExpression *) override {} | ||
void visit(InputPropertyExpression *) override {} | ||
void visit(VariablePropertyExpression *) override {} | ||
void visit(DestPropertyExpression *) override {} | ||
void visit(SourcePropertyExpression *) override {} | ||
void visit(EdgeSrcIdExpression *) override {} | ||
void visit(EdgeTypeExpression *) override {} | ||
void visit(EdgeRankExpression *) override {} | ||
void visit(EdgeDstIdExpression *) override {} | ||
void visit(VertexExpression *) override {} | ||
void visit(EdgeExpression *) override {} | ||
void visit(ColumnExpression *) override {} | ||
|
||
void visit(ListComprehensionExpression *expr) override; | ||
// match path pattern expression | ||
void visit(MatchPathPatternExpression *expr) override; | ||
|
||
Expression *andAll(const std::vector<Expression *> &exprs) const; | ||
|
||
private: | ||
ObjectPool *pool_{nullptr}; | ||
ValidateContext *vctx_{nullptr}; | ||
|
||
std::list<std::string> localVariables_; // local variable defined in List Comprehension | ||
}; | ||
|
||
} // namespace graph | ||
} // namespace nebula |
Oops, something went wrong.