use of com.facebook.presto.spi.block.BlockBuilder 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.BlockBuilder 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.BlockBuilder 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();
}
use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.
the class MapTransformValueFunction method transform.
public static Block transform(Type keyType, Type valueType, Type transformedValueType, Block block, MethodHandle function) {
int positionCount = block.getPositionCount();
BlockBuilder resultBuilder = new InterleavedBlockBuilder(ImmutableList.of(keyType, transformedValueType), new BlockBuilderStatus(), positionCount);
for (int position = 0; position < positionCount; position += 2) {
Object key = readNativeValue(keyType, block, position);
Object value = readNativeValue(valueType, block, position + 1);
Object transformedValue;
try {
transformedValue = function.invoke(key, value);
} catch (Throwable throwable) {
throw Throwables.propagate(throwable);
}
keyType.appendTo(block, position, resultBuilder);
writeNativeValue(transformedValueType, resultBuilder, transformedValue);
}
return resultBuilder.build();
}
use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.
the class RowEqualOperator method equals.
public static boolean equals(Type rowType, Block leftRow, Block rightRow) {
// TODO: Fix this. It feels very inefficient and unnecessary to wrap and unwrap with Block
BlockBuilder leftBlockBuilder = rowType.createBlockBuilder(new BlockBuilderStatus(), 1);
BlockBuilder rightBlockBuilder = rowType.createBlockBuilder(new BlockBuilderStatus(), 1);
rowType.writeObject(leftBlockBuilder, leftRow);
rowType.writeObject(rightBlockBuilder, rightRow);
return rowType.equalTo(leftBlockBuilder.build(), 0, rightBlockBuilder.build(), 0);
}
Aggregations