Presto SQL : Changing time zones using time zone string coming as a result of a query is not working

霸气de小男生 提交于 2019-12-01 09:55:52

问题


I am connecting to AWS Athena through Mode Analytics Platform and querying a table using its Query Engine ( which is based on Presto 0.172 ). This table public.zones has time zone information stored in a column called time_zone on some regions I am interested in, stored as varchar.

For example if I type:

SELECT time_zone 
FROM public.zones
LIMIT 4;

I get (as expected):

time_zone
----------  
US/Pacific 
US/Eastern 
US/Eastern 
US/Eastern 

I can run this test query:

SELECT 
  timestamp '2017-06-01 12:34:56.789' AT TIME ZONE 'US/Eastern' AS time_eastern,
  time_zone 
FROM public.zones
LIMIT 4;

and I get (as expected)

time_eastern                        time_zone
----------------------------------  ----------
2017-06-01 08:34:56.789 US/Eastern  US/Pacific
2017-06-01 08:34:56.789 US/Eastern  US/Eastern
2017-06-01 08:34:56.789 US/Eastern  US/Eastern
2017-06-01 08:34:56.789 US/Eastern  US/Eastern

Now, I want to represent the same time string '2017-06-01 12:34:56.789' in different time zones that I query from the zones table. I expected the following query to run. (It runs on PostgreSQL).

SELECT 
  timestamp '2017-06-01 12:34:56.789' AT TIME ZONE time_zone AS time_custom,
  time_zone 
FROM public.zones
LIMIT 4;

I get the following error:

[Simba][AthenaJDBC](100071) An error has been thrown from the AWS Athena client. 
line 2:52: no viable alternative at input 'TIME ZONE time_zone'

What is the reason for this not working in Presto SQL / AWS Athena Query Engine ?

Can anyone suggest any work-arounds or what is my syntactical error if any?


回答1:


AT TIME ZONE accepts only literal or interval.

Presto 320 adds with_timezone (for timestamp values) at_timezone (for timestamp with time zone values) exactly for this purpose.

If you are using older Presto version (such as Athena as of this writing), you can use following workaround. You can cast your timestamp value to a varchar, concatenate with zone and cast to timestamp with time zone.

presto> select cast(cast(t as varchar) || ' ' || zone as timestamp with time zone)
  from (values (timestamp '2017-06-01 12:34:56.789', 'US/Pacific')) x(t, zone);
                    _col0
---------------------------------------------
 2017-06-01 12:34:56.789 America/Los_Angeles
(1 row)

(Note: tested on Presto 320. If this doesn't work on Athena yet, let me know.)



来源:https://stackoverflow.com/questions/52230931/presto-sql-changing-time-zones-using-time-zone-string-coming-as-a-result-of-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!