{"record":{"id":"b9b3095750b37ca5","repo":"prestodb/presto","slug":"view-already-exists","errorCode":null,"errorMessage":"View already exists","messagePattern":"View already exists","errorType":"exception","errorClass":"ViewAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"presto-hive/src/main/java/com/facebook/presto/hive/HiveMetadata.java","lineNumber":2476,"sourceCode":"\n    @Override\n    public void createView(ConnectorSession session, ConnectorTableMetadata viewMetadata, String viewData, boolean replace)\n    {\n        MetastoreContext metastoreContext = getMetastoreContext(session);\n        SchemaTableName viewName = viewMetadata.getTable();\n        Table table = createTableObjectForViewCreation(\n                session,\n                viewMetadata,\n                createViewProperties(session, prestoVersion),\n                typeTranslator,\n                metastoreContext,\n                encodeViewData(viewData));\n        PrincipalPrivileges principalPrivileges = buildInitialPrivilegeSet(session.getUser());\n\n        Optional<Table> existing = metastore.getTable(metastoreContext, viewName.getSchemaName(), viewName.getTableName());\n        if (existing.isPresent()) {\n            if (!replace || !MetastoreUtil.isPrestoView(existing.get())) {\n                throw new ViewAlreadyExistsException(viewName);\n            }\n\n            metastore.replaceView(metastoreContext, viewName.getSchemaName(), viewName.getTableName(), table, principalPrivileges);\n            return;\n        }\n\n        try {\n            metastore.createTable(session, table, principalPrivileges, Optional.empty(), false, new PartitionStatistics(createEmptyStatistics(), ImmutableMap.of()), emptyList());\n        }\n        catch (TableAlreadyExistsException e) {\n            throw new ViewAlreadyExistsException(e.getTableName());\n        }\n    }\n\n    @Override\n    public void dropView(ConnectorSession session, SchemaTableName viewName)\n    {\n        ConnectorViewDefinition view = getViews(session, viewName.toSchemaTablePrefix()).get(viewName);","sourceCodeStart":2458,"sourceCodeEnd":2494,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-hive/src/main/java/com/facebook/presto/hive/HiveMetadata.java#L2458-L2494","documentation":"Mapped from TableAlreadyExistsException when CREATE VIEW (or CREATE OR REPLACE VIEW) finds an object already stored under the view name. If the existing table is not a Presto view (e.g. a real table), or the statement was plain CREATE VIEW without OR REPLACE, creation is rejected. Presto translates the metastore conflict into ViewAlreadyExistsException.","triggerScenarios":"CREATE VIEW on an existing name; CREATE OR REPLACE VIEW where the existing object is a native Hive table (not isPrestoView); metastore already contains a table/view with that schema.table name.","commonSituations":"Re-running a deployment script that creates views idempotently without IF NOT EXISTS; a stale Hive table occupying the intended view name; switching a name from table to view without dropping it first.","solutions":["Use CREATE OR REPLACE VIEW — works only if the existing object is a Presto view","If a real Hive table occupies the name, DROP TABLE <name> first, then create the view","Use CREATE VIEW IF NOT EXISTS to skip when it already exists","Pick a different view name"],"exampleFix":"-- before\nCREATE VIEW analytics.daily_revenue AS SELECT ...;\n-- after\nCREATE OR REPLACE VIEW analytics.daily_revenue AS SELECT ...;\n-- or defensively:\nCREATE VIEW IF NOT EXISTS analytics.daily_revenue AS SELECT ...;","handlingStrategy":"validation","validationCode":"-- guard in deploy tooling\nSELECT true FROM system.metadata.tables\nWHERE schema_name = 'analytics' AND table_name = 'daily_revenue';\n-- if true: use CREATE OR REPLACE VIEW or drop first","typeGuard":null,"tryCatchPattern":"try {\n    createView(ddl);\n} catch (ViewAlreadyExistsException e) {\n    // deliberate idempotency: verify then replace\n    run(\"CREATE OR REPLACE VIEW \" + viewName + \" AS \" + sql);\n}","preventionTips":["Make DDL idempotent with IF NOT EXISTS / OR REPLACE","Never give views the same name as existing tables","Clean up legacy tables before migrating names to views","Version-control view DDL and detect drift before deploy"],"tags":["hive","view","already-exists","ddl"],"backgroundTag":"object-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"}