Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rust): avoid panic when projecting solitary count into empty frame #17393

Merged
merged 1 commit into from
Jul 3, 2024
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
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)
Loading