Skip to content

Commit

Permalink
changed query processing to only translate global predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
lc0197 committed Jun 15, 2020
1 parent baeeadb commit d02429f
Show file tree
Hide file tree
Showing 26 changed files with 327 additions and 52 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<version>0.3.5-SNAPSHOT</version>



<name>GDL - Graph Definition Language</name>
<description>ANTLR Grammar for the definition of extended property graphs.</description>
<url>http://github.com/s1ck/gdl.git</url>
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/org/s1ck/gdl/GDLHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ public static class Builder {
*/
private boolean useDefaultEdgeLabel = true;

/**
* Flag to indicate if the query should be postprocessed, i.e. reduced to simple comparisons.
*/
private boolean processQuery = true;

/**
* Strategy for handling parser errors.
*/
Expand Down Expand Up @@ -308,6 +313,17 @@ public Builder setErrorStrategy(ANTLRErrorStrategy errorStrategy) {
return this;
}

/**
* Sets the processQuery parameter for the {@link GDLLoader}.
*
* @param processQuery processQuery flag (true iff query should be postprocessed)
* @return builder
*/
public Builder setProcessQuery(boolean processQuery){
this.processQuery = processQuery;
return this;
}

/**
* Initialize GDL Handler from given ASCII String.
*
Expand Down Expand Up @@ -367,7 +383,7 @@ private GDLHandler build(ANTLRInputStream antlrInputStream) {
parser.setErrorHandler(errorStrategy);

GDLLoader loader = new GDLLoader(graphLabel, vertexLabel, edgeLabel,
useDefaultGraphLabel, useDefaultVertexLabel, useDefaultEdgeLabel);
useDefaultGraphLabel, useDefaultVertexLabel, useDefaultEdgeLabel, processQuery);
new ParseTreeWalker().walk(loader, parser.database());
return new GDLHandler(loader);
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/s1ck/gdl/GDLLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,15 @@ public void exitQuery(GDLParser.QueryContext ctx) {
if(processPredicates) {
postprocessPredicates();
}
ArrayList<String> vars = new ArrayList<>();
vars.addAll(userEdgeCache.keySet());
vars.addAll(userVertexCache.keySet());
vars.addAll(autoEdgeCache.keySet());
vars.addAll(autoVertexCache.keySet());
vars.remove(TimeSelector.GLOBAL_SELECTOR);
if(predicates!=null) {
predicates = predicates.replaceGlobalByLocal(vars);
}
for(Vertex v : vertices) {
addPredicates(Predicate.fromGraphElement(v, getDefaultVertexLabel()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@
package org.s1ck.gdl.model.comparables;

import org.s1ck.gdl.model.comparables.time.TimeSelector;
import org.s1ck.gdl.model.predicates.Predicate;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

public interface ComparableExpression extends Serializable{
/**
* Returns the variables of the expression
* @return variable
*/
public Set<String> getVariables();

/**
* Returns the variable of the expression
* Same functionality is given by {@code getVariables}, kept for downward compatibility
* @return variable
*/
public String getVariable();
Expand All @@ -40,4 +50,15 @@ public interface ComparableExpression extends Serializable{
*/
boolean isGlobal();

/**
* Replaces global time selectors like __global.val_from by their equivalent expressions
* over all local variables.
* E.g., {@code __global.val_from} would be replaced by {@code MAX(v1.val_from,...,
* vn.val_from)} for variables {@code v1,...,vn}.
* @param variables all query variables
* @return comparable with global selector replaced by their local variable equivalent
* expressions. If comparable is not a global time selector, identity function.
*/
ComparableExpression replaceGlobalByLocal(List<String> variables);

}
21 changes: 17 additions & 4 deletions src/main/java/org/s1ck/gdl/model/comparables/ElementSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

import org.s1ck.gdl.model.comparables.time.TimeSelector;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Used to compare elements (by id)
*/
Expand All @@ -37,15 +42,23 @@ public ElementSelector(String variable) {
this.variable = variable;
}

/**
* Returns the variable which references the element
* @return variable name
*/
@Override
public Set<String> getVariables() {
HashSet<String> var = new HashSet<>();
var.add(variable);
return var;
}

@Override
public String getVariable() {
return this.variable;
}

@Override
public ComparableExpression replaceGlobalByLocal(List<String> variables) {
return this;
}

@Override
public boolean containsSelectorType(TimeSelector.TimeField type){
return false;
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/org/s1ck/gdl/model/comparables/Literal.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

import org.s1ck.gdl.model.comparables.time.TimeSelector;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Represents a literal like String, Integer, ...
*/
Expand All @@ -41,10 +46,17 @@ public Object getValue() {
return value;
}

/**
* Returns null since this does not reference a variable
* @return null
*/

@Override
public Set<String> getVariables() {
return new HashSet<String>();
}

@Override
public ComparableExpression replaceGlobalByLocal(List<String> variables) {
return this;
}

@Override
public String getVariable() {
return null;
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/org/s1ck/gdl/model/comparables/PropertySelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

import org.s1ck.gdl.model.comparables.time.TimeSelector;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Selects a property of a variable
*/
Expand Down Expand Up @@ -47,10 +52,13 @@ public String getPropertyName() {
return propertyName;
}

/**
* Returns the variable which references the element
* @return variable name
*/
@Override
public Set<String> getVariables() {
HashSet<String> var = new HashSet<>();
var.add(variable);
return var;
}

@Override
public String getVariable() {
return this.variable;
Expand All @@ -66,6 +74,11 @@ public boolean isGlobal(){
return false;
}

@Override
public ComparableExpression replaceGlobalByLocal(List<String> variables) {
return this;
}

@Override
public String toString() {
return variable + "." + propertyName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.s1ck.gdl.model.comparables.time;

import org.s1ck.gdl.model.comparables.ComparableExpression;
import org.s1ck.gdl.model.predicates.Predicate;
import org.s1ck.gdl.model.predicates.booleans.And;
import org.s1ck.gdl.model.predicates.booleans.Not;
Expand All @@ -8,6 +9,7 @@
import org.s1ck.gdl.utils.Comparator;

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

/**
* Represents a MAX(p1,...,pn) term, where p1...pn are TimePoints
Expand Down Expand Up @@ -70,6 +72,11 @@ public long getUpperBound(){
return res;
}

@Override
public String getVariable() {
return null;
}

@Override
public boolean containsSelectorType(TimeSelector.TimeField type){
for(TimePoint p: args){
Expand Down Expand Up @@ -186,6 +193,15 @@ protected Predicate unfoldLTE(TimePoint arg){
return disjGt;
}

@Override
public ComparableExpression replaceGlobalByLocal(List<String> variables) {
TimePoint[] newArgs = new TimePoint[args.size()];
for(int i=0; i<args.size(); i++){
newArgs[i] = (TimePoint) args.get(i).replaceGlobalByLocal(variables);
}
return new MaxTimePoint(newArgs);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.s1ck.gdl.model.comparables.time;

import org.s1ck.gdl.model.comparables.ComparableExpression;
import org.s1ck.gdl.model.predicates.Predicate;
import org.s1ck.gdl.model.predicates.booleans.And;
import org.s1ck.gdl.model.predicates.booleans.Not;
Expand All @@ -8,6 +9,7 @@
import org.s1ck.gdl.utils.Comparator;

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

/**
* Represents a MAX(p1,...,pn) term, where p1...pn are TimePoints
Expand Down Expand Up @@ -67,6 +69,11 @@ public long getUpperBound(){
return res;
}

@Override
public String getVariable() {
return null;
}

@Override
public boolean containsSelectorType(TimeSelector.TimeField type){
for(TimePoint p: args){
Expand Down Expand Up @@ -223,5 +230,14 @@ public boolean isGlobal(){
return false;
}

@Override
public ComparableExpression replaceGlobalByLocal(List<String> variables) {
TimePoint[] newArgs = new TimePoint[args.size()];
for(int i=0; i<args.size(); i++){
newArgs[i] = (TimePoint) args.get(i).replaceGlobalByLocal(variables);
}
return new MinTimePoint(newArgs);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static org.s1ck.gdl.model.comparables.time.TimeSelector.GLOBAL_SELECTOR;
import static org.s1ck.gdl.utils.Comparator.*;
Expand Down Expand Up @@ -41,10 +42,15 @@ public PlusTimePoint(TimePoint timePoint, TimeConstant constant){
}

@Override
public ArrayList<String> getVariables() {
public Set<String> getVariables() {
return timePoint.getVariables();
}

@Override
public String getVariable() {
return timePoint.getVariable();
}

@Override
public long evaluate(){
long tp = timePoint.evaluate();
Expand Down Expand Up @@ -293,4 +299,9 @@ private Predicate forAllVariables(Comparator comp, ComparableExpression rhs, Lis
public boolean isGlobal(){
return timePoint.isGlobal();
}

@Override
public ComparableExpression replaceGlobalByLocal(List<String> variables) {
return new PlusTimePoint((TimePoint)timePoint.replaceGlobalByLocal(variables), constant);
}
}
16 changes: 14 additions & 2 deletions src/main/java/org/s1ck/gdl/model/comparables/time/TimeLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import java.time.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.s1ck.gdl.utils.Comparator;

Expand Down Expand Up @@ -124,8 +126,13 @@ public int getSecond(){


@Override
public ArrayList<String> getVariables(){
return new ArrayList<>();
public Set<String> getVariables(){
return new HashSet<>();
}

@Override
public String getVariable() {
return null;
}

@Override
Expand Down Expand Up @@ -182,4 +189,9 @@ public Predicate unfoldGlobal(Comparator comp, ComparableExpression rhs, List<St
public boolean isGlobal(){
return false;
}

@Override
public ComparableExpression replaceGlobalByLocal(List<String> variables) {
return this;
}
}
16 changes: 0 additions & 16 deletions src/main/java/org/s1ck/gdl/model/comparables/time/TimePoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,6 @@ public abstract class TimePoint implements ComparableExpression {
*/
public static final long UNDEFINED = -1L;

/**
* Returns null, no variable involved in a timestamp by default
* Not often used in implementations, as typically more than one variable can be involved
* @return null
*/
@Override
public String getVariable() {
return null;
}

/**
* all variables involved in the expression
* @return ArrayList of variable names (potentially empty)
*/
public abstract ArrayList<String> getVariables();

/**
* calculates the value of the timestamp (UNIX epoch long), if possible.
* E.g., a timestamp like v.VAL_FROM can not be assigned a unique long value
Expand Down
Loading

0 comments on commit d02429f

Please sign in to comment.