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
10 changes: 6 additions & 4 deletions pyiceberg/expressions/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,27 +555,29 @@ def _(self, _: StringType) -> Literal[str]:
@to.register(IntegerType)
def _(self, type_var: IntegerType) -> Literal[int]:
try:
number = int(float(self.value))
dec = Decimal(self.value)
number = int(self.value) if dec.as_tuple().exponent == 0 else int(float(self.value))

if IntegerType.max < number:
return IntAboveMax()
elif IntegerType.min > number:
return IntBelowMin()
return LongLiteral(number)
except ValueError as e:
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))
dec = Decimal(self.value)
long_value = int(self.value) if dec.as_tuple().exponent == 0 else int(float(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:
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
8 changes: 8 additions & 0 deletions tests/expressions/test_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,14 @@ 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_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
Loading