0
votes

My DynamoDB Query is returning "Query key condition not supported".

NSMutableDictionary * conditions = [[NSMutableDictionary alloc] init];

DynamoDBCondition * googleConditionLat = [[DynamoDBCondition alloc] init];
googleConditionLat.comparisonOperator = @"BETWEEN";
DynamoDBAttributeValue * googleIDAttributeLat1 = [[DynamoDBAttributeValue alloc] initWithN:[NSString stringWithFormat:@"%f", location.coordinate.latitude - 0.0005]];
[googleConditionLat addAttributeValueList:googleIDAttributeLat1];
DynamoDBAttributeValue * googleIDAttributeLat2 = [[DynamoDBAttributeValue alloc] initWithN:[NSString stringWithFormat:@"%f", location.coordinate.latitude + 0.0005]];
[googleConditionLat addAttributeValueList:googleIDAttributeLat2];
[conditions setObject:googleConditionLat forKey:kLatitudeKey];

#if 1
DynamoDBCondition * googleConditionLong = [[DynamoDBCondition alloc] init];
googleConditionLong.comparisonOperator = @"BETWEEN";
DynamoDBAttributeValue * googleIDAttributeLong1 = [[DynamoDBAttributeValue alloc] initWithN:[NSString stringWithFormat:@"%f", location.coordinate.longitude - 0.0005]];
[googleConditionLong addAttributeValueList:googleIDAttributeLong1];
DynamoDBAttributeValue * googleIDAttributeLong2 = [[DynamoDBAttributeValue alloc] initWithN:[NSString stringWithFormat:@"%f", location.coordinate.longitude + 0.0005]];
[googleConditionLong addAttributeValueList:googleIDAttributeLong2];
[conditions setObject:googleConditionLong forKey:kLongitudeKey];
#endif

NSMutableDictionary *queryStartKey = [[NSMutableDictionary alloc] init];;
do
{
    DynamoDBQueryRequest *queryRequest = [[DynamoDBQueryRequest alloc] init];
    queryRequest.tableName = PLACE_TABLE_NAME;
    queryRequest.exclusiveStartKey = queryStartKey;
    queryRequest.keyConditions = conditions;
    queryRequest.consistentRead = false;
    queryRequest.indexName = @"lat-long-global-index";

    @try
    {
        DynamoDBQueryResponse *queryResponse = [[AmazonClientManager ddb] query:queryRequest];

My global secondary index is:

Index Name: lat-long-global-index Hash Key:lat (Number) Range Key:long (Number)

I have a very similar query on a GSI to the same table with one string hash key using "EQ" which works fine.

Any thoughts appreciated...

2

2 Answers

2
votes

Sadly, you can't use BETWEEN on HASH

From the documentation:

For a query on an index, you can only have conditions on the index key attributes. You must specify the index hash attribute name and value as an EQ condition. You can optionally specify a second condition, referring to the index key range attribute.

1
votes

Solved this by trying the query on the AWS console. It looks like you cant check for a range of values on the first key - it is a hash key same as the primary hash key.

A table redesign required...