use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardListObjectInspector in project hive by apache.
the class TestStandardObjectInspectors method testStandardUnionObjectInspector.
@SuppressWarnings("unchecked")
public void testStandardUnionObjectInspector() throws Throwable {
try {
ArrayList<ObjectInspector> objectInspectors = new ArrayList<ObjectInspector>();
// add primitive types
objectInspectors.add(PrimitiveObjectInspectorFactory.javaIntObjectInspector);
objectInspectors.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
objectInspectors.add(PrimitiveObjectInspectorFactory.javaBooleanObjectInspector);
// add a list
objectInspectors.add(ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaIntObjectInspector));
// add a map
objectInspectors.add(ObjectInspectorFactory.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.javaIntObjectInspector, PrimitiveObjectInspectorFactory.javaStringObjectInspector));
// add a struct
List<String> fieldNames = new ArrayList<String>();
fieldNames.add("myDouble");
fieldNames.add("myLong");
ArrayList<ObjectInspector> fieldObjectInspectors = new ArrayList<ObjectInspector>();
fieldObjectInspectors.add(PrimitiveObjectInspectorFactory.javaDoubleObjectInspector);
fieldObjectInspectors.add(PrimitiveObjectInspectorFactory.javaLongObjectInspector);
objectInspectors.add(ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldObjectInspectors));
StandardUnionObjectInspector uoi1 = ObjectInspectorFactory.getStandardUnionObjectInspector(objectInspectors);
StandardUnionObjectInspector uoi2 = ObjectInspectorFactory.getStandardUnionObjectInspector((ArrayList<ObjectInspector>) objectInspectors.clone());
assertEquals(uoi1, uoi2);
assertEquals(ObjectInspectorUtils.getObjectInspectorName(uoi1), ObjectInspectorUtils.getObjectInspectorName(uoi2));
assertTrue(ObjectInspectorUtils.compareTypes(uoi1, uoi2));
// compareSupported returns false because Union can contain
// an object of Map
assertFalse(ObjectInspectorUtils.compareSupported(uoi1));
// construct unionObjectInspector without Map field.
ArrayList<ObjectInspector> ois = (ArrayList<ObjectInspector>) objectInspectors.clone();
ois.set(4, PrimitiveObjectInspectorFactory.javaIntObjectInspector);
assertTrue(ObjectInspectorUtils.compareSupported(ObjectInspectorFactory.getStandardUnionObjectInspector(ois)));
// metadata
assertEquals(Category.UNION, uoi1.getCategory());
List<? extends ObjectInspector> uois = uoi1.getObjectInspectors();
assertEquals(6, uois.size());
for (int i = 0; i < 6; i++) {
assertEquals(objectInspectors.get(i), uois.get(i));
}
StringBuilder unionTypeName = new StringBuilder();
unionTypeName.append("uniontype<");
for (int i = 0; i < uois.size(); i++) {
if (i > 0) {
unionTypeName.append(",");
}
unionTypeName.append(uois.get(i).getTypeName());
}
unionTypeName.append(">");
assertEquals(unionTypeName.toString(), uoi1.getTypeName());
// TypeInfo
TypeInfo typeInfo1 = TypeInfoUtils.getTypeInfoFromObjectInspector(uoi1);
assertEquals(Category.UNION, typeInfo1.getCategory());
assertEquals(UnionTypeInfo.class.getName(), typeInfo1.getClass().getName());
assertEquals(typeInfo1.getTypeName(), uoi1.getTypeName());
assertEquals(typeInfo1, TypeInfoUtils.getTypeInfoFromTypeString(uoi1.getTypeName()));
TypeInfo typeInfo2 = TypeInfoUtils.getTypeInfoFromObjectInspector(uoi2);
assertEquals(typeInfo1, typeInfo2);
assertEquals(TypeInfoUtils.getStandardJavaObjectInspectorFromTypeInfo(typeInfo1), TypeInfoUtils.getStandardJavaObjectInspectorFromTypeInfo(typeInfo2));
assertEquals(TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo(typeInfo1), TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo(typeInfo2));
// null
assertNull(uoi1.getField(null));
assertEquals(-1, uoi1.getTag(null));
// Union
UnionObject union = new StandardUnion((byte) 0, 1);
assertEquals(0, uoi1.getTag(union));
assertEquals(1, uoi1.getField(union));
assertEquals("{0:1}", SerDeUtils.getJSONString(union, uoi1));
assertEquals(0, ObjectInspectorUtils.compare(union, uoi1, new StandardUnion((byte) 0, 1), uoi2));
assertTrue(ObjectInspectorUtils.copyToStandardObject(union, uoi1).equals(1));
union = new StandardUnion((byte) 1, "two");
assertEquals(1, uoi1.getTag(union));
assertEquals("two", uoi1.getField(union));
assertEquals("{1:\"two\"}", SerDeUtils.getJSONString(union, uoi1));
assertEquals(0, ObjectInspectorUtils.compare(union, uoi1, new StandardUnion((byte) 1, "two"), uoi2));
assertTrue(ObjectInspectorUtils.copyToStandardObject(union, uoi1).equals("two"));
union = new StandardUnion((byte) 2, true);
assertEquals(2, uoi1.getTag(union));
assertEquals(true, uoi1.getField(union));
assertEquals("{2:true}", SerDeUtils.getJSONString(union, uoi1));
assertEquals(0, ObjectInspectorUtils.compare(union, uoi1, new StandardUnion((byte) 2, true), uoi2));
assertTrue(ObjectInspectorUtils.copyToStandardObject(union, uoi1).equals(true));
ArrayList<Integer> iList = new ArrayList<Integer>();
iList.add(4);
iList.add(5);
union = new StandardUnion((byte) 3, iList);
assertEquals(3, uoi1.getTag(union));
assertEquals(iList, uoi1.getField(union));
assertEquals("{3:[4,5]}", SerDeUtils.getJSONString(union, uoi1));
assertEquals(0, ObjectInspectorUtils.compare(union, uoi1, new StandardUnion((byte) 3, iList.clone()), uoi2));
assertTrue(ObjectInspectorUtils.copyToStandardObject(union, uoi1).equals(iList));
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(6, "six");
map.put(7, "seven");
map.put(8, "eight");
union = new StandardUnion((byte) 4, map);
assertEquals(4, uoi1.getTag(union));
assertEquals(map, uoi1.getField(union));
assertEquals("{4:{6:\"six\",7:\"seven\",8:\"eight\"}}", SerDeUtils.getJSONString(union, uoi1));
Throwable th = null;
try {
ObjectInspectorUtils.compare(union, uoi1, new StandardUnion((byte) 4, map.clone()), uoi2, null);
} catch (Throwable t) {
th = t;
}
assertNotNull(th);
assertEquals("Compare on map type not supported!", th.getMessage());
assertTrue(ObjectInspectorUtils.copyToStandardObject(union, uoi1).equals(map));
ArrayList<Object> struct = new ArrayList<Object>(2);
struct.add(9.0);
struct.add(10L);
union = new StandardUnion((byte) 5, struct);
assertEquals(5, uoi1.getTag(union));
assertEquals(struct, uoi1.getField(union));
assertEquals("{5:{\"mydouble\":9.0,\"mylong\":10}}", SerDeUtils.getJSONString(union, uoi1));
assertEquals(0, ObjectInspectorUtils.compare(union, uoi1, new StandardUnion((byte) 5, struct.clone()), uoi2));
assertTrue(ObjectInspectorUtils.copyToStandardObject(union, uoi1).equals(struct));
} catch (Throwable e) {
e.printStackTrace();
throw e;
}
}
use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardListObjectInspector in project hive by apache.
the class TestThriftObjectInspectors method testThriftSetObjectInspector.
@SuppressWarnings("unchecked")
public void testThriftSetObjectInspector() throws Throwable {
try {
ObjectInspector oi1 = ObjectInspectorFactory.getReflectionObjectInspector(SetIntString.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
ObjectInspector oi2 = ObjectInspectorFactory.getReflectionObjectInspector(SetIntString.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
assertEquals(oi1, oi2);
// metadata
assertEquals(Category.STRUCT, oi1.getCategory());
StructObjectInspector soi = (StructObjectInspector) oi1;
List<? extends StructField> fields = soi.getAllStructFieldRefs();
assertEquals(2, fields.size());
assertEquals(fields.get(0), soi.getStructFieldRef("sIntString"));
assertEquals(fields.get(1), soi.getStructFieldRef("aString"));
// null
for (int i = 0; i < fields.size(); i++) {
assertNull(soi.getStructFieldData(null, fields.get(i)));
}
// real object
IntString s1 = new IntString();
s1.setMyint(1);
s1.setMyString("test");
s1.setUnderscore_int(2);
Set<IntString> set1 = new HashSet<IntString>();
set1.add(s1);
SetIntString s = new SetIntString();
s.setSIntString(set1);
s.setAString("setString");
assertEquals(set1, soi.getStructFieldData(s, fields.get(0)));
assertEquals("setString", soi.getStructFieldData(s, fields.get(1)));
// sub fields
assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorFactory.getReflectionObjectInspector(IntString.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT)), fields.get(0).getFieldObjectInspector());
assertEquals(PrimitiveObjectInspectorFactory.javaStringObjectInspector, fields.get(1).getFieldObjectInspector());
// compare set fields
ListObjectInspector loi = (ListObjectInspector) fields.get(0).getFieldObjectInspector();
assertEquals(1, loi.getListLength(set1));
List<IntString> list = (List<IntString>) loi.getList(set1);
assertEquals(1, list.size());
s1 = (IntString) loi.getListElement(list, 0);
assertEquals(1, s1.getMyint());
assertEquals("test", s1.getMyString());
assertEquals(2, s1.getUnderscore_int());
} catch (Throwable e) {
e.printStackTrace();
throw e;
}
}
use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardListObjectInspector in project hive by apache.
the class MatchPath method createSelectListRR.
/*
* add array<struct> to the list of columns
*/
protected static RowResolver createSelectListRR(MatchPath evaluator, PTFInputDef inpDef) throws SemanticException {
RowResolver rr = new RowResolver();
RowResolver inputRR = inpDef.getOutputShape().getRr();
evaluator.inputColumnNamesMap = new HashMap<String, String>();
ArrayList<String> inputColumnNames = new ArrayList<String>();
ArrayList<ObjectInspector> inpColOIs = new ArrayList<ObjectInspector>();
for (ColumnInfo inpCInfo : inputRR.getColumnInfos()) {
ColumnInfo cInfo = new ColumnInfo(inpCInfo);
String colAlias = cInfo.getAlias();
String[] tabColAlias = inputRR.reverseLookup(inpCInfo.getInternalName());
if (tabColAlias != null) {
colAlias = tabColAlias[1];
}
ASTNode inExpr = null;
inExpr = PTFTranslator.getASTNode(inpCInfo, inputRR);
if (inExpr != null) {
rr.putExpression(inExpr, cInfo);
colAlias = inExpr.toStringTree().toLowerCase();
} else {
colAlias = colAlias == null ? cInfo.getInternalName() : colAlias;
rr.put(cInfo.getTabAlias(), colAlias, cInfo);
}
evaluator.inputColumnNamesMap.put(cInfo.getInternalName(), colAlias);
inputColumnNames.add(colAlias);
inpColOIs.add(cInfo.getObjectInspector());
}
StandardListObjectInspector pathAttrOI = ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorFactory.getStandardStructObjectInspector(inputColumnNames, inpColOIs));
ColumnInfo pathColumn = new ColumnInfo(PATHATTR_NAME, TypeInfoUtils.getTypeInfoFromObjectInspector(pathAttrOI), null, false, false);
rr.put(null, PATHATTR_NAME, pathColumn);
return rr;
}
use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardListObjectInspector in project hive by apache.
the class GenericUDFSortArray method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
GenericUDFUtils.ReturnObjectInspectorResolver returnOIResolver;
returnOIResolver = new GenericUDFUtils.ReturnObjectInspectorResolver(true);
checkArgsSize(arguments, 1, 1);
switch(arguments[0].getCategory()) {
case LIST:
if (!((ListObjectInspector) (arguments[0])).getListElementObjectInspector().getCategory().equals(ObjectInspector.Category.UNION)) {
break;
}
default:
throw new UDFArgumentTypeException(0, "Argument 1" + " of function SORT_ARRAY must be " + serdeConstants.LIST_TYPE_NAME + ", and element type should be either primitive, list, struct, or map, " + "but " + arguments[0].getTypeName() + " was found.");
}
ObjectInspector elementObjectInspector = ((ListObjectInspector) (arguments[0])).getListElementObjectInspector();
argumentOIs = arguments;
converters = new Converter[arguments.length];
ObjectInspector returnOI = returnOIResolver.get(elementObjectInspector);
converters[0] = ObjectInspectorConverters.getConverter(elementObjectInspector, returnOI);
return ObjectInspectorFactory.getStandardListObjectInspector(returnOI);
}
use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardListObjectInspector in project SQLWindowing by hbutani.
the class NPathUtils method createSelectListInputOI.
/*
* add array<struct> to the list of columns
*/
public static ObjectInspector createSelectListInputOI(StructObjectInspector inputOI) {
List<? extends StructField> fields = inputOI.getAllStructFieldRefs();
ArrayList<ObjectInspector> selectListFieldOIs = new ArrayList<ObjectInspector>();
ArrayList<String> selectListFieldNames = new ArrayList<String>();
StandardListObjectInspector pathAttrOI = ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorUtils.getStandardObjectInspector(inputOI));
for (StructField f : fields) {
selectListFieldOIs.add(ObjectInspectorUtils.getStandardObjectInspector(f.getFieldObjectInspector()));
selectListFieldNames.add(f.getFieldName());
}
selectListFieldOIs.add(pathAttrOI);
selectListFieldNames.add(PATHATTR_NAME);
return ObjectInspectorFactory.getStandardStructObjectInspector(selectListFieldNames, selectListFieldOIs);
}
Aggregations