Skip to content

Commit

Permalink
[s1ck#49] generalized asOf predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
lc0197 committed Aug 20, 2020
1 parent 0402fb1 commit 4dcf05c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
7 changes: 6 additions & 1 deletion src/main/antlr4/org/s1ck/gdl/GDL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ temporalComparison
timeFunc
: interval '.' intervalFunc #intvF
| timePoint '.' stampFunc #stmpF
| Identifier '.asOf(' timePoint ')' #asOf
//| Identifier '.asOf(' timePoint ')' #asOf
;

// intervals
Expand Down Expand Up @@ -227,6 +227,7 @@ intervalFunc
| shorterThanOperator
| lengthAtLeastOperator
| lengthAtMostOperator
| asOfOperator
;

overlapsIntervallOperator
Expand Down Expand Up @@ -282,6 +283,10 @@ lengthAtMostOperator
: 'lengthAtMost(' (interval | timeConstant) ')'
;

asOfOperator
: 'asOf(' timePoint ')'
;

timeConstant
: 'Millis(' IntegerLiteral ')'
| 'Seconds(' IntegerLiteral ')'
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/s1ck/gdl/GDLLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,10 @@ public void enterStmpF(GDLParser.StmpFContext ctx) {
*
* @param ctx asOf context
*/
@Override
/*@Override
public void enterAsOf(GDLParser.AsOfContext ctx) {
currentPredicates.add(temporalLoader.createAsOf(ctx));
}
}*/

/**
* Returns the literal for all "Now" literals in the query
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/org/s1ck/gdl/GDLLoaderTemporal.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ private Predicate createIntervalPredicates(TimePoint from, TimePoint to, GDLPars
return createLengthAtLeastPredicates(from, to, intervalFunc.lengthAtLeastOperator());
} else if (intervalFunc.lengthAtMostOperator() != null) {
return createLengthAtMostPredicates(from, to, intervalFunc.lengthAtMostOperator());
} else if(intervalFunc.asOfOperator() != null){
return createAsOfPredicates(from, to, intervalFunc.asOfOperator());
}
return null;
}
Expand Down Expand Up @@ -477,6 +479,16 @@ private Predicate createLengthAtMostPredicates(TimePoint from, TimePoint to,
return durationPredicate;
}

private Predicate createAsOfPredicates(TimePoint from, TimePoint to,
GDLParser.AsOfOperatorContext ctx){
TimePoint arg = buildTimePoint(ctx.timePoint());
Predicate asOfPredicate = new And(
new Comparison(from, LTE, arg),
new Comparison(arg, LTE, to)
);
return asOfPredicate;
}

/**
* Creates a TimeConstant given a suitable context. Constants can be a constant number
* of days ({@code Days(n)}), hours ({@code Hours(n)}), minutes ({@code Minutes(n)}),
Expand Down Expand Up @@ -679,9 +691,9 @@ private Predicate createAfterPredicates(TimePoint from, GDLParser.AfterPointOper
/**
* Create asOf predicate: a.tx_from<=point AND a.tx_to>= point
* @param ctx asOf context
* @return asOf predicate
* @return predicate
*/
Predicate createAsOf(GDLParser.AsOfContext ctx) {
/* Predicate createAsOf(GDLParser.AsOfContext ctx) {
TimePoint tp = buildTimePoint(ctx.timePoint());
String identifier = loader.resolveIdentifier(ctx.Identifier().getText());
return new And(
Expand All @@ -695,7 +707,7 @@ Predicate createAsOf(GDLParser.AsOfContext ctx) {
tp)
);
}
}*/

/**
* Returns the literal for all "Now" literals in the query
Expand Down
24 changes: 20 additions & 4 deletions src/test/java/org/s1ck/gdl/GDLLoaderTemporalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,36 @@ public void succeedsTest(){
@Test
public void asOfTest(){
GDLLoader loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE e.asOf(Timestamp(1970-01-01))");
"WHERE e.tx.asOf(Timestamp(1970-01-01))");
Predicate expected = new And(
new Comparison(
new TimeSelector("e", TX_FROM),
LTE,
new TimeLiteral("1970-01-01")
),
new Comparison(
new TimeSelector("e", TX_TO),
GTE,
new TimeLiteral("1970-01-01")
new TimeLiteral("1970-01-01"),
LTE,
new TimeSelector("e", TX_TO)
)
);
assertPredicateEquals(loader.getPredicates().get(), expected);

loader = getLoaderFromGDLString("MATCH (a)-[e]->(b) " +
"WHERE e.val.asOf(a.tx_from");
expected = new And(
new Comparison(
new TimeSelector("e", VAL_FROM),
LTE,
new TimeSelector("a", TX_FROM)
),
new Comparison(
new TimeSelector("a", TX_FROM),
LTE,
new TimeSelector("e", VAL_TO)
)
);
assertPredicateEquals(loader.getPredicates().get(), expected);
}

@Test
Expand Down

0 comments on commit 4dcf05c

Please sign in to comment.