Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyiceberg/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def _(primitive_type: PrimitiveType, value_str: str) -> int:
_, _, exponent = Decimal(value_str).as_tuple()
if exponent != 0: # Raise if there are digits to the right of the decimal
raise ValueError(f"Cannot convert partition value, value cannot have fractional digits for {primitive_type} partition")
return int(float(value_str))
return int(value_str)


@partition_to_py.register(FloatType)
Expand Down
19 changes: 13 additions & 6 deletions pyiceberg/expressions/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@
UUID_BYTES_LENGTH = 16


def _parse_numeric_string(value: str) -> Decimal:
number = Decimal(value)
if not number.is_finite():
raise ValueError(f"Cannot convert non-finite numeric string: {value}")
return number


class Literal(IcebergRootModel[L], Generic[L], ABC): # type: ignore
"""Literal which has a value and can be converted between types."""

Expand Down Expand Up @@ -555,27 +562,27 @@ def _(self, _: StringType) -> Literal[str]:
@to.register(IntegerType)
def _(self, type_var: IntegerType) -> Literal[int]:
try:
number = int(float(self.value))
number = _parse_numeric_string(self.value)

if IntegerType.max < number:
return IntAboveMax()
elif IntegerType.min > number:
return IntBelowMin()
return LongLiteral(number)
except ValueError as e:
return LongLiteral(int(number))
except (ArithmeticError, OverflowError, ValueError) as e:
raise ValueError(f"Could not convert {self.value} into a {type_var}") from e

@to.register(LongType)
def _(self, type_var: LongType) -> Literal[int]:
try:
long_value = int(float(self.value))
long_value = _parse_numeric_string(self.value)
if LongType.max < long_value:
return LongAboveMax()
elif LongType.min > long_value:
return LongBelowMin()
else:
return LongLiteral(long_value)
except (TypeError, ValueError) as e:
return LongLiteral(int(long_value))
except (ArithmeticError, OverflowError, TypeError, ValueError) as e:
raise ValueError(f"Could not convert {self.value} into a {type_var}") from e

@to.register(DateType)
Expand Down
33 changes: 33 additions & 0 deletions tests/expressions/test_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
IntAboveMax,
IntBelowMin,
Literal,
LongAboveMax,
LongLiteral,
StringLiteral,
TimeLiteral,
Expand Down Expand Up @@ -845,6 +846,38 @@ def test_string_to_int_min_value() -> None:
assert isinstance(literal(str(IntegerType.min - 1)).to(IntegerType()), IntBelowMin)


def test_string_to_long_max_value_without_precision_loss() -> None:
assert literal(str(LongType.max)).to(LongType()) == literal(LongType.max)


def test_string_to_long_large_integer_without_precision_loss() -> None:
assert literal("9007199254740993").to(LongType()) == literal(9007199254740993)


def test_string_to_long_decimal_like_integer_without_precision_loss() -> None:
assert literal("9007199254740993.0").to(LongType()) == literal(9007199254740993)


def test_string_to_long_scientific_notation_integer_without_precision_loss() -> None:
assert literal("9007199254740993e0").to(LongType()) == literal(9007199254740993)


def test_string_to_long_max_decimal_like_integer_without_precision_loss() -> None:
assert literal(f"{LongType.max}.0").to(LongType()) == literal(LongType.max)


def test_string_to_integer_scientific_notation_without_regression() -> None:
assert literal("1e3").to(IntegerType()) == literal(1000)


def test_string_to_integer_large_scientific_notation_above_max() -> None:
assert isinstance(literal("1e1000000").to(IntegerType()), IntAboveMax)


def test_string_to_long_large_scientific_notation_above_max() -> None:
assert isinstance(literal("1e1000000").to(LongType()), LongAboveMax)


def test_string_to_integer_type_invalid_value() -> None:
with pytest.raises(ValueError) as e:
_ = literal("abc").to(IntegerType())
Expand Down
3 changes: 3 additions & 0 deletions tests/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,11 @@ def test_unscaled_to_decimal(unscaled: int, scale: int, expected_result: Decimal
(IntegerType(), "1", 1),
(IntegerType(), "9999", 9999),
(LongType(), "123456789", 123456789),
(LongType(), "9007199254740993", 9007199254740993),
(LongType(), str(LongType.max), LongType.max),
(FloatType(), "1.1", 1.1),
(DoubleType(), "99999.9", 99999.9),
(TimestampNanoType(), "9007199254740993", 9007199254740993),
(DecimalType(5, 2), "123.45", Decimal("123.45")),
(StringType(), "foo", "foo"),
(UUIDType(), "f79c3e09-677c-4bbd-a479-3f349cb785e7", uuid.UUID("f79c3e09-677c-4bbd-a479-3f349cb785e7")),
Expand Down