Skip to content

Commit

Permalink
feat: update method argument types to ConstantNode and add validation…
Browse files Browse the repository at this point in the history
… for argument count in CEL method calls
  • Loading branch information
skynetigor committed Jan 24, 2025
1 parent 0e56333 commit a37cd37
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion keep/api/core/cel_to_sql/cel_ast_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def member_dot_arg(self, tree: lark.Tree) -> None:
if self.member_access_stack:
left: PropertyAccessNode = self.member_access_stack.pop()

method = MethodAccessNode(member_name=right, args=reversed(exprlist))
method = MethodAccessNode(member_name=right, args=[item for item in reversed(exprlist)])
left.value = method

self.member_access_stack.append(method)
Expand Down
6 changes: 3 additions & 3 deletions keep/api/core/cel_to_sql/sql_providers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,17 +351,17 @@ def _visit_method_calling(
raise NotImplementedError(f"'{method_name}' method is not supported")

def _visit_contains_method_calling(
self, property_path: str, method_args: List[str]
self, property_path: str, method_args: List[ConstantNode]
) -> str:
raise NotImplementedError("'contains' method must be implemented in the child class")

def _visit_startwith_method_calling(
self, property_path: str, method_args: List[str]
self, property_path: str, method_args: List[ConstantNode]
) -> str:
raise NotImplementedError("'startswith' method call must be implemented in the child class")

def _visit_endswith_method_calling(
self, property_path: str, method_args: List[str]
self, property_path: str, method_args: List[ConstantNode]
) -> str:
raise NotImplementedError("'endswith' method call must be implemented in the child class")

Expand Down
22 changes: 16 additions & 6 deletions keep/api/core/cel_to_sql/sql_providers/sqlite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from types import NoneType
from typing import List
from keep.api.core.cel_to_sql.ast_nodes import ConstantNode
from keep.api.core.cel_to_sql.sql_providers.base import BaseCelToSqlProvider


Expand All @@ -26,16 +27,25 @@ def cast(self, exp, to_type):
return f"CAST({exp} as {to_type_str})"

def _visit_contains_method_calling(
self, property_path: str, method_args: List[str]
self, property_path: str, method_args: List[ConstantNode]
) -> str:
return f'{property_path} LIKE '%{method_args[0]}%''
if len(method_args) != 1:
raise ValueError(f'{property_path}.contains accepts 1 argument but got {len(method_args)}')

return f'{property_path} LIKE '%{method_args[0].value}%''

def _visit_starts_with_method_calling(
self, property_path: str, method_args: List[str]
self, property_path: str, method_args: List[ConstantNode]
) -> str:
return f"{property_path} LIKE '{method_args[0]}%'"
if len(method_args) != 1:
raise ValueError(f'{property_path}.startsWith accepts 1 argument but got {len(method_args)}')

return f"{property_path} LIKE '{method_args[0].value}%'"

def _visit_ends_with_method_calling(
self, property_path: str, method_args: List[str]
self, property_path: str, method_args: List[ConstantNode]
) -> str:
return f"{property_path} LIKE '%{method_args[0]}'"
if len(method_args) != 1:
raise ValueError(f'{property_path}.endsWith accepts 1 argument but got {len(method_args)}')

return f"{property_path} LIKE '%{method_args[0].value}'"

0 comments on commit a37cd37

Please sign in to comment.