Skip to content

Commit

Permalink
HIVE-15540: Impl DatabaseMetaData#getURL() and `DatabaseMetaData#ge…
Browse files Browse the repository at this point in the history
…tUserName()` for HiveServer2 JDBC Driver
  • Loading branch information
linghengqian committed Nov 22, 2024
1 parent afe05b9 commit 5cdac26
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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());
}
}
10 changes: 7 additions & 3 deletions jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -744,12 +744,16 @@ public <T> T getObject(int columnIndex, Class<T> 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)
Expand Down

0 comments on commit 5cdac26

Please sign in to comment.