Skip to content

Commit fb96df1

Browse files
authored
Return bounds sentinels for long date literals (#3546)
1 parent 7fff821 commit fb96df1

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

pyiceberg/expressions/literals.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ def _(self, _: DoubleType) -> Literal[float]:
329329

330330
@to.register(DateType)
331331
def _(self, _: DateType) -> Literal[int]:
332+
if IntegerType.max < self.value:
333+
return IntAboveMax()
334+
elif IntegerType.min > self.value:
335+
return IntBelowMin()
332336
return DateLiteral(self.value)
333337

334338
@to.register(TimeType)

tests/expressions/test_expressions.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
from pyiceberg.schema import Accessor, Schema
6868
from pyiceberg.typedef import Record
6969
from pyiceberg.types import (
70+
DateType,
7071
DecimalType,
7172
DoubleType,
7273
FloatType,
@@ -1150,6 +1151,26 @@ def test_above_int_bounds_greater_than_or_equal(
11501151
assert GreaterThanOrEqual("a", below_int_min).bind(int_schema) is AlwaysTrue()
11511152

11521153

1154+
@pytest.fixture
1155+
def date_schema() -> Schema:
1156+
return Schema(NestedField(field_id=1, name="a", field_type=DateType(), required=False))
1157+
1158+
1159+
def test_above_date_bounds_equal_to(date_schema: Schema, above_int_max: Literal[int], below_int_min: Literal[int]) -> None:
1160+
assert EqualTo("a", above_int_max).bind(date_schema) is AlwaysFalse()
1161+
assert EqualTo("a", below_int_min).bind(date_schema) is AlwaysFalse()
1162+
1163+
1164+
def test_above_date_bounds_less_than(date_schema: Schema, above_int_max: Literal[int], below_int_min: Literal[int]) -> None:
1165+
assert LessThan("a", above_int_max).bind(date_schema) is AlwaysTrue()
1166+
assert LessThan("a", below_int_min).bind(date_schema) is AlwaysFalse()
1167+
1168+
1169+
def test_above_date_bounds_greater_than(date_schema: Schema, above_int_max: Literal[int], below_int_min: Literal[int]) -> None:
1170+
assert GreaterThan("a", above_int_max).bind(date_schema) is AlwaysFalse()
1171+
assert GreaterThan("a", below_int_min).bind(date_schema) is AlwaysTrue()
1172+
1173+
11531174
@pytest.fixture
11541175
def float_schema() -> Schema:
11551176
return Schema(NestedField(field_id=1, name="a", field_type=FloatType(), required=False))

tests/expressions/test_literals.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ def test_integer_to_date_conversion() -> None:
160160
assert date_lit.value == date_delta
161161

162162

163+
def test_long_to_date_outside_bound() -> None:
164+
big_lit = literal(IntegerType.max + 1).to(LongType())
165+
above_max_lit = big_lit.to(DateType())
166+
assert above_max_lit == IntAboveMax()
167+
168+
small_lit = literal(IntegerType.min - 1).to(LongType())
169+
below_min_lit = small_lit.to(DateType())
170+
assert below_min_lit == IntBelowMin()
171+
172+
163173
def test_long_to_integer_within_bound() -> None:
164174
lit = literal(34).to(LongType())
165175
int_lit = lit.to(IntegerType())

0 commit comments

Comments
 (0)