Repository: prestodb/presto
Issue: Cannot chain `AT TIME ZONE`

Chaining `AT TIME ZONE` together results in the error `null`: 

```
presto> SELECT TIMESTAMP '2012-10-31 01:00' AT TIME ZONE 'America/Los_Angeles' AT TIME ZONE 'UTC';
Query 20160520_185347_00028_kmihc failed: null

presto>
```

Can work around this with e.g.: 

```
presto> WITH tz_la AS (SELECT TIMESTAMP '2012-10-31 01:00' AT TIME ZONE 'America/Los_Angeles' AS ts)
     -> SELECT tz_la.ts, tz_la.ts AT TIME ZONE 'UTC' from tz_la;
                     ts                      |            _col1
---------------------------------------------+-----------------------------
 2012-10-31 01:00:00.000 America/Los_Angeles | 2012-10-31 08:00:00.000 UTC
(1 row)

Query 20160520_185900_00029_kmihc, FINISHED, 1 node
Splits: 1 total, 0 done (0.00%)
0:00 [0 rows, 0B] [0 rows/s, 0B/s]

presto>
```

This is on Presto 0.146. I believe this worked before but I could be mistaken and I haven't had time to bisect. I'll follow up if I find more info there. 


Changed files:
presto-tests/src/main/java/com/facebook/presto/tests/AbstractTestQueries.java
presto-main/src/main/java/com/facebook/presto/sql/planner/DesugaringRewriter.java

diff --git a/presto-main/src/main/java/com/facebook/presto/sql/planner/DesugaringRewriter.java b/presto-main/src/main/java/com/facebook/presto/sql/planner/DesugaringRewriter.java
index 593924b40a..ae1236a4a0 100644
--- a/presto-main/src/main/java/com/facebook/presto/sql/planner/DesugaringRewriter.java
+++ b/presto-main/src/main/java/com/facebook/presto/sql/planner/DesugaringRewriter.java
@@ -45,7 +45,7 @@ public class DesugaringRewriter
     public Expression rewriteAtTimeZone(AtTimeZone node, Void context, ExpressionTreeRewriter<Void> treeRewriter)
     {
         Expression value = treeRewriter.rewrite(node.getValue(), context);
-        Type type = expressionTypes.get(value);
+        Type type = expressionTypes.get(node.getValue());
         if (type.equals(TIME)) {
             value = new Cast(value, TIME_WITH_TIME_ZONE.getDisplayName());
         }
diff --git a/presto-tests/src/main/java/com/facebook/presto/tests/AbstractTestQueries.java b/presto-tests/src/main/java/com/facebook/presto/tests/AbstractTestQueries.java
index 046ae76db1..95174a774e 100644
--- a/presto-tests/src/main/java/com/facebook/presto/tests/AbstractTestQueries.java
+++ b/presto-tests/src/main/java/com/facebook/presto/tests/AbstractTestQueries.java
@@ -4401,6 +4401,13 @@ public abstract class AbstractTestQueries
                 "values TIMESTAMP '1969-12-31 16:01:00-08:00', TIMESTAMP '1970-01-01 00:01:00-08:00', TIMESTAMP '1969-12-31 08:01:00-08:00'");
         assertQuery("SELECT min(x) AT TIME ZONE 'America/Los_Angeles' FROM (values TIMESTAMP '1970-01-01 00:01:00+00:00', TIMESTAMP '1970-01-01 08:01:00+08:00', TIMESTAMP '1969-12-31 16:01:00-08:00') t(x)",
                 "values TIMESTAMP '1969-12-31 16:01:00-08:00'");
+
+        // with chained AT TIME ZONE
+        assertQuery("SELECT TIMESTAMP '2012-10-31 01:00' AT TIME ZONE 'America/Los_Angeles' AT TIME ZONE 'UTC'", "SELECT TIMESTAMP '2012-10-30 18:00:00.000 America/Los_Angeles'");
+        assertQuery("SELECT TIMESTAMP '2012-10-31 01:00' AT TIME ZONE 'Asia/Tokyo' AT TIME ZONE 'America/Los_Angeles'", "SELECT TIMESTAMP '2012-10-30 18:00:00.000 America/Los_Angeles'");
+        assertQuery("SELECT TIMESTAMP '2012-10-31 01:00' AT TIME ZONE 'America/Los_Angeles' AT TIME ZONE 'Asia/Shanghai'", "SELECT TIMESTAMP '2012-10-30 18:00:00.000 America/Los_Angeles'");
+        assertQuery("SELECT min(x) AT TIME ZONE 'America/Los_Angeles' AT TIME ZONE 'UTC' FROM (values TIMESTAMP '1970-01-01 00:01:00+00:00', TIMESTAMP '1970-01-01 08:01:00+08:00', TIMESTAMP '1969-12-31 16:01:00-08:00') t(x)",
+                "values TIMESTAMP '1969-12-31 16:01:00-08:00'");
     }
 
     @Test