private static boolean isWritableType(TypeInfo typeInfo) { switch (typeInfo.getCategory()) { case PRIMITIVE: PrimitiveCategory primitiveCategory = ((PrimitiveTypeInfo) typeInfo).getPrimitiveCategory(); return isWritablePrimitiveType(primitiveCategory); case MAP: MapTypeInfo mapTypeInfo = (MapTypeInfo) typeInfo; return isWritableType(mapTypeInfo.getMapKeyTypeInfo()) && isWritableType(mapTypeInfo.getMapValueTypeInfo()); case LIST: ListTypeInfo listTypeInfo = (ListTypeInfo) typeInfo; return isWritableType(listTypeInfo.getListElementTypeInfo()); case STRUCT: StructTypeInfo structTypeInfo = (StructTypeInfo) typeInfo; return structTypeInfo.getAllStructFieldTypeInfos().stream().allMatch(HiveWriteUtils::isWritableType); } return false; }
public static Type getPrimitiveHType(PrimitiveTypeInfo basePrimitiveTypeInfo) { for(Type t : values()) { if(t.getPrimitiveCategory() == basePrimitiveTypeInfo.getPrimitiveCategory()) { return t; } } throw new TypeNotPresentException(basePrimitiveTypeInfo.getTypeName(), null); } //aid in testing
public PrimitiveCategory getPrimitiveCategory() { return getPrimitiveTypeEntry().primitiveCategory; }
private static void checkArgumentTypes(TypeInfo[] parameters) throws UDFArgumentTypeException { if (parameters.length != 2) { throw new UDFArgumentTypeException(parameters.length - 1, "Exactly two arguments are expected."); } if (parameters[0].getCategory() != ObjectInspector.Category.PRIMITIVE) { throw new UDFArgumentTypeException(0, "Only primitive type arguments are accepted but " + parameters[0].getTypeName() + " is passed."); } if (parameters[1].getCategory() != ObjectInspector.Category.PRIMITIVE) { throw new UDFArgumentTypeException(1, "Only primitive type arguments are accepted but " + parameters[1].getTypeName() + " is passed."); } if (!acceptedPrimitiveCategory(((PrimitiveTypeInfo) parameters[0]).getPrimitiveCategory())) { throw new UDFArgumentTypeException(0, "Only numeric type arguments are accepted but " + parameters[0].getTypeName() + " is passed."); } if (!acceptedPrimitiveCategory(((PrimitiveTypeInfo) parameters[1]).getPrimitiveCategory())) { throw new UDFArgumentTypeException(1, "Only numeric type arguments are accepted but " + parameters[1].getTypeName() + " is passed."); } }
public static TypeDescription convertTypeInfo(TypeInfo info) { switch (info.getCategory()) { case PRIMITIVE: { PrimitiveTypeInfo pinfo = (PrimitiveTypeInfo) info; switch (pinfo.getPrimitiveCategory()) { case BOOLEAN: return TypeDescription.createBoolean(); " category " + pinfo.getPrimitiveCategory()); ListTypeInfo linfo = (ListTypeInfo) info; return TypeDescription.createList (convertTypeInfo(linfo.getListElementTypeInfo())); (convertTypeInfo(minfo.getMapKeyTypeInfo()), convertTypeInfo(minfo.getMapValueTypeInfo())); StructTypeInfo sinfo = (StructTypeInfo) info; TypeDescription result = TypeDescription.createStruct(); for(String fieldName: sinfo.getAllStructFieldNames()) { result.addField(fieldName, convertTypeInfo(sinfo.getStructFieldTypeInfo(fieldName))); info.getCategory());
private static Field toField(String name, TypeInfo typeInfo) { switch (typeInfo.getCategory()) { case PRIMITIVE: final PrimitiveTypeInfo primitiveTypeInfo = (PrimitiveTypeInfo) typeInfo; switch (primitiveTypeInfo.getPrimitiveCategory()) { case BOOLEAN: return Field.nullable(name, MinorType.BIT.getType()); final TypeInfo elementTypeInfo = listTypeInfo.getListElementTypeInfo(); return new Field(name, FieldType.nullable(MinorType.LIST.getType()), Lists.newArrayList(toField(DEFAULT_ARROW_FIELD_NAME, elementTypeInfo))); case STRUCT: final StructTypeInfo structTypeInfo = (StructTypeInfo) typeInfo; final List<TypeInfo> fieldTypeInfos = structTypeInfo.getAllStructFieldTypeInfos(); final List<String> fieldNames = structTypeInfo.getAllStructFieldNames(); final List<Field> structFields = Lists.newArrayList(); final int structSize = fieldNames.size(); case MAP: final MapTypeInfo mapTypeInfo = (MapTypeInfo) typeInfo; final TypeInfo keyTypeInfo = mapTypeInfo.getMapKeyTypeInfo(); final TypeInfo valueTypeInfo = mapTypeInfo.getMapValueTypeInfo(); final StructTypeInfo mapStructTypeInfo = new StructTypeInfo(); mapStructTypeInfo.setAllStructFieldNames(Lists.newArrayList("keys", "values")); mapStructTypeInfo.setAllStructFieldTypeInfos( Lists.newArrayList(keyTypeInfo, valueTypeInfo)); final ListTypeInfo mapListStructTypeInfo = new ListTypeInfo(); mapListStructTypeInfo.setListElementTypeInfo(mapStructTypeInfo);
for (ExprNodeDesc d : desc.getChildren()) { TypeInfo typeInfo = d.getTypeInfo(); if (typeInfo.getCategory() != Category.STRUCT) { return false; .getAllStructFieldTypeInfos(); ArrayList<String> fieldNames = structTypeInfo.getAllStructFieldNames(); final int fieldCount = fieldTypeInfos.size(); for (int f = 0; f < fieldCount; f++) { TypeInfo fieldTypeInfo = fieldTypeInfos.get(f); Category category = fieldTypeInfo.getCategory(); if (category != Category.PRIMITIVE) { setExpressionIssue(expressionTitle, "Cannot vectorize struct field " + fieldNames.get(f) + " of type " + fieldTypeInfo.getTypeName()); return false; InConstantType inConstantType = VectorizationContext .getInConstantTypeFromPrimitiveCategory(fieldPrimitiveTypeInfo .getPrimitiveCategory());
public static ColumnVector.Type getColumnVectorTypeFromTypeInfo(TypeInfo typeInfo, DataTypePhysicalVariation dataTypePhysicalVariation) throws HiveException { switch (typeInfo.getCategory()) { case STRUCT: return Type.STRUCT; case PRIMITIVE: { PrimitiveTypeInfo primitiveTypeInfo = (PrimitiveTypeInfo) typeInfo; PrimitiveCategory primitiveCategory = primitiveTypeInfo.getPrimitiveCategory(); throw new HiveException("Unexpected primitive type category " + primitiveCategory); throw new HiveException("Unexpected type category " + typeInfo.getCategory());
private String castConstantToString(Object scalar, TypeInfo type) throws HiveException { if (null == scalar) { return null; } PrimitiveTypeInfo ptinfo = (PrimitiveTypeInfo) type; String typename = type.getTypeName(); switch (ptinfo.getPrimitiveCategory()) { case FLOAT: case DOUBLE: case BYTE: case SHORT: case INT: case LONG: return ((Number) scalar).toString(); case DECIMAL: HiveDecimal decimalVal = (HiveDecimal) scalar; return decimalVal.toString(); default: throw new HiveException("Unsupported type "+typename+" for cast to String"); } }
public boolean isCategory(PrimitiveCategory category) { return columnType.getCategory() == ObjectInspector.Category.PRIMITIVE && ((PrimitiveTypeInfo)columnType).getPrimitiveCategory() == category; }
public void init(List<String> typeNames) throws HiveException { final int size = typeNames.size(); categories = new Category[size]; primitiveCategories = new PrimitiveCategory[size]; outputColumnNums = new int[size]; TypeInfo typeInfo; for (int i = 0; i < size; i++) { typeInfo = TypeInfoUtils.getTypeInfoFromTypeString(typeNames.get(i)); categories[i] = typeInfo.getCategory(); if (categories[i] == Category.PRIMITIVE) { primitiveCategories[i] = ((PrimitiveTypeInfo) typeInfo).getPrimitiveCategory(); } outputColumnNums[i] = i; } }
boolean isList = (object.getTypeInfo().getCategory() == ObjectInspector.Category.LIST); if (isList) { objectTypeInfo = ((ListTypeInfo) objectTypeInfo).getListElementTypeInfo(); if (objectTypeInfo.getCategory() != Category.STRUCT) { throw new SemanticException(ErrorMsg.INVALID_DOT.getMsg(expr)); TypeInfo t = ((StructTypeInfo) objectTypeInfo).getStructFieldTypeInfo(fieldNameString); if (isList) { t = TypeInfoFactory.getListTypeInfo(t); if (myt.getCategory() == Category.LIST) { TypeInfo t = ((ListTypeInfo) myt).getListElementTypeInfo(); if (PrimitiveObjectInspectorUtils.intTypeEntry.equals(colTypeInfo.getPrimitiveTypeEntry()) && (constVal instanceof Number || constVal instanceof String)) { children.set(constIdx, new ExprNodeConstantDesc(new Integer(constVal.toString()))); } else if (PrimitiveObjectInspectorUtils.longTypeEntry.equals(colTypeInfo.getPrimitiveTypeEntry()) && (constVal instanceof Number || constVal instanceof String)) { children.set(constIdx, new ExprNodeConstantDesc(new Long(constVal.toString()))); }else if (PrimitiveObjectInspectorUtils.doubleTypeEntry.equals(colTypeInfo.getPrimitiveTypeEntry()) && (constVal instanceof Number || constVal instanceof String)) { children.set(constIdx, new ExprNodeConstantDesc(new Double(constVal.toString()))); } else if (PrimitiveObjectInspectorUtils.floatTypeEntry.equals(colTypeInfo.getPrimitiveTypeEntry()) && (constVal instanceof Number || constVal instanceof String)) { children.set(constIdx, new ExprNodeConstantDesc(new Float(constVal.toString()))); } else if (PrimitiveObjectInspectorUtils.byteTypeEntry.equals(colTypeInfo.getPrimitiveTypeEntry()) && (constVal instanceof Number || constVal instanceof String)) { children.set(constIdx, new ExprNodeConstantDesc(new Byte(constVal.toString()))); } else if (PrimitiveObjectInspectorUtils.shortTypeEntry.equals(colTypeInfo.getPrimitiveTypeEntry()) && (constVal instanceof Number || constVal instanceof String)) { children.set(constIdx, new ExprNodeConstantDesc(new Short(constVal.toString())));
/** * Returns whether it is possible to implicitly convert an object of Class * from to Class to. */ public static boolean implicitConvertible(TypeInfo from, TypeInfo to) { if (from.equals(to)) { return true; } // Reimplemented to use PrimitiveCategory rather than TypeInfo, because // 2 TypeInfos from the same qualified type (varchar, decimal) should still be // seen as equivalent. if (from.getCategory() == Category.PRIMITIVE && to.getCategory() == Category.PRIMITIVE) { return implicitConvertible( ((PrimitiveTypeInfo) from).getPrimitiveCategory(), ((PrimitiveTypeInfo) to).getPrimitiveCategory()); } return false; } }
static String getScratchName(TypeInfo typeInfo) throws HiveException { // For now, leave DECIMAL precision/scale in the name so DecimalColumnVector scratch columns // don't need their precision/scale adjusted... if (typeInfo.getCategory() == Category.PRIMITIVE && ((PrimitiveTypeInfo) typeInfo).getPrimitiveCategory() == PrimitiveCategory.DECIMAL) { return typeInfo.getTypeName(); } // And, for Complex Types, also leave the children types in place... if (typeInfo.getCategory() != Category.PRIMITIVE) { return typeInfo.getTypeName(); } Type columnVectorType = VectorizationContext.getColumnVectorTypeFromTypeInfo(typeInfo); return columnVectorType.name().toLowerCase(); }
private static void validateAvroType(TypeInfo type, String columnName) { if (type.getCategory() == Category.MAP) { TypeInfo keyType = mapTypeInfo(type).getMapKeyTypeInfo(); if ((keyType.getCategory() != Category.PRIMITIVE) || (primitiveTypeInfo(keyType).getPrimitiveCategory() != PrimitiveCategory.STRING)) { throw new PrestoException(NOT_SUPPORTED, format("Column %s has a non-varchar map key, which is not supported by Avro", columnName)); } } else if (type.getCategory() == Category.PRIMITIVE) { PrimitiveCategory primitive = primitiveTypeInfo(type).getPrimitiveCategory(); if (primitive == PrimitiveCategory.BYTE) { throw new PrestoException(NOT_SUPPORTED, format("Column %s is tinyint, which is not supported by Avro. Use integer instead.", columnName)); } if (primitive == PrimitiveCategory.SHORT) { throw new PrestoException(NOT_SUPPORTED, format("Column %s is smallint, which is not supported by Avro. Use integer instead.", columnName)); } } }
private TypeDesc convertTypeString(String typeString) throws HiveException { TypeDesc typeDesc; TypeInfo typeInfo = TypeInfoUtils.getTypeInfoFromTypeString(typeString); Preconditions.checkState( typeInfo.getCategory() == ObjectInspector.Category.PRIMITIVE, "Unsupported non-primitive type " + typeString); switch (((PrimitiveTypeInfo) typeInfo).getPrimitiveCategory()) { case BOOLEAN: typeDesc = new TypeDesc(TypeDesc.Type.BOOLEAN); break; default: throw new HiveException("Unsupported type " + typeString);
private void initConvertSourceEntry(int logicalColumnIndex, TypeInfo convertSourceTypeInfo) { isConvert[logicalColumnIndex] = true; final Category convertSourceCategory = convertSourceTypeInfo.getCategory(); convertSourceOI[logicalColumnIndex] = TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo(convertSourceTypeInfo); if (convertSourceCategory == Category.PRIMITIVE) { // These need to be based on the target. final PrimitiveCategory targetPrimitiveCategory = ((PrimitiveTypeInfo) targetTypeInfos[logicalColumnIndex]).getPrimitiveCategory(); switch (targetPrimitiveCategory) { case DATE: convertTargetWritables[logicalColumnIndex] = new DateWritableV2(); break; case STRING: convertTargetWritables[logicalColumnIndex] = new Text(); break; default: // No additional data type specific setting. break; } } }
private Schema createAvroMap(TypeInfo typeInfo) { TypeInfo keyTypeInfo = ((MapTypeInfo) typeInfo).getMapKeyTypeInfo(); if (((PrimitiveTypeInfo) keyTypeInfo).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.STRING) { throw new UnsupportedOperationException("Key of Map can only be a String"); } TypeInfo valueTypeInfo = ((MapTypeInfo) typeInfo).getMapValueTypeInfo(); Schema valueSchema = createAvroSchema(valueTypeInfo); return Schema.createMap(valueSchema); }
currentScore = 0; List<TypeInfo> argumentsAccepted = TypeInfoUtils.getParameterTypeInfos(m, argumentsPassed.size()); Iterator<TypeInfo> argsPassedIter = argumentsPassed.iterator(); for (TypeInfo acceptedType : argumentsAccepted) { if (acceptedType.getCategory() == Category.PRIMITIVE && passedType.getCategory() == Category.PRIMITIVE) { PrimitiveGrouping acceptedPg = PrimitiveObjectInspectorUtils.getPrimitiveGrouping( ((PrimitiveTypeInfo) acceptedType).getPrimitiveCategory()); PrimitiveGrouping passedPg = PrimitiveObjectInspectorUtils.getPrimitiveGrouping( ((PrimitiveTypeInfo) passedType).getPrimitiveCategory()); if (acceptedPg == passedPg) {
/** * Default implementation for getting the approximate type info for the operator result. * Divide operator overrides this. * @return */ protected PrimitiveTypeInfo deriveResultApproxTypeInfo() { PrimitiveTypeInfo left = (PrimitiveTypeInfo) TypeInfoUtils.getTypeInfoFromObjectInspector(leftOI); PrimitiveTypeInfo right = (PrimitiveTypeInfo) TypeInfoUtils.getTypeInfoFromObjectInspector(rightOI); // string types get converted to double if (PrimitiveObjectInspectorUtils.getPrimitiveGrouping(left.getPrimitiveCategory()) == PrimitiveGrouping.STRING_GROUP) { left = TypeInfoFactory.doubleTypeInfo; } if (PrimitiveObjectInspectorUtils.getPrimitiveGrouping(right.getPrimitiveCategory()) == PrimitiveGrouping.STRING_GROUP) { right = TypeInfoFactory.doubleTypeInfo; } // Use type promotion PrimitiveCategory commonCat = FunctionRegistry.getPrimitiveCommonCategory(left, right); if (commonCat == PrimitiveCategory.DECIMAL) { // Hive 0.12 behavior where double * decimal -> decimal is gone. return TypeInfoFactory.doubleTypeInfo; } else if (commonCat == null) { return TypeInfoFactory.doubleTypeInfo; } else { return left.getPrimitiveCategory() == commonCat ? left : right; } }