-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
94 additions
and
4 deletions.
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
75 changes: 75 additions & 0 deletions
75
google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/QueryOptionsTest.java
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.spanner.connection; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import com.google.cloud.spanner.ResultSet; | ||
import com.google.cloud.spanner.Statement; | ||
import com.google.spanner.v1.ExecuteSqlRequest; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class QueryOptionsTest extends AbstractMockServerTest { | ||
|
||
@Test | ||
public void testUseOptimizerVersionFromConnectionUrl() { | ||
try (Connection connection = createConnection("?optimizerVersion=10")) { | ||
Repeat.twice( | ||
() -> { | ||
executeSelect1AndConsumeResults(connection); | ||
|
||
assertEquals(1, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class)); | ||
ExecuteSqlRequest request = | ||
mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(0); | ||
assertEquals("10", request.getQueryOptions().getOptimizerVersion()); | ||
|
||
mockSpanner.clearRequests(); | ||
}); | ||
} | ||
} | ||
|
||
@Test | ||
public void testUseOptimizerVersionFromStatement() { | ||
try (Connection connection = createConnection()) { | ||
connection.execute(Statement.of("set optimizer_version='7'")); | ||
|
||
Repeat.twice( | ||
() -> { | ||
executeSelect1AndConsumeResults(connection); | ||
|
||
assertEquals(1, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class)); | ||
ExecuteSqlRequest request = | ||
mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(0); | ||
assertEquals("7", request.getQueryOptions().getOptimizerVersion()); | ||
|
||
mockSpanner.clearRequests(); | ||
}); | ||
} | ||
} | ||
|
||
private void executeSelect1AndConsumeResults(Connection connection) { | ||
try (ResultSet resultSet = connection.executeQuery(SELECT1_STATEMENT)) { | ||
//noinspection StatementWithEmptyBody | ||
while (resultSet.next()) { | ||
// ignore | ||
} | ||
} | ||
} | ||
} |