Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ data: {
"DIFFERENT",
"PARTITIONED",
"YIELD",
"IF"
"IF",
"OPTIONAL"
]

# let noReservedKeywords can be a identifier
Expand Down Expand Up @@ -435,6 +436,8 @@ data: {
"USING"
"YIELD"
"NEXT"
"OPTIONAL"

]

# List of additional join types. Each is a method with no arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ SqlCall GQLMatchStatement() :
SqlCall statement = null;
}
{
<MATCH> statement = SqlMatchPattern(statement)
statement = SqlMatchPattern(statement)
(
(
statement = SqlLetStatement(statement) (<COMMA> statement = SqlLetStatement(statement))*
[
[ <NEXT> ] <MATCH>
[ <NEXT> ]
statement = SqlMatchPattern(statement)
]
)
|
(
[ <NEXT> ] <MATCH>
[ <NEXT> ]
statement = SqlMatchPattern(statement)
)
)*
Expand Down Expand Up @@ -366,8 +366,10 @@ SqlMatchPattern SqlMatchPattern(SqlNode preMatch) :
SqlNode condition = null;
SqlNodeList orderBy = null;
SqlNode count = null;
boolean optional = false;
}
{
[ <OPTIONAL> { optional = true; } ] <MATCH>
pathPattern = SqlUnionPathPattern() { pathList.add(pathPattern); }
( <COMMA> pathPattern = SqlUnionPathPattern() { pathList.add(pathPattern); } )*

Expand All @@ -387,7 +389,7 @@ SqlMatchPattern SqlMatchPattern(SqlNode preMatch) :
]
{
graphPattern = new SqlNodeList(pathList, s.addAll(pathList).pos());
return new SqlMatchPattern(s.end(this), preMatch, graphPattern, condition, orderBy, count);
return new SqlMatchPattern(s.end(this), preMatch, graphPattern, condition, orderBy, count, optional);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ public class SqlMatchPatternOperator extends SqlOperator {

private SqlMatchPatternOperator() {
super("MatchPattern", SqlKind.OTHER, 2, true,
ReturnTypes.SCOPE, null, null);
ReturnTypes.SCOPE, null, null);
}

@Override
public SqlCall createCall(
SqlLiteral functionQualifier,
SqlParserPos pos,
SqlNode... operands) {
SqlLiteral functionQualifier,
SqlParserPos pos,
SqlNode... operands) {
return new SqlMatchPattern(pos, operands[0], (SqlNodeList) operands[1], operands[2],
(SqlNodeList) operands[3], operands[4]);
(SqlNodeList) operands[3], operands[4], false);
}

@Override
Expand All @@ -49,10 +49,10 @@ public SqlSyntax getSyntax() {

@Override
public void unparse(
SqlWriter writer,
SqlCall call,
int leftPrec,
int rightPrec) {
SqlWriter writer,
SqlCall call,
int leftPrec,
int rightPrec) {
call.unparse(writer, leftPrec, rightPrec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ public class SqlMatchPattern extends SqlCall {

private SqlNode limit;

private boolean optional;

public SqlMatchPattern(SqlParserPos pos, SqlNode from, SqlNodeList pathPatterns,
SqlNode where, SqlNodeList orderBy, SqlNode limit) {
SqlNode where, SqlNodeList orderBy, SqlNode limit, boolean optional) {
super(pos);
this.from = from;
this.pathPatterns = pathPatterns;
this.where = where;
this.orderBy = orderBy;
this.limit = limit;
this.optional = optional;
}

@Override
Expand All @@ -57,7 +60,7 @@ public SqlOperator getOperator() {
@Override
public List<SqlNode> getOperandList() {
return ImmutableNullableList.of(getFrom(), getPathPatterns(), getWhere(),
getOrderBy(), getLimit());
getOrderBy(), getLimit());
}

@Override
Expand All @@ -82,7 +85,6 @@ public SqlNodeList getOrderBy() {
return orderBy;
}


public SqlNode getLimit() {
return limit;
}
Expand Down Expand Up @@ -170,4 +172,8 @@ public void setWhere(SqlNode where) {
public boolean isSinglePattern() {
return pathPatterns.size() == 1 && pathPatterns.get(0) instanceof SqlPathPattern;
}

public boolean isOptional() {
return optional;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,11 @@ public void testGQLMatchOrder() throws Exception {
String unParseStmts = parseStmtsAndUnParse(parseStmtsAndUnParse(unParseSql));
Assert.assertEquals(unParseStmts, unParseSql);
}

@Test
public void testGQLOptionalMatch() throws Exception {
String unParseSql = parseSqlAndUnParse("GQLOptionalMatch.sql");
String unParseStmts = parseStmtsAndUnParse(parseStmtsAndUnParse(unParseSql));
Assert.assertEquals(unParseStmts, unParseSql);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

OPTIONAL MATCH (a:Person) RETURN a.name;
MATCH (a:Person) OPTIONAL MATCH (a)-[:Knows]->(b:Person) RETURN a.name, b.name;
OPTIONAL MATCH (a:Person) WHERE a.age > 20 RETURN a.name;
MATCH (a:Person) OPTIONAL MATCH (a)-[:Knows]->(b:Person) OPTIONAL MATCH (b)-[:Knows]->(c:Person) RETURN a.name, b.name, c.name;
Loading