Skip to content

Commit

Permalink
fix(rust): avoid panic when projecting solitary count into empty frame
Browse files Browse the repository at this point in the history
When the schema of a frame is empty, a count expression is still valid
since it does not refer to any columns. In this scenario, selecting
the "last" column in the schema would previously panic, since there
are none. Fix this by handling this edge case explicitly.

- Closes #16904
  • Loading branch information
wence- committed Jul 3, 2024
1 parent 276655a commit 2d6ceb8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,25 @@ pub(super) fn process_projection(
// the whole file while we only want the count
if exprs.len() == 1 && is_count(exprs[0].node(), expr_arena) {
let input_schema = lp_arena.get(input).schema(lp_arena);
// simply select the last column
// NOTE: the first can be the inserted index column, so that might not work
let (first_name, _) = input_schema.try_get_at_index(input_schema.len() - 1)?;
let expr = expr_arena.add(AExpr::Column(ColumnName::from(first_name.as_str())));
if !acc_projections.is_empty() {
check_double_projection(
&exprs[0],
expr_arena,
&mut acc_projections,
&mut projected_names,
);
}
let expr = if input_schema.is_empty() {
// If the input schema is empty, we should just project
// ourselves
exprs[0].node()
} else {
// simply select the last column
// NOTE: the first can be the inserted index column, so that might not work
let (first_name, _) = input_schema.try_get_at_index(input_schema.len() - 1)?;
let expr = expr_arena.add(AExpr::Column(ColumnName::from(first_name.as_str())));
if !acc_projections.is_empty() {
check_double_projection(
&exprs[0],
expr_arena,
&mut acc_projections,
&mut projected_names,
);
}
expr
};
add_expr_to_accumulated(expr, &mut acc_projections, &mut projected_names, expr_arena);
local_projection.push(exprs.pop().unwrap());
proj_pd.is_count_star = true;
Expand Down
11 changes: 11 additions & 0 deletions py-polars/tests/unit/test_projections.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,14 @@ def test_projection_pushdown_semi_anti_no_selection(
assert "PROJECT 1/2" in (
q_a.join(q_b, left_on="a", right_on="b", how=how).explain()
)


def test_projection_empty_frame_len_16904() -> None:
df = pl.LazyFrame({})

q = df.select(pl.len())

assert "PROJECT */0" in q.explain()

expect = pl.DataFrame({"len": [0]}, schema_overrides={"len": pl.UInt32()})
assert_frame_equal(q.collect(), expect)

0 comments on commit 2d6ceb8

Please sign in to comment.