Aggarwal-Raghav commented on code in PR #6207:
URL: https://github.com/apache/hive/pull/6207#discussion_r2564249293
##########
beeline/src/java/org/apache/hive/beeline/Commands.java:
##########
@@ -340,48 +341,48 @@ public boolean reconnect(String line) {
return true;
}
-
public boolean scan(String line) throws IOException {
- TreeSet<String> names = new TreeSet<String>();
-
if (beeLine.getDrivers() == null) {
beeLine.setDrivers(beeLine.scanDrivers());
}
- beeLine.info(beeLine.loc("drivers-found-count",
beeLine.getDrivers().size()));
+ // Use a TreeSet to get a unique, sorted list of drivers by class name.
+ Set<Driver> drivers =
+ new TreeSet<>(Comparator.comparing(d -> d.getClass().getName()));
+ drivers.addAll(beeLine.getDrivers());
- // unique the list
- for (Iterator<Driver> i = beeLine.getDrivers().iterator(); i.hasNext();) {
- names.add(i.next().getClass().getName());
- }
+ // Get count of the unique driver in classpath
+ beeLine.info(beeLine.loc("drivers-found-count", drivers.size()));
- beeLine.output(beeLine.getColorBuffer()
- .bold(beeLine.getColorBuffer().pad(beeLine.loc("compliant"),
10).getMono())
- .bold(beeLine.getColorBuffer().pad(beeLine.loc("jdbc-version"),
8).getMono())
- .bold(beeLine.getColorBuffer(beeLine.loc("driver-class")).getMono()));
+ beeLine.output(
+ beeLine
+ .getColorBuffer()
+ .bold(beeLine.getColorBuffer().pad(beeLine.loc("compliant"),
10).getMono())
+ .bold(beeLine.getColorBuffer().pad(beeLine.loc("jdbc-version"),
8).getMono())
+
.bold(beeLine.getColorBuffer(beeLine.loc("driver-class")).getMono()));
- for (Iterator<String> i = names.iterator(); i.hasNext();) {
- String name = i.next().toString();
+ for (Driver driver : drivers) {
+ String name = driver.getClass().getName();
try {
- Driver driver = (Driver) Class.forName(name).newInstance();
- ColorBuffer msg = beeLine.getColorBuffer()
- .pad(driver.jdbcCompliant() ? "yes" : "no", 10)
- .pad(driver.getMajorVersion() + "."
- + driver.getMinorVersion(), 8)
- .append(name);
+ // Use the driver instance that ServiceLoader already created for us.
+ ColorBuffer msg =
+ beeLine
+ .getColorBuffer()
+ .pad(driver.jdbcCompliant() ? "yes" : "no", 10)
+ .pad(driver.getMajorVersion() + "." +
driver.getMinorVersion(), 8)
+ .append(name);
if (driver.jdbcCompliant()) {
beeLine.output(msg);
} else {
beeLine.output(beeLine.getColorBuffer().red(msg.getMono()));
}
} catch (Throwable t) {
Review Comment:
I have mixed opinion on this.
As `jdbcCompliant()`, `getMajorVersion()`, `getMinorVersion()` don't have
checked exception but there is a possibility that a faulty/3rd party driver in
classpath can through Exception at runtime. Hence, I was inclined to keep it.
Let me know if you think otherwise.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]