Skip to content
Open
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
9 changes: 1 addition & 8 deletions pygeofilter/backends/cql2_json/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,7 @@ def isnull(self, node, arg):

@handle(ast.Function)
def function(self, node, *args):
name = node.name.lower()
if name == "lower":
ret = {"lower": args[0]}
elif name == "upper":
ret = {"upper": args[0]}
else:
ret = {"function": name, "args": [*args]}
return ret
return {"op": node.name, "args": [*args]}

@handle(ast.In)
def in_(self, node, lhs, *options):
Expand Down
6 changes: 6 additions & 0 deletions pygeofilter/parsers/cql2_json/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ def walk_cql_json(node: JsonType): # noqa: C901
args = [cast(ast.Node, walk_cql_json(arg)) for arg in args]
return BINARY_OP_PREDICATES_MAP[op](*args)

else:
return ast.Function(
op,
[walk_cql_json(arg) for arg in args],
)

raise ValueError(f"Unable to parse expression node {node!r}")


Expand Down
23 changes: 23 additions & 0 deletions tests/parsers/cql2_json/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from pygeoif import geometry

from pygeofilter import ast, values
from pygeofilter.backends.cql2_json.evaluate import CQL2Evaluator
from pygeofilter.parsers.cql2_json import parse


Expand Down Expand Up @@ -717,3 +718,25 @@ def test_function_attr_string_arg():
],
),
)


def test_function_spec_format():
"""Parse spec-format function: {"op": "my_func", "args": [...]}"""
result = parse({"op": "my_func", "args": [{"property": "attr"}, 42]})
assert result == ast.Function("my_func", [ast.Attribute("attr"), 42])


def test_function_old_format_still_works():
"""Parse old format: {"function": {"name": "...", "arguments": [...]}}"""
result = parse(
{"function": {"name": "my_func", "arguments": [{"property": "attr"}]}}
)
assert result == ast.Function("my_func", [ast.Attribute("attr")])


def test_function_encode_spec_format():
"""Encode Function node to spec format: {"op": ..., "args": [...]}"""
evaluator = CQL2Evaluator(None, None)
node = ast.Function("my_func", [ast.Attribute("attr")])
result = evaluator.evaluate(node)
assert result == {"op": "my_func", "args": [{"property": "attr"}]}
Loading