use of com.facebook.presto.spi.function.TypeParameter in project presto by prestodb.
the class ArrayFilterFunction method filterBoolean.
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = boolean.class)
@SqlType("array(T)")
public static Block filterBoolean(@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++) {
Boolean input = null;
if (!arrayBlock.isNull(position)) {
input = elementType.getBoolean(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.TypeParameter in project presto by prestodb.
the class ArrayFilterFunction method filterVoid.
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = void.class)
@SqlType("array(T)")
public static Block filterVoid(@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++) {
Boolean keep;
try {
keep = (Boolean) function.invokeExact(null);
} catch (Throwable throwable) {
throw Throwables.propagate(throwable);
}
if (TRUE.equals(keep)) {
resultBuilder.appendNull();
}
}
return resultBuilder.build();
}
use of com.facebook.presto.spi.function.TypeParameter in project presto by prestodb.
the class ArrayFilterFunction method filterDouble.
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = double.class)
@SqlType("array(T)")
public static Block filterDouble(@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++) {
Double input = null;
if (!arrayBlock.isNull(position)) {
input = elementType.getDouble(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.TypeParameter in project presto by prestodb.
the class MapToMapCast method toMap.
@TypeParameter("FK")
@TypeParameter("FV")
@TypeParameter("TK")
@TypeParameter("TV")
@SqlType("map(TK,TV)")
public static Block toMap(@OperatorDependency(operator = EQUAL, returnType = StandardTypes.BOOLEAN, argumentTypes = { "TK", "TK" }) MethodHandle toKeyEqualsFunction, @OperatorDependency(operator = CAST, returnType = "TK", argumentTypes = { "FK" }) MethodHandle keyCastFunction, @OperatorDependency(operator = CAST, returnType = "TV", argumentTypes = { "FV" }) MethodHandle valueCastFunction, @TypeParameter("FK") Type fromKeyType, @TypeParameter("FV") Type fromValueType, @TypeParameter("TK") Type toKeyType, @TypeParameter("TV") Type toValueType, ConnectorSession session, @SqlType("map(FK,FV)") Block fromMap) {
// loop over all the parameter types and bind ConnectorSession if needed
// TODO: binding `ConnectorSession` should be done in function invocation framework
Class<?>[] parameterTypes = keyCastFunction.type().parameterArray();
for (int i = 0; i < parameterTypes.length; i++) {
if (parameterTypes[i] == ConnectorSession.class) {
keyCastFunction = MethodHandles.insertArguments(keyCastFunction, i, session);
break;
}
}
parameterTypes = valueCastFunction.type().parameterArray();
for (int i = 0; i < parameterTypes.length; i++) {
if (parameterTypes[i] == ConnectorSession.class) {
valueCastFunction = MethodHandles.insertArguments(valueCastFunction, i, session);
break;
}
}
TypedSet typedSet = new TypedSet(toKeyType, fromMap.getPositionCount() / 2);
BlockBuilder keyBlockBuilder = toKeyType.createBlockBuilder(new BlockBuilderStatus(), fromMap.getPositionCount() / 2);
for (int i = 0; i < fromMap.getPositionCount(); i += 2) {
Object fromKey = readNativeValue(fromKeyType, fromMap, i);
try {
Object toKey = keyCastFunction.invoke(fromKey);
if (toKey == null) {
throw new PrestoException(StandardErrorCode.INVALID_CAST_ARGUMENT, "map key is null");
}
writeNativeValue(toKeyType, keyBlockBuilder, toKey);
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, Error.class);
Throwables.propagateIfInstanceOf(t, PrestoException.class);
throw new PrestoException(StandardErrorCode.GENERIC_INTERNAL_ERROR, t);
}
}
Block keyBlock = keyBlockBuilder.build();
BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(toKeyType, toValueType), new BlockBuilderStatus(), fromMap.getPositionCount());
for (int i = 0; i < fromMap.getPositionCount(); i += 2) {
if (!typedSet.contains(keyBlock, i / 2)) {
typedSet.add(keyBlock, i / 2);
toKeyType.appendTo(keyBlock, i / 2, blockBuilder);
if (fromMap.isNull(i + 1)) {
blockBuilder.appendNull();
continue;
}
Object fromValue = readNativeValue(fromValueType, fromMap, i + 1);
try {
Object toValue = valueCastFunction.invoke(fromValue);
writeNativeValue(toValueType, blockBuilder, toValue);
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, Error.class);
Throwables.propagateIfInstanceOf(t, PrestoException.class);
throw new PrestoException(StandardErrorCode.GENERIC_INTERNAL_ERROR, t);
}
} else {
// if there are duplicated keys, fail it!
throw new PrestoException(StandardErrorCode.INVALID_CAST_ARGUMENT, "duplicate keys");
}
}
return blockBuilder.build();
}
use of com.facebook.presto.spi.function.TypeParameter in project presto by prestodb.
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);
BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), leftArrayCount + rightArrayCount);
appendTypedArray(leftArray, type, typedSet, distinctElementBlockBuilder);
appendTypedArray(rightArray, type, typedSet, distinctElementBlockBuilder);
return distinctElementBlockBuilder.build();
}
Aggregations