Skip to content

Commit

Permalink
fix: allow getting metadata without calling next() (#1691)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
olavloite authored Feb 16, 2022
1 parent 96cbac9 commit 4cfe74e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,26 @@ public ResultSetStats getStats() {

@Override
public Type getType() {
Preconditions.checkState(nextCalledByClient, MISSING_NEXT_CALL);
return delegate.getType();
}

@Override
public int getColumnCount() {
Preconditions.checkState(nextCalledByClient, MISSING_NEXT_CALL);
return delegate.getColumnCount();
}

@Override
public int getColumnIndex(String columnName) {
Preconditions.checkState(nextCalledByClient, MISSING_NEXT_CALL);
return delegate.getColumnIndex(columnName);
}

@Override
public Type getColumnType(int columnIndex) {
Preconditions.checkState(nextCalledByClient, MISSING_NEXT_CALL);
return delegate.getColumnType(columnIndex);
}

@Override
public Type getColumnType(String columnName) {
Preconditions.checkState(nextCalledByClient, MISSING_NEXT_CALL);
return delegate.getColumnType(columnName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,17 @@ private DirectExecuteResultSet createSubject() {
public void testMethodCallBeforeNext()
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
List<String> excludedMethods =
Arrays.asList("getStats", "next", "close", "ofResultSet", "equals", "hashCode");
Arrays.asList(
"getStats",
"next",
"close",
"ofResultSet",
"equals",
"hashCode",
"getType",
"getColumnCount",
"getColumnIndex",
"getColumnType");
DirectExecuteResultSet subject = createSubject();
callMethods(subject, excludedMethods, IllegalStateException.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.cloud.spanner.ResultSet;
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.Type;
import com.google.cloud.spanner.connection.ITAbstractSpannerTest;
import com.google.cloud.spanner.connection.SqlScriptVerifier;
import java.math.BigInteger;
Expand Down Expand Up @@ -213,4 +214,28 @@ public void testMultipleOpenResultSets() throws InterruptedException {
rs2.close();
}
}

@Test
public void testGetMetadataFromAnalyzeQuery() {
assumeFalse("analyze query is not supported on the emulator", isUsingEmulator());
try (ITConnection connection = createConnection()) {
// Request a query plan without executing the query and verify that we can get the column
// metadata of the query without calling resultSet.next() first.
try (ResultSet resultSet =
connection.analyzeQuery(
Statement.of("SELECT number, name FROM NUMBERS"), QueryAnalyzeMode.PLAN)) {
assertEquals(2, resultSet.getColumnCount());

assertEquals(0, resultSet.getColumnIndex("number"));
assertEquals("number", resultSet.getType().getStructFields().get(0).getName());
assertEquals(Type.int64(), resultSet.getColumnType(0));
assertEquals(Type.int64(), resultSet.getColumnType("number"));

assertEquals(1, resultSet.getColumnIndex("name"));
assertEquals("name", resultSet.getType().getStructFields().get(1).getName());
assertEquals(Type.string(), resultSet.getColumnType(1));
assertEquals(Type.string(), resultSet.getColumnType("name"));
}
}
}
}

0 comments on commit 4cfe74e

Please sign in to comment.