Skip to content

feat(spanner): Add Cloud Spanner last statement samples #5296

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

sgorse123
Copy link

Support for the last statement option was added in googleapis/google-cloud-go#11638 by @olavloite. This adds a sample of how to use the last statement option.

@sgorse123 sgorse123 requested review from a team as code owners April 21, 2025 19:57
Copy link

snippet-bot bot commented Apr 21, 2025

Here is the summary of changes.

You are about to add 2 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Apr 21, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Hello @sgorse123, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request adds samples demonstrating the usage of the LastStatement option in Cloud Spanner DML operations. The LastStatement option allows users to execute a DML statement as the final statement in a transaction, which can improve performance in some cases. The PR includes two new sample functions: insertAndUpdateDmlWithLastStatement for standard Spanner and pgInsertAndUpdateDmlWithLastStatement for Spanner with PostgreSQL interface. These samples insert a record and then update it within a read-write transaction, using the LastStatement option for the update operation. The PR also adds integration tests for these new samples.

Highlights

  • New Samples: Introduces two new sample functions, insertAndUpdateDmlWithLastStatement and pgInsertAndUpdateDmlWithLastStatement, showcasing the use of the LastStatement option in DML operations for both standard Spanner and Spanner with PostgreSQL interface.
  • Integration Tests: Adds integration tests for the new samples to ensure they function correctly and demonstrate the expected behavior of the LastStatement option.
  • DML Operations: Demonstrates how to insert and update records within a read-write transaction using DML statements, with a focus on using the LastStatement option for the update operation.

Changelog

Click here to see the changelog
  • spanner/spanner_snippets/spanner/integration_test.go
    • Added TestDmlWithLastStatementSample to test the insertAndUpdateDmlWithLastStatement function.
    • Added TestPgDmlWithLastStatementSample to test the pgInsertAndUpdateDmlWithLastStatement function.
    • The new tests create a database, run the sample, and assert that the output contains the expected messages.
  • spanner/spanner_snippets/spanner/pg_spanner_dml_last_statement.go
    • Created a new file containing the pgInsertAndUpdateDmlWithLastStatement function.
    • The function inserts a record into the Singers table and then updates it using DML statements within a read-write transaction.
    • The LastStatement option is used for the update operation.
  • spanner/spanner_snippets/spanner/spanner_dml_last_statement.go
    • Created a new file containing the insertAndUpdateDmlWithLastStatement function.
    • The function inserts a record into the Singers table and then updates it using DML statements within a read-write transaction.
    • The LastStatement option is used for the update operation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


In Spanner's realm, DML takes flight,
Last statement shines, a guiding light.
Transactions flow,
As updates grow,
Optimizing queries, day and night.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds samples demonstrating the usage of the last statement option in Cloud Spanner DML operations. The changes include new test cases and sample code for both standard and PostgreSQL-dialect Spanner databases. Overall, the code is well-structured and the samples are clear and concise. However, there are a few areas that could be improved for better readability and maintainability.

Summary of Findings

  • Inconsistent naming: The naming of the test functions and sample functions could be more consistent to improve readability. For example, using a consistent prefix like Test for test functions and a descriptive name for sample functions would be beneficial.
  • Magic numbers: The SingerId values (54213, 54214) are used directly in the code without explanation. Consider defining these as constants with descriptive names to improve readability and maintainability.

Merge Readiness

The pull request introduces valuable sample code demonstrating the LastStatement option for DML operations in Cloud Spanner. While the code is functional, addressing the naming inconsistencies and magic numbers would improve its overall quality and maintainability. I am unable to directly approve this pull request, and recommend that others review and approve this code before merging. Given the medium severity issues, I recommend addressing these before merging.


package spanner

// [START pg_spanner_dml_last_statement]
Copy link
Contributor

Choose a reason for hiding this comment

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

this needs to be a valid region tag starting with a product prefix.

Copy link
Author

Choose a reason for hiding this comment

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

Updated to prefix with spanner_postgresql, thanks

@iennae iennae changed the title Add Cloud Spanner last statement samples feat(spanner): Add Cloud Spanner last statement samples Apr 22, 2025
@product-auto-label product-auto-label bot added the api: spanner Issues related to the Spanner API. label Apr 22, 2025
@sgorse123 sgorse123 requested a review from iennae April 28, 2025 16:07
@telpirion telpirion self-assigned this Apr 28, 2025
Copy link
Collaborator

