Skip to content

Commit

Permalink
support gql list/set/map
Browse files Browse the repository at this point in the history
  • Loading branch information
jievince committed Nov 11, 2021
1 parent 1d0c583 commit 9ccfcf9
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .linters/cpp/checkKeyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
'KW_XOR',
'KW_USE',
'KW_SET',
'KW_LIST',
'KW_MAP',
'KW_FROM',
'KW_WHERE',
'KW_MATCH',
Expand Down
27 changes: 18 additions & 9 deletions src/common/expression/ContainerExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@

namespace nebula {

// TODO(jie): toString of list should add `LIST` prefix
std::string ListExpression::toString() const {
// list *expression* is not allowed to be empty
DCHECK(!items_.empty());
std::string buf;
buf.reserve(256);

Expand All @@ -23,7 +22,11 @@ std::string ListExpression::toString() const {
buf += expr->toString();
buf += ",";
}
buf.back() = ']';
if (items_.empty()) {
buf += "]";
} else {
buf.back() = ']';
}

return buf;
}
Expand Down Expand Up @@ -78,9 +81,8 @@ void ListExpression::resetFrom(Decoder &decoder) {

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

// TODO(jie): toString of set should add `SET` prefix
std::string SetExpression::toString() const {
// set *expression* is not allowed to be empty
DCHECK(!items_.empty());
std::string buf;
buf.reserve(256);

Expand All @@ -89,7 +91,11 @@ std::string SetExpression::toString() const {
buf += expr->toString();
buf += ",";
}
buf.back() = '}';
if (items_.empty()) {
buf += "}";
} else {
buf.back() = '}';
}

return buf;
}
Expand Down Expand Up @@ -144,9 +150,8 @@ void SetExpression::resetFrom(Decoder &decoder) {

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

// TODO(jie): toString of map should add `MAP` prefix
std::string MapExpression::toString() const {
// map *expression* is not allowed to be empty
DCHECK(!items_.empty());
std::string buf;
buf.reserve(256);

Expand All @@ -157,7 +162,11 @@ std::string MapExpression::toString() const {
buf += kv.second->toString();
buf += ",";
}
buf.back() = '}';
if (items_.empty()) {
buf += "}";
} else {
buf.back() = '}';
}

return buf;
}
Expand Down
4 changes: 2 additions & 2 deletions src/graph/validator/test/YieldValidatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ TEST_F(YieldValidatorTest, TypeCastTest) {
{
std::string query = "YIELD (MAP)(\"12\")";
auto result = checkResult(query);
EXPECT_EQ(std::string(result.message()), "SyntaxError: syntax error near `(\"12\")'");
EXPECT_EQ(std::string(result.message()), "SyntaxError: syntax error near `)(\"12\")'");
}
{
std::string query = "YIELD (SET)12";
auto result = checkResult(query);
EXPECT_EQ(std::string(result.message()), "SyntaxError: syntax error near `SET'");
EXPECT_EQ(std::string(result.message()), "SyntaxError: syntax error near `)12'");
}
{
std::string query = "YIELD (PATH)true";
Expand Down
32 changes: 30 additions & 2 deletions src/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ static constexpr size_t kCommentLengthLimit = 256;
%token KW_SESSIONS KW_SESSION
%token KW_KILL KW_QUERY KW_QUERIES KW_TOP
%token KW_GEOGRAPHY KW_POINT KW_LINESTRING KW_POLYGON
%token KW_LIST KW_MAP

/* symbols */
%token L_PAREN R_PAREN L_BRACKET R_BRACKET L_BRACE R_BRACE COMMA
Expand Down Expand Up @@ -293,8 +294,8 @@ static constexpr size_t kCommentLengthLimit = 256;
%type <in_bound_clause> in_bound_clause
%type <out_bound_clause> out_bound_clause
%type <both_in_out_clause> both_in_out_clause
%type <expression_list> expression_list
%type <map_item_list> map_item_list
%type <expression_list> expression_list opt_expression_list
%type <map_item_list> map_item_list opt_map_item_list
%type <case_list> when_then_list
%type <expr> case_condition
%type <expr> case_default
Expand Down Expand Up @@ -1191,12 +1192,27 @@ list_expression
: L_BRACKET expression_list R_BRACKET {
$$ = ListExpression::make(qctx->objPool(), $2);
}
| KW_LIST L_BRACKET opt_expression_list R_BRACKET {
$$ = ListExpression::make(qctx->objPool(), $3);
}
;

set_expression
: L_BRACE expression_list R_BRACE {
$$ = SetExpression::make(qctx->objPool(), $2);
}
| KW_SET L_BRACE opt_expression_list R_BRACE {
$$ = SetExpression::make(qctx->objPool(), $3);
}
;

opt_expression_list
: %empty {
$$ = ExpressionList::make(qctx->objPool());
}
| expression_list {
$$ = $1;
}
;

expression_list
Expand All @@ -1214,6 +1230,18 @@ map_expression
: L_BRACE map_item_list R_BRACE {
$$ = MapExpression::make(qctx->objPool(), $2);
}
| KW_MAP L_BRACE opt_map_item_list R_BRACE {
$$ = MapExpression::make(qctx->objPool(), $3);
}
;

opt_map_item_list
: %empty {
$$ = MapItemList::make(qctx->objPool());
}
| map_item_list {
$$ = $1;
}
;

map_item_list
Expand Down
3 changes: 2 additions & 1 deletion src/parser/scanner.lex
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ IP_OCTET ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
"XOR" { return TokenType::KW_XOR; }
"USE" { return TokenType::KW_USE; }
"SET" { return TokenType::KW_SET; }
"LIST" { return TokenType::KW_LIST; }
"MAP" { return TokenType::KW_MAP; }
"FROM" { return TokenType::KW_FROM; }
"WHERE" { return TokenType::KW_WHERE; }
"MATCH" { return TokenType::KW_MATCH; }
Expand Down Expand Up @@ -259,7 +261,6 @@ IP_OCTET ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
"POINT" { return TokenType::KW_POINT; }
"LINESTRING" { return TokenType::KW_LINESTRING; }
"POLYGON" { return TokenType::KW_POLYGON; }

"TRUE" { yylval->boolval = true; return TokenType::BOOL; }
"FALSE" { yylval->boolval = false; return TokenType::BOOL; }

Expand Down
2 changes: 1 addition & 1 deletion src/parser/test/ParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2668,7 +2668,7 @@ TEST_F(ParserTest, MatchMultipleTags) {

TEST_F(ParserTest, MatchListSubscriptRange) {
{
std::string query = "WITH [0, 1, 2] AS list RETURN list[0..] AS l";
std::string query = "WITH [0, 1, 2] AS l RETURN l[0..] AS l2";
auto result = parse(query);
ASSERT_TRUE(result.ok()) << result.status();
}
Expand Down
53 changes: 53 additions & 0 deletions tests/tck/features/basic/data.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Feature: data

Background: Prepare space
Given a graph with space named "nba"

Scenario: list, set, map
When executing query:
"""
RETURN size(LIST[]) AS a, size(SET{}) AS b, size(MAP{}) AS c
"""
Then the result should be, in any order, with relax comparison:
| a | b | c |
| 0 | 0 | 0 |
When executing query:
"""
RETURN 1 IN LIST[] AS a, "Tony" IN SET{} AS b, "a" IN MAP{} AS c
"""
Then the result should be, in any order, with relax comparison:
| a | b | c |
| false | false | false |
When executing query:
"""
RETURN LIST[1, 2] AS a, SET{1, 2, 1} AS b, MAP{a:1, b:2} AS c, MAP{a: LIST[1,2], b: SET{1,2,1}, c: "hee"} AS d
"""
Then the result should be, in any order, with relax comparison:
| a | b | c | d |
| [1, 2] | {1, 2} | {a:1, b:2} | {a: [1, 2], b: {2, 1}, c: "hee"} |
When executing query:
"""
RETURN 1 IN LIST[1, 2] AS a, 2 IN SET{1, 2, 1} AS b, "a" IN MAP{a:1, b:2} AS c, MAP{a: LIST[1,2], b: SET{1,2,1}, c: "hee"}["b"] AS d
"""
Then the result should be, in any order, with relax comparison:
| a | b | c | d |
| true | true | true | {2, 1} |
When executing query:
"""
RETURN [], {}, {}
"""
Then a SyntaxError should be raised at runtime:
When executing query:
"""
RETURN [1, 2] AS a, {1, 2, 1} AS b, {a:1, b:2} AS c
"""
Then the result should be, in any order, with relax comparison:
| a | b | c |
| [1, 2] | {1, 2} | {a:1, b:2} |
When executing query:
"""
RETURN 2 IN [1, 2] AS a, 2 IN {1, 2, 1} AS b, "b" IN MAP{a:1, b:2} AS c
"""
Then the result should be, in any order, with relax comparison:
| a | b | c |
| true | true | true |
4 changes: 2 additions & 2 deletions tests/tck/features/expression/ListComprehension.feature
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ Feature: ListComprehension
When executing query:
"""
UNWIND [1, 2, 3, 4, 5] AS a RETURN a * 2 AS x
| RETURN [n in collect($-.x) WHERE n > 5 | n + 1] AS list
| RETURN [n in collect($-.x) WHERE n > 5 | n + 1] AS l
"""
Then the result should be, in any order:
| list |
| l |
| [7, 9, 11] |
44 changes: 22 additions & 22 deletions tests/tck/openCypher/features/expressions/list/List2.feature
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ Feature: List2 - List Slicing
Given any graph
When executing query:
"""
WITH [1, 2, 3, 4, 5] AS list
RETURN list[1..3] AS r
WITH [1, 2, 3, 4, 5] AS l
RETURN l[1..3] AS r
"""
Then the result should be, in any order:
| r |
Expand All @@ -49,8 +49,8 @@ Feature: List2 - List Slicing
Given any graph
When executing query:
"""
WITH [1, 2, 3] AS list
RETURN list[1..] AS r
WITH [1, 2, 3] AS l
RETURN l[1..] AS r
"""
Then the result should be, in any order:
| r |
Expand All @@ -61,8 +61,8 @@ Feature: List2 - List Slicing
Given any graph
When executing query:
"""
WITH [1, 2, 3] AS list
RETURN list[..2] AS r
WITH [1, 2, 3] AS l
RETURN l[..2] AS r
"""
Then the result should be, in any order:
| r |
Expand All @@ -73,8 +73,8 @@ Feature: List2 - List Slicing
Given any graph
When executing query:
"""
WITH [1, 2, 3] AS list
RETURN list[0..1] AS r
WITH [1, 2, 3] AS l
RETURN l[0..1] AS r
"""
Then the result should be, in any order:
| r |
Expand All @@ -85,8 +85,8 @@ Feature: List2 - List Slicing
Given any graph
When executing query:
"""
WITH [1, 2, 3] AS list
RETURN list[0..0] AS r
WITH [1, 2, 3] AS l
RETURN l[0..0] AS r
"""
Then the result should be, in any order:
| r |
Expand All @@ -97,8 +97,8 @@ Feature: List2 - List Slicing
Given any graph
When executing query:
"""
WITH [1, 2, 3] AS list
RETURN list[-3..-1] AS r
WITH [1, 2, 3] AS l
RETURN l[-3..-1] AS r
"""
Then the result should be, in any order:
| r |
Expand All @@ -109,8 +109,8 @@ Feature: List2 - List Slicing
Given any graph
When executing query:
"""
WITH [1, 2, 3] AS list
RETURN list[3..1] AS r
WITH [1, 2, 3] AS l
RETURN l[3..1] AS r
"""
Then the result should be, in any order:
| r |
Expand All @@ -121,8 +121,8 @@ Feature: List2 - List Slicing
Given any graph
When executing query:
"""
WITH [1, 2, 3] AS list
RETURN list[-5..5] AS r
WITH [1, 2, 3] AS l
RETURN l[-5..5] AS r
"""
Then the result should be, in any order:
| r |
Expand All @@ -133,8 +133,8 @@ Feature: List2 - List Slicing
Given any graph
When executing query:
"""
WITH [1, 2, 3] AS list
RETURN list[<lower>..<upper>] AS r
WITH [1, 2, 3] AS l
RETURN l[<lower>..<upper>] AS r
"""
Then the result should be, in any order:
| r |
Expand All @@ -157,8 +157,8 @@ Feature: List2 - List Slicing
| to | 3 |
When executing query:
"""
WITH [1, 2, 3] AS list
RETURN list[$from..$to] AS r
WITH [1, 2, 3] AS l
RETURN l[$from..$to] AS r
"""
Then the result should be, in any order:
| r |
Expand All @@ -173,8 +173,8 @@ Feature: List2 - List Slicing
| to | 1 |
When executing query:
"""
WITH [1, 2, 3] AS list
RETURN list[$from..$to] AS r
WITH [1, 2, 3] AS l
RETURN l[$from..$to] AS r
"""
Then the result should be, in any order:
| r |
Expand Down
Loading

0 comments on commit 9ccfcf9

Please sign in to comment.