Search in sources :

Example 51 with MapType

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

the class AbstractTestAccumuloRowSerializer method testMap.

@Test
public void testMap() throws Exception {
    AccumuloRowSerializer serializer = serializerClass.getConstructor().newInstance();
    Type type = new MapType(VARCHAR, BIGINT);
    Map<Object, Object> expected = ImmutableMap.of("a", 1L, "b", 2L, "3", 3L);
    byte[] data = serializer.encode(type, AccumuloRowSerializer.getBlockFromMap(type, expected));
    Map<Object, Object> actual = serializer.decode(type, data);
    assertEquals(actual, expected);
    deserializeData(serializer, data);
    actual = AccumuloRowSerializer.getMapFromBlock(type, serializer.getMap(COLUMN_NAME, type));
    assertEquals(actual, expected);
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Example 52 with MapType

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

the class TestSerDeUtils method testReuse.

@Test
public void testReuse() throws Exception {
    BytesWritable value = new BytesWritable();
    byte[] first = "hello world".getBytes(UTF_8);
    value.set(first, 0, first.length);
    byte[] second = "bye".getBytes(UTF_8);
    value.set(second, 0, second.length);
    Type type = new TypeToken<Map<BytesWritable, Long>>() {
    }.getType();
    ObjectInspector inspector = getInspector(type);
    Block actual = getBlockObject(new MapType(createUnboundedVarcharType(), BIGINT), ImmutableMap.of(value, 0L), inspector);
    Block expected = mapBlockOf(createUnboundedVarcharType(), BIGINT, "bye", 0L);
    assertBlockEquals(actual, expected);
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) RowType(com.facebook.presto.type.RowType) VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) Type(java.lang.reflect.Type) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) ObjectInspectorFactory.getReflectionObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getReflectionObjectInspector) Block(com.facebook.presto.spi.block.Block) BytesWritable(org.apache.hadoop.io.BytesWritable) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) TreeMap(java.util.TreeMap) MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Example 53 with MapType

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

the class UnnestOperator method getUnnestedTypes.

private static List<Type> getUnnestedTypes(List<Type> types) {
    ImmutableList.Builder<Type> builder = ImmutableList.builder();
    for (Type type : types) {
        checkArgument(type instanceof ArrayType || type instanceof MapType, "Can only unnest map and array types");
        builder.addAll(type.getTypeParameters());
    }
    return builder.build();
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) ImmutableList(com.google.common.collect.ImmutableList) MapType(com.facebook.presto.type.MapType)

Example 54 with MapType

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

the class TestArrayTransformFunction method testTypeCombinations.

