|
43 | 43 | Reference, |
44 | 44 | StartsWith, |
45 | 45 | ) |
46 | | -from pyiceberg.expressions.literals import DecimalLiteral, LongLiteral, literal |
| 46 | +from pyiceberg.expressions.literals import DecimalLiteral, DoubleLiteral, LongLiteral, literal |
| 47 | +from pyiceberg.expressions.visitors import bind |
| 48 | +from pyiceberg.schema import Schema |
| 49 | +from pyiceberg.types import DecimalType, DoubleType, LongType, NestedField |
47 | 50 |
|
48 | 51 |
|
49 | 52 | def test_always_true() -> None: |
@@ -152,18 +155,54 @@ def test_in_mixed_numbers() -> None: |
152 | 155 | assert result == In("foo", {LongLiteral(5), DecimalLiteral(Decimal("6.5"))}), values |
153 | 156 | assert isinstance(result, In) |
154 | 157 | assert {type(value) for value in result.literals} == {LongLiteral, DecimalLiteral}, values |
155 | | - assert NotIn("foo", {LongLiteral(5), DecimalLiteral(Decimal("1e3"))}) == parser.parse("foo not in (5, 1e3)") |
| 158 | + |
| 159 | + result = parser.parse("foo not in (5, 6.5, 1e3)") |
| 160 | + assert result == NotIn("foo", {LongLiteral(5), DecimalLiteral(Decimal("6.5")), DoubleLiteral(1000.0)}) |
| 161 | + assert isinstance(result, NotIn) |
| 162 | + assert {type(value) for value in result.literals} == {LongLiteral, DecimalLiteral, DoubleLiteral} |
156 | 163 |
|
157 | 164 |
|
158 | 165 | def test_scientific_notation() -> None: |
| 166 | + # A number with an exponent is a double, as in ANSI SQL |
| 167 | + for expression, expected in [ |
| 168 | + ("foo > 1e-3", GreaterThan("foo", 0.001)), |
| 169 | + ("1e-3 < foo", GreaterThan("foo", 0.001)), |
| 170 | + ("foo < -1.5E3", LessThan("foo", -1500.0)), |
| 171 | + ("foo = 2e2", EqualTo("foo", 200.0)), |
| 172 | + ("foo = .5e1", EqualTo("foo", 5.0)), |
| 173 | + ]: |
| 174 | + result = parser.parse(expression) |
| 175 | + assert result == expected, expression |
| 176 | + assert isinstance(result.literal, DoubleLiteral), expression |
| 177 | + |
| 178 | + assert And(GreaterThanOrEqual("foo", 10.0), LessThanOrEqual("foo", 25.0)) == parser.parse("foo between 1e1 and 2.5e1") |
| 179 | + |
| 180 | + |
| 181 | +def test_bind_numeric_literals() -> None: |
| 182 | + schema = Schema( |
| 183 | + NestedField(1, "dec", DecimalType(9, 2), required=False), |
| 184 | + NestedField(2, "lng", LongType(), required=False), |
| 185 | + NestedField(3, "dbl", DoubleType(), required=False), |
| 186 | + ) |
159 | 187 | for expression, expected in [ |
160 | | - ("foo > 1e-3", GreaterThan("foo", Decimal("0.001"))), |
161 | | - ("1e-3 < foo", GreaterThan("foo", Decimal("0.001"))), |
162 | | - ("foo < -1.5E3", LessThan("foo", Decimal("-1500"))), |
163 | | - ("foo = 2e2", EqualTo("foo", Decimal("200"))), |
164 | | - ("foo between 1e1 and 2.5e1", And(GreaterThanOrEqual("foo", Decimal("10")), LessThanOrEqual("foo", Decimal("25")))), |
| 188 | + ("dec = 1e3", DecimalLiteral(Decimal("1000.00"))), |
| 189 | + ("dec > 2.5e-1", DecimalLiteral(Decimal("0.25"))), |
| 190 | + ("dec = 12.34", DecimalLiteral(Decimal("12.34"))), |
| 191 | + ("lng = 1e3", LongLiteral(1000)), |
| 192 | + ("lng = 7", LongLiteral(7)), |
| 193 | + ("dbl > 1e-3", DoubleLiteral(0.001)), |
| 194 | + ("dbl > 2.5", DoubleLiteral(2.5)), |
165 | 195 | ]: |
166 | | - assert parser.parse(expression) == expected, expression |
| 196 | + bound = bind(schema, parser.parse(expression), case_sensitive=True) |
| 197 | + assert bound.literal == expected, expression |
| 198 | + assert type(bound.literal) is type(expected), expression |
| 199 | + if isinstance(expected, DecimalLiteral): |
| 200 | + assert bound.literal.value.as_tuple() == expected.value.as_tuple(), expression |
| 201 | + |
| 202 | + bound = bind(schema, parser.parse("lng in (1, 2e1, 3)"), case_sensitive=True) |
| 203 | + assert bound.literals == {LongLiteral(1), LongLiteral(20), LongLiteral(3)} |
| 204 | + with pytest.raises(ValueError, match="value has a fractional part"): |
| 205 | + bind(schema, parser.parse("lng = 1.5e0"), case_sensitive=True) |
167 | 206 |
|
168 | 207 |
|
169 | 208 | def test_simple_and() -> None: |
|
0 commit comments