Skip to content

Commit 741e4cf

Browse files
authored
fix: use default query options with statement cache (#2860)
Statements that were executed using the Connection API and that were not found in the statement cache (that is; it was the first time it was executed), would not use the default query options that had been set for the connection. This would mean that for example an optimizer version that had been set for the connection would not be used the first time a given query string would be executed using a connection. All subsequent executions of the statement would use specified optimizer version. This change ensures that both the first and all following executions of the statement use the default query options that have been set.
1 parent aa9ad7f commit 741e4cf

File tree

3 files changed

+94
-4
lines changed

3 files changed

+94
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ ParsedStatement parse(Statement statement, QueryOptions defaultQueryOptions) {
470470

471471
ParsedStatement parsedStatement = statementCache.getIfPresent(statement.getSql());
472472
if (parsedStatement == null) {
473-
parsedStatement = internalParse(statement, null);
473+
parsedStatement = internalParse(statement, defaultQueryOptions);
474474
statementCache.put(statement.getSql(), parsedStatement.forCache());
475475
return parsedStatement;
476476
}

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.google.protobuf.ListValue;
3737
import com.google.protobuf.Value;
3838
import com.google.spanner.v1.ExecuteSqlRequest;
39+
import com.google.spanner.v1.ResultSet;
3940
import com.google.spanner.v1.ResultSetMetadata;
4041
import com.google.spanner.v1.ResultSetStats;
4142
import com.google.spanner.v1.StructType;
@@ -66,7 +67,10 @@ public abstract class AbstractMockServerTest {
6667
public static final long COUNT_AFTER_INSERT = 1L;
6768
public static final Statement SELECT_COUNT_STATEMENT =
6869
Statement.of("SELECT COUNT(*) AS C FROM TEST WHERE ID=1");
69-
private static final ResultSetMetadata SELECT_COUNT_METADATA =
70+
71+
protected static final Statement SELECT1_STATEMENT = Statement.of("SELECT 1");
72+
73+
private static final ResultSetMetadata SINGLE_COL_INT64_RESULTSET_METADATA =
7074
ResultSetMetadata.newBuilder()
7175
.setRowType(
7276
StructType.newBuilder()
@@ -86,7 +90,7 @@ public abstract class AbstractMockServerTest {
8690
.setStringValue(String.valueOf(COUNT_BEFORE_INSERT))
8791
.build())
8892
.build())
89-
.setMetadata(SELECT_COUNT_METADATA)
93+
.setMetadata(SINGLE_COL_INT64_RESULTSET_METADATA)
9094
.build();
9195
public static final com.google.spanner.v1.ResultSet SELECT_COUNT_RESULTSET_AFTER_INSERT =
9296
com.google.spanner.v1.ResultSet.newBuilder()
@@ -95,7 +99,7 @@ public abstract class AbstractMockServerTest {
9599
.addValues(
96100
Value.newBuilder().setStringValue(String.valueOf(COUNT_AFTER_INSERT)).build())
97101
.build())
98-
.setMetadata(SELECT_COUNT_METADATA)
102+
.setMetadata(SINGLE_COL_INT64_RESULTSET_METADATA)
99103
.build();
100104
public static final com.google.spanner.v1.ResultSet UPDATE_RETURNING_RESULTSET =
101105
com.google.spanner.v1.ResultSet.newBuilder()
@@ -110,6 +114,16 @@ public abstract class AbstractMockServerTest {
110114
.setType(Type.newBuilder().setCodeValue(TypeCode.INT64_VALUE))
111115
.build())))
112116
.build();
117+
118+
protected static final ResultSet SELECT1_RESULTSET =
119+
ResultSet.newBuilder()
120+
.setMetadata(SINGLE_COL_INT64_RESULTSET_METADATA)
121+
.addRows(
122+
ListValue.newBuilder()
123+
.addValues(Value.newBuilder().setStringValue("1").build())
124+
.build())
125+
.build();
126+
113127
public static final Statement INSERT_STATEMENT =
114128
Statement.of("INSERT INTO TEST (ID, NAME) VALUES (1, 'test aborted')");
115129
public static final Statement INSERT_RETURNING_STATEMENT =
@@ -173,6 +187,7 @@ public void getOperation(
173187
StatementResult.updateReturning(PG_INSERT_RETURNING_STATEMENT, UPDATE_RETURNING_RESULTSET));
174188
mockSpanner.putStatementResult(
175189
StatementResult.query(SELECT_RANDOM_STATEMENT, RANDOM_RESULT_SET));
190+
mockSpanner.putStatementResult(StatementResult.query(SELECT1_STATEMENT, SELECT1_RESULTSET));
176191

177192
futureParentHandlers = Logger.getLogger(AbstractFuture.class.getName()).getUseParentHandlers();
178193
exceptionRunnableParentHandlers =
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.spanner.connection;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import com.google.cloud.spanner.ResultSet;
22+
import com.google.cloud.spanner.Statement;
23+
import com.google.spanner.v1.ExecuteSqlRequest;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.junit.runners.JUnit4;
27+
28+
@RunWith(JUnit4.class)
29+
public class QueryOptionsTest extends AbstractMockServerTest {
30+
31+
@Test
32+
public void testUseOptimizerVersionFromConnectionUrl() {
33+
try (Connection connection = createConnection("?optimizerVersion=10")) {
34+
Repeat.twice(
35+
() -> {
36+
executeSelect1AndConsumeResults(connection);
37+
38+
assertEquals(1, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
39+
ExecuteSqlRequest request =
40+
mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(0);
41+
assertEquals("10", request.getQueryOptions().getOptimizerVersion());
42+
43+
mockSpanner.clearRequests();
44+
});
45+
}
46+
}
47+
48+
@Test
49+
public void testUseOptimizerVersionFromStatement() {
50+
try (Connection connection = createConnection()) {
51+
connection.execute(Statement.of("set optimizer_version='7'"));
52+
53+
Repeat.twice(
54+
() -> {
55+
executeSelect1AndConsumeResults(connection);
56+
57+
assertEquals(1, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
58+
ExecuteSqlRequest request =
59+
mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(0);
60+
assertEquals("7", request.getQueryOptions().getOptimizerVersion());
61+
62+
mockSpanner.clearRequests();
63+
});
64+
}
65+
}
66+
67+
private void executeSelect1AndConsumeResults(Connection connection) {
68+
try (ResultSet resultSet = connection.executeQuery(SELECT1_STATEMENT)) {
69+
//noinspection StatementWithEmptyBody
70+
while (resultSet.next()) {
71+
// ignore
72+
}
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)