Search in sources :

Example 81 with Description

use of com.facebook.presto.spi.function.Description 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 82 with Description

use of com.facebook.presto.spi.function.Description in project presto by prestodb.

the class JoniRegexpFunctions method regexpExtractAll.

@Description("group(s) extracted using the given pattern")
@ScalarFunction
@LiteralParameters("x")
@SqlType("array(varchar(x))")
public static Block regexpExtractAll(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) Regex pattern, @SqlType(StandardTypes.BIGINT) long groupIndex) {
    Matcher matcher = pattern.matcher(source.getBytes());
    int nextStart = 0;
    int offset = getMatchingOffset(matcher, nextStart, source.length(), false);
    if (offset == -1) {
        return EMPTY_BLOCK;
    }
    validateGroup(groupIndex, matcher.getEagerRegion());
    ArrayList<Integer> matches = new ArrayList<>(10);
    int group = toIntExact(groupIndex);
    do {
        int beg = matcher.getBegin();
        int end = matcher.getEnd();
        if (end == beg) {
            nextStart = beg + 1;
        } else {
            nextStart = end;
        }
        Region region = matcher.getEagerRegion();
        matches.add(region.beg[group]);
        matches.add(region.end[group]);
        offset = getMatchingOffset(matcher, nextStart, source.length(), false);
    } while (offset != -1);
    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, matches.size());
    for (int i = 0; i < matches.size(); i += 2) {
        int beg = matches.get(i);
        int end = matches.get(i + 1);
        if (beg == -1 || end == -1) {
            blockBuilder.appendNull();
        } else {
            VARCHAR.writeSlice(blockBuilder, source, beg, end - beg);
        }
    }
    return blockBuilder;
}
Also used : Matcher(io.airlift.joni.Matcher) ArrayList(java.util.ArrayList) Region(io.airlift.joni.Region) Constraint(com.facebook.presto.type.Constraint) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 83 with Description

use of com.facebook.presto.spi.function.Description in project presto by prestodb.

the class IpPrefixFunctions method ipSubnetRange.

@Description("Array of smallest and largest IP address in the subnet of the given IP prefix")
@ScalarFunction("ip_subnet_range")
@SqlType("array(IPADDRESS)")
public static Block ipSubnetRange(@SqlType(StandardTypes.IPPREFIX) Slice value) {
    BlockBuilder blockBuilder = IPADDRESS.createBlockBuilder(null, 2);
    IPADDRESS.writeSlice(blockBuilder, ipSubnetMin(value));
    IPADDRESS.writeSlice(blockBuilder, ipSubnetMax(value));
    return blockBuilder.build();
}
Also used : BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 84 with Description

use of com.facebook.presto.spi.function.Description in project presto by prestodb.

the class DateTimeFunctions method lastDayOfMonthFromTimestampWithTimeZone.

@Description("last day of the month of the given timestamp")
@ScalarFunction("last_day_of_month")
@SqlType(StandardTypes.DATE)
public static long lastDayOfMonthFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone) {
    ISOChronology isoChronology = unpackChronology(timestampWithTimeZone);
    long millis = unpackMillisUtc(timestampWithTimeZone);
    // Calculate point in time corresponding to midnight (00:00) of first day of next month in the given zone.
    millis = isoChronology.monthOfYear().roundCeiling(millis + 1);
    // Convert to UTC and take the previous day
    millis = isoChronology.getZone().convertUTCToLocal(millis) - MILLISECONDS_IN_DAY;
    return MILLISECONDS.toDays(millis);
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 85 with Description

use of com.facebook.presto.spi.function.Description in project presto by prestodb.

the class HyperLogLogFunctions method scalarMerge.

@ScalarFunction("merge_hll")
@Description("merge the contents of an array of HyperLogLogs")
@SqlType(StandardTypes.HYPER_LOG_LOG)
@SqlNullable
public static Slice scalarMerge(@SqlType("array(HyperLogLog)") Block block) {
    if (block.getPositionCount() == 0) {
        return null;
    }
    HyperLogLog merged = null;
    int firstNonNullIndex = 0;
    while (firstNonNullIndex < block.getPositionCount() && block.isNull(firstNonNullIndex)) {
        firstNonNullIndex++;
    }
    if (firstNonNullIndex == block.getPositionCount()) {
        return null;
    }
    Slice initialSlice = block.getSlice(firstNonNullIndex, 0, block.getSliceLength(firstNonNullIndex));
    merged = HyperLogLog.newInstance(initialSlice);
    for (int i = firstNonNullIndex; i < block.getPositionCount(); i++) {
        Slice currentSlice = block.getSlice(i, 0, block.getSliceLength(i));
        if (!block.isNull(i)) {
            merged.mergeWith(HyperLogLog.newInstance(currentSlice));
        }
    }
    return merged.serialize();
}
Also used : Slice(io.airlift.slice.Slice) HyperLogLog(com.facebook.airlift.stats.cardinality.HyperLogLog) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

Description (com.facebook.presto.spi.function.Description)105 SqlType (com.facebook.presto.spi.function.SqlType)103 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)101 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)41 SqlNullable (com.facebook.presto.spi.function.SqlNullable)37 OGCGeometry.createFromEsriGeometry (com.esri.core.geometry.ogc.OGCGeometry.createFromEsriGeometry)20 GeometryUtils.jsonFromJtsGeometry (com.facebook.presto.geospatial.GeometryUtils.jsonFromJtsGeometry)20 GeometryUtils.wktFromJtsGeometry (com.facebook.presto.geospatial.GeometryUtils.wktFromJtsGeometry)20 Geometry (org.locationtech.jts.geom.Geometry)20 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)19 PrestoException (com.facebook.presto.spi.PrestoException)18 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)15 Slice (io.airlift.slice.Slice)15 SqlScalarFunction (com.facebook.presto.metadata.SqlScalarFunction)14 Constraint (com.facebook.presto.type.Constraint)14 DecimalOperators.modulusScalarFunction (com.facebook.presto.type.DecimalOperators.modulusScalarFunction)13 Point (com.esri.core.geometry.Point)10 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)7 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)7 Envelope (com.esri.core.geometry.Envelope)6