package org.fukata.hive.udf;

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import java.util.Map;

@Description(name = "map_value", value = "map_value extract value from map", extended = "")
public class MapValue extends UDF {
    @SuppressWarnings("rawtypes")
    public String evaluate (Object map, Object...keys) {
        Object value = null;
        for ( Object key : keys ) {
            @SuppressWarnings("unchecked")
            Map<Object, Object> m = (Map<Object, Object>) (value == null ? map : value); 
            if ( !m.containsKey(key) ) return null;

            value = m.get(key);
            if ( null == value ) return null;
            
            String clazzName = value.getClass().getName();
            if ( "java.lang.String".equals(clazzName) ) {
                return value.toString();
            }
        }

        String clazzName = value.getClass().getName();
        if ( "java.lang.String".equals(clazzName) ) {
            return value.toString();
        }

        return value.toString();
    }
}

@@ -182,25 +182,35 @@
 alue = %22
-map_value
+_FUNC_(map, keys) -
  extract
@@ -229,23 +229,8 @@
 map%22
-, extended = %22%22
 )%0Apu

change description