Skip to content

Fix Dataset.to_sql replace leaving stale rows for an empty dataset - #9295

Open
QTSHI wants to merge 1 commit into
huggingface:mainfrom
QTSHI:cursor/fix-to-sql-empty-replace-260a
Open

QTSHI wants to merge 1 commit into
huggingface:mainfrom
QTSHI:cursor/fix-to-sql-empty-replace-260a

Conversation

@QTSHI

@QTSHI QTSHI commented Nov 30, 2026

Copy link
Copy Markdown

Dataset.to_sql(..., if_exists="replace") on an empty dataset left the previous table contents in place. range(0, 0, batch_size) never calls pandas, and the call still returned 0, so the return value does not show that the old rows are still there. An empty DataFrame.to_sql(..., if_exists="replace") drops and recreates the table.

When the dataset has no rows and if_exists is "replace", the writer now makes one pandas write of an empty frame on the caller side, before any worker pool. "fail" and "append" are unchanged: an empty dataset still writes nothing.

This does not overlap with #8839. That PR fixes ordering for num_proc > 1 by writing the first batch on the parent, then letting workers append. It leaves an empty dataset as a no-op because there is no first batch, and the race it fixes only exists when later batches exist. This change only adds the missing replace write for the zero-row case. Non-empty batches, including that first-batch-then-append order, are untouched. Empty "fail" and "append" stay no-ops, which is the empty-input behavior #8839 preserves. The two patches compose: empty + replace writes once on the parent and does not start workers; non-empty + num_proc > 1 remains #8839.

Tests

tests/io/test_sql.py covers the in-memory sqlite repro (empty select([]) after a one-row write), num_proc=2, replacing a table whose columns do not match, creating a missing table from an empty dataset, and the no-op for the default, "fail", and "append" both when the table exists and when it does not.

python -m pytest tests/io/test_sql.py tests/packaged_modules/test_sql.py tests/test_arrow_dataset.py -q -k "to_sql or sql"
33 passed, 474 deselected
make style

Python 3.12.3, pandas 3.1.2, SQLAlchemy 2.1.9, PyArrow 26.0.1. The rest of the suite was not run.

Fixes #9289

An empty dataset never entered the offset loop, so if_exists="replace"
did not reach pandas and the existing table kept its rows. Write one
empty frame in that case. fail and append remain no-ops.

Fixes huggingface#9289

@atishay-kasliwal atishay-kasliwal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Dataset.select([]).to_sql(..., if_exists='replace') now empties an existing table and replaces one with a different schema (with and without num_proc=2), and creates an empty table when none exists, which is what pandas.DataFrame.to_sql does with an empty frame. Master left the old rows in place. append, fail and the default are unchanged.

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.

Dataset.to_sql with if_exists="replace" retains stale rows when the dataset is empty

2 participants