diff --git a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java index ea5bbddc44ef9..cdbda55aa431e 100644 --- a/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java +++ b/java/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java @@ -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; @@ -62,7 +64,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; /** @@ -77,6 +78,7 @@ public final class ArrowFlightSqlClientHandler implements AutoCloseable { private final Set options = new HashSet<>(); private final Builder builder; private final String catalog; + private boolean setCatalogInSession = false; ArrowFlightSqlClientHandler(final FlightSqlClient sqlClient, final Builder builder, @@ -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; + } } /** @@ -220,7 +225,7 @@ public void close() throws SQLException { } private boolean hasCatalog() { - return !Strings.isNullOrEmpty(catalog); + return Optional.ofNullable(catalog).isPresent(); } /** @@ -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.builder() .put(CATALOG, SessionOptionValueFactory.makeSessionOptionValue(catalog)) .build()); final SetSessionOptionsResult result = sqlClient.setSessionOptions(setSessionOptionRequest, getOptions()); + setCatalogInSession = false; + if (result.hasErrors()) { Map errors = result.getErrors(); for (Map.Entry 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(); } }