DateTime, such as toTimeZone, accept only a constant time zone, so they fail when the time zone comes from a column. To get the local time of each row, convert the value to a string with toString or formatDateTime. Both accept a time zone that differs from row to row.
The error
Why the time zone must be constant
In ClickHouse, the time zone is not stored with each value. It is a property of the data type:DateTime('Asia/Tokyo') and DateTime('UTC') are different types that store the same number of seconds since the Unix epoch and differ only in how values are converted to and from text. All values of a column have one type, so the result of toTimeZone can have only one time zone, and it has to be known before the query runs.
Get the local time as a string
AString result has no time zone attached, so toString and formatDateTime can use a different time zone for every row:
system.time_zones. A row with an unknown name makes the query fail.
Get the hour or the date of the local time
If you need parts of the local time, such as the hour or the calendar date, parse the string back into aDateTime64 with a fixed time zone and apply the usual functions to it:
toDateTime64 explicitly, as 'UTC' in this example. Without it, the string is interpreted in the time zone of the server or of the session, and the result depends on where the query runs.
The query uses toDateTime64 and toDate32 instead of toDateTime and toDate, because the local time of a DateTime value can lie outside the range of DateTime and Date, which both start on 1970-01-01. For example, 1970-01-01 00:00:00 UTC is 1969-12-31 19:00:00 in America/New_York. toDateTime and toDate return a wrong value for such a local time without an error, while DateTime64 and Date32 cover it.
Do not enable allow_nonconst_timezone_arguments
Theallow_nonconst_timezone_arguments setting makes the error disappear, but it does not convert the values. It exists only for compatibility. With the setting enabled, the non-constant argument is ignored, and the result keeps the time zone of the input column: