unikdahal commented on code in PR #4444: URL: https://github.com/apache/arrow-adbc/pull/4444#discussion_r3523598373
########## java/driver/flight-sql/src/main/java/org/apache/arrow/adbc/driver/flightsql/FlightSqlSessionUtil.java: ########## @@ -0,0 +1,172 @@ +/* + * 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( + "[Flight SQL] Expected JSON array for string list option, got: " + json, + e, + AdbcStatusCode.INVALID_ARGUMENT, + null, + 0); + } + } + + /** + * 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; + return (T) Boolean.valueOf(Boolean.parseBoolean(String.valueOf(raw))); + } Review Comment: refactored -- 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]
