GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: Implementation for batch dml in dbapi by ankiaga · Pull Request #1055 · googleapis/python-spanner

This repository was archived by the owner on Jun 8, 2026. It is now read-only.

Conversation

@ankiaga

@ankiaga ankiaga commented Dec 12, 2023

Copy link
Copy Markdown
Contributor

No description provided.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ankiaga
feat: Implementation for batch dml in dbapi
bb19174
@ankiaga
ankiaga requested review from a team December 12, 2023 03:40
@product-auto-label product-auto-label Bot added size: l Pull request size is large. api: spanner Issues related to the googleapis/python-spanner API. labels Dec 12, 2023
@ankiaga
ankiaga requested review from aseering and olavloite December 12, 2023 03:40
@ankiaga
Few changes
3ceb081
@ankiaga ankiaga added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 12, 2023
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 12, 2023
@ankiaga
ankiaga requested review from manu2 and pratickchokhani December 12, 2023 07:53
olavloite
olavloite reviewed Dec 13, 2023
View reviewed changes
Comment thread google/cloud/spanner_dbapi/batch_dml_executor.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/batch_dml_executor.py Outdated
and parsed_statement.statement_type != StatementType.INSERT
):
raise ProgrammingError(
"Only DML statements are allowed in batch " "DML mode."

@olavloite olavloite Dec 12, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

nit: can this be just one string instead of two concatenated strings?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ankiaga ankiaga Dec 13, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Fixed

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread google/cloud/spanner_dbapi/batch_dml_executor.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/batch_dml_executor.py
connection._transaction = None
raise Aborted(status.message)
elif status.code != OK:
raise OperationalError(status.message)

@olavloite olavloite Dec 12, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Should (could) this also include the status code?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ankiaga ankiaga Dec 14, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Will take it in a follow up PR

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread google/cloud/spanner_dbapi/client_side_statement_parser.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/connection.py Outdated
self._read_only = value

@property
def batch_mode(self):

@olavloite olavloite Dec 13, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

If I understand it correctly, giving these names that do not start with an underscore will make them part of the public API. In that case, we should document them and also add validations to verify that they are only called with valid arguments. (But probably we should make them internal instead)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ankiaga ankiaga Dec 13, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Removed the property

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread google/cloud/spanner_dbapi/connection.py Outdated
Args:
value (BatchMode)
"""
self._batch_mode = value

@olavloite olavloite Dec 13, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

What happens if an external uses calls this function when the connection is already in the middle of a different type of batch (e.g. it is now in a DML batch, and it is called to set it to DDL batch)?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ankiaga ankiaga Dec 13, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Removed this property

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/system/test_dbapi.py
self._cursor.execute("start batch dml")
self._insert_row(7)
self._insert_row(8)
self._cursor.execute("run batch")

@olavloite olavloite Dec 13, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

It's probably difficult, but: Do we have any way that we could verify that the run batch call really sends an ExecuteBatchDml request, and does not execute multiple ExecuteSql requests sequentially?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ankiaga ankiaga Dec 13, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Do you mean if we could assert something in the test for that?

Manually I have verified it by debugging

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@olavloite olavloite Dec 14, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yes, I meant automated. That would then also guard us against any regressions, for example if something changes in the underlying client library, which cause this to use multiple ExecuteSql RPCs instead of one ExecuteBatchDml RPC.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ankiaga ankiaga Dec 14, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This needs some investigation, so will take it in a follow up PR

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/unit/spanner_dbapi/test_batch_dml_executor.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread tests/system/test_dbapi.py
VALUES ({i}, 'first-name-{i}', 'last-name-{i}', '[email protected]')
"""
)

@olavloite olavloite Dec 13, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We should also have tests for the unhappy path of using batch DML:

  1. What happens if the batch contains an invalid statement as the first statement?
  2. What happens if the batch contains an invalid statement in the middle?
  3. What happens if the batch contains an invalid statement at the end?
  4. Can we test and verify that the retry logic for Batch DML works as expected?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ankiaga ankiaga Dec 13, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Added test for invalid statements.

There are issues with the retry logic which I am fixing in the next PR

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@olavloite olavloite Dec 14, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Ack

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ankiaga
Incorporated comments
6ff121d
@ankiaga ankiaga added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 14, 2023
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 14, 2023
@ankiaga ankiaga added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 14, 2023
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Dec 14, 2023
olavloite
olavloite approved these changes Dec 14, 2023
View reviewed changes

@olavloite olavloite left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Looks generally good to me, but with one remaining question/nit on the exception that is raised when a batch fails. The exception should also contain the update counts of the statements that did succeed. Are we sure that it does?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread google/cloud/spanner_dbapi/batch_dml_executor.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread google/cloud/spanner_dbapi/client_side_statement_parser.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread tests/system/test_dbapi.py
self._cursor.execute("start batch dml")
self._insert_row(7)
self._insert_row(8)
self._cursor.execute("run batch")

@olavloite olavloite Dec 14, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yes, I meant automated. That would then also guard us against any regressions, for example if something changes in the underlying client library, which cause this to use multiple ExecuteSql RPCs instead of one ExecuteBatchDml RPC.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/system/test_dbapi.py
"""
)
with pytest.raises(OperationalError):
self._cursor.execute("run batch")

@olavloite olavloite Dec 14, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I would have expected this to return a specific batch error that:

  1. Contains the error code, message etc.
  2. And contains the update counts of the statements that did succeed.

See for example this for the JDBC driver: https://github.com/googleapis/java-spanner-jdbc/blob/1f89f78c37b9e118e2c0cbc7f56d3eb1d5745863/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcPreparedStatementTest.java#L797

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ankiaga ankiaga Dec 14, 2023

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

As discussed we need to create a custom exception class, so will take it in follow up PR

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/unit/spanner_dbapi/test_cursor.py
mock_db.run_in_transaction = mock_run_in = mock.MagicMock()
cursor.execute(sql="sql")
mock_run_in.assert_called_once_with(
cursor._do_execute_update_in_autocommit, "sql WHERE 1=1", None

@olavloite olavloite Dec 14, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Why did this change in this PR?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ankiaga ankiaga Dec 14, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The logic to add WHERE clause has moved to parse_utils.classify_statement now which we are mocking here so its same as what is returned as per line 255

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/system/test_dbapi.py
VALUES ({i}, 'first-name-{i}', 'last-name-{i}', '[email protected]')
"""
)

@olavloite olavloite Dec 14, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Ack

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ankiaga
Merge branch 'main' into batch_dml
2157ebf
@ankiaga
ankiaga enabled auto-merge (squash) December 14, 2023 17:17
@ankiaga
ankiaga merged commit 7a92315 into googleapis:main Dec 14, 2023
@release-please release-please Bot mentioned this pull request Dec 14, 2023
chore(main): release 3.41.0 #1009
Merged
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@aseering aseering Awaiting requested review from aseering
@manu2 manu2 Awaiting requested review from manu2
@pratickchokhani pratickchokhani Awaiting requested review from pratickchokhani
1 more reviewer
@olavloite olavloite olavloite approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

api: spanner Issues related to the googleapis/python-spanner API. size: l Pull request size is large.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@ankiaga @olavloite @yoshi-kokoro
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.