| 367 | |
| 368 | @pytest.mark.parametrize("arrow_type", [pa.decimal128(3, 2), pa.decimal256(3, 2)]) |
| 369 | def test_sum_decimal_array(arrow_type): |
| 370 | from decimal import Decimal |
| 371 | max_precision_type = ( |
| 372 | pa.decimal128(38, arrow_type.scale) |
| 373 | if pa.types.is_decimal128(arrow_type) |
| 374 | else pa.decimal256(76, arrow_type.scale) |
| 375 | ) |
| 376 | expected_sum = Decimal("5.79") |
| 377 | expected_sum_overflow = Decimal("10.00") |
| 378 | zero = Decimal("0.00") |
| 379 | |
| 380 | # No overflow |
| 381 | arr = pa.array([Decimal("1.23"), Decimal("4.56")], type=arrow_type) |
| 382 | assert arr.sum().as_py() == expected_sum |
| 383 | assert arr.sum().type == max_precision_type |
| 384 | |
| 385 | arr = pa.array([Decimal("1.23"), Decimal("4.56"), None], type=arrow_type) |
| 386 | assert arr.sum().as_py() == expected_sum |
| 387 | assert arr.sum().type == max_precision_type |
| 388 | |
| 389 | # With overflow |
| 390 | arr = pa.array([Decimal("1.23"), Decimal("8.77")], type=arrow_type) |
| 391 | assert arr.sum().as_py() == expected_sum_overflow |
| 392 | assert arr.sum().type == max_precision_type |
| 393 | |
| 394 | arr = pa.array([Decimal("1.23"), Decimal("8.77"), None], type=arrow_type) |
| 395 | assert arr.sum().as_py() == expected_sum_overflow |
| 396 | assert arr.sum().type == max_precision_type |
| 397 | |
| 398 | arr = pa.array([None], type=arrow_type) |
| 399 | assert arr.sum().as_py() is None # noqa: E711 |
| 400 | assert arr.sum().type == max_precision_type # noqa: E711 |
| 401 | assert arr.sum(min_count=0).as_py() == zero |
| 402 | assert arr.sum(min_count=0).type == max_precision_type |
| 403 | |
| 404 | arr = pa.array([], type=arrow_type) |
| 405 | assert arr.sum().as_py() is None # noqa: E711 |
| 406 | assert arr.sum().type == max_precision_type # noqa: E711 |
| 407 | assert arr.sum(min_count=0).as_py() == zero |
| 408 | assert arr.sum(min_count=0).type == max_precision_type |
| 409 | |
| 410 | |
| 411 | @pytest.mark.parametrize('arrow_type', numerical_arrow_types) |