From 8b08a91816e67724b7497fba998db9a4b89f1446 Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Thu, 3 Mar 2016 18:21:55 +0800 Subject: [PATCH] add support for nested filtered aggregators --- pydruid/utils/aggregators.py | 2 +- tests/utils/test_aggregators.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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'),