Skip to content

Commit

Permalink
Fix: ISE 500 when non-numerical value appears in range search in JSON…
Browse files Browse the repository at this point in the history
… column (#953)
  • Loading branch information
psrok1 authored Jul 9, 2024
1 parent e2ea6cb commit f37645f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
20 changes: 14 additions & 6 deletions mwdb/core/search/parse_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def unescape_string(value: str) -> str:
return re.sub(r"\\(.)", r"\1", value)


def transform_for_eq_statement(escaped_value: str) -> str:
def transform_for_regular_statement(escaped_value: str) -> str:
return unescape_string(escaped_value)


Expand Down Expand Up @@ -180,7 +180,7 @@ def transform_token(token: StringToken) -> StringToken:
return join_tokenized_string(transformed_string)


def transform_for_config_eq_statement(escaped_value: str) -> str:
def transform_for_config_regular_statement(escaped_value: str) -> str:
"""
Transforms Lucene value to value for == condition against
"unicode-escape"-escaped JSON value
Expand Down Expand Up @@ -352,7 +352,7 @@ def string_equals(column: ColumnElement, escaped_value: str):
pattern = transform_for_like_statement(escaped_value)
return column.like(pattern)
else:
value = transform_for_eq_statement(escaped_value)
value = transform_for_regular_statement(escaped_value)
return column == value


Expand All @@ -361,7 +361,7 @@ def config_string_equals(column: ColumnElement, escaped_value: str):
pattern = transform_for_config_like_statement(escaped_value)
return column.like(pattern)
else:
value = transform_for_config_eq_statement(escaped_value)
value = transform_for_config_regular_statement(escaped_value)
return column == value


Expand All @@ -377,15 +377,15 @@ def _jsonpath_string_equals(path_selector: PathSelector, value: str) -> str:

def jsonpath_string_equals(path_selector: PathSelector, escaped_value: str) -> str:
# Wildcards are not supported
value = transform_for_eq_statement(escaped_value)
value = transform_for_regular_statement(escaped_value)
return _jsonpath_string_equals(path_selector, value)


def jsonpath_config_string_equals(
path_selector: PathSelector, escaped_value: str
) -> str:
# Wildcards are not supported
value = transform_for_config_eq_statement(escaped_value)
value = transform_for_config_regular_statement(escaped_value)
return _jsonpath_string_equals(path_selector, value)


Expand Down Expand Up @@ -429,6 +429,14 @@ def jsonpath_range_equals(
low, high = high, low
include_low, include_high = include_high, include_low

if low is not None and not is_nonstring_object(low):
low = transform_for_regular_statement(low)
low = jsonpath_quote(low)

if high is not None and not is_nonstring_object(high):
high = transform_for_regular_statement(high)
high = jsonpath_quote(high)

low_condition = f"@ >= {low}" if include_low else f"@ > {low}"
high_condition = f"@ <= {high}" if include_high else f"@ < {high}"

Expand Down
9 changes: 9 additions & 0 deletions tests/backend/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,12 @@ def test_attribute_falsy_values(admin_session, random_attribute):
admin_session.add_attribute(sample_id, attr_name, "")
admin_session.add_attribute(sample_id, attr_name, ["nonempty"])
admin_session.add_attribute(sample_id, attr_name, {"nonempty": None})


def test_attribute_json_string_range(admin_session, random_attribute):
sample_id, attr_name = random_attribute
admin_session.add_attribute(sample_id, attr_name, {"creation-time": "2024-06-01 12:00:00"})
assert len(admin_session.search(f'attribute.{attr_name}.creation-time:>="2024-05"')) == 1
assert len(admin_session.search(f'attribute.{attr_name}.creation-time:>="2024-07"')) == 0
assert len(admin_session.search(f'attribute.{attr_name}.creation-time:["2024-07" TO "2024-08"]')) == 0
assert len(admin_session.search(f'attribute.{attr_name}.creation-time:["2024-06" TO "2024-07"]')) == 1

0 comments on commit f37645f

Please sign in to comment.