Search in sources :

Example 1 with RowFieldName

use of io.trino.spi.type.RowFieldName in project trino by trinodb.

the class TestRowParametricType method testTypeSignatureRoundTrip.

@Test
public void testTypeSignatureRoundTrip() {
    TypeSignature typeSignature = new TypeSignature(ROW, TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName("col1")), BIGINT.getTypeSignature())), TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName("col2")), DOUBLE.getTypeSignature())));
    List<TypeParameter> parameters = typeSignature.getParameters().stream().map(parameter -> TypeParameter.of(parameter, TESTING_TYPE_MANAGER)).collect(Collectors.toList());
    Type rowType = RowParametricType.ROW.createType(TESTING_TYPE_MANAGER, parameters);
    assertEquals(rowType.getTypeSignature(), typeSignature);
}
Also used : NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) ROW(io.trino.spi.type.StandardTypes.ROW) Type(io.trino.spi.type.Type) TESTING_TYPE_MANAGER(io.trino.type.InternalTypeManager.TESTING_TYPE_MANAGER) Test(org.testng.annotations.Test) TypeParameter(io.trino.spi.type.TypeParameter) Collectors(java.util.stream.Collectors) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) List(java.util.List) BIGINT(io.trino.spi.type.BigintType.BIGINT) RowFieldName(io.trino.spi.type.RowFieldName) Optional(java.util.Optional) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) Assert.assertEquals(io.trino.testing.assertions.Assert.assertEquals) TypeSignature(io.trino.spi.type.TypeSignature) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeParameter(io.trino.spi.type.TypeParameter) Type(io.trino.spi.type.Type) RowFieldName(io.trino.spi.type.RowFieldName) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) Test(org.testng.annotations.Test)

Example 2 with RowFieldName

use of io.trino.spi.type.RowFieldName in project trino by trinodb.

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.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(filedName)), fieldType.getTypeSignature())));
    }
    return TESTING_TYPE_MANAGER.getParameterizedType(StandardTypes.ROW, typeSignatureParameters.build());
}
Also used : TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) OrcType(io.trino.orc.metadata.OrcType) MapType(io.trino.spi.type.MapType) VarbinaryType(io.trino.spi.type.VarbinaryType) CharType(io.trino.spi.type.CharType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RowFieldName(io.trino.spi.type.RowFieldName) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature)

Example 3 with RowFieldName

use of io.trino.spi.type.RowFieldName in project trino by trinodb.

the class RowParametricType method createType.

@Override
public Type createType(TypeManager typeManager, List<TypeParameter> parameters) {
    checkArgument(!parameters.isEmpty(), "Row type must have at least one parameter");
    checkArgument(parameters.stream().allMatch(parameter -> parameter.getKind() == ParameterKind.NAMED_TYPE), "Expected only named types as a parameters, got %s", parameters);
    List<TypeSignatureParameter> typeSignatureParameters = parameters.stream().map(TypeParameter::getNamedType).map(parameter -> TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(parameter.getName(), parameter.getType().getTypeSignature()))).collect(toList());
    List<RowType.Field> fields = parameters.stream().map(TypeParameter::getNamedType).map(parameter -> new RowType.Field(parameter.getName().map(RowFieldName::getName), parameter.getType())).collect(toList());
    return RowType.createWithTypeSignature(new TypeSignature(StandardTypes.ROW, typeSignatureParameters), fields);
}
Also used : RowType(io.trino.spi.type.RowType) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) Type(io.trino.spi.type.Type) StandardTypes(io.trino.spi.type.StandardTypes) TypeParameter(io.trino.spi.type.TypeParameter) ParameterKind(io.trino.spi.type.ParameterKind) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Collectors.toList(java.util.stream.Collectors.toList) ParametricType(io.trino.spi.type.ParametricType) RowFieldName(io.trino.spi.type.RowFieldName) TypeManager(io.trino.spi.type.TypeManager) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) TypeSignature(io.trino.spi.type.TypeSignature) TypeParameter(io.trino.spi.type.TypeParameter) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature)

Example 4 with RowFieldName

use of io.trino.spi.type.RowFieldName in project trino by trinodb.

the class RaptorStorageManager method getType.

