Skip to content

Commit

Permalink
[s1ck#49] ensured that multiple "Now" literals in the query all get t…
Browse files Browse the repository at this point in the history
…he same value
  • Loading branch information
lc0197 committed Jun 23, 2020
1 parent 4ee4902 commit d847453
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/main/java/org/s1ck/gdl/GDLHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.s1ck.gdl.model.Edge;
import org.s1ck.gdl.model.Graph;
import org.s1ck.gdl.model.Vertex;
import org.s1ck.gdl.model.comparables.time.TimeLiteral;
import org.s1ck.gdl.model.predicates.Predicate;

import java.io.IOException;
Expand Down Expand Up @@ -164,6 +165,14 @@ public Map<String, Edge> getEdgeCache(boolean includeUserDefined, boolean includ
return loader.getEdgeCache(includeUserDefined, includeAutoGenerated);
}

/**
* Returns the literal for all "Now" literals in the query
* @return literal for all "Now" literals in the query
*/
public TimeLiteral getNowLit(){
return loader.getNowLit();
}

/**
* Builds a GDL Handler.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/s1ck/gdl/GDLLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,14 @@ public void enterAsOf(GDLParser.AsOfContext ctx) {
currentPredicates.add(temporalLoader.createAsOf(ctx));
}

/**
* Returns the literal for all "Now" literals in the query
* @return literal for all "Now" literals in the query
*/
public TimeLiteral getNowLit(){
return temporalLoader.getNowLit();
}

/**
* Called when we leave an NotExpression.
*
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/org/s1ck/gdl/GDLLoaderTemporal.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public class GDLLoaderTemporal {
*/
private final GDLLoader loader;

/**
* Used as value for all "Now" literals in the query
*/
private final TimeLiteral nowLit;

/**
* Creates a new instance
*
Expand All @@ -36,6 +41,7 @@ public class GDLLoaderTemporal {
public GDLLoaderTemporal(GDLLoader loader) {
this.predicateStack = new ArrayDeque<>();
this.loader = loader;
this.nowLit = new TimeLiteral("Now");
}

/**
Expand Down Expand Up @@ -76,7 +82,11 @@ private TimePoint buildTimePoint(GDLParser.TimePointContext ctx) {
* @return TimeLiteral
*/
private TimeLiteral buildTimeLiteral(GDLParser.TimeLiteralContext ctx) {
return new TimeLiteral(ctx.getText().trim());
if(ctx.getText().trim().toLowerCase().equals("now")){
return new TimeLiteral(nowLit.getMilliseconds());
} else{
return new TimeLiteral(ctx.getText().trim());
}
}

/**
Expand Down Expand Up @@ -662,4 +672,12 @@ Predicate createAsOf(GDLParser.AsOfContext ctx) {

);
}

/**
* Returns the literal for all "Now" literals in the query
* @return literal for all "Now" literals in the query
*/
public TimeLiteral getNowLit(){
return nowLit;
}
}
12 changes: 12 additions & 0 deletions src/test/java/org/s1ck/gdl/GDLLoaderTemporalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.s1ck.gdl.model.predicates.expressions.Comparison;
import org.s1ck.gdl.utils.Comparator;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.s1ck.gdl.model.comparables.time.TimeSelector.TimeField.*;
import static org.s1ck.gdl.utils.Comparator.*;
Expand Down Expand Up @@ -529,6 +530,17 @@ private void lengthComparisonTest(String operator, Comparator comparator){
assertPredicateEquals(loader.getPredicates().get(), expected);
}

@Test
public void timeLitNowTest(){
GDLLoader loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE Now<=Now");
System.out.println(loader.getPredicates());
Comparison comp = (Comparison) loader.getPredicates().get();
// all "Now"s should have the exact same value
assertEquals(comp.getComparableExpressions()[0], comp.getComparableExpressions()[1]);
assertEquals(loader.getNowLit(), comp.getComparableExpressions()[0]);
}

/**
* Does not fail iff {@code result==expected} or {@code result.switchSides()==expected}
* @param result predicate to compare
Expand Down

0 comments on commit d847453

Please sign in to comment.