Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for array aggregation in GroupByAggregate op #29

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions cycquery/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3122,6 +3122,7 @@ class GroupByAggregate(QueryOp):
>>> GroupByAggregate("person_id", {"person_id": ("count", "visit_count")})(table)
>>> GroupByAggregate("person_id", {"lab_name": "string_agg"}, {"lab_name": ", "})(table)
>>> GroupByAggregate("person_id", {"lab_name": ("string_agg", "lab_name_agg"}, {"lab_name": ", "})(table)
>>> GroupByAggregate("person_id", {"lab_name": ("array_agg", "lab_name_array")})(table)

"""

Expand Down Expand Up @@ -3163,6 +3164,7 @@ def __call__(self, table: TableTypes) -> Subquery:
"count": func.count,
"median": func.percentile_cont(0.5).within_group,
"string_agg": func.string_agg,
"array_agg": func.array_agg,
}

aggfunc_tuples = list(self.aggfuncs.items())
Expand Down
16 changes: 8 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ plugins = ["sqlalchemy.ext.mypy.plugin"]

[tool.ruff]
include = ["*.py", "pyproject.toml", "*.ipynb"]
select = [
lint.select = [
"A", # flake8-builtins
"B", # flake8-bugbear
"COM", # flake8-commas
Expand All @@ -93,9 +93,9 @@ select = [
"ERA", # eradicate
"PL", # pylint
]
fixable = ["A", "B", "COM", "C4", "RET", "SIM", "ICN", "Q", "RSE", "D", "E", "F", "I", "W", "N", "ERA", "PL"]
lint.fixable = ["A", "B", "COM", "C4", "RET", "SIM", "ICN", "Q", "RSE", "D", "E", "F", "I", "W", "N", "ERA", "PL"]
line-length = 88
ignore = [
lint.ignore = [
"B905", # `zip()` without an explicit `strict=` parameter
"E501", # line too long
"D203", # 1 blank line required before class docstring
Expand All @@ -105,19 +105,19 @@ ignore = [
]

# Ignore import violations in all `__init__.py` files.
[tool.ruff.per-file-ignores]
[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["E402", "F401", "F403", "F811"]

[tool.ruff.pep8-naming]
[tool.ruff.lint.pep8-naming]
ignore-names = ["X*", "setUp"]

[tool.ruff.isort]
[tool.ruff.lint.isort]
lines-after-imports = 2

[tool.ruff.pydocstyle]
[tool.ruff.lint.pydocstyle]
convention = "numpy"

[tool.ruff.pycodestyle]
[tool.ruff.lint.pycodestyle]
max-doc-length = 88

[tool.pytest.ini_options]
Expand Down
21 changes: 21 additions & 0 deletions tests/cycquery/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ def test_group_by_aggregate(
{"visit_concept_name": ", "},
),
).run()
measurements_array_agg = measurements_table.ops(
GroupByAggregate(
"person_id",
{"value_as_number": ("array_agg", "value_as_number_array")},
),
).run()
measurements_sum = measurements_table.ops(
GroupByAggregate(
"person_id",
Expand Down Expand Up @@ -446,6 +452,21 @@ def test_group_by_aggregate(
].item()
== 75.7
)
assert "value_as_number_array" in measurements_array_agg.columns
assert isinstance(
measurements_array_agg[measurements_array_agg["person_id"] == 33][
"value_as_number_array"
][0],
str,
)
assert (
len(
measurements_array_agg[measurements_array_agg["person_id"] == 33][
"value_as_number_array"
][0],
)
> 0
)


@pytest.mark.integration_test()
Expand Down