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 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 JsonToMapCast method toMap.
@UsedByGeneratedCode
public static Block toMap(Type mapType, ConnectorSession connectorSession, Slice json) {
try {
Map<?, ?> map = (Map<?, ?>) stackRepresentationToObject(connectorSession, json, mapType);
if (map == null) {
return null;
}
Type keyType = ((MapType) mapType).getKeyType();
Type valueType = ((MapType) mapType).getValueType();
BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(keyType, valueType), new BlockBuilderStatus(), map.size() * 2);
for (Map.Entry<?, ?> entry : map.entrySet()) {
appendToBlockBuilder(keyType, entry.getKey(), blockBuilder);
appendToBlockBuilder(valueType, entry.getValue(), blockBuilder);
}
return blockBuilder.build();
} catch (RuntimeException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to " + mapType, e);
}
}
Aggregations