Skip to content

GH-44366: [Python][Acero] RecordBatch.filter on expression raises error if result set is empty - #46057

Merged
raulcd merged 6 commits into
apache:mainfrom
koenvo:bugfix/filter-record-batch
Apr 9, 2025
Merged

GH-44366: [Python][Acero] RecordBatch.filter on expression raises error if result set is empty#46057
raulcd merged 6 commits into
apache:mainfrom
koenvo:bugfix/filter-record-batch

Conversation

@koenvo

@koenvo koenvo commented Apr 8, 2025

Copy link
Copy Markdown
Contributor

Rationale for this change

When filtering a RecordBatch using an expression that results in zero matching rows, an IndexError was raised due to an attempt to access the first batch of an empty result. This change ensures that such cases are handled gracefully by returning an empty RecordBatch with the correct schema.

What changes are included in this PR?

Fixed a bug in _filter_table where filtering all rows from a RecordBatch would raise an IndexError.

Added a helper function _empty_record_batch_from_schema to construct an empty RecordBatch with the correct schema.

Are these changes tested?

Yes, the behavior was verified manually. Let me know if you'd like a dedicated test case added to the test suite.

Are there any user-facing changes?

No changes to public APIs. The fix ensures more robust handling of edge cases internally.

@github-actions

github-actions Bot commented Apr 8, 2025

Copy link
Copy Markdown

Thanks for opening a pull request!

If this is not a minor PR. Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose

Opening GitHub issues ahead of time contributes to the Openness of the Apache Arrow project.

Then could you also rename the pull request title in the following format?

GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}

or

MINOR: [${COMPONENT}] ${SUMMARY}

See also:

@Fokko

Fokko commented Apr 8, 2025

Copy link
Copy Markdown
Contributor

cc @wgtmac

@koenvo koenvo changed the title Fix IndexError when filtering all rows from a RecordBatch GH-44366: [Python][Acero] RecordBatch.filter on expression raises error if result set is empty Apr 8, 2025
@github-actions

github-actions Bot commented Apr 8, 2025

Copy link
Copy Markdown

⚠️ GitHub issue #44366 has been automatically assigned in GitHub to PR creator.

@wgtmac

wgtmac commented Apr 8, 2025

Copy link
Copy Markdown
Member

Thanks for the fix! It looks reasonable to me. However, I'm not familiar with the convention here. Perhaps @jorisvandenbossche @pitrou can help.

Comment thread python/pyarrow/tests/arrow_44366.py Outdated
Comment thread python/pyarrow/acero.py Outdated
@raulcd

raulcd commented Apr 8, 2025

Copy link
Copy Markdown
Member

Thanks for the PR!

@raulcd
raulcd requested a review from AlenkaF April 8, 2025 15:39

@AlenkaF AlenkaF left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for contributing a fix for this.
I have one comment that will need a small change, otherwise the changes look good!

Comment thread python/pyarrow/acero.py Outdated
Comment thread python/pyarrow/tests/test_acero.py Outdated
@github-actions github-actions Bot added awaiting changes Awaiting changes awaiting committer review Awaiting committer review awaiting review Awaiting review and removed awaiting review Awaiting review awaiting committer review Awaiting committer review awaiting changes Awaiting changes labels Apr 9, 2025
@github-actions github-actions Bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Apr 9, 2025

@AlenkaF AlenkaF left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great to me now, thank you for all the updates!
Will wait for @raulcd to give another round of review and for the CI to finish before merging.

@koenvo

koenvo commented Apr 9, 2025

Copy link
Copy Markdown
Contributor Author

Thank you all so much for the guidance!

@raulcd raulcd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR!
The CI failure is unrelated, I've opened to follow up:
#46077

@raulcd
raulcd merged commit b1618e0 into apache:main Apr 9, 2025
@raulcd raulcd removed the awaiting committer review Awaiting committer review label Apr 9, 2025
@github-actions

github-actions Bot commented Apr 9, 2025

Copy link
Copy Markdown

⚠️ GitHub issue #44366 has been automatically assigned in GitHub to PR creator.

@conbench-apache-arrow

Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 4 benchmarking runs that have been run so far on merge-commit b1618e0.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 7 possible false positives for unstable benchmarks that are known to sometimes produce them.

