Search in sources :

Example 36 with SliceOutput

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

the class FeatureUnitNormalizer method getSerializedData.

@Override
public byte[] getSerializedData() {
    // Serialization format is (<key:int><min:double><max:double>)*
    SliceOutput output = Slices.allocate((SizeOf.SIZE_OF_INT + 2 * SizeOf.SIZE_OF_DOUBLE) * mins.size()).getOutput();
    for (int key : mins.keySet()) {
        output.appendInt(key);
        output.appendDouble(mins.get(key));
        output.appendDouble(maxs.get(key));
    }
    return output.slice().getBytes();
}
Also used : SliceOutput(io.airlift.slice.SliceOutput)

Example 37 with SliceOutput

use of io.airlift.slice.SliceOutput 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 = getMatchingOffset(matcher, nextStart, source.length());
        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 38 with SliceOutput

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

the class MapToJsonCast method toJson.

@UsedByGeneratedCode
public static Slice toJson(ObjectKeyProvider provider, JsonGeneratorWriter writer, SqlFunctionProperties properties, Block block) {
    try {
        Map<String, Integer> orderedKeyToValuePosition = new TreeMap<>();
        for (int i = 0; i < block.getPositionCount(); i += 2) {
            String objectKey = provider.getObjectKey(block, i);
            orderedKeyToValuePosition.put(objectKey, i + 1);
        }
        SliceOutput output = new DynamicSliceOutput(40);
        try (JsonGenerator jsonGenerator = createJsonGenerator(JSON_FACTORY, output)) {
            jsonGenerator.writeStartObject();
            for (Map.Entry<String, Integer> entry : orderedKeyToValuePosition.entrySet()) {
                jsonGenerator.writeFieldName(entry.getKey());
                writer.writeJsonValue(jsonGenerator, block, entry.getValue(), properties);
            }
            jsonGenerator.writeEndObject();
        }
        return output.slice();
    } catch (IOException e) {
        throwIfUnchecked(e);
        throw new RuntimeException(e);
    }
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonUtil.createJsonGenerator(com.facebook.presto.util.JsonUtil.createJsonGenerator) IOException(java.io.IOException) TreeMap(java.util.TreeMap) Map(java.util.Map) TreeMap(java.util.TreeMap) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 39 with SliceOutput

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

the class ArrayToJsonCast method toJson.

public static Slice toJson(JsonGeneratorWriter writer, SqlFunctionProperties properties, Block block) {
    try {
        SliceOutput output = new DynamicSliceOutput(40);
        try (JsonGenerator jsonGenerator = createJsonGenerator(JSON_FACTORY, output)) {
            jsonGenerator.writeStartArray();
            for (int i = 0; i < block.getPositionCount(); i++) {
                writer.writeJsonValue(jsonGenerator, block, i, properties);
            }
            jsonGenerator.writeEndArray();
        }
        return output.slice();
    } catch (IOException e) {
        throwIfUnchecked(e);
        throw new RuntimeException(e);
    }
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonUtil.createJsonGenerator(com.facebook.presto.util.JsonUtil.createJsonGenerator) IOException(java.io.IOException)

Example 40 with SliceOutput

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

the class JoniRegexpReplaceLambdaFunction method regexpReplace.

@LiteralParameters("x")
@SqlType("varchar")
@SqlNullable
public Slice regexpReplace(@SqlType("varchar") Slice source, @SqlType(JoniRegexpType.NAME) Regex pattern, @SqlType("function(array(varchar), varchar(x))") UnaryFunctionInterface replaceFunction) {
    // If there is no match we can simply return the original source without doing copy.
    Matcher matcher = pattern.matcher(source.getBytes());
    if (matcher.search(0, source.length(), Option.DEFAULT) == -1) {
        return source;
    }
    SliceOutput output = new DynamicSliceOutput(source.length());
    // Prepare a BlockBuilder that will be used to create the target block
    // that will be passed to the lambda function.
    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 0);
    int groupCount = pattern.numberOfCaptures();
    int appendPosition = 0;
    int nextStart = 0;
    do {
        // In such case, nextStart is last appendPosition + 1.
        if (matcher.getEnd() == matcher.getBegin()) {
            nextStart = matcher.getEnd() + 1;
        } else {
            nextStart = matcher.getEnd();
        }
        // Append the un-matched part
        Slice unmatched = source.slice(appendPosition, matcher.getBegin() - appendPosition);
        appendPosition = matcher.getEnd();
        output.appendBytes(unmatched);
        // Append the capturing groups to the target block that will be passed to lambda
        Region matchedRegion = matcher.getEagerRegion();
        for (int i = 1; i <= groupCount; i++) {
            // Add to the block builder if the matched region is not null. In Joni null is represented as [-1, -1]
            if (matchedRegion.beg[i] >= 0 && matchedRegion.end[i] >= 0) {
                VARCHAR.writeSlice(blockBuilder, source, matchedRegion.beg[i], matchedRegion.end[i] - matchedRegion.beg[i]);
            } else {
                blockBuilder.appendNull();
            }
        }
        Block target = blockBuilder.getRegion(blockBuilder.getPositionCount() - groupCount, groupCount);
        // Call the lambda function to replace the block, and append the result to output
        Slice replaced = (Slice) replaceFunction.apply(target);
        if (replaced == null) {
            // replacing a substring with null (unknown) makes the entire string null
            return null;
        }
        output.appendBytes(replaced);
    } while (matcher.search(nextStart, source.length(), Option.DEFAULT) != -1);
    // Append the last un-matched part
    output.writeBytes(source, appendPosition, source.length() - appendPosition);
    return output.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) Region(io.airlift.joni.Region) Block(com.facebook.presto.common.block.Block) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) SqlNullable(com.facebook.presto.spi.function.SqlNullable) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

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