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

[RE-361] add datetime operator #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 5 additions & 0 deletions parser/JsonQuery.g4
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,17 @@ value
| VERSION #version
| STRING #string
| DOUBLE #double
| DATETIME #datetime
| '-'? INT EXP? #long
| listInts #listOfInts
| listDoubles #listOfDoubles
| listStrings #listOfStrings
;

DATETIME
: DIGIT DIGIT DIGIT DIGIT '-' DIGIT DIGIT '-' DIGIT DIGIT 'T' DIGIT DIGIT ':' DIGIT DIGIT
;

VERSION
: INT '.' INT '.' INT
;
Expand Down
4 changes: 3 additions & 1 deletion parser/JsonQuery.interp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ null
null
null
null
null
'\n'
null
null
Expand Down Expand Up @@ -63,6 +64,7 @@ PLUS
MINUS
DIVISON
ATTRNAME
DATETIME
VERSION
STRING
DOUBLE
Expand All @@ -88,4 +90,4 @@ subListOfInts


atn:
[4, 1, 34, 137, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 1, 0, 1, 0, 3, 0, 27, 8, 0, 1, 0, 3, 0, 30, 8, 0, 1, 0, 1, 0, 3, 0, 34, 8, 0, 1, 0, 1, 0, 3, 0, 38, 8, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,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31, 1, 0, 0, 0, 31, 33, 5, 1, 0, 0, 32, 34, 5, 34, 0, 0, 33, 32, 1, 0, 0, 0, 33, 34, 1, 0, 0, 0, 34, 35, 1, 0, 0, 0, 35, 37, 3, 0, 0, 0, 36, 38, 5, 34, 0, 0, 37, 36, 1, 0, 0, 0, 37, 38, 1, 0, 0, 0, 38, 39, 1, 0, 0, 0, 39, 40, 5, 2, 0, 0, 40, 62, 1, 0, 0, 0, 41, 42, 3, 2, 1, 0, 42, 43, 5, 34, 0, 0, 43, 44, 5, 3, 0, 0, 44, 62, 1, 0, 0, 0, 45, 46, 3, 2, 1, 0, 46, 47, 5, 34, 0, 0, 47, 48, 7, 0, 0, 0, 48, 49, 5, 34, 0, 0, 49, 50, 3, 6, 3, 0, 50, 62, 1, 0, 0, 0, 51, 52, 7, 1, 0, 0, 52, 53, 5, 34, 0, 0, 53, 54, 5, 1, 0, 0, 54, 55, 3, 16, 8, 0, 55, 56, 5, 2, 0, 0, 56, 57, 5, 34, 0, 0, 57, 58, 7, 2, 0, 0, 58, 59, 5, 34, 0, 0, 59, 60, 3, 6, 3, 0, 60, 62, 1, 0, 0, 0, 61, 24, 1, 0, 0, 0, 61, 41, 1, 0, 0, 0, 61, 45, 1, 0, 0, 0, 61, 51, 1, 0, 0, 0, 62, 70, 1, 0, 0, 0, 63, 64, 10, 4, 0, 0, 64, 65, 5, 34, 0, 0, 65, 66, 5, 9, 0, 0, 66, 67, 5, 34, 0, 0, 67, 69, 3, 0, 0, 5, 68, 63, 1, 0, 0, 0, 69, 72, 1, 0, 0, 0, 70, 68, 1, 0, 0, 0, 70, 71, 1, 0, 0, 0, 71, 1, 1, 0, 0, 0, 72, 70, 1, 0, 0, 0, 73, 75, 5, 26, 0, 0, 74, 76, 3, 4, 2, 0, 75, 74, 1, 0, 0, 0, 75, 76, 1, 0, 0, 0, 76, 3, 1, 0, 0, 0, 77, 78, 5, 4, 0, 0, 78, 79, 3, 2, 1, 0, 79, 5, 1, 0, 0, 0, 80, 96, 5, 10, 0, 0, 81, 96, 5, 11, 0, 0, 82, 96, 5, 27, 0, 0, 83, 96, 5, 28, 0, 0, 84, 96, 5, 29, 0, 0, 85, 87, 5, 5, 0, 0, 86, 85, 1, 0, 0, 0, 86, 87, 1, 0, 0, 0, 87, 88, 1, 0, 0, 0, 88, 90, 5, 30, 0, 0, 89, 91, 5, 31, 0, 0, 90, 89, 1, 0, 0, 0, 90, 91, 1, 0, 0, 0, 91, 96, 1, 0, 0, 0, 92, 96, 3, 20, 10, 0, 93, 96, 3, 12, 6, 0, 94, 96, 3, 8, 4, 0, 95, 80, 1, 0, 0, 0, 95, 81, 1, 0, 0, 0, 95, 82, 1, 0, 0, 0, 95, 83, 1, 0, 0, 0, 95, 84, 1, 0, 0, 0, 95, 86, 1, 0, 0, 0, 95, 92, 1, 0, 0, 0, 95, 93, 1, 0, 0, 0, 95, 94, 1, 0, 0, 0, 96, 7, 1, 0, 0, 0, 97, 98, 5, 6, 0, 0, 98, 99, 3, 10, 5, 0, 99, 9, 1, 0, 0, 0, 100, 101, 5, 28, 0, 0, 101, 102, 5, 33, 0, 0, 102, 106, 3, 10, 5, 0, 103, 104, 5, 28, 0, 0, 104, 106, 5, 7, 0, 0, 105, 100, 1, 0, 0, 0, 105, 103, 1, 0, 0, 0, 106, 11, 1, 0, 0, 0, 107, 108, 5, 6, 0, 0, 108, 109, 3, 14, 7, 0, 109, 13, 1, 0, 0, 0, 110, 111, 5, 29, 0, 0, 111, 112, 5, 33, 0, 0, 112, 116, 3, 14, 7, 0, 113, 114, 5, 29, 0, 0, 114, 116, 5, 7, 0, 0, 115, 110, 1, 0, 0, 0, 115, 113, 1, 0, 0, 0, 116, 15, 1, 0, 0, 0, 117, 118, 3, 18, 9, 0, 118, 17, 1, 0, 0, 0, 119, 120, 3, 2, 1, 0, 120, 121, 5, 33, 0, 0, 121, 122, 3, 18, 9, 0, 122, 125, 1, 0, 0, 0, 123, 125, 3, 2, 1, 0, 124, 119, 1, 0, 0, 0,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 3, 7, 117, 8, 7, 1, 8, 1, 8, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9, 3, 9, 126, 8, 9, 1, 10, 1, 10, 1, 10, 1, 11, 1, 11, 1, 11, 1, 11, 1, 11, 3, 11, 136, 8, 11, 1, 11, 0, 1, 0, 12, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 0, 3, 1, 0, 12, 21, 1, 0, 22, 25, 1, 0, 13, 18, 149, 0, 61, 1, 0, 0, 0, 2, 73, 1, 0, 0, 0, 4, 77, 1, 0, 0, 0, 6, 96, 1, 0, 0, 0, 8, 98, 1, 0, 0, 0, 10, 106, 1, 0, 0, 0, 12, 108, 1, 0, 0, 0, 14, 116, 1, 0, 0, 0, 16, 118, 1, 0, 0, 0, 18, 125, 1, 0, 0, 0, 20, 127, 1, 0, 0, 0, 22, 135, 1, 0, 0, 0, 24, 26, 6, 0, -1, 0, 25, 27, 5, 8, 0, 0, 26, 25, 1, 0, 0, 0, 26, 27, 1, 0, 0, 0, 27, 29, 1, 0, 0, 0, 28, 30, 5, 35, 0, 0, 29, 28, 1, 0, 0, 0, 29, 30, 1, 0, 0, 0, 30, 31, 1, 0, 0, 0, 31, 33, 5, 1, 0, 0, 32, 34, 5, 35, 0, 0, 33, 32, 1, 0, 0, 0, 33, 34, 1, 0, 0, 0, 34, 35, 1, 0, 0, 0, 35, 37, 3, 0, 0, 0, 36, 38, 5, 35, 0, 0, 37, 36, 1, 0, 0, 0, 37, 38, 1, 0, 0, 0, 38, 39, 1, 0, 0, 0, 39, 40, 5, 2, 0, 0, 40, 62, 1, 0, 0, 0, 41, 42, 3, 2, 1, 0, 42, 43, 5, 35, 0, 0, 43, 44, 5, 3, 0, 0, 44, 62, 1, 0, 0, 0, 45, 46, 3, 2, 1, 0, 46, 47, 5, 35, 0, 0, 47, 48, 7, 0, 0, 0, 48, 49, 5, 35, 0, 0, 49, 50, 3, 6, 3, 0, 50, 62, 1, 0, 0, 0, 51, 52, 7, 1, 0, 0, 52, 53, 5, 35, 0, 0, 53, 54, 5, 1, 0, 0, 54, 55, 3, 16, 8, 0, 55, 56, 5, 2, 0, 0, 56, 57, 5, 35, 0, 0, 57, 58, 7, 2, 0, 0, 58, 59, 5, 35, 0, 0, 59, 60, 3, 6, 3, 0, 60, 62, 1, 0, 0, 0, 61, 24, 1, 0, 0, 0, 61, 41, 1, 0, 0, 0, 61, 45, 1, 0, 0, 0, 61, 51, 1, 0, 0, 0, 62, 70, 1, 0, 0, 0, 63, 64, 10, 4, 0, 0, 64, 65, 5, 35, 0, 0, 65, 66, 5, 9, 0, 0, 66, 67, 5, 35, 0, 0, 67, 69, 3, 0, 0, 5, 68, 63, 1, 0, 0, 0, 69, 72, 1, 0, 0, 0, 70, 68, 1, 0, 0, 0, 70, 71, 1, 0, 0, 0, 71, 1, 1, 0, 0, 0, 72, 70, 1, 0, 0, 0, 73, 75, 5, 26, 0, 0, 74, 76, 3, 4, 2, 0, 75, 74, 1, 0, 0, 0, 75, 76, 1, 0, 0, 0, 76, 3, 1, 0, 0, 0, 77, 78, 5, 4, 0, 0, 78, 79, 3, 2, 1, 0, 79, 5, 1, 0, 0, 0, 80, 97, 5, 10, 0, 0, 81, 97, 5, 11, 0, 0, 82, 97, 5, 28, 0, 0, 83, 97, 5, 29, 0, 0, 84, 97, 5, 30, 0, 0, 85, 97, 5, 27, 0, 0, 86, 88, 5, 5, 0, 0, 87, 86, 1, 0, 0, 0, 87, 88, 1, 0, 0, 0, 88, 89, 1, 0, 0, 0, 89, 91, 5, 31, 0, 0, 90, 92, 5, 32, 0, 0, 91, 90, 1, 0, 0, 0, 91, 92, 1, 0, 0, 0, 92, 97, 1, 0, 0, 0, 93, 97, 3, 20, 10, 0, 94, 97, 3, 12, 6, 0, 95, 97, 3, 8, 4, 0, 96, 80, 1, 0, 0, 0, 96, 81, 1, 0, 0, 0, 96, 82, 1, 0, 0, 0, 96, 83, 1, 0, 0, 0, 96, 84, 1, 0, 0, 0, 96, 85, 1, 0, 0, 0, 96, 87, 1, 0, 0, 0, 96, 93, 1, 0, 0, 0, 96, 94, 1, 0, 0, 0, 96, 95, 1, 0, 0, 0, 97, 7, 1, 0, 0, 0, 98, 99, 5, 6, 0, 0, 99, 100, 3, 10, 5, 0, 100, 9, 1, 0, 0, 0, 101, 102, 5, 29, 0, 0, 102, 103, 5, 34, 0, 0, 103, 107, 3, 10, 5, 0, 104, 105, 5, 29, 0, 0, 105, 107, 5, 7, 0, 0, 106, 101, 1, 0, 0, 0, 106, 104, 1, 0, 0, 0, 107, 11, 1, 0, 0, 0, 108, 109, 5, 6, 0, 0, 109, 110, 3, 14, 7, 0, 110, 13, 1, 0, 0, 0, 111, 112, 5, 30, 0, 0, 112, 113, 5, 34, 0, 0, 113, 117, 3, 14, 7, 0, 114, 115, 5, 30, 0, 0, 115, 117, 5, 7, 0, 0, 116, 111, 1, 0, 0, 0, 116, 114, 1, 0, 0, 0, 117, 15, 1, 0, 0, 0, 118, 119, 3, 18, 9, 0, 119, 17, 1, 0, 0, 0, 120, 121, 3, 2, 1, 0, 121, 122, 5, 34, 0, 0, 122, 123, 3, 18, 9, 0, 123, 126, 1, 0, 0, 0, 124, 126, 3, 2, 1, 0, 125, 120, 1, 0, 0, 0, 125, 124, 1, 0, 0, 0, 126, 19, 1, 0, 0, 0, 127, 128, 5, 6, 0, 0, 128, 129, 3, 22, 11, 0, 129, 21, 1, 0, 0, 0, 130, 131, 5, 31, 0, 0, 131, 132, 5, 34, 0, 0, 132, 136, 3, 22, 11, 0, 133, 134, 5, 31, 0, 0, 134, 136, 5, 7, 0, 0, 135, 130, 1, 0, 0, 0, 135, 133, 1, 0, 0, 0, 136, 23, 1, 0, 0, 0, 14, 26, 29, 33, 37, 61, 70, 75, 87, 91, 96, 106, 116, 125, 135]
19 changes: 10 additions & 9 deletions parser/JsonQuery.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ PLUS=23
MINUS=24
DIVISON=25
ATTRNAME=26
VERSION=27
STRING=28
DOUBLE=29
INT=30
EXP=31
NEWLINE=32
COMMA=33
SP=34
DATETIME=27
VERSION=28
STRING=29
DOUBLE=30
INT=31
EXP=32
NEWLINE=33
COMMA=34
SP=35
'('=1
')'=2
'pr'=3
Expand All @@ -44,4 +45,4 @@ SP=34
'SUM'=23
'SUBTRACT'=24
'DIV'=25
'\n'=32
'\n'=33
5 changes: 4 additions & 1 deletion parser/JsonQueryLexer.interp

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions parser/JsonQueryLexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ PLUS=23
MINUS=24
DIVISON=25
ATTRNAME=26
VERSION=27
STRING=28
DOUBLE=29
INT=30
EXP=31
NEWLINE=32
COMMA=33
SP=34
DATETIME=27
VERSION=28
STRING=29
DOUBLE=30
INT=31
EXP=32
NEWLINE=33
COMMA=34
SP=35
'('=1
')'=2
'pr'=3
Expand All @@ -44,4 +45,4 @@ SP=34
'SUM'=23
'SUBTRACT'=24
'DIV'=25
'\n'=32
'\n'=33
85 changes: 85 additions & 0 deletions parser/datetime_operation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package parser

