Search in sources :

Example 1 with PrimitiveObjectInspectorFactory.javaIntObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaIntObjectInspector in project hive by apache.

the class TestObjectInspectorConverters method testObjectInspectorConverters.

public void testObjectInspectorConverters() throws Throwable {
    try {
        // Boolean
        Converter booleanConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaIntObjectInspector, PrimitiveObjectInspectorFactory.writableBooleanObjectInspector);
        assertEquals("BooleanConverter", new BooleanWritable(false), booleanConverter.convert(Integer.valueOf(0)));
        assertEquals("BooleanConverter", new BooleanWritable(true), booleanConverter.convert(Integer.valueOf(1)));
        assertEquals("BooleanConverter", null, booleanConverter.convert(null));
        // Byte
        Converter byteConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaIntObjectInspector, PrimitiveObjectInspectorFactory.writableByteObjectInspector);
        assertEquals("ByteConverter", new ByteWritable((byte) 0), byteConverter.convert(Integer.valueOf(0)));
        assertEquals("ByteConverter", new ByteWritable((byte) 1), byteConverter.convert(Integer.valueOf(1)));
        assertEquals("ByteConverter", null, byteConverter.convert(null));
        // Short
        Converter shortConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaIntObjectInspector, PrimitiveObjectInspectorFactory.writableShortObjectInspector);
        assertEquals("ShortConverter", new ShortWritable((short) 0), shortConverter.convert(Integer.valueOf(0)));
        assertEquals("ShortConverter", new ShortWritable((short) 1), shortConverter.convert(Integer.valueOf(1)));
        assertEquals("ShortConverter", null, shortConverter.convert(null));
        // Int
        Converter intConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaIntObjectInspector, PrimitiveObjectInspectorFactory.writableIntObjectInspector);
        assertEquals("IntConverter", new IntWritable(0), intConverter.convert(Integer.valueOf(0)));
        assertEquals("IntConverter", new IntWritable(1), intConverter.convert(Integer.valueOf(1)));
        assertEquals("IntConverter", null, intConverter.convert(null));
        // Long
        Converter longConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaIntObjectInspector, PrimitiveObjectInspectorFactory.writableLongObjectInspector);
        assertEquals("LongConverter", new LongWritable(0), longConverter.convert(Integer.valueOf(0)));
        assertEquals("LongConverter", new LongWritable(1), longConverter.convert(Integer.valueOf(1)));
        assertEquals("LongConverter", null, longConverter.convert(null));
        // Float
        Converter floatConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaIntObjectInspector, PrimitiveObjectInspectorFactory.writableFloatObjectInspector);
        assertEquals("LongConverter", new FloatWritable(0), floatConverter.convert(Integer.valueOf(0)));
        assertEquals("LongConverter", new FloatWritable(1), floatConverter.convert(Integer.valueOf(1)));
        assertEquals("LongConverter", null, floatConverter.convert(null));
        // Double
        Converter doubleConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaIntObjectInspector, PrimitiveObjectInspectorFactory.writableDoubleObjectInspector);
        assertEquals("DoubleConverter", new DoubleWritable(0), doubleConverter.convert(Integer.valueOf(0)));
        assertEquals("DoubleConverter", new DoubleWritable(1), doubleConverter.convert(Integer.valueOf(1)));
        assertEquals("DoubleConverter", null, doubleConverter.convert(null));
        // Char
        Converter charConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaBooleanObjectInspector, PrimitiveObjectInspectorFactory.javaHiveCharObjectInspector);
        assertEquals("CharConverter", new HiveChar("TRUE", -1), charConverter.convert(Boolean.valueOf(true)));
        assertEquals("CharConverter", new HiveChar("FALSE", -1), charConverter.convert(Boolean.valueOf(false)));
        charConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaBooleanObjectInspector, PrimitiveObjectInspectorFactory.writableHiveCharObjectInspector);
        assertEquals("CharConverter", new HiveCharWritable(new HiveChar("TRUE", -1)), charConverter.convert(Boolean.valueOf(true)));
        assertEquals("CharConverter", new HiveCharWritable(new HiveChar("FALSE", -1)), charConverter.convert(Boolean.valueOf(false)));
        charConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaIntObjectInspector, PrimitiveObjectInspectorFactory.javaHiveCharObjectInspector);
        assertEquals("CharConverter", new HiveChar("0", -1), charConverter.convert(Integer.valueOf(0)));
        assertEquals("CharConverter", new HiveChar("1", -1), charConverter.convert(Integer.valueOf(1)));
        charConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaIntObjectInspector, PrimitiveObjectInspectorFactory.writableHiveCharObjectInspector);
        assertEquals("CharConverter", new HiveCharWritable(new HiveChar("0", -1)), charConverter.convert(Integer.valueOf(0)));
        assertEquals("CharConverter", new HiveCharWritable(new HiveChar("1", -1)), charConverter.convert(Integer.valueOf(1)));
        charConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.javaHiveCharObjectInspector);
        assertEquals("CharConverter", new HiveChar("hive", -1), charConverter.convert(String.valueOf("hive")));
        charConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.writableHiveCharObjectInspector);
        assertEquals("CharConverter", new HiveCharWritable(new HiveChar("hive", -1)), charConverter.convert(String.valueOf("hive")));
        // VarChar
        Converter varcharConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaBooleanObjectInspector, PrimitiveObjectInspectorFactory.javaHiveVarcharObjectInspector);
        assertEquals("VarCharConverter", new HiveVarchar("TRUE", -1), varcharConverter.convert(Boolean.valueOf(true)));
        assertEquals("VarCharConverter", new HiveVarchar("FALSE", -1), varcharConverter.convert(Boolean.valueOf(false)));
        varcharConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaBooleanObjectInspector, PrimitiveObjectInspectorFactory.writableHiveVarcharObjectInspector);
        assertEquals("VarCharConverter", new HiveVarcharWritable(new HiveVarchar("TRUE", -1)), varcharConverter.convert(Boolean.valueOf(true)));
        assertEquals("VarCharConverter", new HiveVarcharWritable(new HiveVarchar("FALSE", -1)), varcharConverter.convert(Boolean.valueOf(false)));
        varcharConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaIntObjectInspector, PrimitiveObjectInspectorFactory.javaHiveVarcharObjectInspector);
        assertEquals("VarCharConverter", new HiveVarchar("0", -1), varcharConverter.convert(Integer.valueOf(0)));
        assertEquals("VarCharConverter", new HiveVarchar("1", -1), varcharConverter.convert(Integer.valueOf(1)));
        varcharConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaIntObjectInspector, PrimitiveObjectInspectorFactory.writableHiveVarcharObjectInspector);
        assertEquals("VarCharConverter", new HiveVarcharWritable(new HiveVarchar("0", -1)), varcharConverter.convert(Integer.valueOf(0)));
        assertEquals("VarCharConverter", new HiveVarcharWritable(new HiveVarchar("1", -1)), varcharConverter.convert(Integer.valueOf(1)));
        varcharConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.javaHiveVarcharObjectInspector);
        assertEquals("VarCharConverter", new HiveVarchar("hive", -1), varcharConverter.convert(String.valueOf("hive")));
        varcharConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.writableHiveVarcharObjectInspector);
        assertEquals("VarCharConverter", new HiveVarcharWritable(new HiveVarchar("hive", -1)), varcharConverter.convert(String.valueOf("hive")));
        // Text
        Converter textConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaIntObjectInspector, PrimitiveObjectInspectorFactory.writableStringObjectInspector);
        assertEquals("TextConverter", new Text("0"), textConverter.convert(Integer.valueOf(0)));
        assertEquals("TextConverter", new Text("1"), textConverter.convert(Integer.valueOf(1)));
        assertEquals("TextConverter", null, textConverter.convert(null));
        textConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.writableBinaryObjectInspector, PrimitiveObjectInspectorFactory.writableStringObjectInspector);
        assertEquals("TextConverter", new Text("hive"), textConverter.convert(new BytesWritable(new byte[] { (byte) 'h', (byte) 'i', (byte) 'v', (byte) 'e' })));
        assertEquals("TextConverter", null, textConverter.convert(null));
        textConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.writableStringObjectInspector, PrimitiveObjectInspectorFactory.writableStringObjectInspector);
        assertEquals("TextConverter", new Text("hive"), textConverter.convert(new Text("hive")));
        assertEquals("TextConverter", null, textConverter.convert(null));
        textConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.writableStringObjectInspector);
        assertEquals("TextConverter", new Text("hive"), textConverter.convert(new String("hive")));
        assertEquals("TextConverter", null, textConverter.convert(null));
        textConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaHiveDecimalObjectInspector, PrimitiveObjectInspectorFactory.writableStringObjectInspector);
        assertEquals("TextConverter", new Text("100.001"), textConverter.convert(HiveDecimal.create("100.001")));
        assertEquals("TextConverter", null, textConverter.convert(null));
        // Binary
        Converter baConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.writableBinaryObjectInspector);
        assertEquals("BAConverter", new BytesWritable(new byte[] { (byte) 'h', (byte) 'i', (byte) 'v', (byte) 'e' }), baConverter.convert("hive"));
        assertEquals("BAConverter", null, baConverter.convert(null));
        baConverter = ObjectInspectorConverters.getConverter(PrimitiveObjectInspectorFactory.writableStringObjectInspector, PrimitiveObjectInspectorFactory.writableBinaryObjectInspector);
        assertEquals("BAConverter", new BytesWritable(new byte[] { (byte) 'h', (byte) 'i', (byte) 'v', (byte) 'e' }), baConverter.convert(new Text("hive")));
        assertEquals("BAConverter", null, baConverter.convert(null));
        // Union
        ArrayList<String> fieldNames = new ArrayList<String>();
        fieldNames.add("firstInteger");
        fieldNames.add("secondString");
        fieldNames.add("thirdBoolean");
        ArrayList<ObjectInspector> fieldObjectInspectors = new ArrayList<ObjectInspector>();
        fieldObjectInspectors.add(PrimitiveObjectInspectorFactory.javaIntObjectInspector);
        fieldObjectInspectors.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
        fieldObjectInspectors.add(PrimitiveObjectInspectorFactory.javaBooleanObjectInspector);
        ArrayList<String> fieldNames2 = new ArrayList<String>();
        fieldNames2.add("firstString");
        fieldNames2.add("secondInteger");
        fieldNames2.add("thirdBoolean");
        ArrayList<ObjectInspector> fieldObjectInspectors2 = new ArrayList<ObjectInspector>();
        fieldObjectInspectors2.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
        fieldObjectInspectors2.add(PrimitiveObjectInspectorFactory.javaIntObjectInspector);
        fieldObjectInspectors2.add(PrimitiveObjectInspectorFactory.javaBooleanObjectInspector);
        Converter unionConverter0 = ObjectInspectorConverters.getConverter(ObjectInspectorFactory.getStandardUnionObjectInspector(fieldObjectInspectors), ObjectInspectorFactory.getStandardUnionObjectInspector(fieldObjectInspectors2));
        Object convertedObject0 = unionConverter0.convert(new StandardUnionObjectInspector.StandardUnion((byte) 0, 1));
        List<String> expectedObject0 = new ArrayList<String>();
        expectedObject0.add("1");
        assertEquals(expectedObject0, convertedObject0);
        Converter unionConverter1 = ObjectInspectorConverters.getConverter(ObjectInspectorFactory.getStandardUnionObjectInspector(fieldObjectInspectors), ObjectInspectorFactory.getStandardUnionObjectInspector(fieldObjectInspectors2));
        Object convertedObject1 = unionConverter1.convert(new StandardUnionObjectInspector.StandardUnion((byte) 1, "1"));
        List<Integer> expectedObject1 = new ArrayList<Integer>();
        expectedObject1.add(1);
        assertEquals(expectedObject1, convertedObject1);
        Converter unionConverter2 = ObjectInspectorConverters.getConverter(ObjectInspectorFactory.getStandardUnionObjectInspector(fieldObjectInspectors), ObjectInspectorFactory.getStandardUnionObjectInspector(fieldObjectInspectors2));
        Object convertedObject2 = unionConverter2.convert(new StandardUnionObjectInspector.StandardUnion((byte) 2, true));
        List<Boolean> expectedObject2 = new ArrayList<Boolean>();
        expectedObject2.add(true);
        assertEquals(expectedObject2, convertedObject2);
        // Union (extra fields)
        ArrayList<String> fieldNamesExtra = new ArrayList<String>();
        fieldNamesExtra.add("firstInteger");
        fieldNamesExtra.add("secondString");
        fieldNamesExtra.add("thirdBoolean");
        ArrayList<ObjectInspector> fieldObjectInspectorsExtra = new ArrayList<ObjectInspector>();
        fieldObjectInspectorsExtra.add(PrimitiveObjectInspectorFactory.javaIntObjectInspector);
        fieldObjectInspectorsExtra.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
        fieldObjectInspectorsExtra.add(PrimitiveObjectInspectorFactory.javaBooleanObjectInspector);
        ArrayList<String> fieldNamesExtra2 = new ArrayList<String>();
        fieldNamesExtra2.add("firstString");
        fieldNamesExtra2.add("secondInteger");
        ArrayList<ObjectInspector> fieldObjectInspectorsExtra2 = new ArrayList<ObjectInspector>();
        fieldObjectInspectorsExtra2.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
        fieldObjectInspectorsExtra2.add(PrimitiveObjectInspectorFactory.javaIntObjectInspector);
        Converter unionConverterExtra = ObjectInspectorConverters.getConverter(ObjectInspectorFactory.getStandardUnionObjectInspector(fieldObjectInspectorsExtra), ObjectInspectorFactory.getStandardUnionObjectInspector(fieldObjectInspectorsExtra2));
        Object convertedObjectExtra = unionConverterExtra.convert(new StandardUnionObjectInspector.StandardUnion((byte) 2, true));
        List<Object> expectedObjectExtra = new ArrayList<Object>();
        expectedObjectExtra.add(null);
        // we should get back null
        assertEquals(expectedObjectExtra, convertedObjectExtra);
    } catch (Throwable e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : HiveChar(org.apache.hadoop.hive.common.type.HiveChar) ArrayList(java.util.ArrayList) DoubleWritable(org.apache.hadoop.hive.serde2.io.DoubleWritable) ShortWritable(org.apache.hadoop.hive.serde2.io.ShortWritable) Converter(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter) LongWritable(org.apache.hadoop.io.LongWritable) ByteWritable(org.apache.hadoop.hive.serde2.io.ByteWritable) IntWritable(org.apache.hadoop.io.IntWritable) HiveCharWritable(org.apache.hadoop.hive.serde2.io.HiveCharWritable) HiveVarcharWritable(org.apache.hadoop.hive.serde2.io.HiveVarcharWritable) Text(org.apache.hadoop.io.Text) BytesWritable(org.apache.hadoop.io.BytesWritable) HiveVarchar(org.apache.hadoop.hive.common.type.HiveVarchar) FloatWritable(org.apache.hadoop.io.FloatWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable)

Example 2 with PrimitiveObjectInspectorFactory.javaIntObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaIntObjectInspector in project hive by apache.

the class TestObjectInspectorUtils method testObjectInspectorUtils.

public void testObjectInspectorUtils() throws Throwable {
    try {
        ObjectInspector oi1 = ObjectInspectorFactory.getReflectionObjectInspector(Complex.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
        // metadata
        assertEquals(Category.STRUCT, oi1.getCategory());
        // standard ObjectInspector
        StructObjectInspector soi = (StructObjectInspector) ObjectInspectorUtils.getStandardObjectInspector(oi1);
        List<? extends StructField> fields = soi.getAllStructFieldRefs();
        assertEquals(10, fields.size());
        assertEquals(fields.get(0), soi.getStructFieldRef("aint"));
        // null
        for (int i = 0; i < fields.size(); i++) {
            assertNull(soi.getStructFieldData(null, fields.get(i)));
        }
        // real object
        Complex cc = new Complex();
        cc.setAint(1);
        cc.setAString("test");
        List<Integer> c2 = Arrays.asList(new Integer[] { 1, 2, 3 });
        cc.setLint(c2);
        List<String> c3 = Arrays.asList(new String[] { "one", "two" });
        cc.setLString(c3);
        List<IntString> c4 = new ArrayList<IntString>();
        cc.setLintString(c4);
        cc.setMStringString(null);
        // standard object
        Object c = ObjectInspectorUtils.copyToStandardObject(cc, oi1);
        assertEquals(1, soi.getStructFieldData(c, fields.get(0)));
        assertEquals("test", soi.getStructFieldData(c, fields.get(1)));
        assertEquals(c2, soi.getStructFieldData(c, fields.get(2)));
        assertEquals(c3, soi.getStructFieldData(c, fields.get(3)));
        assertEquals(c4, soi.getStructFieldData(c, fields.get(4)));
        assertNull(soi.getStructFieldData(c, fields.get(5)));
        ArrayList<Object> cfields = new ArrayList<Object>();
        for (int i = 0; i < 10; i++) {
            cfields.add(soi.getStructFieldData(c, fields.get(i)));
        }
        assertEquals(cfields, soi.getStructFieldsDataAsList(c));
        // sub fields
        assertEquals(PrimitiveObjectInspectorFactory.javaIntObjectInspector, fields.get(0).getFieldObjectInspector());
        assertEquals(PrimitiveObjectInspectorFactory.javaStringObjectInspector, fields.get(1).getFieldObjectInspector());
        assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaIntObjectInspector), fields.get(2).getFieldObjectInspector());
        assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector), fields.get(3).getFieldObjectInspector());
        assertEquals(ObjectInspectorUtils.getStandardObjectInspector(ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorFactory.getReflectionObjectInspector(IntString.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT))), fields.get(4).getFieldObjectInspector());
        assertEquals(ObjectInspectorFactory.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.javaStringObjectInspector), fields.get(5).getFieldObjectInspector());
    } catch (Throwable e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : ArrayList(java.util.ArrayList) IntString(org.apache.hadoop.hive.serde2.thrift.test.IntString) Complex(org.apache.hadoop.hive.serde2.thrift.test.Complex) IntString(org.apache.hadoop.hive.serde2.thrift.test.IntString)

Example 3 with PrimitiveObjectInspectorFactory.javaIntObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaIntObjectInspector in project hive by apache.

the class TestProtocolBuffersObjectInspectors method testProtocolBuffersObjectInspectors.

public void testProtocolBuffersObjectInspectors() throws Throwable {
    try {
        ObjectInspector oi1 = ObjectInspectorFactory.getReflectionObjectInspector(Complex.class, ObjectInspectorFactory.ObjectInspectorOptions.PROTOCOL_BUFFERS);
        ObjectInspector oi2 = ObjectInspectorFactory.getReflectionObjectInspector(Complex.class, ObjectInspectorFactory.ObjectInspectorOptions.PROTOCOL_BUFFERS);
        assertEquals(oi1, oi2);
        // metadata
        assertEquals(Category.STRUCT, oi1.getCategory());
        StructObjectInspector soi = (StructObjectInspector) oi1;
        List<? extends StructField> fields = soi.getAllStructFieldRefs();
        assertEquals(5, fields.size());
        assertEquals(fields.get(0), soi.getStructFieldRef("aint_"));
        // null
        for (int i = 0; i < fields.size(); i++) {
            assertNull(soi.getStructFieldData(null, fields.get(i)));
        }
        // real object
        List<Integer> c2 = Arrays.asList(new Integer[] { 1, 2, 3 });
        List<String> c3 = Arrays.asList(new String[] { "one", "two" });
        List<IntString> c4 = new ArrayList<IntString>();
        Complex c = new Complex(1, "test", c2, c3, c4);
        assertEquals(1, soi.getStructFieldData(c, fields.get(0)));
        assertEquals("test", soi.getStructFieldData(c, fields.get(1)));
        assertEquals(c2, soi.getStructFieldData(c, fields.get(2)));
        assertEquals(c3, soi.getStructFieldData(c, fields.get(3)));
        assertEquals(c4, soi.getStructFieldData(c, fields.get(4)));
        ArrayList<Object> cfields = new ArrayList<Object>();
        for (int i = 0; i < 5; i++) {
            cfields.add(soi.getStructFieldData(c, fields.get(i)));
        }
        assertEquals(cfields, soi.getStructFieldsDataAsList(c));
        // sub fields
        assertEquals(PrimitiveObjectInspectorFactory.javaIntObjectInspector, fields.get(0).getFieldObjectInspector());
        assertEquals(PrimitiveObjectInspectorFactory.javaStringObjectInspector, fields.get(1).getFieldObjectInspector());
        assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaIntObjectInspector), fields.get(2).getFieldObjectInspector());
        assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector), fields.get(3).getFieldObjectInspector());
        assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorFactory.getReflectionObjectInspector(IntString.class, ObjectInspectorFactory.ObjectInspectorOptions.PROTOCOL_BUFFERS)), fields.get(4).getFieldObjectInspector());
    } catch (Throwable e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : ArrayList(java.util.ArrayList) IntString(org.apache.hadoop.hive.serde2.proto.test.Complexpb.IntString) Complex(org.apache.hadoop.hive.serde2.proto.test.Complexpb.Complex) IntString(org.apache.hadoop.hive.serde2.proto.test.Complexpb.IntString)

Example 4 with PrimitiveObjectInspectorFactory.javaIntObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaIntObjectInspector in project hive by apache.

the class TestThriftObjectInspectors method testThriftObjectInspectors.

public void testThriftObjectInspectors() throws Throwable {
    try {
        ObjectInspector oi1 = ObjectInspectorFactory.getReflectionObjectInspector(Complex.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
        ObjectInspector oi2 = ObjectInspectorFactory.getReflectionObjectInspector(Complex.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
        assertEquals(oi1, oi2);
        // metadata
        assertEquals(Category.STRUCT, oi1.getCategory());
        StructObjectInspector soi = (StructObjectInspector) oi1;
        List<? extends StructField> fields = soi.getAllStructFieldRefs();
        assertEquals(10, fields.size());
        assertEquals(fields.get(0), soi.getStructFieldRef("aint"));
        // null
        for (int i = 0; i < fields.size(); i++) {
            assertNull(soi.getStructFieldData(null, fields.get(i)));
        }
        ObjectInspector oi = ObjectInspectorFactory.getReflectionObjectInspector(PropValueUnion.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
        assertNotNull(oi.toString());
        // real object
        Complex c = new Complex();
        c.setAint(1);
        c.setAString("test");
        List<Integer> c2 = Arrays.asList(new Integer[] { 1, 2, 3 });
        c.setLint(c2);
        List<String> c3 = Arrays.asList(new String[] { "one", "two" });
        c.setLString(c3);
        List<IntString> c4 = new ArrayList<IntString>();
        c.setLintString(c4);
        c.setMStringString(null);
        c.setAttributes(null);
        c.setUnionField1(null);
        c.setUnionField2(null);
        c.setUnionField3(null);
        assertEquals(1, soi.getStructFieldData(c, fields.get(0)));
        assertEquals("test", soi.getStructFieldData(c, fields.get(1)));
        assertEquals(c2, soi.getStructFieldData(c, fields.get(2)));
        assertEquals(c3, soi.getStructFieldData(c, fields.get(3)));
        assertEquals(c4, soi.getStructFieldData(c, fields.get(4)));
        assertNull(soi.getStructFieldData(c, fields.get(5)));
        assertNull(soi.getStructFieldData(c, fields.get(6)));
        assertNull(soi.getStructFieldData(c, fields.get(7)));
        assertNull(soi.getStructFieldData(c, fields.get(8)));
        assertNull(soi.getStructFieldData(c, fields.get(9)));
        ArrayList<Object> cfields = new ArrayList<Object>();
        for (int i = 0; i < 10; i++) {
            cfields.add(soi.getStructFieldData(c, fields.get(i)));
        }
        assertEquals(cfields, soi.getStructFieldsDataAsList(c));
        // sub fields
        assertEquals(PrimitiveObjectInspectorFactory.javaIntObjectInspector, fields.get(0).getFieldObjectInspector());
        assertEquals(PrimitiveObjectInspectorFactory.javaStringObjectInspector, fields.get(1).getFieldObjectInspector());
        assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaIntObjectInspector), fields.get(2).getFieldObjectInspector());
        assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector), fields.get(3).getFieldObjectInspector());
        assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorFactory.getReflectionObjectInspector(IntString.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT)), fields.get(4).getFieldObjectInspector());
        assertEquals(ObjectInspectorFactory.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.javaStringObjectInspector), fields.get(5).getFieldObjectInspector());
    } catch (Throwable e) {
        e.printStackTrace();
        throw e;
    }
}
Also used : ArrayList(java.util.ArrayList) SetIntString(org.apache.hadoop.hive.serde2.thrift.test.SetIntString) IntString(org.apache.hadoop.hive.serde2.thrift.test.IntString) Complex(org.apache.hadoop.hive.serde2.thrift.test.Complex) SetIntString(org.apache.hadoop.hive.serde2.thrift.test.SetIntString) IntString(org.apache.hadoop.hive.serde2.thrift.test.IntString)

