public static void checkLiteralOverflowInBinaryStyle(BigDecimal value, ScalarType scalarType)
            throws AnalysisException {
        int realPrecision = getRealPrecision(value);
        int realScale = getRealScale(value);
        BigInteger underlyingInt = value.setScale(scalarType.getScalarScale(), RoundingMode.HALF_UP).unscaledValue();
        int numBytes = scalarType.getPrimitiveType().getTypeSize();
        // In BE, Overflow checking uses maximum/minimum binary values instead of maximum/minimum decimal values.
        // for instance: if PrimitiveType is decimal32, then 2147483647/-2147483648 is used instead of
        // 999999999/-999999999.
        BigInteger maxBinary = BigInteger.ONE.shiftLeft(numBytes * 8 - 1).subtract(BigInteger.ONE);
        BigInteger minBinary = BigInteger.ONE.shiftLeft(numBytes * 8 - 1).negate();
        if (underlyingInt.compareTo(minBinary) < 0 || underlyingInt.compareTo(maxBinary) > 0) {
            String errMsg = String.format(
                    "Typed decimal literal(%s) is overflow, value='%s' (precision=%d, scale=%d)",
                    scalarType.toString(), value.toPlainString(), realPrecision, realScale);
            throw new AnalysisException(errMsg);
        }
    }
@Test
    public void testCheckLiteralOverflowFail() throws AnalysisException {
        BigDecimal decimal32Values[] = {
                new BigDecimal("2147483.6476"),
                new BigDecimal("-2147483.6489"),
                new BigDecimal("2147483.648"),
                new BigDecimal("-2147483.649"),
        };
        ScalarType decimal32p4s3 = ScalarType.createDecimalV3Type(PrimitiveType.DECIMAL32, 4, 3);
        for (BigDecimal dec32 : decimal32Values) {
            try {
                DecimalLiteral.checkLiteralOverflowInBinaryStyle(dec32, decimal32p4s3);
                Assert.fail("should throw exception");
            } catch (Exception ignored) {
            }
        }

        BigDecimal decimal64Values[] = {
                new BigDecimal("9223372036854.775808"),
                new BigDecimal("9223372036854.7758085"),
                new BigDecimal("-9223372036854.775809"),
                new BigDecimal("-9223372036854.7758086"),
        };
        ScalarType decimal64p10s6 = ScalarType.createDecimalV3Type(PrimitiveType.DECIMAL64, 10, 6);
        for (BigDecimal dec64 : decimal64Values) {
            try {
                DecimalLiteral.checkLiteralOverflowInBinaryStyle(dec64, decimal64p10s6);
                Assert.fail("should throw exception");
            } catch (Exception ignored) {
            }
        }

        BigDecimal decimal128Values[] = {
                new BigDecimal("1701411834604692317316873037.15884105728"),
                new BigDecimal("1701411834604692317316873037.158841057285"),
                new BigDecimal("-1701411834604692317316873037.15884105729"),
                new BigDecimal("-1701411834604692317316873037.158841057286"),
        };
        ScalarType decimal128p36s11 = ScalarType.createDecimalV3Type(PrimitiveType.DECIMAL128, 36, 11);
        for (BigDecimal dec128 : decimal128Values) {
            try {
                DecimalLiteral.checkLiteralOverflowInBinaryStyle(dec128, decimal128p36s11);
                Assert.fail("should throw exception");
            } catch (Exception ignored) {
            }
        }
    }