use of com.facebook.presto.spi.type.TypeSignatureParameter in project presto by prestodb.
the class HiveType method getTypeSignature.
@Nonnull
private static TypeSignature getTypeSignature(TypeInfo typeInfo) {
switch(typeInfo.getCategory()) {
case PRIMITIVE:
Type primitiveType = getPrimitiveType((PrimitiveTypeInfo) typeInfo);
if (primitiveType == null) {
break;
}
return primitiveType.getTypeSignature();
case MAP:
MapTypeInfo mapTypeInfo = (MapTypeInfo) typeInfo;
TypeSignature keyType = getTypeSignature(mapTypeInfo.getMapKeyTypeInfo());
TypeSignature valueType = getTypeSignature(mapTypeInfo.getMapValueTypeInfo());
return new TypeSignature(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType), TypeSignatureParameter.of(valueType)));
case LIST:
ListTypeInfo listTypeInfo = (ListTypeInfo) typeInfo;
TypeSignature elementType = getTypeSignature(listTypeInfo.getListElementTypeInfo());
return new TypeSignature(StandardTypes.ARRAY, ImmutableList.of(TypeSignatureParameter.of(elementType)));
case STRUCT:
StructTypeInfo structTypeInfo = (StructTypeInfo) typeInfo;
List<TypeInfo> structFieldTypeInfos = structTypeInfo.getAllStructFieldTypeInfos();
List<String> structFieldNames = structTypeInfo.getAllStructFieldNames();
if (structFieldTypeInfos.size() != structFieldNames.size()) {
throw new PrestoException(HiveErrorCode.HIVE_INVALID_METADATA, format("Invalid Hive struct type: %s", typeInfo));
}
ImmutableList.Builder<TypeSignatureParameter> typeSignatureBuilder = ImmutableList.builder();
for (int i = 0; i < structFieldTypeInfos.size(); i++) {
TypeSignature typeSignature = getTypeSignature(structFieldTypeInfos.get(i));
// Lower case the struct field names.
// Otherwise, Presto will refuse to write to columns whose struct type has field names containing upper case characters.
// Users can't work around this by casting in their queries because Presto parser always lower case types.
// TODO: This is a hack. Presto engine should be able to handle identifiers in a case insensitive way where necessary.
String rowFieldName = structFieldNames.get(i).toLowerCase(Locale.US);
typeSignatureBuilder.add(TypeSignatureParameter.of(new NamedTypeSignature(rowFieldName, typeSignature)));
}
return new TypeSignature(StandardTypes.ROW, typeSignatureBuilder.build());
}
throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", typeInfo));
}
use of com.facebook.presto.spi.type.TypeSignatureParameter in project presto by prestodb.
the class QueryResults method fixValue.
/**
* Force values coming from Jackson to have the expected object type.
*/
private static Object fixValue(TypeSignature signature, Object value) {
if (value == null) {
return null;
}
if (signature.getBase().equals(ARRAY)) {
List<Object> fixedValue = new ArrayList<>();
for (Object object : List.class.cast(value)) {
fixedValue.add(fixValue(signature.getTypeParametersAsTypeSignatures().get(0), object));
}
return fixedValue;
}
if (signature.getBase().equals(MAP)) {
TypeSignature keySignature = signature.getTypeParametersAsTypeSignatures().get(0);
TypeSignature valueSignature = signature.getTypeParametersAsTypeSignatures().get(1);
Map<Object, Object> fixedValue = new HashMap<>();
for (Map.Entry<?, ?> entry : (Set<Map.Entry<?, ?>>) Map.class.cast(value).entrySet()) {
fixedValue.put(fixValue(keySignature, entry.getKey()), fixValue(valueSignature, entry.getValue()));
}
return fixedValue;
}
if (signature.getBase().equals(ROW)) {
Map<String, Object> fixedValue = new LinkedHashMap<>();
List<Object> listValue = List.class.cast(value);
checkArgument(listValue.size() == signature.getParameters().size(), "Mismatched data values and row type");
for (int i = 0; i < listValue.size(); i++) {
TypeSignatureParameter parameter = signature.getParameters().get(i);
checkArgument(parameter.getKind() == ParameterKind.NAMED_TYPE, "Unexpected parameter [%s] for row type", parameter);
NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
String key = namedTypeSignature.getName();
fixedValue.put(key, fixValue(namedTypeSignature.getTypeSignature(), listValue.get(i)));
}
return fixedValue;
}
switch(signature.getBase()) {
case BIGINT:
if (value instanceof String) {
return Long.parseLong((String) value);
}
return ((Number) value).longValue();
case INTEGER:
if (value instanceof String) {
return Integer.parseInt((String) value);
}
return ((Number) value).intValue();
case SMALLINT:
if (value instanceof String) {
return Short.parseShort((String) value);
}
return ((Number) value).shortValue();
case TINYINT:
if (value instanceof String) {
return Byte.parseByte((String) value);
}
return ((Number) value).byteValue();
case DOUBLE:
if (value instanceof String) {
return Double.parseDouble((String) value);
}
return ((Number) value).doubleValue();
case REAL:
if (value instanceof String) {
return Float.parseFloat((String) value);
}
return ((Number) value).floatValue();
case BOOLEAN:
if (value instanceof String) {
return Boolean.parseBoolean((String) value);
}
return Boolean.class.cast(value);
case VARCHAR:
case JSON:
case TIME:
case TIME_WITH_TIME_ZONE:
case TIMESTAMP:
case TIMESTAMP_WITH_TIME_ZONE:
case DATE:
case INTERVAL_YEAR_TO_MONTH:
case INTERVAL_DAY_TO_SECOND:
case DECIMAL:
case CHAR:
return String.class.cast(value);
default:
// as a plain text and everything else is base64 encoded binary
if (value instanceof String) {
return Base64.getDecoder().decode((String) value);
}
return value;
}
}
use of com.facebook.presto.spi.type.TypeSignatureParameter in project presto by prestodb.
the class MongoSession method guessFieldType.
private Optional<TypeSignature> guessFieldType(Object value) {
if (value == null) {
return Optional.empty();
}
TypeSignature typeSignature = null;
if (value instanceof String) {
typeSignature = createUnboundedVarcharType().getTypeSignature();
} else if (value instanceof Integer || value instanceof Long) {
typeSignature = BIGINT.getTypeSignature();
} else if (value instanceof Boolean) {
typeSignature = BOOLEAN.getTypeSignature();
} else if (value instanceof Float || value instanceof Double) {
typeSignature = DOUBLE.getTypeSignature();
} else if (value instanceof Date) {
typeSignature = TIMESTAMP.getTypeSignature();
} else if (value instanceof ObjectId) {
typeSignature = OBJECT_ID.getTypeSignature();
} else if (value instanceof List) {
List<Optional<TypeSignature>> subTypes = ((List<?>) value).stream().map(this::guessFieldType).collect(toList());
if (subTypes.isEmpty() || subTypes.stream().anyMatch(t -> !t.isPresent())) {
return Optional.empty();
}
Set<TypeSignature> signatures = subTypes.stream().map(t -> t.get()).collect(toSet());
if (signatures.size() == 1) {
typeSignature = new TypeSignature(StandardTypes.ARRAY, signatures.stream().map(s -> TypeSignatureParameter.of(s)).collect(Collectors.toList()));
} else {
// TODO: presto cli doesn't handle empty field name row type yet
typeSignature = new TypeSignature(StandardTypes.ROW, IntStream.range(0, subTypes.size()).mapToObj(idx -> TypeSignatureParameter.of(new NamedTypeSignature(String.format("%s%d", implicitPrefix, idx + 1), subTypes.get(idx).get()))).collect(toList()));
}
} else if (value instanceof Document) {
List<TypeSignatureParameter> parameters = new ArrayList<>();
for (String key : ((Document) value).keySet()) {
Optional<TypeSignature> fieldType = guessFieldType(((Document) value).get(key));
if (!fieldType.isPresent()) {
return Optional.empty();
}
parameters.add(TypeSignatureParameter.of(new NamedTypeSignature(key, fieldType.get())));
}
typeSignature = new TypeSignature(StandardTypes.ROW, parameters);
}
return Optional.ofNullable(typeSignature);
}
use of com.facebook.presto.spi.type.TypeSignatureParameter in project presto by prestodb.
the class OrcTester method rowType.
private static Type rowType(Type... fieldTypes) {
ImmutableList.Builder<TypeSignatureParameter> typeSignatureParameters = ImmutableList.builder();
for (int i = 0; i < fieldTypes.length; i++) {
String filedName = "field_" + i;
Type fieldType = fieldTypes[i];
typeSignatureParameters.add(TypeSignatureParameter.of(new NamedTypeSignature(filedName, fieldType.getTypeSignature())));
}
return TYPE_MANAGER.getParameterizedType(ROW, typeSignatureParameters.build());
}
use of com.facebook.presto.spi.type.TypeSignatureParameter in project presto by prestodb.
the class OrcStorageManager method getType.
private Type getType(List<OrcType> types, int index) {
OrcType type = types.get(index);
switch(type.getOrcTypeKind()) {
case BOOLEAN:
return BOOLEAN;
case LONG:
return BIGINT;
case DOUBLE:
return DOUBLE;
case STRING:
return createUnboundedVarcharType();
case BINARY:
return VARBINARY;
case DECIMAL:
return DecimalType.createDecimalType(type.getPrecision().get(), type.getScale().get());
case LIST:
TypeSignature elementType = getType(types, type.getFieldTypeIndex(0)).getTypeSignature();
return typeManager.getParameterizedType(StandardTypes.ARRAY, ImmutableList.of(TypeSignatureParameter.of(elementType)));
case MAP:
TypeSignature keyType = getType(types, type.getFieldTypeIndex(0)).getTypeSignature();
TypeSignature valueType = getType(types, type.getFieldTypeIndex(1)).getTypeSignature();
return typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType), TypeSignatureParameter.of(valueType)));
case STRUCT:
List<String> fieldNames = type.getFieldNames();
ImmutableList.Builder<TypeSignatureParameter> fieldTypes = ImmutableList.builder();
for (int i = 0; i < type.getFieldCount(); i++) {
fieldTypes.add(TypeSignatureParameter.of(new NamedTypeSignature(fieldNames.get(i), getType(types, type.getFieldTypeIndex(i)).getTypeSignature())));
}
return typeManager.getParameterizedType(StandardTypes.ROW, fieldTypes.build());
}
throw new PrestoException(RAPTOR_ERROR, "Unhandled ORC type: " + type);
}
Aggregations