Search in sources :

Example 16 with DynamicSliceOutput

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

the class AbstractTestBlock method createGreaterValue.

private static Slice createGreaterValue(Slice expectedValue, int offset, int length) {
    DynamicSliceOutput greaterOutput = new DynamicSliceOutput(length + 1);
    greaterOutput.writeBytes(expectedValue, offset, length);
    greaterOutput.writeByte('_');
    return greaterOutput.slice();
}
Also used : DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput)

Example 17 with DynamicSliceOutput

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

the class AbstractTestHiveFileFormats method blockToSlice.

private static Slice blockToSlice(Block block) {
    // This function is strictly for testing use only
    SliceOutput sliceOutput = new DynamicSliceOutput(1000);
    BlockSerdeUtil.writeBlock(sliceOutput, block.copyRegion(0, block.getPositionCount()));
    return sliceOutput.slice();
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput)

Example 18 with DynamicSliceOutput

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

the class ParquetCompressionUtils method decompressGzip.

private static Slice decompressGzip(Slice input, int uncompressedSize) throws IOException {
    if (uncompressedSize == 0) {
        return EMPTY_SLICE;
    }
    DynamicSliceOutput sliceOutput = new DynamicSliceOutput(uncompressedSize);
    byte[] buffer = new byte[uncompressedSize];
    try (InputStream gzipInputStream = new GZIPInputStream(input.getInput(), GZIP_BUFFER_SIZE)) {
        int bytesRead;
        while ((bytesRead = gzipInputStream.read(buffer)) != -1) {
            sliceOutput.write(buffer, 0, bytesRead);
        }
        return sliceOutput.getUnderlyingSlice();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput)

Example 19 with DynamicSliceOutput

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

the class JoniRegexpFunctions method regexpReplace.

@Description("replaces substrings matching a regular expression by given string")
@ScalarFunction
@LiteralParameters({ "x", "y", "z" })
// to get the formula: x + max(x * y / 2, y) * (x + 1)
@Constraint(variable = "z", expression = "min(2147483647, x + max(x * y / 2, y) * (x + 1))")
@SqlType("varchar(z)")
public static Slice regexpReplace(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) Regex pattern, @SqlType("varchar(y)") Slice replacement) {
    Matcher matcher = pattern.matcher(source.getBytes());
    SliceOutput sliceOutput = new DynamicSliceOutput(source.length() + replacement.length() * 5);
    int lastEnd = 0;
    // nextStart is the same as lastEnd, unless the last match was zero-width. In such case, nextStart is lastEnd + 1.
    int nextStart = 0;
    while (true) {
        int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
        if (offset == -1) {
            break;
        }
        if (matcher.getEnd() == matcher.getBegin()) {
            nextStart = matcher.getEnd() + 1;
        } else {
            nextStart = matcher.getEnd();
        }
        Slice sliceBetweenReplacements = source.slice(lastEnd, matcher.getBegin() - lastEnd);
        lastEnd = matcher.getEnd();
        sliceOutput.appendBytes(sliceBetweenReplacements);
        appendReplacement(sliceOutput, source, pattern, matcher.getEagerRegion(), replacement);
    }
    sliceOutput.appendBytes(source.slice(lastEnd, source.length() - lastEnd));
    return sliceOutput.slice();
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Matcher(io.airlift.joni.Matcher) Slice(io.airlift.slice.Slice) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Constraint(com.facebook.presto.type.Constraint) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) Constraint(com.facebook.presto.type.Constraint) SqlType(com.facebook.presto.spi.function.SqlType)

Example 20 with DynamicSliceOutput

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

the class MockExchangeRequestProcessor method handle.

@Override
public Response handle(Request request) throws Exception {
    if (request.getMethod().equalsIgnoreCase("DELETE")) {
        return new TestingResponse(HttpStatus.NO_CONTENT, ImmutableListMultimap.of(), new byte[0]);
    }
    // verify we got a data size and it parses correctly
    assertTrue(!request.getHeaders().get(PrestoHeaders.PRESTO_MAX_SIZE).isEmpty());
    DataSize maxSize = DataSize.valueOf(request.getHeader(PrestoHeaders.PRESTO_MAX_SIZE));
    assertEquals(maxSize, expectedMaxSize);
    RequestLocation requestLocation = new RequestLocation(request.getUri());
    URI location = requestLocation.getLocation();
    BufferResult result = buffers.getUnchecked(location).getPages(requestLocation.getSequenceId(), maxSize);
    byte[] bytes = new byte[0];
    HttpStatus status;
    if (!result.getSerializedPages().isEmpty()) {
        DynamicSliceOutput sliceOutput = new DynamicSliceOutput(64);
        PagesSerdeUtil.writeSerializedPages(sliceOutput, result.getSerializedPages());
        bytes = sliceOutput.slice().getBytes();
        status = HttpStatus.OK;
    } else {
        status = HttpStatus.NO_CONTENT;
    }
    return new TestingResponse(status, ImmutableListMultimap.of(CONTENT_TYPE, PRESTO_PAGES, PRESTO_TASK_INSTANCE_ID, String.valueOf(result.getTaskInstanceId()), PRESTO_PAGE_TOKEN, String.valueOf(result.getToken()), PRESTO_PAGE_NEXT_TOKEN, String.valueOf(result.getNextToken()), PRESTO_BUFFER_COMPLETE, String.valueOf(result.isBufferComplete())), bytes);
}
Also used : TestingResponse(io.airlift.http.client.testing.TestingResponse) BufferResult(com.facebook.presto.execution.buffer.BufferResult) HttpStatus(io.airlift.http.client.HttpStatus) DataSize(io.airlift.units.DataSize) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) URI(java.net.URI)

Aggregations

DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)24 SliceOutput (io.airlift.slice.SliceOutput)9 Test (org.testng.annotations.Test)8 Block (com.facebook.presto.spi.block.Block)5 Slice (io.airlift.slice.Slice)4 BlockSerdeUtil.writeBlock (com.facebook.presto.block.BlockSerdeUtil.writeBlock)3 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)3 Page (com.facebook.presto.spi.Page)2 PrestoException (com.facebook.presto.spi.PrestoException)2 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)2 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)2 SqlType (com.facebook.presto.spi.function.SqlType)2 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)1 BufferResult (com.facebook.presto.execution.buffer.BufferResult)1 Signature (com.facebook.presto.metadata.Signature)1 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)1 BlockEncoding (com.facebook.presto.spi.block.BlockEncoding)1 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)1 Description (com.facebook.presto.spi.function.Description)1 Type (com.facebook.presto.spi.type.Type)1