use of com.facebook.presto.spi.function.TypeParameter in project presto by prestodb.
the class ArrayLessThanOrEqualOperator method lessThanOrEqualLong.
@TypeParameter("E")
@TypeParameterSpecialization(name = "E", nativeContainerType = long.class)
@SqlType(StandardTypes.BOOLEAN)
public static boolean lessThanOrEqualLong(@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();
}
use of com.facebook.presto.spi.function.TypeParameter in project presto by prestodb.
the class ArrayDistinctFromOperator method isDistinctFrom.
@TypeParameter("E")
@SqlType(StandardTypes.BOOLEAN)
public static boolean isDistinctFrom(@OperatorDependency(operator = IS_DISTINCT_FROM, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle function, @TypeParameter("E") Type type, @SqlType("array(E)") Block left, @IsNull boolean leftNull, @SqlType("array(E)") Block right, @IsNull boolean rightNull) {
if (leftNull != rightNull) {
return true;
}
if (leftNull) {
return false;
}
if (left.getPositionCount() != right.getPositionCount()) {
return true;
}
for (int i = 0; i < left.getPositionCount(); i++) {
Object leftValue = readNativeValue(type, left, i);
boolean leftValueNull = leftValue == null;
if (leftValueNull) {
leftValue = defaultValue(type.getJavaType());
}
Object rightValue = readNativeValue(type, right, i);
boolean rightValueNull = rightValue == null;
if (rightValueNull) {
rightValue = defaultValue(type.getJavaType());
}
try {
if ((boolean) function.invoke(leftValue, leftValueNull, rightValue, rightValueNull)) {
return true;
}
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, Error.class);
Throwables.propagateIfInstanceOf(t, PrestoException.class);
throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
}
}
return false;
}
use of com.facebook.presto.spi.function.TypeParameter in project presto by prestodb.
the class ArrayEqualOperator method equals.
@TypeParameter("E")
@SqlType(StandardTypes.BOOLEAN)
public static boolean equals(@OperatorDependency(operator = EQUAL, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle equalsFunction, @TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
if (leftArray.getPositionCount() != rightArray.getPositionCount()) {
return false;
}
for (int i = 0; i < leftArray.getPositionCount(); i++) {
checkElementNotNull(leftArray.isNull(i), ARRAY_NULL_ELEMENT_MSG);
checkElementNotNull(rightArray.isNull(i), ARRAY_NULL_ELEMENT_MSG);
Object leftElement = readNativeValue(type, leftArray, i);
Object rightElement = readNativeValue(type, rightArray, i);
try {
if (!(boolean) equalsFunction.invoke(leftElement, rightElement)) {
return false;
}
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, Error.class);
Throwables.propagateIfInstanceOf(t, PrestoException.class);
throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
}
}
return true;
}
use of com.facebook.presto.spi.function.TypeParameter in project presto by prestodb.
the class ArrayExceptFunction method except.
@TypeParameter("E")
@SqlType("array(E)")
public static Block except(@TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
int leftPositionCount = leftArray.getPositionCount();
int rightPositionCount = rightArray.getPositionCount();
if (leftPositionCount == 0) {
return leftArray;
}
TypedSet typedSet = new TypedSet(type, leftPositionCount + rightPositionCount);
BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), leftPositionCount);
for (int i = 0; i < rightPositionCount; i++) {
typedSet.add(rightArray, i);
}
for (int i = 0; i < leftPositionCount; i++) {
if (!typedSet.contains(leftArray, i)) {
typedSet.add(leftArray, i);
type.appendTo(leftArray, i, distinctElementBlockBuilder);
}
}
return distinctElementBlockBuilder.build();
}
use of com.facebook.presto.spi.function.TypeParameter in project presto by prestodb.
the class ArrayFilterFunction method filterLong.
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = long.class)
@SqlType("array(T)")
public static Block filterLong(@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++) {
Long input = null;
if (!arrayBlock.isNull(position)) {
input = elementType.getLong(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();
}
Aggregations