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);
}
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();
}
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;
}
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;
}
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;
}
Aggregations