-
Notifications
You must be signed in to change notification settings - Fork 128
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: add option to indicate that a statement is the last in a transaction #3647
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f953f76
feat: add option to indicate that a statement is the last in a transa…
olavloite 34694b9
feat: set last_statement for autocommit statements
olavloite 39c1290
Merge branch 'main' into last-statement
olavloite 849b433
fix: add hashCode implementation
olavloite a227b7c
chore: generate libraries at Fri Feb 14 08:41:45 UTC 2025
cloud-java-bot bbdd857
fix: extract error details from SpannerException
olavloite 426a89a
Merge branch 'last-statement' of github.com:googleapis/java-spanner i…
olavloite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
import com.google.cloud.spanner.Mutation; | ||
import com.google.cloud.spanner.Options; | ||
import com.google.cloud.spanner.Options.QueryOption; | ||
import com.google.cloud.spanner.Options.QueryUpdateOption; | ||
import com.google.cloud.spanner.Options.UpdateOption; | ||
import com.google.cloud.spanner.PartitionOptions; | ||
import com.google.cloud.spanner.ReadOnlyTransaction; | ||
|
@@ -298,7 +299,8 @@ private ApiFuture<ResultSet> executeDmlReturningAsync( | |
writeTransaction.run( | ||
transaction -> | ||
DirectExecuteResultSet.ofResultSet( | ||
transaction.executeQuery(update.getStatement(), options))); | ||
transaction.executeQuery( | ||
update.getStatement(), appendLastStatement(options)))); | ||
state = UnitOfWorkState.COMMITTED; | ||
return resultSet; | ||
} catch (Throwable t) { | ||
|
@@ -554,11 +556,15 @@ private ApiFuture<Tuple<Long, ResultSet>> executeTransactionalUpdateAsync( | |
transaction -> { | ||
if (analyzeMode == AnalyzeMode.NONE) { | ||
return Tuple.of( | ||
transaction.executeUpdate(update.getStatement(), options), null); | ||
transaction.executeUpdate( | ||
update.getStatement(), appendLastStatement(options)), | ||
null); | ||
} | ||
ResultSet resultSet = | ||
transaction.analyzeUpdateStatement( | ||
update.getStatement(), analyzeMode.getQueryAnalyzeMode(), options); | ||
update.getStatement(), | ||
analyzeMode.getQueryAnalyzeMode(), | ||
appendLastStatement(options)); | ||
return Tuple.of(null, resultSet); | ||
}); | ||
state = UnitOfWorkState.COMMITTED; | ||
|
@@ -582,6 +588,29 @@ private ApiFuture<Tuple<Long, ResultSet>> executeTransactionalUpdateAsync( | |
return transactionalResult; | ||
} | ||
|
||
private static final QueryUpdateOption[] LAST_STATEMENT_OPTIONS = | ||
new QueryUpdateOption[] {Options.lastStatement()}; | ||
|
||
private static UpdateOption[] appendLastStatement(UpdateOption[] options) { | ||
if (options.length == 0) { | ||
return LAST_STATEMENT_OPTIONS; | ||
} | ||
UpdateOption[] result = new UpdateOption[options.length + 1]; | ||
System.arraycopy(options, 0, result, 0, options.length); | ||
result[result.length - 1] = LAST_STATEMENT_OPTIONS[0]; | ||
return result; | ||
} | ||
|
||
private static QueryOption[] appendLastStatement(QueryOption[] options) { | ||
if (options.length == 0) { | ||
return LAST_STATEMENT_OPTIONS; | ||
} | ||
QueryOption[] result = new QueryOption[options.length + 1]; | ||
System.arraycopy(options, 0, result, 0, options.length); | ||
result[result.length - 1] = LAST_STATEMENT_OPTIONS[0]; | ||
return result; | ||
} | ||
Comment on lines
+594
to
+612
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: We cannot generalize these two methods into one, due to the large number of different possible interfaces for Query/Update/Read/QueryOrUpdate/QueryOrUpdateOrTransaction/Options. |
||
|
||
/** | ||
* Adds a callback to the given future that retries the update statement using Partitioned DML if | ||
* the original statement fails with a {@link TransactionMutationLimitExceededException}. | ||
|
@@ -719,7 +748,8 @@ private ApiFuture<long[]> executeTransactionalBatchUpdateAsync( | |
try { | ||
long[] res = | ||
transaction.batchUpdate( | ||
Iterables.transform(updates, ParsedStatement::getStatement), options); | ||
Iterables.transform(updates, ParsedStatement::getStatement), | ||
appendLastStatement(options)); | ||
state = UnitOfWorkState.COMMITTED; | ||
return res; | ||
} catch (Throwable t) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Quick clarification: Since a SingleUseTransaction can only execute one query, do we implicitly mark that statement as the last statement? The changes in this file seem to be related to this, correct?
What happens if we don’t mark it as the last statement in SingleUseTransaction?
Since the customer cannot run additional queries anyway, I’m trying to understand the benefit of marking it as the last statement.
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.
Note: This is only relevant in read/write transactions, so in this case only for DML statements with a THEN RETURN clause, which are also executed using
executeQuery(..)
.Your observation that we automatically mark a statement in a SingleUseTransaction as the last_statement is correct. We do this, because we know that this will be the only statement in this transaction. By giving Spanner this hint, Spanner can internally optimise the execution of this statement by skipping some verifications, that are also executed during the Commit call.
And more in general: By supplying this hint to Spanner, Spanner could in the future also consider other possible optimizations that are possible when it knows that the current statement will be the last in a read/write transaction.