Example usage for com.google.common.primitives SignedBytes checkedCast

List of usage examples for com.google.common.primitives SignedBytes checkedCast

Introduction

In this page you can find the example usage for com.google.common.primitives SignedBytes checkedCast.

Prototype

public static byte checkedCast(long value) 

Source Link

Document

Returns the byte value that is equal to value , if possible.

Usage

From source file:com.facebook.presto.type.TinyintOperators.java

@ScalarOperator(ADD)
@SqlType(StandardTypes.TINYINT)//from   w  w w . j  ava  2  s  .c o  m
public static long add(@SqlType(StandardTypes.TINYINT) long left, @SqlType(StandardTypes.TINYINT) long right) {
    try {
        return SignedBytes.checkedCast(left + right);
    } catch (IllegalArgumentException e) {
        throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE,
                format("tinyint addition overflow: %s + %s", left, right), e);
    }
}

From source file:com.facebook.presto.type.TinyintOperators.java

@ScalarOperator(SUBTRACT)
@SqlType(StandardTypes.TINYINT)/*from w  ww.  j  ava  2  s  .  co  m*/
public static long subtract(@SqlType(StandardTypes.TINYINT) long left,
        @SqlType(StandardTypes.TINYINT) long right) {
    try {
        return SignedBytes.checkedCast(left - right);
    } catch (IllegalArgumentException e) {
        throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE,
                format("tinyint subtraction overflow: %s - %s", left, right), e);
    }
}

From source file:com.facebook.presto.type.TinyintOperators.java

@ScalarOperator(MULTIPLY)
@SqlType(StandardTypes.TINYINT)//  www  .jav a  2  s  . c o  m
public static long multiply(@SqlType(StandardTypes.TINYINT) long left,
        @SqlType(StandardTypes.TINYINT) long right) {
    try {
        return SignedBytes.checkedCast(left * right);
    } catch (IllegalArgumentException e) {
        throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE,
                format("tinyint multiplication overflow: %s * %s", left, right), e);
    }
}

From source file:io.prestosql.plugin.jdbc.StandardColumnMappings.java

public static LongWriteFunction tinyintWriteFunction() {
    return (statement, index, value) -> statement.setByte(index, SignedBytes.checkedCast(value));
}

From source file:com.facebook.presto.type.TinyintOperators.java

@ScalarOperator(NEGATION)
@SqlType(StandardTypes.TINYINT)//from  w  ww.jav a2  s .  c  o m
public static long negate(@SqlType(StandardTypes.TINYINT) long value) {
    try {
        return SignedBytes.checkedCast(-value);
    } catch (IllegalArgumentException e) {
        throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "tinyint negation overflow: " + value, e);
    }
}

From source file:io.prestosql.plugin.hive.HiveBucketing.java

private static int hash(TypeInfo type, Block block, int position) {
    // This function mirrors the behavior of function hashCode in
    // HIVE-12025 ba83fd7bff serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java
    // https://github.com/apache/hive/blob/ba83fd7bff/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java

    // HIVE-7148 proposed change to bucketing hash algorithms. If that gets implemented, this function will need to change significantly.

    if (block.isNull(position)) {
        return 0;
    }//from w  w  w .ja  va 2s  . c o m

    switch (type.getCategory()) {
    case PRIMITIVE: {
        PrimitiveTypeInfo typeInfo = (PrimitiveTypeInfo) type;
        PrimitiveCategory primitiveCategory = typeInfo.getPrimitiveCategory();
        Type prestoType = requireNonNull(HiveType.getPrimitiveType(typeInfo));
        switch (primitiveCategory) {
        case BOOLEAN:
            return prestoType.getBoolean(block, position) ? 1 : 0;
        case BYTE:
            return SignedBytes.checkedCast(prestoType.getLong(block, position));
        case SHORT:
            return Shorts.checkedCast(prestoType.getLong(block, position));
        case INT:
            return toIntExact(prestoType.getLong(block, position));
        case LONG:
            long bigintValue = prestoType.getLong(block, position);
            return (int) ((bigintValue >>> 32) ^ bigintValue);
        case FLOAT:
            return (int) prestoType.getLong(block, position);
        case DOUBLE:
            long doubleValue = doubleToLongBits(prestoType.getDouble(block, position));
            return (int) ((doubleValue >>> 32) ^ doubleValue);
        case STRING:
            return hashBytes(0, prestoType.getSlice(block, position));
        case VARCHAR:
            return hashBytes(1, prestoType.getSlice(block, position));
        case DATE:
            // day offset from 1970-01-01
            long days = prestoType.getLong(block, position);
            return toIntExact(days);
        case TIMESTAMP:
            long millisSinceEpoch = prestoType.getLong(block, position);
            // seconds << 30 + nanoseconds
            long secondsAndNanos = (Math.floorDiv(millisSinceEpoch, 1000L) << 30)
                    + Math.floorMod(millisSinceEpoch, 1000);
            return (int) ((secondsAndNanos >>> 32) ^ secondsAndNanos);
        default:
            throw new UnsupportedOperationException(
                    "Computation of Hive bucket hashCode is not supported for Hive primitive category: "
                            + primitiveCategory.toString() + ".");
        }
    }
    case LIST: {
        Block elementsBlock = block.getObject(position, Block.class);
        return hashOfList((ListTypeInfo) type, elementsBlock);
    }
    case MAP: {
        Block elementsBlock = block.getObject(position, Block.class);
        return hashOfMap((MapTypeInfo) type, elementsBlock);
    }
    default:
        // TODO: support more types, e.g. ROW
        throw new UnsupportedOperationException(
                "Computation of Hive bucket hashCode is not supported for Hive category: "
                        + type.getCategory().toString() + ".");
    }
}

