Skip to content

Commit 4cfe74e

Browse files
authored
fix: allow getting metadata without calling next() (#1691)
* fix: allow getting metadata without calling next() DirectExecuteResultSet, which is used in the Connection API, directly executes any query that it is given without the need to call ResultSet.next() first. This also makes it possible to get the ResultSet metadata from the result without the need to calling ResultSet.next() first. * fix: add methods to exclude list for test
1 parent 96cbac9 commit 4cfe74e

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,31 +96,26 @@ public ResultSetStats getStats() {
9696

9797
@Override
9898
public Type getType() {
99-
Preconditions.checkState(nextCalledByClient, MISSING_NEXT_CALL);
10099
return delegate.getType();
101100
}
102101

103102
@Override
104103
public int getColumnCount() {
105-
Preconditions.checkState(nextCalledByClient, MISSING_NEXT_CALL);
106104
return delegate.getColumnCount();
107105
}
108106

109107
@Override
110108
public int getColumnIndex(String columnName) {
111-
Preconditions.checkState(nextCalledByClient, MISSING_NEXT_CALL);
112109
return delegate.getColumnIndex(columnName);
113110
}
114111

115112
@Override
116113
public Type getColumnType(int columnIndex) {
117-
Preconditions.checkState(nextCalledByClient, MISSING_NEXT_CALL);
118114
return delegate.getColumnType(columnIndex);
119115
}
120116

121117
@Override
122118
public Type getColumnType(String columnName) {
123-
Preconditions.checkState(nextCalledByClient, MISSING_NEXT_CALL);
124119
return delegate.getColumnType(columnName);
125120
}
126121

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,17 @@ private DirectExecuteResultSet createSubject() {
5353
public void testMethodCallBeforeNext()
5454
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
5555
List<String> excludedMethods =
56-
Arrays.asList("getStats", "next", "close", "ofResultSet", "equals", "hashCode");
56+
Arrays.asList(
57+
"getStats",
58+
"next",
59+
"close",
60+
"ofResultSet",
61+
"equals",
62+
"hashCode",
63+
"getType",
64+
"getColumnCount",
65+
"getColumnIndex",
66+
"getColumnType");
5767
DirectExecuteResultSet subject = createSubject();
5868
callMethods(subject, excludedMethods, IllegalStateException.class);
5969
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/it/ITReadOnlySpannerTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.google.cloud.spanner.ResultSet;
3434
import com.google.cloud.spanner.SpannerException;
3535
import com.google.cloud.spanner.Statement;
36+
import com.google.cloud.spanner.Type;
3637
import com.google.cloud.spanner.connection.ITAbstractSpannerTest;
3738
import com.google.cloud.spanner.connection.SqlScriptVerifier;
3839
import java.math.BigInteger;
@@ -213,4 +214,28 @@ public void testMultipleOpenResultSets() throws InterruptedException {
213214
rs2.close();
214215
}
215216
}
217+
218+
@Test
219+
public void testGetMetadataFromAnalyzeQuery() {
220+
assumeFalse("analyze query is not supported on the emulator", isUsingEmulator());
221+
try (ITConnection connection = createConnection()) {
222+
// Request a query plan without executing the query and verify that we can get the column
223+
// metadata of the query without calling resultSet.next() first.
224+
try (ResultSet resultSet =
225+
connection.analyzeQuery(
226+
Statement.of("SELECT number, name FROM NUMBERS"), QueryAnalyzeMode.PLAN)) {
227+
assertEquals(2, resultSet.getColumnCount());
228+
229+
assertEquals(0, resultSet.getColumnIndex("number"));
230+
assertEquals("number", resultSet.getType().getStructFields().get(0).getName());
231+
assertEquals(Type.int64(), resultSet.getColumnType(0));
232+
assertEquals(Type.int64(), resultSet.getColumnType("number"));
233+
234+
assertEquals(1, resultSet.getColumnIndex("name"));
235+
assertEquals("name", resultSet.getType().getStructFields().get(1).getName());
236+
assertEquals(Type.string(), resultSet.getColumnType(1));
237+
assertEquals(Type.string(), resultSet.getColumnType("name"));
238+
}
239+
}
240+
}
216241
}

0 commit comments

Comments
 (0)