Skip to content

Commit 5935c44

Browse files
authored
[ZEPPELIN-6225] Refactor HiveStatement unwrapping for improved readability and robustness
### What is this PR for? To address this issue, nested type casting logic has been extracted into a dedicated static method (`unwrapHiveStatement`). Since the immediate wrapper (like `DelegatingStatement`) in the Statement chain doesn't reliably support standard `isWrapperFor` and `unwrap(Class<T> iface)` calls (often returning `false` or throwing `SQLException` as observed in its implementation), a `while` loop is used to iteratively unwrap the underlying `DelegatingStatement` layers. To prevent potential infinite loops or excessive unwrapping attempts, a **`MAX_STATEMENT_UNWRAP_DEPTH`** constant has been introduced, limiting the maximum unwrapping depth. Error handling and logging have also been improved to provide clearer insights into the unwrapping process. In `HiveStatement.class` : ``` public boolean isWrapperFor(Class<?> iface) throws SQLException { return false; } public <T> T unwrap(Class<T> iface) throws SQLException { throw new SQLException("Cannot unwrap to " + iface); } ``` ### What type of PR is it? Refactoring ### Todos * [x] - Replace nested casts and Improve readability ### What is the Jira issue? * Open an issue on [Jira](https://issues.apache.org/jira/secure/RapidBoard.jspa?rapidView=632&projectKey=ZEPPELIN&view=detail&selectedIssue=ZEPPELIN-6225) ### How should this be tested? * Strongly recommended: add automated unit tests for any new or changed behavior * Outline any manual steps to test the PR here. ### Questions: * Does the license files need to update? - No * Is there breaking changes for older versions? - No * Does this needs documentation? - No Closes #4968 from hyunw9/fix/simplify-hive-statement-unwrapping. Signed-off-by: Cheng Pan <chengpan@apache.org>
1 parent 1fe6b04 commit 5935c44

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

jdbc/src/main/java/org/apache/zeppelin/jdbc/hive/HiveUtils.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class HiveUtils {
4141

4242
private static final Logger LOGGER = LoggerFactory.getLogger(HiveUtils.class);
4343
private static final int DEFAULT_QUERY_PROGRESS_INTERVAL = 1000;
44+
private static final int MAX_STATEMENT_UNWRAP_DEPTH = 10;
4445

4546
private static final Pattern JOBURL_PATTERN =
4647
Pattern.compile(".*Tracking URL = (\\S*).*", Pattern.DOTALL);
@@ -59,8 +60,7 @@ public static void startHiveMonitorThread(Statement stmt,
5960
InterpreterContext context,
6061
boolean displayLog,
6162
JDBCInterpreter jdbcInterpreter) {
62-
HiveStatement hiveStmt = (HiveStatement)
63-
((DelegatingStatement) ((DelegatingStatement) stmt).getDelegate()).getDelegate();
63+
HiveStatement hiveStmt = unwrapHiveStatement(stmt);
6464
String hiveVersion = HiveVersionInfo.getVersion();
6565
ProgressBar progressBarTemp = null;
6666
if (isProgressBarSupported(hiveVersion)) {
@@ -120,6 +120,27 @@ public static void startHiveMonitorThread(Statement stmt,
120120
}
121121
}
122122

123+
private static HiveStatement unwrapHiveStatement(Statement stmt) {
124+
125+
Statement current = stmt;
126+
int unwrapAttempts = 0;
127+
128+
while (current instanceof DelegatingStatement && unwrapAttempts < MAX_STATEMENT_UNWRAP_DEPTH) {
129+
current = ((DelegatingStatement) current).getDelegate();
130+
unwrapAttempts++;
131+
if (current instanceof HiveStatement) {
132+
return (HiveStatement) current;
133+
}
134+
}
135+
136+
LOGGER.warn("Could not find HiveStatement within the Statement object after {} "
137+
+ "unwrapping attempts. Final type inspected: {} "
138+
+ "Hive query monitoring may not be active",
139+
MAX_STATEMENT_UNWRAP_DEPTH, current != null ?
140+
current.getClass().getName() : "null");
141+
return null;
142+
}
143+
123144
/**
124145
* Extract hive job url in the following order:
125146
* 1. Extract job url from hive logs when hive use mr engine

0 commit comments

Comments
 (0)