Skip to content

Commit 9a2f3fc

Browse files
authored
fix: getColumnCount would fail for empty partititioned result sets (#2588)
Calling getColumnCount() would fail for ResultSets that were returned for partitioned queries. The reason for this was that the number of columns would be calculated based on the last seen row. This would not work for ResultSets without any rows at all.
1 parent 9c69e57 commit 9a2f3fc

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

+20
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,26 @@ public Type getType() {
394394
return rowProducer.getType();
395395
}
396396

397+
@Override
398+
public int getColumnCount() {
399+
return getType().getStructFields().size();
400+
}
401+
402+
@Override
403+
public int getColumnIndex(String columnName) {
404+
return getType().getFieldIndex(columnName);
405+
}
406+
407+
@Override
408+
public Type getColumnType(int columnIndex) {
409+
return getType().getStructFields().get(columnIndex).getType();
410+
}
411+
412+
@Override
413+
public Type getColumnType(String columnName) {
414+
return getType().getStructFields().get(getColumnIndex(columnName)).getType();
415+
}
416+
397417
@Override
398418
public int getNumPartitions() {
399419
return rowProducer.getNumPartitions();

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

+15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.cloud.spanner.connection;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
2021
import static org.junit.Assert.assertThrows;
2122
import static org.junit.Assert.assertTrue;
2223
import static org.junit.Assert.fail;
@@ -30,6 +31,7 @@
3031
import com.google.cloud.spanner.SpannerException;
3132
import com.google.cloud.spanner.SpannerExceptionFactory;
3233
import com.google.cloud.spanner.Struct;
34+
import com.google.cloud.spanner.Type;
3335
import java.util.ArrayList;
3436
import java.util.BitSet;
3537
import java.util.Collection;
@@ -140,6 +142,19 @@ public void testAllResultsAreReturned() {
140142
while (resultSet.next()) {
141143
assertRowExists(results.allRows, resultSet.getCurrentRowAsStruct(), rowsFound);
142144
}
145+
// Verify that we can get the metadata after having gotten all rows.
146+
// This failed in the initial release of this feature for result sets that were empty. The
147+
// reason for that was that the initial implementation would do a call to currentRowAsStruct,
148+
// which would always be null for result sets that never returned any data.
149+
assertNotNull(resultSet.getMetadata());
150+
if (numPartitions == 0) {
151+
assertEquals(0, resultSet.getColumnCount());
152+
} else {
153+
assertEquals(18, resultSet.getColumnCount());
154+
assertEquals(Type.bool(), resultSet.getColumnType(0));
155+
assertEquals(Type.bool(), resultSet.getColumnType("COL0"));
156+
assertEquals(10, resultSet.getColumnIndex("COL10"));
157+
}
143158
// Check that all rows were found.
144159
assertEquals(results.allRows.size(), rowsFound.nextClearBit(0));
145160
// Check extended metadata.

0 commit comments

Comments
 (0)