use of io.trino.spi.function.TypeParameter in project trino by trinodb.
the class ArrayExceptFunction method except.
@TypeParameter("E")
@SqlType("array(E)")
public static Block except(@TypeParameter("E") Type type, @OperatorDependency(operator = EQUAL, argumentTypes = { "E", "E" }, convention = @Convention(arguments = { BLOCK_POSITION, BLOCK_POSITION }, result = NULLABLE_RETURN)) BlockPositionEqual elementEqual, @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) {
int leftPositionCount = leftArray.getPositionCount();
int rightPositionCount = rightArray.getPositionCount();
if (leftPositionCount == 0 || rightPositionCount == 0) {
return leftArray;
}
TypedSet typedSet = createEqualityTypedSet(type, elementEqual, elementHashCode, leftPositionCount, "array_except");
BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(null, leftPositionCount);
for (int i = 0; i < rightPositionCount; i++) {
typedSet.add(rightArray, i);
}
for (int i = 0; i < leftPositionCount; i++) {
if (typedSet.add(leftArray, i)) {
type.appendTo(leftArray, i, distinctElementBlockBuilder);
}
}
return distinctElementBlockBuilder.build();
}
use of io.trino.spi.function.TypeParameter in project trino by trinodb.
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)") LongToBooleanFunction function) {
int positionCount = arrayBlock.getPositionCount();
BlockBuilder resultBuilder = elementType.createBlockBuilder(null, positionCount);
for (int position = 0; position < positionCount; position++) {
Long input = null;
if (!arrayBlock.isNull(position)) {
input = elementType.getLong(arrayBlock, position);
}
Boolean keep = function.apply(input);
if (TRUE.equals(keep)) {
elementType.appendTo(arrayBlock, position, resultBuilder);
}
}
return resultBuilder.build();
}
use of io.trino.spi.function.TypeParameter 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.spi.function.TypeParameter 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());
}
use of io.trino.spi.function.TypeParameter in project trino by trinodb.
the class ApproximateCountDistinctAggregation method input.
@InputFunction
@TypeParameter("T")
public static void input(@OperatorDependency(operator = XX_HASH_64, argumentTypes = "T", convention = @Convention(arguments = NEVER_NULL, result = FAIL_ON_NULL)) MethodHandle methodHandle, @AggregationState HyperLogLogState state, @SqlType("T") long value, @SqlType(StandardTypes.DOUBLE) double maxStandardError) {
HyperLogLog hll = getOrCreateHyperLogLog(state, maxStandardError);
state.addMemoryUsage(-hll.estimatedInMemorySize());
long hash;
try {
hash = (long) methodHandle.invokeExact(value);
} catch (Throwable t) {
throw internalError(t);
}
hll.addHash(hash);
state.addMemoryUsage(hll.estimatedInMemorySize());
}
Aggregations