{"record":{"id":"44ae8eda9e0918de","repo":"apache/beam","slug":"table-s-already-exists","errorCode":null,"errorMessage":"Table '%s' already exists","messagePattern":"Table '(.+?)' already exists","errorType":"validation","errorClass":"SqlUtil.newContextException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/parser/SqlCreateExternalTable.java","lineNumber":148,"sourceCode":"    }\n    if (location != null) {\n      writer.keyword(\"LOCATION\");\n      location.unparse(writer, 0, 0);\n    }\n    if (tblProperties != null) {\n      writer.keyword(\"TBLPROPERTIES\");\n      tblProperties.unparse(writer, 0, 0);\n    }\n  }\n\n  @Override\n  public void execute(CalcitePrepare.Context context) {\n    final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);\n    if (pair.left.plus().getTable(pair.right) != null) {\n      // Table exists.\n      if (!ifNotExists) {\n        // They did not specify IF NOT EXISTS, so give error.\n        throw SqlUtil.newContextException(\n            name.getParserPosition(), RESOURCE.tableExists(pair.right));\n      }\n      return;\n    }\n\n    Schema schema = pair.left.schema;\n\n    BeamCalciteSchema beamCalciteSchema;\n    if (schema instanceof CatalogManagerSchema) {\n      TableName pathOverride = TableName.create(name.toString());\n      CatalogManagerSchema catalogManagerSchema = (CatalogManagerSchema) schema;\n      catalogManagerSchema.maybeRegisterProvider(pathOverride, SqlDdlNodes.getString(type));\n\n      CatalogSchema catalogSchema = catalogManagerSchema.getCatalogSchema(pathOverride);\n      beamCalciteSchema = catalogSchema.getDatabaseSchema(pathOverride);\n    } else if (schema instanceof BeamCalciteSchema) {\n      beamCalciteSchema = (BeamCalciteSchema) schema;\n    } else {","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/parser/SqlCreateExternalTable.java#L130-L166","documentation":"Thrown when CREATE EXTERNAL TABLE targets a table name that already exists in the Calcite schema and the statement lacks IF NOT EXISTS. Beam's SQL DDL parser checks pair.left.plus().getTable(pair.right) before building the table and raises a parse-context error via Calcite's RESOURCE.tableExists. It is a user-facing duplicate-definition guard, not an internal failure.","triggerScenarios":"Executing `CREATE EXTERNAL TABLE <name> ...` where a table (or external table) with the same simple name is already registered in the target schema and IF NOT EXISTS was not specified.","commonSituations":"Re-running an idempotent DDL script or pipeline bootstrap without IF NOT EXISTS; two jobs/DDL sessions creating the same external table name; migrating scripts from engines that silently replace tables.","solutions":["Add IF NOT EXISTS to the CREATE EXTERNAL TABLE statement","Use a different table name","Explicitly DROP EXTERNAL TABLE the existing table first (or use CREATE OR REPLACE semantics if supported)","Inspect existing tables with schema metadata queries before running DDL"],"exampleFix":"// before\nCREATE EXTERNAL TABLE orders (...) TYPE 'csv' LOCATION '/tmp/orders';\n// after\nCREATE EXTERNAL TABLE IF NOT EXISTS orders (...) TYPE 'csv' LOCATION '/tmp/orders';","handlingStrategy":"validation","validationCode":"// Run before DDL\n// SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'orders';\nboolean exists = schema.getTableNames().stream().anyMatch(n -> n.equalsIgnoreCase(\"orders\"));\nif (exists) { /* skip create or drop first */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always use IF NOT EXISTS in idempotent bootstrap scripts","Drop-then-create explicitly when replacement is intended","Keep DDL scripts run-once or track applied migrations"],"tags":["sql","ddl","duplicate-table"],"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"}