This is an automated email from the ASF dual-hosted git repository.
jerryshao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/main by this push:
new ad0ef8095a [#14213] fix(python): Support positive-exponent decimal
literals (#14806)
ad0ef8095a is described below
commit ad0ef8095a56b480c08caf5fbbeb5f69aac78027
Author: karmendra8386 <[email protected]>
AuthorDate: Wed Nov 11 22:06:19 2026 -0500
[#14213] fix(python): Support positive-exponent decimal literals (#14806)
### What changes were proposed in this pull request?
Fix `Literals.decimal_literal` in the Python client so decimal values
with a positive exponent are typed correctly. The precision now counts
the zeroes a positive exponent adds, and the scale is clamped to 0, so
for example `Decimal("1E+3")` becomes decimal(4,0) instead of raising
`ValueError` from `DecimalType.of` for the negative scale. Values
without a positive exponent are unchanged.
This PR takes over the draft #14214. On Nov 6, @jerryshao reviewed that
draft on #14213, confirmed the bug against current `main` and that the
fix looks correct, and said that if there was no update in the next few
days I would be welcome to take it over. There has been no update since
Oct 25, so this PR cherry-picks @Kayvan-Zahiri's commit as-is, with his
authorship preserved, and carries it through review. The one thing the
earlier review did not cover was running the Python tests locally, which
is done here (see below).
### Why are the changes needed?
`decimal_literal` computed `scale = -exponent`, so any `Decimal` with a
positive exponent produced a negative scale and was rejected, even
though the value itself is a valid decimal.
Fixes #14213.
### Does this PR introduce _any_ user-facing change?
Decimal literals with positive exponents, which previously raised an
error, now produce a decimal type. No other behavior changes.
### How was this patch tested?
The cherry-picked commit adds regression tests to `test_literals.py`
covering positive exponents, the unchanged cases and the 38-digit
precision limit.
Ran locally on this branch: the literals tests pass (16 passed), and the
full client-python unit suite passes:
```
1434 passed, 1087 subtests passed
```
Pylint on the changed files is 10.00/10 and black check is clean.
---------
Co-authored-by: Kayvan Zahiri
<[email protected]>
---
.../api/rel/expressions/literals/literals.py | 17 ++++++++--
.../tests/unittests/api/rel/test_literals.py | 37 ++++++++++++++++++++++
2 files changed, 52 insertions(+), 2 deletions(-)
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))