use of com.facebook.presto.spi.block.BlockBuilderStatus in project presto by prestodb.
the class ZipFunction method zip.
@UsedByGeneratedCode
public static Block zip(List<Type> types, Block... arrays) {
int biggestCardinality = 0;
for (Block array : arrays) {
biggestCardinality = Math.max(biggestCardinality, array.getPositionCount());
}
RowType rowType = new RowType(types, Optional.empty());
BlockBuilder outputBuilder = rowType.createBlockBuilder(new BlockBuilderStatus(), biggestCardinality);
for (int outputPosition = 0; outputPosition < biggestCardinality; outputPosition++) {
BlockBuilder rowBuilder = outputBuilder.beginBlockEntry();
for (int fieldIndex = 0; fieldIndex < arrays.length; fieldIndex++) {
if (arrays[fieldIndex].getPositionCount() <= outputPosition) {
rowBuilder.appendNull();
} else {
types.get(fieldIndex).appendTo(arrays[fieldIndex], outputPosition, rowBuilder);
}
}
outputBuilder.closeEntry();
}
return outputBuilder.build();
}
use of com.facebook.presto.spi.block.BlockBuilderStatus in project presto by prestodb.
the class RowHashCodeOperator method hash.
@UsedByGeneratedCode
public static long hash(Type rowType, Block block) {
BlockBuilder blockBuilder = rowType.createBlockBuilder(new BlockBuilderStatus(), 1);
blockBuilder.writeObject(block).closeEntry();
return rowType.hash(blockBuilder.build(), 0);
}
use of com.facebook.presto.spi.block.BlockBuilderStatus in project presto by prestodb.
the class SequenceFunction method fixedWidthSequence.
private static Block fixedWidthSequence(long start, long stop, long step, FixedWidthType type) {
checkCondition(step != 0, INVALID_FUNCTION_ARGUMENT, "step must not be zero");
checkCondition(step > 0 ? stop >= start : stop < start, INVALID_FUNCTION_ARGUMENT, "sequence stop value should be greater than or equal to start value if step is greater than zero otherwise stop should be less than start");
int length = toIntExact((stop - start) / step + 1L);
BlockBuilder blockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), length);
for (long i = 0, value = start; i < length; ++i, value += step) {
type.writeLong(blockBuilder, value);
}
return blockBuilder.build();
}
use of com.facebook.presto.spi.block.BlockBuilderStatus in project presto by prestodb.
the class StringFunctions method split.
@ScalarFunction
@LiteralParameters({ "x", "y" })
@SqlType("array(varchar(x))")
public static Block split(@SqlType("varchar(x)") Slice string, @SqlType("varchar(y)") Slice delimiter, @SqlType(StandardTypes.BIGINT) long limit) {
checkCondition(limit > 0, INVALID_FUNCTION_ARGUMENT, "Limit must be positive");
checkCondition(limit <= Integer.MAX_VALUE, INVALID_FUNCTION_ARGUMENT, "Limit is too large");
checkCondition(delimiter.length() > 0, INVALID_FUNCTION_ARGUMENT, "The delimiter may not be the empty string");
BlockBuilder parts = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), 1, string.length());
// If limit is one, the last and only element is the complete string
if (limit == 1) {
VARCHAR.writeSlice(parts, string);
return parts.build();
}
int index = 0;
while (index < string.length()) {
int splitIndex = string.indexOf(delimiter, index);
// Found split?
if (splitIndex < 0) {
break;
}
// Add the part from current index to found split
VARCHAR.writeSlice(parts, string, index, splitIndex - index);
// Continue searching after delimiter
index = splitIndex + delimiter.length();
// Reached limit-1 parts so we can stop
if (parts.getPositionCount() == limit - 1) {
break;
}
}
// Rest of string
VARCHAR.writeSlice(parts, string, index, string.length() - index);
return parts.build();
}
use of com.facebook.presto.spi.block.BlockBuilderStatus in project presto by prestodb.
the class MapTransformKeyFunction method transform.
public static Block transform(Type keyType, Type transformedKeyType, Type valueType, ConnectorSession session, Block block, MethodHandle function) {
int positionCount = block.getPositionCount();
BlockBuilder resultBuilder = new InterleavedBlockBuilder(ImmutableList.of(transformedKeyType, valueType), new BlockBuilderStatus(), positionCount);
TypedSet typedSet = new TypedSet(transformedKeyType, positionCount / 2);
for (int position = 0; position < positionCount; position += 2) {
Object key = readNativeValue(keyType, block, position);
Object value = readNativeValue(valueType, block, position + 1);
Object transformedKey;
try {
transformedKey = function.invoke(key, value);
} catch (Throwable throwable) {
throw Throwables.propagate(throwable);
}
if (transformedKey == null) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
}
writeNativeValue(transformedKeyType, resultBuilder, transformedKey);
valueType.appendTo(block, position + 1, resultBuilder);
if (typedSet.contains(resultBuilder, position)) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Duplicate keys (%s) are not allowed", transformedKeyType.getObjectValue(session, resultBuilder, position)));
}
typedSet.add(resultBuilder, position);
}
return resultBuilder.build();
}
Aggregations