Skip to content

Commit db17d8d

Browse files
committed
fix: allow DML THEN RETURN with retryAbortsInternally=false
1 parent 8f0da07 commit db17d8d

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/connection/ReadWriteTransaction.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,19 @@ public ApiFuture<ResultSet> executeQueryAsync(
689689
InterceptorsUsage.IGNORE_INTERCEPTORS,
690690
ImmutableList.of(SpannerGrpc.getExecuteStreamingSqlMethod()));
691691
} else {
692-
res = super.executeQueryAsync(callType, statement, analyzeMode, options);
692+
// Handle both SELECT queries and DML with THEN RETURN without delegating to the base class,
693+
// which rejects non-SELECT statements.
694+
res =
695+
executeStatementAsync(
696+
callType,
697+
statement,
698+
() -> {
699+
checkTimedOut();
700+
checkAborted();
701+
return DirectExecuteResultSet.ofResultSet(
702+
internalExecuteQuery(statement, analyzeMode, options));
703+
},
704+
SpannerGrpc.getExecuteStreamingSqlMethod());
693705
}
694706
ApiFutures.addCallback(res, new StatementResultCallback<>(), MoreExecutors.directExecutor());
695707
return res;

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/ReadWriteTransactionTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,22 @@ public void testExecuteUpdate() {
287287
assertThat(get(transaction.executeUpdateAsync(CallType.SYNC, parsedStatement)), is(1L));
288288
}
289289

290+
@Test
291+
public void testExecuteQueryWithDmlReturningWithoutRetry() {
292+
ParsedStatement parsedStatement = mock(ParsedStatement.class);
293+
when(parsedStatement.getType()).thenReturn(StatementType.UPDATE);
294+
when(parsedStatement.isUpdate()).thenReturn(true);
295+
when(parsedStatement.hasReturningClause()).thenReturn(true);
296+
Statement statement =
297+
Statement.of("INSERT INTO TEST (ID, NAME) VALUES (1, 'x') THEN RETURN *");
298+
when(parsedStatement.getStatement()).thenReturn(statement);
299+
300+
ReadWriteTransaction transaction = createSubject(/* commitBehavior= */ CommitBehavior.SUCCEED);
301+
ResultSet rs =
302+
get(transaction.executeQueryAsync(CallType.SYNC, parsedStatement, AnalyzeMode.NONE));
303+
assertThat(rs, is(notNullValue()));
304+
}
305+
290306
@Test
291307
public void testGetCommitTimestampBeforeCommit() {
292308
ParsedStatement parsedStatement = mock(ParsedStatement.class);

0 commit comments

Comments
 (0)