Search in sources :

Example 6 with MapType

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

the class TestMapTransformValueFunction method testTypeCombinations.

@Test
public void testTypeCombinations() throws Exception {
    assertFunction("transform_values(map(ARRAY [25, 26, 27], ARRAY [25, 26, 27]), (k, v) -> k + v)", new MapType(INTEGER, INTEGER), ImmutableMap.of(25, 50, 26, 52, 27, 54));
    assertFunction("transform_values(map(ARRAY [25, 26, 27], ARRAY [26.1, 31.2, 37.1]), (k, v) -> CAST(v - k AS BIGINT))", new MapType(INTEGER, BIGINT), ImmutableMap.of(25, 1L, 26, 5L, 27, 10L));
    assertFunction("transform_values(map(ARRAY [25, 27], ARRAY [false, true]), (k, v) -> if(v, k + 1, k + 2))", new MapType(INTEGER, INTEGER), ImmutableMap.of(25, 27, 27, 28));
    assertFunction("transform_values(map(ARRAY [25, 26, 27], ARRAY ['abc', 'd', 'xy']), (k, v) -> k + length(v))", new MapType(INTEGER, BIGINT), ImmutableMap.of(25, 28L, 26, 27L, 27, 29L));
    assertFunction("transform_values(map(ARRAY [25, 26, 27], ARRAY [ARRAY ['a'], ARRAY ['a', 'c'], ARRAY ['a', 'b', 'c']]), (k, v) -> k + cardinality(v))", new MapType(INTEGER, BIGINT), ImmutableMap.of(25, 26L, 26, 28L, 27, 30L));
    assertFunction("transform_values(map(ARRAY [25.5, 26.75, 27.875], ARRAY [25, 26, 27]), (k, v) -> k - v)", new MapType(DOUBLE, DOUBLE), ImmutableMap.of(25.5, 0.5, 26.75, 0.75, 27.875, 0.875));
    assertFunction("transform_values(map(ARRAY [25.5, 26.75, 27.875], ARRAY [25.0, 26.0, 27.0]), (k, v) -> k - v)", new MapType(DOUBLE, DOUBLE), ImmutableMap.of(25.5, 0.5, 26.75, 0.75, 27.875, 0.875));
    assertFunction("transform_values(map(ARRAY [25.5, 27.5], ARRAY [false, true]), (k, v) -> if(v, k + 0.1, k + 0.2))", new MapType(DOUBLE, DOUBLE), ImmutableMap.of(25.5, 25.7, 27.5, 27.6));
    assertFunction("transform_values(map(ARRAY [25.5, 26.5, 27.5], ARRAY ['a', 'def', 'xy']), (k, v) -> k + length(v))", new MapType(DOUBLE, DOUBLE), ImmutableMap.of(25.5, 26.5, 26.5, 29.5, 27.5, 29.5));
    assertFunction("transform_values(map(ARRAY [25.5, 26.5, 27.5], ARRAY [ARRAY ['a'], ARRAY ['a', 'c'], ARRAY ['a', 'b', 'c']]), (k, v) -> k + cardinality(v))", new MapType(DOUBLE, DOUBLE), ImmutableMap.of(25.5, 26.5, 26.5, 28.5, 27.5, 30.5));
    assertFunction("transform_values(map(ARRAY [true, false], ARRAY [25, 26]), (k, v) -> k AND v = 25)", new MapType(BOOLEAN, BOOLEAN), ImmutableMap.of(true, true, false, false));
    assertFunction("transform_values(map(ARRAY [false, true], ARRAY [25.5, 26.5]), (k, v) -> k OR v > 100)", new MapType(BOOLEAN, BOOLEAN), ImmutableMap.of(false, false, true, true));
    assertFunction("transform_values(map(ARRAY [true, false], ARRAY [false, null]), (k, v) -> NOT k OR v)", new MapType(BOOLEAN, BOOLEAN), ImmutableMap.of(false, true, true, false));
    assertFunction("transform_values(map(ARRAY [false, true], ARRAY ['abc', 'def']), (k, v) -> NOT k AND v = 'abc')", new MapType(BOOLEAN, BOOLEAN), ImmutableMap.of(false, true, true, false));
    assertFunction("transform_values(map(ARRAY [true, false], ARRAY [ARRAY ['a', 'b'], ARRAY ['a', 'b', 'c']]), (k, v) -> k OR cardinality(v) = 3)", new MapType(BOOLEAN, BOOLEAN), ImmutableMap.of(false, true, true, true));
    assertFunction("transform_values(map(ARRAY ['s0', 's1', 's2'], ARRAY [25, 26, 27]), (k, v) -> k || ':' || CAST(v as VARCHAR))", new MapType(createVarcharType(2), VARCHAR), ImmutableMap.of("s0", "s0:25", "s1", "s1:26", "s2", "s2:27"));
    assertFunction("transform_values(map(ARRAY ['s0', 's1', 's2'], ARRAY [25.5, 26.5, 27.5]), (k, v) -> k || ':' || CAST(v as VARCHAR))", new MapType(createVarcharType(2), VARCHAR), ImmutableMap.of("s0", "s0:25.5", "s1", "s1:26.5", "s2", "s2:27.5"));
    assertFunction("transform_values(map(ARRAY ['s0', 's2'], ARRAY [false, true]), (k, v) -> if(v, k, CAST(v AS VARCHAR)))", new MapType(createVarcharType(2), VARCHAR), ImmutableMap.of("s0", "false", "s2", "s2"));
    assertFunction("transform_values(map(ARRAY ['s0', 's1', 's2'], ARRAY ['abc', 'def', 'xyz']), (k, v) -> k || ':' || v)", new MapType(createVarcharType(2), VARCHAR), ImmutableMap.of("s0", "s0:abc", "s1", "s1:def", "s2", "s2:xyz"));
    assertFunction("transform_values(map(ARRAY ['s0', 's1', 's2'], ARRAY [ARRAY ['a', 'b'], ARRAY ['a', 'c'], ARRAY ['a', 'b', 'c']]), (k, v) -> k || ':' || array_max(v))", new MapType(createVarcharType(2), VARCHAR), ImmutableMap.of("s0", "s0:b", "s1", "s1:c", "s2", "s2:c"));
    assertFunction("transform_values(map(ARRAY [ARRAY [1, 2], ARRAY [3, 4]], ARRAY [25, 26]), (k, v) -> if(v % 2 = 0, reverse(k), k))", new MapType(new ArrayType(INTEGER), new ArrayType(INTEGER)), ImmutableMap.of(ImmutableList.of(1, 2), ImmutableList.of(1, 2), ImmutableList.of(3, 4), ImmutableList.of(4, 3)));
    assertFunction("transform_values(map(ARRAY [ARRAY [1, 2], ARRAY [3, 4]], ARRAY [25.5, 26.5]), (k, v) -> CAST(k AS ARRAY(DOUBLE)) || v)", new MapType(new ArrayType(INTEGER), new ArrayType(DOUBLE)), ImmutableMap.of(ImmutableList.of(1, 2), ImmutableList.of(1., 2., 25.5), ImmutableList.of(3, 4), ImmutableList.of(3., 4., 26.5)));
    assertFunction("transform_values(map(ARRAY [ARRAY [1, 2], ARRAY [3, 4]], ARRAY [false, true]), (k, v) -> if(v, reverse(k), k))", new MapType(new ArrayType(INTEGER), new ArrayType(INTEGER)), ImmutableMap.of(ImmutableList.of(1, 2), ImmutableList.of(1, 2), ImmutableList.of(3, 4), ImmutableList.of(4, 3)));
    assertFunction("transform_values(map(ARRAY [ARRAY [1, 2], ARRAY []], ARRAY ['a', 'ff']), (k, v) -> k || from_base(v, 16))", new MapType(new ArrayType(INTEGER), new ArrayType(BIGINT)), ImmutableMap.of(ImmutableList.of(1, 2), ImmutableList.of(1L, 2L, 10L), ImmutableList.of(), ImmutableList.of(255L)));
    assertFunction("transform_values(map(ARRAY [ARRAY [3, 4], ARRAY []], ARRAY [ARRAY ['a', 'b', 'c'], ARRAY ['a', 'c']]), (k, v) -> transform(k, x -> CAST(x AS VARCHAR)) || v)", new MapType(new ArrayType(INTEGER), new ArrayType(VARCHAR)), ImmutableMap.of(ImmutableList.of(3, 4), ImmutableList.of("3", "4", "a", "b", "c"), ImmutableList.of(), ImmutableList.of("a", "c")));
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Example 7 with MapType

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

the class TestMapFilterFunction method testTypeCombinations.

@Test
public void testTypeCombinations() throws Exception {
    assertFunction("map_filter(map(ARRAY [25, 26, 27], ARRAY [25, 26, 27]), (k, v) -> k = 25 OR v = 27)", new MapType(INTEGER, INTEGER), ImmutableMap.of(25, 25, 27, 27));
    assertFunction("map_filter(map(ARRAY [25, 26, 27], ARRAY [25.5, 26.5, 27.5]), (k, v) -> k = 25 OR v = 27.5)", new MapType(INTEGER, DOUBLE), ImmutableMap.of(25, 25.5, 27, 27.5));
    assertFunction("map_filter(map(ARRAY [25, 26, 27], ARRAY [false, null, true]), (k, v) -> k = 25 OR v)", new MapType(INTEGER, BOOLEAN), ImmutableMap.of(25, false, 27, true));
    assertFunction("map_filter(map(ARRAY [25, 26, 27], ARRAY ['abc', 'def', 'xyz']), (k, v) -> k = 25 OR v = 'xyz')", new MapType(INTEGER, createVarcharType(3)), ImmutableMap.of(25, "abc", 27, "xyz"));
    assertFunction("map_filter(map(ARRAY [25, 26, 27], ARRAY [ARRAY ['a', 'b'], ARRAY ['a', 'c'], ARRAY ['a', 'b', 'c']]), (k, v) -> k = 25 OR cardinality(v) = 3)", new MapType(INTEGER, new ArrayType(createVarcharType(1))), ImmutableMap.of(25, ImmutableList.of("a", "b"), 27, ImmutableList.of("a", "b", "c")));
    assertFunction("map_filter(map(ARRAY [25.5, 26.5, 27.5], ARRAY [25, 26, 27]), (k, v) -> k = 25.5 OR v = 27)", new MapType(DOUBLE, INTEGER), ImmutableMap.of(25.5, 25, 27.5, 27));
    assertFunction("map_filter(map(ARRAY [25.5, 26.5, 27.5], ARRAY [25.5, 26.5, 27.5]), (k, v) -> k = 25.5 OR v = 27.5)", new MapType(DOUBLE, DOUBLE), ImmutableMap.of(25.5, 25.5, 27.5, 27.5));
    assertFunction("map_filter(map(ARRAY [25.5, 26.5, 27.5], ARRAY [false, null, true]), (k, v) -> k = 25.5 OR v)", new MapType(DOUBLE, BOOLEAN), ImmutableMap.of(25.5, false, 27.5, true));
    assertFunction("map_filter(map(ARRAY [25.5, 26.5, 27.5], ARRAY ['abc', 'def', 'xyz']), (k, v) -> k = 25.5 OR v = 'xyz')", new MapType(DOUBLE, createVarcharType(3)), ImmutableMap.of(25.5, "abc", 27.5, "xyz"));
    assertFunction("map_filter(map(ARRAY [25.5, 26.5, 27.5], ARRAY [ARRAY ['a', 'b'], ARRAY ['a', 'c'], ARRAY ['a', 'b', 'c']]), (k, v) -> k = 25.5 OR cardinality(v) = 3)", new MapType(DOUBLE, new ArrayType(createVarcharType(1))), ImmutableMap.of(25.5, ImmutableList.of("a", "b"), 27.5, ImmutableList.of("a", "b", "c")));
    assertFunction("map_filter(map(ARRAY [true, false], ARRAY [25, 26]), (k, v) -> k AND v = 25)", new MapType(BOOLEAN, INTEGER), ImmutableMap.of(true, 25));
    assertFunction("map_filter(map(ARRAY [false, true], ARRAY [25.5, 26.5]), (k, v) -> k OR v > 100)", new MapType(BOOLEAN, DOUBLE), ImmutableMap.of(true, 26.5));
    Map<Boolean, Boolean> falseToNullMap = new HashMap<>();
    falseToNullMap.put(false, null);
    assertFunction("map_filter(map(ARRAY [true, false], ARRAY [false, null]), (k, v) -> NOT k OR v)", new MapType(BOOLEAN, BOOLEAN), falseToNullMap);
    assertFunction("map_filter(map(ARRAY [false, true], ARRAY ['abc', 'def']), (k, v) -> NOT k AND v = 'abc')", new MapType(BOOLEAN, createVarcharType(3)), ImmutableMap.of(false, "abc"));
    assertFunction("map_filter(map(ARRAY [true, false], ARRAY [ARRAY ['a', 'b'], ARRAY ['a', 'b', 'c']]), (k, v) -> k OR cardinality(v) = 3)", new MapType(BOOLEAN, new ArrayType(createVarcharType(1))), ImmutableMap.of(true, ImmutableList.of("a", "b"), false, ImmutableList.of("a", "b", "c")));
    assertFunction("map_filter(map(ARRAY ['s0', 's1', 's2'], ARRAY [25, 26, 27]), (k, v) -> k = 's0' OR v = 27)", new MapType(createVarcharType(2), INTEGER), ImmutableMap.of("s0", 25, "s2", 27));
    assertFunction("map_filter(map(ARRAY ['s0', 's1', 's2'], ARRAY [25.5, 26.5, 27.5]), (k, v) -> k = 's0' OR v = 27.5)", new MapType(createVarcharType(2), DOUBLE), ImmutableMap.of("s0", 25.5, "s2", 27.5));
    assertFunction("map_filter(map(ARRAY ['s0', 's1', 's2'], ARRAY [false, null, true]), (k, v) -> k = 's0' OR v)", new MapType(createVarcharType(2), BOOLEAN), ImmutableMap.of("s0", false, "s2", true));
    assertFunction("map_filter(map(ARRAY ['s0', 's1', 's2'], ARRAY ['abc', 'def', 'xyz']), (k, v) -> k = 's0' OR v = 'xyz')", new MapType(createVarcharType(2), createVarcharType(3)), ImmutableMap.of("s0", "abc", "s2", "xyz"));
    assertFunction("map_filter(map(ARRAY ['s0', 's1', 's2'], ARRAY [ARRAY ['a', 'b'], ARRAY ['a', 'c'], ARRAY ['a', 'b', 'c']]), (k, v) -> k = 's0' OR cardinality(v) = 3)", new MapType(createVarcharType(2), new ArrayType(createVarcharType(1))), ImmutableMap.of("s0", ImmutableList.of("a", "b"), "s2", ImmutableList.of("a", "b", "c")));
    assertFunction("map_filter(map(ARRAY [ARRAY [1, 2], ARRAY [3, 4], ARRAY []], ARRAY [25, 26, 27]), (k, v) -> k = ARRAY [1, 2] OR v = 27)", new MapType(new ArrayType(INTEGER), INTEGER), ImmutableMap.of(ImmutableList.of(1, 2), 25, ImmutableList.of(), 27));
    assertFunction("map_filter(map(ARRAY [ARRAY [1, 2], ARRAY [3, 4], ARRAY []], ARRAY [25.5, 26.5, 27.5]), (k, v) -> k = ARRAY [1, 2] OR v = 27.5)", new MapType(new ArrayType(INTEGER), DOUBLE), ImmutableMap.of(ImmutableList.of(1, 2), 25.5, ImmutableList.of(), 27.5));
    assertFunction("map_filter(map(ARRAY [ARRAY [1, 2], ARRAY [3, 4], ARRAY []], ARRAY [false, null, true]), (k, v) -> k = ARRAY [1, 2] OR v)", new MapType(new ArrayType(INTEGER), BOOLEAN), ImmutableMap.of(ImmutableList.of(1, 2), false, ImmutableList.of(), true));
    assertFunction("map_filter(map(ARRAY [ARRAY [1, 2], ARRAY [3, 4], ARRAY []], ARRAY ['abc', 'def', 'xyz']), (k, v) -> k = ARRAY [1, 2] OR v = 'xyz')", new MapType(new ArrayType(INTEGER), createVarcharType(3)), ImmutableMap.of(ImmutableList.of(1, 2), "abc", ImmutableList.of(), "xyz"));
    assertFunction("map_filter(map(ARRAY [ARRAY [1, 2], ARRAY [3, 4], ARRAY []], ARRAY [ARRAY ['a', 'b'], ARRAY ['a', 'b', 'c'], ARRAY ['a', 'c']]), (k, v) -> cardinality(k) = 0 OR cardinality(v) = 3)", new MapType(new ArrayType(INTEGER), new ArrayType(createVarcharType(1))), ImmutableMap.of(ImmutableList.of(3, 4), ImmutableList.of("a", "b", "c"), ImmutableList.of(), ImmutableList.of("a", "c")));
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) HashMap(java.util.HashMap) MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Example 8 with MapType

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

the class TestMapFilterFunction method testNull.

@Test
public void testNull() throws Exception {
    Map<Integer, Void> oneToNullMap = new HashMap<>();
    oneToNullMap.put(1, null);
    assertFunction("map_filter(map(ARRAY[1], ARRAY [NULL]), (k, v) -> v IS NULL)", new MapType(INTEGER, UNKNOWN), oneToNullMap);
    assertFunction("map_filter(map(ARRAY[1], ARRAY [NULL]), (k, v) -> v IS NOT NULL)", new MapType(INTEGER, UNKNOWN), ImmutableMap.of());
    assertFunction("map_filter(map(ARRAY[1], ARRAY [CAST (NULL AS INTEGER)]), (k, v) -> v IS NULL)", new MapType(INTEGER, INTEGER), oneToNullMap);
    Map<Integer, Void> sequenceToNullMap = new HashMap<>();
    sequenceToNullMap.put(1, null);
    sequenceToNullMap.put(2, null);
    sequenceToNullMap.put(3, null);
    assertFunction("map_filter(map(ARRAY[1, 2, 3], ARRAY [NULL, NULL, NULL]), (k, v) -> v IS NULL)", new MapType(INTEGER, UNKNOWN), sequenceToNullMap);
    assertFunction("map_filter(map(ARRAY[1, 2, 3], ARRAY [NULL, NULL, NULL]), (k, v) -> v IS NOT NULL)", new MapType(INTEGER, UNKNOWN), ImmutableMap.of());
}
Also used : HashMap(java.util.HashMap) MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Example 9 with MapType

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

the class TestMapFilterFunction method testBasic.

@Test
public void testBasic() throws Exception {
    assertFunction("map_filter(map(ARRAY [5, 6, 7, 8], ARRAY [5, 6, 6, 5]), (x, y) -> x <= 6 OR y = 5)", new MapType(INTEGER, INTEGER), ImmutableMap.of(5, 5, 6, 6, 8, 5));
    assertFunction("map_filter(map(ARRAY [5 + RANDOM(1), 6, 7, 8], ARRAY [5, 6, 6, 5]), (x, y) -> x <= 6 OR y = 5)", new MapType(INTEGER, INTEGER), ImmutableMap.of(5, 5, 6, 6, 8, 5));
    assertFunction("map_filter(map(ARRAY ['a', 'b', 'c', 'd'], ARRAY [1, 2, NULL, 4]), (k, v) -> v IS NOT NULL)", new MapType(createVarcharType(1), INTEGER), ImmutableMap.of("a", 1, "b", 2, "d", 4));
    assertFunction("map_filter(map(ARRAY ['a', 'b', 'c'], ARRAY [TRUE, FALSE, NULL]), (k, v) -> v)", new MapType(createVarcharType(1), BOOLEAN), ImmutableMap.of("a", true));
}
Also used : MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Example 10 with MapType

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

the class TestStringFunctions method testSplitToMap.

@Test
public void testSplitToMap() {
    MapType expectedType = new MapType(VARCHAR, VARCHAR);
    assertFunction("SPLIT_TO_MAP('', ',', '=')", expectedType, ImmutableMap.of());
    assertFunction("SPLIT_TO_MAP('a=123,b=.4,c=,=d', ',', '=')", expectedType, ImmutableMap.of("a", "123", "b", ".4", "c", "", "", "d"));
    assertFunction("SPLIT_TO_MAP('=', ',', '=')", expectedType, ImmutableMap.of("", ""));
    assertFunction("SPLIT_TO_MAP('key=>value', ',', '=>')", expectedType, ImmutableMap.of("key", "value"));
    assertFunction("SPLIT_TO_MAP('key => value', ',', '=>')", expectedType, ImmutableMap.of("key ", " value"));
    // Test SPLIT_TO_MAP for non-ASCII
    assertFunction("SPLIT_TO_MAP('亠仿亡', '一', '仿')", expectedType, ImmutableMap.of("亠", "亡"));
    // If corresponding value is not found, then ""(empty string) is its value
    assertFunction("SPLIT_TO_MAP('什仿', '一', '仿')", expectedType, ImmutableMap.of("什", ""));
    // If corresponding key is not found, then ""(empty string) is its key
    assertFunction("SPLIT_TO_MAP('仿仁', '一', '仿')", expectedType, ImmutableMap.of("", "仁"));
    // Entry delimiter and key-value delimiter must not be the same.
    assertInvalidFunction("SPLIT_TO_MAP('', '仿', '仿')", "entryDelimiter and keyValueDelimiter must not be the same");
    assertInvalidFunction("SPLIT_TO_MAP('a=123,b=.4,c=', '=', '=')", "entryDelimiter and keyValueDelimiter must not be the same");
    // Duplicate keys are not allowed to exist.
    assertInvalidFunction("SPLIT_TO_MAP('a=123,a=.4', ',', '=')", "Duplicate keys (a) are not allowed");
    assertInvalidFunction("SPLIT_TO_MAP('亠仿亡一亠仿亱', '一', '仿')", "Duplicate keys (亠) are not allowed");
    // Key-value delimiter must appear exactly once in each entry.
    assertInvalidFunction("SPLIT_TO_MAP('key', ',', '=')", "Key-value delimiter must appear exactly once in each entry. Bad input: 'key'");
    assertInvalidFunction("SPLIT_TO_MAP('key==value', ',', '=')", "Key-value delimiter must appear exactly once in each entry. Bad input: 'key==value'");
    assertInvalidFunction("SPLIT_TO_MAP('key=va=lue', ',', '=')", "Key-value delimiter must appear exactly once in each entry. Bad input: 'key=va=lue'");
}
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