From bf136584a940990f243cfb11d8900ccd3968895a Mon Sep 17 00:00:00 2001 From: Andrew Jefferson <8148776+eastlondoner@users.noreply.github.com> Date: Wed, 21 Mar 2018 17:02:31 +0100 Subject: [PATCH] =?UTF-8?q?Put=20projection=20after=20filter=20so=20that?= =?UTF-8?q?=20filters=20can=20act=20on=20fields=20that=20ar=E2=80=A6=20(#1?= =?UTF-8?q?470)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Put projection after filter so that filters can act on fields that are not projected * fixing whitespace --- moto/dynamodb2/models.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/moto/dynamodb2/models.py b/moto/dynamodb2/models.py index f4e61ca9f..73b09d73c 100644 --- a/moto/dynamodb2/models.py +++ b/moto/dynamodb2/models.py @@ -536,14 +536,6 @@ class Table(BaseModel): else: results.sort(key=lambda item: item.range_key) - if projection_expression: - expressions = [x.strip() for x in projection_expression.split(',')] - results = copy.deepcopy(results) - for result in results: - for attr in list(result.attrs): - if attr not in expressions: - result.attrs.pop(attr) - if scan_index_forward is False: results.reverse() @@ -552,6 +544,14 @@ class Table(BaseModel): if filter_expression is not None: results = [item for item in results if filter_expression.expr(item)] + if projection_expression: + expressions = [x.strip() for x in projection_expression.split(',')] + results = copy.deepcopy(results) + for result in results: + for attr in list(result.attrs): + if attr not in expressions: + result.attrs.pop(attr) + results, last_evaluated_key = self._trim_results(results, limit, exclusive_start_key) return results, scanned_count, last_evaluated_key