Skip to content

Commit

Permalink
Merge pull request #37 from gianm/cardinality
Browse files Browse the repository at this point in the history
Cardinality aggregator
  • Loading branch information
xvrl committed Dec 7, 2015
2 parents e9d6648 + fa2ecf9 commit 70fc6f6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 6 additions & 0 deletions pydruid/utils/aggregators.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ def hyperunique(raw_metric):
return {"type": "hyperUnique", "fieldName": raw_metric}


def cardinality(raw_column, by_row=False):
if type(raw_column) is not list:
raw_column = [raw_column]
return {"type": "cardinality", "fieldNames": raw_column, "byRow": by_row}


def filtered(filter, agg):
return {"type": "filtered",
"filter": Filter.build_filter(filter),
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def run_tests(self):

setup(
name='pydruid',
version='0.2.3',
version='0.2.4',
author='Deep Ganguli',
author_email='deep@metamarkets.com',
packages=['pydruid', 'pydruid.utils'],
Expand Down
18 changes: 15 additions & 3 deletions tests/utils/test_aggregators.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def test_filtered_aggregator(self):
aggregators.doublesum('metric3'),
aggregators.min('metric4'),
aggregators.max('metric5'),
aggregators.hyperunique('metric6')]
aggregators.hyperunique('metric6'),
aggregators.cardinality('dim1'),
aggregators.cardinality(['dim1', 'dim2'], by_row=True)]
for agg in aggs:
expected = {
'type': 'filtered',
Expand All @@ -46,7 +48,9 @@ def test_build_aggregators(self):
'agg3': aggregators.doublesum('metric3'),
'agg4': aggregators.min('metric4'),
'agg5': aggregators.max('metric5'),
'agg6': aggregators.hyperunique('metric6')
'agg6': aggregators.hyperunique('metric6'),
'agg7': aggregators.cardinality('dim1'),
'agg8': aggregators.cardinality(['dim1', 'dim2'], by_row=True)
}
built_agg = aggregators.build_aggregators(agg_input)
expected = [
Expand All @@ -56,6 +60,8 @@ def test_build_aggregators(self):
{'name': 'agg4', 'type': 'min', 'fieldName': 'metric4'},
{'name': 'agg5', 'type': 'max', 'fieldName': 'metric5'},
{'name': 'agg6', 'type': 'hyperUnique', 'fieldName': 'metric6'},
{'name': 'agg7', 'type': 'cardinality', 'fieldNames': ['dim1'], 'byRow': False},
{'name': 'agg8', 'type': 'cardinality', 'fieldNames': ['dim1', 'dim2'], 'byRow': True},
]
assert (sorted(built_agg, key=itemgetter('name')) ==
sorted(expected, key=itemgetter('name')))
Expand All @@ -74,7 +80,11 @@ def test_build_filtered_aggregator(self):
'agg5': aggregators.filtered(filter_,
aggregators.max('metric5')),
'agg6': aggregators.filtered(filter_,
aggregators.hyperunique('metric6'))
aggregators.hyperunique('metric6')),
'agg7': aggregators.filtered(filter_,
aggregators.cardinality('dim1')),
'agg8': aggregators.filtered(filter_,
aggregators.cardinality(['dim1', 'dim2'], by_row=True)),
}
base = {
'type': 'filtered',
Expand All @@ -92,6 +102,8 @@ def test_build_filtered_aggregator(self):
{'name': 'agg4', 'type': 'min', 'fieldName': 'metric4'},
{'name': 'agg5', 'type': 'max', 'fieldName': 'metric5'},
{'name': 'agg6', 'type': 'hyperUnique', 'fieldName': 'metric6'},
{'name': 'agg7', 'type': 'cardinality', 'fieldNames': ['dim1'], 'byRow': False},
{'name': 'agg8', 'type': 'cardinality', 'fieldNames': ['dim1', 'dim2'], 'byRow': True},
]
expected = []
for agg in aggs:
Expand Down

0 comments on commit 70fc6f6

Please sign in to comment.