Skip to content
This repository has been archived by the owner on Dec 1, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' into rewrite-in-expr
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiee authored Jul 23, 2021
2 parents 761b269 + 1b018a0 commit 24acf65
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 119 deletions.
11 changes: 4 additions & 7 deletions src/executor/query/UnwindExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ folly::Future<Status> UnwindExecutor::execute() {
DataSet ds;
ds.colNames = unwind->colNames();
for (; iter->valid(); iter->next()) {
Value list = unwindExpr->eval(ctx(iter.get()));
const Value& list = unwindExpr->eval(ctx(iter.get()));
std::vector<Value> vals = extractList(list);
for (auto &v : vals) {
Row row;
Expand All @@ -41,17 +41,14 @@ folly::Future<Status> UnwindExecutor::execute() {
return finish(ResultBuilder().value(Value(std::move(ds))).finish());
}

std::vector<Value> UnwindExecutor::extractList(Value &val) {
std::vector<Value> UnwindExecutor::extractList(const Value &val) {
std::vector<Value> ret;
if (val.isList()) {
auto &list = val.getList();
ret.reserve(list.size());
for (size_t i = 0; i < list.size(); ++i) {
ret.emplace_back(std::move(list[i]));
}
ret = list.values;
} else {
if (!(val.isNull() || val.empty())) {
ret.emplace_back(std::move(val));
ret.push_back(val);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/executor/query/UnwindExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UnwindExecutor final : public Executor {
folly::Future<Status> execute() override;

private:
std::vector<Value> extractList(Value &val);
std::vector<Value> extractList(const Value &val);
};

} // namespace graph
Expand Down
23 changes: 17 additions & 6 deletions src/parser/MatchSentence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ std::string WithClause::toString() const {
buf += "DISTINCT ";
}

buf += columns_->toString();
buf += returnItems_->toString();

if (orderFactors_ != nullptr) {
buf += " ";
Expand Down Expand Up @@ -173,6 +173,21 @@ std::string MatchPath::toString() const {
return buf;
}

std::string MatchReturnItems::toString() const {
std::string buf;
if (includeExisting_) {
buf += '*';
}
if (columns_) {
if (includeExisting_) {
buf += ',';
}
buf += columns_->toString();
}

return buf;
}

std::string MatchReturn::toString() const {
std::string buf;
buf.reserve(64);
Expand All @@ -183,11 +198,7 @@ std::string MatchReturn::toString() const {
buf += "DISTINCT ";
}

if (isAll()) {
buf += '*';
} else {
buf += columns_->toString();
}
buf += returnItems_->toString();

if (orderFactors_ != nullptr) {
buf += " ";
Expand Down
62 changes: 36 additions & 26 deletions src/parser/MatchSentence.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,34 +261,45 @@ class MatchPath final {
std::vector<std::unique_ptr<MatchEdge>> edges_;
};

class MatchReturnItems final {
public:
explicit MatchReturnItems(bool includeExisting, YieldColumns* columns = nullptr)
: includeExisting_(includeExisting), columns_(columns) {}

bool includeExisting() const { return includeExisting_; }

YieldColumns* columns() { return columns_.get(); }

const YieldColumns* columns() const { return columns_.get(); }

std::string toString() const;

private:
bool includeExisting_{false}; // `*` indicates include all existing variables
std::unique_ptr<YieldColumns> columns_;
};


class MatchReturn final {
public:
explicit MatchReturn(YieldColumns* columns = nullptr,
explicit MatchReturn(MatchReturnItems* returnItems = nullptr,
OrderFactors* orderFactors = nullptr,
Expression* skip = nullptr,
Expression* limit = nullptr,
bool distinct = false) {
columns_.reset(columns);
returnItems_.reset(returnItems);
orderFactors_.reset(orderFactors);
skip_ = skip;
limit_ = limit;
isDistinct_ = distinct;
if (columns_ == nullptr) {
isAll_ = true;
}
}

const YieldColumns* columns() const {
return columns_.get();
}

void setColumns(YieldColumns *columns) {
columns_.reset(columns);
MatchReturnItems* returnItems() {
return returnItems_.get();
}

bool isAll() const {
return isAll_;
const MatchReturnItems* returnItems() const {
return returnItems_.get();
}

bool isDistinct() const {
Expand All @@ -314,12 +325,11 @@ class MatchReturn final {
std::string toString() const;

private:
std::unique_ptr<YieldColumns> columns_;
bool isAll_{false};
bool isDistinct_{false};
std::unique_ptr<OrderFactors> orderFactors_;
Expression* skip_{nullptr};
Expression* limit_{nullptr};
std::unique_ptr<MatchReturnItems> returnItems_;
bool isDistinct_{false};
std::unique_ptr<OrderFactors> orderFactors_;
Expression* skip_{nullptr};
Expression* limit_{nullptr};
};


Expand Down Expand Up @@ -425,27 +435,27 @@ class UnwindClause final : public ReadingClause {

class WithClause final : public ReadingClause {
public:
explicit WithClause(YieldColumns *cols,
explicit WithClause(MatchReturnItems *returnItems,
OrderFactors *orderFactors = nullptr,
Expression *skip = nullptr,
Expression *limit = nullptr,
WhereClause *where = nullptr,
bool distinct = false)
: ReadingClause(Kind::kWith) {
columns_.reset(cols);
returnItems_.reset(returnItems);
orderFactors_.reset(orderFactors);
skip_ = skip;
limit_ = limit;
where_.reset(where);
isDistinct_ = distinct;
}

YieldColumns* columns() {
return columns_.get();
MatchReturnItems* returnItems() {
return returnItems_.get();
}

const YieldColumns* columns() const {
return columns_.get();
const MatchReturnItems* returnItems() const {
return returnItems_.get();
}

OrderFactors* orderFactors() {
Expand Down Expand Up @@ -487,7 +497,7 @@ class WithClause final : public ReadingClause {
std::string toString() const override;

private:
std::unique_ptr<YieldColumns> columns_;
std::unique_ptr<MatchReturnItems> returnItems_;
std::unique_ptr<OrderFactors> orderFactors_;
Expression* skip_{nullptr};
Expression* limit_{nullptr};
Expand Down
25 changes: 16 additions & 9 deletions src/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ static constexpr size_t kCommentLengthLimit = 256;
MatchEdge *match_edge;
MatchEdgeProp *match_edge_prop;
MatchEdgeTypeList *match_edge_type_list;
MatchReturnItems *match_return_items;
MatchReturn *match_return;
ReadingClause *reading_clause;
MatchClauseList *match_clause_list;
Expand Down Expand Up @@ -302,6 +303,7 @@ static constexpr size_t kCommentLengthLimit = 256;
%type <match_node_label_list> match_node_label_list
%type <match_edge> match_edge
%type <match_edge_prop> match_edge_prop
%type <match_return_items> match_return_items
%type <match_return> match_return
%type <expr> match_skip
%type <expr> match_limit
Expand Down Expand Up @@ -1389,7 +1391,7 @@ unwind_clause
;

with_clause
: KW_WITH yield_columns match_order_by match_skip match_limit where_clause {
: KW_WITH match_return_items match_order_by match_skip match_limit where_clause {
if ($6 && graph::ExpressionUtils::findAny($6->filter(),{Expression::Kind::kAggregate})) {
delete($2);
delete($3);
Expand All @@ -1400,7 +1402,7 @@ with_clause
}
$$ = new WithClause($2, $3, $4, $5, $6, false/*distinct*/);
}
| KW_WITH KW_DISTINCT yield_columns match_order_by match_skip match_limit where_clause {
| KW_WITH KW_DISTINCT match_return_items match_order_by match_skip match_limit where_clause {
if ($7 && graph::ExpressionUtils::findAny($7->filter(),{Expression::Kind::kAggregate})) {
delete($3);
delete($4);
Expand Down Expand Up @@ -1627,19 +1629,24 @@ match_edge_type_list
;

match_return
: KW_RETURN yield_columns match_order_by match_skip match_limit {
: KW_RETURN match_return_items match_order_by match_skip match_limit {
$$ = new MatchReturn($2, $3, $4, $5);
}
| KW_RETURN KW_DISTINCT yield_columns match_order_by match_skip match_limit {
| KW_RETURN KW_DISTINCT match_return_items match_order_by match_skip match_limit {
$$ = new MatchReturn($3, $4, $5, $6, true);
}
| KW_RETURN STAR match_order_by match_skip match_limit {
$$ = new MatchReturn(nullptr, $3, $4, $5);
;

match_return_items
: STAR {
$$ = new MatchReturnItems(true);
}
| KW_RETURN KW_DISTINCT STAR match_order_by match_skip match_limit {
$$ = new MatchReturn(nullptr, $4, $5, $6, true);
| STAR COMMA yield_columns {
$$ = new MatchReturnItems(true, $3);
}
| yield_columns {
$$ = new MatchReturnItems(false, $1);
}
;

match_order_by
: %empty {
Expand Down
Loading

0 comments on commit 24acf65

Please sign in to comment.