Repository: getmoto/moto

Issue:
LocalSecondaryIndexes does not work for querying in dynamodb
Hi,

I am using the moto3 for testing inside my projects. It us really cool and thank you guys for your effort.

But recently I have discovered there is a problem with querying LocalSecondaryIndexes in DynamoDb. 

I use mock_dynamodb2 and create a local index in a table. After adding some items into the table I try to query the index to get values that are greater than a specific value. But all items are returned. If you are interested I can provide a full testing example.

Also I noticed there is no tests that check my use case in the repository.

Could  you please take a look?


Discussion:
Also. It is not possible to call dynamodb client create table with  "GlobalSecondaryIndexes": [] or "LocalSecondaryIndexes": [] in a real boto3 implementation. 

Could it be also fixed?

Thank you.


Sorry. My bad. Found a bug in my code. Sorry guys. And double checked your code. Looks like there is no difference between local and global index querying. Sorry :)
All good - happy to hear it's working now @krupnov :)

> Also. It is not possible to call dynamodb client create table with "GlobalSecondaryIndexes": [] or "LocalSecondaryIndexes": [] in a real boto3 implementation.
> 

I'll reopen the issue to get this fixed, as that is arguably a bug in our implementation!

Resolving patch:
diff --git a/moto/dynamodb2/responses.py b/moto/dynamodb2/responses.py
--- a/moto/dynamodb2/responses.py
+++ b/moto/dynamodb2/responses.py
@@ -179,8 +179,20 @@ def create_table(self):
         # getting attribute definition
         attr = body["AttributeDefinitions"]
         # getting the indexes
-        global_indexes = body.get("GlobalSecondaryIndexes", [])
-        local_secondary_indexes = body.get("LocalSecondaryIndexes", [])
+        global_indexes = body.get("GlobalSecondaryIndexes")
+        if global_indexes == []:
+            return self.error(
+                "ValidationException",
+                "One or more parameter values were invalid: List of GlobalSecondaryIndexes is empty",
+            )
+        global_indexes = global_indexes or []
+        local_secondary_indexes = body.get("LocalSecondaryIndexes")
+        if local_secondary_indexes == []:
+            return self.error(
+                "ValidationException",
+                "One or more parameter values were invalid: List of LocalSecondaryIndexes is empty",
+            )
+        local_secondary_indexes = local_secondary_indexes or []
         # Verify AttributeDefinitions list all
         expected_attrs = []
         expected_attrs.extend([key["AttributeName"] for key in key_schema])


Test patch:
diff --git a/tests/test_dynamodb2/exceptions/test_dynamodb_exceptions.py b/tests/test_dynamodb2/exceptions/test_dynamodb_exceptions.py
--- a/tests/test_dynamodb2/exceptions/test_dynamodb_exceptions.py
+++ b/tests/test_dynamodb2/exceptions/test_dynamodb_exceptions.py
@@ -428,3 +428,41 @@ def test_hash_key_can_only_use_equals_operations(operator):
     err = exc.value.response["Error"]
     err["Code"].should.equal("ValidationException")
     err["Message"].should.equal("Query key condition not supported")
+
+
+@mock_dynamodb2
+def test_creating_table_with_0_local_indexes():
+    dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
+
+    with pytest.raises(ClientError) as exc:
+        dynamodb.create_table(
+            TableName="test-table",
+            KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
+            AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],
+            ProvisionedThroughput={"ReadCapacityUnits": 1, "WriteCapacityUnits": 1},
+            LocalSecondaryIndexes=[],
+        )
+    err = exc.value.response["Error"]
+    err["Code"].should.equal("ValidationException")
+    err["Message"].should.equal(
+        "One or more parameter values were invalid: List of LocalSecondaryIndexes is empty"
+    )
+
+
+@mock_dynamodb2
+def test_creating_table_with_0_global_indexes():
+    dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
+
+    with pytest.raises(ClientError) as exc:
+        dynamodb.create_table(
+            TableName="test-table",
+            KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
+            AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],
+            ProvisionedThroughput={"ReadCapacityUnits": 1, "WriteCapacityUnits": 1},
+            GlobalSecondaryIndexes=[],
+        )
+    err = exc.value.response["Error"]
+    err["Code"].should.equal("ValidationException")
+    err["Message"].should.equal(
+        "One or more parameter values were invalid: List of GlobalSecondaryIndexes is empty"
+    )