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();
}
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;
}
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();
}
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);
}
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();
}
Aggregations