use of org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo in project hive by apache.
the class VectorAssignRow method assignConvertRowColumn.
private void assignConvertRowColumn(ColumnVector columnVector, int batchIndex, TypeInfo targetTypeInfo, ObjectInspector sourceObjectInspector, Writable convertTargetWritable, Object object) {
final Category targetCategory = targetTypeInfo.getCategory();
if (targetCategory == null) {
/*
* This is a column that we don't want (i.e. not included) -- we are done.
*/
return;
}
if (object == null) {
VectorizedBatchUtil.setNullColIsNullValue(columnVector, batchIndex);
return;
}
try {
switch(targetCategory) {
case PRIMITIVE:
final PrimitiveObjectInspector sourcePrimitiveOI = (PrimitiveObjectInspector) sourceObjectInspector;
final PrimitiveCategory targetPrimitiveCategory = ((PrimitiveTypeInfo) targetTypeInfo).getPrimitiveCategory();
switch(targetPrimitiveCategory) {
case VOID:
VectorizedBatchUtil.setNullColIsNullValue(columnVector, batchIndex);
return;
case BOOLEAN:
((LongColumnVector) columnVector).vector[batchIndex] = (PrimitiveObjectInspectorUtils.getBoolean(object, sourcePrimitiveOI) ? 1 : 0);
break;
case BYTE:
((LongColumnVector) columnVector).vector[batchIndex] = PrimitiveObjectInspectorUtils.getByte(object, sourcePrimitiveOI);
break;
case SHORT:
((LongColumnVector) columnVector).vector[batchIndex] = PrimitiveObjectInspectorUtils.getShort(object, sourcePrimitiveOI);
break;
case INT:
((LongColumnVector) columnVector).vector[batchIndex] = PrimitiveObjectInspectorUtils.getInt(object, sourcePrimitiveOI);
break;
case LONG:
((LongColumnVector) columnVector).vector[batchIndex] = PrimitiveObjectInspectorUtils.getLong(object, sourcePrimitiveOI);
break;
case TIMESTAMP:
{
final Timestamp timestamp = PrimitiveObjectInspectorUtils.getTimestamp(object, sourcePrimitiveOI);
if (timestamp == null) {
VectorizedBatchUtil.setNullColIsNullValue(columnVector, batchIndex);
return;
}
((TimestampColumnVector) columnVector).set(batchIndex, timestamp.toSqlTimestamp());
}
break;
case DATE:
{
final Date date = PrimitiveObjectInspectorUtils.getDate(object, sourcePrimitiveOI);
if (date == null) {
VectorizedBatchUtil.setNullColIsNullValue(columnVector, batchIndex);
return;
}
DateWritableV2 dateWritable = (DateWritableV2) convertTargetWritable;
if (dateWritable == null) {
dateWritable = new DateWritableV2();
}
dateWritable.set(date);
((LongColumnVector) columnVector).vector[batchIndex] = dateWritable.getDays();
}
break;
case FLOAT:
((DoubleColumnVector) columnVector).vector[batchIndex] = PrimitiveObjectInspectorUtils.getFloat(object, sourcePrimitiveOI);
break;
case DOUBLE:
((DoubleColumnVector) columnVector).vector[batchIndex] = PrimitiveObjectInspectorUtils.getDouble(object, sourcePrimitiveOI);
break;
case BINARY:
{
final BytesWritable bytesWritable = PrimitiveObjectInspectorUtils.getBinary(object, sourcePrimitiveOI);
if (bytesWritable == null) {
VectorizedBatchUtil.setNullColIsNullValue(columnVector, batchIndex);
return;
}
((BytesColumnVector) columnVector).setVal(batchIndex, bytesWritable.getBytes(), 0, bytesWritable.getLength());
}
break;
case STRING:
{
final String string = PrimitiveObjectInspectorUtils.getString(object, sourcePrimitiveOI);
if (string == null) {
VectorizedBatchUtil.setNullColIsNullValue(columnVector, batchIndex);
return;
}
Text text = (Text) convertTargetWritable;
if (text == null) {
text = new Text();
}
text.set(string);
((BytesColumnVector) columnVector).setVal(batchIndex, text.getBytes(), 0, text.getLength());
}
break;
case VARCHAR:
{
// UNDONE: Performance problem with conversion to String, then bytes...
final HiveVarchar hiveVarchar = PrimitiveObjectInspectorUtils.getHiveVarchar(object, sourcePrimitiveOI);
if (hiveVarchar == null) {
VectorizedBatchUtil.setNullColIsNullValue(columnVector, batchIndex);
return;
}
// TODO: Do we need maxLength checking?
byte[] bytes = hiveVarchar.getValue().getBytes();
((BytesColumnVector) columnVector).setVal(batchIndex, bytes, 0, bytes.length);
}
break;
case CHAR:
{
// UNDONE: Performance problem with conversion to String, then bytes...
final HiveChar hiveChar = PrimitiveObjectInspectorUtils.getHiveChar(object, sourcePrimitiveOI);
if (hiveChar == null) {
VectorizedBatchUtil.setNullColIsNullValue(columnVector, batchIndex);
return;
}
// We store CHAR in vector row batch with padding stripped.
// TODO: Do we need maxLength checking?
final byte[] bytes = hiveChar.getStrippedValue().getBytes();
((BytesColumnVector) columnVector).setVal(batchIndex, bytes, 0, bytes.length);
}
break;
case DECIMAL:
{
final HiveDecimal hiveDecimal = PrimitiveObjectInspectorUtils.getHiveDecimal(object, sourcePrimitiveOI);
if (hiveDecimal == null) {
VectorizedBatchUtil.setNullColIsNullValue(columnVector, batchIndex);
return;
}
if (columnVector instanceof Decimal64ColumnVector) {
Decimal64ColumnVector dec64ColVector = (Decimal64ColumnVector) columnVector;
dec64ColVector.set(batchIndex, hiveDecimal);
if (dec64ColVector.isNull[batchIndex]) {
return;
}
} else {
((DecimalColumnVector) columnVector).set(batchIndex, hiveDecimal);
}
}
break;
case INTERVAL_YEAR_MONTH:
{
final HiveIntervalYearMonth intervalYearMonth = PrimitiveObjectInspectorUtils.getHiveIntervalYearMonth(object, sourcePrimitiveOI);
if (intervalYearMonth == null) {
VectorizedBatchUtil.setNullColIsNullValue(columnVector, batchIndex);
return;
}
((LongColumnVector) columnVector).vector[batchIndex] = intervalYearMonth.getTotalMonths();
}
break;
case INTERVAL_DAY_TIME:
{
final HiveIntervalDayTime intervalDayTime = PrimitiveObjectInspectorUtils.getHiveIntervalDayTime(object, sourcePrimitiveOI);
if (intervalDayTime == null) {
VectorizedBatchUtil.setNullColIsNullValue(columnVector, batchIndex);
return;
}
((IntervalDayTimeColumnVector) columnVector).set(batchIndex, intervalDayTime);
}
break;
default:
throw new RuntimeException("Primitive category " + targetPrimitiveCategory.name() + " not supported");
}
break;
case LIST:
{
final ListColumnVector listColumnVector = (ListColumnVector) columnVector;
final ListObjectInspector sourceListOI = (ListObjectInspector) sourceObjectInspector;
final ObjectInspector sourceElementOI = sourceListOI.getListElementObjectInspector();
final int size = sourceListOI.getListLength(object);
final TypeInfo targetElementTypeInfo = ((ListTypeInfo) targetTypeInfo).getListElementTypeInfo();
listColumnVector.offsets[batchIndex] = listColumnVector.childCount;
listColumnVector.childCount += size;
listColumnVector.ensureSize(listColumnVector.childCount, true);
listColumnVector.lengths[batchIndex] = size;
for (int i = 0; i < size; i++) {
final Object element = sourceListOI.getListElement(object, i);
final int offset = (int) (listColumnVector.offsets[batchIndex] + i);
assignConvertRowColumn(listColumnVector.child, offset, targetElementTypeInfo, sourceElementOI, null, element);
}
}
break;
case MAP:
{
final MapColumnVector mapColumnVector = (MapColumnVector) columnVector;
final MapObjectInspector mapObjectInspector = (MapObjectInspector) sourceObjectInspector;
final MapTypeInfo mapTypeInfo = (MapTypeInfo) targetTypeInfo;
final Map<?, ?> map = mapObjectInspector.getMap(object);
for (Map.Entry<?, ?> entry : map.entrySet()) {
assignConvertRowColumn(mapColumnVector.keys, batchIndex, mapTypeInfo.getMapKeyTypeInfo(), mapObjectInspector.getMapKeyObjectInspector(), null, entry.getKey());
assignConvertRowColumn(mapColumnVector.values, batchIndex, mapTypeInfo.getMapValueTypeInfo(), mapObjectInspector.getMapValueObjectInspector(), null, entry.getValue());
}
}
break;
case STRUCT:
{
final StructColumnVector structColumnVector = (StructColumnVector) columnVector;
final StructObjectInspector sourceStructOI = (StructObjectInspector) sourceObjectInspector;
final List<? extends StructField> sourceFields = sourceStructOI.getAllStructFieldRefs();
final StructTypeInfo targetStructTypeInfo = (StructTypeInfo) targetTypeInfo;
final List<TypeInfo> targetTypeInfos = targetStructTypeInfo.getAllStructFieldTypeInfos();
final int size = targetTypeInfos.size();
for (int i = 0; i < size; i++) {
if (i < sourceFields.size()) {
final StructField sourceStructField = sourceFields.get(i);
final ObjectInspector sourceFieldOI = sourceStructField.getFieldObjectInspector();
final Object sourceData = sourceStructOI.getStructFieldData(object, sourceStructField);
assignConvertRowColumn(structColumnVector.fields[i], batchIndex, targetTypeInfos.get(i), sourceFieldOI, null, sourceData);
} else {
final ColumnVector fieldColumnVector = structColumnVector.fields[i];
VectorizedBatchUtil.setNullColIsNullValue(fieldColumnVector, batchIndex);
}
}
}
break;
case UNION:
{
final UnionColumnVector unionColumnVector = (UnionColumnVector) columnVector;
final UnionObjectInspector unionObjectInspector = (UnionObjectInspector) sourceObjectInspector;
final UnionTypeInfo unionTypeInfo = (UnionTypeInfo) targetTypeInfo;
final int tag = unionObjectInspector.getTag(object);
assignConvertRowColumn(unionColumnVector.fields[tag], batchIndex, unionTypeInfo.getAllUnionObjectTypeInfos().get(tag), unionObjectInspector.getObjectInspectors().get(tag), null, unionObjectInspector.getField(tag));
}
break;
default:
throw new RuntimeException("Category " + targetCategory.name() + " not supported");
}
} catch (NumberFormatException e) {
// Some of the conversion methods throw this exception on numeric parsing errors.
VectorizedBatchUtil.setNullColIsNullValue(columnVector, batchIndex);
return;
}
// We always set the null flag to false when there is a value.
columnVector.isNull[batchIndex] = false;
}
use of org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo in project hive by apache.
the class VectorAssignRow method assignNullRowColumn.
private void assignNullRowColumn(ColumnVector columnVector, int batchIndex, TypeInfo targetTypeInfo) {
VectorizedBatchUtil.setNullColIsNullValue(columnVector, batchIndex);
if (targetTypeInfo.getCategory() == Category.STRUCT) {
final StructColumnVector structColumnVector = (StructColumnVector) columnVector;
final StructTypeInfo targetStructTypeInfo = (StructTypeInfo) targetTypeInfo;
final List<TypeInfo> targetFieldTypeInfos = targetStructTypeInfo.getAllStructFieldTypeInfos();
final int size = targetFieldTypeInfos.size();
for (int i = 0; i < size; i++) {
assignRowColumn(structColumnVector.fields[i], batchIndex, targetFieldTypeInfos.get(i), null);
}
}
}
use of org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo in project hive by apache.
the class SampleHBaseKeyFactory2 method setupFilter.
private HBaseScanRange setupFilter(String keyColName, List<IndexSearchCondition> conditions) throws IOException {
Map<String, List<IndexSearchCondition>> fieldConds = new HashMap<String, List<IndexSearchCondition>>();
for (IndexSearchCondition condition : conditions) {
assert keyColName.equals(condition.getColumnDesc().getColumn());
String fieldName = condition.getFields()[0];
List<IndexSearchCondition> fieldCond = fieldConds.get(fieldName);
if (fieldCond == null) {
fieldConds.put(fieldName, fieldCond = new ArrayList<IndexSearchCondition>());
}
fieldCond.add(condition);
}
HBaseScanRange range = new HBaseScanRange();
ByteArrayOutputStream startRow = new ByteArrayOutputStream();
ByteArrayOutputStream stopRow = new ByteArrayOutputStream();
StructTypeInfo type = (StructTypeInfo) keyMapping.columnType;
for (String name : type.getAllStructFieldNames()) {
List<IndexSearchCondition> fieldCond = fieldConds.get(name);
if (fieldCond == null || fieldCond.size() > 2) {
continue;
}
byte[] startElement = null;
byte[] stopElement = null;
for (IndexSearchCondition condition : fieldCond) {
if (condition.getConstantDesc().getValue() == null) {
continue;
}
String comparisonOp = condition.getComparisonOp();
String constantVal = String.valueOf(condition.getConstantDesc().getValue());
if (comparisonOp.endsWith("UDFOPEqual")) {
startElement = toBinary(constantVal, FIXED_LENGTH, false, false);
stopElement = toBinary(constantVal, FIXED_LENGTH, true, true);
} else if (comparisonOp.endsWith("UDFOPEqualOrGreaterThan")) {
startElement = toBinary(constantVal, FIXED_LENGTH, false, false);
} else if (comparisonOp.endsWith("UDFOPGreaterThan")) {
startElement = toBinary(constantVal, FIXED_LENGTH, false, true);
} else if (comparisonOp.endsWith("UDFOPEqualOrLessThan")) {
stopElement = toBinary(constantVal, FIXED_LENGTH, true, false);
} else if (comparisonOp.endsWith("UDFOPLessThan")) {
stopElement = toBinary(constantVal, FIXED_LENGTH, true, true);
} else {
throw new IOException(comparisonOp + " is not a supported comparison operator");
}
}
if (startRow != null) {
if (startElement != null) {
startRow.write(startElement);
} else {
if (startRow.size() > 0) {
range.setStartRow(startRow.toByteArray());
}
startRow = null;
}
}
if (stopRow != null) {
if (stopElement != null) {
stopRow.write(stopElement);
} else {
if (stopRow.size() > 0) {
range.setStopRow(stopRow.toByteArray());
}
stopRow = null;
}
}
if (startElement == null && stopElement == null) {
break;
}
}
if (startRow != null && startRow.size() > 0) {
range.setStartRow(startRow.toByteArray());
}
if (stopRow != null && stopRow.size() > 0) {
range.setStopRow(stopRow.toByteArray());
}
return range;
}
use of org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo in project hive by apache.
the class HCatRecordObjectInspectorFactory method getStandardObjectInspectorFromTypeInfo.
public static ObjectInspector getStandardObjectInspectorFromTypeInfo(TypeInfo typeInfo) {
ObjectInspector oi = cachedObjectInspectors.getIfPresent(typeInfo);
if (oi == null) {
LOG.debug("Got asked for OI for {}, [{}]", typeInfo.getCategory(), typeInfo.getTypeName());
switch(typeInfo.getCategory()) {
case PRIMITIVE:
oi = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector((PrimitiveTypeInfo) typeInfo);
break;
case STRUCT:
StructTypeInfo structTypeInfo = (StructTypeInfo) typeInfo;
List<String> fieldNames = structTypeInfo.getAllStructFieldNames();
List<TypeInfo> fieldTypeInfos = structTypeInfo.getAllStructFieldTypeInfos();
List<ObjectInspector> fieldObjectInspectors = new ArrayList<ObjectInspector>(fieldTypeInfos.size());
for (int i = 0; i < fieldTypeInfos.size(); i++) {
fieldObjectInspectors.add(getStandardObjectInspectorFromTypeInfo(fieldTypeInfos.get(i)));
}
oi = ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldObjectInspectors);
break;
case LIST:
ObjectInspector elementObjectInspector = getStandardObjectInspectorFromTypeInfo(((ListTypeInfo) typeInfo).getListElementTypeInfo());
oi = ObjectInspectorFactory.getStandardListObjectInspector(elementObjectInspector);
break;
case MAP:
ObjectInspector keyObjectInspector = getStandardObjectInspectorFromTypeInfo(((MapTypeInfo) typeInfo).getMapKeyTypeInfo());
ObjectInspector valueObjectInspector = getStandardObjectInspectorFromTypeInfo(((MapTypeInfo) typeInfo).getMapValueTypeInfo());
oi = ObjectInspectorFactory.getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
break;
default:
oi = null;
}
cachedObjectInspectors.asMap().put(typeInfo, oi);
}
return oi;
}
use of org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo in project hive by apache.
the class DDLPlanUtils method formatType.
/**
* Struct fields are identifiers, need to be put between ``.
*/
private String formatType(TypeInfo typeInfo) throws HiveException {
switch(typeInfo.getCategory()) {
case PRIMITIVE:
return typeInfo.getTypeName();
case STRUCT:
StringBuilder structFormattedType = new StringBuilder();
StructTypeInfo structTypeInfo = (StructTypeInfo) typeInfo;
for (int i = 0; i < structTypeInfo.getAllStructFieldNames().size(); i++) {
if (structFormattedType.length() != 0) {
structFormattedType.append(", ");
}
String structElementName = structTypeInfo.getAllStructFieldNames().get(i);
String structElementType = formatType(structTypeInfo.getAllStructFieldTypeInfos().get(i));
structFormattedType.append("`" + structElementName + "`:" + structElementType);
}
return "struct<" + structFormattedType.toString() + ">";
case LIST:
ListTypeInfo listTypeInfo = (ListTypeInfo) typeInfo;
String elementType = formatType(listTypeInfo.getListElementTypeInfo());
return "array<" + elementType + ">";
case MAP:
MapTypeInfo mapTypeInfo = (MapTypeInfo) typeInfo;
String keyTypeInfo = mapTypeInfo.getMapKeyTypeInfo().getTypeName();
String valueTypeInfo = formatType(mapTypeInfo.getMapValueTypeInfo());
return "map<" + keyTypeInfo + "," + valueTypeInfo + ">";
case UNION:
StringBuilder unionFormattedType = new StringBuilder();
UnionTypeInfo unionTypeInfo = (UnionTypeInfo) typeInfo;
for (TypeInfo unionElementTypeInfo : unionTypeInfo.getAllUnionObjectTypeInfos()) {
if (unionFormattedType.length() != 0) {
unionFormattedType.append(", ");
}
String unionElementType = formatType(unionElementTypeInfo);
unionFormattedType.append(unionElementType);
}
return "uniontype<" + unionFormattedType.toString() + ">";
default:
throw new RuntimeException("Unknown type: " + typeInfo.getCategory());
}
}
Aggregations