@UsedByGeneratedCode public static Slice longDecimalToLongDecimal(Slice value, int sourcePrecision, int sourceScale, int resultPrecision, int resultScale) { return encodeUnscaledValue(bigintToBigintFloorSaturatedCast(decodeUnscaledValue(value), sourceScale, resultPrecision, resultScale)); }
private static Slice longDecimal(String value) { return encodeScaledValue(new BigDecimal(value)); } }
@VisibleForTesting static double convertPartitionValueToDouble(Type type, Object value) { if (type.equals(BIGINT) || type.equals(INTEGER) || type.equals(SMALLINT) || type.equals(TINYINT)) { return (Long) value; } if (type.equals(DOUBLE)) { return (Double) value; } if (type.equals(REAL)) { return intBitsToFloat(((Long) value).intValue()); } if (type instanceof DecimalType) { DecimalType decimalType = (DecimalType) type; if (isShortDecimal(decimalType)) { return parseDouble(Decimals.toString((Long) value, decimalType.getScale())); } if (isLongDecimal(decimalType)) { return parseDouble(Decimals.toString((Slice) value, decimalType.getScale())); } throw new IllegalArgumentException("Unexpected decimal type: " + decimalType); } if (type.equals(DATE)) { return (Long) value; } throw new IllegalArgumentException("Unexpected type: " + type); }
public static Slice encodeScaledValue(BigDecimal value, int scale) { checkArgument(scale >= 0); return encodeScaledValue(value.setScale(scale, UNNECESSARY)); }
public static Slice getLongDecimalValue(HiveDecimalWritable writable, int columnScale) { BigInteger value = new BigInteger(writable.getInternalStorage()); value = rescale(value, writable.getScale(), columnScale); return encodeUnscaledValue(value); } }
else if (isShortDecimal(type) && columnStatistics.getDecimalStatistics() != null) { return createDomain(type, hasNullValue, columnStatistics.getDecimalStatistics(), value -> rescale(value, (DecimalType) type).unscaledValue().longValue()); else if (isLongDecimal(type) && columnStatistics.getDecimalStatistics() != null) { return createDomain(type, hasNullValue, columnStatistics.getDecimalStatistics(), value -> encodeUnscaledValue(rescale(value, (DecimalType) type).unscaledValue()));
type.writeLong(blockBuilder, ((Number) value).longValue()); else if (Decimals.isShortDecimal(type)) { type.writeLong(blockBuilder, ((SqlDecimal) value).toBigDecimal().unscaledValue().longValue()); else if (Decimals.isLongDecimal(type)) { type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) value).toBigDecimal().unscaledValue()));
@UsedByGeneratedCode public static Slice multiplyShortShortLong(long a, long b) { return multiplyLongLongLong(encodeUnscaledValue(a), encodeUnscaledValue(b)); }
prefilledValue = timestampPartitionKey(columnValue, hiveStorageTimeZone, name); else if (isShortDecimal(type)) { prefilledValue = shortDecimalPartitionKey(columnValue, (DecimalType) type, name); else if (isLongDecimal(type)) { prefilledValue = longDecimalPartitionKey(columnValue, (DecimalType) type, name);
@UsedByGeneratedCode public static boolean longDecimalToBoolean(Slice decimal, long precision, long scale, BigInteger tenToScale) { return !decodeUnscaledValue(decimal).equals(ZERO); }
private static SqlScalarFunction castFunctionFromDecimalTo(TypeSignature to, String... methodNames) { Signature signature = Signature.builder() .kind(SCALAR) .operatorType(CAST) .argumentTypes(parseTypeSignature("decimal(precision,scale)", ImmutableSet.of("precision", "scale"))) .returnType(to) .build(); return SqlScalarFunction.builder(DecimalCasts.class) .signature(signature) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup .methods(methodNames) .withExtraParameters((context) -> { long precision = context.getLiteral("precision"); long scale = context.getLiteral("scale"); Number tenToScale; if (isShortDecimal(context.getParameterTypes().get(0))) { tenToScale = longTenToNth(intScale(scale)); } else { tenToScale = bigIntegerTenToNth(intScale(scale)); } return ImmutableList.of(precision, scale, tenToScale); }))) .build(); }
case StandardTypes.DECIMAL: DecimalType decimalType = (DecimalType) type; if (isShortDecimal(decimalType)) { return (block, position) -> Decimals.toString(decimalType.getLong(block, position), decimalType.getScale()); return (block, position) -> Decimals.toString( decodeUnscaledValue(type.getSlice(block, position)), decimalType.getScale());
slice = constantSlice.slice(0, Math.min(((VarcharType) type).getLength(), constantSlice.length())); else if (isLongDecimal(type)) { slice = encodeScaledValue(ZERO);
@Override public void encodeValueInto(Block block, int position, SliceOutput output) { if (isShortDecimal(type)) { writeLong(output, type.getLong(block, position)); } else { writeSlice(output, block, position); } }
@JsonValue @Override public String toString() { return Decimals.toString(unscaledValue, scale); }
protected static SqlDecimal decimal(String decimalString) { DecimalParseResult parseResult = Decimals.parseIncludeLeadingZerosInPrecision(decimalString); BigInteger unscaledValue; if (parseResult.getType().isShort()) { unscaledValue = BigInteger.valueOf((Long) parseResult.getObject()); } else { unscaledValue = Decimals.decodeUnscaledValue((Slice) parseResult.getObject()); } return new SqlDecimal(unscaledValue, parseResult.getType().getPrecision(), parseResult.getType().getScale()); }
public static ReadMapping decimalReadMapping(DecimalType decimalType) { // JDBC driver can return BigDecimal with lower scale than column's scale when there are trailing zeroes int scale = decimalType.getScale(); if (decimalType.isShort()) { return longReadMapping(decimalType, (resultSet, columnIndex) -> encodeShortScaledValue(resultSet.getBigDecimal(columnIndex), scale)); } return sliceReadMapping(decimalType, (resultSet, columnIndex) -> encodeScaledValue(resultSet.getBigDecimal(columnIndex), scale)); }
private void encodeValue(Block block, int position, SliceOutput output) { if (isShortDecimal(type)) { output.writeBytes(utf8Slice(Decimals.toString(type.getLong(block, position), type.getScale()))); } else { output.writeBytes(utf8Slice(Decimals.toString(type.getSlice(block, position), type.getScale()))); } }
public static DecimalParseResult parseIncludeLeadingZerosInPrecision(String stringValue) { return parse(stringValue, true); }
public static BigDecimal parseHiveDecimal(byte[] bytes, int start, int length, DecimalType columnType) { BigDecimal parsed = new BigDecimal(new String(bytes, start, length, UTF_8)); if (parsed.scale() > columnType.getScale()) { // Hive rounds HALF_UP too parsed = parsed.setScale(columnType.getScale(), HALF_UP); } return rescale(parsed, columnType); } }