Search in sources :

Example 91 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class PolymorphicScalarFunction method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
    List<TypeSignature> resolvedParameterTypeSignatures = applyBoundVariables(getSignature().getArgumentTypes(), boundVariables);
    List<Type> resolvedParameterTypes = resolveTypes(resolvedParameterTypeSignatures, typeManager);
    TypeSignature resolvedReturnTypeSignature = applyBoundVariables(getSignature().getReturnType(), boundVariables);
    Type resolvedReturnType = typeManager.getType(resolvedReturnTypeSignature);
    SpecializeContext context = new SpecializeContext(boundVariables, resolvedParameterTypes, resolvedReturnType, typeManager, functionRegistry);
    Optional<Method> matchingMethod = Optional.empty();
    Optional<MethodsGroup> matchingMethodsGroup = Optional.empty();
    for (MethodsGroup candidateMethodsGroup : methodsGroups) {
        for (Method candidateMethod : candidateMethodsGroup.getMethods()) {
            if (matchesParameterAndReturnTypes(candidateMethod, resolvedParameterTypes, resolvedReturnType) && predicateIsTrue(candidateMethodsGroup, context)) {
                if (matchingMethod.isPresent()) {
                    if (onlyFirstMatchedMethodHasPredicate(matchingMethodsGroup.get(), candidateMethodsGroup)) {
                        continue;
                    }
                    throw new IllegalStateException("two matching methods (" + matchingMethod.get().getName() + " and " + candidateMethod.getName() + ") for parameter types " + resolvedParameterTypeSignatures);
                }
                matchingMethod = Optional.of(candidateMethod);
                matchingMethodsGroup = Optional.of(candidateMethodsGroup);
            }
        }
    }
    checkState(matchingMethod.isPresent(), "no matching method for parameter types %s", resolvedParameterTypes);
    List<Object> extraParameters = computeExtraParameters(matchingMethodsGroup.get(), context);
    MethodHandle matchingMethodHandle = applyExtraParameters(matchingMethod.get(), extraParameters);
    return new ScalarFunctionImplementation(nullableResult, nullableArguments, nullFlags, matchingMethodHandle, deterministic);
}
Also used : ScalarFunctionImplementation(com.facebook.presto.operator.scalar.ScalarFunctionImplementation) MethodsGroup(com.facebook.presto.metadata.SqlScalarFunctionBuilder.MethodsGroup) Method(java.lang.reflect.Method) SpecializeContext(com.facebook.presto.metadata.SqlScalarFunctionBuilder.SpecializeContext) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Type(com.facebook.presto.spi.type.Type) MethodHandle(java.lang.invoke.MethodHandle)

Example 92 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class TopNOperator method getOutput.

@Override
public Page getOutput() {
    if (outputIterator == null || !outputIterator.hasNext()) {
        // no data
        if (topNBuilder == null) {
            return null;
        }
        // only flush if we are finishing or the aggregation builder is full
        if (!finishing && !topNBuilder.isFull()) {
            return null;
        }
        // Only partial aggregation can flush early. Also, check that we are not flushing tiny bits at a time
        if (finishing || partial) {
            outputIterator = topNBuilder.build();
            topNBuilder = null;
        }
    }
    pageBuilder.reset();
    while (!pageBuilder.isFull() && outputIterator.hasNext()) {
        Block[] next = outputIterator.next();
        pageBuilder.declarePosition();
        for (int i = 0; i < next.length; i++) {
            Type type = types.get(i);
            type.appendTo(next[i], 0, pageBuilder.getBlockBuilder(i));
        }
    }
    return pageBuilder.build();
}
Also used : Type(com.facebook.presto.spi.type.Type) Block(com.facebook.presto.spi.block.Block)

Example 93 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class TopNRowNumberOperator method getPage.

private Page getPage() {
    if (!flushingPartition.isPresent()) {
        flushingPartition = getFlushingPartition();
    }
    pageBuilder.reset();
    long sizeDelta = 0;
    while (!pageBuilder.isFull() && flushingPartition.isPresent()) {
        FlushingPartition currentFlushingPartition = flushingPartition.get();
        while (!pageBuilder.isFull() && currentFlushingPartition.hasNext()) {
            Block[] next = currentFlushingPartition.next();
            sizeDelta += sizeOfRow(next);
            pageBuilder.declarePosition();
            for (int i = 0; i < outputChannels.length; i++) {
                int channel = outputChannels[i];
                Type type = types.get(i);
                type.appendTo(next[channel], 0, pageBuilder.getBlockBuilder(i));
            }
            if (generateRowNumber) {
                BIGINT.writeLong(pageBuilder.getBlockBuilder(outputChannels.length), currentFlushingPartition.getRowNumber());
            }
        }
        if (!currentFlushingPartition.hasNext()) {
            flushingPartition = getFlushingPartition();
        }
    }
    if (pageBuilder.isEmpty()) {
        return null;
    }
    Page page = pageBuilder.build();
    operatorContext.freeMemory(sizeDelta);
    return page;
}
Also used : Type(com.facebook.presto.spi.type.Type) Block(com.facebook.presto.spi.block.Block) Page(com.facebook.presto.spi.Page)

