Skip to content

Commit

Permalink
Merge db9ae84 into 117c81d
Browse files Browse the repository at this point in the history
  • Loading branch information
ikonst committed Jun 11, 2020
2 parents 117c81d + db9ae84 commit 068da0d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
20 changes: 15 additions & 5 deletions pynamodb/connection/base.py
Expand Up @@ -7,6 +7,7 @@
import sys
import time
import uuid
import warnings
from base64 import b64decode
from threading import local
from typing import Any, Dict, List, Mapping, Optional, Sequence
Expand Down Expand Up @@ -40,9 +41,9 @@
TRANSACT_WRITE_ITEMS, TRANSACT_GET_ITEMS, CLIENT_REQUEST_TOKEN, TRANSACT_ITEMS, TRANSACT_CONDITION_CHECK,
TRANSACT_GET, TRANSACT_PUT, TRANSACT_DELETE, TRANSACT_UPDATE, UPDATE_EXPRESSION,
RETURN_VALUES_ON_CONDITION_FAILURE_VALUES, RETURN_VALUES_ON_CONDITION_FAILURE,
AVAILABLE_BILLING_MODES, DEFAULT_BILLING_MODE, BILLING_MODE, PAY_PER_REQUEST_BILLING_MODE,
AVAILABLE_BILLING_MODES, DEFAULT_BILLING_MODE, BILLING_MODE, PAY_PER_REQUEST_BILLING_MODE,
PROVISIONED_BILLING_MODE,
TIME_TO_LIVE_SPECIFICATION, ENABLED, UPDATE_TIME_TO_LIVE
TIME_TO_LIVE_SPECIFICATION, ENABLED, UPDATE_TIME_TO_LIVE, BETWEEN
)
from pynamodb.exceptions import (
TableError, QueryError, PutError, DeleteError, UpdateError, GetError, ScanError, TableDoesNotExist,
Expand Down Expand Up @@ -1278,13 +1279,22 @@ def query(
key_condition = getattr(Path([hash_keyname]), '__eq__')(hash_condition_value)

if range_key_condition is not None:
if range_key_condition.is_valid_range_key_condition(range_keyname):
key_condition = key_condition & range_key_condition
if str(range_key_condition.values[0]) == range_keyname:
# http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html#DDB-Query-request-KeyConditionExpression
if range_key_condition.operator in ['=', '<', '<=', '>', '>=', BETWEEN, 'begins_with']:
key_condition &= range_key_condition
else:
raise ValueError(f'Invalid range key condition "{range_key_condition}": '
f'operator "{range_key_condition.operator}" is not supported for range key conditions')
elif filter_condition is None:
# Try to gracefully handle the case where a user passed in a filter as a range key condition
warnings.warn(f'Using range key condition "{range_key_condition}" as a filter condition '
f'since it does not operate on the range key "{range_keyname}"; '
'fix query to use "filter_condition"')
(filter_condition, range_key_condition) = (range_key_condition, None)
else:
raise ValueError("{} is not a valid range key condition".format(range_key_condition))
raise ValueError(f'Invalid range key condition "{range_key_condition}": '
f'operand is "{range_key_condition.values[0]}"; expected "{range_keyname}"')

operation_kwargs[KEY_CONDITION_EXPRESSION] = key_condition.serialize(
name_placeholders, expression_attribute_values)
Expand Down
4 changes: 0 additions & 4 deletions pynamodb/expressions/condition.py
Expand Up @@ -14,10 +14,6 @@ def __init__(self, operator, *values):
self.operator = operator
self.values = values

# http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html#DDB-Query-request-KeyConditionExpression
def is_valid_range_key_condition(self, path):
return self.operator in ['=', '<', '<=', '>', '>=', BETWEEN, 'begins_with'] and str(self.values[0]) == path

def serialize(self, placeholder_names, expression_attribute_values):
values = [value.serialize(placeholder_names, expression_attribute_values) for value in self.values]
return self.format_string.format(*values, operator=self.operator)
Expand Down

0 comments on commit 068da0d

Please sign in to comment.