Search in sources :

Example 11 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 12 with UsedByGeneratedCode

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

the class DecimalOperators method multiplyLongLongLong.

@UsedByGeneratedCode
public static Slice multiplyLongLongLong(Slice a, Slice b) {
    try {
        Slice result = UnscaledDecimal128Arithmetic.multiply(a, b);
        throwIfOverflows(result);
        return result;
    } catch (ArithmeticException e) {
        throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Decimal overflow", e);
    }
}
Also used : Slice(io.airlift.slice.Slice) PrestoException(com.facebook.presto.spi.PrestoException) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 13 with UsedByGeneratedCode

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

the class DecimalCasts method varcharToLongDecimal.

@UsedByGeneratedCode
public static Slice varcharToLongDecimal(Slice value, long precision, long scale, BigInteger tenToScale) {
    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 encodeUnscaledValue(decimal.unscaledValue());
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) BigDecimal(java.math.BigDecimal) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 14 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 15 with UsedByGeneratedCode

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

the class ArrayConcatUtils method appendElement.

@UsedByGeneratedCode
public static Block appendElement(Type elementType, Block block, Slice value) {
    BlockBuilder blockBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), block.getPositionCount() + 1);
    for (int i = 0; i < block.getPositionCount(); i++) {
        elementType.appendTo(block, i, blockBuilder);
    }
    elementType.writeSlice(blockBuilder, value);
    return blockBuilder.build();
}
Also used : BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) 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