Skip to content

Commit

Permalink
support cypher parameter
Browse files Browse the repository at this point in the history
fix aysn block

revert nebula-python url

disable homonymic parameter variable

support sdk interface of map/list

support go from parameter

support list and map

fix tck

support fetch

small fix for point initialization

fix tck

add tck

support subgraph

support find path

small change
  • Loading branch information
czpmango committed Oct 12, 2021
1 parent d8e5539 commit 86116aa
Show file tree
Hide file tree
Showing 51 changed files with 647 additions and 36 deletions.
1 change: 1 addition & 0 deletions src/common/expression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ nebula_add_library(
PredicateExpression.cpp
ListComprehensionExpression.cpp
ReduceExpression.cpp
ParameterExpression.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 @@ -21,6 +21,7 @@
#include "common/expression/LabelExpression.h"
#include "common/expression/ListComprehensionExpression.h"
#include "common/expression/LogicalExpression.h"
#include "common/expression/ParameterExpression.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;
// parameter expression
virtual void visit(ParameterExpression *expr) = 0;
};

} // namespace nebula
Expand Down
9 changes: 9 additions & 0 deletions src/common/expression/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "common/expression/LabelExpression.h"
#include "common/expression/ListComprehensionExpression.h"
#include "common/expression/LogicalExpression.h"
#include "common/expression/ParameterExpression.h"
#include "common/expression/PathBuildExpression.h"
#include "common/expression/PredicateExpression.h"
#include "common/expression/PropertyExpression.h"
Expand Down Expand Up @@ -497,6 +498,11 @@ Expression* Expression::decode(ObjectPool* pool, Expression::Decoder& decoder) {
exp->resetFrom(decoder);
return exp;
}
case Expression::Kind::kParam: {
exp = ParameterExpression::make(pool);
exp->resetFrom(decoder);
return exp;
}
case Expression::Kind::kTSPrefix:
case Expression::Kind::kTSWildcard:
case Expression::Kind::kTSRegexp:
Expand Down Expand Up @@ -719,6 +725,9 @@ std::ostream& operator<<(std::ostream& os, Expression::Kind kind) {
case Expression::Kind::kReduce:
os << "Reduce";
break;
case Expression::Kind::kParam:
os << "Parameter";
break;
}
return os;
}
Expand Down
1 change: 1 addition & 0 deletions src/common/expression/Expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class Expression {
kIsNotEmpty,

kSubscriptRange,
kParam,
};

Expression(ObjectPool* pool, Kind kind);
Expand Down
34 changes: 34 additions & 0 deletions src/common/expression/ParameterExpression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* 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.
*/

#include "common/expression/ParameterExpression.h"

#include "common/expression/ExprVisitor.h"

namespace nebula {

const Value& ParameterExpression::eval(ExpressionContext& ectx) { return ectx.getVar(name_); }

std::string ParameterExpression::toString() const { return '$' + name_; }

bool ParameterExpression::operator==(const Expression& rhs) const {
if (kind_ != rhs.kind()) {
return false;
}
const auto& expr = static_cast<const ParameterExpression&>(rhs);
return name_ == expr.name();
}

void ParameterExpression::writeTo(Encoder& encoder) const {
encoder << kind_;
encoder << name_;
}

void ParameterExpression::resetFrom(Decoder& decoder) { name_ = decoder.readStr(); }

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

} // namespace nebula
47 changes: 47 additions & 0 deletions src/common/expression/ParameterExpression.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* 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.
*/

#pragma once

#include "common/expression/Expression.h"

// The ParameterExpression use for parameterized statement
namespace nebula {

class ParameterExpression : public Expression {
public:
ParameterExpression& operator=(const ParameterExpression& rhs) = delete;
ParameterExpression& operator=(ParameterExpression&&) = delete;

static ParameterExpression* make(ObjectPool* pool, const std::string& name = "") {
return pool->add(new ParameterExpression(pool, name));
}

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

const Value& eval(ExpressionContext& ctx) override;

const std::string& name() const { return name_; }

std::string toString() const override;

void accept(ExprVisitor* visitor) override;

Expression* clone() const override { return ParameterExpression::make(pool_, name()); }

protected:
explicit ParameterExpression(ObjectPool* pool, const std::string& name = "")
: Expression(pool, Kind::kParam), name_(name) {}

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

protected:
std::string name_;
Value result_;
};

} // namespace nebula
19 changes: 19 additions & 0 deletions src/common/expression/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,25 @@ nebula_add_test(
${THRIFT_LIBRARIES}
)

nebula_add_test(
NAME param_expression_test
SOURCES ParameterExpressionTest.cpp
OBJECTS
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:datatypes_obj>
$<TARGET_OBJECTS:wkt_wkb_io_obj>
$<TARGET_OBJECTS:expr_ctx_mock_obj>
$<TARGET_OBJECTS:function_manager_obj>
$<TARGET_OBJECTS:agg_function_manager_obj>
$<TARGET_OBJECTS:time_obj>
$<TARGET_OBJECTS:time_utils_obj>
$<TARGET_OBJECTS:fs_obj>
LIBRARIES
gtest
${THRIFT_LIBRARIES}
)

nebula_add_test(
NAME list_comprehension_expression_test
SOURCES ListComprehensionExpressionTest.cpp
Expand Down
6 changes: 6 additions & 0 deletions src/common/expression/test/EncodeDecodeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ TEST(ExpressionEncodeDecode, LabelExpression) {
ASSERT_EQ(*origin, *decoded);
}

TEST(ExpressionEncodeDecode, ParameterExpression) {
auto origin = ParameterExpression::make(&pool, "p1");
auto decoded = Expression::decode(&pool, Expression::encode(*origin));
ASSERT_EQ(*origin, *decoded);
}

TEST(ExpressionEncodeDecode, CaseExpression) {
{
// CASE 23 WHEN 24 THEN 1 END
Expand Down
2 changes: 2 additions & 0 deletions src/common/expression/test/ExpressionContextMock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ std::unordered_map<std::string, Value> ExpressionContextMock::vals_ = {
{"path_edge2", Value(Edge("2", "3", 1, "edge", 0, {}))},
{"path_v2", Value(Vertex("3", {}))},
{"path_edge3", Value(Edge("3", "4", 1, "edge", 0, {}))},
{"param1", Value(1)},
{"param2", Value(List(std::vector<Value>{1, 2, 3, 4, 5, 6, 7, 8}))},
};

Value ExpressionContextMock::getColumn(int32_t index) const {
Expand Down
31 changes: 31 additions & 0 deletions src/common/expression/test/ParameterExpressionTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* 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.
*/
#include "common/expression/test/TestBase.h"

namespace nebula {

class ParameterExpressionTest : public ExpressionTest {};

TEST_F(ParameterExpressionTest, ParamExprToString) {
auto expr = ParameterExpression::make(&pool, "param1");
ASSERT_EQ("$param1", expr->toString());
}

TEST_F(ParameterExpressionTest, ParamEvaluate) {
auto expr = ParameterExpression::make(&pool, "param1");
auto value = Expression::eval(expr, gExpCtxt);
ASSERT_TRUE(value.isInt());
ASSERT_EQ(1, value.getInt());
}
} // namespace nebula

int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
folly::init(&argc, &argv, true);
google::SetStderrLogging(google::INFO);

return RUN_ALL_TESTS();
}
1 change: 1 addition & 0 deletions src/common/expression/test/TestBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "common/expression/LabelExpression.h"
#include "common/expression/ListComprehensionExpression.h"
#include "common/expression/LogicalExpression.h"
#include "common/expression/ParameterExpression.h"
#include "common/expression/PathBuildExpression.h"
#include "common/expression/PredicateExpression.h"
#include "common/expression/PropertyExpression.h"
Expand Down
6 changes: 6 additions & 0 deletions src/graph/context/QueryContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ void QueryContext::init() {
objPool_ = std::make_unique<ObjectPool>();
ep_ = std::make_unique<ExecutionPlan>();
ectx_ = std::make_unique<ExecutionContext>();
// copy parameterMap into ExecutionContext
if (rctx_) {
for (auto item : rctx_->parameterMap()) {
ectx_->setValue(std::move(item.first), std::move(item.second));
}
}
idGen_ = std::make_unique<IdGenerator>(0);
symTable_ = std::make_unique<SymbolTable>(objPool_.get());
vctx_ = std::make_unique<ValidateContext>(std::make_unique<AnonVarGenerator>(symTable_.get()));
Expand Down
4 changes: 4 additions & 0 deletions src/graph/context/QueryContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ class QueryContext {

bool isKilled() const { return killed_.load(); }

bool existParameter(const std::string& param) const {
return ectx_->exist(param) && (ectx_->getValue(param).type() != Value::Type::DATASET);
}

private:
void init();

Expand Down
2 changes: 1 addition & 1 deletion src/graph/context/QueryExpressionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class QueryExpressionContext final : public ExpressionContext {

void setVar(const std::string&, Value val) override;

QueryExpressionContext& operator()(Iterator* iter) {
QueryExpressionContext& operator()(Iterator* iter = nullptr) {
iter_ = iter;
return *this;
}
Expand Down
2 changes: 2 additions & 0 deletions src/graph/context/ast/QueryAstContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ enum FromType {
kInstantExpr,
kVariable,
kPipe,
kParam,
};

struct Starts {
Expand All @@ -27,6 +28,7 @@ struct Starts {
Expression* originalSrc{nullptr};
std::string userDefinedVarName;
std::string runtimeVidName;
Expression* runtimeVid{nullptr};
std::vector<Value> vids;
};

Expand Down
8 changes: 6 additions & 2 deletions src/graph/planner/ngql/FetchVerticesPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ StatusOr<SubPlan> FetchVerticesPlanner::transform(AstContext* astCtx) {
auto& starts = fetchCtx_->from;

std::string vidsVar;
if (!starts.vids.empty() && starts.originalSrc == nullptr) {
if (starts.runtimeVid != nullptr) {
starts.src = starts.runtimeVid;
} else if (!starts.vids.empty() && starts.originalSrc == nullptr) {
PlannerUtil::buildConstantInput(qctx, starts, vidsVar);
} else {
starts.src = starts.originalSrc;
Expand All @@ -55,7 +57,9 @@ StatusOr<SubPlan> FetchVerticesPlanner::transform(AstContext* astCtx) {
buildVertexProps(fetchCtx_->exprProps.tagProps()),
{},
fetchCtx_->distinct);
getVertices->setInputVar(vidsVar);
if (!vidsVar.empty()) {
getVertices->setInputVar(vidsVar);
}

subPlan.root = Project::make(qctx, getVertices, fetchCtx_->yieldExpr);
if (fetchCtx_->distinct) {
Expand Down
7 changes: 5 additions & 2 deletions src/graph/planner/ngql/GoPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,9 @@ SubPlan GoPlanner::oneStepPlan(SubPlan& startVidPlan) {
gn->setVertexProps(buildVertexProps(goCtx_->exprProps.srcTagProps()));
gn->setEdgeProps(buildEdgeProps(false));
gn->setSrc(goCtx_->from.src);
gn->setInputVar(goCtx_->vidsVar);
if (!goCtx_->vidsVar.empty()) {
gn->setInputVar(goCtx_->vidsVar);
}

auto* sampleLimit = buildSampleLimit(gn, 1 /* one step */);

Expand Down Expand Up @@ -533,7 +535,8 @@ SubPlan GoPlanner::mToNStepsPlan(SubPlan& startVidPlan) {
StatusOr<SubPlan> GoPlanner::transform(AstContext* astCtx) {
goCtx_ = static_cast<GoContext*>(astCtx);
auto qctx = goCtx_->qctx;
goCtx_->joinInput = goCtx_->from.fromType != FromType::kInstantExpr;
auto fromType = goCtx_->from.fromType;
goCtx_->joinInput = fromType != FromType::kInstantExpr && fromType != FromType::kParam;
goCtx_->joinDst = !goCtx_->exprProps.dstTagProps().empty();

SubPlan startPlan = PlannerUtil::buildStart(qctx, goCtx_->from, goCtx_->vidsVar);
Expand Down
5 changes: 5 additions & 0 deletions src/graph/planner/ngql/PathPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ void PathPlanner::doBuildEdgeProps(std::unique_ptr<std::vector<EdgeProp>>& edgeP

void PathPlanner::buildStart(Starts& starts, std::string& vidsVar, bool reverse) {
auto qctx = pathCtx_->qctx;
auto runtimeVid = starts.runtimeVid;
if (runtimeVid) {
auto vid = runtimeVid->eval(QueryExpressionContext(qctx->ectx())());
starts.vids.emplace_back(std::move(vid));
}
if (!starts.vids.empty() && starts.originalSrc == nullptr) {
PlannerUtil::buildConstantInput(qctx, starts, vidsVar);
} else {
Expand Down
8 changes: 6 additions & 2 deletions src/graph/planner/ngql/SubgraphPlanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ StatusOr<SubPlan> SubgraphPlanner::nSteps(SubPlan& startVidPlan, const std::stri
gn->setSrc(subgraphCtx_->from.src);
gn->setVertexProps(std::move(vertexProps).value());
gn->setEdgeProps(std::move(edgeProps).value());
gn->setInputVar(input);
if (!input.empty()) {
gn->setInputVar(input);
}

auto oneMoreStepOutput = qctx->vctx()->anonVarGen()->getVar();
auto loopSteps = qctx->vctx()->anonVarGen()->getVar();
subgraphCtx_->loopSteps = loopSteps;
auto* subgraph = Subgraph::make(qctx, gn, oneMoreStepOutput, loopSteps, steps.steps() + 1);
subgraph->setOutputVar(input);
if (!input.empty()) {
subgraph->setOutputVar(input);
}
subgraph->setColNames({nebula::kVid});

auto* condition = loopCondition(steps.steps() + 1, gn->outputVar());
Expand Down
Loading

0 comments on commit 86116aa

Please sign in to comment.