Skip to content

Revert "enable multivalues insert (#19664)" #21355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions doc/source/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4719,12 +4719,6 @@ writes ``data`` to the database in batches of 1000 rows at a time:

data.to_sql('data_chunked', engine, chunksize=1000)

.. note::

The function :func:`~pandas.DataFrame.to_sql` will perform a multi-value
insert if the engine dialect ``supports_multivalues_insert``. This will
greatly speed up the insert in some cases.

SQL data types
++++++++++++++

Expand Down
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.23.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ and bug fixes. We recommend that all users upgrade to this version.
Fixed Regressions
~~~~~~~~~~~~~~~~~


- Reverted the ability of :func:`~DataFrame.to_sql` to perform multivalue
inserts as this caused regression in certain cases (:issue:`21103`).
In the future this will be made configurable.
- Fixed regression in the :attr:`DatetimeIndex.date` and :attr:`DatetimeIndex.time`
attributes in case of timezone-aware data: :attr:`DatetimeIndex.time` returned
a tz-aware time instead of tz-naive (:issue:`21267`) and :attr:`DatetimeIndex.date`
Expand Down
28 changes: 3 additions & 25 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,29 +572,8 @@ def create(self):
else:
self._execute_create()

def insert_statement(self, data, conn):
"""
Generate tuple of SQLAlchemy insert statement and any arguments
to be executed by connection (via `_execute_insert`).

Parameters
----------
conn : SQLAlchemy connectable(engine/connection)
Connection to recieve the data
data : list of dict
The data to be inserted

Returns
-------
SQLAlchemy statement
insert statement
*, optional
Additional parameters to be passed when executing insert statement
"""
dialect = getattr(conn, 'dialect', None)
if dialect and getattr(dialect, 'supports_multivalues_insert', False):
return self.table.insert(data),
return self.table.insert(), data
def insert_statement(self):
return self.table.insert()

def insert_data(self):
if self.index is not None:
Expand Down Expand Up @@ -633,9 +612,8 @@ def insert_data(self):
return column_names, data_list

def _execute_insert(self, conn, keys, data_iter):
"""Insert data into this table with database connection"""
data = [{k: v for k, v in zip(keys, row)} for row in data_iter]
conn.execute(*self.insert_statement(data, conn))
conn.execute(self.insert_statement(), data)

def insert(self, chunksize=None):
keys, data_list = self.insert_data()
Expand Down
26 changes: 0 additions & 26 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1665,29 +1665,6 @@ class Temporary(Base):

tm.assert_frame_equal(df, expected)

def test_insert_multivalues(self):
# issues addressed
# https://github.com/pandas-dev/pandas/issues/14315
# https://github.com/pandas-dev/pandas/issues/8953

db = sql.SQLDatabase(self.conn)
df = DataFrame({'A': [1, 0, 0], 'B': [1.1, 0.2, 4.3]})
table = sql.SQLTable("test_table", db, frame=df)
data = [
{'A': 1, 'B': 0.46},
{'A': 0, 'B': -2.06}
]
statement = table.insert_statement(data, conn=self.conn)[0]

if self.supports_multivalues_insert:
assert statement.parameters == data, (
'insert statement should be multivalues'
)
else:
assert statement.parameters is None, (
'insert statement should not be multivalues'
)


class _TestSQLAlchemyConn(_EngineToConnMixin, _TestSQLAlchemy):

Expand All @@ -1702,7 +1679,6 @@ class _TestSQLiteAlchemy(object):

"""
flavor = 'sqlite'
supports_multivalues_insert = True

@classmethod
def connect(cls):
Expand Down Expand Up @@ -1751,7 +1727,6 @@ class _TestMySQLAlchemy(object):

"""
flavor = 'mysql'
supports_multivalues_insert = True

@classmethod
def connect(cls):
Expand Down Expand Up @@ -1821,7 +1796,6 @@ class _TestPostgreSQLAlchemy(object):

"""
flavor = 'postgresql'
supports_multivalues_insert = True

@classmethod
def connect(cls):
Expand Down