Skip to content

Commit 3ccff29

Browse files
Db2 Partition Query Fix. (#3257)
Co-authored-by: Fal Bharadwaj <falgunb@amazon.com>
1 parent 0e1875a commit 3ccff29

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

athena-db2/src/main/java/com/amazonaws/athena/connectors/db2/Db2Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class Db2Constants
3939
"from syscat.tables where type in ('T', 'U', 'V', 'W') and tabschema = ? " +
4040
"ORDER BY tabname OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
4141

42-
static final String PARTITION_QUERY = "SELECT DATAPARTITIONID FROM SYSCAT.DATAPARTITIONS WHERE TABSCHEMA = ? AND TABNAME = ? AND SEQNO > 0";
42+
static final String PARTITION_QUERY = "SELECT DATAPARTITIONID FROM SYSCAT.DATAPARTITIONS WHERE TABSCHEMA = ? AND TABNAME = ? AND SEQNO >= 0";
4343
static final String COLUMN_INFO_QUERY = "select colname, typename from syscat.columns where tabschema = ? AND tabname = ?";
4444
private Db2Constants() {}
4545
}

athena-db2/src/test/java/com/amazonaws/athena/connectors/db2/Db2MetadataHandlerTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import java.util.stream.Collectors;
7272

7373
import static com.amazonaws.athena.connector.lambda.metadata.ListTablesRequest.UNLIMITED_PAGE_SIZE_VALUE;
74+
import static com.amazonaws.athena.connector.lambda.domain.predicate.Constraints.DEFAULT_NO_LIMIT;
7475
import static com.amazonaws.athena.connectors.db2.Db2Constants.PARTITION_NUMBER;
7576
import static org.mockito.ArgumentMatchers.nullable;
7677

@@ -374,6 +375,52 @@ public void doListPaginatedTables()
374375
// With unlimited page size, there should be no next token (null) since all results are returned
375376
executePaginatedTableTest(values, expected, "0", UNLIMITED_PAGE_SIZE_VALUE, null);
376377
}
378+
379+
@Test
380+
public void doGetSplits_withSinglePartition_returnsSplit()
381+
throws Exception {
382+
Constraints constraints = new Constraints(
383+
Collections.emptyMap(),
384+
Collections.emptyList(),
385+
Collections.emptyList(),
386+
DEFAULT_NO_LIMIT,
387+
Collections.emptyMap(),
388+
null
389+
);
390+
TableName tableName = new TableName("testSchema", "testTable");
391+
392+
PreparedStatement partitionPreparedStatement = Mockito.mock(PreparedStatement.class);
393+
Mockito.when(this.connection.prepareStatement(Db2Constants.PARTITION_QUERY)).thenReturn(partitionPreparedStatement);
394+
ResultSet partitionResultSet = mockResultSet(new String[]{"DATAPARTITIONID"}, new int[]{Types.INTEGER}, new Object[][]{{0}}, new AtomicInteger(-1));
395+
Mockito.when(partitionPreparedStatement.executeQuery()).thenReturn(partitionResultSet);
396+
397+
PreparedStatement colNamePreparedStatement = Mockito.mock(PreparedStatement.class);
398+
Mockito.when(this.connection.prepareStatement(Db2Constants.COLUMN_INFO_QUERY)).thenReturn(colNamePreparedStatement);
399+
ResultSet colNameResultSet = mockResultSet(new String[]{"COLNAME"}, new int[]{Types.VARCHAR}, new Object[][]{{"PC"}}, new AtomicInteger(-1));
400+
Mockito.when(colNamePreparedStatement.executeQuery()).thenReturn(colNameResultSet);
401+
Mockito.when(colNameResultSet.next()).thenReturn(true);
402+
403+
Mockito.when(this.connection.getMetaData().getSearchStringEscape()).thenReturn(null);
404+
405+
Schema partitionSchema = this.db2MetadataHandler.getPartitionSchema("testCatalogName");
406+
Set<String> partitionCols = partitionSchema.getFields().stream().map(Field::getName).collect(Collectors.toSet());
407+
GetTableLayoutRequest getTableLayoutRequest = new GetTableLayoutRequest(this.federatedIdentity, "testQueryId", "testCatalogName", tableName, constraints, partitionSchema, partitionCols);
408+
409+
GetTableLayoutResponse getTableLayoutResponse = this.db2MetadataHandler.doGetTableLayout(this.blockAllocator, getTableLayoutRequest);
410+
411+
BlockAllocator splitBlockAllocator = new BlockAllocatorImpl();
412+
GetSplitsRequest getSplitsRequest = new GetSplitsRequest(this.federatedIdentity, "testQueryId", "testCatalogName", tableName, getTableLayoutResponse.getPartitions(), new ArrayList<>(partitionCols), constraints, null);
413+
GetSplitsResponse getSplitsResponse = this.db2MetadataHandler.doGetSplits(splitBlockAllocator, getSplitsRequest);
414+
415+
Set<Map<String, String>> expectedSplits = com.google.common.collect.ImmutableSet.of(
416+
com.google.common.collect.ImmutableMap.of(
417+
PARTITION_NUMBER, "0",
418+
Db2MetadataHandler.PARTITIONING_COLUMN, "PC"));
419+
420+
Assert.assertEquals(expectedSplits.size(), getSplitsResponse.getSplits().size());
421+
Set<Map<String, String>> actualSplits = getSplitsResponse.getSplits().stream().map(Split::getProperties).collect(Collectors.toSet());
422+
Assert.assertEquals(expectedSplits, actualSplits);
423+
}
377424

378425
/**
379426
* Helper method to execute paginated table test and verify results

0 commit comments

Comments
 (0)