Search in sources :

Example 6 with RowFieldName

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);
}
Also used : WarningCollector(com.facebook.presto.spi.WarningCollector) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ROW(com.facebook.presto.common.type.StandardTypes.ROW) TypeSignature(com.facebook.presto.common.type.TypeSignature) Inject(javax.inject.Inject) Objects.requireNonNull(java.util.Objects.requireNonNull) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) UserDefinedType(com.facebook.presto.common.type.UserDefinedType) TransactionManager(com.facebook.presto.transaction.TransactionManager) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) Futures.immediateFuture(com.google.common.util.concurrent.Futures.immediateFuture) Session(com.facebook.presto.Session) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) CreateType(com.facebook.presto.sql.tree.CreateType) Streams(com.google.common.collect.Streams) String.format(java.lang.String.format) SqlParser(com.facebook.presto.sql.parser.SqlParser) List(java.util.List) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Expression(com.facebook.presto.sql.tree.Expression) Optional(java.util.Optional) RowFieldName(com.facebook.presto.common.type.RowFieldName) Metadata(com.facebook.presto.metadata.Metadata) AccessControl(com.facebook.presto.security.AccessControl) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) UserDefinedType(com.facebook.presto.common.type.UserDefinedType)

Example 7 with RowFieldName

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);
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) Assert.assertEquals(com.facebook.presto.testing.assertions.Assert.assertEquals) Test(org.testng.annotations.Test) ROW(com.facebook.presto.common.type.StandardTypes.ROW) Collectors(java.util.stream.Collectors) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) TypeSignature(com.facebook.presto.common.type.TypeSignature) DOUBLE(com.facebook.presto.common.type.StandardTypes.DOUBLE) List(java.util.List) Optional(java.util.Optional) RowFieldName(com.facebook.presto.common.type.RowFieldName) TypeParameter(com.facebook.presto.common.type.TypeParameter) BIGINT(com.facebook.presto.common.type.StandardTypes.BIGINT) Type(com.facebook.presto.common.type.Type) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeParameter(com.facebook.presto.common.type.TypeParameter) Type(com.facebook.presto.common.type.Type) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) Test(org.testng.annotations.Test)

Example 8 with RowFieldName

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();
}
Also used : ArrayType(io.delta.standalone.types.ArrayType) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) StructType(io.delta.standalone.types.StructType) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ImmutableList(com.google.common.collect.ImmutableList) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) MapType(io.delta.standalone.types.MapType)

Example 9 with RowFieldName

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;
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature)

Example 10 with RowFieldName

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))))));
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) Test(org.testng.annotations.Test)

Aggregations

NamedTypeSignature (com.facebook.presto.common.type.NamedTypeSignature)10 RowFieldName (com.facebook.presto.common.type.RowFieldName)10 TypeSignature (com.facebook.presto.common.type.TypeSignature)9 TypeSignatureParameter (com.facebook.presto.common.type.TypeSignatureParameter)9 ImmutableList (com.google.common.collect.ImmutableList)6 Type (com.facebook.presto.common.type.Type)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 List (java.util.List)4 PrestoException (com.facebook.presto.spi.PrestoException)3 Optional (java.util.Optional)3 RowType (com.facebook.presto.common.type.RowType)2 StandardTypes (com.facebook.presto.common.type.StandardTypes)2 ROW (com.facebook.presto.common.type.StandardTypes.ROW)2 TypeParameter (com.facebook.presto.common.type.TypeParameter)2 VarcharType.createUnboundedVarcharType (com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Collectors.toList (java.util.stream.Collectors.toList)2 Test (org.testng.annotations.Test)2 Logger (com.facebook.airlift.log.Logger)1 Session (com.facebook.presto.Session)1