From source file:com.facebook.presto.plugin.jdbc.JdbcPageSink.java

private void appendColumn(Page page, int position, int channel) throws SQLException {
    Block block = page.getBlock(channel);
    int parameter = channel + 1;

    if (block.isNull(position)) {
        statement.setObject(parameter, null);
        return;//from  ww  w. j  a va2  s  . c o  m
    }

    Type type = columnTypes.get(channel);
    if (BOOLEAN.equals(type)) {
        statement.setBoolean(parameter, type.getBoolean(block, position));
    } else if (BIGINT.equals(type)) {
        statement.setLong(parameter, type.getLong(block, position));
    } else if (INTEGER.equals(type)) {
        statement.setInt(parameter, toIntExact(type.getLong(block, position)));
    } else if (SMALLINT.equals(type)) {
        statement.setShort(parameter, Shorts.checkedCast(type.getLong(block, position)));
    } else if (TINYINT.equals(type)) {
        statement.setByte(parameter, SignedBytes.checkedCast(type.getLong(block, position)));
    } else if (DOUBLE.equals(type)) {
        statement.setDouble(parameter, type.getDouble(block, position));
    } else if (REAL.equals(type)) {
        statement.setFloat(parameter, intBitsToFloat(toIntExact(type.getLong(block, position))));
    } else if (type instanceof DecimalType) {
        statement.setBigDecimal(parameter, readBigDecimal((DecimalType) type, block, position));
    } else if (isVarcharType(type) || isCharType(type)) {
        statement.setString(parameter, type.getSlice(block, position).toStringUtf8());
    } else if (VARBINARY.equals(type)) {
        statement.setBytes(parameter, type.getSlice(block, position).getBytes());
    } else if (DATE.equals(type)) {
        // convert to midnight in default time zone
        long utcMillis = DAYS.toMillis(type.getLong(block, position));
        long localMillis = getInstanceUTC().getZone().getMillisKeepLocal(DateTimeZone.getDefault(), utcMillis);
        statement.setDate(parameter, new Date(localMillis));
    } else {
        throw new PrestoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
    }
}

From source file:com.facebook.presto.type.FloatOperators.java

@ScalarOperator(CAST)
@SqlType(StandardTypes.TINYINT)//  w ww.  j av a 2s  .com
public static long castToTinyint(@SqlType(StandardTypes.FLOAT) long value) {
    try {
        return SignedBytes.checkedCast((long) MathFunctions.round((double) intBitsToFloat((int) value)));
    } catch (IllegalArgumentException e) {
        throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for tinyint: " + value, e);
    }
}

From source file:com.facebook.presto.type.IntegerOperators.java

@ScalarOperator(CAST)
@SqlType(StandardTypes.TINYINT)/*from  ww  w.  j  a  v  a  2s.  c o  m*/
public static long castToTinyint(@SqlType(StandardTypes.INTEGER) long value) {
    try {
        return SignedBytes.checkedCast(value);
    } catch (IllegalArgumentException e) {
        throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for tinyint: " + value, e);
    }
}

From source file:io.prestosql.type.SmallintOperators.java

@ScalarOperator(CAST)
@SqlType(StandardTypes.TINYINT)/*w w  w.j a va2  s  .c  o m*/
public static long castToTinyint(@SqlType(StandardTypes.SMALLINT) long value) {
    try {
        return SignedBytes.checkedCast(value);
    } catch (IllegalArgumentException e) {
        throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for tinyint: " + value, e);
    }
}