@telpirion telpirion left a comment

Choose a reason for hiding this comment

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

Thank you @sgorse123 for submitting this sample!

FYI: this repo is intended for code samples to be published on cloud.google.com. I can't find any records for these region_tags being used in other programming languages. Do you happen to know whether there is a plan to update the documentation with these samples?

update_stmt := spanner.Statement{
SQL: `UPDATE Singers SET LastName = 'Doe' WHERE SingerId = 54213`,
}
updateRowCount, err := txn.UpdateWithOptions(context.Background(), update_stmt, spanner.QueryOptions{LastStatement: true})
Copy link
Collaborator

Choose a reason for hiding this comment

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

issue: create new Context and QueryOption objects outside of the call to UpdateWithOptions().

Suggestion:

ctx := context.Background()
opts := spanner.QueryOptions{ LastStatement: true }
updateRowCount, err := txn.UpdateWithOptions(ctx, updateStmt, opts)

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the suggested code block, updated QueryOptions to be instantiated on new line as suggested, and used the existing ctx instead of calling context.Background()

}
fmt.Fprintf(w, "%d record(s) inserted.\n", insertRowCount)

update_stmt := spanner.Statement{
Copy link
Collaborator

Choose a reason for hiding this comment

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

issue: use camelCase for variable names.

See: https://github.com/GoogleCloudPlatform/golang-samples/pulls

Copy link
Author

Choose a reason for hiding this comment

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

Done

}
defer client.Close()

// Insert records into the Singers table and then updates
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: maintain consistent conjugation of verb forms in comment (e.g. "Inserts" and "updates" OR "Insert" and "update")

Copy link
Author

Choose a reason for hiding this comment

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

Done, and moved this to be the function comment

// the row while also setting the update DML as the last
// statement.
_, err = client.ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error {
insert_stmt := spanner.Statement{
Copy link
Collaborator

Choose a reason for hiding this comment

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

issue: put variables in camelCase.

Copy link
Author

Choose a reason for hiding this comment

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

Done

update_stmt := spanner.Statement{
SQL: `UPDATE Singers SET LastName = 'Doe' WHERE SingerId = 54214`,
}
updateRowCount, err := txn.UpdateWithOptions(context.Background(), update_stmt, spanner.QueryOptions{LastStatement: true})
Copy link
Collaborator

Choose a reason for hiding this comment

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

issue: instantiate Context and QueryOptions objects on new lines, not inside call to UpdateWithOptions()

Copy link
Author

Choose a reason for hiding this comment

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

Updated QueryOptions to be instantiated on new line as suggested, and used the existing ctx instead of calling context.Background()

@sgorse123
Copy link
Author

Thank you @sgorse123 for submitting this sample!

FYI: this repo is intended for code samples to be published on cloud.google.com. I can't find any records for these region_tags being used in other programming languages.

Apologies as this is the first sample I am attempting to submit, I tried to follow the pattern I saw used in other DML samples.

Example of PostgresSQL sample that uses spanner_dml_{sample_spefic_suffix}.

Example of GoogleSQL sample that uses spanner_dml_{sample_spefic_suffix}.

Do you happen to know whether there is a plan to update the documentation with these samples?

Yes, we plan to add the samples in Cloud Spanner geo-partitioning documentation soon: https://cloud.google.com/spanner/docs/geo-partitioning

@sgorse123
Copy link
Author

Also tried to update the Java and Python PRs to have matching region tags.

Java PR: googleapis/java-spanner#3830
Python PR: googleapis/python-spanner#1341

Copy link
Collaborator

@telpirion telpirion left a comment

Choose a reason for hiding this comment

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

Thank you for addressing that first round of comments!

Just remove the calls to t.Parallel() in the tests and we can merge this.

@@ -1332,6 +1332,44 @@ func TestAddSplitPointsSample(t *testing.T) {
assertContains(t, out, "Added split points")
}

func TestDmlWithLastStatementSample(t *testing.T) {
_ = testutil.SystemTest(t)
t.Parallel()
Copy link
Collaborator

Choose a reason for hiding this comment

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

issue: don't run these tests in parallel unless you are explicitly testing parallelization. This can cause race conditions and/or resource contention in our CI/CD.


func TestPgDmlWithLastStatementSample(t *testing.T) {
_ = testutil.SystemTest(t)
t.Parallel()
Copy link
Collaborator

Choose a reason for hiding this comment

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

See previous comment. Please don't run tests in parallel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants