Skip to content
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

feat: use DML batches in executemany() method #412

Merged
merged 25 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from 11 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
11 changes: 4 additions & 7 deletions google/cloud/spanner_dbapi/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,14 @@ def transaction_checkout(self):
Begin a new transaction, if there is no transaction in
this connection yet. Return the begun one otherwise.

The method is non operational in autocommit mode.
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved

:rtype: :class:`google.cloud.spanner_v1.transaction.Transaction`
:returns: A Cloud Spanner transaction object, ready to use.
"""
if not self.autocommit:
if not self.inside_transaction:
self._transaction = self._session_checkout().transaction()
self._transaction.begin()
if not self.inside_transaction:
self._transaction = self._session_checkout().transaction()
self._transaction.begin()

return self._transaction
return self._transaction
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved

def _raise_if_closed(self):
"""Helper to check the connection state before running a query.
Expand Down
29 changes: 26 additions & 3 deletions google/cloud/spanner_dbapi/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
from google.cloud.spanner_dbapi.utils import PeekIterator
from google.cloud.spanner_dbapi.utils import StreamedManyResultSets

from google.rpc.code_pb2 import ABORTED, OK

_UNSET_COUNT = -1

ColumnDetails = namedtuple("column_details", ["null_ok", "spanner_type"])
Expand Down Expand Up @@ -258,9 +260,30 @@ def executemany(self, operation, seq_of_params):

many_result_set = StreamedManyResultSets()

for params in seq_of_params:
self.execute(operation, params)
many_result_set.add_iter(self._itr)
if classification in (parse_utils.STMT_INSERT, parse_utils.STMT_UPDATING):
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
statements = []

for params in seq_of_params:
sql, params = parse_utils.sql_pyformat_args_to_spanner(
operation, params
)
statements.append((sql, params, get_param_types(params)))

transaction = self.connection.transaction_checkout()
status, res = transaction.batch_update(statements)
many_result_set.add_iter(res)

if status.code == ABORTED:
raise Aborted(status.details)
elif status.code != OK:
raise OperationalError(status.details)

if self.connection.autocommit:
transaction.commit()
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
else:
for params in seq_of_params:
self.execute(operation, params)
many_result_set.add_iter(self._itr)

self._result_set = many_result_set
self._itr = many_result_set
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/spanner_dbapi/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,6 @@ def test_transaction_checkout(self):
mock_transaction.committed = mock_transaction.rolled_back = False
self.assertEqual(connection.transaction_checkout(), mock_transaction)

connection._autocommit = True
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
self.assertIsNone(connection.transaction_checkout())

def test_close(self):
from google.cloud.spanner_dbapi import connect, InterfaceError

Expand Down
134 changes: 134 additions & 0 deletions tests/unit/spanner_dbapi/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,140 @@ def test_executemany(self):
(mock.call(operation, (1,)), mock.call(operation, (2,)))
)

def test_executemany_insert_batch_non_autocommit(self):
from google.cloud.spanner_v1.param_types import INT64
from google.cloud.spanner_dbapi import connect
from google.rpc.code_pb2 import OK

sql = """INSERT INTO table (col1, "col2", `col3`, `"col4"`) VALUES (%s, %s, %s, %s)"""

with mock.patch(
"google.cloud.spanner_v1.instance.Instance.exists", return_value=True
):
with mock.patch(
"google.cloud.spanner_v1.database.Database.exists", return_value=True,
):
connection = connect("test-instance", "test-database")

connection._transaction = mock.Mock(committed=False, rolled_back=False)
connection._transaction.batch_update = mock.Mock(
return_value=[mock.Mock(code=OK), []]
)

cursor = connection.cursor()
cursor.executemany(sql, [(1, 2, 3, 4), (5, 6, 7, 8)])

connection._transaction.batch_update.assert_called_once_with(
[
(
"""INSERT INTO table (col1, "col2", `col3`, `"col4"`) VALUES (@a0, @a1, @a2, @a3)""",
{"a0": 1, "a1": 2, "a2": 3, "a3": 4},
{"a0": INT64, "a1": INT64, "a2": INT64, "a3": INT64},
),
(
"""INSERT INTO table (col1, "col2", `col3`, `"col4"`) VALUES (@a0, @a1, @a2, @a3)""",
{"a0": 5, "a1": 6, "a2": 7, "a3": 8},
{"a0": INT64, "a1": INT64, "a2": INT64, "a3": INT64},
),
]
)

