Search in sources :

Example 1 with MapType

use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.

the class ParquetReader method readMap.

private ColumnChunk readMap(GroupField field) throws IOException {
    List<Type> parameters = field.getType().getTypeParameters();
    checkArgument(parameters.size() == 2, "Maps must have two type parameters, found %s", parameters.size());
    Block[] localBlocks = new Block[parameters.size()];
    ColumnChunk columnChunk = readColumnChunk(field.getChildren().get(0).get());
    localBlocks[0] = columnChunk.getBlock();
    localBlocks[1] = readColumnChunk(field.getChildren().get(1).get()).getBlock();
    IntList offsets = new IntArrayList();
    BooleanList valueIsNull = new BooleanArrayList();
    calculateCollectionOffsets(field, offsets, valueIsNull, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels());
    Block mapBlock = ((MapType) field.getType()).createBlockFromKeyValue(Optional.of(valueIsNull.toBooleanArray()), offsets.toIntArray(), localBlocks[0], localBlocks[1]);
    return new ColumnChunk(mapBlock, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels());
}
Also used : BooleanList(it.unimi.dsi.fastutil.booleans.BooleanList) MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) BooleanArrayList(it.unimi.dsi.fastutil.booleans.BooleanArrayList) RunLengthEncodedBlock(io.prestosql.spi.block.RunLengthEncodedBlock) Block(io.prestosql.spi.block.Block) ArrayBlock(io.prestosql.spi.block.ArrayBlock) RowBlock(io.prestosql.spi.block.RowBlock) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) MapType(io.prestosql.spi.type.MapType) IntList(it.unimi.dsi.fastutil.ints.IntList)

Example 2 with MapType

use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.

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 = mapType.hash(mapArrayBuilder.build(), 0);
    assertOperator(HASH_CODE, inputString, BIGINT, hashResult);
}
Also used : MapType(io.prestosql.spi.type.MapType) BlockBuilder(io.prestosql.spi.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(io.prestosql.util.StructuralTestUtil.appendToBlockBuilder)

Example 3 with MapType

use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.

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.prestosql.spi.type.ArrayType) MapType(io.prestosql.spi.type.MapType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType) ArrayType(io.prestosql.spi.type.ArrayType) StructuralTestUtil.mapType(io.prestosql.util.StructuralTestUtil.mapType) VarcharType.createVarcharType(io.prestosql.spi.type.VarcharType.createVarcharType) SqlType(io.prestosql.spi.function.SqlType) Test(org.testng.annotations.Test)

Example 4 with MapType

use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.

the class AbstractTestType method getNonNullValueForType.

/**
 * @return a non-null value, represented in native container type
 */
private static Object getNonNullValueForType(Type type) {
    if (type.getJavaType() == boolean.class) {
        return true;
    }
    if (type.getJavaType() == long.class) {
        return 1L;
    }
    if (type.getJavaType() == double.class) {
        return 1.0;
    }
    if (type.getJavaType() == Slice.class) {
        return Slices.utf8Slice("_");
    }
    if (type instanceof ArrayType) {
        ArrayType arrayType = (ArrayType) type;
        Type elementType = arrayType.getElementType();
        Object elementNonNullValue = getNonNullValueForType(elementType);
        return arrayBlockOf(elementType, elementNonNullValue);
    }
    if (type instanceof MapType) {
        MapType mapType = (MapType) type;
        Type keyType = mapType.getKeyType();
        Type valueType = mapType.getValueType();
        Object keyNonNullValue = getNonNullValueForType(keyType);
        Object valueNonNullValue = getNonNullValueForType(valueType);
        Map<?, ?> map = ImmutableMap.of(keyNonNullValue, valueNonNullValue);
        return mapBlockOf(keyType, valueType, map);
    }
    if (type instanceof RowType) {
        RowType rowType = (RowType) type;
        List<Type> elementTypes = rowType.getTypeParameters();
        Object[] elementNonNullValues = elementTypes.stream().map(AbstractTestType::getNonNullValueForType).toArray(Object[]::new);
        return toRow(elementTypes, elementNonNullValues);
    }
    throw new IllegalStateException("Unsupported Java type " + type.getJavaType() + " (for type " + type + ")");
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) UnknownType(io.prestosql.spi.type.UnknownType) MapType(io.prestosql.spi.type.MapType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType)

Example 5 with MapType

use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.

the class TestArrayOperators method testArrayHashOperator.

@Test
public void testArrayHashOperator() {
    assertArrayHashOperator("ARRAY[1, 2]", INTEGER, ImmutableList.of(1, 2));
    assertArrayHashOperator("ARRAY[true, false]", BOOLEAN, ImmutableList.of(true, false));
    // test with ARRAY[ MAP( ARRAY[1], ARRAY[2] ) ]
    MapType mapType = mapType(INTEGER, INTEGER);
    assertArrayHashOperator("ARRAY[MAP(ARRAY[1], ARRAY[2])]", mapType, ImmutableList.of(mapBlockOf(INTEGER, INTEGER, ImmutableMap.of(1L, 2L))));
}
Also used : MapType(io.prestosql.spi.type.MapType) Test(org.testng.annotations.Test)

Aggregations

MapType (io.prestosql.spi.type.MapType)67 Type (io.prestosql.spi.type.Type)28 Test (org.testng.annotations.Test)26 ArrayType (io.prestosql.spi.type.ArrayType)20 BlockBuilder (io.prestosql.spi.block.BlockBuilder)17 RowType (io.prestosql.spi.type.RowType)16 Signature (io.prestosql.spi.function.Signature)14 Block (io.prestosql.spi.block.Block)11 TypeSignature.parseTypeSignature (io.prestosql.spi.type.TypeSignature.parseTypeSignature)10 PrestoException (io.prestosql.spi.PrestoException)7 List (java.util.List)6 Map (java.util.Map)6 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableMap (com.google.common.collect.ImmutableMap)5 BuiltInScalarFunctionImplementation (io.prestosql.spi.function.BuiltInScalarFunctionImplementation)5 VarcharType (io.prestosql.spi.type.VarcharType)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 TypedSet (io.prestosql.operator.aggregation.TypedSet)3 SqlType (io.prestosql.spi.function.SqlType)3 BigintType (io.prestosql.spi.type.BigintType)3