You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hivemall.apache.org by my...@apache.org on 2019/02/06 08:16:56 UTC

[incubator-hivemall] 01/02: Modified to_string_array to be a generic UDF

This is an automated email from the ASF dual-hosted git repository.

myui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-hivemall.git

commit 8d8d70138ef69cfedf02bacd3a2f937596141730
Author: Makoto Yui <my...@apache.org>
AuthorDate: Wed Feb 6 17:15:47 2019 +0900

    Modified to_string_array to be a generic UDF
---
 .../hivemall/tools/array/ToStringArrayUDF.java     | 64 +++++++++++++++++++---
 1 file changed, 55 insertions(+), 9 deletions(-)

diff --git a/core/src/main/java/hivemall/tools/array/ToStringArrayUDF.java b/core/src/main/java/hivemall/tools/array/ToStringArrayUDF.java
index 306673c..d1b7454 100644
--- a/core/src/main/java/hivemall/tools/array/ToStringArrayUDF.java
+++ b/core/src/main/java/hivemall/tools/array/ToStringArrayUDF.java
@@ -18,22 +18,68 @@
  */
 package hivemall.tools.array;
 
-import java.util.List;
+import hivemall.utils.hadoop.HiveUtils;
+import hivemall.utils.lang.StringUtils;
 
-import javax.annotation.Nullable;
+import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.hadoop.hive.ql.exec.Description;
-import org.apache.hadoop.hive.ql.exec.UDF;
+import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.ql.udf.UDFType;
-import org.apache.hadoop.io.Text;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
+import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
 
-@Description(name = "to_string_array", value = "_FUNC_(array<ANY>) - Returns an array of strings")
+@Description(name = "to_string_array", value = "_FUNC_(array<ANY>) - Returns an array of strings",
+        extended = "select to_string_array(array(1.0,2.0,3.0));\n\n" + "[\"1.0\",\"2.0\",\"3.0\"]")
 @UDFType(deterministic = true, stateful = false)
-public final class ToStringArrayUDF extends UDF {
+public final class ToStringArrayUDF extends GenericUDF {
+
+    private ListObjectInspector listOI;
+    private List<String> result;
+
+    @Override
+    public ObjectInspector initialize(ObjectInspector[] argOIs) throws UDFArgumentException {
+        if (argOIs.length != 1) {
+            throw new UDFArgumentException(
+                "to_string_array expects exactly one argument: " + argOIs.length);
+        }
+        this.listOI = HiveUtils.asListOI(argOIs[0]);
+        this.result = new ArrayList<>();
+
+        return ObjectInspectorFactory.getStandardListObjectInspector(
+            PrimitiveObjectInspectorFactory.javaStringObjectInspector);
+    }
+
+    @Override
+    public List<String> evaluate(DeferredObject[] arguments) throws HiveException {
+        final Object arg0 = arguments[0].get();
+        if (arg0 == null) {
+            return null;
+        }
+
+        result.clear();
+
+        final int len = listOI.getListLength(arg0);
+        for (int i = 0; i < len; i++) {
+            final Object e = listOI.getListElement(arg0, i);
+            if (e == null) {
+                result.add(null);
+            } else {
+                result.add(e.toString());
+            }
+        }
+
+        return result;
+    }
 
-    @Nullable
-    public List<Text> evaluate(@Nullable final List<Text> inArray) {
-        return inArray;
+    @Override
+    public String getDisplayString(String[] children) {
+        return "to_string_array(" + StringUtils.join(children, ',') + ')';
     }
 
 }