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

negative range seems to work #101

Open
wants to merge 2 commits into
base: master
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
2 changes: 1 addition & 1 deletion luqum/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class CheckNestedFields(visitor.TreeVisitor):
"""

def __init__(self, nested_fields, object_fields=None, sub_fields=None):
assert(isinstance(nested_fields, dict))
assert isinstance(nested_fields, dict)
self.object_fields = normalize_object_fields_specs(object_fields)
self.object_prefixes = set(k.rsplit(".", 1)[0] for k in self.object_fields or [])
self.nested_fields = flatten_nested_fields_specs(nested_fields)
Expand Down
4 changes: 3 additions & 1 deletion luqum/elasticsearch/nested.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ def extract_nested_queries(query, query_nester=None):
if "nested" in query:
params = {k: v for k, v in query["nested"].items() if k not in ("query", "name")}

def sub_query_nester(req, name):
def sub_query_nester_func(req, name):
nested = {"nested": {"query": req, **params}}
if query_nester is not None:
nested = query_nester(nested, name)
if name is not None:
nested["nested"]["_name"] = name
return nested

sub_query_nester = sub_query_nester_func

bool_param = {"must", "should", "must_not"} & set(query.keys())
if bool_param and in_nested:
# we are in a list of operations in a bool inside a nested,
Expand Down
24 changes: 23 additions & 1 deletion luqum/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
('nonassoc', 'PLUS', 'MINUS'),
('nonassoc', 'BOOST'),
('nonassoc', 'TO'),
('right', 'UMINUS')
)

# term
Expand Down Expand Up @@ -95,6 +96,7 @@
)*
)
'''.format(time_re=TIME_RE)

# phrase
PHRASE_RE = r'''
(?P<phrase> # phrase
Expand Down Expand Up @@ -296,13 +298,33 @@ def p_grouping(p):


def p_range(p):
'''unary_expression : LBRACKET phrase_or_term TO phrase_or_term RBRACKET'''
'''unary_expression : LBRACKET \
phrase_or_possibly_negative_term \
TO phrase_or_possibly_negative_term \
RBRACKET'''

include_low = p[1].value == "["
include_high = p[5].value == "]"
p[0] = Range(p[2], p[4], include_low, include_high)
head_tail.range(p)


def p_possibly_negative_term(p):
'''possibly_negative_term : MINUS phrase_or_term %prec UMINUS
| phrase_or_term'''
if len(p) == 3:
p[0] = Prohibit(p[2])
head_tail.unary(p)
else:
p[0] = p[1]


def p_phrase_or_possibly_negative_term(p):
'''phrase_or_possibly_negative_term : possibly_negative_term
| PHRASE'''
p[0] = p[1]


def p_lessthan(p):
'''unary_expression : LESSTHAN phrase_or_term'''
include_bound = '=' in p[1].value
Expand Down
51 changes: 0 additions & 51 deletions luqum/parsetab.py

This file was deleted.

11 changes: 11 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,14 @@ def test_illegal_character_exception(self):
str(raised.exception),
"Illegal character '\\' at position 0",
)

def test_negative_values_in_ranges(self):
parsed = parser.parse("[-1 TO 5]")
self.assertEqual(str(parsed), "[-1 TO 5]")

parsed = parser.parse("[-10 TO -1]")
self.assertEqual(str(parsed), "[-10 TO -1]")

# semantically incorrect but correct from the parser's perspective
parsed = parser.parse("[5 TO -1]")
self.assertEqual(str(parsed), "[5 TO -1]")
Loading