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

Make cql2_like_to_es() understand escaped backslashes #286

Merged
merged 2 commits into from
Aug 23, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed
- Support escaped backslashes in CQL2 `LIKE` queries, and reject invalid (or incomplete) escape sequences. [#286](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/286)

## [v3.0.0] - 2024-08-14

### Changed
Expand Down
40 changes: 25 additions & 15 deletions stac_fastapi/core/stac_fastapi/core/extensions/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,41 @@
from enum import Enum
from typing import Any, Dict

_cql2_like_patterns = re.compile(r"\\.|[%_]|\\$")
_valid_like_substitutions = {
"\\\\": "\\",
"\\%": "%",
"\\_": "_",
"%": "*",
"_": "?",
}


def _replace_like_patterns(match: re.Match) -> str:
pattern = match.group()
try:
return _valid_like_substitutions[pattern]
except KeyError:
raise ValueError(f"'{pattern}' is not a valid escape sequence")


def cql2_like_to_es(string: str) -> str:
"""
Convert CQL2 wildcard characters to Elasticsearch wildcard characters. Specifically, it converts '_' to '?' and '%' to '*', handling escape characters properly.
Convert CQL2 "LIKE" characters to Elasticsearch "wildcard" characters.

Args:
string (str): The string containing CQL2 wildcard characters.

Returns:
str: The converted string with Elasticsearch compatible wildcards.

Raises:
ValueError: If an invalid escape sequence is encountered.
"""
# Translate '%' and '_' only if they are not preceded by a backslash '\'
percent_pattern = r"(?<!\\)%"
underscore_pattern = r"(?<!\\)_"
# Remove the escape character before '%' or '_'
escape_pattern = r"\\(?=[_%])"

# Replace '%' with '*' for broad wildcard matching
string = re.sub(percent_pattern, "*", string)
# Replace '_' with '?' for single character wildcard matching
string = re.sub(underscore_pattern, "?", string)
# Remove the escape character used in the CQL2 format
string = re.sub(escape_pattern, "", string)

return string
return _cql2_like_patterns.sub(
repl=_replace_like_patterns,
string=string,
)


class LogicalOp(str, Enum):
Expand Down
46 changes: 46 additions & 0 deletions stac_fastapi/tests/extensions/test_cql2_like_to_es.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pytest

from stac_fastapi.core.extensions.filter import cql2_like_to_es


@pytest.mark.parametrize(
"cql2_value, expected_es_value",
(
# no-op
("", ""),
# backslash
("\\\\", "\\"),
# percent
("%", "*"),
(r"\%", "%"),
(r"\\%", r"\*"),
(r"\\\%", r"\%"),
# underscore
("_", "?"),
(r"\_", "_"),
(r"\\_", r"\?"),
(r"\\\_", r"\_"),
),
)
def test_cql2_like_to_es_success(cql2_value: str, expected_es_value: str) -> None:
"""Verify CQL2 LIKE query strings are converted correctly."""

assert cql2_like_to_es(cql2_value) == expected_es_value


@pytest.mark.parametrize(
"cql2_value",
(
pytest.param("\\", id="trailing backslash escape"),
pytest.param("\\1", id="invalid escape sequence"),
),
)
def test_cql2_like_to_es_invalid(cql2_value: str) -> None:
"""Verify that incomplete or invalid escape sequences are rejected.

CQL2 currently doesn't appear to define how to handle invalid escape sequences.
This test assumes that undefined behavior is caught.
"""

with pytest.raises(ValueError):
cql2_like_to_es(cql2_value)
Loading