/** * Converts {@link BigDecimal} to {@link Slice} representing it for long {@link DecimalType}. * It is caller responsibility to ensure that {@code value.scale()} equals to {@link DecimalType#getScale()}. */ public static Slice encodeScaledValue(BigDecimal value) { return encodeUnscaledValue(value.unscaledValue()); }
public static Slice currentTokenAsLongDecimal(JsonParser parser, int precision, int scale) throws IOException { BigDecimal bigDecimal = currentTokenAsJavaDecimal(parser, precision, scale); if (bigDecimal == null) { return null; } return encodeUnscaledValue(bigDecimal.unscaledValue()); }
@UsedByGeneratedCode public static Slice multiplyShortShortLong(long a, long b) { return multiplyLongLongLong(encodeUnscaledValue(a), encodeUnscaledValue(b)); }
private Slice parseSlice(Slice slice, int offset, int length) { BigDecimal decimal = parseBigDecimal(slice, offset, length); return encodeUnscaledValue(decimal.unscaledValue()); }
public static Slice longDecimalPartitionKey(String value, DecimalType type, String name) { return Decimals.encodeUnscaledValue(decimalPartitionKey(value, type, name).unscaledValue()); }
private static Slice encodeUnscaledValue(String unscaledValue) { return Decimals.encodeUnscaledValue(new BigInteger(unscaledValue)); } }
@UsedByGeneratedCode public static Slice shortDecimalToLongDecimal(long value, int sourcePrecision, int sourceScale, int resultPrecision, int resultScale) { return encodeUnscaledValue(bigintToBigintFloorSaturatedCast(BigInteger.valueOf(value), sourceScale, resultPrecision, resultScale)); }
@Test public void testUnscaledValueToSlice() { assertEquals(encodeUnscaledValue(0L), encodeUnscaledValue(BigInteger.valueOf(0L))); assertEquals(encodeUnscaledValue(1L), encodeUnscaledValue(BigInteger.valueOf(1L))); assertEquals(encodeUnscaledValue(-1L), encodeUnscaledValue(BigInteger.valueOf(-1L))); assertEquals(encodeUnscaledValue(Long.MAX_VALUE), encodeUnscaledValue(BigInteger.valueOf(Long.MAX_VALUE))); assertEquals(encodeUnscaledValue(Long.MIN_VALUE), encodeUnscaledValue(BigInteger.valueOf(Long.MIN_VALUE))); }
@UsedByGeneratedCode public static Slice genericIntegerTypeToLongDecimal(long value, int resultPrecision, int resultScale) { return encodeUnscaledValue(bigDecimalToBigintFloorSaturatedCast(BigDecimal.valueOf(value), resultPrecision, resultScale)); } }
public static Block decimalArrayBlockOf(DecimalType type, BigDecimal decimal) { if (type.isShort()) { long longDecimal = decimal.unscaledValue().longValue(); return arrayBlockOf(type, longDecimal); } else { Slice sliceDecimal = Decimals.encodeUnscaledValue(decimal.unscaledValue()); return arrayBlockOf(type, sliceDecimal); } }
public static Block decimalMapBlockOf(DecimalType type, BigDecimal decimal) { if (type.isShort()) { long longDecimal = decimal.unscaledValue().longValue(); return mapBlockOf(type, type, longDecimal, longDecimal); } else { Slice sliceDecimal = Decimals.encodeUnscaledValue(decimal.unscaledValue()); return mapBlockOf(type, type, sliceDecimal, sliceDecimal); } }
@UsedByGeneratedCode public static Slice multiplyShortLongLong(long a, Slice b) { return multiplyLongLongLong(encodeUnscaledValue(a), b); }
@UsedByGeneratedCode public static Slice multiplyLongShortLong(Slice a, long b) { return multiplyLongLongLong(a, encodeUnscaledValue(b)); }
public static Slice getLongDecimalValue(HiveDecimalWritable writable, int columnScale) { BigInteger value = new BigInteger(writable.getInternalStorage()); value = rescale(value, writable.getScale(), columnScale); return encodeUnscaledValue(value); } }
@UsedByGeneratedCode public static Slice varcharToLongDecimal(Slice value, long precision, long scale, BigInteger tenToScale) { BigDecimal result; String stringValue = value.toString(UTF_8); try { result = new BigDecimal(stringValue).setScale(intScale(scale), HALF_UP); } catch (NumberFormatException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s). Value is not a number.", stringValue, precision, scale)); } if (overflows(result, precision)) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast VARCHAR '%s' to DECIMAL(%s, %s). Value too large.", stringValue, precision, scale)); } return encodeUnscaledValue(result.unscaledValue()); }
@Override protected void writeValue(DecimalOutputStream outputStream, Long value) { outputStream.writeUnscaledValue(Decimals.encodeUnscaledValue(value)); }
@UsedByGeneratedCode public static Slice longDecimalToLongDecimal(Slice value, int sourcePrecision, int sourceScale, int resultPrecision, int resultScale) { return encodeUnscaledValue(bigintToBigintFloorSaturatedCast(decodeUnscaledValue(value), sourceScale, resultPrecision, resultScale)); }
@LiteralParameters({"p", "s"}) @SqlType("decimal(p, s)") public static Slice negate(@SqlType("decimal(p, s)") Slice arg) { BigInteger argBigInteger = Decimals.decodeUnscaledValue(arg); return encodeUnscaledValue(argBigInteger.negate()); } }
@Override protected void readValue(BlockBuilder blockBuilder, Type type) { if (definitionLevel == columnDescriptor.getMaxDefinitionLevel()) { Binary value = valuesReader.readBytes(); type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(new BigInteger(value.getBytes()))); } else if (isValueNull()) { blockBuilder.appendNull(); } }
public static Block createLongDecimalSequenceBlock(int start, int end, DecimalType type) { BlockBuilder builder = type.createFixedSizeBlockBuilder(end - start); BigInteger base = BigInteger.TEN.pow(type.getScale()); for (int i = start; i < end; ++i) { type.writeSlice(builder, encodeUnscaledValue(BigInteger.valueOf(i).multiply(base))); } return builder.build(); }