Skip to content

fix: no panic on empty partition in register_record_batches#1627

Open
fangchenli wants to merge 1 commit into
apache:mainfrom
fangchenli:fix/register-record-batches-empty-panic
Open

fix: no panic on empty partition in register_record_batches#1627
fangchenli wants to merge 1 commit into
apache:mainfrom
fangchenli:fix/register-record-batches-empty-panic

Conversation

@fangchenli

Copy link
Copy Markdown

Which issue does this PR close?

Closes #1626 .

Rationale for this change

register_record_batches read the schema via unchecked partitions.0[0][0], which panics when a partition contains no record batches (e.g. the batches of an empty pyarrow table, whose to_batches() returns []).

What changes are included in this PR?

Take the schema from the first available batch across all partitions and return a clear error when there is none, mirroring the empty-table handling added for from_arrow_table in #613. A non-empty later partition now also works instead of panicking on an empty leading one.

Are there any user-facing changes?

register_record_batches now raises a catchable Python Exception on empty input instead of aborting with a Rust panic

`register_record_batches` read the schema via unchecked `partitions.0[0][0]`,
which panics when a partition contains no record batches (e.g. the batches of
an empty pyarrow table, whose `to_batches()` returns `[]`).

Take the schema from the first available batch across all partitions and return
a clear error when there is none, mirroring the empty-table handling added for
`from_arrow_table` in apache#613. A non-empty later partition now also works instead
of panicking on an empty leading one.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@kosiew kosiew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fangchenli,

Thanks for your contribution.
Looks good overall.
I left two small suggestions that could make the intent clearer and strengthen the regression coverage.

let schema = partitions.0[0][0].schema();
// Take the schema from the first available batch; error instead of
// panicking when the partitions hold no batches.
let schema = partitions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use partitions.0.iter().find_map(|partition| partition.first()) here instead of iter().flatten().next()? The behavior is the same, but I think it makes the intent a little clearer: find the first record batch in any partition, then use its schema.

def test_register_record_batches_empty(ctx):
# A partition list with no record batches carries no schema, so this used to
# panic on unchecked `[0][0]` indexing. It should now raise a clear error.
with pytest.raises(ValueError, match="no record batches"):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also assert that ctx.register_record_batches("t", []) raises the same clear ValueError? The fix now covers both an empty partition ([[]]) and an empty outer partition list ([]), while the regression test currently locks in only the first case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

register_record_batches panics ("index out of bounds") on an empty partition

2 participants