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

Fix: ISE 500 when non-numerical value appears in range search in JSON column #953

Merged
merged 1 commit into from
Jul 9, 2024
Merged
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
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
Loading