use of io.trino.spi.function.LiteralParameters 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.LiteralParameters 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.LiteralParameters 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);
}
use of io.trino.spi.function.LiteralParameters in project trino by trinodb.
the class CurrentTimestamp method shortTimestamp.
@LiteralParameters("p")
@SqlType("timestamp(p) with time zone")
public static long shortTimestamp(@LiteralParameter("p") long precision, ConnectorSession session, // need a dummy value since the type inferencer can't bind type arguments exclusively from return type
@SqlNullable @SqlType("timestamp(p) with time zone") Long dummy) {
Instant start = session.getStart();
long epochMillis = start.toEpochMilli();
if (precision < MAX_SHORT_PRECISION) {
epochMillis = round(epochMillis, (int) (MAX_SHORT_PRECISION - precision));
} else {
long nanosOfMilli = start.getNano() % NANOSECONDS_PER_MILLISECOND;
if (roundToNearest(nanosOfMilli, NANOSECONDS_PER_MILLISECOND) == NANOSECONDS_PER_MILLISECOND) {
epochMillis++;
}
}
return packDateTimeWithZone(epochMillis, session.getTimeZoneKey());
}
use of io.trino.spi.function.LiteralParameters in project trino by trinodb.
the class DateDiff method diff.
@LiteralParameters({ "x", "p" })
@SqlType(StandardTypes.BIGINT)
public static long diff(@SqlType("varchar(x)") Slice unit, @SqlType("timestamp(p) with time zone") LongTimestampWithTimeZone timestamp1, @SqlType("timestamp(p) with time zone") LongTimestampWithTimeZone timestamp2) {
long epochMillis1 = timestamp1.getEpochMillis();
long epochMillis2 = timestamp2.getEpochMillis();
ISOChronology chronology = ISOChronology.getInstanceUTC();
return getTimestampField(chronology, unit).getDifferenceAsLong(epochMillis2, epochMillis1);
}
Aggregations