Search in sources :

Example 31 with TypeInfoFactory.doubleTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.doubleTypeInfo in project hive by apache.

the class TestGenericUDFOPPlus method testDoublePlusLong.

@Test
public void testDoublePlusLong() throws HiveException {
    GenericUDFOPPlus udf = new GenericUDFOPPlus();
    // Int
    DoubleWritable left = new DoubleWritable(4.5);
    LongWritable right = new LongWritable(10);
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableDoubleObjectInspector, PrimitiveObjectInspectorFactory.writableLongObjectInspector };
    DeferredObject[] args = { new DeferredJavaObject(left), new DeferredJavaObject(right) };
    PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
    Assert.assertEquals(TypeInfoFactory.doubleTypeInfo, oi.getTypeInfo());
    DoubleWritable res = (DoubleWritable) udf.evaluate(args);
    Assert.assertEquals(new Double(14.5), new Double(res.get()));
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) DoubleWritable(org.apache.hadoop.hive.serde2.io.DoubleWritable) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) LongWritable(org.apache.hadoop.io.LongWritable) Test(org.junit.Test)

Example 32 with TypeInfoFactory.doubleTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.doubleTypeInfo in project hive by apache.

the class TestGenericUDFOPPlus method testVarcharPlusInt.

@Test
public void testVarcharPlusInt() throws HiveException {
    GenericUDFOPPlus udf = new GenericUDFOPPlus();
    // Short
    HiveVarcharWritable left = new HiveVarcharWritable();
    left.set("123");
    IntWritable right = new IntWritable(456);
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableHiveVarcharObjectInspector, PrimitiveObjectInspectorFactory.writableIntObjectInspector };
    DeferredObject[] args = { new DeferredJavaObject(left), new DeferredJavaObject(right) };
    PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
    Assert.assertEquals(oi.getTypeInfo(), TypeInfoFactory.doubleTypeInfo);
    DoubleWritable res = (DoubleWritable) udf.evaluate(args);
    Assert.assertEquals(new Double(579.0), new Double(res.get()));
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) HiveVarcharWritable(org.apache.hadoop.hive.serde2.io.HiveVarcharWritable) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) DoubleWritable(org.apache.hadoop.hive.serde2.io.DoubleWritable) IntWritable(org.apache.hadoop.io.IntWritable) Test(org.junit.Test)

Example 33 with TypeInfoFactory.doubleTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.doubleTypeInfo in project hive by apache.

the class GenericUDFBaseCompare method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 2) {
        throw new UDFArgumentException(opName + " requires two arguments.");
    }
    argumentOIs = arguments;
    for (int i = 0; i < arguments.length; i++) {
        Category category = arguments[i].getCategory();
        if (category != Category.PRIMITIVE) {
            throw new UDFArgumentTypeException(i, "The " + GenericUDFUtils.getOrdinal(i + 1) + " argument of " + opName + "  is expected to a " + Category.PRIMITIVE.toString().toLowerCase() + " type, but " + category.toString().toLowerCase() + " is found");
        }
    }
    if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(TypeInfoFactory.stringTypeInfo) && TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(TypeInfoFactory.stringTypeInfo)) {
        soi0 = (StringObjectInspector) arguments[0];
        soi1 = (StringObjectInspector) arguments[1];
        if (soi0.preferWritable() || soi1.preferWritable()) {
            compareType = CompareType.COMPARE_TEXT;
        } else {
            compareType = CompareType.COMPARE_STRING;
        }
    } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(TypeInfoFactory.intTypeInfo) && TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(TypeInfoFactory.intTypeInfo)) {
        compareType = CompareType.COMPARE_INT;
        ioi0 = (IntObjectInspector) arguments[0];
        ioi1 = (IntObjectInspector) arguments[1];
    } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(TypeInfoFactory.longTypeInfo) && TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(TypeInfoFactory.longTypeInfo)) {
        compareType = CompareType.COMPARE_LONG;
        loi0 = (LongObjectInspector) arguments[0];
        loi1 = (LongObjectInspector) arguments[1];
    } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(TypeInfoFactory.byteTypeInfo) && TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(TypeInfoFactory.byteTypeInfo)) {
        compareType = CompareType.COMPARE_BYTE;
        byoi0 = (ByteObjectInspector) arguments[0];
        byoi1 = (ByteObjectInspector) arguments[1];
    } else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(TypeInfoFactory.booleanTypeInfo) && TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(TypeInfoFactory.booleanTypeInfo)) {
        compareType = CompareType.COMPARE_BOOL;
        boi0 = (BooleanObjectInspector) arguments[0];
        boi1 = (BooleanObjectInspector) arguments[1];
    } else {
        TypeInfo oiTypeInfo0 = TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]);
        TypeInfo oiTypeInfo1 = TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]);
        if (oiTypeInfo0 == oiTypeInfo1 || TypeInfoUtils.doPrimitiveCategoriesMatch(oiTypeInfo0, oiTypeInfo1)) {
            compareType = CompareType.SAME_TYPE;
        } else {
            compareType = CompareType.NEED_CONVERT;
            TypeInfo compareType = FunctionRegistry.getCommonClassForComparison(oiTypeInfo0, oiTypeInfo1);
            // For now, we always convert to double if we can't find a common type
            compareOI = TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo((compareType == null) ? TypeInfoFactory.doubleTypeInfo : compareType);
            converter0 = ObjectInspectorConverters.getConverter(arguments[0], compareOI);
            converter1 = ObjectInspectorConverters.getConverter(arguments[1], compareOI);
        }
    }
    return PrimitiveObjectInspectorFactory.writableBooleanObjectInspector;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) ByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector) Category(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category) IntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)

Example 34 with TypeInfoFactory.doubleTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.doubleTypeInfo in project hive by apache.

the class GenericUDFBaseNwayCompare method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length < 2) {
        throw new UDFArgumentLengthException(getFuncName() + " requires at least 2 arguments, got " + arguments.length);
    }
    if (arguments[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
        throw new UDFArgumentException(getFuncName() + " only takes primitive types, got " + arguments[0].getTypeName());
    }
    argumentOIs = arguments;
    converters = new Converter[arguments.length];
    TypeInfo commonInfo = TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]);
    for (int i = 1; i < arguments.length; i++) {
        PrimitiveTypeInfo currInfo = (PrimitiveTypeInfo) TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[i]);
        commonInfo = FunctionRegistry.getCommonClassForComparison(commonInfo, currInfo);
    }
    resultOI = TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo((commonInfo == null) ? TypeInfoFactory.doubleTypeInfo : commonInfo);
    for (int i = 0; i < arguments.length; i++) {
        converters[i] = ObjectInspectorConverters.getConverter(arguments[i], resultOI);
    }
    return resultOI;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)

Example 35 with TypeInfoFactory.doubleTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.doubleTypeInfo in project hive by apache.

the class TestFunctionRegistry method testImplicitConversion.

public void testImplicitConversion() {
    implicit(TypeInfoFactory.intTypeInfo, TypeInfoFactory.decimalTypeInfo, true);
    implicit(TypeInfoFactory.longTypeInfo, TypeInfoFactory.decimalTypeInfo, true);
    implicit(TypeInfoFactory.floatTypeInfo, TypeInfoFactory.decimalTypeInfo, false);
    implicit(TypeInfoFactory.doubleTypeInfo, TypeInfoFactory.decimalTypeInfo, false);
    implicit(TypeInfoFactory.stringTypeInfo, TypeInfoFactory.decimalTypeInfo, false);
    implicit(TypeInfoFactory.dateTypeInfo, TypeInfoFactory.decimalTypeInfo, false);
    implicit(TypeInfoFactory.timestampTypeInfo, TypeInfoFactory.decimalTypeInfo, false);
    implicit(varchar10, TypeInfoFactory.stringTypeInfo, true);
    implicit(TypeInfoFactory.stringTypeInfo, varchar10, true);
    // Try with parameterized varchar types
    TypeInfo varchar10 = TypeInfoFactory.getPrimitiveTypeInfo("varchar(10)");
    TypeInfo varchar20 = TypeInfoFactory.getPrimitiveTypeInfo("varchar(20)");
    implicit(varchar10, TypeInfoFactory.stringTypeInfo, true);
    implicit(varchar20, TypeInfoFactory.stringTypeInfo, true);
    implicit(TypeInfoFactory.stringTypeInfo, varchar10, true);
    implicit(TypeInfoFactory.stringTypeInfo, varchar20, true);
    implicit(varchar20, varchar10, true);
    implicit(char10, TypeInfoFactory.stringTypeInfo, true);
    implicit(TypeInfoFactory.stringTypeInfo, char10, true);
    implicit(char5, char10, true);
    implicit(char5, varchar10, true);
    implicit(varchar5, char10, true);
    implicit(TypeInfoFactory.intTypeInfo, char10, true);
    implicit(TypeInfoFactory.intTypeInfo, varchar10, true);
    implicit(TypeInfoFactory.intTypeInfo, TypeInfoFactory.stringTypeInfo, true);
}
Also used : PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)

Aggregations

ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)33 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)31 Test (org.junit.Test)30 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)29 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)29 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)29 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)10 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)7 HiveVarcharWritable (org.apache.hadoop.hive.serde2.io.HiveVarcharWritable)7 LongWritable (org.apache.hadoop.io.LongWritable)6 IntWritable (org.apache.hadoop.io.IntWritable)5 ArrayList (java.util.ArrayList)4 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)4 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)3 FloatWritable (org.apache.hadoop.io.FloatWritable)3 Method (java.lang.reflect.Method)2 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)2 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)2 HiveCharWritable (org.apache.hadoop.hive.serde2.io.HiveCharWritable)2 ShortWritable (org.apache.hadoop.hive.serde2.io.ShortWritable)2