Search in sources :

Example 1 with SliceOutput

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

the class PagesSerde method serialize.

public SerializedPage serialize(Page page) {
    // block length is an int
    SliceOutput serializationBuffer = new DynamicSliceOutput(toIntExact((page.getSizeInBytes() + Integer.BYTES)));
    writeRawPage(page, serializationBuffer, blockEncodingSerde);
    if (!compressor.isPresent()) {
        return new SerializedPage(serializationBuffer.slice(), UNCOMPRESSED, page.getPositionCount(), serializationBuffer.size());
    }
    int maxCompressedLength = maxCompressedLength(serializationBuffer.size());
    byte[] compressionBuffer = new byte[maxCompressedLength];
    int actualCompressedLength = compressor.get().compress(serializationBuffer.slice().getBytes(), 0, serializationBuffer.size(), compressionBuffer, 0, maxCompressedLength);
    if (((1.0 * actualCompressedLength) / serializationBuffer.size()) > MINIMUM_COMPRESSION_RATIO) {
        return new SerializedPage(serializationBuffer.slice(), UNCOMPRESSED, page.getPositionCount(), serializationBuffer.size());
    }
    return new SerializedPage(Slices.copyOf(Slices.wrappedBuffer(compressionBuffer, 0, actualCompressedLength)), COMPRESSED, page.getPositionCount(), serializationBuffer.size());
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput)

Example 2 with SliceOutput

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

the class VariableWidthBlock method copyPositions.

@Override
public Block copyPositions(List<Integer> positions) {
    checkValidPositions(positions, positionCount);
    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(slice.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)

Example 3 with SliceOutput

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

the class StringEncoding method unescape.

@SuppressWarnings("AssignmentToForLoopParameter")
private static ColumnData unescape(ColumnData columnData, byte escapeByte) {
    Slice slice = columnData.getSlice();
    // does slice contain escape byte
    if (slice.indexOfByte(escapeByte) < 0) {
        return columnData;
    }
    Slice newSlice = Slices.allocate(slice.length());
    SliceOutput output = newSlice.getOutput();
    int[] newOffsets = new int[columnData.rowCount() + 1];
    for (int row = 0; row < columnData.rowCount(); row++) {
        int offset = columnData.getOffset(row);
        int length = columnData.getLength(row);
        for (int i = 0; i < length; i++) {
            byte value = slice.getByte(offset + i);
            if (value == escapeByte && i + 1 < length) {
                // read byte after escape
                i++;
                value = slice.getByte(offset + i);
            }
            output.write(value);
        }
        newOffsets[row + 1] = output.size();
    }
    return new ColumnData(newOffsets, output.slice());
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) Slice(io.airlift.slice.Slice) ColumnData(com.facebook.presto.rcfile.ColumnData)

Example 4 with SliceOutput

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

the class TestByteArrayStream method test.

@Test
public void test() throws IOException {
    List<List<Slice>> groups = new ArrayList<>();
    for (int groupIndex = 0; groupIndex < 3; groupIndex++) {
        List<Slice> group = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            Slice value = Slices.allocate(8);
            SliceOutput output = value.getOutput();
            output.writeInt(groupIndex);
            output.writeInt(i);
            group.add(value);
        }
        groups.add(group);
    }
    testWriteValue(groups);
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) Slice(io.airlift.slice.Slice) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ByteArrayStreamCheckpoint(com.facebook.presto.orc.checkpoint.ByteArrayStreamCheckpoint) Test(org.testng.annotations.Test)

Example 5 with SliceOutput

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

the class TestLongDecode method testVInt.

@Test
public void testVInt() throws Exception {
    Slice slice = Slices.allocate(100);
    SliceOutput output = slice.getOutput();
    assertVIntRoundTrip(output, 0);
    assertVIntRoundTrip(output, 1);
    assertVIntRoundTrip(output, -1);
    assertVIntRoundTrip(output, Integer.MAX_VALUE);
    assertVIntRoundTrip(output, Integer.MAX_VALUE + 1L);
    assertVIntRoundTrip(output, Integer.MAX_VALUE - 1L);
    assertVIntRoundTrip(output, Integer.MIN_VALUE);
    assertVIntRoundTrip(output, Integer.MIN_VALUE + 1L);
    assertVIntRoundTrip(output, Integer.MIN_VALUE - 1L);
    assertVIntRoundTrip(output, Long.MAX_VALUE);
    assertVIntRoundTrip(output, Long.MAX_VALUE - 1);
    assertVIntRoundTrip(output, Long.MIN_VALUE + 1);
    for (int value = -100_000; value < 100_000; value++) {
        assertVIntRoundTrip(output, value);
    }
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) Slice(io.airlift.slice.Slice) Test(org.testng.annotations.Test)

Aggregations

SliceOutput (io.airlift.slice.SliceOutput)45 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)27 Slice (io.airlift.slice.Slice)15 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