Search in sources :

Example 11 with TypeParameter

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();
}
Also used : BlockBuilder(io.prestosql.spi.block.BlockBuilder) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Example 12 with TypeParameter

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();
}
Also used : Signature.orderableTypeParameter(io.prestosql.spi.function.Signature.orderableTypeParameter) Signature.comparableTypeParameter(io.prestosql.spi.function.Signature.comparableTypeParameter) TypeParameter(io.prestosql.spi.function.TypeParameter) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) OperatorType(io.prestosql.spi.function.OperatorType) TypeVariableConstraint(io.prestosql.spi.function.TypeVariableConstraint) HashSet(java.util.HashSet)

Example 13 with TypeParameter

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());
}
Also used : ArrayList(java.util.ArrayList) PrestoException(io.prestosql.spi.PrestoException) BlockBuilder(io.prestosql.spi.block.BlockBuilder) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Example 14 with TypeParameter

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);
}
Also used : BlockBuilder(io.prestosql.spi.block.BlockBuilder) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Example 15 with TypeParameter

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();
}
Also used : TypedSet(io.prestosql.operator.aggregation.TypedSet) BlockBuilder(io.prestosql.spi.block.BlockBuilder) TypeParameter(io.prestosql.spi.function.TypeParameter) SqlType(io.prestosql.spi.function.SqlType)

Aggregations

TypeParameter (io.prestosql.spi.function.TypeParameter)24 SqlType (io.prestosql.spi.function.SqlType)20 BlockBuilder (io.prestosql.spi.block.BlockBuilder)18 TypedSet (io.prestosql.operator.aggregation.TypedSet)6 TypeParameterSpecialization (io.prestosql.spi.function.TypeParameterSpecialization)5 Block (io.prestosql.spi.block.Block)4 HyperLogLog (io.airlift.stats.cardinality.HyperLogLog)3 PrestoException (io.prestosql.spi.PrestoException)3 InputFunction (io.prestosql.spi.function.InputFunction)3 ArrayType (io.prestosql.spi.type.ArrayType)3 RowType (io.prestosql.spi.type.RowType)3 Type (io.prestosql.spi.type.Type)3 Slice (io.airlift.slice.Slice)2 SqlNullable (io.prestosql.spi.function.SqlNullable)2 MapType (io.prestosql.spi.type.MapType)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 DictionaryBlock (io.prestosql.spi.block.DictionaryBlock)1 PageBuilderStatus (io.prestosql.spi.block.PageBuilderStatus)1 OperatorType (io.prestosql.spi.function.OperatorType)1