import (
"time"
)

const timeLayout = "2006-01-02T15:04"

type DateTimeOperation struct {
NullOperation
}

func (o *DateTimeOperation) EQ(left Operand, right Operand) (bool, error) {
l, r, err := o.get(left, right)
if err != nil {
return false, err
}
return l.Equal(r), nil
}

func (o *DateTimeOperation) NE(left Operand, right Operand) (bool, error) {
l, r, err := o.get(left, right)
if err != nil {
return false, err
}
return !l.Equal(r), nil
}

func (o *DateTimeOperation) GT(left Operand, right Operand) (bool, error) {
l, r, err := o.get(left, right)
if err != nil {
return false, err
}
return l.After(r), nil
}

func (o *DateTimeOperation) LT(left Operand, right Operand) (bool, error) {
l, r, err := o.get(left, right)
if err != nil {
return false, err
}
return l.Before(r), nil
}

func (o *DateTimeOperation) GE(left Operand, right Operand) (bool, error) {
l, r, err := o.get(left, right)
if err != nil {
return false, err
}
return l.After(r) || l.Equal(r), nil
}

func (o *DateTimeOperation) LE(left Operand, right Operand) (bool, error) {
l, r, err := o.get(left, right)
if err != nil {
return false, err
}
return l.Before(r) || l.Equal(r), nil
}

