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

chengpan pushed a commit to branch branch-0.12
in repository https://gitbox.apache.org/repos/asf/zeppelin.git


The following commit(s) were added to refs/heads/branch-0.12 by this push:
     new 72525d23da [ZEPPELIN-6225] Refactor HiveStatement unwrapping for 
improved readability and robustness
72525d23da is described below

commit 72525d23daf5b11cfd8db8b30ef0a9af3c5fd4c7
Author: 강현욱 <43662405+hyu...@users.noreply.github.com>
AuthorDate: Tue Jul 15 21:42:35 2025 +0900

    [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 <cheng...@apache.org>
    (cherry picked from commit 5935c4493ce75208639c59312d1dcd68c08da9c0)
    Signed-off-by: Cheng Pan <cheng...@apache.org>
---
 .../org/apache/zeppelin/jdbc/hive/HiveUtils.java   | 25 ++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/jdbc/src/main/java/org/apache/zeppelin/jdbc/hive/HiveUtils.java 
b/jdbc/src/main/java/org/apache/zeppelin/jdbc/hive/HiveUtils.java
index ddf35f3a4e..f2c683faef 100644
--- a/jdbc/src/main/java/org/apache/zeppelin/jdbc/hive/HiveUtils.java
+++ b/jdbc/src/main/java/org/apache/zeppelin/jdbc/hive/HiveUtils.java
@@ -41,6 +41,7 @@ public class HiveUtils {
 
   private static final Logger LOGGER = 
LoggerFactory.getLogger(HiveUtils.class);
   private static final int DEFAULT_QUERY_PROGRESS_INTERVAL = 1000;
+  private static final int MAX_STATEMENT_UNWRAP_DEPTH = 10;
 
   private static final Pattern JOBURL_PATTERN =
           Pattern.compile(".*Tracking URL = (\\S*).*", Pattern.DOTALL);
@@ -59,8 +60,7 @@ public class HiveUtils {
                                             InterpreterContext context,
                                             boolean displayLog,
                                             JDBCInterpreter jdbcInterpreter) {
-    HiveStatement hiveStmt = (HiveStatement)
-            ((DelegatingStatement) ((DelegatingStatement) 
stmt).getDelegate()).getDelegate();
+    HiveStatement hiveStmt = unwrapHiveStatement(stmt);
     String hiveVersion = HiveVersionInfo.getVersion();
     ProgressBar progressBarTemp = null;
     if (isProgressBarSupported(hiveVersion)) {
@@ -120,6 +120,27 @@ public class HiveUtils {
     }
   }
 
+  private static HiveStatement unwrapHiveStatement(Statement stmt) {
+
+    Statement current = stmt;
+    int unwrapAttempts = 0;
+
+    while (current instanceof DelegatingStatement && unwrapAttempts < 
MAX_STATEMENT_UNWRAP_DEPTH) {
+      current = ((DelegatingStatement) current).getDelegate();
+      unwrapAttempts++;
+      if (current instanceof HiveStatement) {
+        return (HiveStatement) current;
+      }
+    }
+
+    LOGGER.warn("Could not find HiveStatement within the Statement object 
after {} "
+                + "unwrapping attempts. Final type inspected: {} "
+                + "Hive query monitoring may not be active",
+                MAX_STATEMENT_UNWRAP_DEPTH, current != null ?
+                                            current.getClass().getName() : 
"null");
+    return null;
+  }
+
   /**
    * Extract hive job url in the following order:
    * 1. Extract job url from hive logs when hive use mr engine

Reply via email to