Skip to content

GH-55295: [C++][Python] Reject negative num_rows in Dataset head - #55612

Open
josamhany wants to merge 1 commit into
apache:mainfrom
josamhany:fix-dataset-head-negative-num-rows
Open

josamhany wants to merge 1 commit into
apache:mainfrom
josamhany:fix-dataset-head-negative-num-rows

Conversation

@josamhany

@josamhany josamhany commented Nov 14, 2026 •

Copy link
Copy Markdown

Rationale for this change

Passing a negative value to Dataset.head() can result in unexpected behavior, including returning a table with a negative number of rows.

Since the head() API expects a non-negative number of rows, negative values should be rejected explicitly with a clear error message.

What changes are included in this PR?

  • Added validation in AsyncScanner::Head() to reject negative num_rows values.
  • Returns Status::Invalid("num_rows must be non-negative") when a negative value is provided.
  • Added regression tests covering negative values passed to:
    • Dataset.head()
    • Scanner.head()
    • Fragment.head()

Are these changes tested?

Yes.

  • Built PyArrow from source successfully.
  • Targeted tests: 3 passed.
  • Full python/pyarrow/tests/test_dataset.py test suite: 310 passed, 54 skipped.
  • git diff --check passed.

Are there any user-facing changes?

Yes. Calling Dataset.head(), Scanner.head(), or Fragment.head() with a negative num_rows now raises pyarrow.ArrowInvalid with a clear error message instead of producing unexpected behavior.

Was AI used for this PR?

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

Copilot AI balanced review requested due to automatic review settings November 14, 2026 17:08

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions

Copy link
Copy Markdown

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

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #55295 has no components, please add labels for components.

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI 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.

🟢 Approval recommended

The shared C++ validation rejects negative row counts with a clear error, and regression tests cover the Dataset, Scanner, and Fragment APIs.

No open issues 🎉

Review effort: Lite

@josamhany

Copy link
Copy Markdown
Author

Hi, the workflows on this PR are still showing action_required, so CI has not run yet. Could a committer approve the runs?
The change is small: Dataset head now rejects a negative num_rows with an Invalid status instead of accepting it, with a regression test in pyarrow.
@raulcd @rok if either of you has a moment to look, I would appreciate it.

@josamhany josamhany changed the title GH-55295: Reject negative num_rows in Dataset head GH-55295: [C++][Python] Reject negative num_rows in Dataset head Dec 3, 2026

@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 fix. Sorry we are slightly overwhelmed by bot PRs. I am running CI and will merge if no CI issues.

@raulcd
raulcd self-requested a review December 4, 2026 09:06

@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.

The linter failure is related. Could you run pre-commit locally? See:
https://arrow.apache.org/docs/developers/python/development.html#coding-style

@raulcd

raulcd commented Dec 4, 2026

Copy link
Copy Markdown
Member

If you can also rebase with latest main will help solve the CI failures which have been fixed already.

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.

[Python] Dataset.head(-1) returns a Table with negative num_rows

3 participants