Skip to content

Commit a0d3bf1

Browse files
authored
Merge pull request #8 from cloudblue/fix/LITE-24935_nested-function-logical
LITE-24935 added support for nested function-style logical operators
2 parents 985032c + 3faa718 commit a0d3bf1

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

py_rql/grammar.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
and_op: _and
3333
| _L_BRACE _and _R_BRACE
3434
35-
_and: _AND _logical_exp
36-
| term "&" term
37-
| term _COMMA term
35+
_and: _AND? _logical_exp
36+
| term ("&" term)+
37+
| term (_COMMA term)+
3838
3939
or_op: _or
4040
| _L_BRACE _or _R_BRACE
@@ -43,7 +43,7 @@
4343
| _L_BRACE term ("|" term)+ _R_BRACE
4444
| _L_BRACE term (";" term)+ _R_BRACE
4545
46-
_logical_exp: _L_BRACE expr_term (_COMMA expr_term)+ _R_BRACE
46+
_logical_exp: _L_BRACE term (_COMMA term)+ _R_BRACE
4747
4848
not_op: _NOT _L_BRACE expr_term _R_BRACE
4949

tests/test_parser/test_logical.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,14 @@ def test_logical_fail(query):
102102
logical_transform(query)
103103

104104

105-
def test_logical_nesting():
106-
q = '((p1=v1|p1=ge=v2)&(p3=le=v4,gt(p5,v6));ne(p7,p8))'
107-
result = logical_transform(q)
105+
@pytest.mark.parametrize('query', [
106+
'((p1=v1|p1=ge=v2)&(p3=le=v4,gt(p5,v6));ne(p7,p8))',
107+
'(or(p1=v1,p1=ge=v2)&(p3=le=v4,gt(p5,v6));ne(p7,p8))',
108+
'(or(p1=v1,p1=ge=v2)&and(p3=le=v4,gt(p5,v6));ne(p7,p8))',
109+
'or(and((p1=v1|p1=ge=v2),(p3=le=v4,gt(p5,v6))),ne(p7,p8))',
110+
])
111+
def test_logical_nesting_or(query):
112+
result = logical_transform(query)
108113

109114
and_grammar_key = LogicalOperators.get_grammar_key(LogicalOperators.AND)
110115
or_grammar_key = LogicalOperators.get_grammar_key(LogicalOperators.OR)
@@ -126,6 +131,42 @@ def test_logical_nesting():
126131
}
127132

128133

134+
@pytest.mark.parametrize('query', [
135+
'((p1=v1,or(p2=v2,p3=v3),and(p4=v4,p5=v5)))',
136+
'(p1=v1,or(p2=v2,p3=v3),and(p4=v4,p5=v5))',
137+
'and(p1=v1,or(p2=v2,p3=v3),and(p4=v4,p5=v5))',
138+
'p1=v1&(p2=v2|p3=v3)&(p4=v4,p5=v5)',
139+
'p1=v1,(p2=v2|p3=v3),(p4=v4,p5=v5)',
140+
])
141+
def test_logical_nesting_and(query):
142+
result = logical_transform(query)
143+
144+
and_grammar_key = LogicalOperators.get_grammar_key(LogicalOperators.AND)
145+
or_grammar_key = LogicalOperators.get_grammar_key(LogicalOperators.OR)
146+
147+
assert result == {
148+
and_grammar_key: [
149+
('eq', 'p1', 'v1'),
150+
{
151+
and_grammar_key: [
152+
{
153+
or_grammar_key: [
154+
('eq', 'p2', 'v2'),
155+
('eq', 'p3', 'v3'),
156+
],
157+
},
158+
{
159+
and_grammar_key: [
160+
('eq', 'p4', 'v4'),
161+
('eq', 'p5', 'v5'),
162+
],
163+
},
164+
],
165+
},
166+
],
167+
}
168+
169+
129170
def test_and_chain():
130171
q = 'ne(p1,v1)&p2=ge=and,or=v3'
131172
result = logical_transform(q)

0 commit comments

Comments
 (0)