Skip to content
Merged
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
10 changes: 10 additions & 0 deletions duckdb/polars_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ def _pl_tree_to_sql(tree: _ExpressionTree) -> str:
msg = f"Unsupported function type: {func_dict}"
raise NotImplementedError(msg)

if node_type == "Cast":
cast_tree = tree[node_type]
assert isinstance(cast_tree, dict), f"A {node_type} should be a dict but got {type(cast_tree)}"
if cast_tree.get("options") != "NonStrict":
msg = f"Only NonStrict casts can be safely unwrapped, got {cast_tree.get('options')!r}"
raise NotImplementedError(msg)
cast_expr = cast_tree["expr"]
assert isinstance(cast_expr, dict), f"A {node_type} should be a dict but got {type(cast_expr)}"
return _pl_tree_to_sql(cast_expr)

if node_type == "Scalar":
# Detect format: old style (dtype/value) or new style (direct type key)
scalar_tree = tree[node_type]
Expand Down
43 changes: 43 additions & 0 deletions tests/fast/arrow/test_polars.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,3 +702,46 @@ def test_decimal_scale(self):
} }
"""
assert _pl_tree_to_sql(json.loads(scalar_decimal_scale)) == "1"

def test_cast_node_unwraps_inner_expression(self):
"""Cast nodes should be unwrapped to process the inner expression."""
# A Cast wrapping a Column reference
cast_column = json.loads(
'{"Cast": {"expr": {"Column": "a"}, "dtype": {"Decimal": [20, 0]}, "options": "NonStrict"}}'
)
assert _pl_tree_to_sql(cast_column) == '"a"'

# A Cast wrapping a full binary expression
cast_expr = json.loads("""
{
"BinaryExpr": {
"left": {"Cast": {"expr": {"Column": "a"}, "dtype": {"Decimal": [20, 0]}, "options": "NonStrict"}},
"op": "Eq",
"right": {"Literal": {"Int": 1}}
}
}
""")
assert _pl_tree_to_sql(cast_expr) == '("a" = 1)'

def test_cast_node_predicate_pushdown(self):
"""Predicates with Cast nodes should be successfully pushed down."""
# A decimal with non-38 precision produces a Cast node in Polars
expr = pl.col("a") == pl.lit(1, dtype=pl.Decimal(precision=20, scale=0))
valid_filter(expr)

def test_polars_lazy_pushdown_decimal_with_cast(self):
"""End-to-end test: decimal columns with non-38 precision should push down filters."""
con = duckdb.connect()
con.execute("CREATE TABLE test_cast (a DECIMAL(20,0))")
con.execute("INSERT INTO test_cast VALUES (1), (10), (100), (NULL)")
rel = con.sql("FROM test_cast")
lazy_df = rel.pl(lazy=True)

assert lazy_df.filter(pl.col("a") == 1).collect().to_dicts() == [{"a": 1}]
assert lazy_df.filter(pl.col("a") > 1).collect().to_dicts() == [{"a": 10}, {"a": 100}]

def test_explicit_cast_not_pushed_down(self):
"""Explicit user .cast() (Strict) should not be pushed down - falls back to Polars."""
# pl.col("a").cast(pl.Int64) produces a Strict Cast node
expr = pl.col("a").cast(pl.Int64) > 5
invalid_filter(expr)
Loading