Search in sources :

Example 21 with MapType

use of com.facebook.presto.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) {
                assertInstanceOf(value, List.class);
            } else if (column.getType() instanceof MapType) {
                assertInstanceOf(value, Map.class);
            } else {
                fail("Unknown primitive type " + columnIndex);
            }
        }
    }
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) SqlDate(com.facebook.presto.spi.type.SqlDate) SqlVarbinary(com.facebook.presto.spi.type.SqlVarbinary) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Constraint(com.facebook.presto.spi.Constraint) MapType(com.facebook.presto.type.MapType)

Example 22 with MapType

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

the class TestMapTransformKeyFunction method testTypeCombinations.

@Test
public void testTypeCombinations() throws Exception {
    assertFunction("transform_keys(map(ARRAY [25, 26, 27], ARRAY [25, 26, 27]), (k, v) -> k + v)", new MapType(INTEGER, INTEGER), ImmutableMap.of(50, 25, 52, 26, 54, 27));
    assertFunction("transform_keys(map(ARRAY [25, 26, 27], ARRAY [25.5, 26.5, 27.5]), (k, v) -> k + v)", new MapType(DOUBLE, DOUBLE), ImmutableMap.of(50.5, 25.5, 52.5, 26.5, 54.5, 27.5));
    assertFunction("transform_keys(map(ARRAY [25, 26], ARRAY [false, true]), (k, v) -> k % 2 = 0 OR v)", new MapType(BOOLEAN, BOOLEAN), ImmutableMap.of(false, false, true, true));
    assertFunction("transform_keys(map(ARRAY [25, 26, 27], ARRAY ['abc', 'def', 'xyz']), (k, v) -> to_base(k, 16) || substr(v, 1, 1))", new MapType(VARCHAR, createVarcharType(3)), ImmutableMap.of("19a", "abc", "1ad", "def", "1bx", "xyz"));
    assertFunction("transform_keys(map(ARRAY [25, 26], ARRAY [ARRAY ['a'], ARRAY ['b']]), (k, v) -> ARRAY [CAST(k AS VARCHAR)] || v)", new MapType(new ArrayType(VARCHAR), new ArrayType(createVarcharType(1))), ImmutableMap.of(ImmutableList.of("25", "a"), ImmutableList.of("a"), ImmutableList.of("26", "b"), ImmutableList.of("b")));
    assertFunction("transform_keys(map(ARRAY [25.5, 26.5, 27.5], ARRAY [25, 26, 27]), (k, v) -> CAST(k * 2 AS BIGINT) + v)", new MapType(BIGINT, INTEGER), ImmutableMap.of(76L, 25, 79L, 26, 82L, 27));
    assertFunction("transform_keys(map(ARRAY [25.5, 26.5, 27.5], ARRAY [25.5, 26.5, 27.5]), (k, v) -> k + v)", new MapType(DOUBLE, DOUBLE), ImmutableMap.of(51.0, 25.5, 53.0, 26.5, 55.0, 27.5));
    assertFunction("transform_keys(map(ARRAY [25.2, 26.2], ARRAY [false, true]), (k, v) -> CAST(k AS BIGINT) % 2 = 0 OR v)", new MapType(BOOLEAN, BOOLEAN), ImmutableMap.of(false, false, true, true));
    assertFunction("transform_keys(map(ARRAY [25.5, 26.5, 27.5], ARRAY ['abc', 'def', 'xyz']), (k, v) -> CAST(k AS VARCHAR) || substr(v, 1, 1))", new MapType(VARCHAR, createVarcharType(3)), ImmutableMap.of("25.5a", "abc", "26.5d", "def", "27.5x", "xyz"));
    assertFunction("transform_keys(map(ARRAY [25.5, 26.5], ARRAY [ARRAY ['a'], ARRAY ['b']]), (k, v) -> ARRAY [CAST(k AS VARCHAR)] || v)", new MapType(new ArrayType(VARCHAR), new ArrayType(createVarcharType(1))), ImmutableMap.of(ImmutableList.of("25.5", "a"), ImmutableList.of("a"), ImmutableList.of("26.5", "b"), ImmutableList.of("b")));
    assertFunction("transform_keys(map(ARRAY [true, false], ARRAY [25, 26]), (k, v) -> if(k, 2 * v, 3 * v))", new MapType(INTEGER, INTEGER), ImmutableMap.of(50, 25, 78, 26));
    assertFunction("transform_keys(map(ARRAY [false, true], ARRAY [25.5, 26.5]), (k, v) -> if(k, 2 * v, 3 * v))", new MapType(DOUBLE, DOUBLE), ImmutableMap.of(76.5, 25.5, 53.0, 26.5));
    Map<Boolean, Boolean> expectedBoolBoolMap = new HashMap<>();
    expectedBoolBoolMap.put(false, true);
    expectedBoolBoolMap.put(true, null);
    assertFunction("transform_keys(map(ARRAY [true, false], ARRAY [true, NULL]), (k, v) -> if(k, NOT v, v IS NULL))", new MapType(BOOLEAN, BOOLEAN), expectedBoolBoolMap);
    assertFunction("transform_keys(map(ARRAY [false, true], ARRAY ['abc', 'def']), (k, v) -> if(k, substr(v, 1, 2), substr(v, 1, 1)))", new MapType(createVarcharType(3), createVarcharType(3)), ImmutableMap.of("a", "abc", "de", "def"));
    assertFunction("transform_keys(map(ARRAY [true, false], ARRAY [ARRAY ['a', 'b'], ARRAY ['x', 'y']]), (k, v) -> if(k, reverse(v), v))", new MapType(new ArrayType(createVarcharType(1)), new ArrayType(createVarcharType(1))), ImmutableMap.of(ImmutableList.of("b", "a"), ImmutableList.of("a", "b"), ImmutableList.of("x", "y"), ImmutableList.of("x", "y")));
    assertFunction("transform_keys(map(ARRAY ['a', 'ab', 'abc'], ARRAY [25, 26, 27]), (k, v) -> length(k) + v)", new MapType(BIGINT, INTEGER), ImmutableMap.of(26L, 25, 28L, 26, 30L, 27));
    assertFunction("transform_keys(map(ARRAY ['a', 'ab', 'abc'], ARRAY [25.5, 26.5, 27.5]), (k, v) -> length(k) + v)", new MapType(DOUBLE, DOUBLE), ImmutableMap.of(26.5, 25.5, 28.5, 26.5, 30.5, 27.5));
    assertFunction("transform_keys(map(ARRAY ['a', 'b'], ARRAY [false, true]), (k, v) -> k = 'b' OR v)", new MapType(BOOLEAN, BOOLEAN), ImmutableMap.of(false, false, true, true));
    assertFunction("transform_keys(map(ARRAY ['a', 'x'], ARRAY ['bc', 'yz']), (k, v) -> k || v)", new MapType(VARCHAR, createVarcharType(2)), ImmutableMap.of("abc", "bc", "xyz", "yz"));
    assertFunction("transform_keys(map(ARRAY ['x', 'y'], ARRAY [ARRAY ['a'], ARRAY ['b']]), (k, v) -> k || v)", new MapType(new ArrayType(createVarcharType(1)), new ArrayType(createVarcharType(1))), ImmutableMap.of(ImmutableList.of("x", "a"), ImmutableList.of("a"), ImmutableList.of("y", "b"), ImmutableList.of("b")));
    assertFunction("transform_keys(map(ARRAY [ARRAY [1, 2], ARRAY [3, 4]], ARRAY [25, 26]), (k, v) -> reduce(k, 0, (s, x) -> s + x, s -> s) + v)", new MapType(INTEGER, INTEGER), ImmutableMap.of(28, 25, 33, 26));
    assertFunction("transform_keys(map(ARRAY [ARRAY [1, 2], ARRAY [3, 4]], ARRAY [25.5, 26.5]), (k, v) -> reduce(k, 0, (s, x) -> s + x, s -> s) + v)", new MapType(DOUBLE, DOUBLE), ImmutableMap.of(28.5, 25.5, 33.5, 26.5));
    assertFunction("transform_keys(map(ARRAY [ARRAY [1, 2], ARRAY [3, 4]], ARRAY [false, true]), (k, v) -> contains(k, 3) AND v)", new MapType(BOOLEAN, BOOLEAN), ImmutableMap.of(false, false, true, true));
    assertFunction("transform_keys(map(ARRAY [ARRAY [1, 2], ARRAY [3, 4]], ARRAY ['abc', 'xyz']), (k, v) -> transform(k, x -> CAST(x AS VARCHAR)) || v)", new MapType(new ArrayType(VARCHAR), createVarcharType(3)), ImmutableMap.of(ImmutableList.of("1", "2", "abc"), "abc", ImmutableList.of("3", "4", "xyz"), "xyz"));
    assertFunction("transform_keys(map(ARRAY [ARRAY [1, 2], ARRAY [3, 4]], ARRAY [ARRAY ['a'], ARRAY ['a', 'b']]), (k, v) -> transform(k, x -> CAST(x AS VARCHAR)) || v)", new MapType(new ArrayType(VARCHAR), new ArrayType(createVarcharType(1))), ImmutableMap.of(ImmutableList.of("1", "2", "a"), ImmutableList.of("a"), ImmutableList.of("3", "4", "a", "b"), ImmutableList.of("a", "b")));
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) HashMap(java.util.HashMap) MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Example 23 with MapType

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

