ayushtkn commented on code in PR #3806:
URL: https://github.com/apache/hive/pull/3806#discussion_r1033556476
##########
ql/src/test/queries/clientpositive/udf_array_distinct.q:
##########
@@ -0,0 +1,20 @@
+--! 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);
Review Comment:
Add one more test case where the argument is `null`
##########
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.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
argument where all duplicate"
+ + " values have been removed.",
Review Comment:
Where all duplicate value have been removed makes different sense than what
this UDF actually does.
It feels like, if you have [1, 1, 2, 3], The duplicate value is 1 it would
remove 1 and return [2,3].
Better :
```
The function returns an array of the same type as the input array with
distinct values"
```
##########
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.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
argument where all duplicate"
+ + " values have been removed.",
+ 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 null;
+ }
+
+ List<?> retArray = ((ListObjectInspector)
argumentOIs[ARRAY_IDX]).getList(array);
+ return
convertArray(retArray.stream().distinct().collect(Collectors.toList()));
Review Comment:
Do we need the ``convertArray`` method? Can we not do something with the
stream like adding a `map`, ``.map(o -> converter.convert(o))``
Give a check to:
```
return retArray.stream().distinct().map(o ->
converter.convert(o)).collect(Collectors.toList());
```
##########
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.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
argument where all duplicate"
+ + " values have been removed.",
+ 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 null;
+ }
Review Comment:
For an empty array, I don't think so we should return null, we should return
an empty array itself.
--
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]