Skip to content

How to localize a naïf timestamp before inserting into an UTC column #81417

@polmonso

Description

@polmonso

Company or project name

SomEnergia

Question

We have many data sources in clickhouse with naïf columns year, month, day, hour, minute, second and a timezone column which specifies in which timezone each naïf timestamp happened.

We want to now convert this columns into a single UTC column and we're facing several roadblocks due to the time functions not supporting dynamic timezone.

Received exception from server (version 25.5.2):
Code: 44. DB::Exception: Received from localhost:9000. DB::Exception: A value of illegal type was provided as 6th argument 'timezone' to function 'makeDateTime'. Expected: const String, got: String: In scope SELECT *, makeDateTime(2024, 3, 1, 0, 0, 0, timezone) FROM some_data LIMIT 1. (ILLEGAL_COLUMN)

We've seen references to this problem in #6948, althought it focuses more on how the value is displayed. In the issue commentators suggest using toString which supports non-constant timezones, but it feels fragile and probably less performant.

It's a key point that we're not trying to mix timezones in a single column, localizing the timestamp is a necessary step to get the utc value. We're wondering if there's another way to do this, since it should be fairly common when dealing with naïve timestamps.

We've checked the functions in the documentation but we didn't find a function other than toString and the like that supports non-constant timezones.

We ended up doing the the mixing ourselves to circumvent the limitations of the time functions, like so:

      if(
        timezone = 'Atlantic/Canary',
        makeDateTime(year, month, day, hour, minute, second, 'Atlantic/Canary')::DateTime('UTC'),
        makeDateTime(year, month, day, hour, minute, second, 'Europe/Madrid')::DateTime('UTC')
      ) AS ts_utc,

And we would need a MultiIf if we had more timezones, specifying up to all the hundred and so iana timezones in a huge multiif statement.

What is the proper way to get the utc timestamps of naïf timestamps in Clickhouse ? Should we open a merge request or see if we could support dynamic timezones in at least some functions other than toString?

Metadata

Metadata

Assignees

Labels

pr-featurePull request with new product feature

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions