Java Code Examples for com.google.common.primitives.Shorts#checkedCast()

The following examples show how to use com.google.common.primitives.Shorts#checkedCast() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonUtil.java    From presto with Apache License 2.0 6 votes vote down vote up
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()));
    }
}
 
Example 2
Source File: DecimalCasts.java    From presto with Apache License 2.0 6 votes vote down vote up
@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));
    }
}
 
Example 3
Source File: MathFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 4
Source File: AbstractTestParquetReader.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Short intToShort(Integer input)
{
    if (input == null) {
        return null;
    }
    return Shorts.checkedCast(input);
}
 
Example 5
Source File: DoubleOperators.java    From presto with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 6
Source File: IntegerOperators.java    From presto with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 7
Source File: RealOperators.java    From presto with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 8
Source File: SmallintOperators.java    From presto with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 9
Source File: SmallintOperators.java    From presto with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 10
Source File: SmallintOperators.java    From presto with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 11
Source File: SmallintOperators.java    From presto with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 12
Source File: BigintOperators.java    From presto with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 13
Source File: DecimalCasts.java    From presto with Apache License 2.0 5 votes vote down vote up
@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))));
    }
}
 
Example 14
Source File: ResChunk.java    From buck with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 15
Source File: HiveBucketingV1.java    From presto with Apache License 2.0 4 votes vote down vote up
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());
    }
}
 
Example 16
Source File: ThriftMetastoreUtil.java    From presto with Apache License 2.0 4 votes vote down vote up
public static Decimal toMetastoreDecimal(BigDecimal decimal)
{
    return new Decimal(Shorts.checkedCast(decimal.scale()), ByteBuffer.wrap(decimal.unscaledValue().toByteArray()));
}
 
Example 17
Source File: Field.java    From presto with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 18
Source File: Chunk.java    From apkfile with Apache License 2.0 4 votes vote down vote up
Type(int code) {
    this.code = Shorts.checkedCast(code);
}
 
Example 19
Source File: Chunk.java    From android-chunk-utils with Apache License 2.0 4 votes vote down vote up
Type(int code) {
  this.code = Shorts.checkedCast(code);
}
 
Example 20
Source File: Chunk.java    From android-arscblamer with Apache License 2.0 4 votes vote down vote up
Type(int code) {
  this.code = Shorts.checkedCast(code);
}