lidavidm commented on code in PR #4444:
URL: https://github.com/apache/arrow-adbc/pull/4444#discussion_r3525910931


##########
java/driver/flight-sql/src/main/java/org/apache/arrow/adbc/driver/flightsql/FlightSqlSessionUtil.java:
##########
@@ -0,0 +1,176 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.arrow.adbc.driver.flightsql;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import org.apache.arrow.adbc.core.AdbcException;
+import org.apache.arrow.adbc.core.AdbcStatusCode;
+import org.apache.arrow.adbc.core.TypedKey;
+import org.apache.arrow.flight.NoOpSessionOptionValueVisitor;
+import org.apache.arrow.flight.SessionOptionValue;
+
+/** Package-private helpers for Flight SQL session option serialization and 
type conversion. */
+final class FlightSqlSessionUtil {
+
+  static final ObjectMapper MAPPER = new ObjectMapper();
+
+  /**
+   * Extracts a native Java value from a {@link SessionOptionValue}. 
NaN/Infinity doubles are
+   * returned as-is and filtered in {@link #toJson}; String[] is defensively 
cloned; Void returns
+   * {@code null} (callers must handle null before calling {@link #cast}).
+   */
+  static final NoOpSessionOptionValueVisitor<Object> TO_JAVA =
+      new NoOpSessionOptionValueVisitor<Object>() {
+        @Override
+        public Object visit(String v) {
+          return v;
+        }
+
+        @Override
+        public Object visit(boolean v) {
+          return v;
+        }
+
+        @Override
+        public Object visit(long v) {
+          return v;
+        }
+
+        @Override
+        public Object visit(double v) {
+          return v;
+        }
+
+        @Override
+        public Object visit(String[] v) {
+          return v.clone();
+        }
+      };
+
+  /** Serializes all session options to a JSON object string. */
+  static String toJson(Map<String, SessionOptionValue> opts) throws 
AdbcException {
+    Map<String, Object> map = new LinkedHashMap<>();
+    for (Map.Entry<String, SessionOptionValue> e : opts.entrySet()) {
+      Object v = e.getValue().acceptVisitor(TO_JAVA);
+      // JSON does not support NaN or Infinity; represent them as null
+      if (v instanceof Double && !Double.isFinite((Double) v)) {
+        v = null;
+      }
+      map.put(e.getKey(), v);
+    }
+    try {
+      return MAPPER.writeValueAsString(map);
+    } catch (JsonProcessingException e) {
+      throw new AdbcException(
+          "[Flight SQL] Failed to serialize session options", e, 
AdbcStatusCode.INTERNAL, null, 0);
+    }
+  }
+
+  /** Parses a JSON string array (used when a string-list option is supplied 
as JSON). */
+  static String[] parseJsonArray(String json) throws AdbcException {
+    try {
+      return MAPPER.readValue(json, String[].class);
+    } catch (JsonProcessingException e) {
+      throw new AdbcException(

Review Comment:
   There are helpers like withCause so you can use the regular helpers instead 
of directly constructing the exception.



##########
java/driver/flight-sql/src/main/java/org/apache/arrow/adbc/driver/flightsql/FlightSqlConnection.java:
##########
@@ -203,17 +212,167 @@ 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");
+    }
+    final Object raw =
+        FlightSqlSessionUtil.require(fetchSessionOptions(), name)

Review Comment:
   Seems to me this could use fetchSessionOptionsOrEmpty



##########
java/driver/flight-sql/src/main/java/org/apache/arrow/adbc/driver/flightsql/FlightSqlConnection.java:
##########
@@ -203,17 +212,167 @@ 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");
+    }
+    final Object raw =
+        FlightSqlSessionUtil.require(fetchSessionOptions(), 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 (value == null) {
+      throw AdbcException.invalidArgument(
+          "[Flight SQL] null value not allowed for key: "
+              + k
+              + " — use adbc.flight.sql.session.optionerase.<name> to erase an 
option");

Review Comment:
   ```suggestion
                 + " - use adbc.flight.sql.session.optionerase.<name> to erase 
an option");
   ```



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