This is an automated email from the ASF dual-hosted git repository.

CurtHagenlocher pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-adbc.git


The following commit(s) were added to refs/heads/main by this push:
     new 2ee9a10667 feat(csharp): support typed options in connection profiles 
(#4914)
2ee9a10667 is described below

commit 2ee9a106678bb713e1dd0137e50122bf33c54546
Author: Curt Hagenlocher <[email protected]>
AuthorDate: Mon Oct 19 16:42:06 2026 -0700

    feat(csharp): support typed options in connection profiles (#4914)
    
    This adds support for typed options in connection profiles. This
    diverges from the C implementation for backwards compatibility.
    Otherwise, a profile like
    ```
    [Options]
    adbc.snowflake.sql.client_option.max_retry_count = 3
    ```
    would fail in `AdbcDatabaseInit`. See #4913.
    
    Relates to #4751
---
 .../DriverManager/AdbcDriverManager.cs             | 124 +++++++++++++++
 .../DriverManager/FakeAdbcDriver.cs                |  27 ++++
 .../DriverManager/TomlConnectionProfileTests.cs    | 168 +++++++++++++++++++++
 3 files changed, 319 insertions(+)

diff --git a/csharp/src/Apache.Arrow.Adbc/DriverManager/AdbcDriverManager.cs 
b/csharp/src/Apache.Arrow.Adbc/DriverManager/AdbcDriverManager.cs
index dfae3f468f..08d4d8c328 100644
--- a/csharp/src/Apache.Arrow.Adbc/DriverManager/AdbcDriverManager.cs
+++ b/csharp/src/Apache.Arrow.Adbc/DriverManager/AdbcDriverManager.cs
@@ -461,6 +461,8 @@ namespace Apache.Arrow.Adbc.DriverManager
         /// <c>string → string</c> dictionary.  Integer and double values are 
formatted
         /// using <see cref="CultureInfo.InvariantCulture"/>. The merged 
dictionary is
         /// passed to <see 
cref="AdbcDriver.Open(IReadOnlyDictionary{string,string})"/>.
+        /// Use <see cref="OpenDatabaseFromProfileWithTypedOptions"/> to 
preserve the
+        /// types of integer and double options.
         /// The <c>entrypoint</c> option (if any) is consumed by the driver 
manager
         /// and is not forwarded to the driver.
         /// </para>
@@ -513,6 +515,8 @@ namespace Apache.Arrow.Adbc.DriverManager
         /// </list>
         /// The merged dictionary, minus any <c>entrypoint</c> entry, is 
passed to
         /// <see cref="AdbcDriver.Open(IReadOnlyDictionary{string,string})"/>.
+        /// Use <see cref="OpenDatabaseFromProfileWithTypedOptions"/> to 
preserve the
+        /// types of integer and double options.
         /// </para>
         /// </remarks>
         /// <param name="profile">The connection profile specifying the driver 
and options.</param>
@@ -553,6 +557,71 @@ namespace Apache.Arrow.Adbc.DriverManager
             return driver.Open(mergedOptions);
         }
 
+        /// <summary>
+        /// Loads the driver specified by <paramref name="profile"/> and opens 
a database,
+        /// merging profile options with explicitly provided typed options.
+        /// </summary>
+        /// <remarks>
+        /// <para>
+        /// This behaves like
+        /// <see cref="OpenDatabaseFromProfile(ConnectionProfile, 
IReadOnlyDictionary{string, string}, AdbcLoadFlags, string)"/>
+        /// except that the profile's integer and double options keep their 
types, and
+        /// <paramref name="explicitOptions"/> may carry strings, integers,
+        /// floating-point numbers or byte arrays. Options are merged as 
described by
+        /// <see cref="BuildTypedOptions"/> and passed to
+        /// <see cref="AdbcDriver.Open(IReadOnlyDictionary{string,object})"/>, 
so drivers
+        /// implementing ADBC 1.1 or later receive integer and double values 
through
+        /// their typed option setters.
+        /// </para>
+        /// <para>
+        /// Some drivers only accept a given option through the string setter 
and reject
+        /// it when it arrives as an integer or double. Use this method only 
when the
+        /// driver is known to accept typed values for the options being set.
+        /// </para>
+        /// <para>
+        /// If present, the <c>entrypoint</c> option must be a string.
+        /// </para>
+        /// </remarks>
+        /// <param name="profile">The connection profile specifying the driver 
and options.</param>
+        /// <param name="explicitOptions">
+        /// Additional options to merge with profile options. Explicit options 
override profile
+        /// options for the same key. May be <c>null</c> or empty.
+        /// </param>
+        /// <param name="loadOptions">Flags controlling directory search for 
native drivers.</param>
+        /// <param name="additionalSearchPathList">
+        /// An optional extra search path list for native driver discovery.
+        /// </param>
+        /// <returns>An open <see cref="AdbcDatabase"/>.</returns>
+        public static AdbcDatabase OpenDatabaseFromProfileWithTypedOptions(
+            ConnectionProfile profile,
+            IReadOnlyDictionary<string, object>? explicitOptions,
+            AdbcLoadFlags loadOptions = AdbcLoadFlags.Default,
+            string? additionalSearchPathList = null)
+        {
+            if (profile == null) throw new 
ArgumentNullException(nameof(profile));
+
+            Dictionary<string, object> mergedOptions = new Dictionary<string, 
object>(StringComparer.Ordinal);
+            foreach (KeyValuePair<string, object> kv in 
BuildTypedOptions(profile, explicitOptions))
+            {
+                mergedOptions[kv.Key] = kv.Value;
+            }
+
+            // entrypoint is a driver-manager option, not a driver option: 
pull it out
+            // of the bag before opening the database. When set, it overrides 
any
+            // entrypoint declared by a driver manifest found via 
FindLoadDriver.
+            string? entrypoint = null;
+            if (mergedOptions.TryGetValue(EntrypointOptionKey, out object? 
entrypointValue))
+            {
+                entrypoint = entrypointValue as string ?? throw new 
AdbcException(
+                    $"The '{EntrypointOptionKey}' option must be a string.",
+                    AdbcStatusCode.InvalidArgument);
+                mergedOptions.Remove(EntrypointOptionKey);
+            }
+
+            AdbcDriver driver = LoadDriverFromProfile(profile, entrypoint, 
loadOptions, additionalSearchPathList);
+            return driver.Open(mergedOptions);
+        }
+
         /// <summary>
         /// Merges all options from a profile into a flat <c>string → 
