Repository navigation
Conversation
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
left a comment
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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