private static Short intToShort(Integer input) { if (input == null) { return null; } return Shorts.checkedCast(input); }
@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)); } }
private static void assertCastFails(long value) { try { Shorts.checkedCast(value); fail("Cast to short should have failed: " + value); } catch (IllegalArgumentException ex) { assertTrue( value + " not found in exception text: " + ex.getMessage(), ex.getMessage().contains(String.valueOf(value))); } }
@Override public void setField(Block block, int position) { value.set(Shorts.checkedCast(SmallintType.SMALLINT.getLong(block, position))); rowInspector.setStructFieldData(row, field, value); } }
@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(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(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(CAST) @SqlType(StandardTypes.SMALLINT) public static long castToSmallint(@SqlType(StandardTypes.REAL) long value) { try { return Shorts.checkedCast((long) MathFunctions.round((double) intBitsToFloat((int) 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.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.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(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.DOUBLE) double value) { 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); } }
@UsedByGeneratedCode public static long longDecimalToSmallint(Slice decimal, long precision, long scale, BigInteger tenToScale) { try { return Shorts.checkedCast(unscaledDecimalToUnscaledLong(rescale(decimal, intScale(-scale)))); } catch (ArithmeticException | IllegalArgumentException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to SMALLINT", Decimals.toString(decimal, intScale(scale)))); } }
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())); } }
return SignedBytes.checkedCast((long) value); case SHORT: return Shorts.checkedCast((long) value); case INT: return toIntExact((long) value);
statement.setShort(parameter, Shorts.checkedCast(type.getLong(block, position)));
public void testCheckedCast() { for (short value : VALUES) { assertEquals(value, Shorts.checkedCast((long) value)); } assertCastFails(GREATEST + 1L); assertCastFails(LEAST - 1L); assertCastFails(Long.MAX_VALUE); assertCastFails(Long.MIN_VALUE); }
return SignedBytes.checkedCast(prestoType.getLong(block, position)); case SHORT: return Shorts.checkedCast(prestoType.getLong(block, position)); case INT: return toIntExact(prestoType.getLong(block, position));
private static void assertCastFails(long value) { try { Shorts.checkedCast(value); fail("Cast to short should have failed: " + value); } catch (IllegalArgumentException ex) { assertTrue(value + " not found in exception text: " + ex.getMessage(), ex.getMessage().contains(String.valueOf(value))); } }
@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); } }