Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CQL temporal support #105

Merged
merged 4 commits into from
Mar 14, 2022
Merged
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
5 changes: 3 additions & 2 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ It includes [*OGC API - Features*](http://docs.opengeospatial.org/is/17-069r3/17
- `INTERSECTS`,`DISJOINT`,`CONTAINS`,`WITHIN`,`EQUALS`,`CROSSES`,`OVERLAPS`,`TOUCHES`
- [x] distance predicate
- `DWITHIN`
- [ ] temporal literals
- [ ] temporal predicates
- [x] temporal literals
- `1999-01-01`, `2001-12-25T10:01:02`
- [x] temporal predicates
- [ ] functions

### Output formats
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Add the `groupby` query parameter, and ability to aggregate query features
* Support OGC API query parameter `sortby` (`orderBy` is deprecated)
* Add OGC API query parameters `crs` and `bbox-crs` (#98)
* Add support for CQL filtering with `filter` and `filter-crs` query parameters (#101, #102, #103)
* Add CQL filtering query parameters `filter` and `filter-crs` (#101, #102, #103, #105)

### Performance Improvements

Expand Down
24 changes: 24 additions & 0 deletions hugo/content/usage/cql.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,27 @@ The `DWITHIN` predicate allows testing whether a geometry lies within a given di
```
DWITHIN(geom, POINT(-100 49), 0.1)
```

## Temporal filters

Temporal filtering in CQL supports date-time literals and the ability to use
them in conditions against temporal-valued properties
(PostgreSQL [dates or timestamps](https://www.postgresql.org/docs/current/datatype-datetime.html)).

Date-time literals specifiy a date, or a timestamp including a date and time (with optional seconds value):
```
2001-01-01
2001-01-01T10:23
2001-01-01T10:23:45
```

Temporal values can be compared using the conditional operators `<`,`<=`,`>`,`>=`,`=`,`<>`:

```
t > 2001-01-01T00:00 AND t <= 2002-12-31T11:59:59
```

They can also be used in the `BETWEEN` predicate:
```
t BETWEEN 2001-01-01 AND 2001-12-31
```
25 changes: 13 additions & 12 deletions internal/cql/CQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# CQL2 Antlr grammar, with small modifications.
# - Additions: ILIKE

# Build: in this dir: antlr -Dlanguage=Go -package cql CQL.g4 CqlLexer.g4
# Build: in this dir: antlr -Dlanguage=Go -package cql CQLParser.g4 CqlLexer.g4
#
# See examples:
# https://portal.ogc.org/files/96288#cql-bnf
Expand Down Expand Up @@ -33,7 +33,7 @@ predicate : binaryComparisonPredicate
| inPredicate
| spatialPredicate
| distancePredicate
// | temporalPredicate
| temporalPredicate
// | arrayPredicate
// | existencePredicate
;
Expand All @@ -50,8 +50,8 @@ binaryComparisonPredicate : scalarExpression ComparisonOperator scalarExpression
likePredicate : propertyName (NOT)? ( LIKE | ILIKE ) characterLiteral;

betweenPredicate : propertyName (NOT)? BETWEEN
scalarExpression AND scalarExpression ;
// (scalarValue | temporalExpression) AND (scalarValue | temporalExpression);
// scalarExpression AND scalarExpression ;
(scalarExpression | temporalExpression) AND (scalarExpression | temporalExpression);

isNullPredicate : propertyName IS (NOT)? NULL;

Expand All @@ -68,11 +68,11 @@ scalarExpression : scalarValue
;

scalarValue : propertyName
| characterLiteral
| numericLiteral
| booleanLiteral
// | function
;
| characterLiteral
| numericLiteral
| booleanLiteral
// | function
;

propertyName: Identifier;
characterLiteral: CharacterStringLiteral;
Expand Down Expand Up @@ -129,16 +129,17 @@ coordinate : NumericLiteral NumericLiteral;
# A temporal predicate evaluates if two temporal expressions satisfy the
# specified temporal operator.
#============================================================================*/
/*
temporalPredicate : temporalExpression (TemporalOperator | ComparisonOperator) temporalExpression;

temporalPredicate : temporalExpression ComparisonOperator temporalExpression;
//temporalPredicate : temporalExpression (TemporalOperator | ComparisonOperator) temporalExpression;

temporalExpression : propertyName
| temporalLiteral
//| function
;

temporalLiteral: TemporalLiteral;
*/


/*============================================================================
# The IN predicate
Expand Down
20 changes: 16 additions & 4 deletions internal/cql/CQLParser.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,21 @@ Exponent=63
SignedInteger=64
UnsignedInteger=65
Sign=66
WS=67
CharacterStringLiteral=68
QuotedQuote=69
TemporalLiteral=67
Instant=68
FullDate=69
DateYear=70
DateMonth=71
DateDay=72
UtcTime=73
TimeZoneOffset=74
TimeHour=75
TimeMinute=76
TimeSecond=77
NOW=78
WS=79
CharacterStringLiteral=80
QuotedQuote=81
'<'=2
'='=3
'>'=4
Expand All @@ -90,4 +102,4 @@ QuotedQuote=69
';'=53
'?'=54
'|'=55
'\'\''=69
'\'\''=81
12 changes: 6 additions & 6 deletions internal/cql/CqlLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -176,22 +176,22 @@ Sign : PLUS | MINUS;
# Definition of TEMPORAL literals
#============================================================================*/

/*
TemporalLiteral : Instant | Interval;
TemporalLiteral : Instant
// | Interval
;
Instant : FullDate | FullDate 'T' UtcTime | NOW LEFTPAREN RIGHTPAREN;
Interval : (InstantInInterval)? SOLIDUS (InstantInInterval)?;
InstantInInterval : '..' | Instant;
//Interval : (InstantInInterval)? SOLIDUS (InstantInInterval)?;
//InstantInInterval : '..' | Instant;
FullDate : DateYear '-' DateMonth '-' DateDay;
DateYear : DIGIT DIGIT DIGIT DIGIT;
DateMonth : DIGIT DIGIT;
DateDay : DIGIT DIGIT;
UtcTime : TimeHour ':' TimeMinute ':' TimeSecond (TimeZoneOffset)?;
UtcTime : TimeHour ':' TimeMinute (':' TimeSecond)? (TimeZoneOffset)?;
TimeZoneOffset : 'Z' | Sign TimeHour ':' TimeMinute;
TimeHour : DIGIT DIGIT;
TimeMinute : DIGIT DIGIT;
TimeSecond : DIGIT DIGIT (PERIOD (DIGIT)+)?;
NOW : N O W;
*/

/*============================================================================
# ANTLR ignore whitespace
Expand Down
20 changes: 16 additions & 4 deletions internal/cql/CqlLexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,21 @@ Exponent=63
SignedInteger=64
UnsignedInteger=65
Sign=66
WS=67
CharacterStringLiteral=68
QuotedQuote=69
TemporalLiteral=67
Instant=68
FullDate=69
DateYear=70
DateMonth=71
DateDay=72
UtcTime=73
TimeZoneOffset=74
TimeHour=75
TimeMinute=76
TimeSecond=77
NOW=78
WS=79
CharacterStringLiteral=80
QuotedQuote=81
'<'=2
'='=3
'>'=4
Expand All @@ -90,4 +102,4 @@ QuotedQuote=69
';'=53
'?'=54
'|'=55
'\'\''=69
'\'\''=81
47 changes: 45 additions & 2 deletions internal/cql/cql.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ func (l *cqlListener) ExitPredicate(ctx *PredicateContext) {
sql = sqlFor(ctx.SpatialPredicate())
} else if ctx.DistancePredicate() != nil {
sql = sqlFor(ctx.DistancePredicate())
} else if ctx.TemporalPredicate() != nil {
sql = sqlFor(ctx.TemporalPredicate())
}
ctx.SetSql(sql)
}
Expand Down Expand Up @@ -323,8 +325,20 @@ func (l *cqlListener) ExitBetweenPredicate(ctx *BetweenPredicateContext) {
if ctx.NOT() != nil {
not = " NOT"
}
expr1 := sqlFor(ctx.ScalarExpression(0))
expr2 := sqlFor(ctx.ScalarExpression(1))
var expr1 string
if ctx.ScalarExpression(0) != nil {
expr1 = sqlFor(ctx.ScalarExpression(0))
}
if ctx.TemporalExpression(0) != nil {
expr1 = sqlFor(ctx.TemporalExpression(0))
}
var expr2 string
if ctx.ScalarExpression(0) != nil {
expr2 = sqlFor(ctx.ScalarExpression(1))
}
if ctx.TemporalExpression(0) != nil {
expr2 = sqlFor(ctx.TemporalExpression(1))
}
sql := " " + prop + not + " BETWEEN " + expr1 + " AND " + expr2
ctx.SetSql(sql)
}
Expand Down Expand Up @@ -400,6 +414,35 @@ func (l *cqlListener) ExitDistancePredicate(ctx *DistancePredicateContext) {
ctx.SetSql(sb.String())
}

func (l *cqlListener) ExitTemporalPredicate(ctx *TemporalPredicateContext) {
expr1 := sqlFor(ctx.TemporalExpression(0))
expr2 := sqlFor(ctx.TemporalExpression(1))
op := getNodeText(ctx.ComparisonOperator())
sql := expr1 + " " + op + " " + expr2
ctx.SetSql(sql)
}

func (l *cqlListener) ExitTemporalExpression(ctx *TemporalExpressionContext) {
var sb strings.Builder
if ctx.PropertyName() != nil {
sb.WriteString(quotedName(getText(ctx.PropertyName())))
} else {
sb.WriteString(sqlFor(ctx.TemporalLiteral()))
}
ctx.SetSql(sb.String())
}

func (l *cqlListener) ExitTemporalLiteral(ctx *TemporalLiteralContext) {
val := strings.ToUpper(ctx.GetText())
//var sql string
if strings.HasPrefix(val, "NOW") {
val = "NOW"
}
sql := fmt.Sprintf("timestamp '%s'", val)
//TODO: handle NOW()
ctx.SetSql(sql)
}

func (l *cqlListener) ExitGeomExpression(ctx *GeomExpressionContext) {
var sb strings.Builder
if ctx.PropertyName() != nil {
Expand Down
Loading