Commit bf6b47c
authored
Support Cast node in pushdown logic (#298)
Related: #98
When the decimal precision is anything other than 38, Polars wraps the
expression in a Cast node. `_pl_tree_to_sql` didn't handle Cast nodes,
causing silent pushdown failure. This fix makes sure these nodes get
pushed down as well.
```
import duckdb
import polars as pl
import json
# Create a decimal column with precision != 38
con = duckdb.connect()
rel = con.sql("SELECT a::DECIMAL(20,0) AS a FROM range(10) AS t(a)")
lazy_df = rel.pl(lazy=True)
# This filter works but pushdown silently fails
result = lazy_df.filter(pl.col("a") == 1).collect()
print(f"Result: {len(result)} rows") # Returns 1 row (correct)
# Polars serialized it as follows
expr = pl.col("a") == 1
tree = json.loads(expr.meta.serialize(format="json"))
print(json.dumps(tree, indent=2))
# Contains: "Cast": {"expr": ..., "dtype": {"Decimal": [20, 0]}, ...}
```
Note that we have to also check for the strictness of the cast node:
- NonStrict casts are pushable, they are auto-inserted type coercion
casts (like DECIMAL(20,0) to DECIMAL(38,0))
- Strict casts are fallible (and can error, see
pola-rs/polars#22669)2 files changed
Lines changed: 53 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
162 | 172 | | |
163 | 173 | | |
164 | 174 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
702 | 702 | | |
703 | 703 | | |
704 | 704 | | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
0 commit comments