def test_executemany_insert_batch_autocommit(self):
from google.cloud.spanner_v1.param_types import INT64
from google.cloud.spanner_dbapi import connect
from google.rpc.code_pb2 import OK

sql = """INSERT INTO table (col1, "col2", `col3`, `"col4"`) VALUES (%s, %s, %s, %s)"""

with mock.patch(
"google.cloud.spanner_v1.instance.Instance.exists", return_value=True
):
with mock.patch(
"google.cloud.spanner_v1.database.Database.exists", return_value=True,
):
connection = connect("test-instance", "test-database")

connection.autocommit = True

connection._transaction = mock.Mock(committed=False, rolled_back=False)
connection._transaction.batch_update = mock.Mock(
return_value=[mock.Mock(code=OK), []]
)
connection._transaction.commit = mock.Mock()

cursor = connection.cursor()
cursor.executemany(sql, [(1, 2, 3, 4), (5, 6, 7, 8)])

connection._transaction.batch_update.assert_called_once_with(
[
(
"""INSERT INTO table (col1, "col2", `col3`, `"col4"`) VALUES (@a0, @a1, @a2, @a3)""",
{"a0": 1, "a1": 2, "a2": 3, "a3": 4},
{"a0": INT64, "a1": INT64, "a2": INT64, "a3": INT64},
),
(
"""INSERT INTO table (col1, "col2", `col3`, `"col4"`) VALUES (@a0, @a1, @a2, @a3)""",
{"a0": 5, "a1": 6, "a2": 7, "a3": 8},
{"a0": INT64, "a1": INT64, "a2": INT64, "a3": INT64},
),
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
]
)
connection._transaction.commit.assert_called_once()

def test_executemany_insert_batch_failed(self):
from google.cloud.spanner_dbapi import connect
from google.cloud.spanner_dbapi.exceptions import OperationalError
from google.rpc.code_pb2 import UNKNOWN

sql = """INSERT INTO table (col1, "col2", `col3`, `"col4"`) VALUES (%s, %s, %s, %s)"""
err_details = "Details here"

with mock.patch(
"google.cloud.spanner_v1.instance.Instance.exists", return_value=True
):
with mock.patch(
"google.cloud.spanner_v1.database.Database.exists", return_value=True,
):
connection = connect("test-instance", "test-database")

connection.autocommit = True
cursor = connection.cursor()

connection._transaction = mock.Mock(committed=False, rolled_back=False)
connection._transaction.batch_update = mock.Mock(
return_value=(mock.Mock(code=UNKNOWN, details=err_details), [])
)

with self.assertRaisesRegex(OperationalError, err_details):
cursor.executemany(sql, [(1, 2, 3, 4), (5, 6, 7, 8)])

def test_executemany_insert_batch_aborted(self):
from google.api_core.exceptions import Aborted
from google.cloud.spanner_dbapi import connect
from google.rpc.code_pb2 import ABORTED

sql = """INSERT INTO table (col1, "col2", `col3`, `"col4"`) VALUES (%s, %s, %s, %s)"""
err_details = "Aborted details here"

with mock.patch(
"google.cloud.spanner_v1.instance.Instance.exists", return_value=True
):
with mock.patch(
"google.cloud.spanner_v1.database.Database.exists", return_value=True,
):
connection = connect("test-instance", "test-database")

connection.autocommit = True
cursor = connection.cursor()

connection._transaction = mock.Mock(committed=False, rolled_back=False)
connection._transaction.batch_update = mock.Mock(
return_value=(mock.Mock(code=ABORTED, details=err_details), [])
)

with self.assertRaisesRegex(Aborted, err_details):
cursor.executemany(sql, [(1, 2, 3, 4), (5, 6, 7, 8)])

@unittest.skipIf(
sys.version_info[0] < 3, "Python 2 has an outdated iterator definition"
)
Expand Down