Skip to content

Commit 252a35d

Browse files
committed
tests
1 parent 92943cc commit 252a35d

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

duckdb/polars_io.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations # noqa: D100
22

3+
import contextlib
34
import datetime
45
import json
56
import typing
@@ -178,12 +179,10 @@ def _pl_tree_to_sql(tree: _ExpressionTree) -> str:
178179
assert isinstance(decimal_value, list), (
179180
f"A {dtype} should be a two or three member list but got {type(decimal_value)}"
180181
)
181-
if len(decimal_value) == 2: # pre-polars 1.34.0
182-
return str(Decimal(decimal_value[0]) / Decimal(10 ** decimal_value[1]))
183-
assert len(decimal_value) == 3, ( # since polars 1.34.0
182+
assert 2 >= len(decimal_value) <= 3, (
184183
f"A {dtype} should be a two or three member list but got {len(decimal_value)} member list"
185184
)
186-
return str(Decimal(decimal_value[0]) / Decimal(10 ** decimal_value[2]))
185+
return str(Decimal(decimal_value[0]) / Decimal(10 ** decimal_value[-1]))
187186

188187
# Datetime with microseconds since epoch
189188
if dtype.startswith("{'Datetime'") or dtype == "Datetime":
@@ -265,7 +264,8 @@ def source_generator(
265264
relation_final = relation_final.limit(n_rows)
266265
if predicate is not None:
267266
# We have a predicate, if possible, we push it down to DuckDB
268-
duck_predicate = _predicate_to_expression(predicate)
267+
with contextlib.suppress(AssertionError, KeyError):
268+
duck_predicate = _predicate_to_expression(predicate)
269269
# Try to pushdown filter, if one exists
270270
if duck_predicate is not None:
271271
relation_final = relation_final.filter(duck_predicate)

tests/fast/arrow/test_polars.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
import json
23

34
import pytest
45

@@ -8,7 +9,7 @@
89
arrow = pytest.importorskip("pyarrow")
910
pl_testing = pytest.importorskip("polars.testing")
1011

11-
from duckdb.polars_io import _predicate_to_expression # noqa: E402
12+
from duckdb.polars_io import _pl_tree_to_sql, _predicate_to_expression # noqa: E402
1213

1314

1415
def valid_filter(filter):
@@ -605,3 +606,53 @@ def test_polars_lazy_many_batches(self, duckdb_cursor):
605606
correct = duckdb_cursor.execute("FROM t").fetchall()
606607

607608
assert res == correct
609+
610+
def test_invalid_expr_json(self):
611+
bad_key_expr = """
612+
{
613+
"BinaryExpr": {
614+
"left": { "Column": "foo" },
615+
"middle": "Gt",
616+
"right": { "Literal": { "Int": 5 } }
617+
}
618+
}
619+
"""
620+
with pytest.raises(KeyError, match="'op'"):
621+
_pl_tree_to_sql(json.loads(bad_key_expr))
622+
623+
bad_type_expr = """
624+
{
625+
"BinaryExpr": {
626+
"left": { "Column": [ "foo" ] },
627+
"op": "Gt",
628+
"right": { "Literal": { "Int": 5 } }
629+
}
630+
}
631+
"""
632+
with pytest.raises(AssertionError, match="The col name of a Column should be a str but got"):
633+
_pl_tree_to_sql(json.loads(bad_type_expr))
634+
635+
def test_old_dec(self):
636+
bad_key_expr = """
637+
{
638+
"BinaryExpr": {
639+
"left": { "Column": "foo" },
640+
"middle": "Gt",
641+
"right": { "Literal": { "Int": 5 } }
642+
}
643+
}
644+
"""
645+
with pytest.raises(KeyError, match="'op'"):
646+
_pl_tree_to_sql(json.loads(bad_key_expr))
647+
648+
bad_type_expr = """
649+
{
650+
"BinaryExpr": {
651+
"left": { "Column": [ "foo" ] },
652+
"op": "Gt",
653+
"right": { "Literal": { "Int": 5 } }
654+
}
655+
}
656+
"""
657+
with pytest.raises(AssertionError, match="The col name of a Column should be a str but got"):
658+
_pl_tree_to_sql(json.loads(bad_type_expr))

0 commit comments

Comments
 (0)