This is an automated email from the ASF dual-hosted git repository.
gsaihemanth pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git
The following commit(s) were added to refs/heads/master by this push:
new bd02abc9eba HIVE-27482 - Minor issues in array_intersect udf (#4469)
(Taraka Rama Rao Lethavadla, reviewed by Okumin, Attila Turoczy, Sai Hemanth
Gantasala)
bd02abc9eba is described below
commit bd02abc9eba3aa0cd3701b26d9169130551a12ef
Author: tarak271 <[email protected]>
AuthorDate: Wed Jul 12 22:12:52 2023 +0530
HIVE-27482 - Minor issues in array_intersect udf (#4469) (Taraka Rama Rao
Lethavadla, reviewed by Okumin, Attila Turoczy, Sai Hemanth Gantasala)
---
.../ql/udf/generic/GenericUDFArrayIntersect.java | 29 +++++++---
.../udf/generic/TestGenericUDFArrayIntersect.java | 64 +++++++++++++++-------
.../queries/clientpositive/udf_array_intersect.q | 4 +-
.../clientpositive/llap/udf_array_intersect.q.out | 26 ++++-----
4 files changed, 77 insertions(+), 46 deletions(-)
diff --git
a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFArrayIntersect.java
b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFArrayIntersect.java
index 699f25f8dcc..98a92044381 100644
---
a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFArrayIntersect.java
+++
b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFArrayIntersect.java
@@ -4,7 +4,7 @@
* 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 intersect in compliance
+ * "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
@@ -19,21 +19,27 @@ package org.apache.hadoop.hive.ql.udf.generic;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
+import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
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 org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils;
+import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* GenericUDFArrayIntersect.
*/
-@Description(name = "array_intersect", value = "_FUNC_(array1, array2) -
Returns an array of the elements in the intersection of array1 and array2,
without duplicates.", extended =
- "Example:\n" + " > SELECT _FUNC_(array(1, 2, 3,4), array(1,2,3)) FROM
src;\n [1,2,3]")
+@Description(name = "array_intersect", value =
+ "_FUNC_(array1, array2) - Returns an array of the elements in the
intersection of array1 and array2,"
+ + " without duplicates.", extended = "Example:\n"
+ + " > SELECT _FUNC_(array(1, 2, 3,4), array(1,2,3)) FROM src;" + "\n
[1,2,3]")
public class GenericUDFArrayIntersect extends AbstractGenericUDFArrayBase {
static final int ARRAY2_IDX = 1;
private static final String FUNC_NAME = "ARRAY_INTERSECT";
+ static final String ERROR_NOT_COMPARABLE = "Input arrays are not comparable
to use ARRAY_INTERSECT udf";
public GenericUDFArrayIntersect() {
super(FUNC_NAME, 2, 2, ObjectInspector.Category.LIST);
@@ -43,19 +49,24 @@ public class GenericUDFArrayIntersect extends
AbstractGenericUDFArrayBase {
ObjectInspector defaultOI = super.initialize(arguments);
checkArgCategory(arguments, ARRAY2_IDX, ObjectInspector.Category.LIST,
FUNC_NAME,
org.apache.hadoop.hive.serde.serdeConstants.LIST_TYPE_NAME); //Array1
is already getting validated in Parent class
+ if
(!ObjectInspectorUtils.compareTypes(arrayOI.getListElementObjectInspector(),
+ ((ListObjectInspector)
arguments[ARRAY2_IDX]).getListElementObjectInspector())) {
+ // check if elements of arrays are comparable
+ throw new UDFArgumentTypeException(1, ERROR_NOT_COMPARABLE);
+ }
return defaultOI;
}
@Override public Object evaluate(DeferredObject[] arguments) throws
HiveException {
Object array = arguments[ARRAY_IDX].get();
-
- if (array == null || arrayOI.getListLength(array) <= 0) {
+ Object array2 = arguments[ARRAY2_IDX].get();
+ if (array == null || array2 == null) {
return null;
}
-
- List<?> retArray3 = ((ListObjectInspector)
argumentOIs[ARRAY_IDX]).getList(array);
- retArray3.retainAll(((ListObjectInspector)
argumentOIs[ARRAY2_IDX]).getList(arguments[ARRAY2_IDX].get()));
- return retArray3.stream().distinct().map(o ->
converter.convert(o)).collect(Collectors.toList());
+ //Copy the first input array into a new list
+ List<?> resultArray = new ArrayList<>(((ListObjectInspector)
argumentOIs[ARRAY_IDX]).getList(array));
+ resultArray.retainAll(((ListObjectInspector)
argumentOIs[ARRAY2_IDX]).getList(array2));
+ return resultArray.stream().distinct().map(o ->
converter.convert(o)).collect(Collectors.toList());
}
}
diff --git
a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFArrayIntersect.java
b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFArrayIntersect.java
index a8ac767e8e6..36443b6e399 100644
---
a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFArrayIntersect.java
+++
b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFArrayIntersect.java
@@ -19,6 +19,7 @@
package org.apache.hadoop.hive.ql.udf.generic;
import org.apache.hadoop.hive.common.type.Date;
+import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.serde2.io.DateWritableV2;
import org.apache.hadoop.hive.serde2.io.DoubleWritable;
@@ -41,12 +42,18 @@ import static java.util.Arrays.asList;
public class TestGenericUDFArrayIntersect {
private final GenericUDFArrayIntersect udf = new GenericUDFArrayIntersect();
- @Test public void testPrimitive() throws HiveException {
- ObjectInspector[] inputOIs = {
ObjectInspectorFactory.getStandardListObjectInspector(
- PrimitiveObjectInspectorFactory.writableIntObjectInspector),
- ObjectInspectorFactory.getStandardListObjectInspector(
- PrimitiveObjectInspectorFactory.writableIntObjectInspector) };
- udf.initialize(inputOIs);
+ @Test
+ public void testPrimitive() throws HiveException {
+ ObjectInspector intObjectInspector =
ObjectInspectorFactory.getStandardListObjectInspector(
+ PrimitiveObjectInspectorFactory.writableIntObjectInspector);
+ ObjectInspector floatObjectInspector =
ObjectInspectorFactory.getStandardListObjectInspector(
+ PrimitiveObjectInspectorFactory.writableFloatObjectInspector);
+ ObjectInspector doubleObjectInspector =
ObjectInspectorFactory.getStandardListObjectInspector(
+ PrimitiveObjectInspectorFactory.writableDoubleObjectInspector);
+ ObjectInspector longObjectInspector =
ObjectInspectorFactory.getStandardListObjectInspector(
+ PrimitiveObjectInspectorFactory.writableLongObjectInspector);
+ ObjectInspector stringObjectInspector =
ObjectInspectorFactory.getStandardListObjectInspector(
+ PrimitiveObjectInspectorFactory.writableStringObjectInspector);
Object i1 = new IntWritable(1);
Object i2 = new IntWritable(2);
@@ -62,6 +69,7 @@ public class TestGenericUDFArrayIntersect {
inputList.add(i3);
inputList.add(i4);
+ udf.initialize(new ObjectInspector[] { intObjectInspector,
intObjectInspector });
runAndVerify(inputList, asList(i5, i6, i7, i8), asList(i1, i2));
i1 = new FloatWritable(3.3f);
@@ -72,27 +80,41 @@ public class TestGenericUDFArrayIntersect {
i6 = new FloatWritable(1.1f);
i7 = new FloatWritable(2.28f);
i8 = new FloatWritable(2.20f);
- List<Object> inputListf = new ArrayList<>();
- inputListf.add(i1);
- inputListf.add(i2);
- inputListf.add(i3);
- inputListf.add(i4);
+ List<Object> inputFloatList = new ArrayList<>();
+ inputFloatList.add(i1);
+ inputFloatList.add(i2);
+ inputFloatList.add(i3);
+ inputFloatList.add(i4);
- runAndVerify(new ArrayList<>(inputListf), asList(i5, i6, i7, i8),
asList(i1, i2));
-
- runAndVerify(new ArrayList<>(inputListf), inputList, new ArrayList<>());
// Int & float arrays
+ udf.initialize(new ObjectInspector[] { floatObjectInspector,
floatObjectInspector });
+ runAndVerify(new ArrayList<>(inputFloatList), asList(i5, i6, i7, i8),
asList(i1, i2));
Object s1 = new Text("1");
Object s2 = new Text("2");
Object s3 = new Text("4");
Object s4 = new Text("5");
- List<Object> inputLists = new ArrayList<>();
- inputLists.add(s1);
- inputLists.add(s2);
- inputLists.add(s3);
- inputLists.add(s4);
-
- runAndVerify(new ArrayList<>(inputListf), inputLists, new ArrayList<>());
// float and string arrays
+ List<Object> inputStringList = new ArrayList<>();
+ inputStringList.add(s1);
+ inputStringList.add(s2);
+ inputStringList.add(s3);
+ inputStringList.add(s4);
+
+ udf.initialize(new ObjectInspector[] { stringObjectInspector,
stringObjectInspector });
+ runAndVerify(inputStringList,asList(s1,s3),asList(s1,s3));
+ // Empty array output
+ runAndVerify(inputStringList,inputStringList,inputStringList);
+ runAndVerify(inputStringList,asList(),asList());
+ // Empty input arrays
+ runAndVerify(asList(),asList(),asList());
+ // Int & float arrays
+ UDFArgumentTypeException exception =
Assert.assertThrows(UDFArgumentTypeException.class, () -> udf.initialize(new
ObjectInspector[] { floatObjectInspector, intObjectInspector }));
+
Assert.assertEquals(GenericUDFArrayIntersect.ERROR_NOT_COMPARABLE,exception.getMessage());
+ // float and string arrays
+ exception = Assert.assertThrows(UDFArgumentTypeException.class, () ->
udf.initialize(new ObjectInspector[] { floatObjectInspector,
stringObjectInspector }));
+
Assert.assertEquals(GenericUDFArrayIntersect.ERROR_NOT_COMPARABLE,exception.getMessage());
+ // long and double arrays
+ exception = Assert.assertThrows(UDFArgumentTypeException.class, () ->
udf.initialize(new ObjectInspector[] { longObjectInspector,
doubleObjectInspector }));
+
Assert.assertEquals(GenericUDFArrayIntersect.ERROR_NOT_COMPARABLE,exception.getMessage());
}
@Test public void testList() throws HiveException {
diff --git a/ql/src/test/queries/clientpositive/udf_array_intersect.q
b/ql/src/test/queries/clientpositive/udf_array_intersect.q
index 7bd1d0059bd..1f44caf54e0 100644
--- a/ql/src/test/queries/clientpositive/udf_array_intersect.q
+++ b/ql/src/test/queries/clientpositive/udf_array_intersect.q
@@ -16,6 +16,8 @@ SELECT array_intersect(array(null),array(null));
SELECT array_intersect(array(1.12, 2.23, 3.34,
null,1.11,1.12,2.9),array(1.12,3.34,1.11,1.12));
+SELECT array(1,2,3),array_intersect(array(1, 2, 3),array(1,3,4));
+
SELECT array_intersect(array(1.1234567890, 2.234567890, 3.34567890, null,
3.3456789, 2.234567,1.1234567890),array(1.1234567890, 3.34567890,
null,2.234567));
SELECT array_intersect(array(11234567890, 2234567890, 334567890, null,
11234567890, 2234567890, 334567890, null),array(11234567890, 2234567890,
334567890));
@@ -37,6 +39,4 @@ select array_intersect(value,value) from test_null_array;
select value, array_intersect(value,value) from test_null_array;
-SELECT array_intersect(array(1, 2, 3, null,3,4),value) from test_null_array;
-
dfs -rm -r ${system:test.tmp.dir}/test_null_array;
\ No newline at end of file
diff --git a/ql/src/test/results/clientpositive/llap/udf_array_intersect.q.out
b/ql/src/test/results/clientpositive/llap/udf_array_intersect.q.out
index 8cbf84713a8..a334b0967f0 100644
--- a/ql/src/test/results/clientpositive/llap/udf_array_intersect.q.out
+++ b/ql/src/test/results/clientpositive/llap/udf_array_intersect.q.out
@@ -30,7 +30,7 @@ POSTHOOK: query: SELECT array_intersect(array(),array())
POSTHOOK: type: QUERY
POSTHOOK: Input: _dummy_database@_dummy_table
#### A masked pattern was here ####
-NULL
+[]
PREHOOK: query: SELECT array_intersect(array(null),array(null))
PREHOOK: type: QUERY
PREHOOK: Input: _dummy_database@_dummy_table
@@ -49,6 +49,15 @@ POSTHOOK: type: QUERY
POSTHOOK: Input: _dummy_database@_dummy_table
#### A masked pattern was here ####
[1.12,3.34,1.11]
+PREHOOK: query: SELECT array(1,2,3),array_intersect(array(1, 2,
3),array(1,3,4))
+PREHOOK: type: QUERY
+PREHOOK: Input: _dummy_database@_dummy_table
+#### A masked pattern was here ####
+POSTHOOK: query: SELECT array(1,2,3),array_intersect(array(1, 2,
3),array(1,3,4))
+POSTHOOK: type: QUERY
+POSTHOOK: Input: _dummy_database@_dummy_table
+#### A masked pattern was here ####
+[1,2,3] [1,3]
PREHOOK: query: SELECT array_intersect(array(1.1234567890, 2.234567890,
3.34567890, null, 3.3456789, 2.234567,1.1234567890),array(1.1234567890,
3.34567890, null,2.234567))
PREHOOK: type: QUERY
PREHOOK: Input: _dummy_database@_dummy_table
@@ -107,9 +116,9 @@ POSTHOOK: query: select array_intersect(value,value) from
test_null_array
POSTHOOK: type: QUERY
POSTHOOK: Input: default@test_null_array
#### A masked pattern was here ####
-NULL
["NULL"]
["null"]
+[]
PREHOOK: query: select value, array_intersect(value,value) from test_null_array
PREHOOK: type: QUERY
PREHOOK: Input: default@test_null_array
@@ -120,15 +129,4 @@ POSTHOOK: Input: default@test_null_array
#### A masked pattern was here ####
["NULL"] ["NULL"]
["null","null"] ["null"]
-[] NULL
-PREHOOK: query: SELECT array_intersect(array(1, 2, 3, null,3,4),value) from
test_null_array
-PREHOOK: type: QUERY
-PREHOOK: Input: default@test_null_array
-#### A masked pattern was here ####
-POSTHOOK: query: SELECT array_intersect(array(1, 2, 3, null,3,4),value) from
test_null_array
-POSTHOOK: type: QUERY
-POSTHOOK: Input: default@test_null_array
-#### A masked pattern was here ####
-NULL
-NULL
-[]
+[] []