use of io.trino.spi.function.LiteralParameters in project trino by trinodb.
the class TimestampWithTimeZoneToTimeWithTimeZoneCast method longToLong.
@LiteralParameters({ "sourcePrecision", "targetPrecision" })
@SqlType("time(targetPrecision) with time zone")
public static LongTimeWithTimeZone longToLong(@LiteralParameter("targetPrecision") long targetPrecision, @SqlType("timestamp(sourcePrecision) with time zone") LongTimestampWithTimeZone timestamp) {
// source precision is > 3
// target precision is > 9
TimeZoneKey zoneKey = getTimeZoneKey(timestamp.getTimeZoneKey());
long epochMillis = getChronology(zoneKey).getZone().convertUTCToLocal(timestamp.getEpochMillis());
// combine epochMillis with picosOfMilli from the timestamp. We compute modulo 24 to avoid overflow when rescaling epocMilli to picoseconds
long picos = rescale(floorMod(epochMillis, MILLISECONDS_PER_DAY), 3, 12) + timestamp.getPicosOfMilli();
picos = round(picos, (int) (12 - targetPrecision));
return new LongTimeWithTimeZone(floorMod(picos, PICOSECONDS_PER_DAY), DateTimes.getOffsetMinutes(Instant.ofEpochMilli(epochMillis), zoneKey));
}
use of io.trino.spi.function.LiteralParameters in project trino by trinodb.
the class TimestampWithTimeZoneToTimeWithTimeZoneCast method shortToShort.
@LiteralParameters({ "sourcePrecision", "targetPrecision" })
@SqlType("time(targetPrecision) with time zone")
public static long shortToShort(@LiteralParameter("targetPrecision") long targetPrecision, @SqlType("timestamp(sourcePrecision) with time zone") long packedTimestamp) {
// source precision is <= 3
// target precision is <= 9
TimeZoneKey zoneKey = unpackZoneKey(packedTimestamp);
long epochMillis = getChronology(zoneKey).getZone().convertUTCToLocal(unpackMillisUtc(packedTimestamp));
if (targetPrecision <= 3) {
epochMillis = round(epochMillis, (int) (3 - targetPrecision));
}
long nanos = rescale(floorMod(epochMillis, MILLISECONDS_PER_DAY), 3, 9);
return packTimeWithTimeZone(nanos, DateTimes.getOffsetMinutes(Instant.ofEpochMilli(epochMillis), zoneKey));
}
use of io.trino.spi.function.LiteralParameters in project trino by trinodb.
the class ApproximateSetAggregation method input.
@InputFunction
@LiteralParameters("x")
public static void input(@AggregationState HyperLogLogState state, @SqlType("varchar(x)") Slice value) {
HyperLogLog hll = getOrCreateHyperLogLog(state);
state.addMemoryUsage(-hll.estimatedInMemorySize());
hll.add(value);
state.addMemoryUsage(hll.estimatedInMemorySize());
}
use of io.trino.spi.function.LiteralParameters in project trino by trinodb.
the class JoniRegexpFunctions method regexpPosition.
@ScalarFunction
@Description("Returns the index of the n-th matched substring starting from the specified position")
@LiteralParameters("x")
@SqlType(StandardTypes.INTEGER)
public static long regexpPosition(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern, @SqlType(StandardTypes.INTEGER) long start, @SqlType(StandardTypes.INTEGER) long occurrence) {
// start position cannot be smaller than 1
if (start < 1) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "start position cannot be smaller than 1");
}
// occurrence cannot be smaller than 1
if (occurrence < 1) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "occurrence cannot be smaller than 1");
}
// returns -1 if start is greater than the length of source
if (start > SliceUtf8.countCodePoints(source)) {
return -1;
}
Matcher matcher = pattern.matcher(source.getBytes());
long count = 0;
// convert char position to byte position
// subtract 1 because codePointCount starts from zero
int nextStart = SliceUtf8.offsetOfCodePoint(source, (int) start - 1);
while (true) {
int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
// Check whether offset is negative, offset is -1 if no pattern was found or -2 if process was interrupted
if (offset < 0) {
return -1;
}
if (++count == occurrence) {
// Plus 1 because position returned start from 1
return SliceUtf8.countCodePoints(source, 0, matcher.getBegin()) + 1;
}
nextStart = getNextStart(source, matcher);
}
}
use of io.trino.spi.function.LiteralParameters in project trino by trinodb.
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) JoniRegexp 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;
}
nextStart = getNextStart(source, matcher);
Slice sliceBetweenReplacements = source.slice(lastEnd, matcher.getBegin() - lastEnd);
lastEnd = matcher.getEnd();
sliceOutput.appendBytes(sliceBetweenReplacements);
appendReplacement(sliceOutput, source, pattern.regex(), matcher.getEagerRegion(), replacement);
}
sliceOutput.appendBytes(source.slice(lastEnd, source.length() - lastEnd));
return sliceOutput.slice();
}
Aggregations