Example 5 with PrimitiveObjectInspectorFactory.javaIntObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaIntObjectInspector in project hive by apache.

the class TestGenericUDFDateAdd method testDateWritablepToDate.

public void testDateWritablepToDate() throws HiveException {
    GenericUDFDateAdd udf = new GenericUDFDateAdd();
    ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.writableDateObjectInspector;
    ObjectInspector valueOI2 = PrimitiveObjectInspectorFactory.javaIntObjectInspector;
    ObjectInspector[] arguments = { valueOI1, valueOI2 };
    udf.initialize(arguments);
    DeferredObject valueObj1 = new DeferredJavaObject(new DateWritable(new Date(109, 06, 20)));
    DeferredObject valueObj2 = new DeferredJavaObject(new Integer("4"));
    DeferredObject[] args = { valueObj1, valueObj2 };
    DateWritable output = (DateWritable) udf.evaluate(args);
    assertEquals("date_add() test for DATEWRITABLE failed ", "2009-07-24", output.toString());
    // Test with null args
    args = new DeferredObject[] { new DeferredJavaObject(null), valueObj2 };
    assertNull("date_add() 1st arg null", udf.evaluate(args));
    args = new DeferredObject[] { valueObj1, new DeferredJavaObject(null) };
    assertNull("date_add() 2nd arg null", udf.evaluate(args));
    args = new DeferredObject[] { new DeferredJavaObject(null), new DeferredJavaObject(null) };
    assertNull("date_add() both args null", udf.evaluate(args));
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) DateWritable(org.apache.hadoop.hive.serde2.io.DateWritable) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) GenericUDFDateAdd(org.apache.hadoop.hive.ql.udf.generic.GenericUDFDateAdd) Date(java.sql.Date)

Aggregations

ArrayList (java.util.ArrayList)10 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)10 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)6 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)6 DateWritable (org.apache.hadoop.hive.serde2.io.DateWritable)6 Test (org.junit.Test)5 UnionObject (org.apache.hadoop.hive.serde2.objectinspector.UnionObject)4 HashMap (java.util.HashMap)3 GenericUDFDateAdd (org.apache.hadoop.hive.ql.udf.generic.GenericUDFDateAdd)3 GenericUDFDateSub (org.apache.hadoop.hive.ql.udf.generic.GenericUDFDateSub)3 Text (org.apache.hadoop.io.Text)3 TypeToken (com.google.common.reflect.TypeToken)2 Date (java.sql.Date)2 Timestamp (java.sql.Timestamp)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 TimestampWritable (org.apache.hadoop.hive.serde2.io.TimestampWritable)2 Complex (org.apache.hadoop.hive.serde2.thrift.test.Complex)2 IntString (org.apache.hadoop.hive.serde2.thrift.test.IntString)2 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)2