Skip to content

Commit d27b3a7

Browse files
committed
fix: code review comments
1 parent 7e6b799 commit d27b3a7

File tree

5 files changed

+87
-35
lines changed

5 files changed

+87
-35
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,6 @@ public static interface SpannerEnvironment {
177177
*/
178178
@Nonnull
179179
String getOptimizerVersion();
180-
181-
/**
182-
* The optimizer statistics package to use. Must return an empty string to indicate that no
183-
* value has been set.
184-
*/
185-
@Nonnull
186-
String getOptimizerStatisticsPackage();
187180
}
188181

189182
/**
@@ -193,21 +186,13 @@ public static interface SpannerEnvironment {
193186
private static class SpannerEnvironmentImpl implements SpannerEnvironment {
194187
private static final SpannerEnvironmentImpl INSTANCE = new SpannerEnvironmentImpl();
195188
private static final String SPANNER_OPTIMIZER_VERSION_ENV_VAR = "SPANNER_OPTIMIZER_VERSION";
196-
private static final String SPANNER_OPTIMIZER_STATISTICS_PACKAGE_ENV_VAR =
197-
"SPANNER_OPTIMIZER_STATISTICS_PACKAGE";
198189

199190
private SpannerEnvironmentImpl() {}
200191

201192
@Override
202193
public String getOptimizerVersion() {
203194
return MoreObjects.firstNonNull(System.getenv(SPANNER_OPTIMIZER_VERSION_ENV_VAR), "");
204195
}
205-
206-
@Override
207-
public String getOptimizerStatisticsPackage() {
208-
return MoreObjects.firstNonNull(
209-
System.getenv(SPANNER_OPTIMIZER_STATISTICS_PACKAGE_ENV_VAR), "");
210-
}
211196
}
212197

213198
/** Builder for {@link SpannerOptions} instances. */

google-cloud-spanner/src/test/java/com/google/cloud/spanner/DatabaseClientImplTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,7 @@ public void testBackendQueryOptions() {
736736
// Just iterate over the results to execute the query.
737737
while (rs.next()) {}
738738
}
739-
// Check that the last query was executed using a custom optimizer version and statistics
740-
// package.
739+
// Check that the last query was executed using a custom optimizer version.
741740
List<AbstractMessage> requests = mockSpanner.getRequests();
742741
assertThat(requests).isNotEmpty();
743742
assertThat(requests.get(requests.size() - 1)).isInstanceOf(ExecuteSqlRequest.class);
@@ -776,8 +775,7 @@ public void testBackendQueryOptionsWithAnalyzeQuery() {
776775
while (rs.next()) {}
777776
}
778777
}
779-
// Check that the last query was executed using a custom optimizer version and statistics
780-
// package.
778+
// Check that the last query was executed using a custom optimizer version.
781779
List<AbstractMessage> requests = mockSpanner.getRequests();
782780
assertThat(requests).isNotEmpty();
783781
assertThat(requests.get(requests.size() - 1)).isInstanceOf(ExecuteSqlRequest.class);
@@ -818,8 +816,7 @@ public void testBackendPartitionQueryOptions() {
818816
// Just iterate over the results to execute the query.
819817
while (rs.next()) {}
820818
}
821-
// Check that the last query was executed using a custom optimizer version and statistics
822-
// package.
819+
// Check that the last query was executed using a custom optimizer version.
823820
List<AbstractMessage> requests = mockSpanner.getRequests();
824821
assertThat(requests).isNotEmpty();
825822
assertThat(requests.get(requests.size() - 1)).isInstanceOf(ExecuteSqlRequest.class);

google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerImplTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,16 @@ QueryOptions getEnvironmentQueryOptions() {
117117
}.build();
118118

119119
try (SpannerImpl implWithQueryOptions = new SpannerImpl(rpc, optionsWithQueryOptions);
120-
SpannerImpl implWithouQueryOptions = new SpannerImpl(rpc, optionsWithoutQueryOptions)) {
120+
SpannerImpl implWithoutQueryOptions = new SpannerImpl(rpc, optionsWithoutQueryOptions)) {
121121

122122
// Default query options are on a per-database basis, so we should only get the custom options
123123
// for 'db' and not for 'otherDb'.
124124
assertThat(implWithQueryOptions.getDefaultQueryOptions(db)).isEqualTo(queryOptions);
125125
assertThat(implWithQueryOptions.getDefaultQueryOptions(otherDb)).isEqualTo(defaultOptions);
126126

127127
// The other Spanner instance should return default options for both databases.
128-
assertThat(implWithouQueryOptions.getDefaultQueryOptions(db)).isEqualTo(defaultOptions);
129-
assertThat(implWithouQueryOptions.getDefaultQueryOptions(otherDb)).isEqualTo(defaultOptions);
128+
assertThat(implWithoutQueryOptions.getDefaultQueryOptions(db)).isEqualTo(defaultOptions);
129+
assertThat(implWithoutQueryOptions.getDefaultQueryOptions(otherDb)).isEqualTo(defaultOptions);
130130
}
131131
}
132132

google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerOptionsTest.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,6 @@ public void testDefaultQueryOptions() {
437437
public String getOptimizerVersion() {
438438
return "";
439439
}
440-
441-
@Override
442-
public String getOptimizerStatisticsPackage() {
443-
return "";
444-
}
445440
});
446441
SpannerOptions options =
447442
SpannerOptions.newBuilder()
@@ -461,11 +456,6 @@ public String getOptimizerStatisticsPackage() {
461456
public String getOptimizerVersion() {
462457
return "2";
463458
}
464-
465-
@Override
466-
public String getOptimizerStatisticsPackage() {
467-
return "";
468-
}
469459
});
470460
// Create options with '1' as the default query optimizer version. This should be overridden by
471461
// the environment variable.

google-cloud-spanner/src/test/java/com/google/cloud/spanner/it/ITQueryOptionsTest.java

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import com.google.cloud.spanner.Spanner;
2929
import com.google.cloud.spanner.SpannerException;
3030
import com.google.cloud.spanner.Statement;
31+
import com.google.cloud.spanner.TransactionContext;
32+
import com.google.cloud.spanner.TransactionRunner.TransactionCallable;
3133
import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions;
3234
import org.junit.BeforeClass;
3335
import org.junit.ClassRule;
@@ -49,7 +51,9 @@ public class ITQueryOptionsTest {
4951
@BeforeClass
5052
public static void setUpDatabase() {
5153
// Empty database.
52-
db = env.getTestHelper().createTestDatabase();
54+
db =
55+
env.getTestHelper()
56+
.createTestDatabase("CREATE TABLE TEST (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)");
5357
client = env.getTestHelper().getDatabaseClient(db);
5458
}
5559

@@ -98,6 +102,82 @@ public void executeQuery() {
98102
}
99103
}
100104

105+
@Test
106+
public void executeUpdate() {
107+
// Query optimizer version is ignored for DML statements by the backend, but setting it does not
108+
// cause an error.
109+
assertThat(
110+
client
111+
.readWriteTransaction()
112+
.run(
113+
new TransactionCallable<Long>() {
114+
@Override
115+
public Long run(TransactionContext transaction) throws Exception {
116+
return transaction.executeUpdate(
117+
Statement.newBuilder("INSERT INTO TEST (ID, NAME) VALUES (@id, @name)")
118+
.bind("id")
119+
.to(1L)
120+
.bind("name")
121+
.to("One")
122+
.withQueryOptions(
123+
QueryOptions.newBuilder().setOptimizerVersion("1").build())
124+
.build());
125+
}
126+
}))
127+
.isEqualTo(1L);
128+
129+
// Version 'latest' should also work.
130+
assertThat(
131+
client
132+
.readWriteTransaction()
133+
.run(
134+
new TransactionCallable<Long>() {
135+
@Override
136+
public Long run(TransactionContext transaction) throws Exception {
137+
return transaction.executeUpdate(
138+
Statement.newBuilder("INSERT INTO TEST (ID, NAME) VALUES (@id, @name)")
139+
.bind("id")
140+
.to(2L)
141+
.bind("name")
142+
.to("Two")
143+
.withQueryOptions(
144+
QueryOptions.newBuilder().setOptimizerVersion("latest").build())
145+
.build());
146+
}
147+
}))
148+
.isEqualTo(1L);
149+
150+
// Version '100000' is an invalid value, but is ignored by the backend.
151+
assertThat(
152+
client
153+
.readWriteTransaction()
154+
.run(
155+
new TransactionCallable<Long>() {
156+
@Override
157+
public Long run(TransactionContext transaction) throws Exception {
158+
return transaction.executeUpdate(
159+
Statement.newBuilder("INSERT INTO TEST (ID, NAME) VALUES (@id, @name)")
160+
.bind("id")
161+
.to(3L)
162+
.bind("name")
163+
.to("Three")
164+
.withQueryOptions(
165+
QueryOptions.newBuilder().setOptimizerVersion("10000").build())
166+
.build());
167+
}
168+
}))
169+
.isEqualTo(1L);
170+
171+
// Verify that query options are ignored with Partitioned DML as well, and that all the above
172+
// DML INSERT statements succeeded.
173+
assertThat(
174+
client.executePartitionedUpdate(
175+
Statement.newBuilder("UPDATE TEST SET NAME='updated' WHERE 1=1")
176+
.withQueryOptions(QueryOptions.newBuilder().setOptimizerVersion("1").build())
177+
.build()))
178+
.isEqualTo(3L);
179+
}
180+
101181
@Test
102182
public void spannerOptions() {
103183
// Version '1' should work.

0 commit comments

Comments
 (0)