Skip to content

Fix restoring CsvDataset iterators when the file has a header - #129843

Merged
copybara-service[bot] merged 3 commits into
tensorflow:masterfrom
bodapatisaikrishna:fix/csv-dataset-header-restore
Nov 4, 2026
Merged

copybara-service[bot] merged 3 commits into
tensorflow:masterfrom
bodapatisaikrishna:fix/csv-dataset-header-restore

Conversation

@bodapatisaikrishna

@bodapatisaikrishna bodapatisaikrishna commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #129842

Problem

Restoring a CsvDataset iterator from a checkpoint with header=True loses data. Depending on the read buffer size, the iterator silently ends early or fails with Expect N fields but have M in record. make_csv_dataset defaults to header=True, so it is affected too. On tf-nightly, with a 12-row file and resuming at row 3:

after resuming at row 3: [3, 4]       # expected rows 3 to 11; varies between runs
small buffer: InvalidArgumentError: Expect 2 fields but have 1 in record
make_csv_dataset after resuming at row 3: [3, 4, 5, 6]

RestoreInternal calls SetupStreamsLocked, then skips num_buffer_reads - 1 buffers and refills the buffer at the saved pos. With header=True, SetupStreamsLocked also reads the header, which consumes at least one buffer. The saved num_buffer_reads already counts those reads from the start of the file, so the stream ends up past the saved buffer.

Change

Split the stream setup in two:

  • OpenStreamsLocked opens the file and resets the buffer state. RestoreInternal now calls this one.
  • SetupStreamsLocked calls it and then skips the header. GetNextInternal still uses this when moving to the next file.

The saved state format is unchanged, so checkpoints written before this change should restore correctly with it, though I haven't tested that case.

I also considered subtracting the header's reads in RestoreInternal, but that needs extra saved state. Reopening without the header is simpler.

CsvDatasetCheckpointTest only used a file without a header. testHeader adds one, across buffer sizes 2, 16 and the default, with and without GZIP.

Testing

I don't have a Bazel build, so CI on this PR will be the first in-tree build of this change. To test the C++ on a real runtime, I compiled csv_dataset_op.cc out of tree with clang++ against the tf-nightly 2.23.0.dev20261028 macOS arm64 headers, once from master and once with this change. Each was built as a tf.load_op_library kernel under its own op name, and CsvDataset was routed to it by replacing gen_experimental_dataset_ops.csv_dataset_v2.

master kernel this PR
Resume at every row for 12 setups (header, no header, small buffer, GZIP, CRLF, two files, select_cols, take, skip, one column) wrong for all 9 setups with header=True all correct
csv_dataset_test.py (339 tests, 173 skipped) 12 errors, all in the new testHeader all pass

The script from the issue, including make_csv_dataset, returns rows 3 to 11 in every case with this PR's kernel.

pylint --rcfile=tensorflow/tools/ci_build/pylintrc on the test file: 10/10. clang-format --style=google on the .cc file reports no new differences. I did not test other platforms.

Notes

I added a RELEASE.md entry.

RestoreInternal reopens the current file with SetupStreamsLocked, skips
`num_buffer_reads - 1` buffers and refills the buffer. But for
`header=True`, SetupStreamsLocked also reads the header, which consumes
at least one buffer. The saved `num_buffer_reads` already counts those
reads from the start of the file, so the stream ends up past the saved
buffer and `pos` is applied to the wrong data. Rows are silently lost,
or parsing fails with "Expect N fields but have M in record", depending
on the buffer size. `make_csv_dataset`, which defaults to `header=True`,
is affected too.

Split the stream setup into OpenStreamsLocked, which restoring now uses,
and SetupStreamsLocked, which also skips the header when moving on to a
file. The existing checkpoint test had no header; add one for several
buffer sizes, with and without compression.
@google-ml-butler google-ml-butler Bot added the size:S CL Change Size: Small label Nov 2, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request fixes an issue where restoring a CsvDataset iterator from a checkpoint with header=True loses rows or fails with a parse error. It separates stream opening from header skipping to ensure that headers are not skipped again during restoration. However, a critical issue was identified in the restoration logic: if num_buffer_reads is 0, subtracting 1 will cause an underflow when cast to size_t, setting num_buffer_reads_ to SIZE_MAX. A code suggestion is provided to safely handle this case.

@nithyak0204 nithyak0204 self-assigned this Nov 3, 2026
@nithyak0204
nithyak0204 requested a review from a team November 3, 2026 04:28
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Nov 3, 2026
@nithyak0204 nithyak0204 added the comp:data tf.data related issues label Nov 3, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Nov 3, 2026
@nithyak0204 nithyak0204 added the prtype:bugfix PR to fix a bug label Nov 3, 2026

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

In tensorflow/core/kernels/data/experimental/csv_dataset_op.cc (around lines 867 and 896), please update the comment on OpenStreamsLocked and add a short comment on SetupStreamsLocked. This clarifies which helper skips the header row:

      // Opens the file at `current_file_index_` and resets the buffer state
      // without skipping the header record.
      absl::Status OpenStreamsLocked(Env* env)
          TF_EXCLUSIVE_LOCKS_REQUIRED(mu_) {
      // Opens the file at `current_file_index_` and skips the header record
      // when `dataset()->header_` is true.
      absl::Status SetupStreamsLocked(Env* env)
          TF_EXCLUSIVE_LOCKS_REQUIRED(mu_) {

@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Nov 3, 2026
@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Approved by Reviewer in PR Queue Nov 3, 2026

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

This PR has merge conflicts with the base branch. Please rebase the PR and resolve the conflicts so we can continue running internal presubmit tests.

@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Reviewer Requested Changes in PR Queue Nov 3, 2026
@dmiltr3 dmiltr3 removed the ready to pull PR ready for merge process label Nov 3, 2026
@nithyak0204 nithyak0204 added stat:awaiting response Status - Awaiting response from author and removed awaiting review Pull request awaiting review kokoro:force-run Tests on submitted change labels Nov 4, 2026
@bodapatisaikrishna

Copy link
Copy Markdown
Contributor Author

Updated in e83a313: added the comments on OpenStreamsLocked and SetupStreamsLocked.

I resolved the RELEASE.md conflict by merging the latest master (b2b8248) rather than rebasing. Let me know if you need a rebased history instead.

feedback/copybara reported a failure on the previous head. I can't see the internal logs, so please let me know if it fails again on this commit and what it reports.

@google-ml-butler google-ml-butler Bot removed the stat:awaiting response Status - Awaiting response from author label Nov 4, 2026
@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Nov 4, 2026
@github-project-automation github-project-automation Bot moved this from Reviewer Requested Changes to Approved by Reviewer in PR Queue Nov 4, 2026
@copybara-service
copybara-service Bot merged commit 63fbb7a into tensorflow:master Nov 4, 2026
19 checks passed
@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Merged in PR Queue Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:data tf.data related issues kokoro:force-run Tests on submitted change prtype:bugfix PR to fix a bug ready to pull PR ready for merge process size:S CL Change Size: Small

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

CsvDataset loses rows when restoring an iterator from a checkpoint with header=True

3 participants