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

fix(interactive): Add error handling for expression parsing and support get global id for edge #4228

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions flex/engines/graph_db/runtime/adhoc/expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
namespace gs {

namespace runtime {

Expr::Expr(const ReadTransaction& txn, const Context& ctx,
const std::map<std::string, std::string>& params,
const common::Expression& expr, VarType var_type) {
expr_ = parse_expression(txn, ctx, params, expr, var_type);
Expr::Expr() : expr_(nullptr) {}

bl::result<Expr> Expr::MakeExpr(
const ReadTransaction& txn, const Context& ctx,
const std::map<std::string, std::string>& params,
const common::Expression& expr, VarType var_type) {
Expr expression;
BOOST_LEAF_ASSIGN(expression.expr_,
parse_expression(txn, ctx, params, expr, var_type));
return expression;
}

RTAny Expr::eval_path(size_t idx) const {
Expand Down
9 changes: 6 additions & 3 deletions flex/engines/graph_db/runtime/adhoc/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "flex/engines/graph_db/database/read_transaction.h"

#include "flex/engines/graph_db/runtime/adhoc/expr_impl.h"
#include "flex/engines/graph_db/runtime/common/leaf_utils.h"
#include "flex/engines/graph_db/runtime/common/rt_any.h"

namespace gs {
Expand All @@ -26,9 +27,11 @@ namespace runtime {

class Expr {
public:
Expr(const ReadTransaction& txn, const Context& ctx,
const std::map<std::string, std::string>& params,
const common::Expression& expr, VarType var_type);
Expr();
static bl::result<Expr> MakeExpr(
const ReadTransaction& txn, const Context& ctx,
const std::map<std::string, std::string>& params,
const common::Expression& expr, VarType var_type);

RTAny eval_path(size_t idx) const;
RTAny eval_vertex(label_t label, vid_t v, size_t idx) const;
Expand Down
67 changes: 41 additions & 26 deletions flex/engines/graph_db/runtime/adhoc/expr_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ namespace gs {

namespace runtime {

VariableExpr::VariableExpr(const ReadTransaction& txn, const Context& ctx,
const common::Variable& pb, VarType var_type)
: var_(txn, ctx, pb, var_type) {}
VariableExpr::VariableExpr() {}

bl::result<std::unique_ptr<VariableExpr>> VariableExpr::MakeVariableExpr(
const ReadTransaction& txn, const Context& ctx, const common::Variable& pb,
VarType var_type) {
std::unique_ptr<VariableExpr> expr(new VariableExpr());
BOOST_LEAF_ASSIGN(expr->var_, Var::MakeVar(txn, ctx, pb, var_type));
return expr;
}
RTAny VariableExpr::eval_path(size_t idx) const { return var_.get(idx); }
RTAny VariableExpr::eval_vertex(label_t label, vid_t v, size_t idx) const {
return var_.get_vertex(label, v, idx);
Expand Down Expand Up @@ -512,11 +518,18 @@ static inline int get_proiority(const common::ExprOpr& opr) {
}
return 16;
}
static std::unique_ptr<ExprBase> parse_expression_impl(
static bl::result<std::unique_ptr<ExprBase>> parse_expression_impl(
const ReadTransaction& txn, const Context& ctx,
const std::map<std::string, std::string>& params,
const common::Expression& expr, VarType var_type);
static std::unique_ptr<ExprBase> build_expr(

bl::result<std::unique_ptr<VariableExpr>> make_variable_expr(
const ReadTransaction& txn, const Context& ctx, const common::Variable& pb,
VarType var_type) {
return VariableExpr::MakeVariableExpr(txn, ctx, pb, var_type);
}

static bl::result<std::unique_ptr<ExprBase>> build_expr(
const ReadTransaction& txn, const Context& ctx,
const std::map<std::string, std::string>& params,
std::stack<common::ExprOpr>& opr_stack, VarType var_type) {
Expand All @@ -535,7 +548,7 @@ static std::unique_ptr<ExprBase> build_expr(
return std::make_unique<ConstExpr>(parse_param(opr.param(), params));
}
case common::ExprOpr::kVar: {
return std::make_unique<VariableExpr>(txn, ctx, opr.var(), var_type);
return make_variable_expr(txn, ctx, opr.var(), var_type);
}
case common::ExprOpr::kLogical: {
if (opr.logical() == common::Logical::WITHIN) {
Expand All @@ -545,8 +558,7 @@ static std::unique_ptr<ExprBase> build_expr(
opr_stack.pop();
CHECK(lhs.has_var());
CHECK(rhs.has_const_());
auto key =
std::make_unique<VariableExpr>(txn, ctx, lhs.var(), var_type);
BOOST_LEAF_AUTO(key, make_variable_expr(txn, ctx, lhs.var(), var_type));
if (key->type() == RTAnyType::kI64Value) {
return std::make_unique<WithInExpr<int64_t>>(txn, ctx, std::move(key),
rhs.const_());
Expand All @@ -561,20 +573,20 @@ static std::unique_ptr<ExprBase> build_expr(
}
} else if (opr.logical() == common::Logical::NOT ||
opr.logical() == common::Logical::ISNULL) {
auto lhs = build_expr(txn, ctx, params, opr_stack, var_type);
BOOST_LEAF_AUTO(lhs, build_expr(txn, ctx, params, opr_stack, var_type));
return std::make_unique<UnaryLogicalExpr>(std::move(lhs),
opr.logical());
} else {
auto lhs = build_expr(txn, ctx, params, opr_stack, var_type);
auto rhs = build_expr(txn, ctx, params, opr_stack, var_type);
BOOST_LEAF_AUTO(lhs, build_expr(txn, ctx, params, opr_stack, var_type));
BOOST_LEAF_AUTO(rhs, build_expr(txn, ctx, params, opr_stack, var_type));
return std::make_unique<LogicalExpr>(std::move(lhs), std::move(rhs),
opr.logical());
}
break;
}
case common::ExprOpr::kArith: {
auto lhs = build_expr(txn, ctx, params, opr_stack, var_type);
auto rhs = build_expr(txn, ctx, params, opr_stack, var_type);
BOOST_LEAF_AUTO(lhs, build_expr(txn, ctx, params, opr_stack, var_type));
BOOST_LEAF_AUTO(rhs, build_expr(txn, ctx, params, opr_stack, var_type));
return std::make_unique<ArithExpr>(std::move(lhs), std::move(rhs),
opr.arith());
}
Expand All @@ -587,25 +599,29 @@ static std::unique_ptr<ExprBase> build_expr(
for (size_t i = 0; i < len; ++i) {
auto when_expr = op.when_then_expressions(i).when_expression();
auto then_expr = op.when_then_expressions(i).then_result_expression();
when_then_exprs.emplace_back(
parse_expression_impl(txn, ctx, params, when_expr, var_type),
parse_expression_impl(txn, ctx, params, then_expr, var_type));
BOOST_LEAF_AUTO(
when, parse_expression_impl(txn, ctx, params, when_expr, var_type));
BOOST_LEAF_AUTO(
then, parse_expression_impl(txn, ctx, params, then_expr, var_type));
when_then_exprs.emplace_back(std::move(when), std::move(then));
}
auto else_expr = parse_expression_impl(
txn, ctx, params, op.else_result_expression(), var_type);
BOOST_LEAF_AUTO(else_expr, parse_expression_impl(
txn, ctx, params,
op.else_result_expression(), var_type));
return std::make_unique<CaseWhenExpr>(std::move(when_then_exprs),
std::move(else_expr));
}
case common::ExprOpr::kExtract: {
auto hs = build_expr(txn, ctx, params, opr_stack, var_type);
BOOST_LEAF_AUTO(hs, build_expr(txn, ctx, params, opr_stack, var_type));
return std::make_unique<ExtractExpr>(std::move(hs), opr.extract());
}
case common::ExprOpr::kVars: {
auto op = opr.vars();
std::vector<std::unique_ptr<ExprBase>> exprs;
for (int i = 0; i < op.keys_size(); ++i) {
exprs.push_back(
std::make_unique<VariableExpr>(txn, ctx, op.keys(i), var_type));
BOOST_LEAF_AUTO(expr,
make_variable_expr(txn, ctx, op.keys(i), var_type));
exprs.push_back(std::move(expr));
}
return std::make_unique<TupleExpr>(std::move(exprs));
// LOG(FATAL) << "not support" << opr.DebugString();
Expand All @@ -624,9 +640,8 @@ static std::unique_ptr<ExprBase> build_expr(
auto str = any.as_string();
keys_vec.push_back(std::string(str));
}
exprs.emplace_back(
std::make_unique<VariableExpr>(txn, ctx, val,
var_type)); // just for parse
BOOST_LEAF_AUTO(expr, make_variable_expr(txn, ctx, val, var_type));
exprs.push_back(std::move(expr));
}
if (exprs.size() > 0) {
return std::make_unique<MapExpr>(std::move(keys_vec), std::move(exprs));
Expand All @@ -640,7 +655,7 @@ static std::unique_ptr<ExprBase> build_expr(
}
return nullptr;
}
static std::unique_ptr<ExprBase> parse_expression_impl(
static bl::result<std::unique_ptr<ExprBase>> parse_expression_impl(
const ReadTransaction& txn, const Context& ctx,
const std::map<std::string, std::string>& params,
const common::Expression& expr, VarType var_type) {
Expand Down Expand Up @@ -712,7 +727,7 @@ static std::unique_ptr<ExprBase> parse_expression_impl(
}
return build_expr(txn, ctx, params, opr_stack2, var_type);
}
std::unique_ptr<ExprBase> parse_expression(
bl::result<std::unique_ptr<ExprBase>> parse_expression(
const ReadTransaction& txn, const Context& ctx,
const std::map<std::string, std::string>& params,
const common::Expression& expr, VarType var_type) {
Expand Down
9 changes: 6 additions & 3 deletions flex/engines/graph_db/runtime/adhoc/expr_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "flex/proto_generated_gie/expr.pb.h"

#include "flex/engines/graph_db/runtime/adhoc/var.h"
#include "flex/engines/graph_db/runtime/common/leaf_utils.h"
#include "flex/engines/graph_db/runtime/common/rt_any.h"

namespace gs {
Expand Down Expand Up @@ -171,8 +172,10 @@ class WithInExpr : public ExprBase {

class VariableExpr : public ExprBase {
public:
VariableExpr(const ReadTransaction& txn, const Context& ctx,
const common::Variable& pb, VarType var_type);
VariableExpr();
static bl::result<std::unique_ptr<VariableExpr>> MakeVariableExpr(
const ReadTransaction& txn, const Context& ctx,
const common::Variable& pb, VarType var_type);

RTAny eval_path(size_t idx) const override;
RTAny eval_vertex(label_t label, vid_t v, size_t idx) const override;
Expand Down Expand Up @@ -399,7 +402,7 @@ class MapExpr : public ExprBase {
std::vector<std::unique_ptr<ExprBase>> value_exprs;
mutable std::vector<std::vector<RTAny>> values;
};
std::unique_ptr<ExprBase> parse_expression(
bl::result<std::unique_ptr<ExprBase>> parse_expression(
const ReadTransaction& txn, const Context& ctx,
const std::map<std::string, std::string>& params,
const common::Expression& expr, VarType var_type);
Expand Down
2 changes: 1 addition & 1 deletion flex/engines/graph_db/runtime/adhoc/operators/dedup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bl::result<Context> eval_dedup(const algebra::Dedup& opr,
tag = key.tag().id();
}
if (key.has_property()) {
Var var(txn, ctx, key, VarType::kPathVar);
BOOST_LEAF_AUTO(var, Var::MakeVar(txn, ctx, key, VarType::kPathVar));
vars.emplace_back([var](size_t i) { return var.get(i); });
flag = true;
} else {
Expand Down
3 changes: 2 additions & 1 deletion flex/engines/graph_db/runtime/adhoc/operators/edge_expand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ bl::result<Context> eval_edge_expand(
eep.dir = dir;
eep.alias = alias;

GeneralEdgePredicate pred(txn, ctx, params, query_params.predicate());
BOOST_LEAF_AUTO(pred, GeneralEdgePredicate::MakeGeneralEdgePredicate(
txn, ctx, params, query_params.predicate()));

return EdgeExpand::expand_edge(txn, std::move(ctx), eep, pred);
} else {
Expand Down
3 changes: 2 additions & 1 deletion flex/engines/graph_db/runtime/adhoc/operators/get_v.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ bl::result<Context> eval_get_v(
p.tables = parse_tables(query_params);
p.alias = alias;
if (query_params.has_predicate()) {
GeneralVertexPredicate pred(txn, ctx, params, query_params.predicate());
BOOST_LEAF_AUTO(pred, GeneralVertexPredicate::MakeGeneralVertexPredicate(
txn, ctx, params, query_params.predicate()));

if (opt == VOpt::kItself) {
return GetV::get_vertex_from_vertices(txn, std::move(ctx), p, pred);
Expand Down
40 changes: 27 additions & 13 deletions flex/engines/graph_db/runtime/adhoc/operators/group_by.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,25 @@ AggrKind parse_aggregate(physical::GroupBy_AggFunc::Aggregate v) {
}

struct AggFunc {
AggFunc(const physical::GroupBy_AggFunc& opr, const ReadTransaction& txn,
const Context& ctx)
: aggregate(parse_aggregate(opr.aggregate())), alias(-1) {
AggFunc() : alias(-1) {}

static bl::result<AggFunc> MakeAggFunc(const physical::GroupBy_AggFunc& opr,
const ReadTransaction& txn,
const Context& ctx) {
AggFunc agg_func;
agg_func.aggregate = parse_aggregate(opr.aggregate());

if (opr.has_alias()) {
alias = opr.alias().value();
agg_func.alias = opr.alias().value();
}
int var_num = opr.vars_size();
for (int i = 0; i < var_num; ++i) {
vars.emplace_back(txn, ctx, opr.vars(i), VarType::kPathVar);
// vars.emplace_back(txn, ctx, opr.vars(i), VarType::kPathVar);
BOOST_LEAF_AUTO(var,
Var::MakeVar(txn, ctx, opr.vars(i), VarType::kPathVar));
agg_func.vars.push_back(std::move(var));
}
return agg_func;
}

std::vector<Var> vars;
Expand All @@ -80,12 +89,15 @@ struct AggFunc {
};

struct AggKey {
AggKey(const physical::GroupBy_KeyAlias& opr, const ReadTransaction& txn,
const Context& ctx)
: key(txn, ctx, opr.key(), VarType::kPathVar), alias(-1) {
if (opr.has_alias()) {
alias = opr.alias().value();
}
AggKey() : alias(-1) {}
static bl::result<AggKey> MakeAggKey(const physical::GroupBy_KeyAlias& opr,
const ReadTransaction& txn,
const Context& ctx) {
AggKey agg_key;
BOOST_LEAF_ASSIGN(agg_key.key,
Var::MakeVar(txn, ctx, opr.key(), VarType::kPathVar));
agg_key.alias = opr.alias().value();
return agg_key;
}

Var key;
Expand Down Expand Up @@ -536,7 +548,8 @@ bl::result<Context> eval_group_by(const physical::GroupBy& opr,
std::vector<AggKey> mappings;
int func_num = opr.functions_size();
for (int i = 0; i < func_num; ++i) {
functions.emplace_back(opr.functions(i), txn, ctx);
BOOST_LEAF_AUTO(func, AggFunc::MakeAggFunc(opr.functions(i), txn, ctx));
functions.emplace_back(std::move(func));
}

int mappings_num = opr.mappings_size();
Expand All @@ -556,7 +569,8 @@ bl::result<Context> eval_group_by(const physical::GroupBy& opr,
return ret;
} else {
for (int i = 0; i < mappings_num; ++i) {
mappings.emplace_back(opr.mappings(i), txn, ctx);
BOOST_LEAF_AUTO(key, AggKey::MakeAggKey(opr.mappings(i), txn, ctx));
mappings.emplace_back(std::move(key));
}

auto keys_ret =
Expand Down
2 changes: 1 addition & 1 deletion flex/engines/graph_db/runtime/adhoc/operators/order_by.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ bl::result<Context> eval_order_by(const algebra::OrderBy& opr,
int keys_num = opr.pairs_size();
for (int i = 0; i < keys_num; ++i) {
const algebra::OrderBy_OrderingPair& pair = opr.pairs(i);
Var v(txn, ctx, pair.key(), VarType::kPathVar);
BOOST_LEAF_AUTO(v, Var::MakeVar(txn, ctx, pair.key(), VarType::kPathVar));
CHECK(pair.order() == algebra::OrderBy_OrderingPair_Order::
OrderBy_OrderingPair_Order_ASC ||
pair.order() == algebra::OrderBy_OrderingPair_Order::
Expand Down
6 changes: 4 additions & 2 deletions flex/engines/graph_db/runtime/adhoc/operators/project.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ bl::result<Context> eval_project(
continue;
}
}
Expr expr(txn, ctx, params, m.expr(), VarType::kPathVar);
BOOST_LEAF_AUTO(
expr, Expr::MakeExpr(txn, ctx, params, m.expr(), VarType::kPathVar));
int alias = -1;
if (m.has_alias()) {
alias = m.alias().value();
Expand All @@ -86,7 +87,8 @@ bl::result<Context> eval_project(
}
}

Expr expr(txn, ctx, params, m.expr(), VarType::kPathVar);
BOOST_LEAF_AUTO(
expr, Expr::MakeExpr(txn, ctx, params, m.expr(), VarType::kPathVar));
int alias = -1;
if (m.has_alias()) {
alias = m.alias().value();
Expand Down
15 changes: 9 additions & 6 deletions flex/engines/graph_db/runtime/adhoc/operators/scan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ bl::result<Context> eval_scan(
if (!has_other_type_oid && scan_opr.has_idx_predicate()) {
if (scan_opr.has_idx_predicate() && scan_opr_params.has_predicate()) {
Context ctx;
auto expr = parse_expression(
txn, ctx, params, scan_opr_params.predicate(), VarType::kVertexVar);
BOOST_LEAF_AUTO(expr, parse_expression(txn, ctx, params,
scan_opr_params.predicate(),
VarType::kVertexVar));
std::vector<Any> oids{};
if (!parse_idx_predicate(scan_opr.idx_predicate(), params, oids,
scan_oid)) {
Expand Down Expand Up @@ -309,8 +310,9 @@ bl::result<Context> eval_scan(
} else if (scan_opr.has_idx_predicate()) {
if (scan_opr.has_idx_predicate() && scan_opr_params.has_predicate()) {
Context ctx;
auto expr = parse_expression(
txn, ctx, params, scan_opr_params.predicate(), VarType::kVertexVar);
BOOST_LEAF_AUTO(expr, parse_expression(txn, ctx, params,
scan_opr_params.predicate(),
VarType::kVertexVar));
std::vector<Any> oids{};
if (!parse_idx_predicate(scan_opr.idx_predicate(), params, oids,
scan_oid)) {
Expand All @@ -336,8 +338,9 @@ bl::result<Context> eval_scan(

if (scan_opr_params.has_predicate()) {
Context ctx;
auto expr = parse_expression(
txn, ctx, params, scan_opr_params.predicate(), VarType::kVertexVar);
BOOST_LEAF_AUTO(
expr, parse_expression(txn, ctx, params, scan_opr_params.predicate(),
VarType::kVertexVar));
if (expr->is_optional()) {
return Scan::scan_vertex(
txn, scan_params, [&expr](label_t label, vid_t vid) {
Expand Down
Loading
Loading