|
24 | 24 | import static org.junit.jupiter.api.Assertions.assertThrows; |
25 | 25 | import static org.junit.jupiter.api.Assertions.assertTrue; |
26 | 26 |
|
| 27 | +import com.google.api.gax.paging.Page; |
27 | 28 | import com.google.cloud.ServiceOptions; |
| 29 | +import com.google.cloud.bigquery.BigQuery; |
| 30 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 31 | +import com.google.cloud.bigquery.Dataset; |
| 32 | +import com.google.cloud.bigquery.DatasetId; |
| 33 | +import com.google.cloud.bigquery.FieldValueList; |
| 34 | +import com.google.cloud.bigquery.Table; |
| 35 | +import com.google.cloud.bigquery.TableResult; |
28 | 36 | import java.sql.Connection; |
29 | 37 | import java.sql.DriverManager; |
30 | 38 | import java.sql.ResultSet; |
@@ -412,4 +420,59 @@ int getSizeOfResultSet(ResultSet resultSet) throws SQLException { |
412 | 420 | } |
413 | 421 | return count; |
414 | 422 | } |
| 423 | + |
| 424 | + @Test |
| 425 | + public void testTemporaryDatasetLocation() throws SQLException, InterruptedException { |
| 426 | + String projectId = DEFAULT_CATALOG; |
| 427 | + String location = "europe-west3"; |
| 428 | + String randomSuffix = String.valueOf(100 + new Random().nextInt(900)); |
| 429 | + String tempDatasetName = "jdbc_temp_dataset_" + System.currentTimeMillis() + "_" + randomSuffix; |
| 430 | + |
| 431 | + String customConnectionUrl = |
| 432 | + "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId=" |
| 433 | + + projectId |
| 434 | + + ";OAuthType=3;Timeout=3600;Location=" |
| 435 | + + location |
| 436 | + + ";AllowLargeResults=true;LargeResultDataset=" |
| 437 | + + tempDatasetName |
| 438 | + + ";"; |
| 439 | + |
| 440 | + BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService(); |
| 441 | + try (Connection connection = DriverManager.getConnection(customConnectionUrl)) { |
| 442 | + Statement statement = connection.createStatement(); |
| 443 | + String query = "SELECT 1 as val;"; |
| 444 | + try (ResultSet rs = statement.executeQuery(query)) { |
| 445 | + assertTrue(rs.next()); |
| 446 | + assertEquals(1, rs.getInt("val")); |
| 447 | + } |
| 448 | + |
| 449 | + Dataset dataset = bigQuery.getDataset(DatasetId.of(tempDatasetName)); |
| 450 | + assertNotNull(dataset); |
| 451 | + assertEquals(location, dataset.getLocation()); |
| 452 | + |
| 453 | + // Validate that the query results were written to a table in this dataset |
| 454 | + Page<Table> tables = dataset.list(); |
| 455 | + boolean tableFound = false; |
| 456 | + for (Table table : tables.iterateAll()) { |
| 457 | + if (table.getTableId().getTable().startsWith("temp_table_")) { |
| 458 | + tableFound = true; |
| 459 | + TableResult tableData = bigQuery.listTableData(table.getTableId()); |
| 460 | + assertNotNull(tableData); |
| 461 | + assertEquals(1, tableData.getTotalRows()); |
| 462 | + for (FieldValueList row : tableData.iterateAll()) { |
| 463 | + assertEquals(1, row.get(0).getLongValue()); |
| 464 | + } |
| 465 | + break; |
| 466 | + } |
| 467 | + } |
| 468 | + assertTrue(tableFound, "Expected temporary table was not found in the dataset"); |
| 469 | + } finally { |
| 470 | + try { |
| 471 | + bigQuery.delete( |
| 472 | + DatasetId.of(tempDatasetName), BigQuery.DatasetDeleteOption.deleteContents()); |
| 473 | + } catch (Exception e) { |
| 474 | + // Ignore cleanup exceptions to avoid masking the primary test failure |
| 475 | + } |
| 476 | + } |
| 477 | + } |
415 | 478 | } |
0 commit comments