Search in sources :

Example 6 with MapType

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

the class StorageTypeConverter method toStorageType.

public Type toStorageType(Type type) {
    Type storageType;
    if (type.equals(BOOLEAN) || type.equals(BIGINT) || type.equals(INTEGER) || type.equals(SMALLINT) || type.equals(TINYINT) || type.equals(DOUBLE) || type.equals(REAL) || type.equals(DATE) || isCharType(type) || isVarcharType(type) || isLongDecimal(type) || isShortDecimal(type) || type.equals(VARBINARY)) {
        storageType = type;
    } else // Check OrcType::toOrcType() for reference.
    if (type.equals(TIME) || type.equals(TIMESTAMP)) {
        storageType = BIGINT;
    } else if (type instanceof ArrayType) {
        storageType = new ArrayType(toStorageType(((ArrayType) type).getElementType()));
    } else if (type instanceof MapType) {
        storageType = mapType(toStorageType(((MapType) type).getKeyType()), toStorageType(((MapType) type).getValueType()));
    } else {
        throw new PrestoException(NOT_SUPPORTED, "Type not supported: " + type);
    }
    // We cannot write different java types because when Raptor calculates stats, it uses the column types rather than storage types.
    // Need to make sure min/max are compliant with both storage and column types.
    checkState(storageType.getJavaType().equals(type.getJavaType()));
    return storageType;
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) Chars.isCharType(com.facebook.presto.common.type.Chars.isCharType) Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) PrestoException(com.facebook.presto.spi.PrestoException) MapType(com.facebook.presto.common.type.MapType)

Example 7 with MapType

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

the class ParquetSchemaConverter method getMapType.

private org.apache.parquet.schema.Type getMapType(MapType type, String name, List<String> parent) {
    parent = ImmutableList.<String>builder().addAll(parent).add(name).add("key_value").build();
    Type keyType = type.getKeyType();
    Type valueType = type.getValueType();
    return Types.map(OPTIONAL).key(convert(keyType, "key", parent)).value(convert(valueType, "value", parent)).named(name);
}
Also used : PrimitiveType(org.apache.parquet.schema.PrimitiveType) MapType(com.facebook.presto.common.type.MapType) DecimalType(com.facebook.presto.common.type.DecimalType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) OriginalType(org.apache.parquet.schema.OriginalType) Type(com.facebook.presto.common.type.Type) GroupType(org.apache.parquet.schema.GroupType) VarcharType(com.facebook.presto.common.type.VarcharType) RealType(com.facebook.presto.common.type.RealType) MessageType(org.apache.parquet.schema.MessageType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) RowType(com.facebook.presto.common.type.RowType)

Example 8 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, Object[] keys, Object[] values) {
    checkArgument(keys.length == values.length, "keys/values must have the same length");
    MapType mapType = mapType(keyType, valueType);
    BlockBuilder blockBuilder = mapType.createBlockBuilder(null, 10);
    BlockBuilder singleMapBlockWriter = blockBuilder.beginBlockEntry();
    for (int i = 0; i < keys.length; i++) {
        Object key = keys[i];
        Object value = values[i];
        appendToBlockBuilder(keyType, key, singleMapBlockWriter);
        appendToBlockBuilder(valueType, value, singleMapBlockWriter);
    }
    blockBuilder.closeEntry();
    return mapType.getObject(blockBuilder, 0);
}
Also used : MapType(com.facebook.presto.common.type.MapType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(com.facebook.presto.util.StructuralTestUtil.appendToBlockBuilder)

Example 9 with MapType

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

the class TestObjectInputDecoders method testBlockObjectDecoders.

@Test
public void testBlockObjectDecoders() {
    ObjectInputDecoder decoder;
    decoder = createDecoder(new ArrayType(BIGINT), typeManager);
    assertTrue(decoder instanceof ObjectInputDecoders.ArrayObjectInputDecoder);
    assertEquals(((ArrayList) decoder.decode(createLongArrayBlock())).get(0), 2L);
    decoder = createDecoder(new MapType(BIGINT, BIGINT, methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation")), typeManager);
    assertTrue(decoder instanceof ObjectInputDecoders.MapObjectInputDecoder);
    HashMap map = (HashMap) decoder.decode(createLongArrayBlock());
    assertEquals(map.get(2L), 1L);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) HashMap(java.util.HashMap) TestRowType(com.facebook.presto.common.type.TestRowType) MapType(com.facebook.presto.common.type.MapType) Test(org.testng.annotations.Test)

Example 10 with MapType

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

the class AbstractTestHiveClient method assertValueTypes.

private static void assertValueTypes(MaterializedRow row, List<ColumnMetadata> schema) {
    for (int columnIndex = 0; columnIndex < schema.size(); columnIndex++) {
        ColumnMetadata column = schema.get(columnIndex);
        Object value = row.getField(columnIndex);
        if (value != null) {
            if (BOOLEAN.equals(column.getType())) {
                assertInstanceOf(value, Boolean.class);
            } else if (TINYINT.equals(column.getType())) {
                assertInstanceOf(value, Byte.class);
            } else if (SMALLINT.equals(column.getType())) {
                assertInstanceOf(value, Short.class);
            } else if (INTEGER.equals(column.getType())) {
                assertInstanceOf(value, Integer.class);
            } else if (BIGINT.equals(column.getType())) {
                assertInstanceOf(value, Long.class);
            } else if (DOUBLE.equals(column.getType())) {
                assertInstanceOf(value, Double.class);
            } else if (REAL.equals(column.getType())) {
                assertInstanceOf(value, Float.class);
            } else if (isVarcharType(column.getType())) {
                assertInstanceOf(value, String.class);
            } else if (isCharType(column.getType())) {
                assertInstanceOf(value, String.class);
            } else if (VARBINARY.equals(column.getType())) {
                assertInstanceOf(value, SqlVarbinary.class);
            } else if (TIMESTAMP.equals(column.getType())) {
                assertInstanceOf(value, SqlTimestamp.class);
            } else if (DATE.equals(column.getType())) {
                assertInstanceOf(value, SqlDate.class);
            } else if (column.getType() instanceof ArrayType || column.getType() instanceof RowType) {
                assertInstanceOf(value, List.class);
            } else if (column.getType() instanceof MapType) {
                assertInstanceOf(value, Map.class);
            } else {
                fail("Unknown primitive type " + columnIndex);
            }
        }
    }
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) RowType(com.facebook.presto.common.type.RowType) OptionalDouble(java.util.OptionalDouble) Constraint(com.facebook.presto.spi.Constraint) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) SqlDate(com.facebook.presto.common.type.SqlDate) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

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