|
70 | 70 |
|
71 | 71 | import static com.amazonaws.athena.connectors.datalakegen2.DataLakeGen2MetadataHandler.PARTITION_NUMBER; |
72 | 72 | import static org.junit.Assert.assertEquals; |
| 73 | +import static org.junit.Assert.assertNotNull; |
73 | 74 | import static org.mockito.ArgumentMatchers.any; |
74 | 75 | import static org.mockito.ArgumentMatchers.nullable; |
75 | 76 | import static org.mockito.Mockito.RETURNS_DEEP_STUBS; |
@@ -233,6 +234,88 @@ public void doGetTable() |
233 | 234 | assertEquals("testCatalog", getTableResponse.getCatalogName()); |
234 | 235 | } |
235 | 236 |
|
| 237 | + @Test |
| 238 | + public void testGetSchemaWithAzureServerlessEnvironment() |
| 239 | + throws Exception |
| 240 | + { |
| 241 | + BlockAllocator blockAllocator = new BlockAllocatorImpl(); |
| 242 | + |
| 243 | + // Mock the data type query result set for Azure serverless |
| 244 | + String[] dataTypeSchema = {"COLUMN_NAME", "DATA_TYPE"}; |
| 245 | + Object[][] dataTypeValues = { |
| 246 | + {"id", "int"}, |
| 247 | + {"name", "varchar"}, |
| 248 | + {"email", "nvarchar"}, |
| 249 | + {"created_date", "datetime"}, |
| 250 | + {"is_active", "bit"}, |
| 251 | + {"salary", "decimal"}, |
| 252 | + {"binary_data", "varbinary"} |
| 253 | + }; |
| 254 | + AtomicInteger dataTypeRowNumber = new AtomicInteger(-1); |
| 255 | + ResultSet dataTypeResultSet = mockResultSet(dataTypeSchema, dataTypeValues, dataTypeRowNumber); |
| 256 | + |
| 257 | + // Mock the prepared statement and its execution |
| 258 | + java.sql.PreparedStatement mockPreparedStatement = mock(java.sql.PreparedStatement.class); |
| 259 | + when(connection.prepareStatement(any(String.class))).thenReturn(mockPreparedStatement); |
| 260 | + when(mockPreparedStatement.executeQuery()).thenReturn(dataTypeResultSet); |
| 261 | + |
| 262 | + // Mock Azure serverless URL |
| 263 | + when(connection.getMetaData().getURL()).thenReturn("jdbc:sqlserver://myworkspace-ondemand.sql.azuresynapse.net:1433;database=mydatabase;"); |
| 264 | + when(connection.getCatalog()).thenReturn("testCatalog"); |
| 265 | + |
| 266 | + TableName inputTableName = new TableName("TESTSCHEMA", "TESTTABLE"); |
| 267 | + GetTableResponse getTableResponse = this.dataLakeGen2MetadataHandler.doGetTable( |
| 268 | + blockAllocator, new GetTableRequest(this.federatedIdentity, "testQueryId", "testCatalog", inputTableName, Collections.emptyMap())); |
| 269 | + |
| 270 | + // Verify the response |
| 271 | + assertNotNull(getTableResponse); |
| 272 | + assertEquals(inputTableName, getTableResponse.getTableName()); |
| 273 | + assertEquals("testCatalog", getTableResponse.getCatalogName()); |
| 274 | + |
| 275 | + // Verify that the schema was built using the direct SQL query approach (Azure serverless path) |
| 276 | + Schema responseSchema = getTableResponse.getSchema(); |
| 277 | + assertNotNull(responseSchema); |
| 278 | + assertEquals(8, responseSchema.getFields().size()); // 7 columns from our mock data + 1 partition field |
| 279 | + } |
| 280 | + |
| 281 | + @Test |
| 282 | + public void testGetSchemaWithStandardEnvironment() |
| 283 | + throws Exception |
| 284 | + { |
| 285 | + BlockAllocator blockAllocator = new BlockAllocatorImpl(); |
| 286 | + |
| 287 | + // Mock the data type query result set |
| 288 | + String[] dataTypeSchema = {"COLUMN_NAME", "DATA_TYPE"}; |
| 289 | + Object[][] dataTypeValues = {{"testCol1", "int"}, {"testCol2", "varchar"}}; |
| 290 | + AtomicInteger dataTypeRowNumber = new AtomicInteger(-1); |
| 291 | + ResultSet dataTypeResultSet = mockResultSet(dataTypeSchema, dataTypeValues, dataTypeRowNumber); |
| 292 | + |
| 293 | + // Mock the prepared statement and its execution |
| 294 | + java.sql.PreparedStatement mockPreparedStatement = mock(java.sql.PreparedStatement.class); |
| 295 | + when(connection.prepareStatement(any(String.class))).thenReturn(mockPreparedStatement); |
| 296 | + when(mockPreparedStatement.executeQuery()).thenReturn(dataTypeResultSet); |
| 297 | + |
| 298 | + // Mock standard SQL Server URL (non-serverless) |
| 299 | + when(connection.getMetaData().getURL()).thenReturn("jdbc:sqlserver://myserver.database.windows.net:1433;database=mydatabase;"); |
| 300 | + when(connection.getCatalog()).thenReturn("testCatalog"); |
| 301 | + |
| 302 | + // Mock the getColumns result set for standard environment |
| 303 | + String[] schema = {"TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "COLUMN_NAME", "DATA_TYPE", "TYPE_NAME", "COLUMN_SIZE", "BUFFER_LENGTH", "DECIMAL_DIGITS", "NUM_PREC_RADIX", "NULLABLE", "REMARKS", "COLUMN_DEF", "SQL_DATA_TYPE", "SQL_DATETIME_SUB", "CHAR_OCTET_LENGTH", "ORDINAL_POSITION", "IS_NULLABLE"}; |
| 304 | + Object[][] values = {{"testCatalog", "TESTSCHEMA", "TESTTABLE", "testCol1", Types.INTEGER, "int", 10, 4, 0, 10, 1, "", "", Types.INTEGER, 0, 4, 1, "YES"}}; |
| 305 | + AtomicInteger rowNumber = new AtomicInteger(-1); |
| 306 | + ResultSet getColumnsResultSet = mockResultSet(schema, values, rowNumber); |
| 307 | + when(connection.getMetaData().getColumns("testCatalog", "TESTSCHEMA", "TESTTABLE", null)).thenReturn(getColumnsResultSet); |
| 308 | + |
| 309 | + TableName inputTableName = new TableName("TESTSCHEMA", "TESTTABLE"); |
| 310 | + GetTableResponse getTableResponse = this.dataLakeGen2MetadataHandler.doGetTable( |
| 311 | + blockAllocator, new GetTableRequest(this.federatedIdentity, "testQueryId", "testCatalog", inputTableName, Collections.emptyMap())); |
| 312 | + |
| 313 | + // Verify the response |
| 314 | + assertNotNull(getTableResponse); |
| 315 | + assertEquals(inputTableName, getTableResponse.getTableName()); |
| 316 | + assertEquals("testCatalog", getTableResponse.getCatalogName()); |
| 317 | + } |
| 318 | + |
236 | 319 | @Test |
237 | 320 | public void doListTables() throws Exception |
238 | 321 | { |
|
0 commit comments