Kyperr commented on code in PR #4747:
URL: https://github.com/apache/arrow-adbc/pull/4747#discussion_r3915495901


##########
java/driver/flight-sql/src/main/java/org/apache/arrow/adbc/driver/flightsql/FlightSqlConnection.java:
##########
@@ -208,11 +217,174 @@ public void setAutoCommit(boolean enableAutoCommit) 
throws AdbcException {
     }
   }
 
+  @Override
+  public <T> T getOption(TypedKey<T> key) throws AdbcException {
+    final String k = key.getKey();
+
+    if (k.equals(FlightSqlConnectionProperties.SESSION_OPTIONS)) {
+      if (key.getType() != String.class) {
+        return AdbcConnection.super.getOption(key);
+      }
+      return 
key.cast(FlightSqlSessionUtil.toJson(fetchSessionOptionsOrEmpty()));
+    }
+
+    final String prefix;
+    if 
(k.startsWith(FlightSqlConnectionProperties.SESSION_OPTION_BOOL_PREFIX)) {
+      prefix = FlightSqlConnectionProperties.SESSION_OPTION_BOOL_PREFIX;
+    } else if 
(k.startsWith(FlightSqlConnectionProperties.SESSION_OPTION_STRING_LIST_PREFIX)) 
{
+      prefix = FlightSqlConnectionProperties.SESSION_OPTION_STRING_LIST_PREFIX;
+    } else if 
(k.startsWith(FlightSqlConnectionProperties.SESSION_OPTION_PREFIX)) {
+      prefix = FlightSqlConnectionProperties.SESSION_OPTION_PREFIX;
+    } else {
+      return AdbcConnection.super.getOption(key);
+    }
+
+    final String name = k.substring(prefix.length());
+    if (name.isEmpty()) {
+      throw AdbcException.invalidArgument("[Flight SQL] Session option name 
must not be empty");
+    }
+    if (!FlightSqlSessionUtil.supportsType(key, prefix)) {
+      return AdbcConnection.super.getOption(key);
+    }
+
+    final Object raw =
+        FlightSqlSessionUtil.require(fetchSessionOptionsOrEmpty(), name)
+            .acceptVisitor(FlightSqlSessionUtil.TO_JAVA);
+    if (raw == null) {
+      throw new AdbcException(
+          "[Flight SQL] Session option not found: " + name,
+          null,
+          AdbcStatusCode.NOT_FOUND,
+          null,
+          0);
+    }
+    final T result = FlightSqlSessionUtil.cast(key, raw, name);
+    return result != null ? result : AdbcConnection.super.getOption(key);
+  }
+
+  @Override
+  public <T> void setOption(TypedKey<T> key, T value) throws AdbcException {
+    final String k = key.getKey();
+
+    if 
(k.startsWith(FlightSqlConnectionProperties.SESSION_OPTION_ERASE_PREFIX)) {
+      final String name =
+          
k.substring(FlightSqlConnectionProperties.SESSION_OPTION_ERASE_PREFIX.length());
+      doSetSessionOption(name, 
SessionOptionValueFactory.makeEmptySessionOptionValue());
+
+    } else if 
(k.startsWith(FlightSqlConnectionProperties.SESSION_OPTION_BOOL_PREFIX)) {
+      if (value == null) {
+        throw invalidNullValue(k);
+      }
+      final String name =
+          
k.substring(FlightSqlConnectionProperties.SESSION_OPTION_BOOL_PREFIX.length());
+      final boolean b;
+      if (key.getType() == Boolean.class) {
+        if (!(value instanceof Boolean)) {
+          throw invalidValueType(k, value, Boolean.class);
+        }
+        b = (Boolean) value;
+      } else if (key.getType() == String.class) {
+        if (!(value instanceof String)) {
+          throw invalidValueType(k, value, String.class);
+        }
+        b = FlightSqlSessionUtil.parseStrictBoolean((String) value, name);
+      } else {
+        AdbcConnection.super.setOption(key, value);
+        return;
+      }
+      doSetSessionOption(name, 
SessionOptionValueFactory.makeSessionOptionValue(b));
+
+    } else if 
(k.startsWith(FlightSqlConnectionProperties.SESSION_OPTION_STRING_LIST_PREFIX)) 
{
+      if (value == null) {
+        throw invalidNullValue(k);
+      }
+      final String name =
+          
k.substring(FlightSqlConnectionProperties.SESSION_OPTION_STRING_LIST_PREFIX.length());
+      final String[] arr;
+      if (key.getType() == String[].class) {
+        if (!(value instanceof String[])) {
+          throw invalidValueType(k, value, String[].class);
+        }
+        arr = FlightSqlSessionUtil.validateStringArray((String[]) value);
+      } else if (key.getType() == String.class) {
+        if (!(value instanceof String)) {
+          throw invalidValueType(k, value, String.class);
+        }
+        arr = FlightSqlSessionUtil.parseJsonArray((String) value);
+      } else {
+        AdbcConnection.super.setOption(key, value);
+        return;
+      }
+      doSetSessionOption(name, 
SessionOptionValueFactory.makeSessionOptionValue(arr));
+
+    } else if 
(k.startsWith(FlightSqlConnectionProperties.SESSION_OPTION_PREFIX)) {
+      if (value == null) {
+        throw invalidNullValue(k);
+      }
+      final String name = 
k.substring(FlightSqlConnectionProperties.SESSION_OPTION_PREFIX.length());
+      final SessionOptionValue sv;
+      if (key.getType() == String.class) {
+        if (!(value instanceof String)) {
+          throw invalidValueType(k, value, String.class);
+        }
+        sv = SessionOptionValueFactory.makeSessionOptionValue((String) value);
+      } else if (key.getType() == Long.class) {
+        if (!(value instanceof Long)) {
+          throw invalidValueType(k, value, Long.class);
+        }
+        sv = SessionOptionValueFactory.makeSessionOptionValue((Long) value);
+      } else if (key.getType() == Double.class) {
+        if (!(value instanceof Double)) {
+          throw invalidValueType(k, value, Double.class);
+        }
+        sv = SessionOptionValueFactory.makeSessionOptionValue((Double) value);
+      } else {
+        AdbcConnection.super.setOption(key, value);
+        return;
+      }
+      doSetSessionOption(name, sv);
+
+    } else if (k.equals(FlightSqlConnectionProperties.SESSION_OPTIONS)) {
+      throw AdbcException.notImplemented(
+          "[Flight SQL] adbc.flight.sql.session.options is read-only");
+
+    } else {
+      AdbcConnection.super.setOption(key, value);
+    }
+  }
+
+  private static AdbcException invalidNullValue(String key) {
+    return AdbcException.invalidArgument(
+        "[Flight SQL] null value not allowed for key: "
+            + key
+            + " - use adbc.flight.sql.session.optionerase.<name> to erase an 
option");
+  }
+
+  private static AdbcException invalidValueType(String key, Object value, 
Class<?> expectedType) {
+    return AdbcException.invalidArgument(
+        "[Flight SQL] invalid value type for key "
+            + key
+            + ": expected "
+            + expectedType.getSimpleName()
+            + ", got "
+            + value.getClass().getSimpleName());
+  }
+
   @Override
   public void close() throws AdbcException {
-    clientCache.invalidateAll();
     try {
-      AutoCloseables.close(client, allocator);
+      AutoCloseables.close(
+          () -> {
+            try {
+              // Best-effort: the Go driver also ignores all errors closing 
the session.
+              client.closeSession(new CloseSessionRequest());

Review Comment:
   Some servers will throw an exception if this does not pass the callOptions. 
Looks like you went through the effort to implement those options, but they 
aren't used here.



-- 
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]

Reply via email to