bigquery-emulator icon indicating copy to clipboard operation
bigquery-emulator copied to clipboard

TIMESTAMP returns as float, which can't be handled in python-bigquery

Open Gekko0114 opened this issue 10 months ago • 4 comments

What happened?

When I am using python-bigquery library, I encounter an issue handling timestamps. The bigquery-emulator returns value as floats, such as 1.0, 2.0, .... However, Python can't convert floats represented as strings into integers. For example int('1.0') returns in an error.

Could you fix this issue?

https://github.com/googleapis/python-bigquery/blob/b03a2afabde7f42be45f62fabd3dc0e6a9a493e1/google/cloud/bigquery/_helpers.py#L206-L210

What did you expect to happen?

Timestamp should be handled in python-bigquery library.

How can we reproduce it (as minimally and precisely as possible)?

Data like this can't be handled in python big-query clients.


              - name: created_at
                type: TIMESTAMP
            data:
              - id: 1
                created_at: "2025-01-14T12:00:00"

Anything else we need to know?

No response

Gekko0114 avatar Feb 19 '25 06:02 Gekko0114

I'm using this workaround for my tests:

@pytest.fixture(scope="session", autouse=True)
def _patch_big_query_timestamp_from_json() -> Generator[None]:
    from google.cloud.bigquery import _helpers

    real_fn = _helpers._timestamp_from_json

    def _wrapper(value, field):
        try:
            return real_fn(value, field)
        except ValueError:
            return datetime.fromtimestamp(float(value))

    with patch.dict(_helpers._CELLDATA_FROM_JSON, TIMESTAMP=_wrapper):
        yield

mathieu-lemay avatar Mar 20 '25 22:03 mathieu-lemay

Thanks, I will try it :)

Gekko0114 avatar Mar 21 '25 13:03 Gekko0114

This is even worse than I expected. If I save this datetime(year=1970, month=1, day=1, microsecond=1, tzinfo=UTC), the real BigQuery will return 1, as expected. One microsecond has passed since epoch. The emulator returns a float instead, which I can work around. But the emulator should return 0.000001. Instead it returns 0.1. So there's no way to know if it is 1, 10, 100, 1000, 10000 or 100000 microsecond.

mathieu-lemay avatar Mar 21 '25 17:03 mathieu-lemay

Any updates on this? This is still an issue for us.

0xcaffeinated avatar Apr 03 '25 06:04 0xcaffeinated