Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HIVE-15540: Impl DatabaseMetaData#getURL() and DatabaseMetaData#getUserName() for HiveServer2 JDBC Driver #5554

Merged
merged 1 commit into from
Dec 12, 2024

Conversation

linghengqian
Copy link
Member

@linghengqian linghengqian commented Nov 22, 2024

What changes were proposed in this pull request?

  • Impl DatabaseMetaData#getURL() and DatabaseMetaData#getUserName() for HiveServer2 JDBC Driver.

Why are the changes needed?

        try (Connection connection = dataSource.getConnection()) {
            return connection.getMetaData().getURL();
        } catch (final SQLFeatureNotSupportedException sqlFeatureNotSupportedException) {
            try (Connection connection = dataSource.getConnection()) {
                Class<?> hiveConnectionClass = Class.forName("org.apache.hive.jdbc.HiveConnection");
                if (connection.isWrapperFor(hiveConnectionClass)) {
                    Object hiveConnection = connection.unwrap(hiveConnectionClass);
                    String connectedUrl = (String) hiveConnectionClass.getMethod("getConnectedUrl").invoke(hiveConnection);
                    assertThat(connectedUrl, is("jdbc:hive2://127.0.0.1:2181/demo_ds_0;initFile=/tmp/init.sql;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2"));
                    return connectedUrl;
                }
                throw new SQLWrapperException(sqlFeatureNotSupportedException);
            } catch (final SQLException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException exception) {
                throw new RuntimeException(exception);
            }
        } catch (final SQLException ex) {
            throw new SQLWrapperException(ex);
        }

Does this PR introduce any user-facing change?

  • Maybe only third-party frameworks or tools like DBeaver will find the impact of the current PR. If one uses a web framework like Spring Boot framework, he has no way of knowing what the current PR does.

Is the change a dependency upgrade?

  • No

How was this patch tested?

sdk install java 8.0.422-tem
sdk use java 8.0.422-tem
sdk install maven
mvn clean install -DskipTests -Pitests
mvn test -Dtest=TestHiveDatabaseMetaData -Dtest.output.overwrite=true -pl itests/hive-unit -Pitests

@linghengqian linghengqian marked this pull request as ready for review November 22, 2024 13:59
@linghengqian linghengqian changed the title [WIP]HIVE-15540: Impl DatabaseMetaData#getURL() and DatabaseMetaData#getUserName() for HiveServer2 JDBC Driver HIVE-15540: Impl DatabaseMetaData#getURL() and DatabaseMetaData#getUserName() for HiveServer2 JDBC Driver Nov 22, 2024
@InvisibleProgrammer
Copy link
Contributor

I just looked into the jdbc driver recently and got surprised similarly as you.

Can I ask you to add some tests around the changes? I think TestJdbcDriver can be a great place to do that.

}

public String getUserName() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
return connection.getConnParams().getSessionVars().get("user");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be more bullet-proof to get the user name similar way as HiveConnection does: It gets the username, or it is not provided, gives back the anonymous:

  /**
   * @return username from sessConfMap
   */
  private String getUserName() {
    return getSessionValue(JdbcConnectionParams.AUTH_USER, JdbcConnectionParams.ANONYMOUS_USER);
  }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • HiveConnection#getSessionValue(String, String) and HiveConnection#getUserName() are both private methods. I guess what you mean is?
  public String getUserName() throws SQLException {
    String username = connection.getConnParams().getSessionVars().get(JdbcConnectionParams.AUTH_USER);
    if ((username == null) || username.isEmpty()) {
      username = JdbcConnectionParams.ANONYMOUS_USER;
    }
    return username;
  }
  • Changing the org.apache.hive.jdbc.TestJdbcDriver doesn't seem intuitive enough, should I create a org.apache.hive.jdbc.TestHiveDatabaseMetaData class?
  • By the way, Jenkins used by Hive limits a single PR to 5 hours between two CIs, and the current PR's github tag is a bit inconsistent with the actual situation.

Copy link
Contributor

@InvisibleProgrammer InvisibleProgrammer Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HiveConnection#getSessionValue(String, String) and HiveConnection#getUserName() are both private methods. I guess what you mean is?

Yes, something like that.

Changing the org.apache.hive.jdbc.TestJdbcDriver doesn't seem intuitive enough, should I create a org.apache.hive.jdbc.TestHiveDatabaseMetaData class?

Excuse me, wrong copy-paste. So, there is already a test class here: https://github.com/apache/hive/blob/master/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestHiveDatabaseMetaData.java

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I added new unit tests. I guess Hive Doc does not prohibit the use of Guava's utility classes in unit tests.
  • I must admit that I found the Hive Contributor Guide on ASF Confluence to be out of date, and I had to spend some time looking up how to start a single unit test locally.
sdk install java 8.0.422-tem
sdk use java 8.0.422-tem
sdk install maven
mvn clean install -DskipTests -Pitests
mvn test -Dtest=TestHiveDatabaseMetaData -Dtest.output.overwrite=true -pl itests/hive-unit -Pitests

Copy link
Contributor

@InvisibleProgrammer InvisibleProgrammer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you for the change.

Copy link
Member Author

@linghengqian linghengqian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sdk install java 8.0.422-tem
sdk use java 8.0.422-tem
sdk install maven
mvn clean install -DskipTests -Pitests
mvn test -Dtest=TestMiniLlapLocalCliDriver -Dtest.output.overwrite=true -pl itests/qtest -Pitests

Copy link
Member

@ayushtkn ayushtkn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments, rest looks good

Comment on lines 31 to 32
import org.junit.Before;
import org.junit.Test;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just avoid changing the import order, just makes things complicated while backporting

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines 751 to 753
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;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just do

  public String getUserName() throws SQLException {
    return connection.getUserName();
  }

and make getUserName() in HiveConnection package-private (default)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member

@ayushtkn ayushtkn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ayushtkn ayushtkn merged commit 28baba2 into apache:master Dec 12, 2024
6 checks passed
@linghengqian linghengqian deleted the presto-bump branch December 12, 2024 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants