Skip to content

Commit

Permalink
[resotocore][fix] Fix aggregation functions using the same variable (#…
Browse files Browse the repository at this point in the history
…643)

* [resotocore][fix] Fix aggregation functions using the same variable
  • Loading branch information
aquamatthias authored Feb 9, 2022
1 parent 9cdd1f6 commit 16a2e1e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
4 changes: 3 additions & 1 deletion resotocore/core/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
Expand Down
10 changes: 4 additions & 6 deletions resotocore/core/db/arango_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}"
Expand Down
4 changes: 4 additions & 0 deletions resotocore/tests/core/db/graphdb_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 16a2e1e

Please sign in to comment.