the class TestMapTransformValueFunction method testEmpty.

@Test
public void testEmpty() throws Exception {
    assertFunction("transform_values(map(ARRAY[], ARRAY[]), (k, v) -> NULL)", new MapType(UNKNOWN, UNKNOWN), ImmutableMap.of());
    assertFunction("transform_values(map(ARRAY[], ARRAY[]), (k, v) -> k)", new MapType(UNKNOWN, UNKNOWN), ImmutableMap.of());
    assertFunction("transform_values(map(ARRAY[], ARRAY[]), (k, v) -> v)", new MapType(UNKNOWN, UNKNOWN), ImmutableMap.of());
    assertFunction("transform_values(map(ARRAY[], ARRAY[]), (k, v) -> 0)", new MapType(UNKNOWN, INTEGER), ImmutableMap.of());
    assertFunction("transform_values(map(ARRAY[], ARRAY[]), (k, v) -> true)", new MapType(UNKNOWN, BOOLEAN), ImmutableMap.of());
    assertFunction("transform_values(map(ARRAY[], ARRAY[]), (k, v) -> 'value')", new MapType(UNKNOWN, createVarcharType(5)), ImmutableMap.of());
    assertFunction("transform_values(CAST (map(ARRAY[], ARRAY[]) AS MAP(BIGINT,VARCHAR)), (k, v) -> k + CAST(v as BIGINT))", new MapType(BIGINT, BIGINT), ImmutableMap.of());
    assertFunction("transform_values(CAST (map(ARRAY[], ARRAY[]) AS MAP(BIGINT,VARCHAR)), (k, v) -> CAST(k AS VARCHAR) || v)", new MapType(BIGINT, VARCHAR), ImmutableMap.of());
}
Also used : MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Example 24 with MapType

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

the class TestMapTransformValueFunction method testBasic.

@Test
public void testBasic() throws Exception {
    assertFunction("transform_values(map(ARRAY [1, 2, 3, 4], ARRAY [10, 20, 30, 40]), (k, v) -> k + v)", new MapType(INTEGER, INTEGER), ImmutableMap.of(1, 11, 2, 22, 3, 33, 4, 44));
    assertFunction("transform_values(map(ARRAY ['a', 'b', 'c', 'd'], ARRAY [1, 2, 3, 4]), (k, v) -> v * v)", new MapType(createVarcharType(1), INTEGER), ImmutableMap.of("a", 1, "b", 4, "c", 9, "d", 16));
    assertFunction("transform_values(map(ARRAY ['a', 'b', 'c', 'd'], ARRAY [1, 2, 3, 4]), (k, v) -> k || CAST(v as VARCHAR))", new MapType(createVarcharType(1), VARCHAR), ImmutableMap.of("a", "a1", "b", "b2", "c", "c3", "d", "d4"));
    assertFunction("transform_values(map(ARRAY[1, 2, 3], ARRAY [1.0, 1.4, 1.7]), (k, v) -> map(ARRAY[1, 2, 3], ARRAY['one', 'two', 'three'])[k] || '_' || CAST(v AS VARCHAR))", new MapType(INTEGER, VARCHAR), ImmutableMap.of(1, "one_1.0", 2, "two_1.4", 3, "three_1.7"));
}
Also used : MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Example 25 with MapType

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

the class TestMapTransformValueFunction method testNullValue.

@Test
public void testNullValue() throws Exception {
    Map<Integer, Void> sequenceToNullMap = new HashMap<>();
    sequenceToNullMap.put(1, null);
    sequenceToNullMap.put(2, null);
    sequenceToNullMap.put(3, null);
    assertFunction("transform_values(map(ARRAY[1, 2, 3], ARRAY ['a', 'b', 'c']), (k, v) -> NULL)", new MapType(INTEGER, UNKNOWN), sequenceToNullMap);
    Map<Integer, String> mapWithNullValue = new HashMap<>();
    mapWithNullValue.put(1, "a");
    mapWithNullValue.put(2, "b");
    mapWithNullValue.put(3, null);
    assertFunction("transform_values(map(ARRAY[1, 2, 3], ARRAY ['a', 'b', NULL]), (k, v) -> v)", new MapType(INTEGER, createVarcharType(1)), mapWithNullValue);
    assertFunction("transform_values(map(ARRAY[1, 2, 3], ARRAY [10, 11, NULL]), (k, v) -> to_base(v, 16))", new MapType(INTEGER, createVarcharType(64)), mapWithNullValue);
    assertFunction("transform_values(map(ARRAY[1, 2, 3], ARRAY ['10', '11', 'Invalid']), (k, v) -> to_base(TRY_CAST(v as BIGINT), 16))", new MapType(INTEGER, createVarcharType(64)), mapWithNullValue);
    assertFunction("transform_values(map(ARRAY[1, 2, 3], ARRAY [0, 0, 0]), (k, v) -> element_at(map(ARRAY[1, 2], ARRAY['a', 'b']), k + v))", new MapType(INTEGER, createVarcharType(1)), mapWithNullValue);
    assertFunction("transform_values(map(ARRAY[1, 2, 3], ARRAY ['a', 'b', NULL]), (k, v) -> IF(v IS NULL, k + 1.0, k + 0.5))", new MapType(INTEGER, DOUBLE), ImmutableMap.of(1, 1.5, 2, 2.5, 3, 4.0));
}
Also used : HashMap(java.util.HashMap) 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