use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class TestSignatureBinder method testBindLiteralForRepeatedDecimal.
@Test
public void testBindLiteralForRepeatedDecimal() {
TypeSignature leftType = parseTypeSignature("decimal(p,s)", ImmutableSet.of("p", "s"));
TypeSignature rightType = parseTypeSignature("decimal(p,s)", ImmutableSet.of("p", "s"));
Signature function = functionSignature().returnType(parseTypeSignature(StandardTypes.BOOLEAN)).argumentTypes(leftType, rightType).build();
assertThat(function).boundTo("decimal(10,5)", "decimal(10,5)").produces(new BoundVariables(ImmutableMap.of(), ImmutableMap.of("p", 10L, "s", 5L)));
assertThat(function).boundTo("decimal(10,8)", "decimal(9,8)").withCoercion().produces(new BoundVariables(ImmutableMap.of(), ImmutableMap.of("p", 10L, "s", 8L)));
assertThat(function).boundTo("decimal(10,2)", "decimal(10,8)").withCoercion().produces(new BoundVariables(ImmutableMap.of(), ImmutableMap.of("p", 16L, "s", 8L)));
assertThat(function).boundTo("unknown", "decimal(10,5)").withCoercion().produces(new BoundVariables(ImmutableMap.of(), ImmutableMap.of("p", 10L, "s", 5L)));
}
use of com.facebook.presto.common.type.TypeSignature 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(Optional::get).collect(toSet());
if (signatures.size() == 1) {
typeSignature = new TypeSignature(StandardTypes.ARRAY, signatures.stream().map(TypeSignatureParameter::of).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(Optional.of(new RowFieldName(String.format("%s%d", implicitPrefix, idx + 1), false)), 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(Optional.of(new RowFieldName(key, false)), fieldType.get())));
}
typeSignature = new TypeSignature(StandardTypes.ROW, parameters);
}
return Optional.ofNullable(typeSignature);
}
use of com.facebook.presto.common.type.TypeSignature 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 VARCHAR:
return createVarcharType(type.getLength().get());
case CHAR:
return createCharType(type.getLength().get());
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(Optional.of(new RowFieldName(fieldNames.get(i), false)), getType(types, type.getFieldTypeIndex(i)).getTypeSignature())));
}
return typeManager.getParameterizedType(StandardTypes.ROW, fieldTypes.build());
}
throw new PrestoException(RAPTOR_ERROR, "Unhandled ORC type: " + type);
}
use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class HiveType method getTypeSignature.
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(Optional.of(new RowFieldName(rowFieldName, false)), typeSignature)));
}
return new TypeSignature(StandardTypes.ROW, typeSignatureBuilder.build());
}
throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", typeInfo));
}
use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.
the class AggregationUtils method generateAggregationName.
public static String generateAggregationName(String baseName, TypeSignature outputType, List<TypeSignature> inputTypes) {
StringBuilder sb = new StringBuilder();
sb.append(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, getAbbreviatedTypeName(outputType)));
for (TypeSignature inputType : inputTypes) {
sb.append(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, getAbbreviatedTypeName(inputType)));
}
sb.append(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, baseName.toLowerCase(ENGLISH)));
return sb.toString();
}
Aggregations