Skip to content

Commit f3496f3

Browse files
authored
HIVE-29497: [HPLSQL] quit is not exiting from execution flow in HPLSQL. (#6405)
1 parent 25d4a85 commit f3496f3

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

hplsql/src/main/java/org/apache/hive/hplsql/Exec.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,11 +1037,13 @@ public void printExceptions() {
10371037
} else if (sig.type == Signal.Type.VALIDATION) {
10381038
error(((HplValidationException)sig.exception).getCtx(), sig.exception.getMessage());
10391039
} else if (sig.type == Signal.Type.SQLEXCEPTION) {
1040-
console.printError("Unhandled exception in HPL/SQL");
1040+
console.printError("Unhandled exception in HPL/SQL: " + sig.value);
10411041
} else if (sig.type == Signal.Type.UNSUPPORTED_OPERATION) {
10421042
console.printError(sig.value == null ? "Unsupported operation" : sig.value);
10431043
} else if (sig.exception != null) {
10441044
console.printError("HPL/SQL error: " + ExceptionUtils.getStackTrace(sig.exception));
1045+
} else if (sig.type == Signal.Type.LEAVE_PROGRAM) {
1046+
console.printLine("Leaving Program: " + sig.value);
10451047
} else if (sig.value != null) {
10461048
console.printError(sig.value);
10471049
} else {
@@ -1096,6 +1098,14 @@ public Integer visitStmt(HplsqlParser.StmtContext ctx) {
10961098
return 0;
10971099
}
10981100
}
1101+
1102+
if (!exec.signals.empty()) {
1103+
Signal sig = exec.signals.peek();
1104+
if (sig.type == Signal.Type.LEAVE_PROGRAM) {
1105+
return 0;
1106+
}
1107+
}
1108+
10991109
Var prev = stackPop();
11001110
if (prev != null && prev.value != null) {
11011111
console.printLine(prev.toString());
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Ln:1 IF
22
Ln:1 IF TRUE executed
33
Ln:1 QUIT
4-
0
4+
Leaving Program: 0

itests/hive-unit/src/test/java/org/apache/hive/beeline/TestHplSqlViaBeeLine.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,24 @@ public void testPrintMessageAfterExecuteSetHiveConfig() throws Throwable {
14011401
testScriptFile(scriptText, args(), "Should print this message!...", OutStream.ERR);
14021402
}
14031403

1404+
@Test
1405+
public void testQuit() throws Throwable {
1406+
String scriptText =
1407+
"SET hplsql.onerror='seterror';\n" +
1408+
"INSERT INTO abc VALUES('Tbl1 Not Exists');\n" +
1409+
"PRINT 'ERRORCODE: ' || ERRORCODE;\n" +
1410+
"IF ERRORCODE <> 0 THEN\n" +
1411+
" PRINT 'Error detected. Exiting...';\n" +
1412+
" .QUIT ERRORCODE;\n" +
1413+
"END IF;\n" +
1414+
"INSERT INTO def VALUES('Tbl2 Not Exists');\n" +
1415+
"PRINT 'Should not print this message'";
1416+
// Inverted match, output should not have 'Table not found def' error
1417+
testScriptFile(scriptText, args(),
1418+
"^(.(?!(Caused by: org.apache.hadoop.hive.ql.metadata.InvalidTableException: Table not found def)))*$",
1419+
OutStream.ERR);
1420+
}
1421+
14041422
private static List<String> args() {
14051423
return Arrays.asList("-d", BeeLine.BEELINE_DEFAULT_JDBC_DRIVER,
14061424
"-u", miniHS2.getBaseJdbcURL() + ";mode=hplsql", "-n", USER_NAME);

0 commit comments

Comments
 (0)