Skip to content

Commit

Permalink
fix bug #4
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronshan committed Jul 18, 2018
1 parent e014b5c commit 277be74
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mvn clean package -DskipTests

你也可以直接在发布页下载打包好了最新版本 [发布页](https://github.com/aaronshan/hive-third-functions/releases).

> 当前最新的版本是 `2.1.2`
> 当前最新的版本是 `2.1.3`
## 函数

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ It will generate hive-third-functions-${version}-shaded.jar in target directory.

You can also directly download file from [release page](https://github.com/aaronshan/hive-third-functions/releases).

> current latest version is `2.1.2`
> current latest version is `2.1.3`
## Functions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import cc.shanruifeng.functions.fastuitl.ints.IntArrays;
import java.util.ArrayList;
import java.util.Arrays;

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
Expand Down Expand Up @@ -139,18 +141,14 @@ public Object evaluate(DeferredObject[] arguments) throws HiveException {
} else if (compareValue < 0) {
leftCurrentPosition++;
} else {
result.add(converter.convert(leftArrayElement));
result.add(converter.convert(leftArrayOI.getListElement(leftArray, leftPositions[leftCurrentPosition])));
leftCurrentPosition++;
rightCurrentPosition++;

Object leftArrayElementTmp1 = leftArrayOI.getListElement(leftArray, leftPositions[leftBasePosition]);
Object leftArrayElementTmp2 = leftArrayOI.getListElement(leftArray, leftPositions[leftCurrentPosition]);
Object rightArrayElementTmp1 = rightArrayOI.getListElement(rightArray, rightPositions[rightBasePosition]);
Object rightArrayElementTmp2 = rightArrayOI.getListElement(rightArray, rightPositions[rightCurrentPosition]);
while (leftCurrentPosition < leftArrayLength && ObjectInspectorUtils.compare(leftArrayElementTmp1, leftArrayElementOI, leftArrayElementTmp2, leftArrayElementOI) == 0) {
while (leftCurrentPosition < leftArrayLength && compare(leftArrayOI, leftArray, leftBasePosition, leftCurrentPosition) == 0) {
leftCurrentPosition++;
}
while (rightCurrentPosition < rightArrayLength && ObjectInspectorUtils.compare(rightArrayElementTmp1, rightArrayElementOI, rightArrayElementTmp2, rightArrayElementOI) == 0) {
while (rightCurrentPosition < rightArrayLength && compare(rightArrayOI, rightArray, rightBasePosition, rightCurrentPosition) == 0) {
rightCurrentPosition++;
}
}
Expand All @@ -159,6 +157,13 @@ public Object evaluate(DeferredObject[] arguments) throws HiveException {
return result;
}

private int compare(ListObjectInspector arrayOI, Object array, int position1, int position2) {
ObjectInspector arrayElementOI = arrayOI.getListElementObjectInspector();
Object arrayElementTmp1 = arrayOI.getListElement(array, leftPositions[position1]);
Object arrayElementTmp2 = arrayOI.getListElement(array, leftPositions[position2]);
return ObjectInspectorUtils.compare(arrayElementTmp1, arrayElementOI, arrayElementTmp2, arrayElementOI);
}

@Override
public String getDisplayString(String[] strings) {
assert (strings.length == ARG_COUNT);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cc.shanruifeng.functions.array;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
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;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.*;

/**
* @author ruifeng.shan
* @date 2018-07-18
* @time 13:00
*/
public class UDFArrayIntersectTest {
@Test
public void testArrayIntersect() throws HiveException {
UDFArrayIntersect udf = new UDFArrayIntersect();

ObjectInspector leftArrayOI = ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaIntObjectInspector);
ObjectInspector rightArrayOI = ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaIntObjectInspector);
ObjectInspector[] arguments = {leftArrayOI, rightArrayOI};

udf.initialize(arguments);

assertTrue(Iterables.elementsEqual(ImmutableList.of(1,2,5), evaluate(ImmutableList.of(0,1,2,3,4,5), ImmutableList.of(1,1,2,2,5,5), udf)));
assertTrue(Iterables.elementsEqual(ImmutableList.of(1,2,3,4), evaluate(ImmutableList.of(0,1,2,3,4,4), ImmutableList.of(1,1,2,2,3,4), udf)));
assertTrue(Iterables.elementsEqual(ImmutableList.of(1,2,3,4), evaluate(ImmutableList.of(0,1,1,2,3,4,4), ImmutableList.of(1,1,2,2,3,4), udf)));
}

private ArrayList<Object> evaluate(List<Integer> leftArray, List<Integer> rightArray, UDFArrayIntersect udf) throws HiveException {
GenericUDF.DeferredObject leftArrayObj = new GenericUDF.DeferredJavaObject(leftArray);
GenericUDF.DeferredObject rightArrayObj = new GenericUDF.DeferredJavaObject(rightArray);
GenericUDF.DeferredObject[] args = {leftArrayObj, rightArrayObj};
ArrayList<Object> output = (ArrayList<Object>) udf.evaluate(args);
return output;
}
}

1 comment on commit 277be74

@sevenseablue
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我也update一下.

Please sign in to comment.