|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.hive.ql.ddl.function.drop; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 23 | +import static org.mockito.ArgumentMatchers.any; |
| 24 | +import static org.mockito.ArgumentMatchers.anyString; |
| 25 | +import static org.mockito.ArgumentMatchers.eq; |
| 26 | +import static org.mockito.Mockito.mock; |
| 27 | +import static org.mockito.Mockito.mockStatic; |
| 28 | +import static org.mockito.Mockito.when; |
| 29 | + |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +import org.apache.hadoop.hive.conf.HiveConf; |
| 34 | +import org.apache.hadoop.hive.conf.HiveConf.ConfVars; |
| 35 | +import org.apache.hadoop.hive.conf.HiveConfForTest; |
| 36 | +import org.apache.hadoop.hive.metastore.api.Database; |
| 37 | +import org.apache.hadoop.hive.metastore.api.Function; |
| 38 | +import org.apache.hadoop.hive.metastore.api.FunctionType; |
| 39 | +import org.apache.hadoop.hive.metastore.api.PrincipalType; |
| 40 | +import org.apache.hadoop.hive.ql.Context; |
| 41 | +import org.apache.hadoop.hive.ql.QueryState; |
| 42 | +import org.apache.hadoop.hive.ql.exec.FunctionRegistry; |
| 43 | +import org.apache.hadoop.hive.ql.metadata.Hive; |
| 44 | +import org.apache.hadoop.hive.ql.plan.HiveOperation; |
| 45 | +import org.apache.hadoop.hive.ql.parse.ASTNode; |
| 46 | +import org.apache.hadoop.hive.ql.parse.ParseUtils; |
| 47 | +import org.apache.hadoop.hive.ql.parse.SemanticException; |
| 48 | +import org.apache.hadoop.hive.ql.session.SessionState; |
| 49 | +import org.junit.jupiter.api.AfterEach; |
| 50 | +import org.junit.jupiter.api.BeforeEach; |
| 51 | +import org.junit.jupiter.api.Test; |
| 52 | +import org.mockito.MockedStatic; |
| 53 | + |
| 54 | +/** |
| 55 | + * Tests for DropFunctionAnalyzer focusing on the case where the function's JAR resource is |
| 56 | + * unavailable (e.g. deleted from HDFS after the function was registered). |
| 57 | + */ |
| 58 | +class TestDropFunctionAnalyzer { |
| 59 | + |
| 60 | + private HiveConf conf; |
| 61 | + |
| 62 | + @BeforeEach |
| 63 | + void setUp() { |
| 64 | + conf = new HiveConfForTest(getClass()); |
| 65 | + SessionState.start(conf); |
| 66 | + } |
| 67 | + |
| 68 | + @AfterEach |
| 69 | + void tearDown() throws Exception { |
| 70 | + SessionState ss = SessionState.get(); |
| 71 | + if (ss != null) { |
| 72 | + ss.close(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private DropFunctionAnalyzer createAnalyzer(Hive mockDb) throws SemanticException { |
| 77 | + QueryState queryState = QueryState.getNewQueryState(conf, null); |
| 78 | + queryState.setCommandType(HiveOperation.DROPFUNCTION); |
| 79 | + return new DropFunctionAnalyzer(queryState, mockDb); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * When FunctionRegistry.getFunctionInfo returns null (JAR unavailable) but the function still |
| 84 | + * exists in the metastore, DROP FUNCTION must proceed and emit a drop task so the orphaned |
| 85 | + * definition is actually removed. |
| 86 | + */ |
| 87 | + @Test |
| 88 | + void testDropSucceedsWhenJarUnavailableButFunctionInMetastore() throws Exception { |
| 89 | + Hive mockDb = mock(Hive.class); |
| 90 | + Database msDatabase = new Database("default", "", "/tmp", Collections.emptyMap()); |
| 91 | + Function msFunction = new Function("dummy", "default", "com.example.DummyUDF", |
| 92 | + "user", PrincipalType.USER, 0, FunctionType.JAVA, Collections.emptyList()); |
| 93 | + |
| 94 | + when(mockDb.getFunctions("default", "dummy")).thenReturn(List.of("dummy")); |
| 95 | + when(mockDb.getFunction("default", "dummy")).thenReturn(msFunction); |
| 96 | + when(mockDb.getDatabase("default")).thenReturn(msDatabase); |
| 97 | + when(mockDb.getDatabase(any(), eq("default"))).thenReturn(msDatabase); |
| 98 | + |
| 99 | + try (MockedStatic<FunctionRegistry> registry = mockStatic(FunctionRegistry.class)) { |
| 100 | + registry.when(() -> FunctionRegistry.getFunctionInfo("dummy")).thenReturn(null); |
| 101 | + |
| 102 | + DropFunctionAnalyzer analyzer = createAnalyzer(mockDb); |
| 103 | + analyzer.analyzeInternal(parse("drop function dummy")); |
| 104 | + |
| 105 | + assertEquals(1, analyzer.getRootTasks().size(), "Expected one DROP task even when JAR is unavailable"); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * When the function does not exist in either the session registry or the metastore, DROP FUNCTION |
| 111 | + * (without IF EXISTS) must surface the error to the client. |
| 112 | + */ |
| 113 | + @Test |
| 114 | + void testDropThrowsWhenFunctionNotInMetastore() throws Exception { |
| 115 | + conf.setBoolVar(ConfVars.DROP_IGNORES_NON_EXISTENT, false); |
| 116 | + Hive mockDb = mock(Hive.class); |
| 117 | + when(mockDb.getFunctions(anyString(), anyString())).thenReturn(Collections.emptyList()); |
| 118 | + |
| 119 | + try (MockedStatic<FunctionRegistry> registry = mockStatic(FunctionRegistry.class)) { |
| 120 | + registry.when(() -> FunctionRegistry.getFunctionInfo("dummy")).thenReturn(null); |
| 121 | + |
| 122 | + DropFunctionAnalyzer analyzer = createAnalyzer(mockDb); |
| 123 | + assertThrows(SemanticException.class, () -> analyzer.analyzeInternal(parse("drop function dummy")), |
| 124 | + "Expected SemanticException when function not in registry or metastore"); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * DROP FUNCTION IF EXISTS must silently succeed (no task, no exception) when the function is |
| 130 | + * absent from both the session registry and the metastore. |
| 131 | + */ |
| 132 | + @Test |
| 133 | + void testDropIfExistsSilentWhenFunctionAbsent() throws Exception { |
| 134 | + Hive mockDb = mock(Hive.class); |
| 135 | + when(mockDb.getFunctions(anyString(), anyString())).thenReturn(Collections.emptyList()); |
| 136 | + |
| 137 | + try (MockedStatic<FunctionRegistry> registry = mockStatic(FunctionRegistry.class)) { |
| 138 | + registry.when(() -> FunctionRegistry.getFunctionInfo("dummy")).thenReturn(null); |
| 139 | + |
| 140 | + DropFunctionAnalyzer analyzer = createAnalyzer(mockDb); |
| 141 | + analyzer.analyzeInternal(parse("drop function if exists dummy")); |
| 142 | + |
| 143 | + assertEquals(0, analyzer.getRootTasks().size(), "Expected no tasks when function is absent and IF EXISTS is set"); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private ASTNode parse(String sql) throws Exception { |
| 148 | + return ParseUtils.parse(sql, new Context(conf)); |
| 149 | + } |
| 150 | +} |
0 commit comments