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


##########
java/driver/flight-sql/src/main/java/org/apache/arrow/adbc/driver/flightsql/FlightSqlSessionUtil.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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 AdbcException.invalidArgument(
+              "[Flight SQL] Expected JSON array for string list option, got: " 
+ json)
+          .withCause(e);
+    }
+  }
+
+  /**
+   * Casts a raw Java value extracted via {@link #TO_JAVA} to the type 
requested by a {@link
+   * TypedKey}. {@code raw} must not be {@code null} (Void options must be 
rejected before calling
+   * this). Returns {@code null} for unsupported types so the caller can 
delegate to the default
+   * {@code AdbcConnection} implementation.
+   */
+  @SuppressWarnings("unchecked")
+  static <T> T cast(TypedKey<T> key, Object raw, String optionName) throws 
AdbcException {
+    final Class<T> type = key.getType();
+    if (type == String.class) {
+      if (raw instanceof String[]) {
+        try {
+          return (T) MAPPER.writeValueAsString(raw);
+        } catch (JsonProcessingException e) {
+          throw new AdbcException(
+              "[Flight SQL] Failed to serialize string list option as JSON",
+              e,
+              AdbcStatusCode.INTERNAL,
+              null,
+              0);
+        }
+      }
+      return (T) String.valueOf(raw);
+    }
+    if (type == Boolean.class) {
+      if (raw instanceof Boolean) return (T) raw;
+      String s = String.valueOf(raw);
+      if ("true".equalsIgnoreCase(s)) return (T) Boolean.TRUE;
+      if ("false".equalsIgnoreCase(s)) return (T) Boolean.FALSE;
+      throw AdbcException.invalidArgument(
+          "[Flight SQL] Session option '" + optionName + "' cannot be parsed 
as Boolean: " + raw);
+    }
+    if (type == String[].class) {
+      if (raw instanceof String[]) return (T) raw;
+      return (T) new String[] {String.valueOf(raw)};
+    }
+    try {
+      if (type == Long.class) {
+        if (raw instanceof Long) return (T) raw;
+        if (raw instanceof Number) return (T) Long.valueOf(((Number) 
raw).longValue());
+        return (T) Long.valueOf(Long.parseLong(String.valueOf(raw)));
+      }
+      if (type == Double.class) {
+        if (raw instanceof Double) return (T) raw;
+        if (raw instanceof Number) return (T) Double.valueOf(((Number) 
raw).doubleValue());
+        return (T) Double.valueOf(Double.parseDouble(String.valueOf(raw)));
+      }
+    } catch (NumberFormatException e) {
+      throw AdbcException.invalidArgument(
+          "[Flight SQL] Session option '"
+              + optionName
+              + "' cannot be parsed as "
+              + type.getSimpleName()
+              + ": "
+              + raw);
+    }

Review Comment:
   When parsing Long/Double session options fails, the code throws 
INVALID_ARGUMENT but drops the original NumberFormatException. Preserving the 
cause makes debugging much easier and matches the error-wrapping intent 
described in the PR.



##########
java/driver/flight-sql/src/main/java/org/apache/arrow/adbc/driver/flightsql/FlightSqlConnection.java:
##########
@@ -203,13 +212,124 @@ 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(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 (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");
+    }
+
+    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)) {
+      final String name =
+          
k.substring(FlightSqlConnectionProperties.SESSION_OPTION_BOOL_PREFIX.length());
+      final boolean b;
+      if (value instanceof Boolean) {
+        b = (Boolean) value;
+      } else {
+        b = Boolean.parseBoolean(value.toString());
+      }

Review Comment:
   Boolean session options are parsed with Boolean.parseBoolean(...), which 
silently treats any non-"true" value as false (e.g., "yes" -> false). This can 
set an unintended value without surfacing INVALID_ARGUMENT, and is inconsistent 
with the stricter boolean parsing used on reads.



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