diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java index a2da15f5b6..2e151ec91a 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcWithMiniHS2.java @@ -1312,6 +1312,19 @@ public void testPermFunc() throws Exception { assertEquals(3, res.getInt(1)); assertFalse("no more results", res.next()); + //try creating same function again which should fail with AlreadyExistsException + String createSameFunctionAgain = + "CREATE FUNCTION example_add AS '" + testUdfClassName + "' USING JAR '" + jarFilePath + "'"; + try { + stmt.execute(createSameFunctionAgain); + }catch (Exception e){ + assertTrue("recreating same function failed with AlreadyExistsException ", e.getMessage().contains("AlreadyExistsException")); + } + + // Call describe to see if function still available in registry + res = stmt.executeQuery("DESCRIBE FUNCTION " + testDbName + ".example_add"); + checkForNotExist(res); + // A new connection should be able to call describe/use function without issue Connection conn2 = getConnection(testDbName); Statement stmt2 = conn2.createStatement(); diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionTask.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionTask.java index df3105b49c..2061cf4577 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionTask.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionTask.java @@ -208,8 +208,9 @@ private int createPermanentFunction(Hive db, CreateFunctionDesc createFunctionDe db.createFunction(func); } catch (Exception e) { // Addition to metastore failed, remove the function from the registry except if already exists. - if(!(e.getCause() instanceof AlreadyExistsException)) + if(!(e.getCause() instanceof AlreadyExistsException)) { FunctionRegistry.unregisterPermanentFunction(registeredName); + } setException(e); LOG.error("Failed to add function " + createFunctionDesc.getFunctionName() + " to the metastore.", e);