Search in sources :

Example 21 with UsedByGeneratedCode

use of com.facebook.presto.annotation.UsedByGeneratedCode in project presto by prestodb.

the class DecimalCasts method varcharToShortDecimal.

@UsedByGeneratedCode
public static long varcharToShortDecimal(Slice value, long precision, long scale, long tenToScale) {
    try {
        String stringValue = value.toString(UTF_8);
        BigDecimal decimal = new BigDecimal(stringValue).setScale(intScale(scale), HALF_UP);
        if (overflows(decimal, precision)) {
            throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s)", stringValue, precision, scale));
        }
        return decimal.unscaledValue().longValue();
    } catch (NumberFormatException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s)", value.toString(UTF_8), precision, scale));
    }
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) BigDecimal(java.math.BigDecimal) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 22 with UsedByGeneratedCode

use of com.facebook.presto.annotation.UsedByGeneratedCode in project presto by prestodb.

the class MapConcatFunction method mapConcat.

@UsedByGeneratedCode
public static Block mapConcat(Type keyType, Type valueType, 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
    TypedSet typedSet = new TypedSet(keyType, entries / 2);
    BlockBuilder keyBlockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder valueBlockBuilder = pageBuilder.getBlockBuilder(1);
    // the last map
    Block map = maps[lastMapIndex];
    int total = 0;
    for (int i = 0; i < map.getPositionCount(); i += 2) {
        typedSet.add(map, i);
        keyType.appendTo(map, i, keyBlockBuilder);
        valueType.appendTo(map, i + 1, valueBlockBuilder);
        total++;
    }
    // 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, keyBlockBuilder);
                valueType.appendTo(map, i + 1, valueBlockBuilder);
                total++;
            }
        }
    }
    // the first map
    map = maps[firstMapIndex];
    for (int i = 0; i < map.getPositionCount(); i += 2) {
        if (!typedSet.contains(map, i)) {
            keyType.appendTo(map, i, keyBlockBuilder);
            valueType.appendTo(map, i + 1, valueBlockBuilder);
            total++;
        }
    }
    pageBuilder.declarePositions(total);
    Block[] blocks = new Block[2];
    blocks[0] = keyBlockBuilder.getRegion(keyBlockBuilder.getPositionCount() - total, total);
    blocks[1] = valueBlockBuilder.getRegion(valueBlockBuilder.getPositionCount() - total, total);
    return new InterleavedBlock(blocks);
}
Also used : TypedSet(com.facebook.presto.operator.aggregation.TypedSet) Block(com.facebook.presto.spi.block.Block) InterleavedBlock(com.facebook.presto.spi.block.InterleavedBlock) PageBuilder(com.facebook.presto.spi.PageBuilder) InterleavedBlock(com.facebook.presto.spi.block.InterleavedBlock) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 23 with UsedByGeneratedCode

use of com.facebook.presto.annotation.UsedByGeneratedCode in project presto by prestodb.

the class JsonToMapCast method toMap.

@UsedByGeneratedCode
public static Block toMap(Type mapType, ConnectorSession connectorSession, Slice json) {
    try {
        Map<?, ?> map = (Map<?, ?>) stackRepresentationToObject(connectorSession, json, mapType);
        if (map == null) {
            return null;
        }
        Type keyType = ((MapType) mapType).getKeyType();
        Type valueType = ((MapType) mapType).getValueType();
        BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(keyType, valueType), new BlockBuilderStatus(), map.size() * 2);
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            appendToBlockBuilder(keyType, entry.getKey(), blockBuilder);
            appendToBlockBuilder(valueType, entry.getValue(), blockBuilder);
        }
        return blockBuilder.build();
    } catch (RuntimeException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to " + mapType, e);
    }
}
Also used : MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) OperatorType(com.facebook.presto.spi.function.OperatorType) PrestoException(com.facebook.presto.spi.PrestoException) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) Map(java.util.Map) MapType(com.facebook.presto.type.MapType) TypeJsonUtils.appendToBlockBuilder(com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Aggregations

UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)23 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)17 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)15 PrestoException (com.facebook.presto.spi.PrestoException)7 Block (com.facebook.presto.spi.block.Block)3 Slice (io.airlift.slice.Slice)3 PageBuilder (com.facebook.presto.spi.PageBuilder)2 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)2 OperatorType (com.facebook.presto.spi.function.OperatorType)2 Type (com.facebook.presto.spi.type.Type)2 TypeJsonUtils.appendToBlockBuilder (com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder)2 BigDecimal (java.math.BigDecimal)2 TypedSet (com.facebook.presto.operator.aggregation.TypedSet)1 InterleavedBlock (com.facebook.presto.spi.block.InterleavedBlock)1 ArrayType (com.facebook.presto.type.ArrayType)1 MapType (com.facebook.presto.type.MapType)1 RowType (com.facebook.presto.type.RowType)1 TypeJsonUtils.stackRepresentationToObject (com.facebook.presto.type.TypeJsonUtils.stackRepresentationToObject)1 ImmutableList (com.google.common.collect.ImmutableList)1 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)1