Search in sources :

Example 6 with TypeSignatureParameter

use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.

the class ParquetReader method readStruct.

private ColumnChunk readStruct(GroupField field) throws IOException {
    List<TypeSignatureParameter> fields = field.getType().getTypeSignature().getParameters();
    Block[] blocks = new Block[fields.size()];
    ColumnChunk columnChunk = null;
    List<Optional<Field>> parameters = field.getChildren();
    for (int i = 0; i < fields.size(); i++) {
        Optional<Field> parameter = parameters.get(i);
        if (parameter.isPresent()) {
            columnChunk = readColumnChunk(parameter.get());
            blocks[i] = columnChunk.getBlock();
        }
    }
    for (int i = 0; i < fields.size(); i++) {
        if (blocks[i] == null) {
            blocks[i] = RunLengthEncodedBlock.create(field.getType(), null, columnChunk.getBlock().getPositionCount());
        }
    }
    BooleanList structIsNull = StructColumnReader.calculateStructOffsets(field, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels());
    boolean[] structIsNullVector = structIsNull.toBooleanArray();
    Block rowBlock = RowBlock.fromFieldBlocks(structIsNullVector.length, Optional.of(structIsNullVector), blocks);
    return new ColumnChunk(rowBlock, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels());
}
Also used : BooleanList(it.unimi.dsi.fastutil.booleans.BooleanList) GroupField(com.facebook.presto.parquet.GroupField) PrimitiveField(com.facebook.presto.parquet.PrimitiveField) Field(com.facebook.presto.parquet.Field) Optional(java.util.Optional) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) IntArrayBlock(com.facebook.presto.common.block.IntArrayBlock) RowBlock(com.facebook.presto.common.block.RowBlock) ArrayBlock(com.facebook.presto.common.block.ArrayBlock) LongArrayBlock(com.facebook.presto.common.block.LongArrayBlock) Block(com.facebook.presto.common.block.Block)

Example 7 with TypeSignatureParameter

use of com.facebook.presto.common.type.TypeSignatureParameter 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));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrestoException(com.facebook.presto.spi.PrestoException) TypeInfoUtils.getTypeInfosFromTypeString(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils.getTypeInfosFromTypeString) TypeInfoUtils.getTypeInfoFromTypeString(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils.getTypeInfoFromTypeString) TypeInfoFactory.doubleTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.doubleTypeInfo) TypeInfoFactory.dateTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.dateTypeInfo) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) TypeInfoFactory.longTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.longTypeInfo) TypeInfoFactory.shortTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.shortTypeInfo) VarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo) TypeInfoFactory.timestampTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.timestampTypeInfo) TypeInfoFactory.floatTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.floatTypeInfo) TypeInfoFactory.intTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.intTypeInfo) TypeInfoFactory.binaryTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.binaryTypeInfo) TypeInfoFactory.byteTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.byteTypeInfo) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) TypeInfoFactory.booleanTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.booleanTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) TypeInfoFactory.stringTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.stringTypeInfo) CharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo) CharType.createCharType(com.facebook.presto.common.type.CharType.createCharType) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) Type(com.facebook.presto.common.type.Type) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo)

Example 8 with TypeSignatureParameter

use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.

the class BigQueryResultPageSource method writeBlock.

private void writeBlock(BlockBuilder output, Type type, Object value) {
    if (type instanceof ArrayType && value instanceof List<?>) {
        BlockBuilder builder = output.beginBlockEntry();
        for (Object element : (List<?>) value) {
            appendTo(type.getTypeParameters().get(0), element, builder);
        }
        output.closeEntry();
        return;
    }
    if (type instanceof RowType && value instanceof GenericRecord) {
        GenericRecord record = (GenericRecord) value;
        BlockBuilder builder = output.beginBlockEntry();
        List<String> fieldNames = new ArrayList<>();
        for (int i = 0; i < type.getTypeSignature().getParameters().size(); i++) {
            TypeSignatureParameter parameter = type.getTypeSignature().getParameters().get(i);
            fieldNames.add(parameter.getNamedTypeSignature().getName().orElse("field" + i));
        }
        checkState(fieldNames.size() == type.getTypeParameters().size(), "fieldName doesn't match with type size : %s", type);
        for (int index = 0; index < type.getTypeParameters().size(); index++) {
            appendTo(type.getTypeParameters().get(index), record.get(fieldNames.get(index)), builder);
        }
        output.closeEntry();
        return;
    }
    throw new BigQueryException(BIGQUERY_UNSUPPORTED_TYPE_FOR_BLOCK, "Unhandled type for Block: " + type.getTypeSignature());
}
Also used : ArrayList(java.util.ArrayList) RowType(com.facebook.presto.common.type.RowType) ArrayType(com.facebook.presto.common.type.ArrayType) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) GenericRecord(org.apache.avro.generic.GenericRecord) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 9 with TypeSignatureParameter

use of com.facebook.presto.common.type.TypeSignatureParameter in project presto by prestodb.

the class RowParametricType method createType.

@Override
public Type createType(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.of(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 : StandardTypes(com.facebook.presto.common.type.StandardTypes) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) TypeSignature(com.facebook.presto.common.type.TypeSignature) ParametricType(com.facebook.presto.common.type.ParametricType) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Collectors.toList(java.util.stream.Collectors.toList) ParameterKind(com.facebook.presto.common.type.ParameterKind) RowFieldName(com.facebook.presto.common.type.RowFieldName) TypeParameter(com.facebook.presto.common.type.TypeParameter) Type(com.facebook.presto.common.type.Type) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) RowType(com.facebook.presto.common.type.RowType) TypeParameter(com.facebook.presto.common.type.TypeParameter) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature)

Example 10 with TypeSignatureParameter

use of com.facebook.presto.common.type.TypeSignatureParameter 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)

Aggregations

TypeSignatureParameter (com.facebook.presto.common.type.TypeSignatureParameter)21 NamedTypeSignature (com.facebook.presto.common.type.NamedTypeSignature)15 ImmutableList (com.google.common.collect.ImmutableList)13 TypeSignature (com.facebook.presto.common.type.TypeSignature)11 Type (com.facebook.presto.common.type.Type)9 RowFieldName (com.facebook.presto.common.type.RowFieldName)8 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)8 PrestoException (com.facebook.presto.spi.PrestoException)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Optional (java.util.Optional)5 CharType (com.facebook.presto.common.type.CharType)4 DecimalType (com.facebook.presto.common.type.DecimalType)4 VarcharType (com.facebook.presto.common.type.VarcharType)4 Map (java.util.Map)4 Collectors.toList (java.util.stream.Collectors.toList)4 RowType (com.facebook.presto.common.type.RowType)3 StandardTypes (com.facebook.presto.common.type.StandardTypes)3 Objects.requireNonNull (java.util.Objects.requireNonNull)3 Set (java.util.Set)3