diff --git a/hplsql/src/main/java/org/apache/hive/hplsql/Exec.java b/hplsql/src/main/java/org/apache/hive/hplsql/Exec.java index a8d5187b773d..de233e225f6c 100644 --- a/hplsql/src/main/java/org/apache/hive/hplsql/Exec.java +++ b/hplsql/src/main/java/org/apache/hive/hplsql/Exec.java @@ -1037,11 +1037,13 @@ public void printExceptions() { } else if (sig.type == Signal.Type.VALIDATION) { error(((HplValidationException)sig.exception).getCtx(), sig.exception.getMessage()); } else if (sig.type == Signal.Type.SQLEXCEPTION) { - console.printError("Unhandled exception in HPL/SQL"); + console.printError("Unhandled exception in HPL/SQL: " + sig.value); } else if (sig.type == Signal.Type.UNSUPPORTED_OPERATION) { console.printError(sig.value == null ? "Unsupported operation" : sig.value); } else if (sig.exception != null) { console.printError("HPL/SQL error: " + ExceptionUtils.getStackTrace(sig.exception)); + } else if (sig.type == Signal.Type.LEAVE_PROGRAM) { + console.printLine("Leaving Program: " + sig.value); } else if (sig.value != null) { console.printError(sig.value); } else { @@ -1096,6 +1098,14 @@ public Integer visitStmt(HplsqlParser.StmtContext ctx) { return 0; } } + + if (!exec.signals.empty()) { + Signal sig = exec.signals.peek(); + if (sig.type == Signal.Type.LEAVE_PROGRAM) { + return 0; + } + } + Var prev = stackPop(); if (prev != null && prev.value != null) { console.printLine(prev.toString()); diff --git a/hplsql/src/test/results/local/if3_bteq.out.txt b/hplsql/src/test/results/local/if3_bteq.out.txt index cf2e0899a14e..e7adb8f19d4e 100644 --- a/hplsql/src/test/results/local/if3_bteq.out.txt +++ b/hplsql/src/test/results/local/if3_bteq.out.txt @@ -1,4 +1,4 @@ Ln:1 IF Ln:1 IF TRUE executed Ln:1 QUIT -0 +Leaving Program: 0 diff --git a/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestHplSqlViaBeeLine.java b/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestHplSqlViaBeeLine.java index 2aa433990e84..e186790d487c 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestHplSqlViaBeeLine.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestHplSqlViaBeeLine.java @@ -1401,6 +1401,24 @@ public void testPrintMessageAfterExecuteSetHiveConfig() throws Throwable { testScriptFile(scriptText, args(), "Should print this message!...", OutStream.ERR); } + @Test + public void testQuit() throws Throwable { + String scriptText = + "SET hplsql.onerror='seterror';\n" + + "INSERT INTO abc VALUES('Tbl1 Not Exists');\n" + + "PRINT 'ERRORCODE: ' || ERRORCODE;\n" + + "IF ERRORCODE <> 0 THEN\n" + + " PRINT 'Error detected. Exiting...';\n" + + " .QUIT ERRORCODE;\n" + + "END IF;\n" + + "INSERT INTO def VALUES('Tbl2 Not Exists');\n" + + "PRINT 'Should not print this message'"; + // Inverted match, output should not have 'Table not found def' error + testScriptFile(scriptText, args(), + "^(.(?!(Caused by: org.apache.hadoop.hive.ql.metadata.InvalidTableException: Table not found def)))*$", + OutStream.ERR); + } + private static List args() { return Arrays.asList("-d", BeeLine.BEELINE_DEFAULT_JDBC_DRIVER, "-u", miniHS2.getBaseJdbcURL() + ";mode=hplsql", "-n", USER_NAME);