From 5cdac263156770309a9be9f283a11ae7ab2f077c Mon Sep 17 00:00:00 2001 From: linghengqian Date: Fri, 22 Nov 2024 21:33:16 +0800 Subject: [PATCH] HIVE-15540: Impl `DatabaseMetaData#getURL()` and `DatabaseMetaData#getUserName()` for HiveServer2 JDBC Driver --- .../hive/jdbc/TestHiveDatabaseMetaData.java | 28 +++++++++++++------ .../hive/jdbc/HiveDatabaseMetaData.java | 10 +++++-- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestHiveDatabaseMetaData.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestHiveDatabaseMetaData.java index 2e5128e1eaff..6d9e11aeaded 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestHiveDatabaseMetaData.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestHiveDatabaseMetaData.java @@ -18,18 +18,15 @@ package org.apache.hive.jdbc; -import org.apache.hive.jdbc.HiveConnection; -import org.apache.hive.jdbc.Utils; +import com.google.common.collect.ImmutableMap; import org.apache.hive.jdbc.Utils.JdbcConnectionParams; +import org.junit.Before; +import org.junit.Test; +import java.sql.SQLException; +import java.util.HashMap; import java.util.LinkedHashMap; -import java.util.Properties; import java.util.Map; -import java.util.HashMap; -import java.sql.SQLException; - -import org.junit.Before; -import org.junit.Test; import static org.junit.Assert.*; @@ -107,4 +104,19 @@ public void testHiveConnectionUdateServerHiveConf() { .get(Utils.JdbcConnectionParams.HIVE_CONF_PREFIX + "hive.default.nulls.last")); } + + @Test + public void testGetUserName() throws SQLException { + JdbcConnectionParams params = new JdbcConnectionParams(); + params.setSessionVars(ImmutableMap.of("user", "foo")); + HiveConnection hiveConnection = new HiveConnection(); + hiveConnection.setConnParams(params); + hiveDatabaseMetaData = new HiveDatabaseMetaData(hiveConnection, null, null); + assertEquals("foo", hiveDatabaseMetaData.getUserName()); + } + + @Test + public void testGetURL() { + assertEquals(connection.getConnectedUrl(), hiveDatabaseMetaData.getURL()); + } } \ No newline at end of file diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java b/jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java index d1cfd7eb2c56..26906cffe2c3 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java +++ b/jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java @@ -744,12 +744,16 @@ public T getObject(int columnIndex, Class type) throws SQLException { }; } - public String getURL() throws SQLException { - throw new SQLFeatureNotSupportedException("Method not supported"); + public String getURL() { + return connection.getConnectedUrl(); } public String getUserName() throws SQLException { - throw new SQLFeatureNotSupportedException("Method not supported"); + String userName = connection.getConnParams().getSessionVars().get(JdbcConnectionParams.AUTH_USER); + if ((userName == null) || userName.isEmpty()) { + userName = JdbcConnectionParams.ANONYMOUS_USER; + } + return userName; } public ResultSet getVersionColumns(String catalog, String schema, String table)