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

DynamoDB: table name can't be surround by single quotation marks #25

Open
khanh-alice opened this issue Sep 30, 2024 · 1 comment
Open
Labels
enhancement New feature or request

Comments

@khanh-alice
Copy link

khanh-alice commented Sep 30, 2024

Unsure if it's used to work or intended, but I notice that moto allows DynamoDB's table name to be surround by single quotation marks. This is not the case on an real DynamoDB.

Given main.py

import boto3


def execute_statement():
    client = boto3.client("dynamodb", region_name="us-east-1")

    table_name = "testtable"
    response = client.execute_statement(Statement=f"select pk from '{table_name}'")

    items = response.get("Items")
    print(items)


if __name__ == "__main__":
    execute_statement()

and main_test.py

import boto3
from moto import mock_aws


@mock_aws
def test_main():
    client = boto3.client("dynamodb", region_name="us-east-1")
    table_name = "testtable"

    client.create_table(
        TableName=table_name,
        KeySchema=[
            {"AttributeName": "pk", "KeyType": "HASH"},
        ],
        AttributeDefinitions=[
            {"AttributeName": "pk", "AttributeType": "S"},
        ],
        BillingMode="PAY_PER_REQUEST",
    )
    client.put_item(
        TableName=table_name,
        Item={
            "pk": {"S": "1"},
        },
    )

    from main import execute_statement
    execute_statement()

Running main.py return this error

Traceback (most recent call last):
  File "/Users/khanh.le/Git/mototest/main.py", line 15, in <module>
    execute_statement()
  File "/Users/khanh.le/Git/mototest/main.py", line 8, in execute_statement
    response = client.execute_statement(Statement=f"select pk from '{table_name}'")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/khanh.le/Git/mototest/.venv/lib/python3.12/site-packages/botocore/client.py", line 569, in _api_call
    return self._make_api_call(operation_name, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/khanh.le/Git/mototest/.venv/lib/python3.12/site-packages/botocore/client.py", line 1023, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the ExecuteStatement operation: Unexpected from source

But running moto_test.py returns a successful test.

A quick check on PartiQL Editor on AWS console also shows some kind of errors when I try surround the table name with single quotation mark.

SELECT
image

UPDATE
image

DELETE
image

INSERT
image

boto3==1.35.29
botocore==1.35.29
moto==5.0.16
py-partiql-parser==0.5.6
@bblommers
Copy link
Collaborator

I looked into this at some point, and I remember being.. bewildered by AWS' choices.

Not sure about the details - was it that AWS allows both no quotes or double quotes, but not single quotes? Either that, or there were inconsistencies in what types of quotes they accept/require for other parts of the query, like attributes.

It is confusing either way, and it would be nice if we could at least match the behaviour, so I'll mark it as an enhancement to add this validation.

@bblommers bblommers added the enhancement New feature or request label Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants