use of io.prestosql.spi.function.SqlType in project hetu-core by openlookeng.
the class DateTimeFunctions method addFieldValueTimeWithTimeZone.
@Description("add the specified amount of time to the given time")
@LiteralParameters("x")
@ScalarFunction("date_add")
@SqlType(StandardTypes.TIME_WITH_TIME_ZONE)
public static long addFieldValueTimeWithTimeZone(@SqlType("varchar(x)") Slice unit, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone) {
ISOChronology chronology = unpackChronology(timeWithTimeZone);
long millis = modulo24Hour(chronology, getTimeField(chronology, unit).add(unpackMillisUtc(timeWithTimeZone), toIntExact(value)));
return updateMillisUtc(millis, timeWithTimeZone);
}
use of io.prestosql.spi.function.SqlType in project hetu-core by openlookeng.
the class DateTimeFunctions method fromISO8601Date.
@ScalarFunction("from_iso8601_date")
@LiteralParameters("x")
@SqlType(StandardTypes.DATE)
public static long fromISO8601Date(ConnectorSession session, @SqlType("varchar(x)") Slice iso8601DateTime) {
DateTimeFormatter formatter = ISODateTimeFormat.dateElementParser().withChronology(UTC_CHRONOLOGY);
DateTime dateTime = parseDateTimeHelper(formatter, iso8601DateTime.toStringUtf8());
return MILLISECONDS.toDays(dateTime.getMillis());
}
use of io.prestosql.spi.function.SqlType in project hetu-core by openlookeng.
the class ArrayRemoveFunction method remove.
@TypeParameter("E")
@SqlType("array(E)")
public Block remove(@OperatorDependency(operator = EQUAL, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle equalsFunction, @TypeParameter("E") Type type, @SqlType("array(E)") Block array, @SqlType("E") Object value) {
List<Integer> positions = new ArrayList<>();
for (int i = 0; i < array.getPositionCount(); i++) {
Object element = readNativeValue(type, array, i);
try {
if (element == null) {
positions.add(i);
continue;
}
Boolean result = (Boolean) equalsFunction.invoke(element, value);
if (result == null) {
throw new PrestoException(NOT_SUPPORTED, "array_remove does not support arrays with elements that are null or contain null");
}
if (!result) {
positions.add(i);
}
} catch (Throwable t) {
throw internalError(t);
}
}
if (array.getPositionCount() == positions.size()) {
return array;
}
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
for (int position : positions) {
type.appendTo(array, position, blockBuilder);
}
pageBuilder.declarePositions(positions.size());
return blockBuilder.getRegion(blockBuilder.getPositionCount() - positions.size(), positions.size());
}
use of io.prestosql.spi.function.SqlType in project hetu-core by openlookeng.
the class ArraySortFunction method sort.
@TypeParameter("E")
@SqlType("array(E)")
public Block sort(@OperatorDependency(operator = LESS_THAN, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle lessThanFunction, @TypeParameter("E") Type type, @SqlType("array(E)") Block block) {
int arrayLength = block.getPositionCount();
if (positions.size() < arrayLength) {
positions = Ints.asList(new int[arrayLength]);
}
for (int i = 0; i < arrayLength; i++) {
positions.set(i, i);
}
Collections.sort(positions.subList(0, arrayLength), new Comparator<Integer>() {
@Override
public int compare(Integer p1, Integer p2) {
boolean nullLeft = block.isNull(p1);
boolean nullRight = block.isNull(p2);
if (nullLeft && nullRight) {
return 0;
}
if (nullLeft) {
return 1;
}
if (nullRight) {
return -1;
}
// TODO: This could be quite slow, it should use parametric equals
return type.compareTo(block, p1, block, p2);
}
});
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
for (int i = 0; i < arrayLength; i++) {
type.appendTo(block, positions.get(i), blockBuilder);
}
pageBuilder.declarePositions(arrayLength);
return blockBuilder.getRegion(blockBuilder.getPositionCount() - arrayLength, arrayLength);
}
use of io.prestosql.spi.function.SqlType in project hetu-core by openlookeng.
the class ArrayUnionFunction method union.
@TypeParameter("E")
@SqlType("array(E)")
public static Block union(@TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
int leftArrayCount = leftArray.getPositionCount();
int rightArrayCount = rightArray.getPositionCount();
TypedSet typedSet = new TypedSet(type, leftArrayCount + rightArrayCount, "array_union");
BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(null, leftArrayCount + rightArrayCount);
appendTypedArray(leftArray, type, typedSet, distinctElementBlockBuilder);
appendTypedArray(rightArray, type, typedSet, distinctElementBlockBuilder);
return distinctElementBlockBuilder.build();
}
Aggregations