Skip to content

Commit 8803175

Browse files
authored
Merge pull request #179 from UncoderIO/gis-8217
Gis 8217
2 parents b559bd2 + 46f6af6 commit 8803175

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

uncoder-core/app/translator/core/tokenizer.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from app.translator.core.custom_types.tokens import GroupType, LogicalOperatorType, OperatorType
2525
from app.translator.core.custom_types.values import ValueType
2626
from app.translator.core.escape_manager import EscapeManager
27-
from app.translator.core.exceptions.functions import NotSupportedFunctionException
2827
from app.translator.core.exceptions.parser import (
2928
QueryParenthesesException,
3029
TokenizerGeneralException,
@@ -64,7 +63,16 @@ class QueryTokenizer(BaseTokenizer):
6463
fields_operator_map: ClassVar[dict[str, str]] = {}
6564
operators_map: ClassVar[dict[str, str]] = {} # used to generate re pattern. so the keys order is important
6665

67-
logical_operator_pattern = r"^(?P<logical_operator>and|or|not|AND|OR|NOT)\s+"
66+
logical_operators_map: ClassVar[dict[str, str]] = {
67+
"and": LogicalOperatorType.AND,
68+
"AND": LogicalOperatorType.AND,
69+
"or": LogicalOperatorType.OR,
70+
"OR": LogicalOperatorType.OR,
71+
"not": LogicalOperatorType.NOT,
72+
"NOT": LogicalOperatorType.NOT,
73+
}
74+
_logical_operator_pattern = f"(?P<logical_operator>{'|'.join(logical_operators_map)})"
75+
logical_operator_pattern = rf"^{_logical_operator_pattern}\s+"
6876
field_value_pattern = r"""^___field___\s*___operator___\s*___value___"""
6977
base_value_pattern = r"(?:___value_pattern___)"
7078

@@ -276,8 +284,8 @@ def _check_field_value_match(self, query: str, white_space_pattern: str = r"\s+"
276284

277285
return False
278286

279-
def search_function_value(self, query: str) -> tuple[FunctionValue, str]: # noqa: ARG002
280-
raise NotSupportedFunctionException
287+
def search_function_value(self, query: str) -> tuple[FunctionValue, str]:
288+
...
281289

282290
@staticmethod
283291
def _check_function_value_match(query: str) -> bool: # noqa: ARG004
@@ -295,14 +303,20 @@ def _get_next_token(
295303
logical_operator = logical_operator_search.group("logical_operator")
296304
pos = logical_operator_search.end()
297305
return Identifier(token_type=logical_operator.lower()), query[pos:]
298-
if self.platform_functions and self._check_function_value_match(query):
299-
return self.search_function_value(query)
306+
if self.platform_functions and self._check_function_value_match(query): # noqa: SIM102
307+
if search_result := self.search_function_value(query):
308+
return search_result
300309
if self._check_field_value_match(query):
301310
return self.search_field_value(query)
302311
if self.keyword_pattern and re.match(self.keyword_pattern, query):
303312
return self.search_keyword(query)
304313

305-
raise TokenizerGeneralException("Unsupported query entry")
314+
unsupported_query_entry = self._get_unsupported_query_entry(query)
315+
raise TokenizerGeneralException(f"Unsupported query entry: {unsupported_query_entry}")
316+
317+
def _get_unsupported_query_entry(self, query: str) -> str:
318+
split_by_logical_operator = re.split(rf"\s+{self._logical_operator_pattern}\s+", query, maxsplit=1)
319+
return split_by_logical_operator[0]
306320

307321
@staticmethod
308322
def _validate_parentheses(tokens: list[QUERY_TOKEN_TYPE]) -> None:

0 commit comments

Comments
 (0)