Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/match path pattern expression #3970

1 change: 1 addition & 0 deletions src/codec/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(CODEC_TEST_LIBS
$<TARGET_OBJECTS:meta_obj>
$<TARGET_OBJECTS:process_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:function_manager_obj>
$<TARGET_OBJECTS:wkt_wkb_io_obj>
$<TARGET_OBJECTS:agg_function_manager_obj>
Expand Down
2 changes: 2 additions & 0 deletions src/common/datatypes/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ nebula_add_test(
OBJECTS
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:datatypes_obj>
$<TARGET_OBJECTS:function_manager_obj>
$<TARGET_OBJECTS:wkt_wkb_io_obj>
Expand Down Expand Up @@ -107,6 +108,7 @@ nebula_add_test(
OBJECTS
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:datatypes_obj>
$<TARGET_OBJECTS:wkt_wkb_io_obj>
$<TARGET_OBJECTS:function_manager_obj>
Expand Down
1 change: 1 addition & 0 deletions src/common/expression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ nebula_add_library(
PredicateExpression.cpp
ListComprehensionExpression.cpp
ReduceExpression.cpp
MatchPathPatternExpression.cpp
)

nebula_add_subdirectory(test)
3 changes: 3 additions & 0 deletions src/common/expression/ExprVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "common/expression/LabelExpression.h"
#include "common/expression/ListComprehensionExpression.h"
#include "common/expression/LogicalExpression.h"
#include "common/expression/MatchPathPatternExpression.h"
#include "common/expression/PathBuildExpression.h"
#include "common/expression/PredicateExpression.h"
#include "common/expression/PropertyExpression.h"
Expand Down Expand Up @@ -89,6 +90,8 @@ class ExprVisitor {
virtual void visit(ReduceExpression *expr) = 0;
// subscript range expression
virtual void visit(SubscriptRangeExpression *expr) = 0;
// match path pattern expression
virtual void visit(MatchPathPatternExpression *expr) = 0;
};

} // namespace nebula
Expand Down
7 changes: 7 additions & 0 deletions src/common/expression/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@ Expression* Expression::decode(ObjectPool* pool, Expression::Decoder& decoder) {
case Expression::Kind::kTSFuzzy: {
LOG(FATAL) << "Should not decode text search expression";
return exp;
}
case Expression::Kind::kMatchPathPattern: {
LOG(FATAL) << "Should not decode match path pattern expression.";
return exp;
}
// no default so the compiler will warning when lack
}
Expand Down Expand Up @@ -737,6 +741,9 @@ std::ostream& operator<<(std::ostream& os, Expression::Kind kind) {
case Expression::Kind::kReduce:
os << "Reduce";
break;
case Expression::Kind::kMatchPathPattern:
os << "MatchPathPattern";
break;
}
return os;
}
Expand Down
2 changes: 2 additions & 0 deletions src/common/expression/Expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class Expression {
kIsNotEmpty,

kSubscriptRange,

kMatchPathPattern,
};

Expression(ObjectPool* pool, Kind kind);
Expand Down
50 changes: 50 additions & 0 deletions src/common/expression/MatchPathPatternExpression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright (c) 2022 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#include "common/expression/MatchPathPatternExpression.h"

#include "common/expression/ExprVisitor.h"

namespace nebula {

const Value& MatchPathPatternExpression::eval(ExpressionContext& ctx) {
result_ = DCHECK_NOTNULL(prop_)->eval(ctx);
return result_;
}

bool MatchPathPatternExpression::operator==(const Expression& rhs) const {
if (kind() != rhs.kind()) {
return false;
}

if (matchPath_ != matchPath_) {
return false;
}

// The prop_ field is used for evaluation internally, so it don't identify the expression.
// We don't compare it here.
// Ditto for result_ field.

return true;
}

std::string MatchPathPatternExpression::toString() const {
return matchPath_->toString();
}

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

Expression* MatchPathPatternExpression::clone() const {
auto expr =
MatchPathPatternExpression::make(pool_, std::make_unique<MatchPath>(matchPath_->clone()));
if (prop_ != nullptr) {
expr->setInputProp(static_cast<InputPropertyExpression*>(prop_->clone()));
}
return expr;
}

} // namespace nebula
70 changes: 70 additions & 0 deletions src/common/expression/MatchPathPatternExpression.h
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.
*/

#ifndef COMMON_EXPRESSION_MATCHPATHPATTERNEXPRESSION_H_
#define COMMON_EXPRESSION_MATCHPATHPATTERNEXPRESSION_H_

#include "common/expression/Expression.h"
#include "parser/MatchSentence.h"

namespace nebula {

// For expression like (v:player)-[e:like]->(p)
// Evaluate to [[v, e, p], [v, e, p]...]
class MatchPathPatternExpression final : public Expression {
public:
MatchPathPatternExpression& operator=(const MatchPathPatternExpression& rhs) = delete;
MatchPathPatternExpression& operator=(MatchPathPatternExpression&&) = delete;

static MatchPathPatternExpression* make(ObjectPool* pool,
std::unique_ptr<MatchPath>&& matchPath) {
return pool->add(new MatchPathPatternExpression(pool, std::move(matchPath)));
}

const Value& eval(ExpressionContext& ctx) override;

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

std::string toString() const override;

void accept(ExprVisitor* visitor) override;

Expression* clone() const override;

// Evaluate expression by fetch result from input variable
void setInputProp(const std::string& prop) {
prop_ = InputPropertyExpression::make(pool_, prop);
}

void setInputProp(InputPropertyExpression* expr) {
prop_ = expr;
}

InputPropertyExpression* inputProp() const {
return prop_;
}

private:
explicit MatchPathPatternExpression(ObjectPool* pool, std::unique_ptr<MatchPath>&& matchPath)
: Expression(pool, Kind::kMatchPathPattern), matchPath_(std::move(matchPath)) {}

// This expression contains variable implicitly, so we don't support persist or transform it.
void writeTo(Encoder&) const override {
LOG(FATAL) << "Not implemented";
}

// This expression contains variable implicitly, so we don't support persist or transform it.
void resetFrom(Decoder&) override {
LOG(FATAL) << "Not implemented";
}

private:
std::unique_ptr<MatchPath> matchPath_;
InputPropertyExpression* prop_{
nullptr}; // The column of input stored the result of the expression
Value result_;
};
} // namespace nebula
#endif // COMMON_EXPRESSION_MATCHPATHPATTERNEXPRESSION_H_
4 changes: 4 additions & 0 deletions src/common/expression/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

set(expression_test_common_libs
$<TARGET_OBJECTS:parser_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:network_obj>
$<TARGET_OBJECTS:fs_obj>
$<TARGET_OBJECTS:stats_obj>
Expand Down Expand Up @@ -117,6 +119,7 @@ nebula_add_executable(
FunctionCallExpressionBenchmark.cpp
OBJECTS
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:datatypes_obj>
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:expr_ctx_mock_obj>
Expand All @@ -140,6 +143,7 @@ nebula_add_executable(
AggregateExpressionBenchmark.cpp
OBJECTS
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:datatypes_obj>
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:expr_ctx_mock_obj>
Expand Down
2 changes: 2 additions & 0 deletions src/common/id/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ nebula_add_executable(
$<TARGET_OBJECTS:fs_obj>
$<TARGET_OBJECTS:meta_client_stats_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
LIBRARIES
follybenchmark
boost_regex
Expand Down Expand Up @@ -72,6 +73,7 @@ nebula_add_test(
$<TARGET_OBJECTS:fs_obj>
$<TARGET_OBJECTS:meta_client_stats_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
LIBRARIES
gtest
gtest_main
Expand Down
3 changes: 3 additions & 0 deletions src/common/utils/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ nebula_add_test(
$<TARGET_OBJECTS:storage_thrift_obj>
$<TARGET_OBJECTS:geo_index_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:function_manager_obj>
$<TARGET_OBJECTS:agg_function_manager_obj>
$<TARGET_OBJECTS:time_utils_obj>
Expand All @@ -39,6 +40,7 @@ nebula_add_test(
$<TARGET_OBJECTS:storage_thrift_obj>
$<TARGET_OBJECTS:geo_index_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:function_manager_obj>
$<TARGET_OBJECTS:agg_function_manager_obj>
$<TARGET_OBJECTS:time_utils_obj>
Expand Down Expand Up @@ -67,6 +69,7 @@ nebula_add_test(
$<TARGET_OBJECTS:storage_thrift_obj>
$<TARGET_OBJECTS:geo_index_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:function_manager_obj>
$<TARGET_OBJECTS:agg_function_manager_obj>
$<TARGET_OBJECTS:time_utils_obj>
Expand Down
3 changes: 3 additions & 0 deletions src/daemons/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(common_deps
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:memory_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:function_manager_obj>
$<TARGET_OBJECTS:agg_function_manager_obj>
$<TARGET_OBJECTS:time_utils_obj>
Expand Down Expand Up @@ -125,6 +126,7 @@ nebula_add_executable(
$<TARGET_OBJECTS:graph_session_obj>
$<TARGET_OBJECTS:query_engine_obj>
$<TARGET_OBJECTS:parser_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:validator_obj>
$<TARGET_OBJECTS:expr_visitor_obj>
$<TARGET_OBJECTS:optimizer_obj>
Expand Down Expand Up @@ -209,6 +211,7 @@ nebula_add_executable(
$<TARGET_OBJECTS:graph_session_obj>
$<TARGET_OBJECTS:query_engine_obj>
$<TARGET_OBJECTS:parser_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:validator_obj>
$<TARGET_OBJECTS:expr_visitor_obj>
$<TARGET_OBJECTS:optimizer_obj>
Expand Down
2 changes: 2 additions & 0 deletions src/graph/context/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SET(CONTEXT_TEST_LIBS
$<TARGET_OBJECTS:charset_obj>
$<TARGET_OBJECTS:datatypes_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:function_manager_obj>
$<TARGET_OBJECTS:wkt_wkb_io_obj>
$<TARGET_OBJECTS:agg_function_manager_obj>
Expand Down Expand Up @@ -34,6 +35,7 @@ SET(CONTEXT_TEST_LIBS
$<TARGET_OBJECTS:graph_context_obj>
$<TARGET_OBJECTS:expr_visitor_obj>
$<TARGET_OBJECTS:parser_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:graph_flags_obj>
$<TARGET_OBJECTS:graph_auth_obj>
$<TARGET_OBJECTS:graph_session_obj>
Expand Down
2 changes: 2 additions & 0 deletions src/graph/executor/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
SET(EXEC_QUERY_TEST_OBJS
$<TARGET_OBJECTS:ws_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:network_obj>
$<TARGET_OBJECTS:process_obj>
$<TARGET_OBJECTS:graph_thrift_obj>
Expand Down Expand Up @@ -39,6 +40,7 @@ SET(EXEC_QUERY_TEST_OBJS
$<TARGET_OBJECTS:graph_session_obj>
$<TARGET_OBJECTS:graph_flags_obj>
$<TARGET_OBJECTS:parser_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:validator_obj>
$<TARGET_OBJECTS:planner_obj>
$<TARGET_OBJECTS:plan_obj>
Expand Down
2 changes: 2 additions & 0 deletions src/graph/optimizer/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(OPTIMIZER_TEST_LIB
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:datatypes_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:function_manager_obj>
$<TARGET_OBJECTS:wkt_wkb_io_obj>
$<TARGET_OBJECTS:agg_function_manager_obj>
Expand Down Expand Up @@ -40,6 +41,7 @@ set(OPTIMIZER_TEST_LIB
$<TARGET_OBJECTS:planner_obj>
$<TARGET_OBJECTS:plan_obj>
$<TARGET_OBJECTS:parser_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:graph_context_obj>
$<TARGET_OBJECTS:validator_obj>
$<TARGET_OBJECTS:optimizer_obj>
Expand Down
2 changes: 2 additions & 0 deletions src/graph/planner/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ nebula_add_test(
OBJECTS
$<TARGET_OBJECTS:conf_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:http_client_obj>
$<TARGET_OBJECTS:network_obj>
$<TARGET_OBJECTS:process_obj>
Expand Down Expand Up @@ -52,6 +53,7 @@ nebula_add_test(
$<TARGET_OBJECTS:graph_session_obj>
${EXEC_PLAN_TEST_FLAG_DEPS}
$<TARGET_OBJECTS:parser_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:validator_obj>
$<TARGET_OBJECTS:expr_visitor_obj>
$<TARGET_OBJECTS:planner_obj>
Expand Down
2 changes: 2 additions & 0 deletions src/graph/util/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ nebula_add_test(
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:datatypes_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:function_manager_obj>
$<TARGET_OBJECTS:wkt_wkb_io_obj>
$<TARGET_OBJECTS:agg_function_manager_obj>
Expand Down Expand Up @@ -60,6 +61,7 @@ nebula_add_test(
$<TARGET_OBJECTS:util_obj>
$<TARGET_OBJECTS:plan_obj>
$<TARGET_OBJECTS:parser_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:graph_context_obj>
$<TARGET_OBJECTS:memory_obj>
$<TARGET_OBJECTS:version_obj>
Expand Down
1 change: 1 addition & 0 deletions src/graph/validator/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(VALIDATOR_TEST_LIBS
$<TARGET_OBJECTS:plan_obj>
$<TARGET_OBJECTS:graph_flags_obj>
$<TARGET_OBJECTS:parser_obj>
$<TARGET_OBJECTS:ast_match_path_obj>
$<TARGET_OBJECTS:idgenerator_obj>
$<TARGET_OBJECTS:graph_context_obj>
$<TARGET_OBJECTS:graph_auth_obj>
Expand Down
5 changes: 5 additions & 0 deletions src/graph/visitor/DeduceTypeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,11 @@ void DeduceTypeVisitor::visitVertexPropertyExpr(PropertyExpression *expr) {
void DeduceTypeVisitor::visit(PathBuildExpression *) {
type_ = Value::Type::PATH;
}

void DeduceTypeVisitor::visit(MatchPathPatternExpression *) {
type_ = Value::Type::LIST;
}

#undef DETECT_NARYEXPR_TYPE
#undef DETECT_UNARYEXPR_TYPE
#undef DETECT_BIEXPR_TYPE
Expand Down
2 changes: 2 additions & 0 deletions src/graph/visitor/DeduceTypeVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class DeduceTypeVisitor final : public ExprVisitor {
void visit(ReduceExpression *expr) override;
// subscript range
void visit(SubscriptRangeExpression *expr) override;
// match path pattern expression
void visit(MatchPathPatternExpression *expr) override;

void visitVertexPropertyExpr(PropertyExpression *expr);

Expand Down
Loading