Skip to content

Commit

Permalink
Merge pull request #94 from oracle/subquery_in_from_fixes
Browse files Browse the repository at this point in the history
Fixes for subquery in FROM
  • Loading branch information
oskar-van-rest committed Aug 12, 2022
2 parents afa5811 + 7bf56da commit c1d6b04
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public int hashCode() {
return 31;
}

@Override
public void accept(QueryExpressionVisitor v) {
v.visit(this);
}
Expand Down
12 changes: 10 additions & 2 deletions graph-query-ir/src/main/java/oracle/pgql/lang/ir/PgqlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,17 @@ protected static String printPgqlString(GraphQuery graphQuery) {
throw new IllegalArgumentException(graphQuery.getQueryType().toString());
}

if (graphQuery.getGraphPattern() != null) {
List<TableExpression> tableExpressions = graphQuery.getTableExpressions();
if (!tableExpressions.isEmpty()) {
result += "\nFROM ";
result += printPgqlString(graphQuery.getGraphPattern(), graphQuery.getGraphName());

List<String> tableExpressionStrings = new ArrayList<>();
for (TableExpression tableExpression : tableExpressions) {
tableExpressionStrings.add(tableExpression.getTableExpressionType() == TableExpressionType.GRAPH_PATTERN
? printPgqlString((GraphPattern) tableExpression, graphQuery.getGraphName())
: tableExpression.toString());
}
result += tableExpressionStrings.stream().collect(Collectors.joining(", "));
}
GroupBy groupBy = graphQuery.getGroupBy();
if (groupBy != null && groupBy.getElements().isEmpty() == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
public interface TableExpression {

TableExpressionType getTableExpressionType();

void accept(QueryExpressionVisitor v);
}
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,7 @@ public void visit(SelectQuery selectQuery) {
}

private void visitQuery(GraphQuery query) {
if (query.getGraphPattern() != null) {
query.getGraphPattern().accept(this);
}
query.getTableExpressions().stream().forEach(e -> e.accept(this));
if (query.getGroupBy() != null) {
query.getGroupBy().accept(this);
}
Expand Down
110 changes: 11 additions & 99 deletions pgql-lang/src/test/java/oracle/pgql/lang/MetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.Test;

import oracle.pgql.lang.ir.ExpAsVar;
import oracle.pgql.lang.ir.QueryVariable;
import oracle.pgql.lang.ir.SelectQuery;

public class MetadataTest extends AbstractPgqlTest {
Expand Down Expand Up @@ -961,7 +958,7 @@ private List<ExpAsVar> getExpAsVars(String query) throws Exception {
}

@Test
public void testSelectAllPropertieUnresolvedVariable() throws Exception {
public void testSelectAllPropertiesUnresolvedVariable() throws Exception {
PgqlResult result = parse("SELECT x.* FROM MATCH (v)");
assertTrue(result.getErrorMessages().contains("Unresolved variable"));
}
Expand Down Expand Up @@ -1008,102 +1005,17 @@ public void testOneRowPerEdge() throws Exception {
}

@Test
public void testGetAllVertexPropertiesApi() throws Exception {
PgqlResult result = parse("SELECT 1 FROM MATCH (n:Company) ON financialNetwork");
List<String> allProperties = getVertexProperties(result);
List<String> expectedProperties = new ArrayList<>();
expectedProperties.add("name");
assertEquals(expectedProperties, allProperties);

result = parse("SELECT 1 FROM MATCH (n) ON financialNetwork");
allProperties = getVertexProperties(result);
expectedProperties = new ArrayList<>();
expectedProperties.add("number");
expectedProperties.add("name");
assertEquals(expectedProperties, allProperties);

result = parse("SELECT 1 FROM MATCH (n) ON financialNetwork WHERE has_label(n, 'Account')");
allProperties = getVertexProperties(result);
expectedProperties = new ArrayList<>();
expectedProperties.add("number");
assertEquals(expectedProperties, allProperties);

result = parse("SELECT 1 FROM MATCH (n) ON financialNetwork WHERE has_label(n, 'ACCOUNT')");
allProperties = getVertexProperties(result);
assertEquals(expectedProperties, allProperties);

result = parse("SELECT 1 FROM MATCH (n) ON financialNetwork WHERE \"has_label\"(n, 'Account')");
allProperties = getVertexProperties(result);
assertEquals(expectedProperties, allProperties);

result = parse("SELECT 1 FROM MATCH (n)");
allProperties = getVertexProperties(result);
expectedProperties = new ArrayList<>();
expectedProperties.add("firstName");
expectedProperties.add("dob");
expectedProperties.add("numericProp");
expectedProperties.add("typeConflictProp");
expectedProperties.add("name");
assertEquals(expectedProperties, allProperties);

result = parse("SELECT 1 FROM MATCH (n) WHERE has_label(n, 'Person') OR has_label(n, 'University')");
assertEquals(expectedProperties, allProperties);
}

@Test
public void testGetAllEdgePropertiesApi() throws Exception {
PgqlResult result = parse("SELECT 1 FROM MATCH () -[e:worksFor]-> () ON financialNetwork");
List<String> allProperties = getEdgeProperties(result);
assertEquals(Collections.EMPTY_LIST, allProperties);

result = parse("SELECT 1 FROM MATCH () -[e:worksFor|transaction]-> () ON financialNetwork");
allProperties = getEdgeProperties(result);
List<String> expectedProperties = new ArrayList<>();
expectedProperties.add("amount");
assertEquals(expectedProperties, allProperties);

result = parse("SELECT 1 FROM MATCH () -[e]-> () ON financialNetwork WHERE has_label(e, 'worksFor')");
allProperties = getEdgeProperties(result);
expectedProperties = new ArrayList<>();
assertEquals(expectedProperties, allProperties);

result = parse("SELECT 1 FROM MATCH () -[e]-> () ON financialNetwork WHERE has_label(e, 'transaction')");
allProperties = getEdgeProperties(result);
expectedProperties = new ArrayList<>();
expectedProperties.add("amount");
assertEquals(expectedProperties, allProperties);

result = parse("SELECT 1 FROM MATCH () -[e]-> () ON financialNetwork WHERE has_label(e, 'TRANSACTION')");
allProperties = getEdgeProperties(result);
assertEquals(expectedProperties, allProperties);

result = parse("SELECT 1 FROM MATCH () -[e]-> () ON financialNetwork WHERE \"has_label\"(e, 'transaction')");
allProperties = getEdgeProperties(result);
assertEquals(expectedProperties, allProperties);

result = parse("SELECT 1 FROM MATCH () -[e]-> () ON financialNetwork");
allProperties = getEdgeProperties(result);
assertEquals(expectedProperties, allProperties);

result = parse("SELECT 1 FROM MATCH () -[e]-> ()");
allProperties = getEdgeProperties(result);
expectedProperties = new ArrayList<>();
expectedProperties.add("since");
expectedProperties.add("prop");
expectedProperties.add("typeConflictProp");
expectedProperties.add("PROP");
expectedProperties.add("Typeconflictprop");
assertEquals(expectedProperties, allProperties);
}

private List<String> getEdgeProperties(PgqlResult result) {
QueryVariable edge = result.getGraphQuery().getGraphPattern().getConnections().iterator().next();
return result.getAllProperties(edge);
}
public void testOneRowPerStep() throws Exception {
PgqlResult result = parse("SELECT v1.firstName, e.since, v2.dob FROM MATCH ANY (a) ->+ (b) ONE ROW PER STEP ( v1, e, v2 )");
assertTrue(result.isQueryValid());

private List<String> getVertexProperties(PgqlResult result) {
QueryVariable vertex = result.getGraphQuery().getGraphPattern().getVertices().iterator().next();
return result.getAllProperties(vertex);
String errorMessage = "Property does not exist for any of the labels";
result = parse("SELECT v1.xyz FROM MATCH ANY (a) ->* (b) ONE ROW PER STEP ( v1, e, v2 )");
assertTrue(result.getErrorMessages().contains(errorMessage));
result = parse("SELECT e.xyz FROM MATCH ANY (a) ->* (b) ONE ROW PER STEP ( v1, e, v2 )");
assertTrue(result.getErrorMessages().contains(errorMessage));
result = parse("SELECT v2.xyz FROM MATCH ANY (a) ->* (b) ONE ROW PER STEP ( v1, e, v2 )");
assertTrue(result.getErrorMessages().contains(errorMessage));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,12 @@ public void testInterval() throws Exception {
checkRoundTrip(query);
}

@Test
public void testLateralSubquery() throws Exception {
String query = "SELECT n FROM LATERAL ( SELECT n.prop FROM MATCH (n) ON g1 LIMIT 1 ), MATCH (n IS Person) -[e IS likes]-> (m IS Person) ON g1 WHERE n.name = 'Dave'";
checkRoundTrip(query);
}

private void checkRoundTrip(String query1) throws PgqlException {

/*
Expand Down

0 comments on commit c1d6b04

Please sign in to comment.