Skip to content

Commit

Permalink
feat: Apply slice pushdown immediately to in-memory frames (pola-rs#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored and Henry Harbeck committed Jul 8, 2024
1 parent bb07c5a commit 63cde63
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
10 changes: 10 additions & 0 deletions crates/polars-plan/src/plans/optimizer/slice_pushdown_lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ impl SlicePushDown {
scan_type
};

Ok(lp)
},
(DataFrameScan {df, schema, output_schema, filter, }, Some(state)) if filter.is_none() => {
let df = df.slice(state.offset, state.len as usize);
let lp = DataFrameScan {
df: Arc::new(df),
schema,
output_schema,
filter
};
Ok(lp)
}
(Union {mut inputs, mut options }, Some(state)) => {
Expand Down
19 changes: 8 additions & 11 deletions py-polars/tests/unit/operations/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ def test_slice_nullcount(ref: list[int | None]) -> None:

def test_slice_pushdown_set_sorted() -> None:
ldf = pl.LazyFrame({"foo": [1, 2, 3]})
ldf = ldf.set_sorted("foo").head(5)
ldf = ldf.set_sorted("foo").head(2)
plan = ldf.explain()
# check the set sorted is above slice
assert plan.index("set_sorted") < plan.index("SLICE")
assert "SLICE" not in plan
assert ldf.collect().height == 2


def test_slice_pushdown_literal_projection_14349() -> None:
Expand All @@ -197,19 +197,17 @@ def test_slice_pushdown_literal_projection_14349() -> None:

# For select, slice pushdown should happen when at least 1 input column is selected
q = lf.select("a", x=1).head(0)
plan = q.explain()
assert plan.index("SELECT") < plan.index("SLICE")
# slice isn't in plan if it has been pushed down to the dataframe
assert "SLICE" not in q.explain()
assert q.collect().height == 0

# For with_columns, slice pushdown should happen if the input has at least 1 column
q = lf.with_columns(x=1).head(0)
plan = q.explain()
assert plan.index("WITH_COLUMNS") < plan.index("SLICE")
assert "SLICE" not in q.explain()
assert q.collect().height == 0

q = lf.with_columns(pl.col("a") + 1).head(0)
plan = q.explain()
assert plan.index("WITH_COLUMNS") < plan.index("SLICE")
assert "SLICE" not in q.explain()
assert q.collect().height == 0

# This does not project any of the original columns
Expand All @@ -219,8 +217,7 @@ def test_slice_pushdown_literal_projection_14349() -> None:
assert q.collect().height == 0

q = lf.with_columns(b=1, c=2).head(0)
plan = q.explain()
assert plan.index("WITH_COLUMNS") < plan.index("SLICE")
assert "SLICE" not in q.explain()
assert q.collect().height == 0


Expand Down

0 comments on commit 63cde63

Please sign in to comment.