/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.prestosql.elasticsearch.decoders;

import io.prestosql.spi.PrestoException;
import io.prestosql.spi.block.BlockBuilder;
import io.prestosql.spi.connector.ConnectorSession;
import org.elasticsearch.common.document.DocumentField;
import org.elasticsearch.search.SearchHit;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.function.Supplier;

import static io.prestosql.spi.StandardErrorCode.TYPE_MISMATCH;
import static io.prestosql.spi.type.TimestampType.TIMESTAMP;
import static java.time.format.DateTimeFormatter.ISO_DATE_TIME;

public class TimestampDecoder
        implements Decoder
{
    private final String path;
    private final ZoneId zoneId;

    public TimestampDecoder(ConnectorSession session, String path)
    {
        this.path = path;
        this.zoneId = ZoneId.of(session.getTimeZoneKey().getId());
    }

    @Override
    public void decode(SearchHit hit, Supplier<Object> getter, BlockBuilder output)
    {
        DocumentField documentField = hit.getFields().get(path);
        if (documentField == null) {
            output.appendNull();
        }
        else if (documentField.getValues().size() > 1) {
            throw new PrestoException(TYPE_MISMATCH, "Expected single value for column: " + path);
        }
        else {
            TIMESTAMP.writeLong(output,
                    ISO_DATE_TIME.parse(documentField.getValue(), LocalDateTime::from)
                            .atZone(zoneId)
                            .toInstant()
                            .toEpochMilli());
        }
    }
}

@@ -838,16 +838,42 @@
 chHit;%0A%0A
+import java.time.Instant;%0A
 import j
@@ -896,16 +896,16 @@
 teTime;%0A
-
 import j
@@ -958,16 +958,80 @@
 plier;%0A%0A
+import static io.prestosql.spi.StandardErrorCode.NOT_SUPPORTED;%0A
 import s
@@ -1147,16 +1147,55 @@
 ESTAMP;%0A
+import static java.lang.String.format;%0A
 import s
@@ -1310,16 +1310,71 @@
 coder%0A%7B%0A
+    private static final ZoneId ZULU = ZoneId.of(%22Z%22);%0A
     priv
@@ -2049,37 +2049,130 @@
     
-TIMESTAMP.writeLong(output,%0A 
+Object value = documentField.getValue();%0A%0A            LocalDateTime timestamp;%0A            if (value instanceof String) %7B%0A
     
@@ -2183,18 +2183,27 @@
         
-  
+timestamp =
  ISO_DAT
@@ -2219,32 +2219,22 @@
 rse(
-documentField.getV
+(String) v
 alue
-()
 , Lo
@@ -2251,16 +2251,17 @@
 e::from)
+;
 %0A       
@@ -2257,32 +2257,34 @@
 m);%0A            
+%7D%0A
                 
@@ -2283,101 +2283,609 @@
     
-    .atZone(zoneId)%0A                            .toInstant()%0A                            .toE
+else if (value instanceof Number) %7B%0A                timestamp = LocalDateTime.ofInstant(Instant.ofEpochMilli(((Number) value).longValue()), ZULU);%0A            %7D%0A            else %7B%0A                throw new PrestoException(NOT_SUPPORTED, format(%0A                        %22Unsupported representation for timestamp type: %25s %5B%25s%5D%22,%0A                        value.getClass().getSimpleName(),%0A                        value));%0A            %7D%0A%0A            long epochMillis = timestamp.atZone(zoneId)%0A                    .toInstant()%0A                    .toEpochMilli();%0A%0A            TIMESTAMP.writeLong(output, e
 poch
@@ -2889,18 +2889,17 @@
 ochMilli
-()
+s
 );%0A     

Add support for datetimes exposed as numbers in Elasticsearch