use of com.facebook.presto.common.type.RowFieldName in project presto by prestodb.
the class CreateTypeTask method execute.
@Override
public ListenableFuture<?> execute(CreateType statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
TypeSignature signature;
if (statement.getDistinctType().isPresent()) {
signature = new TypeSignature(statement.getDistinctType().get());
} else {
List<TypeSignatureParameter> typeParameters = Streams.zip(statement.getParameterNames().stream(), statement.getParameterTypes().stream(), (name, type) -> TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName(name, false)), parseTypeSignature(type)))).collect(toImmutableList());
signature = new TypeSignature(ROW, typeParameters);
}
UserDefinedType userDefinedType = new UserDefinedType(QualifiedObjectName.valueOf(statement.getTypeName().toString()), signature);
metadata.getFunctionAndTypeManager().addUserDefinedType(userDefinedType);
return immediateFuture(null);
}
use of com.facebook.presto.common.type.RowFieldName in project presto by prestodb.
the class TestRowParametricType method testTypeSignatureRoundTrip.
@Test
public void testTypeSignatureRoundTrip() {
FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
TypeSignature typeSignature = new TypeSignature(ROW, TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName("col1", false)), new TypeSignature(BIGINT))), TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName("col2", true)), new TypeSignature(DOUBLE))));
List<TypeParameter> parameters = typeSignature.getParameters().stream().map(parameter -> TypeParameter.of(parameter, functionAndTypeManager)).collect(Collectors.toList());
Type rowType = RowParametricType.ROW.createType(parameters);
assertEquals(rowType.getTypeSignature(), typeSignature);
}
use of com.facebook.presto.common.type.RowFieldName 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.RowFieldName 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.RowFieldName in project presto by prestodb.
the class TestClientTypeSignature method testJsonRoundTrip.
@Test
public void testJsonRoundTrip() {
TypeSignature bigint = BIGINT.getTypeSignature();
assertJsonRoundTrip(new ClientTypeSignature(bigint));
assertJsonRoundTrip(new ClientTypeSignature("array", ImmutableList.of(new ClientTypeSignatureParameter(TypeSignatureParameter.of(bigint)))));
assertJsonRoundTrip(new ClientTypeSignature("foo", ImmutableList.of(new ClientTypeSignatureParameter(TypeSignatureParameter.of(42)))));
assertJsonRoundTrip(new ClientTypeSignature("row", ImmutableList.of(new ClientTypeSignatureParameter(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName("foo", false)), bigint))), new ClientTypeSignatureParameter(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName("bar", false)), bigint))))));
}
Aggregations