string</c> dictionary
         /// suitable for passing to <see 
cref="AdbcDriver.Open(IReadOnlyDictionary{string,string})"/>.
@@ -617,6 +686,61 @@ namespace Apache.Arrow.Adbc.DriverManager
             return merged;
         }
 
+        /// <summary>
+        /// Merges options from a profile with explicitly provided options 
into a flat
+        /// <c>string → object</c> dictionary suitable for passing to
+        /// <see cref="AdbcDriver.Open(IReadOnlyDictionary{string,object})"/>.
+        /// </summary>
+        /// <remarks>
+        /// <para>
+        /// Unlike <see cref="BuildStringOptions(ConnectionProfile, 
IReadOnlyDictionary{string, string})"/>,
+        /// integer options are kept as <see cref="long"/> and double options 
as
+        /// <see cref="double"/>, so that drivers implementing ADBC 1.1 or 
later receive
+        /// them through their typed option setters.
+        /// </para>
+        /// <para>
+        /// Options are merged in the following order (later values override 
earlier ones):
+        /// <list type="number">
+        ///   <item>Profile integer options</item>
+        ///   <item>Profile double options</item>
+        ///   <item>Profile string options</item>
+        ///   <item>Explicit options from <paramref 
name="explicitOptions"/></item>
+        /// </list>
+        /// </para>
+        /// </remarks>
+        /// <param name="profile">The connection profile containing base 
options.</param>
+        /// <param name="explicitOptions">
+        /// Additional options that override profile options. May be 
<c>null</c> or empty.
+        /// </param>
+        /// <returns>A merged dictionary of all options.</returns>
+        public static IReadOnlyDictionary<string, object> BuildTypedOptions(
+            ConnectionProfile profile,
+            IReadOnlyDictionary<string, object>? explicitOptions = null)
+        {
+            if (profile == null) throw new 
ArgumentNullException(nameof(profile));
+
+            var merged = new Dictionary<string, 
object>(StringComparer.Ordinal);
+
+            // Profile options first (in order: int, double, string)
+            foreach (var kv in profile.IntOptions)
+                merged[kv.Key] = kv.Value;
+
+            foreach (var kv in profile.DoubleOptions)
+                merged[kv.Key] = kv.Value;
+
+            foreach (var kv in profile.StringOptions)
+                merged[kv.Key] = kv.Value;
+
+            // Explicit options last – they override profile options
+            if (explicitOptions != null)
+            {
+                foreach (var kv in explicitOptions)
+                    merged[kv.Key] = kv.Value;
+            }
+
+            return merged;
+        }
+
         // 
