Search in sources :

Example 26 with SliceOutput

use of io.airlift.slice.SliceOutput in project presto by prestodb.

the class VariableWidthBlockBuilder method copyPositions.

@Override
public Block copyPositions(int[] positions, int offset, int length) {
    checkArrayRange(positions, offset, length);
    int finalLength = 0;
    for (int i = offset; i < offset + length; i++) {
        finalLength += getSliceLength(positions[i]);
    }
    SliceOutput newSlice = Slices.allocate(finalLength).getOutput();
    int[] newOffsets = new int[length + 1];
    boolean[] newValueIsNull = null;
    if (hasNullValue) {
        newValueIsNull = new boolean[length];
    }
    for (int i = 0; i < length; i++) {
        int position = positions[offset + i];
        if (isEntryNull(position)) {
            newValueIsNull[i] = true;
        } else {
            newSlice.writeBytes(sliceOutput.getUnderlyingSlice(), getPositionOffset(position), getSliceLength(position));
        }
        newOffsets[i + 1] = newSlice.size();
    }
    return new VariableWidthBlock(0, length, newSlice.slice(), newOffsets, newValueIsNull);
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput)

Example 27 with SliceOutput

use of io.airlift.slice.SliceOutput in project presto by prestodb.

the class TestBlockEncodingBuffers method serialize.

private static Block serialize(BlockEncodingBuffer buffer) {
    SliceOutput output = new DynamicSliceOutput(toIntExact(buffer.getSerializedSizeInBytes()));
    buffer.serializeTo(output);
    BlockEncodingManager blockEncodingSerde = new BlockEncodingManager();
    return readBlock(blockEncodingSerde, output.slice().getInput());
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) BlockEncodingManager(com.facebook.presto.common.block.BlockEncodingManager) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput)

Example 28 with SliceOutput

use of io.airlift.slice.SliceOutput in project presto by prestodb.

the class LiteralInterpreter method toExpression.

public static Expression toExpression(Object object, Type type) {
    requireNonNull(type, "type is null");
    if (object instanceof Expression) {
        return (Expression) object;
    }
    if (object == null) {
        if (type.equals(UNKNOWN)) {
            return new NullLiteral();
        }
        return new Cast(new NullLiteral(), type.getTypeSignature().toString(), false, true);
    }
    if (type.equals(INTEGER)) {
        return new LongLiteral(object.toString());
    }
    if (type.equals(BIGINT)) {
        LongLiteral expression = new LongLiteral(object.toString());
        if (expression.getValue() >= Integer.MIN_VALUE && expression.getValue() <= Integer.MAX_VALUE) {
            return new GenericLiteral("BIGINT", object.toString());
        }
        return new LongLiteral(object.toString());
    }
    checkArgument(Primitives.wrap(type.getJavaType()).isInstance(object), "object.getClass (%s) and type.getJavaType (%s) do not agree", object.getClass(), type.getJavaType());
    if (type.equals(DOUBLE)) {
        Double value = (Double) object;
        // When changing this, don't forget about similar code for REAL below
        if (value.isNaN()) {
            return new FunctionCall(QualifiedName.of("nan"), ImmutableList.of());
        } else if (value.equals(Double.NEGATIVE_INFINITY)) {
            return ArithmeticUnaryExpression.negative(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()));
        } else if (value.equals(Double.POSITIVE_INFINITY)) {
            return new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of());
        } else {
            return new DoubleLiteral(object.toString());
        }
    }
    if (type.equals(REAL)) {
        Float value = intBitsToFloat(((Long) object).intValue());
        // WARNING for ORC predicate code as above (for double)
        if (value.isNaN()) {
            return new Cast(new FunctionCall(QualifiedName.of("nan"), ImmutableList.of()), StandardTypes.REAL);
        } else if (value.equals(Float.NEGATIVE_INFINITY)) {
            return ArithmeticUnaryExpression.negative(new Cast(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()), StandardTypes.REAL));
        } else if (value.equals(Float.POSITIVE_INFINITY)) {
            return new Cast(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()), StandardTypes.REAL);
        } else {
            return new GenericLiteral("REAL", value.toString());
        }
    }
    if (type instanceof VarcharType) {
        if (object instanceof String) {
            object = Slices.utf8Slice((String) object);
        }
        if (object instanceof Slice) {
            Slice value = (Slice) object;
            int length = SliceUtf8.countCodePoints(value);
            if (length == ((VarcharType) type).getLength()) {
                return new StringLiteral(value.toStringUtf8());
            }
            return new Cast(new StringLiteral(value.toStringUtf8()), type.getDisplayName(), false, true);
        }
        throw new IllegalArgumentException("object must be instance of Slice or String when type is VARCHAR");
    }
    if (type.equals(BOOLEAN)) {
        return new BooleanLiteral(object.toString());
    }
    if (object instanceof Block) {
        SliceOutput output = new DynamicSliceOutput(((Block) object).getSizeInBytes());
        BlockSerdeUtil.writeBlock(output, (Block) object);
        object = output.slice();
    // This if condition will evaluate to true: object instanceof Slice && !type.equals(VARCHAR)
    }
    if (object instanceof Slice) {
        // HACK: we need to serialize VARBINARY in a format that can be embedded in an expression to be
        // able to encode it in the plan that gets sent to workers.
        // We do this by transforming the in-memory varbinary into a call to from_base64(<base64-encoded value>)
        FunctionCall fromBase64 = new FunctionCall(QualifiedName.of("from_base64"), ImmutableList.of(new StringLiteral(VarbinaryFunctions.toBase64((Slice) object).toStringUtf8())));
        Signature signature = FunctionRegistry.getMagicLiteralFunctionSignature(type);
        return new FunctionCall(QualifiedName.of(signature.getName()), ImmutableList.of(fromBase64));
    }
    Signature signature = FunctionRegistry.getMagicLiteralFunctionSignature(type);
    Expression rawLiteral = toExpression(object, FunctionRegistry.typeForMagicLiteral(type));
    return new FunctionCall(QualifiedName.of(signature.getName()), ImmutableList.of(rawLiteral));
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) VarcharType(com.facebook.presto.spi.type.VarcharType) BooleanLiteral(com.facebook.presto.sql.tree.BooleanLiteral) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) ArithmeticUnaryExpression(com.facebook.presto.sql.tree.ArithmeticUnaryExpression) Expression(com.facebook.presto.sql.tree.Expression) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) Block(com.facebook.presto.spi.block.Block) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) DoubleLiteral(com.facebook.presto.sql.tree.DoubleLiteral) NullLiteral(com.facebook.presto.sql.tree.NullLiteral)

Example 29 with SliceOutput

use of io.airlift.slice.SliceOutput in project presto by prestodb.

the class VariableWidthBlockBuilder method copyPositions.

@Override
public Block copyPositions(List<Integer> positions) {
    int finalLength = positions.stream().mapToInt(this::getSliceLength).sum();
    SliceOutput newSlice = Slices.allocate(finalLength).getOutput();
    int[] newOffsets = new int[positions.size() + 1];
    boolean[] newValueIsNull = new boolean[positions.size()];
    for (int i = 0; i < positions.size(); i++) {
        int position = positions.get(i);
        if (isEntryNull(position)) {
            newValueIsNull[i] = true;
        } else {
            newSlice.appendBytes(sliceOutput.getUnderlyingSlice().getBytes(getPositionOffset(position), getSliceLength(position)));
        }
        newOffsets[i + 1] = newSlice.size();
    }
    return new VariableWidthBlock(positions.size(), newSlice.slice(), newOffsets, newValueIsNull);
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput)

Example 30 with SliceOutput

use of io.airlift.slice.SliceOutput in project presto by prestodb.

the class FixedWidthBlock method copyPositions.

@Override
public Block copyPositions(List<Integer> positions) {
    checkValidPositions(positions, positionCount);
    SliceOutput newSlice = Slices.allocate(positions.size() * fixedSize).getOutput();
    SliceOutput newValueIsNull = Slices.allocate(positions.size()).getOutput();
    for (int position : positions) {
        newSlice.writeBytes(slice, position * fixedSize, fixedSize);
        newValueIsNull.writeByte(valueIsNull.getByte(position));
    }
    return new FixedWidthBlock(fixedSize, positions.size(), newSlice.slice(), newValueIsNull.slice());
}
Also used : SliceOutput(io.airlift.slice.SliceOutput)

Aggregations

SliceOutput (io.airlift.slice.SliceOutput)46 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)27 Slice (io.airlift.slice.Slice)16 IOException (java.io.IOException)8 Test (org.testng.annotations.Test)6 Block (com.facebook.presto.common.block.Block)5 UncheckedIOException (java.io.UncheckedIOException)5 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)4 SqlType (com.facebook.presto.spi.function.SqlType)4 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)2 Page (com.facebook.presto.common.Page)2 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)2 PrestoException (com.facebook.presto.spi.PrestoException)2 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)2 SqlNullable (com.facebook.presto.spi.function.SqlNullable)2 ArithmeticUnaryExpression (com.facebook.presto.sql.tree.ArithmeticUnaryExpression)2 BooleanLiteral (com.facebook.presto.sql.tree.BooleanLiteral)2 Cast (com.facebook.presto.sql.tree.Cast)2 JsonUtil.createJsonGenerator (com.facebook.presto.util.JsonUtil.createJsonGenerator)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2