@Test
public void testTypeCombinations() throws Exception {
    assertFunction("transform(ARRAY [25, 26], x -> x + 1)", new ArrayType(INTEGER), ImmutableList.of(26, 27));
    assertFunction("transform(ARRAY [25, 26], x -> x + 1.0)", new ArrayType(DOUBLE), ImmutableList.of(26.0, 27.0));
    assertFunction("transform(ARRAY [25, 26], x -> x = 25)", new ArrayType(BOOLEAN), ImmutableList.of(true, false));
    assertFunction("transform(ARRAY [25, 26], x -> to_base(x, 16))", new ArrayType(createVarcharType(64)), ImmutableList.of("19", "1a"));
    assertFunction("transform(ARRAY [25, 26], x -> ARRAY[x + 1])", new ArrayType(new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of(26), ImmutableList.of(27)));
    assertFunction("transform(ARRAY [25.6, 27.3], x -> CAST(x AS BIGINT))", new ArrayType(BIGINT), ImmutableList.of(26L, 27L));
    assertFunction("transform(ARRAY [25.6, 27.3], x -> x + 1.0)", new ArrayType(DOUBLE), ImmutableList.of(26.6, 28.3));
    assertFunction("transform(ARRAY [25.6, 27.3], x -> x = 25.6)", new ArrayType(BOOLEAN), ImmutableList.of(true, false));
    assertFunction("transform(ARRAY [25.6, 27.3], x -> CAST(x AS VARCHAR))", new ArrayType(createUnboundedVarcharType()), ImmutableList.of("25.6", "27.3"));
    assertFunction("transform(ARRAY [25.6, 27.3], x -> MAP(ARRAY[x + 1], ARRAY[true]))", new ArrayType(new MapType(DOUBLE, BOOLEAN)), ImmutableList.of(ImmutableMap.of(26.6, true), ImmutableMap.of(28.3, true)));
    assertFunction("transform(ARRAY [true, false], x -> if(x, 25, 26))", new ArrayType(INTEGER), ImmutableList.of(25, 26));
    assertFunction("transform(ARRAY [false, true], x -> if(x, 25.6, 28.9))", new ArrayType(DOUBLE), ImmutableList.of(28.9, 25.6));
    assertFunction("transform(ARRAY [true, false], x -> not x)", new ArrayType(BOOLEAN), ImmutableList.of(false, true));
    assertFunction("transform(ARRAY [false, true], x -> CAST(x AS VARCHAR))", new ArrayType(createUnboundedVarcharType()), ImmutableList.of("false", "true"));
    assertFunction("transform(ARRAY [true, false], x -> ARRAY[x])", new ArrayType(new ArrayType(BOOLEAN)), ImmutableList.of(ImmutableList.of(true), ImmutableList.of(false)));
    assertFunction("transform(ARRAY ['41', '42'], x -> from_base(x, 16))", new ArrayType(BIGINT), ImmutableList.of(65L, 66L));
    assertFunction("transform(ARRAY ['25.6', '27.3'], x -> CAST(x AS DOUBLE))", new ArrayType(DOUBLE), ImmutableList.of(25.6, 27.3));
    assertFunction("transform(ARRAY ['abc', 'def'], x -> 'abc' = x)", new ArrayType(BOOLEAN), ImmutableList.of(true, false));
    assertFunction("transform(ARRAY ['abc', 'def'], x -> x || x)", new ArrayType(createUnboundedVarcharType()), ImmutableList.of("abcabc", "defdef"));
    assertFunction("transform(ARRAY ['123', '456'], x -> ROW(x, CAST(x AS INTEGER), x > '3'))", new ArrayType(new RowType(ImmutableList.of(createVarcharType(3), INTEGER, BOOLEAN), Optional.empty())), ImmutableList.of(ImmutableList.of("123", 123, false), ImmutableList.of("456", 456, true)));
    assertFunction("transform(ARRAY [ARRAY ['abc', null, '123'], ARRAY ['def', 'x', '456']], x -> from_base(x[3], 10))", new ArrayType(BIGINT), ImmutableList.of(123L, 456L));
    assertFunction("transform(ARRAY [ARRAY ['abc', null, '123'], ARRAY ['def', 'x', '456']], x -> CAST(x[3] AS DOUBLE))", new ArrayType(DOUBLE), ImmutableList.of(123.0, 456.0));
    assertFunction("transform(ARRAY [ARRAY ['abc', null, '123'], ARRAY ['def', 'x', '456']], x -> x[2] IS NULL)", new ArrayType(BOOLEAN), ImmutableList.of(true, false));
    assertFunction("transform(ARRAY [ARRAY ['abc', null, '123'], ARRAY ['def', 'x', '456']], x -> x[2])", new ArrayType(createVarcharType(3)), asList(null, "x"));
    assertFunction("transform(ARRAY [MAP(ARRAY['abc', 'def'], ARRAY[123, 456]), MAP(ARRAY['ghi', 'jkl'], ARRAY[234, 567])], x -> map_keys(x))", new ArrayType(new ArrayType(createVarcharType(3))), ImmutableList.of(ImmutableList.of("abc", "def"), ImmutableList.of("ghi", "jkl")));
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) RowType(com.facebook.presto.type.RowType) MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Example 55 with MapType

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

the class TestMapFilterFunction method testEmpty.

@Test
public void testEmpty() throws Exception {
    assertFunction("map_filter(map(ARRAY[], ARRAY[]), (k, v) -> true)", new MapType(UNKNOWN, UNKNOWN), ImmutableMap.of());
    assertFunction("map_filter(map(ARRAY[], ARRAY[]), (k, v) -> false)", new MapType(UNKNOWN, UNKNOWN), ImmutableMap.of());
    assertFunction("map_filter(map(ARRAY[], ARRAY[]), (k, v) -> CAST (NULL AS BOOLEAN))", new MapType(UNKNOWN, UNKNOWN), ImmutableMap.of());
    assertFunction("map_filter(CAST (map(ARRAY[], ARRAY[]) AS MAP(BIGINT,VARCHAR)), (k, v) -> true)", new MapType(BIGINT, VARCHAR), ImmutableMap.of());
}
Also used : MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Aggregations

MapType (com.facebook.presto.type.MapType)58 Test (org.testng.annotations.Test)42 ArrayType (com.facebook.presto.type.ArrayType)28 Type (com.facebook.presto.spi.type.Type)20 Signature (com.facebook.presto.metadata.Signature)18 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)14 RowType (com.facebook.presto.type.RowType)12 Block (com.facebook.presto.spi.block.Block)10 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)10 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)10 ImmutableList (com.google.common.collect.ImmutableList)7 HashMap (java.util.HashMap)7 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)6 ImmutableMap (com.google.common.collect.ImmutableMap)6 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)5 List (java.util.List)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)2 KeyValuePairStateSerializer (com.facebook.presto.operator.aggregation.state.KeyValuePairStateSerializer)2