-----------------------------------------------------------------------
         // Helpers – derive entrypoint, search directories, manifest loading
         // 
-----------------------------------------------------------------------
diff --git 
a/csharp/test/Apache.Arrow.Adbc.Tests/DriverManager/FakeAdbcDriver.cs 
b/csharp/test/Apache.Arrow.Adbc.Tests/DriverManager/FakeAdbcDriver.cs
index efe5404dd0..c17bf0f7fe 100644
--- a/csharp/test/Apache.Arrow.Adbc.Tests/DriverManager/FakeAdbcDriver.cs
+++ b/csharp/test/Apache.Arrow.Adbc.Tests/DriverManager/FakeAdbcDriver.cs
@@ -48,4 +48,31 @@ namespace Apache.Arrow.Adbc.Tests.DriverManager
         public override AdbcConnection Connect(IReadOnlyDictionary<string, 
string>? options) =>
             throw new System.NotImplementedException();
     }
+
+    /// <summary>
+    /// Minimal in-memory AdbcDriver that accepts typed options, used to 
verify that
+    /// the driver manager preserves option types.
+    /// </summary>
+    public sealed class FakeTypedAdbcDriver : AdbcDriver
+    {
+        public override AdbcDatabase Open(IReadOnlyDictionary<string, string> 
parameters) =>
+            throw new System.InvalidOperationException("Expected the typed 
Open overload to be called.");
+
+        public override AdbcDatabase Open(IReadOnlyDictionary<string, object> 
parameters) =>
+            new FakeTypedAdbcDatabase(parameters);
+    }
+
+    /// <summary>Minimal in-memory AdbcDatabase returned by <see 
cref="FakeTypedAdbcDriver"/>.</summary>
+    public sealed class FakeTypedAdbcDatabase : AdbcDatabase
+    {
+        public IReadOnlyDictionary<string, object> Parameters { get; }
+
+        public FakeTypedAdbcDatabase(IReadOnlyDictionary<string, object> 
parameters)
+        {
+            Parameters = parameters;
+        }
+
+        public override AdbcConnection Connect(IReadOnlyDictionary<string, 
string>? options) =>
+            throw new System.NotImplementedException();
+    }
 }
diff --git 
a/csharp/test/Apache.Arrow.Adbc.Tests/DriverManager/TomlConnectionProfileTests.cs
 
b/csharp/test/Apache.Arrow.Adbc.Tests/DriverManager/TomlConnectionProfileTests.cs
index c811b47ec1..be8a1f4cee 100644
--- 
a/csharp/test/Apache.Arrow.Adbc.Tests/DriverManager/TomlConnectionProfileTests.cs
+++ 
b/csharp/test/Apache.Arrow.Adbc.Tests/DriverManager/TomlConnectionProfileTests.cs
@@ -1232,5 +1232,173 @@ bool_key = false
             IReadOnlyDictionary<string, string> parameters = 
