use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.
the class ColumnInfo method setTypeInfo.
public static void setTypeInfo(Builder builder, TypeSignature type) {
builder.setColumnType(getType(type));
ImmutableList.Builder<Integer> parameterTypes = ImmutableList.builder();
for (TypeSignatureParameter parameter : type.getParameters()) {
parameterTypes.add(getType(parameter));
}
builder.setColumnParameterTypes(parameterTypes.build());
switch(type.getBase()) {
case "boolean":
builder.setColumnDisplaySize(5);
break;
case "bigint":
builder.setSigned(true);
builder.setPrecision(19);
builder.setScale(0);
builder.setColumnDisplaySize(20);
break;
case "integer":
builder.setSigned(true);
builder.setPrecision(10);
builder.setScale(0);
builder.setColumnDisplaySize(11);
break;
case "smallint":
builder.setSigned(true);
builder.setPrecision(5);
builder.setScale(0);
builder.setColumnDisplaySize(6);
break;
case "tinyint":
builder.setSigned(true);
builder.setPrecision(3);
builder.setScale(0);
builder.setColumnDisplaySize(4);
break;
case "real":
builder.setSigned(true);
builder.setPrecision(9);
builder.setScale(0);
builder.setColumnDisplaySize(16);
break;
case "double":
builder.setSigned(true);
builder.setPrecision(17);
builder.setScale(0);
builder.setColumnDisplaySize(24);
break;
case "char":
builder.setSigned(false);
builder.setPrecision(type.getParameters().get(0).getLongLiteral().intValue());
builder.setColumnDisplaySize(type.getParameters().get(0).getLongLiteral().intValue());
builder.setScale(0);
break;
case "varchar":
builder.setSigned(false);
builder.setPrecision(Math.min(type.getParameters().get(0).getLongLiteral().intValue(), VARCHAR_MAX));
builder.setColumnDisplaySize(Math.min(type.getParameters().get(0).getLongLiteral().intValue(), VARCHAR_MAX));
builder.setScale(0);
break;
case "varbinary":
builder.setSigned(true);
builder.setPrecision(VARBINARY_MAX);
builder.setScale(0);
builder.setColumnDisplaySize(VARBINARY_MAX);
break;
case "time":
builder.setSigned(true);
builder.setPrecision(3);
builder.setScale(0);
builder.setColumnDisplaySize(TIME_MAX);
break;
case "time with time zone":
builder.setSigned(true);
builder.setPrecision(3);
builder.setScale(0);
builder.setColumnDisplaySize(TIME_WITH_TIME_ZONE_MAX);
break;
case "timestamp":
builder.setSigned(true);
builder.setPrecision(3);
builder.setScale(0);
builder.setColumnDisplaySize(TIMESTAMP_MAX);
break;
case "timestamp with time zone":
builder.setSigned(true);
builder.setPrecision(3);
builder.setScale(0);
builder.setColumnDisplaySize(TIMESTAMP_WITH_TIME_ZONE_MAX);
break;
case "date":
builder.setSigned(true);
builder.setScale(0);
builder.setColumnDisplaySize(DATE_MAX);
break;
case "interval year to month":
builder.setColumnDisplaySize(TIMESTAMP_MAX);
break;
case "interval day to second":
builder.setColumnDisplaySize(TIMESTAMP_MAX);
break;
case "decimal":
builder.setSigned(true);
// dot and sign
builder.setColumnDisplaySize(type.getParameters().get(0).getLongLiteral().intValue() + 2);
builder.setPrecision(type.getParameters().get(0).getLongLiteral().intValue());
builder.setScale(type.getParameters().get(1).getLongLiteral().intValue());
break;
}
}
use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.
the class TypeConverter method toHiveTypeInfo.
private static TypeInfo toHiveTypeInfo(Type type) {
if (BOOLEAN.equals(type)) {
return HIVE_BOOLEAN.getTypeInfo();
}
if (BIGINT.equals(type)) {
return HIVE_LONG.getTypeInfo();
}
if (INTEGER.equals(type)) {
return HIVE_INT.getTypeInfo();
}
if (SMALLINT.equals(type)) {
return HIVE_SHORT.getTypeInfo();
}
if (TINYINT.equals(type)) {
return HIVE_BYTE.getTypeInfo();
}
if (REAL.equals(type)) {
return HIVE_FLOAT.getTypeInfo();
}
if (DOUBLE.equals(type)) {
return HIVE_DOUBLE.getTypeInfo();
}
if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
if (varcharType.isUnbounded()) {
return HIVE_STRING.getTypeInfo();
}
if (varcharType.getLengthSafe() <= HiveVarchar.MAX_VARCHAR_LENGTH) {
return getVarcharTypeInfo(varcharType.getLengthSafe());
}
throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported VARCHAR types: VARCHAR(<=%d), VARCHAR.", type, HiveVarchar.MAX_VARCHAR_LENGTH));
}
if (type instanceof CharType) {
CharType charType = (CharType) type;
int charLength = charType.getLength();
if (charLength <= HiveChar.MAX_CHAR_LENGTH) {
return getCharTypeInfo(charLength);
}
throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported CHAR types: CHAR(<=%d).", type, HiveChar.MAX_CHAR_LENGTH));
}
if (VARBINARY.equals(type)) {
return HIVE_BINARY.getTypeInfo();
}
if (DATE.equals(type)) {
return HIVE_DATE.getTypeInfo();
}
if (TIMESTAMP.equals(type)) {
return HIVE_TIMESTAMP.getTypeInfo();
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale());
}
if (isArrayType(type)) {
TypeInfo elementType = toHiveTypeInfo(type.getTypeParameters().get(0));
return getListTypeInfo(elementType);
}
if (isMapType(type)) {
TypeInfo keyType = toHiveTypeInfo(type.getTypeParameters().get(0));
TypeInfo valueType = toHiveTypeInfo(type.getTypeParameters().get(1));
return getMapTypeInfo(keyType, valueType);
}
if (isRowType(type)) {
ImmutableList.Builder<String> fieldNames = ImmutableList.builder();
for (TypeSignatureParameter parameter : type.getTypeSignature().getParameters()) {
if (!parameter.isNamedTypeSignature()) {
throw new IllegalArgumentException(format("Expected all parameters to be named type, but got %s", parameter));
}
NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
if (!namedTypeSignature.getName().isPresent()) {
throw new PrestoException(NOT_SUPPORTED, format("Anonymous row type is not supported in Hive. Please give each field a name: %s", type));
}
fieldNames.add(namedTypeSignature.getName().get());
}
return getStructTypeInfo(fieldNames.build(), type.getTypeParameters().stream().map(TypeConverter::toHiveTypeInfo).collect(toList()));
}
throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", type));
}
use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.
the class DeltaTypeUtils method convertDeltaDataTypePrestoDataType.
/**
* Convert given Delta data type to Presto data type signature.
*
* @param tableName Used in error messages when an unsupported data type is encountered.
* @param columnName Used in error messages when an unsupported data type is encountered.
* @param deltaType Data type to convert
* @return
*/
public static TypeSignature convertDeltaDataTypePrestoDataType(SchemaTableName tableName, String columnName, DataType deltaType) {
checkArgument(deltaType != null);
if (deltaType instanceof StructType) {
StructType deltaStructType = (StructType) deltaType;
ImmutableList.Builder<TypeSignatureParameter> typeSignatureBuilder = ImmutableList.builder();
Arrays.stream(deltaStructType.getFields()).forEach(field -> {
String rowFieldName = field.getName().toLowerCase(Locale.US);
TypeSignature rowFieldType = convertDeltaDataTypePrestoDataType(tableName, columnName + "." + field.getName(), field.getDataType());
typeSignatureBuilder.add(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName(rowFieldName, false)), rowFieldType)));
});
return new TypeSignature(StandardTypes.ROW, typeSignatureBuilder.build());
} else if (deltaType instanceof ArrayType) {
ArrayType deltaArrayType = (ArrayType) deltaType;
TypeSignature elementType = convertDeltaDataTypePrestoDataType(tableName, columnName, deltaArrayType.getElementType());
return new TypeSignature(ARRAY, ImmutableList.of(TypeSignatureParameter.of(elementType)));
} else if (deltaType instanceof MapType) {
MapType deltaMapType = (MapType) deltaType;
TypeSignature keyType = convertDeltaDataTypePrestoDataType(tableName, columnName, deltaMapType.getKeyType());
TypeSignature valueType = convertDeltaDataTypePrestoDataType(tableName, columnName, deltaMapType.getValueType());
return new TypeSignature(MAP, ImmutableList.of(TypeSignatureParameter.of(keyType), TypeSignatureParameter.of(valueType)));
}
return convertDeltaPrimitiveTypeToPrestoPrimitiveType(tableName, columnName, deltaType).getTypeSignature();
}
use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.
the class HiveUtil method translateHiveUnsupportedTypeSignatureForTemporaryTable.
private static TypeSignature translateHiveUnsupportedTypeSignatureForTemporaryTable(TypeSignature typeSignature) {
List<TypeSignatureParameter> parameters = typeSignature.getParameters();
if (typeSignature.getBase().equals("unknown")) {
return new TypeSignature(StandardTypes.BOOLEAN);
}
if (typeSignature.getBase().equals(StandardTypes.ROW)) {
ImmutableList.Builder<TypeSignatureParameter> updatedParameters = ImmutableList.builder();
for (int i = 0; i < parameters.size(); i++) {
TypeSignatureParameter typeSignatureParameter = parameters.get(i);
checkArgument(typeSignatureParameter.isNamedTypeSignature(), "unexpected row type signature parameter: %s", typeSignatureParameter);
NamedTypeSignature namedTypeSignature = typeSignatureParameter.getNamedTypeSignature();
updatedParameters.add(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(namedTypeSignature.getFieldName().orElse(new RowFieldName("_field_" + i, false))), translateHiveUnsupportedTypeSignatureForTemporaryTable(namedTypeSignature.getTypeSignature()))));
}
return new TypeSignature(StandardTypes.ROW, updatedParameters.build());
}
if (!parameters.isEmpty()) {
ImmutableList.Builder<TypeSignatureParameter> updatedParameters = ImmutableList.builder();
for (TypeSignatureParameter parameter : parameters) {
switch(parameter.getKind()) {
case LONG:
case VARIABLE:
updatedParameters.add(parameter);
continue;
case TYPE:
updatedParameters.add(TypeSignatureParameter.of(translateHiveUnsupportedTypeSignatureForTemporaryTable(parameter.getTypeSignature())));
break;
case NAMED_TYPE:
NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
updatedParameters.add(TypeSignatureParameter.of(new NamedTypeSignature(namedTypeSignature.getFieldName(), translateHiveUnsupportedTypeSignatureForTemporaryTable(namedTypeSignature.getTypeSignature()))));
break;
default:
throw new IllegalArgumentException("Unexpected parameter type: " + parameter.getKind());
}
}
return new TypeSignature(typeSignature.getBase(), updatedParameters.build());
}
return typeSignature;
}
use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.
the class OrcType method toOrcType.
private static List<OrcType> toOrcType(int nextFieldTypeIndex, Type type) {
if (BOOLEAN.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.BOOLEAN));
}
if (TINYINT.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.BYTE));
}
if (SMALLINT.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.SHORT));
}
if (INTEGER.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.INT));
}
if (BIGINT.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.LONG));
}
if (DOUBLE.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.DOUBLE));
}
if (REAL.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.FLOAT));
}
if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
if (varcharType.isUnbounded()) {
return ImmutableList.of(new OrcType(OrcTypeKind.STRING));
}
return ImmutableList.of(new OrcType(OrcTypeKind.VARCHAR, varcharType.getLengthSafe()));
}
if (type instanceof CharType) {
return ImmutableList.of(new OrcType(OrcTypeKind.CHAR, ((CharType) type).getLength()));
}
if (VARBINARY.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.BINARY));
}
if (DATE.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.DATE));
}
if (TIMESTAMP.equals(type)) {
return ImmutableList.of(new OrcType(OrcTypeKind.TIMESTAMP));
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return ImmutableList.of(new OrcType(OrcTypeKind.DECIMAL, decimalType.getPrecision(), decimalType.getScale()));
}
if (type.getTypeSignature().getBase().equals(ARRAY)) {
return createOrcArrayType(nextFieldTypeIndex, type.getTypeParameters().get(0));
}
if (type.getTypeSignature().getBase().equals(MAP)) {
return createOrcMapType(nextFieldTypeIndex, type.getTypeParameters().get(0), type.getTypeParameters().get(1));
}
if (type.getTypeSignature().getBase().equals(ROW)) {
List<String> fieldNames = new ArrayList<>();
for (int i = 0; i < type.getTypeSignature().getParameters().size(); i++) {
TypeSignatureParameter parameter = type.getTypeSignature().getParameters().get(i);
fieldNames.add(parameter.getNamedTypeSignature().getName().orElse("field" + i));
}
List<Type> fieldTypes = type.getTypeParameters();
return createOrcRowType(nextFieldTypeIndex, fieldNames, fieldTypes);
}
throw new NotSupportedException(format("Unsupported Hive type: %s", type));
}
Aggregations