groupBy() aggregator was fixed

groupBy method accepts `aggregator`
parameter which is supposed to be
a function to aggregate value withing each group
but instead it was applied to the whole group list

Change-Id: Ic38bbe7bed7c624aa3e17f5f4ed4708fc216c278
Closes-Bug: #1626234
This commit is contained in:
Stan Lagun 2016-08-30 13:32:34 -07:00
parent f71a030508
commit 3fb9178401
2 changed files with 7 additions and 5 deletions

View File

@ -343,12 +343,14 @@ def group_by(engine, collection, key_selector, value_selector=None,
aggregator=None):
groups = {}
if aggregator is None:
aggregator = lambda x: x
new_aggregator = lambda x: x
else:
new_aggregator = lambda x: (x[0], aggregator(x[1]))
for t in collection:
value = t if value_selector is None else value_selector(t)
groups.setdefault(key_selector(t), []).append(value)
utils.limit_memory_usage(engine, (1, groups))
return select(six.iteritems(groups), aggregator)
return select(six.iteritems(groups), new_aggregator)
@specs.method

View File

@ -212,18 +212,18 @@ class TestQueries(yaql.tests.TestCase):
self.assertItemsEqual(
[[1, 'ac'], [2, 'be'], [3, 'd']],
self.eval('$.items().orderBy($[0]).'
'groupBy($[1], $[0], [$[0], $[1].sum()])', data=data))
'groupBy($[1], $[0], $.sum())', data=data))
self.assertItemsEqual(
[[1, ['a', 1, 'c', 1]], [2, ['b', 2, 'e', 2]], [3, ['d', 3]]],
self.eval('$.items().orderBy($[0]).'
'groupBy($[1],, [$[0], $[1].sum()])',
'groupBy($[1],, $.sum())',
data=data))
self.assertItemsEqual(
[[1, ['a', 1, 'c', 1]], [2, ['b', 2, 'e', 2]], [3, ['d', 3]]],
self.eval('$.items().orderBy($[0]).'
'groupBy($[1], aggregator => [$[0], $[1].sum()])',
'groupBy($[1], aggregator => $.sum())',
data=data))
def test_join(self):