use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardStructObjectInspector in project hive by apache.
the class TestGenericUDFSortArrayByField method testSortPrimitiveTupleTwoFieldOrderDESC.
@Test
public void testSortPrimitiveTupleTwoFieldOrderDESC() throws HiveException {
List<ObjectInspector> tuple = new ArrayList<ObjectInspector>();
tuple.add(PrimitiveObjectInspectorFactory.writableStringObjectInspector);
tuple.add(PrimitiveObjectInspectorFactory.writableStringObjectInspector);
ObjectInspector[] inputOIs = { ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorFactory.getStandardStructObjectInspector(asList("Company", "Department"), tuple)), PrimitiveObjectInspectorFactory.writableStringObjectInspector, PrimitiveObjectInspectorFactory.writableHiveVarcharObjectInspector, PrimitiveObjectInspectorFactory.writableStringObjectInspector };
udf.initialize(inputOIs);
Object i1 = asList(new Text("Linkedin"), new Text("HR"));
Object i2 = asList(new Text("Linkedin"), new Text("IT"));
Object i3 = asList(new Text("Linkedin"), new Text("Finance"));
Object i4 = asList(new Text("Facebook"), new Text("IT"));
Object i5 = asList(new Text("Facebook"), new Text("Finance"));
Object i6 = asList(new Text("Facebook"), new Text("HR"));
Object i7 = asList(new Text("Google"), new Text("Logistics"));
Object i8 = asList(new Text("Google"), new Text("Finance"));
Object i9 = asList(new Text("Google"), new Text("HR"));
HiveVarchar vc = new HiveVarchar();
vc.setValue("Department");
GenericUDF.DeferredJavaObject[] argas = { new GenericUDF.DeferredJavaObject(asList(i1, i2, i3, i4, i5, i6, i7, i8, i9)), new GenericUDF.DeferredJavaObject(new Text("Company")), new GenericUDF.DeferredJavaObject(new HiveVarcharWritable(vc)), new GenericUDF.DeferredJavaObject(new Text("DESC")) };
runAndVerify(argas, asList(i2, i1, i3, i7, i9, i8, i4, i6, i5));
}
use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardStructObjectInspector in project hive by apache.
the class SerdeRandomRowSource method chooseSchema.
private void chooseSchema() {
HashSet hashSet = null;
boolean allTypes;
boolean onlyOne = (r.nextInt(100) == 7);
if (onlyOne) {
columnCount = 1;
allTypes = false;
} else {
allTypes = r.nextBoolean();
if (allTypes) {
// One of each type.
columnCount = possibleHiveTypeNames.length;
hashSet = new HashSet<Integer>();
} else {
columnCount = 1 + r.nextInt(20);
}
}
typeNames = new ArrayList<String>(columnCount);
primitiveCategories = new PrimitiveCategory[columnCount];
primitiveTypeInfos = new PrimitiveTypeInfo[columnCount];
primitiveObjectInspectorList = new ArrayList<ObjectInspector>(columnCount);
List<String> columnNames = new ArrayList<String>(columnCount);
for (int c = 0; c < columnCount; c++) {
columnNames.add(String.format("col%d", c));
String typeName;
if (onlyOne) {
typeName = possibleHiveTypeNames[r.nextInt(possibleHiveTypeNames.length)];
} else {
int typeNum;
if (allTypes) {
while (true) {
typeNum = r.nextInt(possibleHiveTypeNames.length);
Integer typeNumInteger = new Integer(typeNum);
if (!hashSet.contains(typeNumInteger)) {
hashSet.add(typeNumInteger);
break;
}
}
} else {
typeNum = r.nextInt(possibleHiveTypeNames.length);
}
typeName = possibleHiveTypeNames[typeNum];
}
if (typeName.equals("char")) {
int maxLength = 1 + r.nextInt(100);
typeName = String.format("char(%d)", maxLength);
} else if (typeName.equals("varchar")) {
int maxLength = 1 + r.nextInt(100);
typeName = String.format("varchar(%d)", maxLength);
} else if (typeName.equals("decimal")) {
typeName = String.format("decimal(%d,%d)", HiveDecimal.SYSTEM_DEFAULT_PRECISION, HiveDecimal.SYSTEM_DEFAULT_SCALE);
}
PrimitiveTypeInfo primitiveTypeInfo = (PrimitiveTypeInfo) TypeInfoUtils.getTypeInfoFromTypeString(typeName);
primitiveTypeInfos[c] = primitiveTypeInfo;
PrimitiveCategory primitiveCategory = primitiveTypeInfo.getPrimitiveCategory();
primitiveCategories[c] = primitiveCategory;
primitiveObjectInspectorList.add(PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(primitiveTypeInfo));
typeNames.add(typeName);
}
rowStructObjectInspector = ObjectInspectorFactory.getStandardStructObjectInspector(columnNames, primitiveObjectInspectorList);
}
use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardStructObjectInspector in project haivvreo by jghoman.
the class AvroObjectInspectorGenerator method createObjectInspectorWorker.
private ObjectInspector createObjectInspectorWorker(TypeInfo ti) throws SerDeException {
// at deserialization and the object inspector will never see the actual union.
if (!supportedCategories(ti))
throw new HaivvreoException("Don't yet support this type: " + ti);
ObjectInspector result;
switch(ti.getCategory()) {
case PRIMITIVE:
PrimitiveTypeInfo pti = (PrimitiveTypeInfo) ti;
result = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(pti.getPrimitiveCategory());
break;
case STRUCT:
StructTypeInfo sti = (StructTypeInfo) ti;
ArrayList<ObjectInspector> ois = new ArrayList<ObjectInspector>(sti.getAllStructFieldTypeInfos().size());
for (TypeInfo typeInfo : sti.getAllStructFieldTypeInfos()) {
ois.add(createObjectInspectorWorker(typeInfo));
}
result = ObjectInspectorFactory.getStandardStructObjectInspector(sti.getAllStructFieldNames(), ois);
break;
case MAP:
MapTypeInfo mti = (MapTypeInfo) ti;
result = ObjectInspectorFactory.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.STRING), createObjectInspectorWorker(mti.getMapValueTypeInfo()));
break;
case LIST:
ListTypeInfo ati = (ListTypeInfo) ti;
result = ObjectInspectorFactory.getStandardListObjectInspector(createObjectInspectorWorker(ati.getListElementTypeInfo()));
break;
case UNION:
UnionTypeInfo uti = (UnionTypeInfo) ti;
List<TypeInfo> allUnionObjectTypeInfos = uti.getAllUnionObjectTypeInfos();
List<ObjectInspector> unionObjectInspectors = new ArrayList<ObjectInspector>(allUnionObjectTypeInfos.size());
for (TypeInfo typeInfo : allUnionObjectTypeInfos) {
unionObjectInspectors.add(createObjectInspectorWorker(typeInfo));
}
result = ObjectInspectorFactory.getStandardUnionObjectInspector(unionObjectInspectors);
break;
default:
throw new HaivvreoException("No Hive categories matched: " + ti);
}
return result;
}
use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardStructObjectInspector in project presto by prestodb.
the class HiveWriteUtils method getJavaObjectInspector.
public static ObjectInspector getJavaObjectInspector(Type type) {
if (type.equals(BooleanType.BOOLEAN)) {
return javaBooleanObjectInspector;
} else if (type.equals(BigintType.BIGINT)) {
return javaLongObjectInspector;
} else if (type.equals(IntegerType.INTEGER)) {
return javaIntObjectInspector;
} else if (type.equals(SmallintType.SMALLINT)) {
return javaShortObjectInspector;
} else if (type.equals(TinyintType.TINYINT)) {
return javaByteObjectInspector;
} else if (type.equals(RealType.REAL)) {
return javaFloatObjectInspector;
} else if (type.equals(DoubleType.DOUBLE)) {
return javaDoubleObjectInspector;
} else if (type instanceof VarcharType) {
return writableStringObjectInspector;
} else if (type instanceof CharType) {
return writableHiveCharObjectInspector;
} else if (type.equals(VarbinaryType.VARBINARY)) {
return javaByteArrayObjectInspector;
} else if (type.equals(DateType.DATE)) {
return javaDateObjectInspector;
} else if (type.equals(TimestampType.TIMESTAMP)) {
return javaTimestampObjectInspector;
} else if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
} else if (isArrayType(type)) {
return ObjectInspectorFactory.getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
} else if (isMapType(type)) {
ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
return ObjectInspectorFactory.getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
} else if (isRowType(type)) {
return ObjectInspectorFactory.getStandardStructObjectInspector(type.getTypeSignature().getParameters().stream().map(parameter -> parameter.getNamedTypeSignature().getName()).collect(toList()), type.getTypeParameters().stream().map(HiveWriteUtils::getJavaObjectInspector).collect(toList()));
}
throw new IllegalArgumentException("unsupported type: " + type);
}
use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardStructObjectInspector in project cdap by caskdata.
the class StandardObjectInspectorsTest method doStandardObjectInspectorTest.
private void doStandardObjectInspectorTest(boolean testComments) {
ArrayList<String> fieldNames = new ArrayList<>();
fieldNames.add("firstInteger");
fieldNames.add("secondString");
fieldNames.add("thirdBoolean");
ArrayList<ObjectInspector> fieldObjectInspectors = new ArrayList<>();
fieldObjectInspectors.add(PrimitiveObjectInspectorFactory.javaIntObjectInspector);
fieldObjectInspectors.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
fieldObjectInspectors.add(PrimitiveObjectInspectorFactory.javaBooleanObjectInspector);
ArrayList<String> fieldComments = new ArrayList<>(3);
if (testComments) {
fieldComments.add("firstInteger comment");
fieldComments.add("secondString comment");
fieldComments.add("thirdBoolean comment");
} else {
// should have null for non-specified comments
for (int i = 0; i < 3; i++) {
fieldComments.add(null);
}
}
StandardStructObjectInspector soi1 = testComments ? ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldObjectInspectors, fieldComments) : ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldObjectInspectors);
StandardStructObjectInspector soi2 = testComments ? ObjectInspectorFactory.getStandardStructObjectInspector((ArrayList<String>) fieldNames.clone(), (ArrayList<ObjectInspector>) fieldObjectInspectors.clone(), (ArrayList<String>) fieldComments.clone()) : ObjectInspectorFactory.getStandardStructObjectInspector((ArrayList<String>) fieldNames.clone(), (ArrayList<ObjectInspector>) fieldObjectInspectors.clone());
Assert.assertEquals(soi1, soi2);
// metadata
Assert.assertEquals(Category.STRUCT, soi1.getCategory());
List<? extends StructField> fields = soi1.getAllStructFieldRefs();
Assert.assertEquals(3, fields.size());
for (int i = 0; i < 3; i++) {
Assert.assertEquals(fieldNames.get(i).toLowerCase(), fields.get(i).getFieldName());
Assert.assertEquals(fieldObjectInspectors.get(i), fields.get(i).getFieldObjectInspector());
Assert.assertEquals(fieldComments.get(i), fields.get(i).getFieldComment());
}
Assert.assertEquals(fields.get(1), soi1.getStructFieldRef("secondString"));
StringBuilder structTypeName = new StringBuilder();
structTypeName.append("struct<");
for (int i = 0; i < fields.size(); i++) {
if (i > 0) {
structTypeName.append(",");
}
structTypeName.append(fields.get(i).getFieldName());
structTypeName.append(":");
structTypeName.append(fields.get(i).getFieldObjectInspector().getTypeName());
}
structTypeName.append(">");
Assert.assertEquals(structTypeName.toString(), soi1.getTypeName());
// null
Assert.assertNull(soi1.getStructFieldData(null, fields.get(0)));
Assert.assertNull(soi1.getStructFieldData(null, fields.get(1)));
Assert.assertNull(soi1.getStructFieldData(null, fields.get(2)));
Assert.assertNull(soi1.getStructFieldsDataAsList(null));
// HashStruct
ArrayList<Object> struct = new ArrayList<>(3);
struct.add(1);
struct.add("two");
struct.add(true);
Assert.assertEquals(1, soi1.getStructFieldData(struct, fields.get(0)));
Assert.assertEquals("two", soi1.getStructFieldData(struct, fields.get(1)));
Assert.assertEquals(true, soi1.getStructFieldData(struct, fields.get(2)));
// Settable
Object struct3 = soi1.create();
soi1.setStructFieldData(struct3, fields.get(0), 1);
soi1.setStructFieldData(struct3, fields.get(1), "two");
soi1.setStructFieldData(struct3, fields.get(2), true);
Assert.assertEquals(struct, struct3);
}
Aggregations