Java Code Examples for com.google.common.primitives.SignedBytes#checkedCast()
The following examples show how to use
com.google.common.primitives.SignedBytes#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 |
public static Long currentTokenAsTinyint(JsonParser parser) throws IOException { switch (parser.currentToken()) { case VALUE_NULL: return null; case VALUE_STRING: case FIELD_NAME: return VarcharOperators.castToTinyint(Slices.utf8Slice(parser.getText())); case VALUE_NUMBER_FLOAT: return DoubleOperators.castToTinyint(parser.getDoubleValue()); case VALUE_NUMBER_INT: return (long) SignedBytes.checkedCast(parser.getLongValue()); case VALUE_TRUE: return BooleanOperators.castToTinyint(true); case VALUE_FALSE: return BooleanOperators.castToTinyint(false); default: throw new JsonCastException(format("Unexpected token when cast to %s: %s", StandardTypes.TINYINT, parser.getText())); } }
Example 2
Source File: DecimalCasts.java From presto with Apache License 2.0 | 6 votes |
@UsedByGeneratedCode public static long shortDecimalToTinyint(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 SignedBytes.checkedCast(longResult); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to TINYINT", longResult)); } }
Example 3
Source File: MathFunctions.java From presto with Apache License 2.0 | 5 votes |
@Description("Round to nearest integer") @ScalarFunction("round") @SqlType(StandardTypes.TINYINT) public static long roundTinyint(@SqlType(StandardTypes.TINYINT) long num, @SqlType(StandardTypes.INTEGER) long decimals) { long rounded = roundLong(num, decimals); try { return SignedBytes.checkedCast(rounded); } catch (IllegalArgumentException e) { throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for tinyint: " + rounded, e); } }
Example 4
Source File: DoubleOperators.java From presto with Apache License 2.0 | 5 votes |
@ScalarOperator(CAST) @SqlType(StandardTypes.TINYINT) public static long castToTinyint(@SqlType(StandardTypes.DOUBLE) double value) { if (Double.isNaN(value)) { throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast double NaN to tinyint"); } try { return SignedBytes.checkedCast((long) MathFunctions.round(value)); } catch (IllegalArgumentException e) { throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for tinyint: " + value, e); } }
Example 5
Source File: IntegerOperators.java From presto with Apache License 2.0 | 5 votes |
@ScalarOperator(CAST) @SqlType(StandardTypes.TINYINT) 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); } }
Example 6
Source File: RealOperators.java From presto with Apache License 2.0 | 5 votes |
@ScalarOperator(CAST) @SqlType(StandardTypes.TINYINT) public static long castToTinyint(@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 tinyint"); } try { return SignedBytes.checkedCast((long) MathFunctions.round((double) floatValue)); } catch (IllegalArgumentException e) { throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, "Out of range for tinyint: " + floatValue, e); } }
Example 7
Source File: SmallintOperators.java From presto with Apache License 2.0 | 5 votes |
@ScalarOperator(CAST) @SqlType(StandardTypes.TINYINT) 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); } }
Example 8
Source File: BigintOperators.java From presto with Apache License 2.0 | 5 votes |
@ScalarOperator(CAST) @SqlType(StandardTypes.TINYINT) public static long castToTinyint(@SqlType(StandardTypes.BIGINT) 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); } }
Example 9
Source File: DecimalCasts.java From presto with Apache License 2.0 | 5 votes |
@UsedByGeneratedCode public static long longDecimalToTinyint(Slice decimal, long precision, long scale, BigInteger tenToScale) { try { return SignedBytes.checkedCast(unscaledDecimalToUnscaledLong(rescale(decimal, DecimalConversions.intScale(-scale)))); } catch (ArithmeticException | IllegalArgumentException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to TINYINT", Decimals.toString(decimal, DecimalConversions.intScale(scale)))); } }
Example 10
Source File: TinyintOperators.java From presto with Apache License 2.0 | 5 votes |
@ScalarOperator(NEGATION) @SqlType(StandardTypes.TINYINT) 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); } }
Example 11
Source File: TinyintOperators.java From presto with Apache License 2.0 | 5 votes |
@ScalarOperator(MULTIPLY) @SqlType(StandardTypes.TINYINT) 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); } }
Example 12
Source File: TinyintOperators.java From presto with Apache License 2.0 | 5 votes |
@ScalarOperator(SUBTRACT) @SqlType(StandardTypes.TINYINT) 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); } }
Example 13
Source File: TinyintOperators.java From presto with Apache License 2.0 | 5 votes |
@ScalarOperator(ADD) @SqlType(StandardTypes.TINYINT) 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); } }
Example 14
Source File: Field.java From presto with Apache License 2.0 | 4 votes |
/** * 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 15
Source File: HiveBucketingV2.java From presto with Apache License 2.0 | 4 votes |
private static int hash(TypeInfo type, Block block, int position) { // This function mirrors the behavior of function hashCodeMurmur in // HIVE-18910 (and following) 7dc47faddb serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java // https://github.com/apache/hive/blob/7dc47faddba9f079bbe2698aaa4d8712e7654f87/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java if (block.isNull(position)) { return 0; } 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 Murmur3.hash32(bytes(Shorts.checkedCast(prestoType.getLong(block, position)))); case INT: return Murmur3.hash32(bytes(toIntExact(prestoType.getLong(block, position)))); case LONG: return Murmur3.hash32(bytes(prestoType.getLong(block, position))); case FLOAT: // convert to canonical NaN if necessary // Sic! we're `floatToIntBits -> cast to float -> floatToRawIntBits` just as it is (implicitly) done in // https://github.com/apache/hive/blob/7dc47faddba9f079bbe2698aaa4d8712e7654f87/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java#L830 return Murmur3.hash32(bytes(floatToRawIntBits(floatToIntBits(intBitsToFloat(toIntExact(prestoType.getLong(block, position))))))); case DOUBLE: // Sic! we're `doubleToLongBits -> cast to double -> doubleToRawLongBits` just as it is (implicitly) done in // https://github.com/apache/hive/blob/7dc47faddba9f079bbe2698aaa4d8712e7654f87/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java#L836 return Murmur3.hash32(bytes(doubleToRawLongBits(doubleToLongBits(prestoType.getDouble(block, position))))); case STRING: return Murmur3.hash32(prestoType.getSlice(block, position).getBytes()); case VARCHAR: return Murmur3.hash32(prestoType.getSlice(block, position).getBytes()); case DATE: // day offset from 1970-01-01 return Murmur3.hash32(bytes(toIntExact(prestoType.getLong(block, position)))); // case TIMESTAMP: // TODO (https://github.com/prestosql/presto/issues/1706): support bucketing v2 for timestamp default: throw new UnsupportedOperationException("Computation of Hive bucket hashCode is not supported for Hive primitive category: " + primitiveCategory); } case LIST: return hashOfList((ListTypeInfo) type, block.getObject(position, Block.class)); case MAP: return hashOfMap((MapTypeInfo) type, block.getObject(position, Block.class)); 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: HiveBucketingV2.java From presto with Apache License 2.0 | 4 votes |
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 Murmur3.hash32(bytes(Shorts.checkedCast((long) value))); case INT: return Murmur3.hash32(bytes(toIntExact((long) value))); case LONG: return Murmur3.hash32(bytes((long) value)); case FLOAT: // convert to canonical NaN if necessary // Sic! we're `floatToIntBits -> cast to float -> floatToRawIntBits` just as it is (implicitly) done in // https://github.com/apache/hive/blob/7dc47faddba9f079bbe2698aaa4d8712e7654f87/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java#L830 return Murmur3.hash32(bytes(floatToRawIntBits(floatToIntBits(intBitsToFloat(toIntExact((long) value)))))); case DOUBLE: // convert to canonical NaN if necessary // Sic! we're `doubleToLongBits -> cast to double -> doubleToRawLongBits` just as it is (implicitly) done in // https://github.com/apache/hive/blob/7dc47faddba9f079bbe2698aaa4d8712e7654f87/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/ObjectInspectorUtils.java#L836 return Murmur3.hash32(bytes(doubleToRawLongBits(doubleToLongBits((double) value)))); case STRING: return Murmur3.hash32(((Slice) value).getBytes()); case VARCHAR: return Murmur3.hash32(((Slice) value).getBytes()); case DATE: // day offset from 1970-01-01 return Murmur3.hash32(bytes(toIntExact((long) value))); // case TIMESTAMP: // TODO (https://github.com/prestosql/presto/issues/1706): support bucketing v2 for timestamp 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 17
Source File: HiveBucketingV1.java From presto with Apache License 2.0 | 4 votes |
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 if (block.isNull(position)) { return 0; } 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: // convert to canonical NaN if necessary return floatToIntBits(intBitsToFloat(toIntExact(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 return toIntExact(prestoType.getLong(block, position)); case TIMESTAMP: return hashTimestamp(prestoType.getLong(block, position)); default: throw new UnsupportedOperationException("Computation of Hive bucket hashCode is not supported for Hive primitive category: " + primitiveCategory); } case LIST: return hashOfList((ListTypeInfo) type, block.getObject(position, Block.class)); case MAP: return hashOfMap((MapTypeInfo) type, block.getObject(position, Block.class)); 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 18
Source File: HiveBucketingV1.java From presto with Apache License 2.0 | 4 votes |
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()); } }