func (o *DateTimeOperation) getTime(operand Operand) (time.Time, error) {
if operand == nil {
return time.Time{}, ErrEvalOperandMissing
}
switch opVal := operand.(type) {
case string:
return time.Parse(timeLayout, opVal)
case time.Time:
return opVal, nil
default:
return time.Time{}, newErrInvalidOperand(operand, opVal)
}
}

func (o *DateTimeOperation) get(left Operand, right Operand) (time.Time, time.Time, error) {
leftVal, err := o.getTime(left)
if err != nil {
return time.Time{}, time.Time{}, err
}
rightVal, err := o.getTime(right)
if err != nil {
return time.Time{}, time.Time{}, err
}
return leftVal, rightVal, nil
}
11 changes: 5 additions & 6 deletions parser/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package parser

import (
"fmt"
"runtime/debug"

"github.com/antlr4-go/antlr/v4"
)
Expand All @@ -19,9 +20,8 @@ func NewEvaluator(rule string) (ret *Evaluator, retErr error) {
// antlr lib has panics for exceptions so we have to put a recover here
// in the unlikely case there is an exception
defer func() {
info := recover()
if info != nil {
retErr = fmt.Errorf("%q", info)
if r := recover(); r != nil {
retErr = fmt.Errorf("stacktrace from panic: \n" + string(debug.Stack()))
}
}()
input := antlr.NewInputStream(rule)
Expand Down Expand Up @@ -53,9 +53,8 @@ func (e *Evaluator) Process(items map[string]interface{}) (ret bool, retErr erro
// antlr lib has panics for exceptions so we have to put a recover here
// in the unlikely case there is an exception
defer func() {
info := recover()
if info != nil {
retErr = fmt.Errorf("%q", info)
if r := recover(); r != nil {
retErr = fmt.Errorf("stacktrace from panic: \n" + string(debug.Stack()))
ret = false
}
}()
Expand Down
Loading
Loading