use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.
the class RepeatFunction method repeat.
@TypeParameter("T")
@SqlType("array(T)")
public static Block repeat(@TypeParameter("T") Type type, @SqlNullable @SqlType("T") Object element, @SqlType(StandardTypes.INTEGER) long count) {
BlockBuilder blockBuilder = createBlockBuilder(type, count);
if (element == null) {
return repeatNullValues(blockBuilder, count);
}
if (count > 0) {
type.writeObject(blockBuilder, element);
checkMaxSize(blockBuilder.getSizeInBytes(), count);
}
for (int i = 1; i < count; i++) {
type.writeObject(blockBuilder, element);
}
return blockBuilder.build();
}
use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.
the class FunctionsParserHelper method createTypeVariableConstraints.
public static List<TypeVariableConstraint> createTypeVariableConstraints(Iterable<TypeParameter> typeParameters, List<ImplementationDependency> dependencies) {
Set<String> orderableRequired = new HashSet<>();
Set<String> comparableRequired = new HashSet<>();
for (ImplementationDependency dependency : dependencies) {
if (dependency instanceof OperatorImplementationDependency) {
OperatorType operator = ((OperatorImplementationDependency) dependency).getOperator();
if (operator == CAST) {
continue;
}
Set<String> argumentTypes = ((OperatorImplementationDependency) dependency).getArgumentTypes().stream().map(TypeSignature::getBase).collect(toImmutableSet());
checkArgument(argumentTypes.size() == 1, "Operator dependency must only have arguments of a single type");
String argumentType = Iterables.getOnlyElement(argumentTypes);
if (COMPARABLE_TYPE_OPERATORS.contains(operator)) {
comparableRequired.add(argumentType);
}
if (ORDERABLE_TYPE_OPERATORS.contains(operator)) {
orderableRequired.add(argumentType);
}
}
}
ImmutableList.Builder<TypeVariableConstraint> typeVariableConstraints = ImmutableList.builder();
for (TypeParameter typeParameter : typeParameters) {
String name = typeParameter.value();
if (orderableRequired.contains(name)) {
typeVariableConstraints.add(orderableTypeParameter(name));
} else if (comparableRequired.contains(name)) {
typeVariableConstraints.add(comparableTypeParameter(name));
} else {
typeVariableConstraints.add(typeVariable(name));
}
}
return typeVariableConstraints.build();
}
use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.
the class ArrayRemoveFunction method remove.
@TypeParameter("E")
@SqlType("array(E)")
public Block remove(@OperatorDependency(operator = EQUAL, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle equalsFunction, @TypeParameter("E") Type type, @SqlType("array(E)") Block array, @SqlType("E") Object value) {
List<Integer> positions = new ArrayList<>();
for (int i = 0; i < array.getPositionCount(); i++) {
Object element = readNativeValue(type, array, i);
try {
if (element == null) {
positions.add(i);
continue;
}
Boolean result = (Boolean) equalsFunction.invoke(element, value);
if (result == null) {
throw new PrestoException(NOT_SUPPORTED, "array_remove does not support arrays with elements that are null or contain null");
}
if (!result) {
positions.add(i);
}
} catch (Throwable t) {
throw internalError(t);
}
}
if (array.getPositionCount() == positions.size()) {
return array;
}
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
for (int position : positions) {
type.appendTo(array, position, blockBuilder);
}
pageBuilder.declarePositions(positions.size());
return blockBuilder.getRegion(blockBuilder.getPositionCount() - positions.size(), positions.size());
}
use of io.prestosql.spi.function.TypeParameter in project hetu-core by openlookeng.
the class ArraySortFunction method sort.
@TypeParameter("E")
@SqlType("array(E)")
public Block sort(@OperatorDependency(operator = LESS_THAN, returnType = StandardTypes.BOOLEAN, argumentTypes = { "E", "E" }) MethodHandle lessThanFunction, @TypeParameter("E") Type type, @SqlType("array(E)") Block block) {
int arrayLength = block.getPositionCount();
if (positions.size() < arrayLength) {
positions = Ints.asList(new int[arrayLength]);
}
for (int i = 0; i < arrayLength; i++) {
positions.set(i, i);
}
Collections.sort(positions.subList(0, arrayLength), new Comparator<Integer>() {
@Override
public int compare(Integer p1, Integer p2) {
boolean nullLeft = block.isNull(p1);
boolean nullRight = block.isNull(p2);
if (nullLeft && nullRight) {
return 0;
}
if (nullLeft) {
return 1;
}
if (nullRight) {
return -1;
}
// TODO: This could be quite slow, it should use parametric equals
return type.compareTo(block, p1, block, p2);
}
});
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
for (int i = 0; i < arrayLength; i++) {
type.appendTo(block, positions.get(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 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, "array_union");
BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(null, leftArrayCount + rightArrayCount);
appendTypedArray(leftArray, type, typedSet, distinctElementBlockBuilder);
appendTypedArray(rightArray, type, typedSet, distinctElementBlockBuilder);
return distinctElementBlockBuilder.build();
}
Aggregations