(IReadOnlyDictionary<string, string>)paramsProp!.GetValue(db)!;
             Assert.Equal("value", parameters["key"]);
         }
+
+        // 
-----------------------------------------------------------------------
+        // Positive: typed profile options
+        // 
-----------------------------------------------------------------------
+
+        [Fact]
+        public void BuildTypedOptions_PreservesOptionTypes()
+        {
+            const string toml = @"
+version = 1
+driver = ""d""
+
+[options]
+str_key = ""hello""
+int_key = 42
+dbl_key = 1.5
+bool_key = true
+";
+            ConnectionProfile profile = 
FilesystemProfileProvider.LoadFromContent(toml);
+            IReadOnlyDictionary<string, object> opts = 
AdbcDriverManager.BuildTypedOptions(profile);
+
+            Assert.Equal("hello", Assert.IsType<string>(opts["str_key"]));
+            Assert.Equal(42L, Assert.IsType<long>(opts["int_key"]));
+            Assert.Equal(1.5, Assert.IsType<double>(opts["dbl_key"]));
+            Assert.Equal("true", Assert.IsType<string>(opts["bool_key"]));
+        }
+
+        [Fact]
+        public void BuildTypedOptions_ExplicitOptionsOverrideAllProfileTypes()
+        {
+            const string toml = @"
+version = 1
+driver = ""d""
+
+[options]
+int_key = 42
+dbl_key = 1.5
+str_key = ""hello""
+";
+            ConnectionProfile profile = 
FilesystemProfileProvider.LoadFromContent(toml);
+            Dictionary<string, object> explicitOptions = new 
Dictionary<string, object>
+            {
+                { "int_key", "forty-two" },
+                { "dbl_key", 7L },
+                { "str_key", 2.5 },
+            };
+
+            IReadOnlyDictionary<string, object> merged = 
AdbcDriverManager.BuildTypedOptions(profile, explicitOptions);
+
+            Assert.Equal("forty-two", merged["int_key"]);
+            Assert.Equal(7L, merged["dbl_key"]);
+            Assert.Equal(2.5, merged["str_key"]);
+        }
+
+        [Fact]
+        public void BuildTypedOptions_NullProfile_ThrowsArgumentNullException()
+        {
+            Assert.Throws<ArgumentNullException>(() => 
AdbcDriverManager.BuildTypedOptions(null!));
+        }
+
+        private static string TypedDriverProfile(string options)
+        {
+            string assemblyPath = 
typeof(FakeTypedAdbcDriver).Assembly.Location;
+            string typeName = typeof(FakeTypedAdbcDriver).FullName!;
+
+            string escapedPath = assemblyPath.Replace("\\", "\\\\");
+            return "profile_version = 1\n"
+                + "driver = \"" + escapedPath + "\"\n"
+                + "\n[Options]\n"
+                + "entrypoint = \"" + ManagedScheme + typeName + "\"\n"
+                + options;
+        }
+
+        private static IReadOnlyDictionary<string, object> 
GetTypedParameters(AdbcDatabase db)
+        {
+            
Assert.Equal("Apache.Arrow.Adbc.Tests.DriverManager.FakeTypedAdbcDatabase", 
db.GetType().FullName);
+            System.Reflection.PropertyInfo? paramsProp = 
db.GetType().GetProperty("Parameters");
+            Assert.NotNull(paramsProp);
+            return (IReadOnlyDictionary<string, 
object>)paramsProp!.GetValue(db)!;
+        }
+
+        [Fact]
+        public void 
OpenDatabaseFromProfileWithTypedOptions_TypedDriver_ReceivesTypedOptions()
+        {
+            string toml = TypedDriverProfile(
+                "str_key = \"hello\"\n"
+                + "int_key = 42\n"
+                + "dbl_key = 1.5\n");
+
+            ConnectionProfile profile = 
FilesystemProfileProvider.LoadFromContent(toml);
+            AdbcDatabase db = 
AdbcDriverManager.OpenDatabaseFromProfileWithTypedOptions(profile, null);
+
+            IReadOnlyDictionary<string, object> parameters = 
GetTypedParameters(db);
+            Assert.Equal("hello", 
Assert.IsType<string>(parameters["str_key"]));
+            Assert.Equal(42L, Assert.IsType<long>(parameters["int_key"]));
+            Assert.Equal(1.5, Assert.IsType<double>(parameters["dbl_key"]));
+            Assert.False(parameters.ContainsKey("entrypoint"));
+        }
+
+        [Fact]
+        public void 
OpenDatabaseFromProfileWithTypedOptions_MergesExplicitTypedOptions()
+        {
+            string toml = TypedDriverProfile(
+                "int_key = 42\n"
+                + "shared_key = 1\n");
+
+            ConnectionProfile profile = 
FilesystemProfileProvider.LoadFromContent(toml);
+            Dictionary<string, object> explicitOptions = new 
Dictionary<string, object>
+            {
+                { "explicit_key", 2.5 },
+                { "shared_key", 7L },
+                { "bytes_key", new byte[] { 1, 2, 3 } },
+            };
+
+            AdbcDatabase db = 
AdbcDriverManager.OpenDatabaseFromProfileWithTypedOptions(profile, 
explicitOptions);
+
+            IReadOnlyDictionary<string, object> parameters = 
GetTypedParameters(db);
+            Assert.Equal(42L, parameters["int_key"]);
+            Assert.Equal(2.5, parameters["explicit_key"]);
+            Assert.Equal(7L, parameters["shared_key"]);
+            Assert.Equal(new byte[] { 1, 2, 3 }, 
Assert.IsType<byte[]>(parameters["bytes_key"]));
+        }
+
+        [Fact]
+        public void 
OpenDatabaseFromProfile_StringOnlyDriver_ReceivesFormattedOptions()
+        {
+            string assemblyPath = typeof(FakeAdbcDriver).Assembly.Location;
+            string typeName = typeof(FakeAdbcDriver).FullName!;
+
+            string escapedPath = assemblyPath.Replace("\\", "\\\\");
+            string toml = "profile_version = 1\n"
+                + "driver = \"" + escapedPath + "\"\n"
+                + "\n[Options]\n"
+                + "entrypoint = \"" + ManagedScheme + typeName + "\"\n"
+                + "int_key = 42\n"
+                + "dbl_key = 1.5\n";
+
+            ConnectionProfile profile = 
FilesystemProfileProvider.LoadFromContent(toml);
+            AdbcDatabase db = 
AdbcDriverManager.OpenDatabaseFromProfile(profile);
+
+            System.Reflection.PropertyInfo? paramsProp = 
db.GetType().GetProperty("Parameters");
+            Assert.NotNull(paramsProp);
+            IReadOnlyDictionary<string, string> parameters = 
(IReadOnlyDictionary<string, string>)paramsProp!.GetValue(db)!;
+            Assert.Equal("42", parameters["int_key"]);
+            Assert.Equal("1.5", parameters["dbl_key"]);
+        }
+
+        [Fact]
+        public void 
OpenDatabaseFromProfileWithTypedOptions_NonStringEntrypoint_Throws()
+        {
+            string toml = TypedDriverProfile("key = \"value\"\n");
+            ConnectionProfile profile = 
FilesystemProfileProvider.LoadFromContent(toml);
+            Dictionary<string, object> explicitOptions = new 
Dictionary<string, object>
+            {
+                { "entrypoint", 42L },
+            };
+
+            AdbcException ex = Assert.Throws<AdbcException>(
+                () => 
AdbcDriverManager.OpenDatabaseFromProfileWithTypedOptions(profile, 
explicitOptions));
+            Assert.Equal(AdbcStatusCode.InvalidArgument, ex.Status);
+        }
+
+        [Fact]
+        public void 
OpenDatabaseFromProfileWithTypedOptions_NullProfile_ThrowsArgumentNullException()
+        {
+            Assert.Throws<ArgumentNullException>(
+                () => 
AdbcDriverManager.OpenDatabaseFromProfileWithTypedOptions(null!, null));
+        }
     }
 }

Reply via email to