use of io.trino.spi.function.SqlType in project trino by trinodb.
the class VarbinaryFunctions method reverse.
@Description("Reverse a given varbinary")
@ScalarFunction("reverse")
@SqlType(StandardTypes.VARBINARY)
public static Slice reverse(@SqlType("varbinary") Slice inputSlice) {
if (inputSlice.length() == 0) {
return EMPTY_SLICE;
}
int length = inputSlice.length();
Slice reverse = Slices.allocate(length);
for (int i = 0; i < length; i++) {
reverse.setByte(i, inputSlice.getByte((length - 1) - i));
}
return reverse;
}
use of io.trino.spi.function.SqlType in project trino by trinodb.
the class FormatDateTime method format.
@LiteralParameters({ "x", "p" })
@SqlType(StandardTypes.VARCHAR)
public static Slice format(ConnectorSession session, @SqlType("timestamp(p)") long timestamp, @SqlType("varchar(x)") Slice formatString) {
// TODO: currently, date formatting only supports up to millis, so we round to that unit
timestamp = scaleEpochMicrosToMillis(round(timestamp, 3));
if (datetimeFormatSpecifiesZone(formatString)) {
// Timezone is unknown for TIMESTAMP w/o TZ so it cannot be printed out.
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "format_datetime for TIMESTAMP type, cannot use 'Z' nor 'z' in format, as this type does not contain TZ information");
}
ISOChronology chronology = ISOChronology.getInstanceUTC();
try {
return utf8Slice(DateTimeFormat.forPattern(formatString.toStringUtf8()).withChronology(chronology).withLocale(session.getLocale()).print(timestamp));
} catch (Exception e) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, e);
}
}
use of io.trino.spi.function.SqlType in project trino by trinodb.
the class SequenceIntervalDayToSecond method sequence.
@LiteralParameters("p")
@SqlType("array(timestamp(p))")
public static Block sequence(@SqlType("timestamp(p)") LongTimestamp start, @SqlType("timestamp(p)") LongTimestamp stop, @SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long step) {
// scale to micros
step = multiplyExact(step, MICROSECONDS_PER_MILLISECOND);
long startMicros = start.getEpochMicros();
long stopMicros = stop.getEpochMicros();
checkValidStep(startMicros, stopMicros, step);
int length = toIntExact((stopMicros - startMicros) / step + 1L);
checkMaxEntry(length);
BlockBuilder blockBuilder = LONG_TYPE.createBlockBuilder(null, length);
for (long i = 0, epochMicros = startMicros; i < length; ++i, epochMicros += step) {
writeLongTimestamp(blockBuilder, epochMicros, start.getPicosOfMicro());
}
return blockBuilder.build();
}
use of io.trino.spi.function.SqlType in project trino by trinodb.
the class SequenceIntervalYearToMonth method sequence.
@LiteralParameters("p")
@SqlType("array(timestamp(p))")
public static Block sequence(@SqlType("timestamp(p)") long start, @SqlType("timestamp(p)") long stop, @SqlType(StandardTypes.INTERVAL_YEAR_TO_MONTH) long step) {
checkValidStep(start, stop, step);
int length = toIntExact(DateDiff.diff(MONTH, start, stop) / step + 1);
checkMaxEntry(length);
BlockBuilder blockBuilder = SHORT_TYPE.createBlockBuilder(null, length);
int offset = 0;
for (int i = 0; i < length; ++i) {
long value = TimestampPlusIntervalYearToMonth.add(start, offset);
SHORT_TYPE.writeLong(blockBuilder, value);
offset += step;
}
return blockBuilder.build();
}
use of io.trino.spi.function.SqlType in project trino by trinodb.
the class WithTimeZone method shortPrecision.
@LiteralParameters({ "x", "p" })
@SqlType("timestamp(p) with time zone")
public static long shortPrecision(@LiteralParameter("p") long precision, @SqlType("timestamp(p)") long timestamp, @SqlType("varchar(x)") Slice zoneId) {
verify(precision <= 3, "Expected precision <= 3");
TimeZoneKey toTimeZoneKey;
try {
toTimeZoneKey = getTimeZoneKey(zoneId.toStringUtf8());
} catch (TimeZoneNotSupportedException e) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("'%s' is not a valid time zone", zoneId.toStringUtf8()));
}
DateTimeZone toDateTimeZone = getDateTimeZone(toTimeZoneKey);
return packDateTimeWithZone(UTC.getMillisKeepLocal(toDateTimeZone, scaleEpochMicrosToMillis(timestamp)), toTimeZoneKey);
}
Aggregations