Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getColumnCount would fail for empty partititioned result sets #2588

Merged
merged 1 commit into from Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -394,6 +394,26 @@ public Type getType() {
return rowProducer.getType();
}

@Override
public int getColumnCount() {
return getType().getStructFields().size();
}

@Override
public int getColumnIndex(String columnName) {
return getType().getFieldIndex(columnName);
}

@Override
public Type getColumnType(int columnIndex) {
return getType().getStructFields().get(columnIndex).getType();
}

@Override
public Type getColumnType(String columnName) {
return getType().getStructFields().get(getColumnIndex(columnName)).getType();
}

@Override
public int getNumPartitions() {
return rowProducer.getNumPartitions();
Expand Down
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.spanner.connection;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
Expand All @@ -30,6 +31,7 @@
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.Struct;
import com.google.cloud.spanner.Type;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collection;
Expand Down Expand Up @@ -140,6 +142,19 @@ public void testAllResultsAreReturned() {
while (resultSet.next()) {
assertRowExists(results.allRows, resultSet.getCurrentRowAsStruct(), rowsFound);
}
// Verify that we can get the metadata after having gotten all rows.
// This failed in the initial release of this feature for result sets that were empty. The
// reason for that was that the initial implementation would do a call to currentRowAsStruct,
// which would always be null for result sets that never returned any data.
assertNotNull(resultSet.getMetadata());
if (numPartitions == 0) {
assertEquals(0, resultSet.getColumnCount());
} else {
assertEquals(18, resultSet.getColumnCount());
assertEquals(Type.bool(), resultSet.getColumnType(0));
assertEquals(Type.bool(), resultSet.getColumnType("COL0"));
assertEquals(10, resultSet.getColumnIndex("COL10"));
}
// Check that all rows were found.
assertEquals(results.allRows.size(), rowsFound.nextClearBit(0));
// Check extended metadata.
Expand Down