{"record":{"id":"465b2adf152a8aee","repo":"prestodb/presto","slug":"table-already-exists-465b2a","errorCode":"TABLE_ALREADY_EXISTS","errorMessage":"Destination table '%s' already exists","messagePattern":"Destination table '(.+?)' already exists","errorType":"error_code","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/StatementAnalyzer.java","lineNumber":819,"sourceCode":"            analysis.setUpdateInfo(node.getUpdateInfo());\n\n            // turn this into a query that has a new table writer node on top.\n            QualifiedObjectName targetTable = createQualifiedObjectName(session, node, node.getName(), metadata);\n            analysis.setCreateTableDestination(targetTable);\n\n            if (metadataResolver.tableExists(targetTable)) {\n                if (node.isNotExists()) {\n                    analysis.setCreateTableAsSelectNoOp(true);\n                    warningCollector.add(new PrestoWarning(\n                            StandardWarningCode.SEMANTIC_WARNING,\n                            format(\"Table '%s' already exists, skipping table creation\", targetTable)));\n                    // Analyze the inner query to populate view definitions for access control checks\n                    if (isAlwaysAnalyzeCreateTableQueryEnabled(session)) {\n                        process(node.getQuery(), scope);\n                    }\n                    return createAndAssignScope(node, scope, Field.newUnqualified(node.getLocation(), \"rows\", BIGINT));\n                }\n                throw new SemanticException(TABLE_ALREADY_EXISTS, node, \"Destination table '%s' already exists\", targetTable);\n            }\n\n            validateProperties(node.getProperties(), scope);\n            analysis.setCreateTableProperties(mapFromProperties(node.getProperties()));\n\n            node.getColumnAliases().ifPresent(analysis::setCreateTableColumnAliases);\n            analysis.setCreateTableComment(node.getComment());\n\n            analysis.addAccessControlCheckForTable(TABLE_CREATE, new AccessControlInfoForTable(accessControl, session.getIdentity(), session.getTransactionId(), session.getAccessControlContext(), targetTable));\n\n            analysis.setCreateTableAsSelectWithData(node.isWithData());\n\n            // analyze the query that creates the table\n            Scope queryScope = process(node.getQuery(), scope);\n\n            ImmutableList.Builder<OutputColumnMetadata> outputColumns = ImmutableList.builder();\n\n            if (node.getColumnAliases().isPresent()) {","sourceCodeStart":801,"sourceCodeEnd":837,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/StatementAnalyzer.java#L801-L837","documentation":"CREATE TABLE AS SELECT (CTAS) refuses to overwrite an existing table unless the statement uses CREATE TABLE IF NOT EXISTS (or the session's existing-table behavior). When metadataResolver reports the destination table already exists and no NOT EXISTS clause short-circuits, visitCreateTableAsSelect throws TABLE_ALREADY_EXISTS.","triggerScenarios":"CREATE TABLE AS SELECT against a destination qualified name that already exists in the connector, without IF NOT EXISTS and without ignore-existing session behavior.","commonSituations":"Re-running an idempotent-by-intent pipeline script; a previous partially successful run left the table behind; name collision between environments pointing at the same catalog.","solutions":["Use CREATE TABLE IF NOT EXISTS to skip when the destination exists","DROP TABLE the destination first if overwrite is intended","DROP the old table and rerun the CTAS to recreate it"],"exampleFix":"-- before\nCREATE TABLE reports.daily AS SELECT * FROM events;\n-- after\nCREATE TABLE IF NOT EXISTS reports.daily AS SELECT * FROM events;","handlingStrategy":"try-catch","validationCode":"// before CTAS:\n// boolean exists = metadataResolver.tableExists(qualifiedTableName);","typeGuard":null,"tryCatchPattern":"catch (SemanticException e) { if (e.getCode() == TABLE_ALREADY_EXISTS) { /* drop or use IF NOT EXISTS */ } }","preventionTips":["Prefer CREATE TABLE IF NOT EXISTS for idempotent pipelines","Add drop-and-recreate steps to job setup when overwrite is intended","Check destination existence in the orchestrator before submitting CTAS"],"tags":["presto","ctas","create-table","already-exists","ddl"],"backgroundTag":"table-already-exists","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}