use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.
the class ArrayPositionFunction method arrayPosition.
@TypeParameter("T")
@SqlType(StandardTypes.BIGINT)
public static long arrayPosition(@TypeParameter("T") Type type, @OperatorDependency(operator = EQUAL, returnType = StandardTypes.BOOLEAN, argumentTypes = { "T", "T" }) MethodHandle equalMethodHandle, @SqlType("array(T)") Block array, @SqlType("T") Slice element) {
int size = array.getPositionCount();
for (int i = 0; i < size; i++) {
if (!array.isNull(i)) {
Slice arrayValue = type.getSlice(array, i);
try {
Boolean result = (Boolean) equalMethodHandle.invokeExact(arrayValue, element);
checkNotIndeterminate(result);
if (result) {
// result is 1-based (instead of 0)
return i + 1;
}
} catch (Throwable t) {
throw internalError(t);
}
}
}
return 0;
}
use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.
the class ArrayReverseFunction method reverse.
@TypeParameter("E")
@SqlType("array(E)")
public Block reverse(@TypeParameter("E") Type type, @SqlType("array(E)") Block block) {
int arrayLength = block.getPositionCount();
if (arrayLength < 2) {
return block;
}
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
for (int i = arrayLength - 1; i >= 0; i--) {
type.appendTo(block, i, blockBuilder);
}
pageBuilder.declarePositions(arrayLength);
return blockBuilder.getRegion(blockBuilder.getPositionCount() - arrayLength, arrayLength);
}
use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.
the class ArrayShuffleFunction method shuffle.
@TypeParameter("E")
@SqlType("array(E)")
public Block shuffle(@TypeParameter("E") Type type, @SqlType("array(E)") Block block) {
int length = block.getPositionCount();
if (positions.length < length) {
positions = new int[length];
}
for (int i = 0; i < length; i++) {
positions[i] = i;
}
// Randomly swap a pair of positions
for (int i = length - 1; i > 0; i--) {
int index = ThreadLocalRandom.current().nextInt(i + 1);
int swap = positions[i];
positions[i] = positions[index];
positions[index] = swap;
}
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
for (int i = 0; i < length; i++) {
type.appendTo(block, positions[i], blockBuilder);
}
pageBuilder.declarePositions(length);
return blockBuilder.getRegion(blockBuilder.getPositionCount() - length, length);
}
use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.
the class ArrayIntersectFunction method intersect.
@TypeParameter("E")
@SqlType("array(E)")
public Block intersect(@TypeParameter("E") Type type, @OperatorDependency(operator = IS_DISTINCT_FROM, returnType = BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle elementIsDistinctFrom, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
Block inputLeftArray = leftArray;
Block inputRightArray = rightArray;
if (inputLeftArray.getPositionCount() < inputRightArray.getPositionCount()) {
Block tempArray = inputLeftArray;
inputLeftArray = inputRightArray;
inputRightArray = tempArray;
}
int leftPositionCount = inputLeftArray.getPositionCount();
int rightPositionCount = inputRightArray.getPositionCount();
if (rightPositionCount == 0) {
return inputRightArray;
}
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
TypedSet rightTypedSet = new TypedSet(type, elementIsDistinctFrom, rightPositionCount, "array_intersect");
for (int i = 0; i < rightPositionCount; i++) {
rightTypedSet.add(inputRightArray, i);
}
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
// The intersected set can have at most rightPositionCount elements
TypedSet intersectTypedSet = new TypedSet(type, Optional.of(elementIsDistinctFrom), blockBuilder, rightPositionCount, "array_intersect");
for (int i = 0; i < leftPositionCount; i++) {
if (rightTypedSet.contains(inputLeftArray, i)) {
intersectTypedSet.add(inputLeftArray, i);
}
}
pageBuilder.declarePositions(intersectTypedSet.size());
return blockBuilder.getRegion(blockBuilder.getPositionCount() - intersectTypedSet.size(), intersectTypedSet.size());
}
use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.
the class ArrayCombinationsFunction method combinations.
@TypeParameter("T")
@SqlType("array(array(T))")
public static Block combinations(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block array, @SqlType(INTEGER) long n) {
int arrayLength = array.getPositionCount();
int combinationLength = toIntExact(n);
checkCondition(combinationLength >= 0, INVALID_FUNCTION_ARGUMENT, "combination size must not be negative: %s", combinationLength);
checkCondition(combinationLength <= MAX_COMBINATION_LENGTH, INVALID_FUNCTION_ARGUMENT, "combination size must not exceed %s: %s", MAX_COMBINATION_LENGTH, combinationLength);
ArrayType arrayType = new ArrayType(elementType);
if (combinationLength > arrayLength) {
return arrayType.createBlockBuilder(new PageBuilderStatus().createBlockBuilderStatus(), 0).build();
}
int combinationCount = combinationCount(arrayLength, combinationLength);
checkCondition(combinationCount * (long) combinationLength <= MAX_RESULT_ELEMENTS, INVALID_FUNCTION_ARGUMENT, "combinations exceed max size");
int[] ids = new int[combinationCount * combinationLength];
int idsPosition = 0;
int[] combination = firstCombination(arrayLength, combinationLength);
do {
arraycopy(combination, 0, ids, idsPosition, combinationLength);
idsPosition += combinationLength;
} while (nextCombination(combination, combinationLength));
verify(idsPosition == ids.length, "idsPosition != ids.length, %s and %s respectively", idsPosition, ids.length);
int[] offsets = new int[combinationCount + 1];
setAll(offsets, i -> i * combinationLength);
return ArrayBlock.fromElementBlock(combinationCount, Optional.empty(), offsets, new DictionaryBlock(array, ids));
}
Aggregations