ayushtkn commented on code in PR #3806:
URL: https://github.com/apache/hive/pull/3806#discussion_r1037110890


##########
ql/src/test/queries/clientpositive/udf_array_distinct.q:
##########
@@ -0,0 +1,33 @@
+--! qt:dataset:src
+set hive.fetch.task.conversion=more;

Review Comment:
   There are too many select *,
   better to add
   ``-- SORT_QUERY_RESULTS`` to avoid any flakiness



##########
ql/src/test/queries/clientpositive/udf_array_distinct.q:
##########
@@ -0,0 +1,33 @@
+--! qt:dataset:src
+set hive.fetch.task.conversion=more;
+
+DESCRIBE FUNCTION array_distinct;
+DESCRIBE FUNCTION EXTENDED array_distinct;
+
+-- evalutes function for array of primitives
+SELECT array_distinct(array(1, 2, 3, null,3,4)) FROM src tablesample (1 rows);
+
+SELECT array_distinct(array()) FROM src tablesample (1 rows);
+
+SELECT array_distinct(array(null)) FROM src tablesample (1 rows);
+
+SELECT array_distinct(array(1.12, 2.23, 3.34, null,1.11,1.12,2.9)) FROM src 
tablesample (1 rows);
+
+SELECT array_distinct(array(1.1234567890, 2.234567890, 3.34567890, null, 
3.3456789, 2.234567,1.1234567890)) FROM src tablesample (1 rows);
+
+SELECT array_distinct(array(11234567890, 2234567890, 334567890, null, 
11234567890, 2234567890, 334567890, null)) FROM src tablesample (1 rows);
+
+SELECT 
array_distinct(array(array("a","b","c","d"),array("a","b","c","d"),array("a","b","c","d","e"),null,array("e","a","b","c","d")))
 FROM src tablesample (1 rows);
+
+# handle null array cases
+
+dfs ${system:test.dfs.mkdir} ${system:test.tmp.dir}/test_null_array;
+
+dfs -copyFromLocal ../../data/files/test_null_array.csv 
${system:test.tmp.dir}/test_null_array/;
+
+create external table test_null_array (id int, value Array<String>) ROW FORMAT 
DELIMITED
+ FIELDS TERMINATED BY ':' collection items terminated by ',' location 
'${system:test.tmp.dir}/test_null_array';

Review Comment:
   This external table doesn't have purge ``true``, so drop won't delete the 
data, ${system:test.tmp.dir} won't get cleaned up after each query execution.
   Better do LOAD data or if not possible do dfs -rm for all of the data 
folders/file that you have created



##########
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/AbstractGenericUDFArrayBase.java:
##########
@@ -106,4 +106,4 @@ ObjectInspector initListOI(ObjectInspector[] arguments) {
         return 
ObjectInspectorFactory.getStandardListObjectInspector(initOI(arguments));
     }
 
-}
+}

Review Comment:
   unrelated change, revert



##########
ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFArrayDistinct.java:
##########
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.hadoop.hive.ql.udf.generic;
+
+import org.apache.hadoop.hive.ql.exec.Description;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Generic UDF for distinct array
+ * <code>ARRAY_DISTINCT(array(obj1, obj2, obj3...))</code>.
+ *
+ * @see org.apache.hadoop.hive.ql.udf.generic.GenericUDF
+ */
+@Description(name = "array_distinct",
+        value = "_FUNC_(array(obj1, obj2,...)) - "
+                + "The function returns an array of the same type as the input 
array with distinct values.",
+        extended = "Example:\n"
+                + "  > SELECT _FUNC_(array('b', 'd', 'd', 'a')) FROM src LIMIT 
1;\n"
+                + "  ['b', 'd', 'a']")
+public class GenericUDFArrayDistinct extends AbstractGenericUDFArrayBase {
+
+    public GenericUDFArrayDistinct() {
+        super("ARRAY_DISTINCT", 1, 1, ObjectInspector.Category.LIST);
+    }
+
+    @Override
+    public Object evaluate(DeferredObject[] arguments) throws HiveException {
+
+        Object array = arguments[ARRAY_IDX].get();
+
+        if (arrayOI.getListLength(array) <= 0) {
+            return new ArrayList<Object>();
+        }

Review Comment:
   0 denotes an empty array, but -1 denotes the argument to the UDF was 
``null``, if argument is ``null``, we should return ``null`` only, if the array 
is empty then we should return an empty array.
   Better to use ``Collections.emptyArray()``
   Something like this might do:
   ```
   
           // If the array is empty, then there are no duplicates, return back 
the empty array
           if (arrayOI.getListLength(array) == 0) {
               return Collections.emptyList();
           } else if (arrayOI.getListLength(array) < 0) {
               return null;
           }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to