Search in sources :

Example 1 with LiteralParameters

use of io.prestosql.spi.function.LiteralParameters in project hetu-core by openlookeng.

the class DataSizeFunctions method parsePrestoDataSize.

@Description("converts data size string to bytes")
@ScalarFunction("parse_presto_data_size")
@LiteralParameters("x")
@SqlType("decimal(38,0)")
public static Slice parsePrestoDataSize(@SqlType("varchar(x)") Slice input) {
    String dataSize = input.toStringUtf8();
    int valueLength = 0;
    for (int i = 0; i < dataSize.length(); i++) {
        char c = dataSize.charAt(i);
        if (isDigit(c) || c == '.') {
            valueLength++;
        } else {
            break;
        }
    }
    if (valueLength == 0) {
        throw invalidDataSize(dataSize);
    }
    BigDecimal value = parseValue(dataSize.substring(0, valueLength), dataSize);
    Unit unit = Unit.parse(dataSize.substring(valueLength), dataSize);
    BigInteger bytes = value.multiply(unit.getFactor()).toBigInteger();
    try {
        return encodeUnscaledValue(bytes);
    } catch (ArithmeticException e) {
        throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("Value out of range: '%s' ('%sB')", dataSize, bytes));
    }
}
Also used : BigInteger(java.math.BigInteger) PrestoException(io.prestosql.spi.PrestoException) BigDecimal(java.math.BigDecimal) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Example 2 with LiteralParameters

use of io.prestosql.spi.function.LiteralParameters in project hetu-core by openlookeng.

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(io.prestosql.type.Constraint) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) LiteralParameters(io.prestosql.spi.function.LiteralParameters) Constraint(io.prestosql.type.Constraint) SqlType(io.prestosql.spi.function.SqlType)

Example 3 with LiteralParameters

use of io.prestosql.spi.function.LiteralParameters in project hetu-core by openlookeng.

the class JoniRegexpFunctions method regexpExtract.

@SqlNullable
@Description("returns regex group of extracted string with a pattern")
@ScalarFunction
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice regexpExtract(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) Regex pattern, @SqlType(StandardTypes.BIGINT) long groupIndex) {
    Matcher matcher = pattern.matcher(source.getBytes());
    validateGroup(groupIndex, matcher.getEagerRegion());
    int group = toIntExact(groupIndex);
    int offset = matcher.search(0, source.length(), Option.DEFAULT);
    if (offset == -1) {
        return null;
    }
    Region region = matcher.getEagerRegion();
    int beg = region.beg[group];
    int end = region.end[group];
    if (beg == -1) {
        // end == -1 must be true
        return null;
    }
    Slice slice = source.slice(beg, end - beg);
    return slice;
}
Also used : Matcher(io.airlift.joni.Matcher) Slice(io.airlift.slice.Slice) Region(io.airlift.joni.Region) Constraint(io.prestosql.type.Constraint) SqlNullable(io.prestosql.spi.function.SqlNullable) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Example 4 with LiteralParameters

use of io.prestosql.spi.function.LiteralParameters in project hetu-core by openlookeng.

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());
    // that will be passed to the lambda function.
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    int groupCount = pattern.numberOfCaptures();
    int appendPosition = 0;
    int nextStart;
    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();
            }
        }
        pageBuilder.declarePositions(groupCount);
        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(io.prestosql.spi.block.Block) BlockBuilder(io.prestosql.spi.block.BlockBuilder) SqlNullable(io.prestosql.spi.function.SqlNullable) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Example 5 with LiteralParameters

use of io.prestosql.spi.function.LiteralParameters in project hetu-core by openlookeng.

the class JsonOperators method castToVarchar.

@ScalarOperator(CAST)
@SqlNullable
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice castToVarchar(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Slice result = currentTokenAsVarchar(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to VARCHAR");
        return result;
    } catch (IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, String.format("Cannot cast '%s' to %s", json.toStringUtf8(), VARCHAR), e);
    }
}
Also used : Slice(io.airlift.slice.Slice) JsonCastException(io.prestosql.util.JsonCastException) PrestoException(io.prestosql.spi.PrestoException) IOException(java.io.IOException) JsonUtil.createJsonParser(io.prestosql.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(io.prestosql.spi.function.ScalarOperator) SqlNullable(io.prestosql.spi.function.SqlNullable) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Aggregations

LiteralParameters (io.prestosql.spi.function.LiteralParameters)25 SqlType (io.prestosql.spi.function.SqlType)24 ScalarFunction (io.prestosql.spi.function.ScalarFunction)20 Description (io.prestosql.spi.function.Description)15 Constraint (io.prestosql.type.Constraint)14 Slice (io.airlift.slice.Slice)10 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)8 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)8 PrestoException (io.prestosql.spi.PrestoException)7 SqlNullable (io.prestosql.spi.function.SqlNullable)7 Matcher (io.airlift.joni.Matcher)6 BlockBuilder (io.prestosql.spi.block.BlockBuilder)5 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)4 SliceOutput (io.airlift.slice.SliceOutput)4 JsonParser (com.fasterxml.jackson.core.JsonParser)3 Region (io.airlift.joni.Region)3 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)3 JsonUtil.createJsonParser (io.prestosql.util.JsonUtil.createJsonParser)3 IOException (java.io.IOException)3 Block (io.prestosql.spi.block.Block)2