Search in sources :

Example 6 with MapType

use of io.trino.spi.type.MapType in project trino by trinodb.

the class TestSimplePagesHashStrategy method testHashRowWithMapType.

@Test
public void testHashRowWithMapType() {
    MapType mapType = new MapType(INTEGER, INTEGER, new TypeOperators());
    Block block = mapType.createBlockFromKeyValue(Optional.empty(), new int[] { 0, 1 }, new IntArrayBlock(1, Optional.empty(), new int[] { 1234 }), new IntArrayBlock(1, Optional.empty(), new int[] { 5678 }));
    SimplePagesHashStrategy strategy = createSimplePagesHashStrategy(mapType, ImmutableList.of(block));
    Page page = new Page(block);
    // This works because MapType is comparable.
    assertEquals(strategy.hashRow(0, page), 451258269207618863L);
}
Also used : IntArrayBlock(io.trino.spi.block.IntArrayBlock) IntArrayBlock(io.trino.spi.block.IntArrayBlock) Block(io.trino.spi.block.Block) Page(io.trino.spi.Page) MapType(io.trino.spi.type.MapType) BlockTypeOperators(io.trino.type.BlockTypeOperators) TypeOperators(io.trino.spi.type.TypeOperators) Test(org.testng.annotations.Test)

Example 7 with MapType

use of io.trino.spi.type.MapType in project trino by trinodb.

the class TestStringFunctions method testSplitToMap.

@Test
public void testSplitToMap() {
    MapType expectedType = 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('\u4EA0\u4EFF\u4EA1', '\u4E00', '\u4EFF')", expectedType, ImmutableMap.of("\u4EA0", "\u4EA1"));
    // If corresponding value is not found, then ""(empty string) is its value
    assertFunction("SPLIT_TO_MAP('\u4EC0\u4EFF', '\u4E00', '\u4EFF')", expectedType, ImmutableMap.of("\u4EC0", ""));
    // If corresponding key is not found, then ""(empty string) is its key
    assertFunction("SPLIT_TO_MAP('\u4EFF\u4EC1', '\u4E00', '\u4EFF')", expectedType, ImmutableMap.of("", "\u4EC1"));
    // Entry delimiter and key-value delimiter must not be the same.
    assertInvalidFunction("SPLIT_TO_MAP('', '\u4EFF', '\u4EFF')", "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('\u4EA0\u4EFF\u4EA1\u4E00\u4EA0\u4EFF\u4EB1', '\u4E00', '\u4EFF')", "Duplicate keys (\u4EA0) 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'");
    assertCachedInstanceHasBoundedRetainedSize("SPLIT_TO_MAP('a=123,b=.4,c=,=d', ',', '=')");
}
Also used : MapType(io.trino.spi.type.MapType) Test(org.testng.annotations.Test)

Example 8 with MapType

use of io.trino.spi.type.MapType in project trino by trinodb.

the class TestMapOperators method testMapEntries.

@Test
public void testMapEntries() {
    Type unknownEntryType = entryType(UNKNOWN, UNKNOWN);
    assertFunction("map_entries(null)", unknownEntryType, null);
    assertFunction("map_entries(MAP(ARRAY[], null))", unknownEntryType, null);
    assertFunction("map_entries(MAP(null, ARRAY[]))", unknownEntryType, null);
    assertFunction("map_entries(MAP(ARRAY[1, 2, 3], null))", entryType(INTEGER, UNKNOWN), null);
    assertFunction("map_entries(MAP(null, ARRAY[1, 2, 3]))", entryType(UNKNOWN, INTEGER), null);
    assertFunction("map_entries(MAP(ARRAY[], ARRAY[]))", unknownEntryType, ImmutableList.of());
    assertFunction("map_entries(MAP(ARRAY[1], ARRAY['x']))", entryType(INTEGER, createVarcharType(1)), ImmutableList.of(ImmutableList.of(1, "x")));
    assertFunction("map_entries(MAP(ARRAY[1, 2], ARRAY['x', 'y']))", entryType(INTEGER, createVarcharType(1)), ImmutableList.of(ImmutableList.of(1, "x"), ImmutableList.of(2, "y")));
    assertFunction("map_entries(MAP(ARRAY['x', 'y'], ARRAY[ARRAY[1, 2], ARRAY[3, 4]]))", entryType(createVarcharType(1), new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of("x", ImmutableList.of(1, 2)), ImmutableList.of("y", ImmutableList.of(3, 4))));
    assertFunction("map_entries(MAP(ARRAY[ARRAY[1.0E0, 2.0E0], ARRAY[3.0E0, 4.0E0]], ARRAY[5.0E0, 6.0E0]))", entryType(new ArrayType(DOUBLE), DOUBLE), ImmutableList.of(ImmutableList.of(ImmutableList.of(1.0, 2.0), 5.0), ImmutableList.of(ImmutableList.of(3.0, 4.0), 6.0)));
    assertFunction("map_entries(MAP(ARRAY['x', 'y'], ARRAY[MAP(ARRAY[1], ARRAY[2]), MAP(ARRAY[3], ARRAY[4])]))", entryType(createVarcharType(1), mapType(INTEGER, INTEGER)), ImmutableList.of(ImmutableList.of("x", ImmutableMap.of(1, 2)), ImmutableList.of("y", ImmutableMap.of(3, 4))));
    assertFunction("map_entries(MAP(ARRAY[MAP(ARRAY[1], ARRAY[2]), MAP(ARRAY[3], ARRAY[4])], ARRAY['x', 'y']))", entryType(mapType(INTEGER, INTEGER), createVarcharType(1)), ImmutableList.of(ImmutableList.of(ImmutableMap.of(1, 2), "x"), ImmutableList.of(ImmutableMap.of(3, 4), "y")));
    // null values
    List<Object> expectedEntries = ImmutableList.of(asList("x", null), asList("y", null));
    assertFunction("map_entries(MAP(ARRAY['x', 'y'], ARRAY[null, null]))", entryType(createVarcharType(1), UNKNOWN), expectedEntries);
    assertCachedInstanceHasBoundedRetainedSize("map_entries(MAP(ARRAY[1, 2], ARRAY['x', 'y']))");
}
Also used : ArrayType(io.trino.spi.type.ArrayType) Type(io.trino.spi.type.Type) SqlType(io.trino.spi.function.SqlType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) RowType(io.trino.spi.type.RowType) DecimalType.createDecimalType(io.trino.spi.type.DecimalType.createDecimalType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) StructuralTestUtil.mapType(io.trino.util.StructuralTestUtil.mapType) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) Test(org.testng.annotations.Test)

Example 9 with MapType

use of io.trino.spi.type.MapType in project trino by trinodb.

the class TestMapOperators method assertMapHashOperator.

private void assertMapHashOperator(String inputString, Type keyType, Type valueType, List<Object> elements) {
    checkArgument(elements.size() % 2 == 0, "the size of elements should be even number");
    MapType mapType = mapType(keyType, valueType);
    BlockBuilder mapArrayBuilder = mapType.createBlockBuilder(null, 1);
    BlockBuilder singleMapWriter = mapArrayBuilder.beginBlockEntry();
    for (int i = 0; i < elements.size(); i += 2) {
        appendToBlockBuilder(keyType, elements.get(i), singleMapWriter);
        appendToBlockBuilder(valueType, elements.get(i + 1), singleMapWriter);
    }
    mapArrayBuilder.closeEntry();
    long hashResult = functionAssertions.getBlockTypeOperators().getHashCodeOperator(mapType).hashCode(mapArrayBuilder.build(), 0);
    assertOperator(HASH_CODE, inputString, BIGINT, hashResult);
    long xxHash64Result = functionAssertions.getBlockTypeOperators().getXxHash64Operator(mapType).xxHash64(mapArrayBuilder.build(), 0);
    assertOperator(XX_HASH_64, inputString, BIGINT, xxHash64Result);
}
Also used : MapType(io.trino.spi.type.MapType) StructuralTestUtil.appendToBlockBuilder(io.trino.util.StructuralTestUtil.appendToBlockBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 10 with MapType

use of io.trino.spi.type.MapType in project trino by trinodb.

the class TestSetDigest method testHashCounts.

@Test
public void testHashCounts() {
    SetDigest digest1 = new SetDigest();
    digest1.add(0);
    digest1.add(0);
    digest1.add(1);
    SetDigest digest2 = new SetDigest();
    digest2.add(0);
    digest2.add(0);
    digest2.add(2);
    digest2.add(2);
    MapType mapType = new MapType(BIGINT, SMALLINT, new TypeOperators());
    Block block = hashCounts(mapType, digest1.serialize());
    assertTrue(block instanceof SingleMapBlock);
    Set<Short> blockValues = new HashSet<>();
    for (int i = 1; i < block.getPositionCount(); i += 2) {
        blockValues.add(block.getShort(i, 0));
    }
    Set<Short> expected = ImmutableSet.of((short) 1, (short) 2);
    assertEquals(blockValues, expected);
    digest1.mergeWith(digest2);
    block = hashCounts(mapType, digest1.serialize());
    assertTrue(block instanceof SingleMapBlock);
    expected = ImmutableSet.of((short) 1, (short) 2, (short) 4);
    blockValues = new HashSet<>();
    for (int i = 1; i < block.getPositionCount(); i += 2) {
        blockValues.add(block.getShort(i, 0));
    }
    assertEquals(blockValues, expected);
}
Also used : SingleMapBlock(io.trino.spi.block.SingleMapBlock) Block(io.trino.spi.block.Block) SingleMapBlock(io.trino.spi.block.SingleMapBlock) MapType(io.trino.spi.type.MapType) TypeOperators(io.trino.spi.type.TypeOperators) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

MapType (io.trino.spi.type.MapType)85 Type (io.trino.spi.type.Type)45 ArrayType (io.trino.spi.type.ArrayType)42 RowType (io.trino.spi.type.RowType)38 BlockBuilder (io.trino.spi.block.BlockBuilder)28 VarcharType (io.trino.spi.type.VarcharType)26 Block (io.trino.spi.block.Block)17 DecimalType (io.trino.spi.type.DecimalType)17 Map (java.util.Map)17 Test (org.testng.annotations.Test)17 ImmutableList (com.google.common.collect.ImmutableList)16 List (java.util.List)14 TypeSignature.mapType (io.trino.spi.type.TypeSignature.mapType)13 CharType (io.trino.spi.type.CharType)12 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)11 TypeOperators (io.trino.spi.type.TypeOperators)10 ImmutableMap (com.google.common.collect.ImmutableMap)9 VarbinaryType (io.trino.spi.type.VarbinaryType)8 Collectors.toList (java.util.stream.Collectors.toList)8