/*
 * 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 com.facebook.presto.decoder.json;

import com.facebook.presto.decoder.DecoderColumnHandle;
import com.facebook.presto.decoder.FieldDecoder;
import com.facebook.presto.decoder.RowDecoder;
import com.facebook.presto.decoder.RowDecoderFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.airlift.slice.Slice;

import javax.inject.Inject;

import java.util.Map;
import java.util.Set;

import static com.facebook.presto.decoder.FieldDecoder.DEFAULT_FIELD_DECODER_NAME;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static java.util.function.Function.identity;

public class JsonRowDecoderFactory
        implements RowDecoderFactory
{
    private final ObjectMapper objectMapper;

    @Inject
    public JsonRowDecoderFactory(ObjectMapper objectMapper)
    {
        this.objectMapper = requireNonNull(objectMapper, "objectMapper is null");
    }

    @Override
    public RowDecoder create(Map<String, String> decoderParams, Set<DecoderColumnHandle> columns)
    {
        requireNonNull(columns, "columnHandles is null");
        return new JsonRowDecoder(objectMapper, chooseFieldDecoders(columns));
    }

    private Map<DecoderColumnHandle, FieldDecoder<JsonNode>> chooseFieldDecoders(Set<DecoderColumnHandle> columns)
    {
        return columns.stream()
                .collect(toImmutableMap(identity(), this::chooseFieldDecoder));
    }

    private FieldDecoder<JsonNode> chooseFieldDecoder(DecoderColumnHandle column)
    {
        checkArgument(!column.isInternal(), "unexpected internal column '%s'", column.getName());
        if (column.getDataFormat() == null || column.getDataFormat().equals(DEFAULT_FIELD_DECODER_NAME)) {
            return new JsonFieldDecoder();
        }
        Class<?> javaType = column.getType().getJavaType();
        if (javaType == Slice.class || javaType == long.class) {
            switch (column.getDataFormat()) {
                case "custom-date-time":
                    return new CustomDateTimeJsonFieldDecoder();
                case "iso8601":
                    return new ISO8601JsonFieldDecoder();
                case "seconds-since-epoch":
                    return new SecondsSinceEpochJsonFieldDecoder();
                case "milliseconds-since-epoch":
                    return new MillisecondsSinceEpochJsonFieldDecoder();
                case "rfc2822":
                    return new RFC2822JsonFieldDecoder();
            }
        }
        throw new IllegalArgumentException(format("unknown data format '%s' for column '%s'", column.getDataFormat(), column.getName()));
    }
}

@@ -804,16 +804,64 @@
 actory;%0A
+import com.facebook.presto.spi.PrestoException;%0A
 import c
@@ -1141,16 +1141,92 @@
 R_NAME;%0A
+import static com.facebook.presto.spi.StandardErrorCode.GENERIC_USER_ERROR;%0A
 import s
@@ -2361,32 +2361,50 @@
 e column)%0A    %7B%0A
+        try %7B%0A    
         checkArg
@@ -2489,24 +2489,28 @@
 ));%0A        
+   
+ 
 if (column.g
@@ -2600,32 +2600,36 @@
 ) %7B%0A            
+    
 return new JsonF
@@ -2643,34 +2643,42 @@
 oder();%0A        
+  
-%7D%0A
+  %7D%0A    
         Class%3C?%3E
@@ -2725,24 +2725,28 @@
 ();%0A        
+ 
+   
 if (javaType
@@ -2786,24 +2786,28 @@
 ng.class) %7B%0A
+    
             
@@ -2852,24 +2852,28 @@
             
+    
 case %22custom
@@ -2885,16 +2885,20 @@
 -time%22:%0A
+    
         
@@ -2966,24 +2966,28 @@
             
+    
 case %22iso860
@@ -3002,32 +3002,36 @@
                 
+  
+  
 return new ISO86
@@ -3064,24 +3064,28 @@
             
+    
 case %22second
@@ -3112,32 +3112,36 @@
                 
+ 
+   
 return new Secon
@@ -3164,32 +3164,36 @@
 FieldDecoder();%0A
+    
                 
@@ -3237,32 +3237,36 @@
                 
+    
 return new Milli
@@ -3314,24 +3314,28 @@
             
+   
+ 
 case %22rfc282
@@ -3338,16 +3338,20 @@
 c2822%22:%0A
+    
         
@@ -3412,26 +3412,33 @@
         
+  
-%7D%0A
+  %7D%0A   
         
 %7D%0A      
@@ -3421,34 +3421,39 @@
    %7D%0A           
+ 
 %7D%0A
+    
         throw ne
@@ -3570,20 +3570,147 @@
 etName()));%0A
+        %7D%0A        catch (IllegalArgumentException e) %7B%0A            throw new PrestoException(GENERIC_USER_ERROR, e);%0A        %7D%0A
     %7D%0A%7D%0A

Throw PrestoException instead IllegalArgumentException from JsonRowDecoderFactory