Example 94 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class UnnestOperator method initializeUnnesters.

private void initializeUnnesters() {
    unnesters.clear();
    for (int i = 0; i < unnestTypes.size(); i++) {
        Type type = unnestTypes.get(i);
        int channel = unnestChannels.get(i);
        Block block = null;
        if (!currentPage.getBlock(channel).isNull(currentPosition)) {
            block = (Block) type.getObject(currentPage.getBlock(channel), currentPosition);
        }
        if (type instanceof ArrayType) {
            unnesters.add(new ArrayUnnester((ArrayType) type, block));
        } else if (type instanceof MapType) {
            unnesters.add(new MapUnnester((MapType) type, block));
        } else {
            throw new IllegalArgumentException("Cannot unnest type: " + type);
        }
    }
    ordinalityCount = 0;
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) Block(com.facebook.presto.spi.block.Block) MapType(com.facebook.presto.type.MapType)

Example 95 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class UnnestOperator method getOutput.

@Override
public Page getOutput() {
    while (!pageBuilder.isFull() && currentPage != null) {
        // Advance until we find data to unnest
        while (!anyUnnesterHasData()) {
            currentPosition++;
            if (currentPosition == currentPage.getPositionCount()) {
                currentPage = null;
                currentPosition = 0;
                break;
            }
            initializeUnnesters();
        }
        while (!pageBuilder.isFull() && anyUnnesterHasData()) {
            // Copy all the channels marked for replication
            for (int replicateChannel = 0; replicateChannel < replicateTypes.size(); replicateChannel++) {
                Type type = replicateTypes.get(replicateChannel);
                int channel = replicateChannels.get(replicateChannel);
                type.appendTo(currentPage.getBlock(channel), currentPosition, pageBuilder.getBlockBuilder(replicateChannel));
            }
            int offset = replicateTypes.size();
            pageBuilder.declarePosition();
            for (Unnester unnester : unnesters) {
                if (unnester.hasNext()) {
                    unnester.appendNext(pageBuilder, offset);
                } else {
                    for (int unnesterChannelIndex = 0; unnesterChannelIndex < unnester.getChannelCount(); unnesterChannelIndex++) {
                        pageBuilder.getBlockBuilder(offset + unnesterChannelIndex).appendNull();
                    }
                }
                offset += unnester.getChannelCount();
            }
            if (withOrdinality) {
                ordinalityCount++;
                BIGINT.writeLong(pageBuilder.getBlockBuilder(offset), ordinalityCount);
            }
        }
    }
    if ((!finishing && !pageBuilder.isFull()) || pageBuilder.isEmpty()) {
        return null;
    }
    Page page = pageBuilder.build();
    pageBuilder.reset();
    return page;
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) Page(com.facebook.presto.spi.Page)

Aggregations

Type (com.facebook.presto.spi.type.Type)392 Test (org.testng.annotations.Test)103 Block (com.facebook.presto.spi.block.Block)83 ImmutableList (com.google.common.collect.ImmutableList)79 ArrayType (com.facebook.presto.type.ArrayType)78 MapType (com.facebook.presto.type.MapType)72 Page (com.facebook.presto.spi.Page)68 PrestoException (com.facebook.presto.spi.PrestoException)51 List (java.util.List)50 VarcharType (com.facebook.presto.spi.type.VarcharType)48 DecimalType (com.facebook.presto.spi.type.DecimalType)44 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)40 MaterializedResult (com.facebook.presto.testing.MaterializedResult)40 ArrayList (java.util.ArrayList)40 MethodHandle (java.lang.invoke.MethodHandle)39 PlanNodeId (com.facebook.presto.sql.planner.plan.PlanNodeId)37 ImmutableMap (com.google.common.collect.ImmutableMap)36 Map (java.util.Map)36 Slice (io.airlift.slice.Slice)35 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)30