{"record":{"id":"3f0d0e6d5fa0b8ae","repo":"prestodb/presto","slug":"generic-user-error-3f0d0e","errorCode":"GENERIC_USER_ERROR","errorMessage":"Function '%s' already exists","messagePattern":"Function '(.+?)' already exists","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-function-namespace-managers-common/src/main/java/com/facebook/presto/functionNamespace/testing/InMemoryFunctionNamespaceManager.java","lineNumber":72,"sourceCode":"\n    public InMemoryFunctionNamespaceManager(String catalogName, SqlFunctionExecutors sqlFunctionExecutors, SqlInvokedFunctionNamespaceManagerConfig config)\n    {\n        super(catalogName, sqlFunctionExecutors, config);\n    }\n\n    @Override\n    public void setBlockEncodingSerde(BlockEncodingSerde blockEncodingSerde)\n    {\n        // Do not need to do anything here since InMemoryFunctionNamespaceManager cannot execute functions\n    }\n\n    @Override\n    public synchronized void createFunction(SqlInvokedFunction function, boolean replace)\n    {\n        checkFunctionLanguageSupported(function);\n        SqlFunctionId functionId = function.getFunctionId();\n        if (!replace && latestFunctions.containsKey(function.getFunctionId())) {\n            throw new PrestoException(GENERIC_USER_ERROR, format(\"Function '%s' already exists\", functionId.getId()));\n        }\n\n        SqlInvokedFunction replacedFunction = latestFunctions.get(functionId);\n        long version = 1;\n        if (replacedFunction != null) {\n            version = parseLong(replacedFunction.getRequiredVersion()) + 1;\n        }\n        latestFunctions.put(functionId, function.withVersion(String.valueOf(version)));\n    }\n\n    @Override\n    public void alterFunction(QualifiedObjectName functionName, Optional<List<TypeSignature>> parameterTypes, AlterRoutineCharacteristics alterRoutineCharacteristics)\n    {\n        throw new PrestoException(NOT_SUPPORTED, \"Alter Function is not supported in InMemoryFunctionNamespaceManager\");\n    }\n\n    @Override\n    public synchronized void dropFunction(QualifiedObjectName functionName, Optional<List<TypeSignature>> parameterTypes, boolean exists)","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-function-namespace-managers-common/src/main/java/com/facebook/presto/functionNamespace/testing/InMemoryFunctionNamespaceManager.java#L54-L90","documentation":"InMemoryFunctionNamespaceManager.createFunction throws GENERIC_USER_ERROR when a function with the same SqlFunctionId already exists and replace=false. The in-memory manager keys functions by id; creating a duplicate without replace would silently shadow the existing definition, so it is rejected.","triggerScenarios":"Calling createFunction(function, replace=false) when latestFunctions already contains function.getFunctionId(); running the same CREATE FUNCTION twice without OR REPLACE; test setup re-registering a helper function.","commonSituations":"Repeated test setup against the shared in-memory manager; forgetting the 'OR REPLACE' clause; idempotency issues where a script reruns CREATE FUNCTION statements.","solutions":["Use CREATE FUNCTION ... OR REPLACE (or pass replace=true to createFunction).","Drop or clear the existing function before re-creating it (dropFunction is unsupported here, so use a fresh manager instance in tests).","Guard creation with a existence check via listFunctions/getFunction before calling createFunction."],"exampleFix":"// before\nCREATE FUNCTION test_ns.double_it(x BIGINT) RETURNS BIGINT RETURN x * 2;\n-- second run fails\n// after\nCREATE OR REPLACE FUNCTION test_ns.double_it(x BIGINT) RETURNS BIGINT RETURN x * 2;","handlingStrategy":"validation","validationCode":"boolean exists = manager.listFunctions(functionName).stream()\n        .anyMatch(f -> f.getFunctionId().equals(function.getFunctionId()));\n// only call createFunction(..., false) when !exists, otherwise use replace=true","typeGuard":null,"tryCatchPattern":"try {\n    manager.createFunction(function, false);\n} catch (PrestoException e) {\n    if (e.getMessage().contains(\"already exists\")) {\n        manager.createFunction(function, true); // OR REPLACE\n    }\n}","preventionTips":["Use CREATE OR REPLACE for idempotent function setup scripts.","Check existing functions before creating in shared test fixtures.","Isolate each test with its own InMemoryFunctionNamespaceManager instance.","Make setup methods idempotent rather than additive."],"tags":["function-namespace","duplicate","create-function","user-error"],"backgroundTag":"function-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"}