Skip to content

Commit

Permalink
apacheGH-41947: [Java] Support catalog in JDBC driver with session op…
Browse files Browse the repository at this point in the history
…tions

PR comments
  • Loading branch information
stevelorddremio committed Jun 10, 2024
1 parent b38f46e commit 356d4bd
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.apache.arrow.driver.jdbc.client.utils.ClientAuthenticationUtils;
import org.apache.arrow.flight.CallOption;
import org.apache.arrow.flight.CallStatus;
import org.apache.arrow.flight.CloseSessionRequest;
import org.apache.arrow.flight.FlightClient;
import org.apache.arrow.flight.FlightClientMiddleware;
Expand Down Expand Up @@ -62,7 +64,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;

/**
Expand All @@ -77,6 +78,7 @@ public final class ArrowFlightSqlClientHandler implements AutoCloseable {
private final Set<CallOption> options = new HashSet<>();
private final Builder builder;
private final String catalog;
private boolean setCatalogInSession = false;

ArrowFlightSqlClientHandler(final FlightSqlClient sqlClient,
final Builder builder,
Expand All @@ -87,6 +89,9 @@ public final class ArrowFlightSqlClientHandler implements AutoCloseable {
this.sqlClient = Preconditions.checkNotNull(sqlClient);
this.builder = builder;
this.catalog = catalog;
if (hasCatalog()) {
setCatalogInSession = true;
}
}

/**
Expand Down Expand Up @@ -220,7 +225,7 @@ public void close() throws SQLException {
}

private boolean hasCatalog() {
return !Strings.isNullOrEmpty(catalog);
return Optional.ofNullable(catalog).isPresent();
}

/**
Expand Down Expand Up @@ -276,18 +281,23 @@ public interface PreparedStatement extends AutoCloseable {
* @return a new prepared statement.
*/
public PreparedStatement prepare(final String query) {
if (hasCatalog()) {
if (setCatalogInSession) {
final SetSessionOptionsRequest setSessionOptionRequest =
new SetSessionOptionsRequest(ImmutableMap.<String, SessionOptionValue>builder()
.put(CATALOG, SessionOptionValueFactory.makeSessionOptionValue(catalog))
.build());
final SetSessionOptionsResult result = sqlClient.setSessionOptions(setSessionOptionRequest, getOptions());
setCatalogInSession = false;

if (result.hasErrors()) {
Map<String, SetSessionOptionsResult.Error> errors = result.getErrors();
for (Map.Entry<String, SetSessionOptionsResult.Error> error : errors.entrySet()) {
LOGGER.warn(error.toString());
}
throw new RuntimeException(String.format("Cannot set session option for catalog = %s", catalog));
throw new CallStatus(FlightStatusCode.INVALID_ARGUMENT)
.withDescription(
String.format("Cannot set session option for catalog = %s. Check log for details.", catalog))
.toRuntimeException();
}
}

Expand Down

0 comments on commit 356d4bd

Please sign in to comment.