zanmato1984 pushed a commit to zanmato1984/arrow that referenced this pull request Apr 15, 2025
…es error if result set is empty (apache#46057)

### Rationale for this change
When filtering a RecordBatch using an expression that results in zero matching rows, an IndexError was raised due to an attempt to access the first batch of an empty result. This change ensures that such cases are handled gracefully by returning an empty RecordBatch with the correct schema.

### What changes are included in this PR?
Fixed a bug in _filter_table where filtering all rows from a RecordBatch would raise an IndexError.

Added a helper function _empty_record_batch_from_schema to construct an empty RecordBatch with the correct schema.

### Are these changes tested?
Yes, the behavior was verified manually. Let me know if you'd like a dedicated test case added to the test suite.

### Are there any user-facing changes?
No changes to public APIs. The fix ensures more robust handling of edge cases internally.

- Github issue: apache#44366
* GitHub Issue: apache#44366

Authored-by: Koen Vossen <info@koenvossen.nl>
Signed-off-by: Raúl Cumplido <raulcumplido@gmail.com>
@WillAyd

WillAyd commented Apr 16, 2025

Copy link
Copy Markdown
Contributor

@github-actions crossbow submit -g python

@github-actions

Copy link
Copy Markdown

Revision: b5ef330

Submitted crossbow builds: ursacomputing/crossbow @ actions-85876e4ee6

Task Status
example-python-minimal-build-fedora-conda GitHub Actions
example-python-minimal-build-ubuntu-venv GitHub Actions
test-conda-python-3.10 GitHub Actions
test-conda-python-3.10-hdfs-2.9.2 GitHub Actions
test-conda-python-3.10-hdfs-3.2.1 GitHub Actions
test-conda-python-3.10-pandas-latest-numpy-latest GitHub Actions
test-conda-python-3.11 GitHub Actions
test-conda-python-3.11-dask-latest GitHub Actions
test-conda-python-3.11-dask-upstream_devel GitHub Actions
test-conda-python-3.11-hypothesis GitHub Actions
test-conda-python-3.11-pandas-latest-numpy-1.26 GitHub Actions
test-conda-python-3.11-pandas-latest-numpy-latest GitHub Actions
test-conda-python-3.11-pandas-nightly-numpy-nightly GitHub Actions
test-conda-python-3.11-pandas-upstream_devel-numpy-nightly GitHub Actions
test-conda-python-3.11-spark-master GitHub Actions
test-conda-python-3.12 GitHub Actions
test-conda-python-3.12-cpython-debug GitHub Actions
test-conda-python-3.13 GitHub Actions
test-conda-python-3.9 GitHub Actions
test-conda-python-3.9-pandas-1.1.3-numpy-1.19.5 GitHub Actions
test-conda-python-emscripten GitHub Actions
test-cuda-python-ubuntu-22.04-cuda-11.7.1 GitHub Actions
test-debian-12-python-3-amd64 GitHub Actions
test-debian-12-python-3-i386 GitHub Actions
test-fedora-39-python-3 GitHub Actions
test-ubuntu-22.04-python-3 GitHub Actions
test-ubuntu-22.04-python-313-freethreading GitHub Actions
test-ubuntu-24.04-python-3 GitHub Actions

@WillAyd

WillAyd commented Apr 16, 2025

Copy link
Copy Markdown
Contributor

I noticed this change caused a very minor regression with the example-python-minimal-build-ubuntu-venv and example-python-minimal-build-fedora-conda jobs. You can see the failing test here:

https://github.com/ursacomputing/crossbow/actions/runs/14504182883/job/40690374677#step:3:13332

From what I can tell, the added code here ends up referencing code from the pyarrow.acero module. However, it is possible to have a pyarrow configuration where compute is enabled but acero is not. The tests are failing in that case

@AlenkaF

AlenkaF commented Apr 17, 2025

Copy link
Copy Markdown
Member

I may have mistakenly suggested moving the tests to test_compute.py, but they should actually remain in test_acero.py. I'll create an issue and a PR to fix this. Thanks @WillAyd for looking into it!

@raulcd

raulcd commented Apr 17, 2025

Copy link
Copy Markdown
Member

or mark them as @pytest.mark.acero

@AlenkaF

AlenkaF commented Apr 17, 2025

Copy link
Copy Markdown
Member

or mark them as @pytest.mark.acero

True! I already opened a PR: #46175. Can change to adding a mark if there will be a preference for it.

GayathriSrividya pushed a commit to GayathriSrividya/iceberg-python that referenced this pull request Jun 8, 2026
…ndexError

RecordBatch.filter(Expression) raises IndexError on PyArrow 17-20 when
the result has zero rows (fixed upstream in apache/arrow#46057). Replace
the try/except approach with the Table-based workaround that avoids the
bug, consistent with the pattern already used for the non-positional-
deletes path.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants