Search in sources :

Example 46 with MapType

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

the class StructuralTestUtil method mapBlockOf.

public static Block mapBlockOf(Type keyType, Type valueType, Map<?, ?> value) {
    MapType mapType = mapType(keyType, valueType);
    BlockBuilder mapArrayBuilder = mapType.createBlockBuilder(null, 1);
    BlockBuilder singleMapWriter = mapArrayBuilder.beginBlockEntry();
    for (Map.Entry<?, ?> entry : value.entrySet()) {
        appendToBlockBuilder(keyType, entry.getKey(), singleMapWriter);
        appendToBlockBuilder(valueType, entry.getValue(), singleMapWriter);
    }
    mapArrayBuilder.closeEntry();
    return mapType.getObject(mapArrayBuilder, 0);
}
Also used : Map(java.util.Map) MapType(com.facebook.presto.common.type.MapType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 47 with MapType

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

the class ColumnIOConverter method constructField.

public static Optional<Field> constructField(Type type, ColumnIO columnIO) {
    if (columnIO == null) {
        return Optional.empty();
    }
    boolean required = columnIO.getType().getRepetition() != OPTIONAL;
    int repetitionLevel = columnRepetitionLevel(columnIO);
    int definitionLevel = columnDefinitionLevel(columnIO);
    if (ROW.equals(type.getTypeSignature().getBase())) {
        GroupColumnIO groupColumnIO = (GroupColumnIO) columnIO;
        List<Type> parameters = type.getTypeParameters();
        ImmutableList.Builder<Optional<Field>> fieldsBuilder = ImmutableList.builder();
        List<TypeSignatureParameter> fields = type.getTypeSignature().getParameters();
        boolean structHasParameters = false;
        for (int i = 0; i < fields.size(); i++) {
            NamedTypeSignature namedTypeSignature = fields.get(i).getNamedTypeSignature();
            String name = namedTypeSignature.getName().get().toLowerCase(Locale.ENGLISH);
            Optional<Field> field = constructField(parameters.get(i), lookupColumnByName(groupColumnIO, name));
            structHasParameters |= field.isPresent();
            fieldsBuilder.add(field);
        }
        if (structHasParameters) {
            return Optional.of(new GroupField(type, repetitionLevel, definitionLevel, required, fieldsBuilder.build()));
        }
        return Optional.empty();
    } else if (MAP.equals(type.getTypeSignature().getBase())) {
        GroupColumnIO groupColumnIO = (GroupColumnIO) columnIO;
        MapType mapType = (MapType) type;
        GroupColumnIO keyValueColumnIO = getMapKeyValueColumn(groupColumnIO);
        if (keyValueColumnIO.getChildrenCount() != 2) {
            return Optional.empty();
        }
        Optional<Field> keyField = constructField(mapType.getKeyType(), keyValueColumnIO.getChild(0));
        Optional<Field> valueField = constructField(mapType.getValueType(), keyValueColumnIO.getChild(1));
        return Optional.of(new GroupField(type, repetitionLevel, definitionLevel, required, ImmutableList.of(keyField, valueField)));
    } else if (ARRAY.equals(type.getTypeSignature().getBase())) {
        GroupColumnIO groupColumnIO = (GroupColumnIO) columnIO;
        List<Type> types = type.getTypeParameters();
        if (groupColumnIO.getChildrenCount() != 1) {
            return Optional.empty();
        }
        Optional<Field> field = constructField(types.get(0), getArrayElementColumn(groupColumnIO.getChild(0)));
        return Optional.of(new GroupField(type, repetitionLevel, definitionLevel, required, ImmutableList.of(field)));
    }
    PrimitiveColumnIO primitiveColumnIO = (PrimitiveColumnIO) columnIO;
    RichColumnDescriptor column = new RichColumnDescriptor(primitiveColumnIO.getColumnDescriptor(), columnIO.getType().asPrimitiveType());
    return Optional.of(new PrimitiveField(type, repetitionLevel, definitionLevel, required, column, primitiveColumnIO.getId()));
}
Also used : Optional(java.util.Optional) ImmutableList(com.google.common.collect.ImmutableList) GroupField(com.facebook.presto.parquet.GroupField) RichColumnDescriptor(com.facebook.presto.parquet.RichColumnDescriptor) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) MapType(com.facebook.presto.common.type.MapType) GroupField(com.facebook.presto.parquet.GroupField) PrimitiveField(com.facebook.presto.parquet.PrimitiveField) Field(com.facebook.presto.parquet.Field) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) PrimitiveField(com.facebook.presto.parquet.PrimitiveField)

Example 48 with MapType

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

the class TestObjectEncoders method testComplexObjectEncoders.

@Test
public void testComplexObjectEncoders() {
    ObjectInspector inspector;
    ObjectEncoder encoder;
    inspector = ObjectInspectors.create(new ArrayType(BIGINT), typeManager);
    encoder = createEncoder(new ArrayType(BIGINT), inspector);
    assertTrue(encoder instanceof ObjectEncoders.ListObjectEncoder);
    Object arrayObject = encoder.encode(new Long[] { 1L, 2L, 3L });
    assertTrue(arrayObject instanceof LongArrayBlock);
    assertEquals(((LongArrayBlock) arrayObject).getLong(0), 1L);
    assertEquals(((LongArrayBlock) arrayObject).getLong(1), 2L);
    assertEquals(((LongArrayBlock) arrayObject).getLong(2), 3L);
    inspector = ObjectInspectors.create(new MapType(VARCHAR, BIGINT, methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation")), typeManager);
    encoder = createEncoder(new MapType(VARCHAR, BIGINT, methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation")), inspector);
    assertTrue(encoder instanceof ObjectEncoders.MapObjectEncoder);
    assertTrue(encoder.encode(new HashMap<String, Long>() {
    }) instanceof SingleMapBlock);
}
Also used : LongArrayBlock(com.facebook.presto.common.block.LongArrayBlock) PrimitiveObjectInspectorFactory.writableByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableByteObjectInspector) PrimitiveObjectInspectorFactory.writableDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableDateObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) PrimitiveObjectInspectorFactory.writableShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableShortObjectInspector) PrimitiveObjectInspectorFactory.writableStringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableStringObjectInspector) PrimitiveObjectInspectorFactory.writableBooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableBooleanObjectInspector) PrimitiveObjectInspectorFactory.writableHiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableHiveDecimalObjectInspector) PrimitiveObjectInspectorFactory.writableBinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableBinaryObjectInspector) PrimitiveObjectInspectorFactory.writableIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableIntObjectInspector) PrimitiveObjectInspectorFactory.writableDoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableDoubleObjectInspector) PrimitiveObjectInspectorFactory.writableLongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableLongObjectInspector) TestRowType(com.facebook.presto.common.type.TestRowType) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) Test(org.testng.annotations.Test)

Example 49 with MapType

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

the class MapUnionAggregation method specialize.

@Override
public InternalAggregationFunction specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
    Type keyType = boundVariables.getTypeVariable("K");
    Type valueType = boundVariables.getTypeVariable("V");
    MapType outputType = (MapType) functionAndTypeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(valueType.getTypeSignature())));
    return generateAggregation(keyType, valueType, outputType);
}
Also used : MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) MapType(com.facebook.presto.common.type.MapType)

Example 50 with MapType

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

the class MapUnionAggregation method generateAggregation.

private static InternalAggregationFunction generateAggregation(Type keyType, Type valueType, MapType outputType) {
    DynamicClassLoader classLoader = new DynamicClassLoader(MapUnionAggregation.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(outputType);
    KeyValuePairStateSerializer stateSerializer = new KeyValuePairStateSerializer(outputType);
    Type intermediateType = stateSerializer.getSerializedType();
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, outputType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(outputType), INPUT_FUNCTION.bindTo(keyType).bindTo(valueType), COMBINE_FUNCTION, OUTPUT_FUNCTION, ImmutableList.of(new AccumulatorStateDescriptor(KeyValuePairsState.class, stateSerializer, new KeyValuePairsStateFactory(keyType, valueType))), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), outputType, true, true, factory);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) KeyValuePairStateSerializer(com.facebook.presto.operator.aggregation.state.KeyValuePairStateSerializer) AccumulatorStateDescriptor(com.facebook.presto.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) KeyValuePairsStateFactory(com.facebook.presto.operator.aggregation.state.KeyValuePairsStateFactory)

Aggregations

MapType (com.facebook.presto.common.type.MapType)92 Type (com.facebook.presto.common.type.Type)49 ArrayType (com.facebook.presto.common.type.ArrayType)40 Test (org.testng.annotations.Test)32 RowType (com.facebook.presto.common.type.RowType)30 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)24 Block (com.facebook.presto.common.block.Block)21 HashMap (java.util.HashMap)12 DecimalType (com.facebook.presto.common.type.DecimalType)11 ImmutableList (com.google.common.collect.ImmutableList)11 List (java.util.List)11 Map (java.util.Map)11 VarcharType (com.facebook.presto.common.type.VarcharType)9 MethodHandle (java.lang.invoke.MethodHandle)9 ArrayList (java.util.ArrayList)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 SingleMapBlock (com.facebook.presto.common.block.SingleMapBlock)7 PrestoException (com.facebook.presto.spi.PrestoException)7 OperatorType (com.facebook.presto.common.function.OperatorType)6 MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)5