Repository navigation
Conversation
PostgreSQL-dialect Spanner uses RETURNING rather than THEN RETURN to read generated columns from inserted rows.
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new sample script, postgresql_insert_dml_returning.py, along with its unit and integration tests, to demonstrate how to insert rows into a PostgreSQL-dialect Spanner database and return values using the RETURNING clause. The review feedback suggests a couple of improvements to make the transaction callback more idiomatic and robust: replacing the row_counts list with a nonlocal integer variable and adding a safety check to ensure results.stats is not None before accessing row_count_exact.
| inserted_names: list[str] = [] | ||
| row_counts: list[int] = [] | ||
|
|
||
| def insert_singers(transaction: spanner_v1.Transaction) -> None: | ||
| inserted_names.clear() | ||
| row_counts.clear() | ||
| results = transaction.execute_sql( | ||
| "INSERT INTO Singers (SingerId, FirstName, LastName) VALUES " | ||
| "(21, 'Luann', 'Chizoba'), " | ||
| "(22, 'Denis', 'Patricio'), " | ||
| "(23, 'Felix', 'Ronan'), " | ||
| "(24, 'Dominik', 'Martyna') " | ||
| "RETURNING FullName" | ||
| ) | ||
| for row in results: | ||
| inserted_names.append(str(row[0])) | ||
| row_counts.append(int(results.stats.row_count_exact)) |
There was a problem hiding this comment.
Using a list (row_counts) to store a single integer value across the transaction callback scope is non-idiomatic in Python and can be simplified using the nonlocal keyword.
Additionally, to adhere to defensive programming practices, we should safeguard against cases where results.stats might be None (or not yet populated) before accessing row_count_exact. Accessing results.stats.row_count_exact directly without a None check could raise an AttributeError, and subsequently accessing row_counts[-1] could raise an IndexError if the list remains empty.
We can refactor this to use a simple row_count integer variable with nonlocal and a safe None check.
| inserted_names: list[str] = [] | |
| row_counts: list[int] = [] | |
| def insert_singers(transaction: spanner_v1.Transaction) -> None: | |
| inserted_names.clear() | |
| row_counts.clear() | |
| results = transaction.execute_sql( | |
| "INSERT INTO Singers (SingerId, FirstName, LastName) VALUES " | |
| "(21, 'Luann', 'Chizoba'), " | |
| "(22, 'Denis', 'Patricio'), " | |
| "(23, 'Felix', 'Ronan'), " | |
| "(24, 'Dominik', 'Martyna') " | |
| "RETURNING FullName" | |
| ) | |
| for row in results: | |
| inserted_names.append(str(row[0])) | |
| row_counts.append(int(results.stats.row_count_exact)) | |
| inserted_names: list[str] = [] | |
| row_count = 0 | |
| def insert_singers(transaction: spanner_v1.Transaction) -> None: | |
| nonlocal row_count | |
| inserted_names.clear() | |
| results = transaction.execute_sql( | |
| "INSERT INTO Singers (SingerId, FirstName, LastName) VALUES " | |
| "(21, 'Luann', 'Chizoba'), " | |
| "(22, 'Denis', 'Patricio'), " | |
| "(23, 'Felix', 'Ronan'), " | |
| "(24, 'Dominik', 'Martyna') " | |
| "RETURNING FullName" | |
| ) | |
| for row in results: | |
| inserted_names.append(str(row[0])) | |
| row_count = int(results.stats.row_count_exact) if results.stats else 0 |
There was a problem hiding this comment.
Thanks! This comment refers to an earlier revision of this file; the code here has since been reworked in later commits, so this thread no longer applies to the current diff.
| print(f"Resource ID: {database.name}") | ||
| for full_name in inserted_names: | ||
| print(f"Full Name: {full_name}") | ||
| print(f"Inserted Rows: {row_counts[-1]}") |
There was a problem hiding this comment.
Thanks! This comment refers to an earlier revision of this file; the code here has since been reworked in later commits, so this thread no longer applies to the current diff.
Uses a nonlocal row counter with a defensive results.stats check and omits project identifiers from stdout and stderr.
…l_returning Problem: multi-line SQL and DDL statements were written as adjacent string fragments. That style is easy to get wrong (a missing trailing space joins two SQL tokens) and is harder to read than the statement itself, and the samples in this set mixed it with triple-quoted SQL. Solution: write each multi-line statement as a single triple-quoted string, matching the layout already used by the other samples. Only whitespace between SQL tokens changes; the statements are otherwise identical. Files updated: - spanner/postgresql/src/spanner_postgresql/postgresql_insert_dml_returning.py
Description
PostgreSQL-dialect Spanner databases use standard PostgreSQL
INSERT ... RETURNINGsyntax to return generated columns in a single round trip.Adds
spanner_postgresql_insert_dml_returningusing the canonical cross-language region tag.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions