Java Code Examples for org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils#compare()

The following examples show how to use org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils#compare() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UDFArrayValueCount.java    From hive-third-functions with Apache License 2.0 6 votes vote down vote up
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
    result.set(0L);

    Object array = arguments[ARRAY_IDX].get();
    Object value = arguments[VALUE_IDX].get();

    int arrayLength = arrayOI.getListLength(array);

    // Check if array is null or empty or value is null
    if (array == null || arrayLength <= 0) {
        return result;
    }

    long count = 0L;
    for (int i = 0; i < arrayLength; ++i) {
        Object listElement = arrayOI.getListElement(array, i);
        if (ObjectInspectorUtils.compare(value, valueOI, listElement, arrayElementOI) == 0) {
            count++;
        }
    }
    result.set(count);

    return result;
}
 
Example 2
Source File: UDFArrayRemove.java    From hive-third-functions with Apache License 2.0 6 votes vote down vote up
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
    result.clear();

    Object array = arguments[ARRAY_IDX].get();
    Object value = arguments[VALUE_IDX].get();

    int arrayLength = arrayOI.getListLength(array);

    // Check if array is null or empty or value is null
    if (value == null || arrayLength <= 0) {
        return null;
    }

    // Compare the value to each element of array until a match is found
    for (int i = 0; i < arrayLength; ++i) {
        Object listElement = arrayOI.getListElement(array, i);
        if (listElement != null) {
            if (ObjectInspectorUtils.compare(value, valueOI, listElement, arrayElementOI) != 0) {
                result.add(listElement);
            }
        }
    }

    return result;
}
 
Example 3
Source File: UDFArrayPosition.java    From hive-third-functions with Apache License 2.0 6 votes vote down vote up
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
    result.set(0L);

    Object array = arguments[ARRAY_IDX].get();
    Object value = arguments[VALUE_IDX].get();

    int arrayLength = arrayOI.getListLength(array);

    // Check if array is null or empty or value is null
    if (value == null || arrayLength <= 0) {
        return result;
    }

    // Compare the value to each element of array until a match is found
    for (int i = 0; i < arrayLength; ++i) {
        Object listElement = arrayOI.getListElement(array, i);
        if (ObjectInspectorUtils.compare(value, valueOI, listElement, arrayElementOI) == 0) {
            result.set(i + 1);
            break;
        }
    }

    return result;
}
 
Example 4
Source File: MapUtils.java    From hive-third-functions with Apache License 2.0 6 votes vote down vote up
public static boolean mapEquals(Map left, Map right, ObjectInspector valueOI) {
    if (left == null || right == null) {
        if (left == null && right == null) {
            return true;
        }
        return false;
    }

    if (left.size() != right.size()) {
        return false;
    }

    for (Object key : left.keySet()) {
        if (ObjectInspectorUtils.compare(left.get(key), valueOI, right.get(key), valueOI) != 0) {
            return false;
        }
    }

    return true;
}
 
Example 5
Source File: ArrayUtils.java    From hive-third-functions with Apache License 2.0 6 votes vote down vote up
public static IntComparator IntArrayCompare(final Object array, final ListObjectInspector arrayOI) {
    return new AbstractIntComparator() {
        @Override
        public int compare(int left, int right) {
            ObjectInspector arrayElementOI = arrayOI.getListElementObjectInspector();
            Object leftArrayElement = arrayOI.getListElement(array, left);
            Object rightArrayElement = arrayOI.getListElement(array, right);
            if (leftArrayElement == null && rightArrayElement == null) {
                return 0;
            }
            if (leftArrayElement == null) {
                return -1;
            }
            if (rightArrayElement == null) {
                return 1;
            }
            int result = ObjectInspectorUtils.compare(leftArrayElement, arrayElementOI, rightArrayElement, arrayElementOI);

            return result;
        }
    };
}
 
Example 6
Source File: ArrayContains.java    From Apache-Hive-Essentials-Second-Edition with MIT License 5 votes vote down vote up
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {

    result.set(false);

    Object array = arguments[ARRAY_IDX].get();
    Object value = arguments[VALUE_IDX].get();

    int arrayLength = arrayOI.getListLength(array);

    // Check if array is null or empty or value is null
    if (value == null || arrayLength <= 0) {
        return result;
    }

    // Compare the value to each element of array until a match is found
    for (int i=0; i<arrayLength; ++i) {
        Object listElement = arrayOI.getListElement(array, i);
        if (listElement != null) {
            if (ObjectInspectorUtils.compare(value, valueOI,
                    listElement, arrayElementOI) == 0) {
                result.set(true);
                break;
            }
        }
    }

    return result;
}
 
Example 7
Source File: UDFArrayContains.java    From hive-third-functions with Apache License 2.0 5 votes vote down vote up
@Override
// Returns <tt>true</tt> if this array contains the specified element.
public Object evaluate(DeferredObject[] arguments) throws HiveException {
    result.set(false);

    Object array = arguments[ARRAY_IDX].get();
    Object value = arguments[VALUE_IDX].get();

    int arrayLength = arrayOI.getListLength(array);

    // Check if array is null or empty or value is null
    if (array == null) {
        return result;
    }

    if (value == null || arrayLength <= 0) {
        return result;
    }

    // Compare the value to each element of array until a match is found
    for (int i = 0; i < arrayLength; ++i) {
        Object listElement = arrayOI.getListElement(array, i);
        if (listElement != null) {
            if (ObjectInspectorUtils.compare(value, valueOI, listElement, arrayElementOI) == 0) {
                result.set(true);
                break;
            }
        }
    }

    return result;
}
 
