首页 > 解决方案 > Presto SQL:使用由于查询而出现的时区字符串更改时区不起作用

问题描述

我通过 Mode Analytics Platform 连接到 AWS Athena 并使用其查询引擎(基于 Presto 0.172)查询表。该表public.zones的时区信息存储在time_zone我感兴趣的某些区域的列中,存储为varchar.

例如,如果我输入:

SELECT time_zone 
FROM public.zones
LIMIT 4;

我得到(如预期的那样):

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

我可以运行这个测试查询:

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

我得到(如预期的那样)

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

现在,我想'2017-06-01 12:34:56.789'在我从 zone 表中查询的不同时区表示相同的时间字符串。我希望以下查询能够运行。(它在 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;

我收到以下错误:

[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'

这在 Presto SQL / AWS Athena 查询引擎中不起作用的原因是什么?

任何人都可以提出任何解决方法或我的语法错误(如果有的话)是什么?

标签: sqlpostgresqltimezoneamazon-athenapresto

解决方案


AT TIME ZONE只接受文字或区间。

Presto 320正是为此目的而添加with_timezone(for timestampvalues) at_timezone(for values)。timestamp with time zone

如果您使用的是旧版 Presto(例如撰写本文时的 Athena),您可以使用以下解决方法。您可以将时间戳值转换为 a varchar,与 zone 连接并转换为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)

(注意:在 Presto 320 上测试过。如果这在 Athena 上还不起作用,请告诉我。)


推荐阅读