use of com.facebook.presto.annotation.UsedByGeneratedCode in project presto by prestodb.
the class MapConcatFunction method mapConcat.
@UsedByGeneratedCode
public static Block mapConcat(Type keyType, Type valueType, Object state, Block[] maps) {
int entries = 0;
int lastMapIndex = maps.length - 1;
int firstMapIndex = lastMapIndex;
for (int i = 0; i < maps.length; i++) {
entries += maps[i].getPositionCount();
if (maps[i].getPositionCount() > 0) {
lastMapIndex = i;
firstMapIndex = min(firstMapIndex, i);
}
}
if (lastMapIndex == firstMapIndex) {
return maps[lastMapIndex];
}
PageBuilder pageBuilder = (PageBuilder) state;
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
// TODO: we should move TypedSet into user state as well
TypedSet typedSet = new TypedSet(keyType, entries / 2);
BlockBuilder keyBlockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder valueBlockBuilder = pageBuilder.getBlockBuilder(1);
// the last map
Block map = maps[lastMapIndex];
int total = 0;
for (int i = 0; i < map.getPositionCount(); i += 2) {
typedSet.add(map, i);
keyType.appendTo(map, i, keyBlockBuilder);
valueType.appendTo(map, i + 1, valueBlockBuilder);
total++;
}
// the map between the last and the first
for (int idx = lastMapIndex - 1; idx > firstMapIndex; idx--) {
map = maps[idx];
for (int i = 0; i < map.getPositionCount(); i += 2) {
if (!typedSet.contains(map, i)) {
typedSet.add(map, i);
keyType.appendTo(map, i, keyBlockBuilder);
valueType.appendTo(map, i + 1, valueBlockBuilder);
total++;
}
}
}
// the first map
map = maps[firstMapIndex];
for (int i = 0; i < map.getPositionCount(); i += 2) {
if (!typedSet.contains(map, i)) {
keyType.appendTo(map, i, keyBlockBuilder);
valueType.appendTo(map, i + 1, valueBlockBuilder);
total++;
}
}
pageBuilder.declarePositions(total);
Block[] blocks = new Block[2];
blocks[0] = keyBlockBuilder.getRegion(keyBlockBuilder.getPositionCount() - total, total);
blocks[1] = valueBlockBuilder.getRegion(valueBlockBuilder.getPositionCount() - total, total);
return new InterleavedBlock(blocks);
}
use of com.facebook.presto.annotation.UsedByGeneratedCode in project presto by prestodb.
the class DecimalOperators method multiplyLongLongLong.
@UsedByGeneratedCode
public static Slice multiplyLongLongLong(Slice a, Slice b) {
try {
Slice result = UnscaledDecimal128Arithmetic.multiply(a, b);
throwIfOverflows(result);
return result;
} catch (ArithmeticException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Decimal overflow", e);
}
}
use of com.facebook.presto.annotation.UsedByGeneratedCode in project presto by prestodb.
the class DecimalCasts method varcharToLongDecimal.
@UsedByGeneratedCode
public static Slice varcharToLongDecimal(Slice value, long precision, long scale, BigInteger tenToScale) {
String stringValue = value.toString(UTF_8);
BigDecimal decimal = new BigDecimal(stringValue).setScale(intScale(scale), HALF_UP);
if (overflows(decimal, precision)) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s)", stringValue, precision, scale));
}
return encodeUnscaledValue(decimal.unscaledValue());
}
use of com.facebook.presto.annotation.UsedByGeneratedCode in project presto by prestodb.
the class DecimalCasts method varcharToShortDecimal.
@UsedByGeneratedCode
public static long varcharToShortDecimal(Slice value, long precision, long scale, long tenToScale) {
try {
String stringValue = value.toString(UTF_8);
BigDecimal decimal = new BigDecimal(stringValue).setScale(intScale(scale), HALF_UP);
if (overflows(decimal, precision)) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s)", stringValue, precision, scale));
}
return decimal.unscaledValue().longValue();
} catch (NumberFormatException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s)", value.toString(UTF_8), precision, scale));
}
}
use of com.facebook.presto.annotation.UsedByGeneratedCode in project presto by prestodb.
the class ArrayConcatUtils method appendElement.
@UsedByGeneratedCode
public static Block appendElement(Type elementType, Block block, Slice value) {
BlockBuilder blockBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), block.getPositionCount() + 1);
for (int i = 0; i < block.getPositionCount(); i++) {
elementType.appendTo(block, i, blockBuilder);
}
elementType.writeSlice(blockBuilder, value);
return blockBuilder.build();
}
Aggregations