use of io.prestosql.spi.function.SqlType in project hetu-core by openlookeng.
the class ArrayUnionFunction method bigintUnion.
@SqlType("array(bigint)")
public static Block bigintUnion(@SqlType("array(bigint)") Block leftArray, @SqlType("array(bigint)") Block rightArray) {
int leftArrayCount = leftArray.getPositionCount();
int rightArrayCount = rightArray.getPositionCount();
LongSet set = new LongOpenHashSet(leftArrayCount + rightArrayCount);
BlockBuilder distinctElementBlockBuilder = BIGINT.createBlockBuilder(null, leftArrayCount + rightArrayCount);
AtomicBoolean containsNull = new AtomicBoolean(false);
appendBigintArray(leftArray, containsNull, set, distinctElementBlockBuilder);
appendBigintArray(rightArray, containsNull, set, distinctElementBlockBuilder);
return distinctElementBlockBuilder.build();
}
use of io.prestosql.spi.function.SqlType 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.SqlType 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.SqlType 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.SqlType 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());
}
Aggregations