Example 8
Source File: ArrayUtils.java    From hive-third-functions with Apache License 2.0 5 votes vote down vote up
public static boolean arrayEquals(Object left, Object right, ListObjectInspector arrayOI) {
    if (left == null || right == null) {
        if (left == null && right == null) {
            return true;
        }
        return false;
    }

    int leftArrayLength = arrayOI.getListLength(left);
    int rightArrayLength = arrayOI.getListLength(right);

    if (leftArrayLength != rightArrayLength) {
        return false;
    }

    ObjectInspector arrayElementOI = arrayOI.getListElementObjectInspector();
    for (int i = 0; i < leftArrayLength; i++) {
        Object leftArrayElement = arrayOI.getListElement(left, i);
        Object rightArrayElement = arrayOI.getListElement(right, i);
        int compareValue = ObjectInspectorUtils.compare(leftArrayElement, arrayElementOI, rightArrayElement, arrayElementOI);
        if (compareValue != 0) {
            return false;
        }
    }

    return true;
}
 
Example 9
Source File: MaxRowUDAF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Override
public void merge(@SuppressWarnings("deprecation") AggregationBuffer agg, Object partial)
        throws HiveException {
    if (partial == null) {
        return;
    }

    final MaxAgg maxagg = (MaxAgg) agg;

    final List<Object> otherObjects;
    if (partial instanceof Object[]) {
        otherObjects = Arrays.asList((Object[]) partial);
    } else if (partial instanceof LazyBinaryStruct) {
        otherObjects = ((LazyBinaryStruct) partial).getFieldsAsList();
    } else if (inputStructOI != null) {
        otherObjects = inputStructOI.getStructFieldsDataAsList(partial);
    } else {
        throw new HiveException("Invalid type: " + partial.getClass().getName());
    }

    boolean isMax = false;
    if (maxagg.objects == null) {
        isMax = true;
    } else {
        int cmp = ObjectInspectorUtils.compare(maxagg.objects[0], outputOIs[0],
            otherObjects.get(0), inputOIs[0]);
        if (cmp < 0) {
            isMax = true;
        }
    }

    if (isMax) {
        int length = otherObjects.size();
        maxagg.objects = new Object[length];
        for (int i = 0; i < length; i++) {
            maxagg.objects[i] = ObjectInspectorUtils.copyToStandardObject(
                otherObjects.get(i), inputOIs[i]);
        }
    }
}
 
Example 10
Source File: ArgminUDF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Override
public IntWritable evaluate(DeferredObject[] arguments) throws HiveException {
    Object arg0 = arguments[0].get();
    if (arg0 == null) {
        return null;
    }

    int index = -1;
    Object minObject = null;
    final int size = listOI.getListLength(arg0);
    for (int i = 0; i < size; i++) {
        Object ai = listOI.getListElement(arg0, i);
        if (ai == null) {
            continue;
        }

        if (minObject == null) {
            minObject = ai;
            index = i;
        } else {
            final int cmp = ObjectInspectorUtils.compare(ai, elemOI, minObject, elemOI);
            if (cmp < 0) {
                minObject = ai;
                index = i;
            }
        }
    }

    result.set(index);
    return result;
}
 
Example 11
Source File: ArgmaxUDF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Override
public IntWritable evaluate(DeferredObject[] arguments) throws HiveException {
    Object arg0 = arguments[0].get();
    if (arg0 == null) {
        return null;
    }

    int index = -1;
    Object maxObject = null;
    final int size = listOI.getListLength(arg0);
    for (int i = 0; i < size; i++) {
        Object ai = listOI.getListElement(arg0, i);
        if (ai == null) {
            continue;
        }

        if (maxObject == null) {
            maxObject = ai;
            index = i;
        } else {
            final int cmp = ObjectInspectorUtils.compare(ai, elemOI, maxObject, elemOI);
            if (cmp > 0) {
                maxObject = ai;
                index = i;
            }
        }
    }

    result.set(index);
    return result;
}
 
Example 12
Source File: MinByUDAF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
void merge(final Object newX, final Object newY,
        @Nonnull final ObjectInspector xInputOI,
        @Nonnull final ObjectInspector yInputOI,
        @Nonnull final ObjectInspector yOutputOI) {
    final int cmp = ObjectInspectorUtils.compare(y, yOutputOI, newY, yInputOI);
    if (x == null || cmp > 0) {// found smaller y
        this.x = ObjectInspectorUtils.copyToStandardObject(newX, xInputOI,
            ObjectInspectorCopyOption.JAVA);
        this.y = ObjectInspectorUtils.copyToStandardObject(newY, yInputOI,
            ObjectInspectorCopyOption.JAVA);
    }
}
 
Example 13
Source File: MaxByUDAF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
void merge(final Object newX, final Object newY,
        @Nonnull final ObjectInspector xInputOI,
        @Nonnull final ObjectInspector yInputOI,
        @Nonnull final ObjectInspector yOutputOI) {
    final int cmp = ObjectInspectorUtils.compare(y, yOutputOI, newY, yInputOI);
    if (x == null || cmp < 0) { // found greater y
        this.x = ObjectInspectorUtils.copyToStandardObject(newX, xInputOI,
            ObjectInspectorCopyOption.JAVA);
        this.y = ObjectInspectorUtils.copyToStandardObject(newY, yInputOI,
            ObjectInspectorCopyOption.JAVA);
    }
}
 
Example 14
Source File: EachTopKUDTF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
private boolean isSameGroup(Object arg1) {
    if (arg1 == null && _previousGroup == null) {
        return true;
    } else if (arg1 == null || _previousGroup == null) {
        return false;
    }
    return ObjectInspectorUtils.compare(arg1, argOIs[1], _previousGroup, prevGroupOI) == 0;
}
 
Example 15
Source File: ArrayIntersectUDF.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(InspectableObject otherOI) {
    return ObjectInspectorUtils.compare(o, oi, otherOI.o, otherOI.oi);
}