You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/10/12 09:20:44 UTC

(shardingsphere) branch master updated: Fix Hive parser SPI loading without Hive JDBC driver (#40162)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 25bd0cb6e2a Fix Hive parser SPI loading without Hive JDBC driver (#40162)
25bd0cb6e2a is described below

commit 25bd0cb6e2a147636e5548b38f51ba5fb61ab73c
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Oct 12 17:20:37 2026 +0800

    Fix Hive parser SPI loading without Hive JDBC driver (#40162)
---
 .../jdbcurl/HiveConnectionPropertiesParser.java    | 28 +++++++++-------
 .../HiveConnectionPropertiesParserTest.java        | 38 ++++++++++++++++++++++
 2 files changed, 55 insertions(+), 11 deletions(-)

diff --git a/database/connector/dialect/hive/src/main/java/org/apache/shardingsphere/database/connector/hive/jdbcurl/HiveConnectionPropertiesParser.java b/database/connector/dialect/hive/src/main/java/org/apache/shardingsphere/database/connector/hive/jdbcurl/HiveConnectionPropertiesParser.java
index 680f2954621..9ecb55c3598 100644
--- a/database/connector/dialect/hive/src/main/java/org/apache/shardingsphere/database/connector/hive/jdbcurl/HiveConnectionPropertiesParser.java
+++ b/database/connector/dialect/hive/src/main/java/org/apache/shardingsphere/database/connector/hive/jdbcurl/HiveConnectionPropertiesParser.java
@@ -33,20 +33,26 @@ import java.util.Properties;
  */
 public final class HiveConnectionPropertiesParser implements ConnectionPropertiesParser {
     
-    @SneakyThrows({ZooKeeperHiveClientException.class, JdbcUriParseException.class, SQLException.class})
     @Override
     public ConnectionProperties parse(final String url, final String username, final String catalog) {
-        JdbcConnectionParams params = Utils.parseURL(url, new Properties());
-        if (null == params.getHost() && 0 == params.getPort()) {
-            throw new RuntimeException("HiveServer2 in embedded mode has been deprecated by Apache Hive, "
-                    + "See https://issues.apache.org/jira/browse/HIVE-28418 . "
-                    + "Users should start local HiveServer2 through Docker Image https://hub.docker.com/r/apache/hive .");
+        return HiveJdbcUrlParser.parse(url);
+    }
+    
+    private static final class HiveJdbcUrlParser {
+        
+        @SneakyThrows({ZooKeeperHiveClientException.class, JdbcUriParseException.class, SQLException.class})
+        private static ConnectionProperties parse(final String url) {
+            JdbcConnectionParams params = Utils.parseURL(url, new Properties());
+            if (null == params.getHost() && 0 == params.getPort()) {
+                throw new RuntimeException("HiveServer2 in embedded mode has been deprecated by Apache Hive, See https://issues.apache.org/jira/browse/HIVE-28418 . "
+                        + "Users should start local HiveServer2 through Docker Image https://hub.docker.com/r/apache/hive .");
+            }
+            Properties queryProps = new Properties();
+            queryProps.putAll(params.getSessionVars());
+            queryProps.putAll(params.getHiveConfs());
+            queryProps.putAll(params.getHiveVars());
+            return new ConnectionProperties(params.getHost(), params.getPort(), params.getDbName(), null, queryProps);
         }
-        Properties queryProps = new Properties();
-        queryProps.putAll(params.getSessionVars());
-        queryProps.putAll(params.getHiveConfs());
-        queryProps.putAll(params.getHiveVars());
-        return new ConnectionProperties(params.getHost(), params.getPort(), params.getDbName(), null, queryProps);
     }
     
     @Override
diff --git a/database/connector/dialect/hive/src/test/java/org/apache/shardingsphere/database/connector/hive/jdbcurl/HiveConnectionPropertiesParserTest.java b/database/connector/dialect/hive/src/test/java/org/apache/shardingsphere/database/connector/hive/jdbcurl/HiveConnectionPropertiesParserTest.java
index b7a3ae040b2..0504a094a6f 100644
--- a/database/connector/dialect/hive/src/test/java/org/apache/shardingsphere/database/connector/hive/jdbcurl/HiveConnectionPropertiesParserTest.java
+++ b/database/connector/dialect/hive/src/test/java/org/apache/shardingsphere/database/connector/hive/jdbcurl/HiveConnectionPropertiesParserTest.java
@@ -33,12 +33,18 @@ import org.junit.jupiter.params.provider.Arguments;
 import org.junit.jupiter.params.provider.MethodSource;
 import org.mockito.MockedStatic;
 
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.URLClassLoader;
 import java.util.Collections;
 import java.util.Properties;
 import java.util.stream.Stream;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.sameInstance;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.ArgumentMatchers.any;
@@ -54,6 +60,38 @@ class HiveConnectionPropertiesParserTest {
     
     private final ConnectionPropertiesParser parser = DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class, databaseType);
     
+    @Test
+    void assertLoadWithoutHiveJdbcDriver() throws IOException, ReflectiveOperationException {
+        String[] classPathEntries = System.getProperty("surefire.test.class.path", System.getProperty("java.class.path")).split(File.pathSeparator);
+        URL[] classPathUrls = new URL[classPathEntries.length];
+        for (int i = 0; i < classPathEntries.length; i++) {
+            classPathUrls[i] = new File(classPathEntries[i]).toURI().toURL();
+        }
+        ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
+        try (URLClassLoader classLoader = new URLClassLoader(classPathUrls, ClassLoader.getSystemClassLoader().getParent()) {
+            
+            @Override
+            protected Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException {
+                if (name.startsWith("org.apache.hive.jdbc.")) {
+                    throw new ClassNotFoundException(name);
+                }
+                return super.loadClass(name, resolve);
+            }
+        }) {
+            Thread.currentThread().setContextClassLoader(classLoader);
+            assertThrows(ClassNotFoundException.class, () -> classLoader.loadClass("org.apache.hive.jdbc.Utils"));
+            Class<?> databaseTypeClass = classLoader.loadClass(DatabaseType.class.getName());
+            Class<?> parserClass = classLoader.loadClass(ConnectionPropertiesParser.class.getName());
+            Method getServiceMethod = classLoader.loadClass(TypedSPILoader.class.getName()).getMethod("getService", Class.class, Object.class);
+            Object hiveDatabaseType = getServiceMethod.invoke(null, databaseTypeClass, "Hive");
+            Object actual = getServiceMethod.invoke(null, parserClass, hiveDatabaseType);
+            assertThat(actual.getClass().getName(), is(HiveConnectionPropertiesParser.class.getName()));
+            assertThat(actual.getClass().getClassLoader(), sameInstance(classLoader));
+        } finally {
+            Thread.currentThread().setContextClassLoader(originalClassLoader);
+        }
+    }
+    
     @ParameterizedTest(name = "{0}")
     @MethodSource("parseSuccessArguments")
     void assertParseWithValidUrl(final String name, final String url, final String expectedHostname, final int expectedPort, final String expectedCatalog, final Properties expectedQueryProps) {