use of com.facebook.presto.spi.function.TypeParameterSpecialization in project presto by prestodb.
the class ArrayFilterFunction method filterBlock.
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = Block.class)
@SqlType("array(T)")
public static Block filterBlock(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block arrayBlock, @SqlType("function(T, boolean)") MethodHandle function) {
int positionCount = arrayBlock.getPositionCount();
BlockBuilder resultBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), positionCount);
for (int position = 0; position < positionCount; position++) {
Block input = null;
if (!arrayBlock.isNull(position)) {
input = (Block) elementType.getObject(arrayBlock, position);
}
Boolean keep;
try {
keep = (Boolean) function.invokeExact(input);
} catch (Throwable throwable) {
throw Throwables.propagate(throwable);
}
if (TRUE.equals(keep)) {
elementType.appendTo(arrayBlock, position, resultBuilder);
}
}
return resultBuilder.build();
}
use of com.facebook.presto.spi.function.TypeParameterSpecialization in project presto by prestodb.
the class ArrayFilterFunction method filterSlice.
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = Slice.class)
@SqlType("array(T)")
public static Block filterSlice(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block arrayBlock, @SqlType("function(T, boolean)") MethodHandle function) {
int positionCount = arrayBlock.getPositionCount();
BlockBuilder resultBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), positionCount);
for (int position = 0; position < positionCount; position++) {
Slice input = null;
if (!arrayBlock.isNull(position)) {
input = elementType.getSlice(arrayBlock, position);
}
Boolean keep;
try {
keep = (Boolean) function.invokeExact(input);
} catch (Throwable throwable) {
throw Throwables.propagate(throwable);
}
if (TRUE.equals(keep)) {
elementType.appendTo(arrayBlock, position, resultBuilder);
}
}
return resultBuilder.build();
}
use of com.facebook.presto.spi.function.TypeParameterSpecialization in project presto by prestodb.
the class ArrayGreaterThanOperator method greaterThanLong.
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = long.class)
@SqlType(StandardTypes.BOOLEAN)
public static boolean greaterThanLong(@OperatorDependency(operator = GREATER_THAN, returnType = StandardTypes.BOOLEAN, argumentTypes = { "T", "T" }) MethodHandle greaterThanFunction, @TypeParameter("T") Type type, @SqlType("array(T)") Block leftArray, @SqlType("array(T)") Block rightArray) {
int len = Math.min(leftArray.getPositionCount(), rightArray.getPositionCount());
int index = 0;
while (index < len) {
checkElementNotNull(leftArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
checkElementNotNull(rightArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
long leftElement = type.getLong(leftArray, index);
long rightElement = type.getLong(rightArray, index);
try {
if ((boolean) greaterThanFunction.invokeExact(leftElement, rightElement)) {
return true;
}
if ((boolean) greaterThanFunction.invokeExact(rightElement, leftElement)) {
return false;
}
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, Error.class);
Throwables.propagateIfInstanceOf(t, PrestoException.class);
throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
}
index++;
}
return leftArray.getPositionCount() > rightArray.getPositionCount();
}
use of com.facebook.presto.spi.function.TypeParameterSpecialization in project presto by prestodb.
the class ArrayLessThanOperator method lessThanLong.
@TypeParameter("E")
@TypeParameterSpecialization(name = "E", nativeContainerType = long.class)
@SqlType(StandardTypes.BOOLEAN)
public static boolean lessThanLong(@OperatorDependency(operator = LESS_THAN, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle lessThanFunction, @TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
int len = Math.min(leftArray.getPositionCount(), rightArray.getPositionCount());
int index = 0;
while (index < len) {
checkElementNotNull(leftArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
checkElementNotNull(rightArray.isNull(index), ARRAY_NULL_ELEMENT_MSG);
long leftElement = type.getLong(leftArray, index);
long rightElement = type.getLong(rightArray, index);
try {
if ((boolean) lessThanFunction.invokeExact(leftElement, rightElement)) {
return true;
}
if ((boolean) lessThanFunction.invokeExact(rightElement, leftElement)) {
return false;
}
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, Error.class);
Throwables.propagateIfInstanceOf(t, PrestoException.class);
throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
}
index++;
}
return leftArray.getPositionCount() < rightArray.getPositionCount();
}
Aggregations