public static BigInteger rescale(BigInteger value, int fromScale, int toScale) { if (toScale < fromScale) { throw new IllegalArgumentException("target scale must be larger than source scale"); } return value.multiply(bigIntegerTenToNth(toScale - fromScale)); }
public static boolean overflows(BigInteger value, int precision) { return value.abs().compareTo(bigIntegerTenToNth(precision)) >= 0; }
private static BigInteger bigDecimalToBigintFloorSaturatedCast(BigDecimal bigDecimal, int resultPrecision, int resultScale) { BigDecimal rescaledValue = bigDecimal.setScale(resultScale, FLOOR); BigInteger unscaledValue = rescaledValue.unscaledValue(); BigInteger maxUnscaledValue = bigIntegerTenToNth(resultPrecision).subtract(ONE); if (unscaledValue.compareTo(maxUnscaledValue) > 0) { return maxUnscaledValue; } BigInteger minUnscaledValue = maxUnscaledValue.negate(); if (unscaledValue.compareTo(minUnscaledValue) < 0) { return minUnscaledValue; } return unscaledValue; }
BigInteger rescaledDividend = dividendBigInteger.multiply(bigIntegerTenToNth(dividendRescaleFactor)); BigInteger rescaledDivisor = divisorBigInteger.multiply(bigIntegerTenToNth(divisorRescaleFactor)); BigInteger[] expectedQuotientAndRemainder = rescaledDividend.divideAndRemainder(rescaledDivisor); BigInteger expectedQuotient = expectedQuotientAndRemainder[0]; BigInteger expectedRemainder = expectedQuotientAndRemainder[1]; boolean overflowIsExpected = expectedQuotient.abs().compareTo(bigIntegerTenToNth(38)) >= 0 || expectedRemainder.abs().compareTo(bigIntegerTenToNth(38)) >= 0;
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(); }
private static SqlScalarFunction castFunctionToDecimalFromBuilder(TypeSignature from, boolean nullableResult, String... methodNames) { Signature signature = Signature.builder() .kind(SCALAR) .operatorType(CAST) .argumentTypes(from) .returnType(parseTypeSignature("decimal(precision,scale)", ImmutableSet.of("precision", "scale"))) .build(); return SqlScalarFunction.builder(DecimalCasts.class) .signature(signature) .deterministic(true) .choice(choice -> choice .nullableResult(nullableResult) .implementation(methodsGroup -> methodsGroup .methods(methodNames) .withExtraParameters((context) -> { DecimalType resultType = (DecimalType) context.getReturnType(); Number tenToScale; if (isShortDecimal(resultType)) { tenToScale = longTenToNth(resultType.getScale()); } else { tenToScale = bigIntegerTenToNth(resultType.getScale()); } return ImmutableList.of(resultType.getPrecision(), resultType.getScale(), tenToScale); }))).build(); }
public static BigInteger rescale(BigInteger value, int fromScale, int toScale) { if (toScale < fromScale) { throw new IllegalArgumentException("target scale must be larger than source scale"); } return value.multiply(bigIntegerTenToNth(toScale - fromScale)); }
public static boolean overflows(BigInteger value, int precision) { return value.abs().compareTo(bigIntegerTenToNth(precision)) >= 0; }