use of io.trino.util.JsonCastException in project trino by trinodb.
the class JsonToMapCast method toMap.
@UsedByGeneratedCode
public static Block toMap(MapType mapType, BlockBuilderAppender mapAppender, ConnectorSession connectorSession, Slice json) {
try (JsonParser jsonParser = createJsonParser(JSON_FACTORY, json)) {
jsonParser.nextToken();
if (jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
return null;
}
BlockBuilder blockBuilder = mapType.createBlockBuilder(null, 1);
mapAppender.append(jsonParser, blockBuilder);
if (jsonParser.nextToken() != null) {
throw new JsonCastException(format("Unexpected trailing token: %s", jsonParser.getText()));
}
return mapType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
} catch (TrinoException | JsonCastException e) {
throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s. %s\n%s", mapType, e.getMessage(), truncateIfNecessaryForErrorMessage(json)), e);
} catch (Exception e) {
throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast to %s.\n%s", mapType, truncateIfNecessaryForErrorMessage(json)), e);
}
}
use of io.trino.util.JsonCastException in project trino by trinodb.
the class DecimalCasts method jsonToLongDecimal.
@UsedByGeneratedCode
public static Int128 jsonToLongDecimal(Slice json, long precision, long scale, Int128 tenToScale) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
parser.nextToken();
Int128 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 TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to DECIMAL(%s,%s)", json.toStringUtf8(), precision, scale), e);
}
}
use of io.trino.util.JsonCastException in project trino by trinodb.
the class DecimalCasts method jsonToShortDecimal.
@UsedByGeneratedCode
public static Long jsonToShortDecimal(Slice json, long precision, long scale, long tenToScale) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
parser.nextToken();
Long result = currentTokenAsShortDecimal(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 TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to DECIMAL(%s,%s)", json.toStringUtf8(), precision, scale), e);
}
}
Aggregations