Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#14213] fix(python): Support positive-exponent decimal literals (#14806)

karmendra8386ad0ef8095a56
diff --git a/clients/client-python/gravitino/api/rel/expressions/literals/literals.py b/clients/client-python/gravitino/api/rel/expressions/literals/literals.py
index e45fb7fb2e..4af4186aed 100644
--- a/clients/client-python/gravitino/api/rel/expressions/literals/literals.py
+++ b/clients/client-python/gravitino/api/rel/expressions/literals/literals.py
@@ -108,8 +108,21 @@ class Literals:
@staticmethod
def decimal_literal(value: decimal.Decimal) -> LiteralImpl[decimal.Decimal]:
- precision: int = len(value.as_tuple().digits)
- scale: int = -value.as_tuple().exponent
+ """Creates a decimal literal from the given value.
+
+ A positive exponent is normalized into the type: the precision counts
+ the exponent zeroes and the scale is clamped to 0, so Decimal("1E+3")
+ gets the type decimal(4, 0). Java's Decimal instead derives its scale
+ from BigDecimal.scale() and rejects such a value, but a literal
+ travels as a value string with an explicit type, which the server
+ accepts.
+ """
+ if not value.is_finite():
+ raise ValueError("Decimal literal must be a finite number")
+ decimal_tuple = value.as_tuple()
+ # Positive exponents add integer digits, not a negative fractional scale.
+ precision: int = len(decimal_tuple.digits) + max(decimal_tuple.exponent, 0)
+ scale: int = max(-decimal_tuple.exponent, 0)
return LiteralImpl(value, Types.DecimalType.of(max(precision, scale), scale))
@staticmethod
diff --git a/clients/client-python/tests/unittests/api/rel/test_literals.py b/clients/client-python/tests/unittests/api/rel/test_literals.py
index 59b17b2e48..2d80767b96 100644
--- a/clients/client-python/tests/unittests/api/rel/test_literals.py
+++ b/clients/client-python/tests/unittests/api/rel/test_literals.py
@@ -48,6 +48,43 @@ class TestLiterals(unittest.TestCase):
self.assertEqual(decimal_val.value(), Decimal("0.00"))
self.assertEqual(decimal_val.data_type(), Types.DecimalType.of(2, 2))
+ def test_decimal_literal_positive_exponent(self) -> None:
+ for text, precision in (
+ ("1E+3", 4),
+ ("-1.23E+4", 5),
+ ("0E+3", 4),
+ ("1E+37", 38),
+ ):
+ with self.subTest(value=text):
+ value = Decimal(text)
+ literal = Literals.decimal_literal(value)
+ self.assertIs(literal.value(), value)
+ self.assertEqual(
+ literal.data_type(), Types.DecimalType.of(precision, 0)
+ )
+
+ def test_decimal_literal_positive_exponent_precision_limit(self) -> None:
+ with self.assertRaisesRegex(ValueError, "Decimal precision must be in range"):
+ Literals.decimal_literal(Decimal("1E+38"))
+
+ def test_decimal_literal_non_finite(self) -> None:
+ for text in ("NaN", "Infinity", "-Infinity"):
+ with self.subTest(value=text):
+ with self.assertRaisesRegex(
+ ValueError, "Decimal literal must be a finite number"
+ ):
+ Literals.decimal_literal(Decimal(text))
+
+ def test_decimal_literal_retains_fractional_scale(self) -> None:
+ for text, precision, scale in (("1E-3", 3, 3), ("123.40", 5, 2)):
+ with self.subTest(value=text):
+ value = Decimal(text)
+ literal = Literals.decimal_literal(value)
+ self.assertIs(literal.value(), value)
+ self.assertEqual(
+ literal.data_type(), Types.DecimalType.of(precision, scale)
+ )
+
def test_date_literal(self):
date_val = Literals.date_literal(date(2023, 1, 1))
self.assertEqual(date_val.value(), date(2023, 1, 1))