Skip to content
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
25 changes: 23 additions & 2 deletions parser/JsonQuery.g4
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ query
: NOT? SP? '(' SP? query SP? ')' #parenExp
| query SP LOGICAL_OPERATOR SP query #logicalExp
| attrPath SP 'pr' #presentExp
| attrPath SP op=( EQ | NE | GT | LT | GE | LE | CO | SW | EW | IN ) SP value #compareExp
| attrPath SP op=( EQ | NE | GT | LT | GE | LE | CO | SW | EW | IN | LIKE ) SP value #compareExp
;

NOT
Expand All @@ -23,6 +23,10 @@ NULL
: 'null'
;

LIKE
: 'like' | 'regx'
;

IN: 'IN' | 'in';
EQ : 'eq' | 'EQ' | '==';
NE : 'ne' | 'NE' | '!=';
Expand Down Expand Up @@ -74,9 +78,26 @@ VERSION
;

STRING
: '"' (ESC | ~ ["\\])* '"'
: '"' (ESC | ~ ["\\] | REGEX)* '"'
;

fragment REGEX: CHOICE | ATOM_CHARACTER+;

fragment CHOICE: GROUP ('|' GROUP)*;

fragment GROUP: '(' (ATOM_CHARACTER | GROUP)* ')';

fragment ATOM_CHARACTER: CHAR | ESCAPE_CHAR | SPECIAL_CHAR | REPEAT;

fragment REPEAT: '{' INT '}';

fragment CHAR: [a-zA-Z0-9];

fragment SPECIAL_CHAR: ',' | '.' | ':' | ';' | '!' | '?' | '(' | ')' | '[' | ']' | '{' | '}' | '-' | '=' | '>' | '<' | '\'' | '\\' | '&' | '|' | '+' | '*' | '$' | '%' | '/' | '^' | '~' | '`' | ' ';

fragment ESCAPE_CHAR: '\\' ~[abfnrtv];


listStrings
: '[' subListOfStrings
;
Expand Down
4 changes: 3 additions & 1 deletion parser/JsonQuery.interp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ null
null
null
null
null
'\n'
null
null
Expand All @@ -44,6 +45,7 @@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 0, 16, 103, 1, 0, 0, 0, 18, 111, 1, 0, 0, 0, 20, 22, 6, 0, -1, 0, 21, 23, 5, 8, 0, 0, 22, 21, 1, 0, 0, 0, 22, 23, 1, 0, 0, 0, 23, 25, 1, 0, 0, 0, 24, 26, 5, 30, 0, 0, 25, 24, 1, 0, 0, 0, 25, 26, 1, 0, 0, 0, 26, 27, 1, 0, 0, 0, 27, 29, 5, 1, 0, 0, 28, 30, 5, 30, 0, 0, 29, 28, 1, 0, 0, 0, 29, 30, 1, 0, 0, 0, 30, 31, 1, 0, 0, 0, 31, 33, 3, 0, 0, 0, 32, 34, 5, 30, 0, 0, 33, 32, 1, 0, 0, 0, 33, 34, 1, 0, 0, 0, 34, 35, 1, 0, 0, 0, 35, 36, 5, 2, 0, 0, 36, 48, 1, 0, 0, 0, 37, 38, 3, 2, 1, 0, 38, 39, 5, 30, 0, 0, 39, 40, 5, 3, 0, 0, 40, 48, 1, 0, 0, 0, 41, 42, 3, 2, 1, 0, 42, 43, 5, 30, 0, 0, 43, 44, 7, 0, 0, 0, 44, 45, 5, 30, 0, 0, 45, 46, 3, 6, 3, 0, 46, 48, 1, 0, 0, 0, 47, 20, 1, 0, 0, 0, 47, 37, 1, 0, 0, 0, 47, 41, 1, 0, 0, 0, 48, 56, 1, 0, 0, 0, 49, 50, 10, 3, 0, 0, 50, 51, 5, 30, 0, 0, 51, 52, 5, 9, 0, 0, 52, 53, 5, 30, 0, 0, 53, 55, 3, 0, 0, 4, 54, 49, 1, 0, 0, 0, 55, 58, 1, 0, 0, 0, 56, 54, 1, 0, 0, 0, 56, 57, 1, 0, 0, 0, 57, 1, 1, 0, 0, 0, 58, 56, 1, 0, 0, 0, 59, 61, 5, 22, 0, 0, 60, 62, 3, 4, 2, 0, 61, 60, 1, 0, 0, 0, 61, 62, 1, 0, 0, 0, 62, 3, 1, 0, 0, 0, 63, 64, 5, 4, 0, 0, 64, 65, 3, 2, 1, 0, 65, 5, 1, 0, 0, 0, 66, 82, 5, 10, 0, 0, 67, 82, 5, 11, 0, 0, 68, 82, 5, 23, 0, 0, 69, 82, 5, 24, 0, 0, 70, 82, 5, 25, 0, 0, 71, 73, 5, 5, 0, 0, 72, 71, 1, 0, 0, 0, 72, 73, 1, 0, 0, 0, 73, 74, 1, 0, 0, 0, 74, 76, 5, 26, 0, 0, 75, 77, 5, 27, 0, 0, 76, 75, 1, 0, 0, 0, 76, 77, 1, 0, 0, 0, 77, 82, 1, 0, 0, 0, 78, 82, 3, 16, 8, 0, 79, 82, 3, 12, 6, 0, 80, 82, 3, 8, 4, 0, 81, 66, 1, 0, 0, 0, 81, 67, 1, 0, 0, 0, 81, 68, 1, 0, 0, 0, 81, 69, 1, 0, 0, 0, 81, 70, 1, 0, 0, 0, 81, 72, 1, 0, 0, 0, 81, 78, 1, 0, 0, 0, 81, 79, 1, 0, 0, 0, 81, 80, 1, 0, 0, 0, 82, 7, 1, 0, 0, 0, 83, 84, 5, 6, 0, 0, 84, 85, 3, 10, 5, 0, 85, 9, 1, 0, 0, 0, 86, 87, 5, 24, 0, 0, 87, 88, 5, 29, 0, 0, 88, 92, 3, 10, 5, 0, 89, 90, 5, 24, 0, 0, 90, 92, 5, 7, 0, 0, 91, 86, 1, 0, 0, 0, 91, 89, 1, 0, 0, 0, 92, 11, 1, 0, 0, 0, 93, 94, 5, 6, 0, 0, 94, 95, 3, 14, 7, 0, 95, 13, 1, 0, 0, 0, 96, 97, 5, 25, 0, 0, 97, 98, 5, 29, 0, 0, 98, 102, 3, 14, 7, 0, 99, 100, 5, 25, 0, 0, 100, 102, 5, 7, 0, 0, 101, 96, 1, 0, 0, 0, 101, 99, 1, 0, 0, 0, 102, 15, 1, 0, 0, 0, 103, 104, 5, 6, 0, 0, 104, 105, 3, 18, 9, 0, 105, 17, 1, 0, 0, 0, 106, 107, 5, 26, 0, 0, 107, 108, 5, 29, 0, 0, 108, 112, 3, 18, 9, 0, 109, 110, 5, 26, 0, 0, 110, 112, 5, 7, 0, 0, 111, 106, 1, 0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, 0, 0, 16, 103, 1, 0, 0, 0, 18, 111, 1, 0, 0, 0, 20, 22, 6, 0, -1, 0, 21, 23, 5, 8, 0, 0, 22, 21, 1, 0, 0, 0, 22, 23, 1, 0, 0, 0, 23, 25, 1, 0, 0, 0, 24, 26, 5, 31, 0, 0, 25, 24, 1, 0, 0, 0, 25, 26, 1, 0, 0, 0, 26, 27, 1, 0, 0, 0, 27, 29, 5, 1, 0, 0, 28, 30, 5, 31, 0, 0, 29, 28, 1, 0, 0, 0, 29, 30, 1, 0, 0, 0, 30, 31, 1, 0, 0, 0, 31, 33, 3, 0, 0, 0, 32, 34, 5, 31, 0, 0, 33, 32, 1, 0, 0, 0, 33, 34, 1, 0, 0, 0, 34, 35, 1, 0, 0, 0, 35, 36, 5, 2, 0, 0, 36, 48, 1, 0, 0, 0, 37, 38, 3, 2, 1, 0, 38, 39, 5, 31, 0, 0, 39, 40, 5, 3, 0, 0, 40, 48, 1, 0, 0, 0, 41, 42, 3, 2, 1, 0, 42, 43, 5, 31, 0, 0, 43, 44, 7, 0, 0, 0, 44, 45, 5, 31, 0, 0, 45, 46, 3, 6, 3, 0, 46, 48, 1, 0, 0, 0, 47, 20, 1, 0, 0, 0, 47, 37, 1, 0, 0, 0, 47, 41, 1, 0, 0, 0, 48, 56, 1, 0, 0, 0, 49, 50, 10, 3, 0, 0, 50, 51, 5, 31, 0, 0, 51, 52, 5, 9, 0, 0, 52, 53, 5, 31, 0, 0, 53, 55, 3, 0, 0, 4, 54, 49, 1, 0, 0, 0, 55, 58, 1, 0, 0, 0, 56, 54, 1, 0, 0, 0, 56, 57, 1, 0, 0, 0, 57, 1, 1, 0, 0, 0, 58, 56, 1, 0, 0, 0, 59, 61, 5, 23, 0, 0, 60, 62, 3, 4, 2, 0, 61, 60, 1, 0, 0, 0, 61, 62, 1, 0, 0, 0, 62, 3, 1, 0, 0, 0, 63, 64, 5, 4, 0, 0, 64, 65, 3, 2, 1, 0, 65, 5, 1, 0, 0, 0, 66, 82, 5, 10, 0, 0, 67, 82, 5, 11, 0, 0, 68, 82, 5, 24, 0, 0, 69, 82, 5, 25, 0, 0, 70, 82, 5, 26, 0, 0, 71, 73, 5, 5, 0, 0, 72, 71, 1, 0, 0, 0, 72, 73, 1, 0, 0, 0, 73, 74, 1, 0, 0, 0, 74, 76, 5, 27, 0, 0, 75, 77, 5, 28, 0, 0, 76, 75, 1, 0, 0, 0, 76, 77, 1, 0, 0, 0, 77, 82, 1, 0, 0, 0, 78, 82, 3, 16, 8, 0, 79, 82, 3, 12, 6, 0, 80, 82, 3, 8, 4, 0, 81, 66, 1, 0, 0, 0, 81, 67, 1, 0, 0, 0, 81, 68, 1, 0, 0, 0, 81, 69, 1, 0, 0, 0, 81, 70, 1, 0, 0, 0, 81, 72, 1, 0, 0, 0, 81, 78, 1, 0, 0, 0, 81, 79, 1, 0, 0, 0, 81, 80, 1, 0, 0, 0, 82, 7, 1, 0, 0, 0, 83, 84, 5, 6, 0, 0, 84, 85, 3, 10, 5, 0, 85, 9, 1, 0, 0, 0, 86, 87, 5, 25, 0, 0, 87, 88, 5, 30, 0, 0, 88, 92, 3, 10, 5, 0, 89, 90, 5, 25, 0, 0, 90, 92, 5, 7, 0, 0, 91, 86, 1, 0, 0, 0, 91, 89, 1, 0, 0, 0, 92, 11, 1, 0, 0, 0, 93, 94, 5, 6, 0, 0, 94, 95, 3, 14, 7, 0, 95, 13, 1, 0, 0, 0, 96, 97, 5, 26, 0, 0, 97, 98, 5, 30, 0, 0, 98, 102, 3, 14, 7, 0, 99, 100, 5, 26, 0, 0, 100, 102, 5, 7, 0, 0, 101, 96, 1, 0, 0, 0, 101, 99, 1, 0, 0, 0, 102, 15, 1, 0, 0, 0, 103, 104, 5, 6, 0, 0, 104, 105, 3, 18, 9, 0, 105, 17, 1, 0, 0, 0, 106, 107, 5, 27, 0, 0, 107, 108, 5, 30, 0, 0, 108, 112, 3, 18, 9, 0, 109, 110, 5, 27, 0, 0, 110, 112, 5, 7, 0, 0, 111, 106, 1, 0, 0, 0, 111, 109, 1, 0, 0, 0, 112, 19, 1, 0, 0, 0, 13, 22, 25, 29, 33, 47, 56, 61, 72, 76, 81, 91, 101, 111]
41 changes: 21 additions & 20 deletions parser/JsonQuery.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ NOT=8
LOGICAL_OPERATOR=9
BOOLEAN=10
NULL=11
IN=12
EQ=13
NE=14
GT=15
LT=16
GE=17
LE=18
CO=19
SW=20
EW=21
ATTRNAME=22
VERSION=23
STRING=24
DOUBLE=25
INT=26
EXP=27
NEWLINE=28
COMMA=29
SP=30
LIKE=12
IN=13
EQ=14
NE=15
GT=16
LT=17
GE=18
LE=19
CO=20
SW=21
EW=22
ATTRNAME=23
VERSION=24
STRING=25
DOUBLE=26
INT=27
EXP=28
NEWLINE=29
COMMA=30
SP=31
'('=1
')'=2
'pr'=3
Expand All @@ -36,4 +37,4 @@ SP=30
'['=6
']'=7
'null'=11
'\n'=28
'\n'=29
13 changes: 12 additions & 1 deletion parser/JsonQueryLexer.interp

Large diffs are not rendered by default.

41 changes: 21 additions & 20 deletions parser/JsonQueryLexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ NOT=8
LOGICAL_OPERATOR=9
BOOLEAN=10
NULL=11
IN=12
EQ=13
NE=14
GT=15
LT=16
GE=17
LE=18
CO=19
SW=20
EW=21
ATTRNAME=22
VERSION=23
STRING=24
DOUBLE=25
INT=26
EXP=27
NEWLINE=28
COMMA=29
SP=30
LIKE=12
IN=13
EQ=14
NE=15
GT=16
LT=17
GE=18
LE=19
CO=20
SW=21
EW=22
ATTRNAME=23
VERSION=24
STRING=25
DOUBLE=26
INT=27
EXP=28
NEWLINE=29
COMMA=30
SP=31
'('=1
')'=2
'pr'=3
Expand All @@ -36,4 +37,4 @@ SP=30
'['=6
']'=7
'null'=11
'\n'=28
'\n'=29
15 changes: 15 additions & 0 deletions parser/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ var conditions = []string{
`(a > 123 or b <= 456)`,
`(aa == 123123131.1232313131312312 or bb == 1231.1312313123123131231)`,
`(jh gt "1.2.3" or kl le "1.4.56")`,
`o like "^(https?://)?(www\.)?([a-zA-Z0-9-]{1,63}\.)*[a-zA-Z0-9][a-zA-Z0-9-]{0,61}\.[a-zA-Z]{2,}(:\d+)?(/.*)?$"`,
`p like ".*世界\s.*"`,
`q like "离职.*未关闭"`,
}

func BenchmarkLogicalExpression(b *testing.B) {
Expand All @@ -31,6 +34,9 @@ func BenchmarkLogicalExpression(b *testing.B) {
"b": 123131,
"aa": 123123.123131232113,
"jh": "1.2.3",
"o": "https://subdomain.example.org",
"p": "Hello, 世界 means world in Chinese.",
"q": "员工离职后系统账未关闭",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand Down Expand Up @@ -58,6 +64,9 @@ func BenchmarkLogicalExpressionBig(b *testing.B) {
"b": 123131,
"aa": 123123.123131232113,
"jh": "1.2.3",
"o": "https://subdomain.example.org",
"p": "Hello, 世界 means world in Chinese.",
"q": "员工离职后系统账未关闭",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand Down Expand Up @@ -86,6 +95,9 @@ func BenchmarkLogicalExpressionBigTreeCached(b *testing.B) {
"b": 123131,
"aa": 123123.123131232113,
"jh": "1.2.3",
"o": "https://subdomain.example.org",
"p": "Hello, 世界 means world in Chinese.",
"q": "员工离职后系统账未关闭",
}
b.ResetTimer()
ev, _ := NewEvaluator(rule)
Expand Down Expand Up @@ -113,6 +125,9 @@ func BenchmarkLogicalExpressionNested(b *testing.B) {
"b": 123131,
"aa": 123123.123131232113,
"jh": "1.2.3",
"o": "https://subdomain.example.org",
"p": "Hello, 世界 means world in Chinese.",
"q": "员工离职后系统账未关闭",
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand Down
13 changes: 13 additions & 0 deletions parser/float_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,16 @@ func (o *FloatOperation) IN(left Operand, right Operand) (bool, error) {
}
return false, nil
}

func (o *FloatOperation) LIKE(left Operand, right Operand) (bool, error) {
leftVal, err := toFloat(left)
if err != nil {
return false, newErrInvalidOperand(left, leftVal)
}
rightVal, ok := right.(string)
if !ok {
return false, newErrInvalidOperand(right, rightVal)
}

return false, nil
}
12 changes: 12 additions & 0 deletions parser/int_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,15 @@ func (o *IntOperation) IN(left Operand, right Operand) (bool, error) {
}
return false, nil
}

func (o *IntOperation) LIKE(left Operand, right Operand) (bool, error) {
leftVal, ok := left.(int)
if !ok {
return false, newErrInvalidOperand(left, leftVal)
}
rightVal, ok := right.(string)
if !ok {
return false, newErrInvalidOperand(right, rightVal)
}
return false, nil
}
Loading