Skip to content

Commit e93d591

Browse files
committed
Only push down unstrict casts
1 parent caa5046 commit e93d591

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

duckdb/polars_io.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ def _pl_tree_to_sql(tree: _ExpressionTree) -> str:
162162
if node_type == "Cast":
163163
cast_tree = tree[node_type]
164164
assert isinstance(cast_tree, dict), f"A {node_type} should be a dict but got {type(cast_tree)}"
165+
if cast_tree.get("options") != "NonStrict":
166+
msg = f"Only NonStrict casts can be safely unwrapped, got {cast_tree.get('options')!r}"
167+
raise NotImplementedError(msg)
165168
cast_expr = cast_tree["expr"]
166169
assert isinstance(cast_expr, dict), f"A {node_type} should be a dict but got {type(cast_expr)}"
167170
return _pl_tree_to_sql(cast_expr)

tests/fast/arrow/test_polars.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,3 +739,9 @@ def test_polars_lazy_pushdown_decimal_with_cast(self):
739739

740740
assert lazy_df.filter(pl.col("a") == 1).collect().to_dicts() == [{"a": 1}]
741741
assert lazy_df.filter(pl.col("a") > 1).collect().to_dicts() == [{"a": 10}, {"a": 100}]
742+
743+
def test_explicit_cast_not_pushed_down(self):
744+
"""Explicit user .cast() (Strict) should not be pushed down - falls back to Polars."""
745+
# pl.col("a").cast(pl.Int64) produces a Strict Cast node
746+
expr = pl.col("a").cast(pl.Int64) > 5
747+
invalid_filter(expr)

0 commit comments

Comments
 (0)