use of io.trino.operator.aggregation.TypedSet in project trino by trinodb.
the class ArrayIntersectFunction method intersect.
@TypeParameter("E")
@SqlType("array(E)")
public Block intersect(@TypeParameter("E") Type type, @OperatorDependency(operator = IS_DISTINCT_FROM, argumentTypes = { "E", "E" }, convention = @Convention(arguments = { BLOCK_POSITION, BLOCK_POSITION }, result = FAIL_ON_NULL)) BlockPositionIsDistinctFrom elementIsDistinctFrom, @OperatorDependency(operator = HASH_CODE, argumentTypes = "E", convention = @Convention(arguments = BLOCK_POSITION, result = FAIL_ON_NULL)) BlockPositionHashCode elementHashCode, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
if (leftArray.getPositionCount() < rightArray.getPositionCount()) {
Block tempArray = leftArray;
leftArray = rightArray;
rightArray = tempArray;
}
int leftPositionCount = leftArray.getPositionCount();
int rightPositionCount = rightArray.getPositionCount();
if (rightPositionCount == 0) {
return rightArray;
}
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
TypedSet rightTypedSet = createDistinctTypedSet(type, elementIsDistinctFrom, elementHashCode, rightPositionCount, "array_intersect");
for (int i = 0; i < rightPositionCount; i++) {
rightTypedSet.add(rightArray, i);
}
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
// The intersected set can have at most rightPositionCount elements
TypedSet intersectTypedSet = createDistinctTypedSet(type, elementIsDistinctFrom, elementHashCode, blockBuilder, rightPositionCount, "array_intersect");
for (int i = 0; i < leftPositionCount; i++) {
if (rightTypedSet.contains(leftArray, i)) {
intersectTypedSet.add(leftArray, i);
}
}
pageBuilder.declarePositions(intersectTypedSet.size());
return blockBuilder.getRegion(blockBuilder.getPositionCount() - intersectTypedSet.size(), intersectTypedSet.size());
}
use of io.trino.operator.aggregation.TypedSet in project trino by trinodb.
the class ArrayDistinctFunction method distinct.
@TypeParameter("E")
@SqlType("array(E)")
public Block distinct(@TypeParameter("E") Type type, @OperatorDependency(operator = IS_DISTINCT_FROM, argumentTypes = { "E", "E" }, convention = @Convention(arguments = { BLOCK_POSITION, BLOCK_POSITION }, result = FAIL_ON_NULL)) BlockPositionIsDistinctFrom elementIsDistinctFrom, @OperatorDependency(operator = HASH_CODE, argumentTypes = "E", convention = @Convention(arguments = BLOCK_POSITION, result = FAIL_ON_NULL)) BlockPositionHashCode elementHashCode, @SqlType("array(E)") Block array) {
if (array.getPositionCount() < 2) {
return array;
}
if (array.getPositionCount() == 2) {
boolean distinct = elementIsDistinctFrom.isDistinctFrom(array, 0, array, 1);
if (distinct) {
return array;
}
return array.getSingleValueBlock(0);
}
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
BlockBuilder distinctElementsBlockBuilder = pageBuilder.getBlockBuilder(0);
TypedSet distinctElements = createDistinctTypedSet(type, elementIsDistinctFrom, elementHashCode, distinctElementsBlockBuilder, array.getPositionCount(), "array_distinct");
for (int i = 0; i < array.getPositionCount(); i++) {
distinctElements.add(array, i);
}
pageBuilder.declarePositions(distinctElements.size());
return distinctElementsBlockBuilder.getRegion(distinctElementsBlockBuilder.getPositionCount() - distinctElements.size(), distinctElements.size());
}
Aggregations