Skip to content

fix(expressions): reject non-finite floats on conversion to Decimal - #4287

Open
AP24110011695 wants to merge 1 commit into
apache:mainfrom
AP24110011695:fix/non-finite-float-to-decimal
Open

AP24110011695 wants to merge 1 commit into
apache:mainfrom
AP24110011695:fix/non-finite-float-to-decimal

Conversation

@AP24110011695

Copy link
Copy Markdown

Fixes #4286

Problem

float('inf') is a valid float/double literal (only NaN is rejected at construction), but converting it to a DecimalType leaked a bare decimal.InvalidOperation from inside Decimal.quantize() — with no context and inconsistent with every other impossible conversion in the module, which raises ValueError("Could not convert X into a Y…"):

DoubleLiteral(float("inf")).to(DecimalType(10, 2))
# decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]

Fix

Both FloatLiteral.to(DecimalType) and DoubleLiteral.to(DecimalType) now raise ValueError("Could not convert inf into a decimal(9, 2), value is not finite") before quantizing. The message follows the suffix style introduced for this module in #4029.

Tests

Two regression tests (fail on main with decimal.InvalidOperation, pass with this change):

  • test_float_to_decimal_conversion_with_infinity
  • test_double_to_decimal_conversion_with_infinity

Full tests/expressions/test_literals.py: 177 passed. tests/test_transforms.py: 275 passed with the same 17 pyarrow-parametrized failures as unmodified main in this environment (baseline verified). ruff (pinned 0.14.3) check + format clean on both touched files.


Transparent disclosure: developed and verified with AI assistance under human direction, per the repo's agent guidelines.

float('inf') is a valid float/double literal, but converting it to a
Decimal type crashed with a bare decimal.InvalidOperation instead of the
ValueError every other impossible conversion in this module raises.
NaN is already rejected at literal construction; infinity now gets a
clean, consistent domain error.

Signed-off-by: Ayush Kumar Saha <225407166+AP24110011695@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Converting an infinity float/double literal to Decimal raises a bare decimal.InvalidOperation

1 participant