From 3fb91784018de335440b01b3b069fe45dc53e025 Mon Sep 17 00:00:00 2001 From: Stan Lagun Date: Tue, 30 Aug 2016 13:32:34 -0700 Subject: [PATCH] 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 --- yaql/standard_library/queries.py | 6 ++++-- yaql/tests/test_queries.py | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/yaql/standard_library/queries.py b/yaql/standard_library/queries.py index a5e6a8b..ace1af2 100644 --- a/yaql/standard_library/queries.py +++ b/yaql/standard_library/queries.py @@ -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 diff --git a/yaql/tests/test_queries.py b/yaql/tests/test_queries.py index 57b827e..da65ec3 100644 --- a/yaql/tests/test_queries.py +++ b/yaql/tests/test_queries.py @@ -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):