下面列出了com.google.common.primitives.Shorts#checkedCast ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Long currentTokenAsSmallint(JsonParser parser)
throws IOException
{
switch (parser.currentToken()) {
case VALUE_NULL:
return null;
case VALUE_STRING:
case FIELD_NAME:
return VarcharOperators.castToSmallint(Slices.utf8Slice(parser.getText()));
case VALUE_NUMBER_FLOAT:
return DoubleOperators.castToSmallint(parser.getDoubleValue());
case VALUE_NUMBER_INT:
return (long) Shorts.checkedCast(parser.getLongValue());
case VALUE_TRUE:
return BooleanOperators.castToSmallint(true);
case VALUE_FALSE:
return BooleanOperators.castToSmallint(false);
default:
throw new JsonCastException(format("Unexpected token when cast to %s: %s", StandardTypes.SMALLINT, parser.getText()));
}
}
@UsedByGeneratedCode
public static long shortDecimalToSmallint(long decimal, long precision, long scale, long tenToScale)
{
// this rounds the decimal value to the nearest integral value
long longResult = (decimal + tenToScale / 2) / tenToScale;
if (decimal < 0) {
longResult = -((-decimal + tenToScale / 2) / tenToScale);
}
try {
return Shorts.checkedCast(longResult);
}
catch (IllegalArgumentException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to SMALLINT", longResult));
}
}
@Description("Round to nearest integer")
@ScalarFunction("round")
@SqlType(StandardTypes.SMALLINT)
public static long roundSmallint(@SqlType(StandardTypes.SMALLINT) long num, @SqlType(StandardTypes.INTEGER) long decimals)
{
long rounded = roundLong(num, decimals);
try {
return Shorts.checkedCast(rounded);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for smallint: " + rounded, e);
}
}
@UsedByGeneratedCode
public static long longDecimalToSmallint(Slice decimal, long precision, long scale, BigInteger tenToScale)
{
try {
return Shorts.checkedCast(unscaledDecimalToUnscaledLong(rescale(decimal, DecimalConversions.intScale(-scale))));
}
catch (ArithmeticException | IllegalArgumentException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to SMALLINT", Decimals.toString(decimal, DecimalConversions.intScale(scale))));
}
}
@ScalarOperator(CAST)
@SqlType(StandardTypes.SMALLINT)
public static long castToSmallint(@SqlType(StandardTypes.BIGINT) long value)
{
try {
return Shorts.checkedCast(value);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for smallint: " + value, e);
}
}
@ScalarOperator(ADD)
@SqlType(StandardTypes.SMALLINT)
public static long add(@SqlType(StandardTypes.SMALLINT) long left, @SqlType(StandardTypes.SMALLINT) long right)
{
try {
return Shorts.checkedCast(left + right);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("smallint addition overflow: %s + %s", left, right), e);
}
}
@ScalarOperator(SUBTRACT)
@SqlType(StandardTypes.SMALLINT)
public static long subtract(@SqlType(StandardTypes.SMALLINT) long left, @SqlType(StandardTypes.SMALLINT) long right)
{
try {
return Shorts.checkedCast(left - right);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("smallint subtraction overflow: %s - %s", left, right), e);
}
}
@ScalarOperator(MULTIPLY)
@SqlType(StandardTypes.SMALLINT)
public static long multiply(@SqlType(StandardTypes.SMALLINT) long left, @SqlType(StandardTypes.SMALLINT) long right)
{
try {
return Shorts.checkedCast(left * right);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, format("smallint multiplication overflow: %s * %s", left, right), e);
}
}
@ScalarOperator(NEGATION)
@SqlType(StandardTypes.SMALLINT)
public static long negate(@SqlType(StandardTypes.SMALLINT) long value)
{
try {
return Shorts.checkedCast(-value);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "smallint negation overflow: " + value, e);
}
}
@ScalarOperator(CAST)
@SqlType(StandardTypes.SMALLINT)
public static long castToSmallint(@SqlType(StandardTypes.REAL) long value)
{
float floatValue = intBitsToFloat((int) value);
if (Float.isNaN(floatValue)) {
throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast real NaN to smallint");
}
try {
return Shorts.checkedCast((long) MathFunctions.round((double) floatValue));
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for smallint: " + floatValue, e);
}
}
@ScalarOperator(CAST)
@SqlType(StandardTypes.SMALLINT)
public static long castToSmallint(@SqlType(StandardTypes.INTEGER) long value)
{
try {
return Shorts.checkedCast(value);
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for smallint: " + value, e);
}
}
@ScalarOperator(CAST)
@SqlType(StandardTypes.SMALLINT)
public static long castToSmallint(@SqlType(StandardTypes.DOUBLE) double value)
{
if (Double.isNaN(value)) {
throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast double NaN to smallint");
}
try {
return Shorts.checkedCast((long) MathFunctions.round(value));
}
catch (IllegalArgumentException e) {
throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for smallint: " + value, e);
}
}
private static Short intToShort(Integer input)
{
if (input == null) {
return null;
}
return Shorts.checkedCast(input);
}
/**
* Convert Presto native value (stack representation) of given type to Accumulo equivalent.
*
* @param value Object to convert
* @param type Destination Presto type
*/
private static Object convert(Object value, Type type)
{
if (value == null) {
return null;
}
checkArgument(Primitives.wrap(type.getJavaType()).isInstance(value), "Invalid representation for %s: %s [%s]", type, value, value.getClass().getName());
// Array? Better be a block!
if (Types.isArrayType(type)) {
// Block
return value;
}
// Map? Better be a block!
if (Types.isMapType(type)) {
// Block
return value;
}
// And now for the plain types
if (type.equals(BIGINT)) {
// long
return value;
}
if (type.equals(INTEGER)) {
return toIntExact((long) value);
}
if (type.equals(BOOLEAN)) {
// boolean
return value;
}
if (type.equals(DATE)) {
return Date.valueOf(LocalDate.ofEpochDay((long) value));
}
if (type.equals(DOUBLE)) {
// double
return value;
}
if (type.equals(REAL)) {
return intBitsToFloat(toIntExact((long) value));
}
if (type.equals(SMALLINT)) {
return Shorts.checkedCast((long) value);
}
if (type.equals(TIME)) {
// TODO this likely is incorrect, passing the millis value interpreted in UTC into millis value interpreted in JVM's zone
// TODO account for non-legacy timestamp
return new Time((long) value);
}
if (type.equals(TIMESTAMP)) {
// TODO this likely is incorrect, passing the millis value interpreted in UTC into millis value interpreted in JVM's zone
// TODO account for non-legacy timestamp
return new Timestamp((long) value);
}
if (type.equals(TINYINT)) {
return SignedBytes.checkedCast((long) value);
}
if (type.equals(VARBINARY)) {
return ((Slice) value).getBytes();
}
if (type instanceof VarcharType) {
return ((Slice) value).toStringUtf8();
}
throw new PrestoException(NOT_SUPPORTED, "Unsupported PrestoType " + type);
}
ResChunk(int chunkType, int headerSize, int chunkSize) {
this.type = Shorts.checkedCast(chunkType);
this.headerSize = Shorts.checkedCast(headerSize);
this.chunkSize = chunkSize;
Preconditions.checkState((chunkSize % 4) == 0);
}
private static int hash(TypeInfo type, Object value)
{
if (value == null) {
return 0;
}
switch (type.getCategory()) {
case PRIMITIVE:
PrimitiveTypeInfo typeInfo = (PrimitiveTypeInfo) type;
PrimitiveCategory primitiveCategory = typeInfo.getPrimitiveCategory();
switch (primitiveCategory) {
case BOOLEAN:
return (boolean) value ? 1 : 0;
case BYTE:
return SignedBytes.checkedCast((long) value);
case SHORT:
return Shorts.checkedCast((long) value);
case INT:
return toIntExact((long) value);
case LONG:
long bigintValue = (long) value;
return (int) ((bigintValue >>> 32) ^ bigintValue);
case FLOAT:
// convert to canonical NaN if necessary
return floatToIntBits(intBitsToFloat(toIntExact((long) value)));
case DOUBLE:
long doubleValue = doubleToLongBits((double) value);
return (int) ((doubleValue >>> 32) ^ doubleValue);
case STRING:
return hashBytes(0, (Slice) value);
case VARCHAR:
return hashBytes(1, (Slice) value);
case DATE:
// day offset from 1970-01-01
return toIntExact((long) value);
case TIMESTAMP:
return hashTimestamp((long) value);
default:
throw new UnsupportedOperationException("Computation of Hive bucket hashCode is not supported for Hive primitive category: " + primitiveCategory);
}
case LIST:
return hashOfList((ListTypeInfo) type, (Block) value);
case MAP:
return hashOfMap((MapTypeInfo) type, (Block) value);
default:
// TODO: support more types, e.g. ROW
throw new UnsupportedOperationException("Computation of Hive bucket hashCode is not supported for Hive category: " + type.getCategory());
}
}
public static Decimal toMetastoreDecimal(BigDecimal decimal)
{
return new Decimal(Shorts.checkedCast(decimal.scale()), ByteBuffer.wrap(decimal.unscaledValue().toByteArray()));
}
Type(int code) {
this.code = Shorts.checkedCast(code);
}
Type(int code) {
this.code = Shorts.checkedCast(code);
}
Type(int code) {
this.code = Shorts.checkedCast(code);
}