googleapis / python-spanner

Apache License 2.0
90 stars 71 forks source link

Local Spanner Emulator doesn't support untyped params for datetime and date #1148

Open andrii-harbour opened 1 month ago

andrii-harbour commented 1 month ago

Environment details

Steps to reproduce

  1. Connect to Google Spanner Emulator
  2. Make sure there is a table with datetime/date column
  3. Run the python code - get an error
  4. Retry the same being connected to the Cloud Spanner, no errors

Code example

In the code below, based on the updates from https://github.com/googleapis/python-spanner/releases/tag/v3.43.0 , I am not passing param_types

from datetime import datetime

from google.auth.credentials import AnonymousCredentials
from google.cloud import spanner

client = spanner.Client(
    project="localhost",
    client_options={"api_endpoint": "localhost:9010"},
    credentials=AnonymousCredentials(),
)
instance = client.instance("local-instance")
database = instance.database("local-database")
with database.snapshot() as snapshot:
    results = snapshot.execute_sql(
        "SELECT * FROM groups WHERE created_at = @created_at", params={"created_at": datetime.now()}
    )
    print(results.to_dict_list())

Stack trace

File "/Users/aorlov/dev/interpreters/mvp/lib/python3.12/site-packages/google/cloud/spanner_v1/streamed.py", line 151, in __iter__
    self._consume_next()
  File "/Users/aorlov/dev/interpreters/mvp/lib/python3.12/site-packages/google/cloud/spanner_v1/streamed.py", line 123, in _consume_next
    response = next(self._response_iterator)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/aorlov/dev/interpreters/mvp/lib/python3.12/site-packages/google/cloud/spanner_v1/snapshot.py", line 88, in _restart_on_unavailable
    iterator = method(request=request)
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/aorlov/dev/interpreters/mvp/lib/python3.12/site-packages/google/cloud/spanner_v1/services/spanner/client.py", line 1444, in execute_streaming_sql
    response = rpc(
               ^^^^
  File "/Users/aorlov/dev/interpreters/mvp/lib/python3.12/site-packages/google/api_core/gapic_v1/method.py", line 131, in __call__
    return wrapped_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/aorlov/dev/interpreters/mvp/lib/python3.12/site-packages/google/api_core/timeout.py", line 120, in func_with_timeout
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/aorlov/dev/interpreters/mvp/lib/python3.12/site-packages/google/api_core/grpc_helpers.py", line 174, in error_remapped_callable
    raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.InvalidArgument: 400 Unable to infer type for parameter created_at. Provide the type binding TIMESTAMP

Thanks in advance!

caio-pizzol commented 1 month ago

+1

larkee commented 1 month ago

Thank you for filing this issue! :pray:

I have filed GoogleCloudPlatform/cloud-spanner-emulator/issues/174 in the emulator repo and will follow up with updates there :+1: