Search in sources :

Example 6 with MapType

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

the class StructuralTestUtil method mapBlockOf.

public static Block mapBlockOf(Type keyType, Type valueType, Object[] keys, Object[] values) {
    checkArgument(keys.length == values.length, "keys/values must have the same length");
    MapType mapType = mapType(keyType, valueType);
    BlockBuilder blockBuilder = mapType.createBlockBuilder(null, 10);
    BlockBuilder singleMapBlockWriter = blockBuilder.beginBlockEntry();
    for (int i = 0; i < keys.length; i++) {
        Object key = keys[i];
        Object value = values[i];
        appendToBlockBuilder(keyType, key, singleMapBlockWriter);
        appendToBlockBuilder(valueType, value, singleMapBlockWriter);
    }
    blockBuilder.closeEntry();
    return mapType.getObject(blockBuilder, 0);
}
Also used : MapType(io.prestosql.spi.type.MapType) BlockBuilder(io.prestosql.spi.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(io.prestosql.util.StructuralTestUtil.appendToBlockBuilder)

Example 7 with MapType

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

the class JsonToMapCast method specialize.

@Override
public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
    checkArgument(arity == 1, "Expected arity to be 1");
    Type keyType = boundVariables.getTypeVariable("K");
    Type valueType = boundVariables.getTypeVariable("V");
    MapType mapType = (MapType) functionAndTypeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(valueType.getTypeSignature())));
    checkCondition(canCastFromJson(mapType), INVALID_CAST_ARGUMENT, "Cannot cast JSON to %s", mapType);
    BlockBuilderAppender keyAppender = createBlockBuilderAppender(mapType.getKeyType());
    BlockBuilderAppender valueAppender = createBlockBuilderAppender(mapType.getValueType());
    MethodHandle methodHandle = METHOD_HANDLE.bindTo(mapType).bindTo(keyAppender).bindTo(valueAppender);
    return new BuiltInScalarFunctionImplementation(true, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), methodHandle);
}
Also used : MapType(io.prestosql.spi.type.MapType) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) BuiltInScalarFunctionImplementation(io.prestosql.spi.function.BuiltInScalarFunctionImplementation) BlockBuilderAppender.createBlockBuilderAppender(io.prestosql.util.JsonUtil.BlockBuilderAppender.createBlockBuilderAppender) BlockBuilderAppender(io.prestosql.util.JsonUtil.BlockBuilderAppender) MapType(io.prestosql.spi.type.MapType) MethodHandle(java.lang.invoke.MethodHandle)

Example 8 with MapType

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

the class MapConcatFunction method mapConcat.

@UsedByGeneratedCode
public static Block mapConcat(MapType mapType, Object state, Block[] maps) {
    int entries = 0;
    int lastMapIndex = maps.length - 1;
    int firstMapIndex = lastMapIndex;
    for (int i = 0; i < maps.length; i++) {
        entries += maps[i].getPositionCount();
        if (maps[i].getPositionCount() > 0) {
            lastMapIndex = i;
            firstMapIndex = min(firstMapIndex, i);
        }
    }
    if (lastMapIndex == firstMapIndex) {
        return maps[lastMapIndex];
    }
    PageBuilder pageBuilder = (PageBuilder) state;
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    // TODO: we should move TypedSet into user state as well
    Type keyType = mapType.getKeyType();
    Type valueType = mapType.getValueType();
    TypedSet typedSet = new TypedSet(keyType, entries / 2, FUNCTION_NAME);
    BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
    // the last map
    Block map = maps[lastMapIndex];
    for (int i = 0; i < map.getPositionCount(); i += 2) {
        typedSet.add(map, i);
        keyType.appendTo(map, i, blockBuilder);
        valueType.appendTo(map, i + 1, blockBuilder);
    }
    // the map between the last and the first
    for (int idx = lastMapIndex - 1; idx > firstMapIndex; idx--) {
        map = maps[idx];
        for (int i = 0; i < map.getPositionCount(); i += 2) {
            if (!typedSet.contains(map, i)) {
                typedSet.add(map, i);
                keyType.appendTo(map, i, blockBuilder);
                valueType.appendTo(map, i + 1, blockBuilder);
            }
        }
    }
    // the first map
    map = maps[firstMapIndex];
    for (int i = 0; i < map.getPositionCount(); i += 2) {
        if (!typedSet.contains(map, i)) {
            keyType.appendTo(map, i, blockBuilder);
            valueType.appendTo(map, i + 1, blockBuilder);
        }
    }
    mapBlockBuilder.closeEntry();
    pageBuilder.declarePosition();
    return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Also used : MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) TypedSet(io.prestosql.operator.aggregation.TypedSet) Block(io.prestosql.spi.block.Block) PageBuilder(io.prestosql.spi.PageBuilder) BlockBuilder(io.prestosql.spi.block.BlockBuilder) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode)

Example 9 with MapType

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

the class MaterializedResult method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else if (BIGINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).longValue());
    } else if (INTEGER.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).intValue());
    } else if (SMALLINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).shortValue());
    } else if (TINYINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).byteValue());
    } else if (REAL.equals(type)) {
        type.writeLong(blockBuilder, (long) floatToRawIntBits(((Number) value).floatValue()));
    } else if (DOUBLE.equals(type)) {
        type.writeDouble(blockBuilder, ((Number) value).doubleValue());
    } else if (BOOLEAN.equals(type)) {
        type.writeBoolean(blockBuilder, (Boolean) value);
    } else if (JSON.equals(type)) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof VarcharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof CharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (VARBINARY.equals(type)) {
        type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) value));
    } else if (DATE.equals(type)) {
        int days = ((SqlDate) value).getDays();
        type.writeLong(blockBuilder, days);
    } else if (TIME.equals(type)) {
        SqlTime time = (SqlTime) value;
        if (time.isLegacyTimestamp()) {
            type.writeLong(blockBuilder, time.getMillisUtc());
        } else {
            type.writeLong(blockBuilder, time.getMillis());
        }
    } else if (TIME_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimeWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimeWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (TIMESTAMP.equals(type)) {
        long millisUtc = ((SqlTimestamp) value).getMillis();
        type.writeLong(blockBuilder, millisUtc);
    } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimestampWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimestampWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (ARRAY.equals(type.getTypeSignature().getBase())) {
        List<Object> list = (List<Object>) value;
        Type elementType = ((ArrayType) type).getElementType();
        BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object element : list) {
            writeValue(elementType, arrayBlockBuilder, element);
        }
        blockBuilder.closeEntry();
    } else if (MAP.equals(type.getTypeSignature().getBase())) {
        Map<Object, Object> map = (Map<Object, Object>) value;
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
        for (Entry<Object, Object> entry : map.entrySet()) {
            writeValue(keyType, mapBlockBuilder, entry.getKey());
            writeValue(valueType, mapBlockBuilder, entry.getValue());
        }
        blockBuilder.closeEntry();
    } else if (type instanceof RowType) {
        List<Object> row = (List<Object>) value;
        List<Type> fieldTypes = type.getTypeParameters();
        BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
        for (int field = 0; field < row.size(); field++) {
            writeValue(fieldTypes.get(field), rowBlockBuilder, row.get(field));
        }
        blockBuilder.closeEntry();
    } else {
        throw new IllegalArgumentException("Unsupported type " + type);
    }
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) SqlTime(io.prestosql.spi.type.SqlTime) SqlTimeWithTimeZone(io.prestosql.spi.type.SqlTimeWithTimeZone) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) CharType(io.prestosql.spi.type.CharType) MapType(io.prestosql.spi.type.MapType) VarcharType(io.prestosql.spi.type.VarcharType) SqlTimestampWithTimeZone(io.prestosql.spi.type.SqlTimestampWithTimeZone) SqlDate(io.prestosql.spi.type.SqlDate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) CharType(io.prestosql.spi.type.CharType) TimeZoneKey(io.prestosql.spi.type.TimeZoneKey) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 10 with MapType

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

the class BenchmarkMapCopy method benchmarkMapCopy.

@Benchmark
@OperationsPerInvocation(POSITIONS)
public BlockBuilder benchmarkMapCopy(BenchmarkData data) {
    Block block = data.getDataBlock();
    BlockBuilder blockBuilder = data.getBlockBuilder();
    MapType mapType = mapType(VARCHAR, BIGINT);
    for (int i = 0; i < POSITIONS; i++) {
        mapType.appendTo(block, i, blockBuilder);
    }
    return blockBuilder;
}
Also used : Block(io.prestosql.spi.block.Block) MapType(io.prestosql.spi.type.MapType) BlockBuilder(io.prestosql.spi.block.BlockBuilder) Benchmark(org.openjdk.jmh.annotations.Benchmark) OperationsPerInvocation(org.openjdk.jmh.annotations.OperationsPerInvocation)

Aggregations

MapType (io.prestosql.spi.type.MapType)80 Type (io.prestosql.spi.type.Type)39 ArrayType (io.prestosql.spi.type.ArrayType)31 RowType (io.prestosql.spi.type.RowType)27 Test (org.testng.annotations.Test)27 BlockBuilder (io.prestosql.spi.block.BlockBuilder)17 Block (io.prestosql.spi.block.Block)15 Signature (io.prestosql.spi.function.Signature)14 VarcharType (io.prestosql.spi.type.VarcharType)11 List (java.util.List)11 BigintType (io.prestosql.spi.type.BigintType)10 BooleanType (io.prestosql.spi.type.BooleanType)10 DoubleType (io.prestosql.spi.type.DoubleType)10 IntegerType (io.prestosql.spi.type.IntegerType)10 TypeSignature.parseTypeSignature (io.prestosql.spi.type.TypeSignature.parseTypeSignature)10 RealType (io.prestosql.spi.type.RealType)9 SmallintType (io.prestosql.spi.type.SmallintType)9 TimestampType (io.prestosql.spi.type.TimestampType)9 TinyintType (io.prestosql.spi.type.TinyintType)9 VarbinaryType (io.prestosql.spi.type.VarbinaryType)9