Skip to content

param_types.TIMESTAMP strips timezone information when generating query #230

@cameroncurrey

Description

@cameroncurrey

Environment details

  • OS type and version: macOS 10.15.7
  • Python version: 3.7.0
  • pip version: 20.3.1
  • google-cloud-spanner version: 2.1.0

When querying with a datetime parameter with timezone information, the query that gets generated strips timezone information from the parameter, causing unexpected data being returned.

Steps to reproduce

Call Snapshot's execute_sql with a datetime param.

When the query actually hits Spanner, the start and end parameters strip the timezone information from the query to generate the strings:
2021-02-08T00:00:00Z and 2021-02-09T00:00:00Z

I would have expected it to generate:
2021-02-08T00:07:00Z and 2021-02-09T00:07:00Z

Code example

import datetime
from google.cloud import spanner

client = spanner.Client(project="my-project")
instance = client.instance("my-instance")
database = instance.database("my-database")

with database.snapshot() as snapshot:
    result = snapshot.execute_sql("""
    	SELECT MyColumn
          FROM MyTable
         WHERE MyTime BETWEEN @start AND @end
        """",
        params={
            "start": datetime.datetime(2021, 2, 8, 0, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=-25200))),
            "end": datetime.datetime(2021, 2, 9, 0, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=-25200))),
        },
        param_types={
            "start": spanner.param_types.TIMESTAMP,
            "end": spanner.param_types.TIMESTAMP,
        },
    )
    rows = list(results)

I believe the issue is here:

return Value(string_value=_datetime_to_rfc3339(value))

Which calls here with a default value of True:
https://github.com/googleapis/python-cloud-core/blob/568578d33f71117564ee9212daad72d4e2b739a6/google/cloud/_helpers.py#L324

Which ultimately uses this string format to generate the string:
https://github.com/googleapis/python-cloud-core/blob/568578d33f71117564ee9212daad72d4e2b739a6/google/cloud/_helpers.py#L44

Metadata

Metadata

Assignees

Labels

api: spannerIssues related to the googleapis/python-spanner API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions