Search in sources :

Example 11 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 12 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)

Example 13 with DynamicSliceOutput

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

the class TestRcFileReaderManual method testNoStartSync.

@Test
public void testNoStartSync() throws Exception {
    SliceOutput output = new DynamicSliceOutput(10 * 1024);
    List<Segment> segments = ImmutableList.of(writeSegment(output, ImmutableList.of(ImmutableList.of(0, 2, 3, 4), ImmutableList.of(10, 12, 13))), writeSegment(output, ImmutableList.of(ImmutableList.of(20, 22), ImmutableList.of(30, 33), ImmutableList.of(40, 44))), writeSegment(output, ImmutableList.of(ImmutableList.of(100, 101, 102))));
    assertFileSegments(output.slice(), segments);
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Test(org.testng.annotations.Test)

Example 14 with DynamicSliceOutput

use of io.airlift.slice.DynamicSliceOutput 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 15 with DynamicSliceOutput

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

the class TestVariableWidthBlockEncoding method testRoundTrip.

@Test
public void testRoundTrip() {
    BlockBuilder expectedBlockBuilder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), 4);
    VARCHAR.writeString(expectedBlockBuilder, "alice");
    VARCHAR.writeString(expectedBlockBuilder, "bob");
    VARCHAR.writeString(expectedBlockBuilder, "charlie");
    VARCHAR.writeString(expectedBlockBuilder, "dave");
    Block expectedBlock = expectedBlockBuilder.build();
    DynamicSliceOutput sliceOutput = new DynamicSliceOutput(1024);
    BlockEncoding blockEncoding = new VariableWidthBlockEncoding();
    blockEncoding.writeBlock(sliceOutput, expectedBlock);
    Block actualBlock = blockEncoding.readBlock(sliceOutput.slice().getInput());
    assertBlockEquals(VARCHAR, actualBlock, expectedBlock);
}
Also used : DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Test(org.testng.annotations.Test)

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