This is an automated email from the ASF dual-hosted git repository.

chengpan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kyuubi.git


The following commit(s) were added to refs/heads/master by this push:
     new 01320c4c2 [KYUUBI #4962] Backport HIVE-19048: Initscript errors are 
ignored
01320c4c2 is described below

commit 01320c4c2f52ca7183e44e7803b8356df11baf00
Author: fwang12 <[email protected]>
AuthorDate: Thu Jun 15 10:09:01 2023 +0800

    [KYUUBI #4962] Backport HIVE-19048: Initscript errors are ignored
    
    ### _Why are the changes needed?_
    
    FYI:
    For hive-3.1.3, if failed to get connection, the return code of `beeline 
-f` is 0, the correct one should be non-zero.
    And it works well for hive-4.0.0-alpha-2.
    
![image](https://github.com/apache/kyuubi/assets/6757692/35ba2329-6f04-4aed-b40b-3c47534554a5)
    
    And the hive-beeline dependency version for kyuubi is 3.1.3.
    
    This pr backports HIVE-19048: Initscript errors are ignored.
    
    https://issues.apache.org/jira/browse/HIVE-19048
    
https://github.com/apache/hive/commit/006bf8a1a49aabf9930744c977fb0b9c91a5ef6c
    
    ### _How was this patch tested?_
    - [x] Add some test cases that check the changes thoroughly including 
negative and positive cases if possible
    
    - [ ] Add screenshots for manual tests if appropriate
    
    - [x] [Run 
test](https://kyuubi.readthedocs.io/en/master/develop_tools/testing.html#running-tests)
 locally before make a pull request
    
    Closes #4962 from turboFei/beeline_exit.
    
    Closes #4962
    
    85f497ad2 [fwang12] add ut
    21e22cf6f [fwang12] save
    49432c256 [fwang12] save
    69f90ae37 [fwang12] save
    b1afa9e26 [fwang12] backport
    
    Authored-by: fwang12 <[email protected]>
    Signed-off-by: Cheng Pan <[email protected]>
---
 .../org/apache/hive/beeline/KyuubiBeeLine.java     | 59 ++++++++++++++++++++++
 .../org/apache/hive/beeline/KyuubiCommands.java    | 11 +++-
 .../org/apache/hive/beeline/KyuubiBeeLineTest.java | 18 +++++++
 kyuubi-hive-beeline/src/test/resources/test.sql    | 17 +++++++
 4 files changed, 104 insertions(+), 1 deletion(-)

diff --git 
a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java 
b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java
index 073870a47..5671cde85 100644
--- 
a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java
+++ 
b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java
@@ -36,6 +36,11 @@ public class KyuubiBeeLine extends BeeLine {
   protected KyuubiCommands commands = new KyuubiCommands(this);
   private Driver defaultDriver;
 
+  // copied from org.apache.hive.beeline.BeeLine
+  private static final int ERRNO_OK = 0;
+  private static final int ERRNO_ARGS = 1;
+  private static final int ERRNO_OTHER = 2;
+
   public KyuubiBeeLine() {
     this(true);
   }
@@ -158,6 +163,11 @@ public class KyuubiBeeLine extends BeeLine {
       }
     }
 
+    // see HIVE-19048 : InitScript errors are ignored
+    if (exit) {
+      return 1;
+    }
+
     int code = 0;
     if (cl.getOptionValues('e') != null) {
       commands = Arrays.asList(cl.getOptionValues('e'));
@@ -189,4 +199,53 @@ public class KyuubiBeeLine extends BeeLine {
     }
     return code;
   }
+
+  // see HIVE-19048 : Initscript errors are ignored
+  @Override
+  int runInit() {
+    String[] initFiles = getOpts().getInitFiles();
+
+    // executionResult will be ERRNO_OK only if all initFiles execute 
successfully
+    int executionResult = ERRNO_OK;
+    boolean exitOnError = !getOpts().getForce();
+    DynFields.BoundField<Boolean> exitField = null;
+
+    if (initFiles != null && initFiles.length != 0) {
+      for (String initFile : initFiles) {
+        info("Running init script " + initFile);
+        try {
+          int currentResult;
+          try {
+            currentResult =
+                DynMethods.builder("executeFile")
+                    .hiddenImpl(BeeLine.class, String.class)
+                    .buildChecked(this)
+                    .invoke(initFile);
+            exitField = DynFields.builder().hiddenImpl(BeeLine.class, 
"exit").buildChecked(this);
+          } catch (Exception t) {
+            error(t.getMessage());
+            currentResult = ERRNO_OTHER;
+          }
+
+          if (currentResult != ERRNO_OK) {
+            executionResult = currentResult;
+
+            if (exitOnError) {
+              return executionResult;
+            }
+          }
+        } finally {
+          // exit beeline if there is initScript failure and --force is not set
+          boolean exit = exitOnError && executionResult != ERRNO_OK;
+          try {
+            exitField.set(exit);
+          } catch (Exception t) {
+            error(t.getMessage());
+            return ERRNO_OTHER;
+          }
+        }
+      }
+    }
+    return executionResult;
+  }
 }
diff --git 
a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java 
b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java
index fdd14d8cb..76fdc19cc 100644
--- 
a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java
+++ 
b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java
@@ -490,7 +490,16 @@ public class KyuubiCommands extends Commands {
       if (!beeLine.isBeeLine()) {
         beeLine.updateOptsForCli();
       }
-      beeLine.runInit();
+
+      // see HIVE-19048 : Initscript errors are ignored
+      int initScriptExecutionResult = beeLine.runInit();
+
+      // if execution of the init script(s) return anything other than 
ERRNO_OK from beeline
+      // exit beeline with error unless --force is set
+      if (initScriptExecutionResult != 0 && !beeLine.getOpts().getForce()) {
+        return beeLine.error("init script execution failed.");
+      }
+
       if (beeLine.getOpts().getInitFiles() != null) {
         beeLine.initializeConsoleReader(null);
       }
diff --git 
a/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java
 
b/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java
index b144c95c6..96b76373d 100644
--- 
a/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java
+++ 
b/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java
@@ -29,4 +29,22 @@ public class KyuubiBeeLineTest {
     int result = kyuubiBeeLine.initArgs(new String[0]);
     assertEquals(0, result);
   }
+
+  @Test
+  public void testKyuubiBeelineExitCodeWithoutConnection() {
+    KyuubiBeeLine kyuubiBeeLine = new KyuubiBeeLine();
+    String scriptFile = 
getClass().getClassLoader().getResource("test.sql").getFile();
+
+    String[] args1 = {"-u", "badUrl", "-e", "show tables"};
+    int result1 = kyuubiBeeLine.initArgs(args1);
+    assertEquals(1, result1);
+
+    String[] args2 = {"-u", "badUrl", "-f", scriptFile};
+    int result2 = kyuubiBeeLine.initArgs(args2);
+    assertEquals(1, result2);
+
+    String[] args3 = {"-u", "badUrl", "-i", scriptFile};
+    int result3 = kyuubiBeeLine.initArgs(args3);
+    assertEquals(1, result3);
+  }
 }
diff --git a/kyuubi-hive-beeline/src/test/resources/test.sql 
b/kyuubi-hive-beeline/src/test/resources/test.sql
new file mode 100644
index 000000000..c7c3ee2f9
--- /dev/null
+++ b/kyuubi-hive-beeline/src/test/resources/test.sql
@@ -0,0 +1,17 @@
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+show tables;

Reply via email to