private Type getType(ColumnMetadata<OrcType> types, OrcColumnId columnId) {
    OrcType type = types.get(columnId);
    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.typeParameter(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.typeParameter(keyType), TypeSignatureParameter.typeParameter(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.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(fieldNames.get(i))), getType(types, type.getFieldTypeIndex(i)).getTypeSignature())));
            }
            return typeManager.getParameterizedType(StandardTypes.ROW, fieldTypes.build());
        default:
            throw new TrinoException(RAPTOR_ERROR, "Unhandled ORC type: " + type);
    }
}
Also used : NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) OrcType(io.trino.orc.metadata.OrcType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RowFieldName(io.trino.spi.type.RowFieldName) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TrinoException(io.trino.spi.TrinoException)

Example 5 with RowFieldName

use of io.trino.spi.type.RowFieldName in project trino by trinodb.

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();
    }
    if (value instanceof Binary) {
        typeSignature = VARBINARY.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_MILLIS.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(Optional::isEmpty)) {
            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::typeParameter).collect(Collectors.toList()));
        } else {
            // TODO: client doesn't handle empty field name row type yet
            typeSignature = new TypeSignature(StandardTypes.ROW, IntStream.range(0, subTypes.size()).mapToObj(idx -> TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(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()) {
                parameters.add(TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(key)), fieldType.get())));
            }
        }
        if (!parameters.isEmpty()) {
            typeSignature = new TypeSignature(StandardTypes.ROW, parameters);
        }
    } else if (value instanceof DBRef) {
        List<TypeSignatureParameter> parameters = new ArrayList<>();
        TypeSignature idFieldType = guessFieldType(((DBRef) value).getId()).orElseThrow(() -> new UnsupportedOperationException("Unable to guess $id field type of DBRef from: " + ((DBRef) value).getId()));
        parameters.add(TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(DATABASE_NAME)), VARCHAR.getTypeSignature())));
        parameters.add(TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(COLLECTION_NAME)), VARCHAR.getTypeSignature())));
        parameters.add(TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(ID)), idFieldType)));
        typeSignature = new TypeSignature(StandardTypes.ROW, parameters);
    }
    return Optional.ofNullable(typeSignature);
}
Also used : Document(org.bson.Document) Arrays(java.util.Arrays) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) Date(java.util.Date) MongoDatabase(com.mongodb.client.MongoDatabase) SchemaNotFoundException(io.trino.spi.connector.SchemaNotFoundException) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) EvictableCacheBuilder(io.trino.collect.cache.EvictableCacheBuilder) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) Map(java.util.Map) ENGLISH(java.util.Locale.ENGLISH) SMALLINT(io.trino.spi.type.SmallintType.SMALLINT) Collectors.toSet(java.util.stream.Collectors.toSet) TypeSignature(io.trino.spi.type.TypeSignature) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Range(io.trino.spi.predicate.Range) Domain(io.trino.spi.predicate.Domain) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) TrinoException(io.trino.spi.TrinoException) Collectors(java.util.stream.Collectors) SchemaTableName(io.trino.spi.connector.SchemaTableName) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) BIGINT(io.trino.spi.type.BigintType.BIGINT) FindIterable(com.mongodb.client.FindIterable) OBJECT_ID(io.trino.plugin.mongodb.ObjectIdType.OBJECT_ID) Optional(java.util.Optional) HostAddress(io.trino.spi.HostAddress) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) IntStream(java.util.stream.IntStream) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) Slice(io.airlift.slice.Slice) Logger(io.airlift.log.Logger) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) TIMESTAMP_MILLIS(io.trino.spi.type.TimestampType.TIMESTAMP_MILLIS) Type(io.trino.spi.type.Type) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) Shorts(com.google.common.primitives.Shorts) MINUTES(java.util.concurrent.TimeUnit.MINUTES) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) VarcharType(io.trino.spi.type.VarcharType) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) MongoCursor(com.mongodb.client.MongoCursor) Verify.verify(com.google.common.base.Verify.verify) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Objects.requireNonNull(java.util.Objects.requireNonNull) ColumnHandle(io.trino.spi.connector.ColumnHandle) RowFieldName(io.trino.spi.type.RowFieldName) HostAddress.fromParts(io.trino.spi.HostAddress.fromParts) VARBINARY(io.trino.spi.type.VarbinaryType.VARBINARY) Math.toIntExact(java.lang.Math.toIntExact) Binary(org.bson.types.Binary) IntegerType(io.trino.spi.type.IntegerType) SignedBytes(com.google.common.primitives.SignedBytes) StandardTypes(io.trino.spi.type.StandardTypes) IndexOptions(com.mongodb.client.model.IndexOptions) TupleDomain(io.trino.spi.predicate.TupleDomain) Throwables.throwIfInstanceOf(com.google.common.base.Throwables.throwIfInstanceOf) Primitives(com.google.common.primitives.Primitives) ExecutionException(java.util.concurrent.ExecutionException) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) Collectors.toList(java.util.stream.Collectors.toList) DBRef(com.mongodb.DBRef) DeleteResult(com.mongodb.client.result.DeleteResult) ObjectId(org.bson.types.ObjectId) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TINYINT(io.trino.spi.type.TinyintType.TINYINT) Cache(com.google.common.cache.Cache) TypeManager(io.trino.spi.type.TypeManager) Optional(java.util.Optional) ObjectId(org.bson.types.ObjectId) RowFieldName(io.trino.spi.type.RowFieldName) DBRef(com.mongodb.DBRef) ArrayList(java.util.ArrayList) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) Document(org.bson.Document) Date(java.util.Date) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) Binary(org.bson.types.Binary)

Aggregations

NamedTypeSignature (io.trino.spi.type.NamedTypeSignature)5 RowFieldName (io.trino.spi.type.RowFieldName)5 TypeSignatureParameter (io.trino.spi.type.TypeSignatureParameter)5 Type (io.trino.spi.type.Type)4 TypeSignature (io.trino.spi.type.TypeSignature)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 List (java.util.List)3 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 OrcType (io.trino.orc.metadata.OrcType)2 TrinoException (io.trino.spi.TrinoException)2 RowType (io.trino.spi.type.RowType)2 StandardTypes (io.trino.spi.type.StandardTypes)2 TypeManager (io.trino.spi.type.TypeManager)2 VarcharType (io.trino.spi.type.VarcharType)2 Collectors.toList (java.util.stream.Collectors.toList)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Throwables.throwIfInstanceOf (com.google.common.base.Throwables.throwIfInstanceOf)1 Verify.verify (com.google.common.base.Verify.verify)1