diff --git a/pydruid/utils/aggregators.py b/pydruid/utils/aggregators.py index 11557d31..2c700bb5 100644 --- a/pydruid/utils/aggregators.py +++ b/pydruid/utils/aggregators.py @@ -61,7 +61,7 @@ def build_aggregators(agg_input): def _build_aggregator(name, kwargs): if kwargs["type"] == "filtered": - kwargs["aggregator"]["name"] = name + kwargs["aggregator"] = _build_aggregator(name, kwargs["aggregator"]) else: kwargs.update({"name": name}) diff --git a/tests/utils/test_aggregators.py b/tests/utils/test_aggregators.py index b5651640..9cee14a9 100644 --- a/tests/utils/test_aggregators.py +++ b/tests/utils/test_aggregators.py @@ -41,6 +41,23 @@ def test_filtered_aggregator(self): actual = aggregators.filtered(filter_, agg) assert actual == expected + def test_nested_filtered_aggregator(self): + filter1 = filters.Filter(dimension='dim1', value='val') + filter2 = filters.Filter(dimension='dim2', value='val') + agg = aggregators.filtered(filter1, + aggregators.filtered(filter2, aggregators.count('metric1'))) + actual = aggregators.build_aggregators({'agg_name': agg}) + # the innermost aggregation must have 'agg_name' + expected = [{ + 'type': 'filtered', + 'aggregator': { + 'type': 'filtered', + 'aggregator': {'fieldName': 'metric1', 'type': 'count', 'name': 'agg_name'}, + 'filter': {'dimension': 'dim2', 'value': 'val', 'type': 'selector'}}, + 'filter': {'dimension': 'dim1', 'value': 'val', 'type': 'selector'} + }] + assert expected == actual + def test_build_aggregators(self): agg_input = { 'agg1': aggregators.count('metric1'),