Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Query fails with more than 10 attributes in a query filter condition. (#751) #752

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions pynamodb/connection/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import logging
import random
import re
import sys
import time
import uuid
Expand Down Expand Up @@ -1240,11 +1241,12 @@ def query(self,
# FilterExpression does not allow key attributes. Check for hash and range key name placeholders
hash_key_placeholder = name_placeholders.get(hash_keyname)
range_key_placeholder = range_keyname and name_placeholders.get(range_keyname)
if (
hash_key_placeholder in filter_expression or
(range_key_placeholder and range_key_placeholder in filter_expression)
):
raise ValueError("'filter_condition' cannot contain key attributes")
if re.search(hash_key_placeholder + r"\D", filter_expression):
raise ValueError("'filter_condition' cannot contain hash key. {} found in {}"
.format(hash_key_placeholder, filter_expression))
if range_key_placeholder and re.search(range_key_placeholder + r"\D", filter_expression):
raise ValueError("'filter_condition' cannot contain range key. {} found in {}"
.format(range_key_placeholder, filter_expression))
operation_kwargs[FILTER_EXPRESSION] = filter_expression
if attributes_to_get:
projection_expression = create_projection_expression(attributes_to_get, name_placeholders)
Expand Down
51 changes: 51 additions & 0 deletions tests/test_base_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,57 @@ def test_query(self):
}
self.assertEqual(req.call_args[0][1], params)

with patch(PATCH_METHOD) as req:
req.return_value = {}
conn.query(
table_name=table_name,
hash_key="FooForum",
range_key_condition=Path('Subject').startswith('thread'),
filter_condition=Path('a2').exists()
| Path('a3').exists()
| Path('a4').exists()
| Path('a5').exists()
| Path('a6').exists()
| Path('a7').exists()
| Path('a8').exists()
| Path('a9').exists()
| Path('a10').exists()
)

params = {
'TableName': 'Thread',
'KeyConditionExpression': '(#0 = :0 AND begins_with (#1, :1))',
'FilterExpression':
'((((((((attribute_exists (#2) '
'OR attribute_exists (#3)) '
'OR attribute_exists (#4)) '
'OR attribute_exists (#5)) '
'OR attribute_exists (#6)) '
'OR attribute_exists (#7)) '
'OR attribute_exists (#8)) '
'OR attribute_exists (#9)) '
'OR attribute_exists (#10))',
'ExpressionAttributeNames': {
'#0': 'ForumName',
'#1': 'Subject',
'#2': 'a2',
'#3': 'a3',
'#4': 'a4',
'#5': 'a5',
'#6': 'a6',
'#7': 'a7',
'#8': 'a8',
'#9': 'a9',
'#10': 'a10'
},
'ExpressionAttributeValues': {
':0': {'S': 'FooForum'},
':1': {'S': 'thread'}
},
'ReturnConsumedCapacity': 'TOTAL'
}
self.assertEqual(req.call_args[0][1], params)

def test_scan(self):
"""
Connection.scan
Expand Down