{"record":{"id":"9d912c0f171bdd5b","repo":"apache/beam","slug":"table-tablereference-already-exists-it-should-have-been","errorCode":null,"errorMessage":"Table '${tableReference}' already exists. It should have been cleaned up by the test rule.","messagePattern":"Table '(.+?)' already exists\\. It should have been cleaned up by the test rule\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/TestBigQuery.java","lineNumber":149,"sourceCode":"            .setProjectId(\n                pipelineOptions.getBigQueryProject() == null\n                    ? pipelineOptions.getProject()\n                    : pipelineOptions.getBigQueryProject())\n            .setDatasetId(pipelineOptions.getTargetDataset())\n            .setTableId(createRandomizedName(description));\n\n    Table newTable =\n        new Table()\n            .setTableReference(tableReference)\n            .setSchema(BigQueryUtils.toTableSchema(schema))\n            .setDescription(\n                \"Table created for \"\n                    + description.getDisplayName()\n                    + \" by TestBigQueryRule. \"\n                    + \"Should be automatically cleaned up after test completion.\");\n\n    if (datasetService.getTable(tableReference) != null) {\n      throw new IllegalStateException(\n          \"Table '\"\n              + tableReference\n              + \"' already exists. \"\n              + \"It should have been cleaned up by the test rule.\");\n    }\n\n    datasetService.createTable(newTable);\n    table = newTable;\n    return table;\n  }\n\n  private void tearDown() throws IOException, InterruptedException {\n    if (this.datasetService == null) {\n      return;\n    }\n\n    try {\n      if (table != null) {","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/TestBigQuery.java#L131-L167","documentation":"TestBigQuery.createTable verifies that the target table does not already exist before creating it, because the rule guarantees automatic cleanup after each test. A pre-existing table means cleanup from a previous run failed, so it throws IllegalStateException naming the table reference.","triggerScenarios":"Calling initializeBigQuery/createTable when the dataset already contains the requested table — leftover from a previous test whose cleanup failed, a crashed JVM, or a fixed table name colliding across tests.","commonSituations":"Reused table names across tests, orphaned tables after a killed CI worker, running tests against a persistent dataset where cleanup was skipped, or parallel test executions sharing a dataset.","solutions":["Delete the leftover table (or dataset) in BigQuery before re-running the test","Make table names unique per test (random suffix) to avoid collisions","Check why the previous test's cleanup failed (exception, killed process) and fix it","Run against an ephemeral dataset that the rule fully owns and deletes afterward","Avoid running such tests in parallel against the same project/dataset"],"exampleFix":"// before\nString table = \"my_table\"; // fixed name, collides across runs\n// after\nString table = \"my_table_\" + UUID.randomUUID().toString().replace(\"-\", \"\");","handlingStrategy":"validation","validationCode":"if (datasetService.getTable(tableReference) != null) {\n  datasetService.deleteTable(tableReference); // or fail fast before the rule does\n}","typeGuard":null,"tryCatchPattern":"try {\n  bigquery.createTable(tableReference, schema);\n} catch (IllegalStateException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"already exists\")) {\n    datasetService.deleteTable(tableReference); // clean orphan then retry once\n  } else throw e;\n}","preventionTips":["Use unique table names per test (UUID suffix)","Run tests against ephemeral datasets the rule owns","Fix root causes of failed prior-test cleanup (killed workers, swallowed exceptions)","Avoid parallel runs sharing one dataset"],"tags":["java","google-bigquery","junit","test-cleanup"],"backgroundTag":"file-already-exists","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}