From 16a2e1e182c709a9a5617f139c6065bde19891c0 Mon Sep 17 00:00:00 2001 From: Matthias Veit Date: Wed, 9 Feb 2022 19:47:43 +0100 Subject: [PATCH] [resotocore][fix] Fix aggregation functions using the same variable (#643) * [resotocore][fix] Fix aggregation functions using the same variable --- resotocore/core/cli/cli.py | 4 +++- resotocore/core/db/arango_query.py | 10 ++++------ resotocore/tests/core/db/graphdb_test.py | 4 ++++ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/resotocore/core/cli/cli.py b/resotocore/core/cli/cli.py index be2eb8de7e..2004ee858b 100644 --- a/resotocore/core/cli/cli.py +++ b/resotocore/core/cli/cli.py @@ -123,7 +123,9 @@ def overview() -> str: aliases = "\n".join( f"{indent}- `{alias}` (`{cmd}`) - {self.parts[cmd].info()}" for alias, cmd in self.aliases.items() ) - replacements = "\n".join(f"{indent}- `@{key}@` -> {value}" for key, value in CLI.replacements().items()) + replacements = "\n".join( + f"{indent}- `@{key}@` -> {value}" for key, value in CLI.replacements(**ctx.env).items() + ) result = dedent( f""" # resotocore CLI ({version()}) diff --git a/resotocore/core/db/arango_query.py b/resotocore/core/db/arango_query.py index 9556e81098..62a9ae63c9 100644 --- a/resotocore/core/db/arango_query.py +++ b/resotocore/core/db/arango_query.py @@ -110,12 +110,10 @@ def func_term(fn: AggregateFunction) -> str: name = f"{cursor}.{fn.name}" if isinstance(fn.name, str) else str(fn.name) return f"{name} {fn.combined_ops()}" if fn.ops else name - vs = {str(v.name): f"var_{num}" for num, v in enumerate(a.group_by)} - fs = {v.name: f"fn_{num}" for num, v in enumerate(a.group_func)} - variables = ", ".join(f"{vs[str(v.name)]}={var_name(v.name)}" for v in a.group_by) - funcs = ", ".join(f"{fs[v.name]}={v.function}({func_term(v)})" for v in a.group_func) - agg_vars = ", ".join(f'"{v.get_as_name()}": {vs[str(v.name)]}' for v in a.group_by) - agg_funcs = ", ".join(f'"{f.get_as_name()}": {fs[f.name]}' for f in a.group_func) + variables = ", ".join(f"var_{num}={var_name(v.name)}" for num, v in enumerate(a.group_by)) + funcs = ", ".join(f"fn_{num}={f.function}({func_term(f)})" for num, f in enumerate(a.group_func)) + agg_vars = ", ".join(f'"{v.get_as_name()}": var_{num}' for num, v in enumerate(a.group_by)) + agg_funcs = ", ".join(f'"{f.get_as_name()}": fn_{num}' for num, f in enumerate(a.group_func)) group_result = f'"group":{{{agg_vars}}},' if a.group_by else "" aggregate_term = f"collect {variables} aggregate {funcs}" return_result = f"{{{group_result} {agg_funcs}}}" diff --git a/resotocore/tests/core/db/graphdb_test.py b/resotocore/tests/core/db/graphdb_test.py index ada57c6d9a..6166b51ebc 100644 --- a/resotocore/tests/core/db/graphdb_test.py +++ b/resotocore/tests/core/db/graphdb_test.py @@ -414,6 +414,10 @@ async def test_query_aggregate(filled_graph_db: ArangoGraphDB, foo_model: Model) async with await filled_graph_db.query_aggregation(QueryModel(agg_combined_var_query, foo_model)) as g: assert [x async for x in g] == [{"group": {"kind": "test_foo_0_"}, "instances": 11}] + agg_multi_fn_same_prop = parse_query('aggregate(sum(f) as a, max(f) as b): is("bla")').on_section("reported") + async with await filled_graph_db.query_aggregation(QueryModel(agg_multi_fn_same_prop, foo_model)) as g: + assert [x async for x in g] == [{"a": 2300, "b": 23}] + @pytest.mark.asyncio async def test_query_with_merge(filled_graph_db: ArangoGraphDB, foo_model: Model) -> None: