use of io.prestosql.spi.annotation.UsedByGeneratedCode in project hetu-core by openlookeng.
the class JsonToArrayCast method toArray.
@UsedByGeneratedCode
public static Block toArray(ArrayType arrayType, BlockBuilderAppender elementAppender, ConnectorSession connectorSession, Slice json) {
try (JsonParser jsonParser = createJsonParser(JSON_FACTORY, json)) {
jsonParser.nextToken();
if (jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
return null;
}
if (jsonParser.getCurrentToken() != START_ARRAY) {
throw new JsonCastException(format("Expected a json array, but got %s", jsonParser.getText()));
}
BlockBuilder blockBuilder = arrayType.getElementType().createBlockBuilder(null, 20);
while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
elementAppender.append(jsonParser, blockBuilder);
}
if (jsonParser.nextToken() != null) {
throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
}
return blockBuilder.build();
} catch (PrestoException | JsonCastException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s\n%s", arrayType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);
} catch (Exception e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s.\n%s", arrayType, truncateIfNecessaryForErrorMessage(json)), e);
}
}
use of io.prestosql.spi.annotation.UsedByGeneratedCode in project hetu-core by openlookeng.
the class MapConcatFunction method mapConcat.
@UsedByGeneratedCode
public static Block mapConcat(MapType mapType, 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
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
TypedSet typedSet = new TypedSet(keyType, entries / 2, FUNCTION_NAME);
BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
// the last map
Block map = maps[lastMapIndex];
for (int i = 0; i < map.getPositionCount(); i += 2) {
typedSet.add(map, i);
keyType.appendTo(map, i, blockBuilder);
valueType.appendTo(map, i + 1, blockBuilder);
}
// 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, blockBuilder);
valueType.appendTo(map, i + 1, blockBuilder);
}
}
}
// the first map
map = maps[firstMapIndex];
for (int i = 0; i < map.getPositionCount(); i += 2) {
if (!typedSet.contains(map, i)) {
keyType.appendTo(map, i, blockBuilder);
valueType.appendTo(map, i + 1, blockBuilder);
}
}
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
use of io.prestosql.spi.annotation.UsedByGeneratedCode in project hetu-core by openlookeng.
the class DecimalCasts method varcharToShortDecimal.
@UsedByGeneratedCode
public static long varcharToShortDecimal(Slice value, long precision, long scale, long tenToScale) {
BigDecimal result;
String stringValue = value.toString(UTF_8);
try {
result = new BigDecimal(stringValue).setScale(DecimalConversions.intScale(scale), HALF_UP);
} catch (NumberFormatException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s). Value is not a number.", stringValue, precision, scale));
}
if (overflows(result, precision)) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s). Value too large.", stringValue, precision, scale));
}
return result.unscaledValue().longValue();
}
use of io.prestosql.spi.annotation.UsedByGeneratedCode in project hetu-core by openlookeng.
the class DecimalCasts method jsonToLongDecimal.
@UsedByGeneratedCode
public static Slice jsonToLongDecimal(Slice json, long precision, long scale, BigInteger tenToScale) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
parser.nextToken();
Slice result = currentTokenAsLongDecimal(parser, intPrecision(precision), DecimalConversions.intScale(scale));
// check no trailing token
checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to DECIMAL(%s,%s)", precision, scale);
return result;
} catch (IOException | NumberFormatException | JsonCastException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to DECIMAL(%s,%s)", json.toStringUtf8(), precision, scale), e);
}
}
use of io.prestosql.spi.annotation.UsedByGeneratedCode in project hetu-core by openlookeng.
the class DecimalCasts method varcharToLongDecimal.
@UsedByGeneratedCode
public static Slice varcharToLongDecimal(Slice value, long precision, long scale, BigInteger tenToScale) {
BigDecimal result;
String stringValue = value.toString(UTF_8);
try {
result = new BigDecimal(stringValue).setScale(DecimalConversions.intScale(scale), HALF_UP);
} catch (NumberFormatException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s). Value is not a number.", stringValue, precision, scale));
}
if (overflows(result, precision)) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s). Value too large.", stringValue, precision, scale));
}
return encodeUnscaledValue(result.unscaledValue());
}
Aggregations