Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 845
ConfusedMan Avatar asked Dec 14 '22 14:12

ConfusedMan


1 Answers

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.)

like image 194
Piotr Findeisen Avatar answered Dec 18 '22 00:12

Piotr Findeisen