You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by cu...@apache.org on 2026/10/15 03:14:53 UTC
(arrow-adbc) branch main updated: feat(csharp): add ADBC 1.1 support to the base classes and C interop (#4885)
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 73a67e902 feat(csharp): add ADBC 1.1 support to the base classes and C interop (#4885)
73a67e902 is described below
commit 73a67e9022ac5d259e90abf7802e002737396962
Author: Curt Hagenlocher <cu...@hagenlocher.org>
AuthorDate: Wed Oct 14 20:14:48 2026 -0700
feat(csharp): add ADBC 1.1 support to the base classes and C interop (#4885)
## What's changed
This adds the ADBC 1.1 APIs to the main C# base classes (`AdbcDriver`,
`AdbcDatabase`, `AdbcConnection` and `AdbcStatement`). They also work
across the C boundary in both directions: a managed driver can be
exported as an ADBC 1.1 driver, and an imported 1.1 driver works through
the base classes.
Until now, 1.1 support lived in separate `*11` types that nothing
implemented. Adding the APIs to the existing classes means every driver
gets them without changing its base class. The `*11` types are now
marked `[Obsolete]`.
This is the second of several PRs. The first, #4884, fixed bugs in the
importer and exporter. Async methods with `CancellationToken` support
come next, then the Client changes and wider DuckDB tests.
### Backwards compatibility
Every new member is virtual and has a default implementation, so
existing drivers compile and behave as before:
- The typed setters (`SetOptionInt64`, `SetOptionDouble`) convert the
value to a string and call `SetOption(string, string)`. The typed
getters parse the result of `GetOption(string)`. Binary options throw
`NotImplemented` unless the driver overrides them.
- `GetOption` throws `NotFound` by default. On `AdbcConnection`, it
instead answers the standard options (autocommit, read-only, isolation
level, current catalog and current schema) from the existing
properties.
- `AdbcDriver.Open(IReadOnlyDictionary<string, object>)` used to throw.
It now converts values to strings and calls the string overload, then
applies any binary values through `SetOptionBytes`.
- No existing method gets a new overload that changes how existing calls
bind. That's why typed connection options use a new name,
`ConnectWithTypedOptions`. A `Connect` overload would make existing
`Connect(null)` calls ambiguous.
### New APIs
- **Typed options** at all three levels:
- `SetOptionInt64`, `SetOptionDouble` and `SetOptionBytes`.
- `GetOption`, `GetOptionInt64`, `GetOptionDouble` and
`GetOptionBytes`.
- **`AdbcDatabase.SetOption(string, object)`** sends each value to the
matching typed setter.
- **`AdbcDatabase.ConnectWithTypedOptions`** connects with options that
aren't all strings.
- **`AdbcConnection.CurrentCatalog` and `CurrentDbSchema`** properties.
- **`AdbcException.Details`** for ADBC 1.1 error details.
- `AdbcDriver.DriverVersion` is now documented.
### C interop
- **Exporter:** a managed driver exported through `CAdbcDriverExporter`
now provides the 1.1 driver functions:
- option getters and setters at every level
- `ErrorGetDetailCount` and `ErrorGetDetail`
- `ConnectionGetStatistics` and `ConnectionGetStatisticNames`
- `StatementExecuteSchema`
- `ConnectionCancel` and `StatementCancel`
Options set before `DatabaseInit` or `ConnectionInit` are stored with
their types, and are passed on when the object is created.
`OperationCanceledException` is reported as
`ADBC_STATUS_CANCELLED`.
- **Importer:** an imported driver's 1.1 functions are reachable through
the base classes. For a 1.0 driver, the importer falls back to 1.0
behavior: typed setters become string options, and getters report
`NotFound`. `GetOption` follows the C API's buffer protocol, retrying
with a larger buffer when the first one is too small.
## Testing
- **`BaseClassDefaultsTests`:** the default implementations on the base
classes, using drivers that override only the 1.0 members.
- **`ExportedDriverRoundTrip11Tests`:** a managed 1.1 driver is exported
and imported again, then exercised through the base classes. This
covers:
- typed options at each level, before and after init
- error details, vendor codes, statistics, partitions and updates
- cancellation of a running statement
- the importer's fallbacks for a 1.0 driver
- **AoT interop tests:** the native AoT fixture driver now implements
1.1 typed options and error details, and the tests use them through
the C API.
- **Results:**
- The full solution builds without warnings, and
`dotnet format --verify-no-changes` is clean.
- The core test project passes on net10.0, net8.0 and net472.
- The AoT interop tests pass against a fixture published from this
branch.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01WWRrCH8uXJhAvRpEHWeadU
---------
Co-authored-by: Claude Opus 5.5 <no...@anthropic.com>
---
csharp/src/Apache.Arrow.Adbc/AdbcConnection.cs | 155 +++
csharp/src/Apache.Arrow.Adbc/AdbcConnection11.cs | 1 +
csharp/src/Apache.Arrow.Adbc/AdbcDatabase.cs | 174 ++-
csharp/src/Apache.Arrow.Adbc/AdbcDatabase11.cs | 1 +
csharp/src/Apache.Arrow.Adbc/AdbcDriver.cs | 45 +-
csharp/src/Apache.Arrow.Adbc/AdbcDriver11.cs | 1 +
csharp/src/Apache.Arrow.Adbc/AdbcException.cs | 14 +
csharp/src/Apache.Arrow.Adbc/AdbcOptionValue.cs | 139 +++
csharp/src/Apache.Arrow.Adbc/AdbcStatement.cs | 94 ++
csharp/src/Apache.Arrow.Adbc/AdbcStatement11.cs | 1 +
.../src/Apache.Arrow.Adbc/C/CAdbcDriverExporter.cs | 1158 +++++++++++++++++---
.../C/CAdbcDriverImporter.Options.cs | 312 ++++++
.../src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs | 633 ++++++++---
csharp/src/Apache.Arrow.Adbc/C/Delegates.cs | 9 +
.../FixtureDriver.cs | 35 +
.../AotFixtureTests.cs | 45 +
.../BaseClassDefaultsTests.cs | 278 +++++
.../ExportedDriverRoundTrip11Tests.cs | 691 ++++++++++++
18 files changed, 3513 insertions(+), 273 deletions(-)
diff --git a/csharp/src/Apache.Arrow.Adbc/AdbcConnection.cs b/csharp/src/Apache.Arrow.Adbc/AdbcConnection.cs
index f1a110b09..785d508b0 100644
--- a/csharp/src/Apache.Arrow.Adbc/AdbcConnection.cs
+++ b/csharp/src/Apache.Arrow.Adbc/AdbcConnection.cs
@@ -185,6 +185,135 @@ namespace Apache.Arrow.Adbc
throw AdbcException.NotImplemented("Connection does not support setting options");
}
+ /// <summary>
+ /// Sets an integer-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <param name="value">Option value</param>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. The default implementation converts the value to
+ /// a string and calls <see cref="SetOption(string, string)"/>.
+ /// </remarks>
+ public virtual void SetOptionInt64(string key, long value)
+ {
+ SetOption(key, AdbcOptionValue.FormatInt64(value));
+ }
+
+ /// <summary>
+ /// Sets a floating-point-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <param name="value">Option value</param>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. The default implementation converts the value to
+ /// a string and calls <see cref="SetOption(string, string)"/>.
+ /// </remarks>
+ public virtual void SetOptionDouble(string key, double value)
+ {
+ SetOption(key, AdbcOptionValue.FormatDouble(value));
+ }
+
+ /// <summary>
+ /// Sets a binary-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <param name="value">Option value. Implementations must copy the data if they retain it.</param>
+ /// <remarks>Added in ADBC 1.1.0.</remarks>
+ public virtual void SetOptionBytes(string key, ReadOnlySpan<byte> value)
+ {
+ throw AdbcException.NotImplemented("Connection does not support binary options");
+ }
+
+ /// <summary>
+ /// Gets the value of a string-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <returns>The option value.</returns>
+ /// <exception cref="AdbcException">
+ /// With <see cref="AdbcStatusCode.NotFound"/> if the option is not recognized or has no value.
+ /// </exception>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. The default implementation answers the standard
+ /// options defined in <see cref="AdbcOptions.Connection"/> from the
+ /// <see cref="AutoCommit"/>, <see cref="ReadOnly"/>, <see cref="IsolationLevel"/>,
+ /// <see cref="CurrentCatalog"/> and <see cref="CurrentDbSchema"/> properties.
+ /// Drivers that override it should call the base implementation for keys they
+ /// don't recognize.
+ /// </remarks>
+ public virtual string GetOption(string key)
+ {
+ switch (key)
+ {
+ case AdbcOptions.Connection.Autocommit:
+ return GetPropertyOption(key, () => AdbcOptions.GetEnabled(AutoCommit));
+ case AdbcOptions.Connection.ReadOnly:
+ return GetPropertyOption(key, () => AdbcOptions.GetEnabled(ReadOnly));
+ case AdbcOptions.Connection.IsolationLevel:
+ return GetPropertyOption(key, () => AdbcOptions.GetIsolationLevel(IsolationLevel));
+ case AdbcOptions.Connection.CurrentCatalog:
+ return GetPropertyOption(key, () => CurrentCatalog);
+ case AdbcOptions.Connection.CurrentDbSchema:
+ return GetPropertyOption(key, () => CurrentDbSchema);
+ default:
+ throw AdbcOptionValue.NotFound(key);
+ }
+ }
+
+ private static string GetPropertyOption(string key, Func<string?> getter)
+ {
+ string? value;
+ try
+ {
+ value = getter();
+ }
+ catch (AdbcException e) when (e.Status == AdbcStatusCode.NotImplemented)
+ {
+ // The driver doesn't override the property, or has no value for it.
+ // GetOption reports that as NotFound.
+ throw AdbcOptionValue.NotFound(key);
+ }
+ return value ?? throw AdbcOptionValue.NotFound(key);
+ }
+
+ /// <summary>
+ /// Gets the value of an integer-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <returns>The option value.</returns>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. The default implementation parses the result of
+ /// <see cref="GetOption(string)"/>.
+ /// </remarks>
+ public virtual long GetOptionInt64(string key)
+ {
+ return AdbcOptionValue.ParseInt64(key, GetOption(key));
+ }
+
+ /// <summary>
+ /// Gets the value of a floating-point-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <returns>The option value.</returns>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. The default implementation parses the result of
+ /// <see cref="GetOption(string)"/>.
+ /// </remarks>
+ public virtual double GetOptionDouble(string key)
+ {
+ return AdbcOptionValue.ParseDouble(key, GetOption(key));
+ }
+
+ /// <summary>
+ /// Gets the value of a binary-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <returns>The option value.</returns>
+ /// <remarks>Added in ADBC 1.1.0.</remarks>
+ public virtual byte[] GetOptionBytes(string key)
+ {
+ throw AdbcOptionValue.NotFound(key);
+ }
+
/// <summary>
/// Create a result set from a serialized PartitionDescriptor.
/// </summary>
@@ -231,6 +360,32 @@ namespace Apache.Arrow.Adbc
set => throw AdbcException.NotImplemented("Connection does not support setting isolation level");
}
+ /// <summary>
+ /// Gets or sets the current catalog.
+ /// </summary>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. The default setter calls <see cref="SetOption(string, string)"/>
+ /// with <see cref="AdbcOptions.Connection.CurrentCatalog"/>.
+ /// </remarks>
+ public virtual string? CurrentCatalog
+ {
+ get => throw AdbcException.NotImplemented("Connection does not support getting the current catalog");
+ set => SetOption(AdbcOptions.Connection.CurrentCatalog, value ?? throw new ArgumentNullException(nameof(value)));
+ }
+
+ /// <summary>
+ /// Gets or sets the current database schema.
+ /// </summary>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. The default setter calls <see cref="SetOption(string, string)"/>
+ /// with <see cref="AdbcOptions.Connection.CurrentDbSchema"/>.
+ /// </remarks>
+ public virtual string? CurrentDbSchema
+ {
+ get => throw AdbcException.NotImplemented("Connection does not support getting the current database schema");
+ set => SetOption(AdbcOptions.Connection.CurrentDbSchema, value ?? throw new ArgumentNullException(nameof(value)));
+ }
+
/// <summary>
/// Attempts to cancel an in-progress operation on a connection.
/// </summary>
diff --git a/csharp/src/Apache.Arrow.Adbc/AdbcConnection11.cs b/csharp/src/Apache.Arrow.Adbc/AdbcConnection11.cs
index f7f3f87ea..e0f27fdbf 100644
--- a/csharp/src/Apache.Arrow.Adbc/AdbcConnection11.cs
+++ b/csharp/src/Apache.Arrow.Adbc/AdbcConnection11.cs
@@ -27,6 +27,7 @@ namespace Apache.Arrow.Adbc
/// Provides methods for query execution, managing prepared statements,
/// using transactions, and so on.
/// </summary>
+ [Obsolete("Use AdbcConnection instead. The ADBC 1.1 APIs have been added to AdbcConnection in a backwards-compatible way.")]
public abstract class AdbcConnection11 : IDisposable
#if NET5_0_OR_GREATER
, IAsyncDisposable
diff --git a/csharp/src/Apache.Arrow.Adbc/AdbcDatabase.cs b/csharp/src/Apache.Arrow.Adbc/AdbcDatabase.cs
index be743d151..d5f11b0a7 100644
--- a/csharp/src/Apache.Arrow.Adbc/AdbcDatabase.cs
+++ b/csharp/src/Apache.Arrow.Adbc/AdbcDatabase.cs
@@ -37,7 +37,7 @@ namespace Apache.Arrow.Adbc
/// <param name="value">Option value</param>
public virtual void SetOption(string key, string value)
{
- throw AdbcException.NotImplemented("Connection does not support setting options");
+ throw AdbcException.NotImplemented("Database does not support setting options");
}
/// <summary>
@@ -45,10 +45,122 @@ namespace Apache.Arrow.Adbc
/// support setting options after opening as well.
/// </summary>
/// <param name="key">Option name</param>
- /// <param name="value">Option value</param>
+ /// <param name="value">
+ /// Option value. Strings and booleans are passed to <see cref="SetOption(string, string)"/>,
+ /// integers to <see cref="SetOptionInt64"/>, floating-point numbers to
+ /// <see cref="SetOptionDouble"/> and byte arrays to <see cref="SetOptionBytes"/>.
+ /// </param>
public virtual void SetOption(string key, object value)
{
- throw AdbcException.NotImplemented("Connection does not support setting options");
+ if (AdbcOptionValue.TryGetInt64(value, out long l))
+ {
+ SetOptionInt64(key, l);
+ }
+ else if (AdbcOptionValue.TryGetDouble(value, out double d))
+ {
+ SetOptionDouble(key, d);
+ }
+ else if (AdbcOptionValue.TryGetBytes(value, out ReadOnlyMemory<byte> bytes))
+ {
+ SetOptionBytes(key, bytes.Span);
+ }
+ else
+ {
+ // Strings and booleans; throws for anything else
+ SetOption(key, AdbcOptionValue.ToOptionString(key, value));
+ }
+ }
+
+ /// <summary>
+ /// Sets an integer-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <param name="value">Option value</param>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. The default implementation converts the value to
+ /// a string and calls <see cref="SetOption(string, string)"/>.
+ /// </remarks>
+ public virtual void SetOptionInt64(string key, long value)
+ {
+ SetOption(key, AdbcOptionValue.FormatInt64(value));
+ }
+
+ /// <summary>
+ /// Sets a floating-point-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <param name="value">Option value</param>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. The default implementation converts the value to
+ /// a string and calls <see cref="SetOption(string, string)"/>.
+ /// </remarks>
+ public virtual void SetOptionDouble(string key, double value)
+ {
+ SetOption(key, AdbcOptionValue.FormatDouble(value));
+ }
+
+ /// <summary>
+ /// Sets a binary-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <param name="value">Option value. Implementations must copy the data if they retain it.</param>
+ /// <remarks>Added in ADBC 1.1.0.</remarks>
+ public virtual void SetOptionBytes(string key, ReadOnlySpan<byte> value)
+ {
+ throw AdbcException.NotImplemented("Database does not support binary options");
+ }
+
+ /// <summary>
+ /// Gets the value of a string-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <returns>The option value.</returns>
+ /// <exception cref="AdbcException">
+ /// With <see cref="AdbcStatusCode.NotFound"/> if the option is not recognized or has no value.
+ /// </exception>
+ /// <remarks>Added in ADBC 1.1.0.</remarks>
+ public virtual string GetOption(string key)
+ {
+ throw AdbcOptionValue.NotFound(key);
+ }
+
+ /// <summary>
+ /// Gets the value of an integer-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <returns>The option value.</returns>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. The default implementation parses the result of
+ /// <see cref="GetOption(string)"/>.
+ /// </remarks>
+ public virtual long GetOptionInt64(string key)
+ {
+ return AdbcOptionValue.ParseInt64(key, GetOption(key));
+ }
+
+ /// <summary>
+ /// Gets the value of a floating-point-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <returns>The option value.</returns>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. The default implementation parses the result of
+ /// <see cref="GetOption(string)"/>.
+ /// </remarks>
+ public virtual double GetOptionDouble(string key)
+ {
+ return AdbcOptionValue.ParseDouble(key, GetOption(key));
+ }
+
+ /// <summary>
+ /// Gets the value of a binary-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <returns>The option value.</returns>
+ /// <remarks>Added in ADBC 1.1.0.</remarks>
+ public virtual byte[] GetOptionBytes(string key)
+ {
+ throw AdbcOptionValue.NotFound(key);
}
/// <summary>
@@ -59,6 +171,62 @@ namespace Apache.Arrow.Adbc
/// </param>
public abstract AdbcConnection Connect(IReadOnlyDictionary<string, string>? options);
+ /// <summary>
+ /// Create a new connection to the database using options that aren't all strings.
+ /// </summary>
+ /// <param name="options">
+ /// Additional options to use when connecting. Values may be strings, booleans,
+ /// integers, floating-point numbers, byte arrays or <see cref="ReadOnlyMemory{T}"/>
+ /// of bytes.
+ /// </param>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. This isn't an overload of
+ /// <see cref="Connect(IReadOnlyDictionary{string, string})"/> because that would
+ /// make existing calls of the form <c>Connect(null)</c> ambiguous.
+ ///
+ /// The default implementation converts the non-binary values to strings and calls
+ /// <see cref="Connect(IReadOnlyDictionary{string, string})"/>, then applies binary
+ /// values to the new connection through <see cref="AdbcConnection.SetOptionBytes"/>.
+ /// Drivers that need binary options while connecting should override it.
+ /// </remarks>
+ public virtual AdbcConnection ConnectWithTypedOptions(IReadOnlyDictionary<string, object>? options)
+ {
+ if (options == null)
+ {
+ return Connect(null);
+ }
+
+ Dictionary<string, string> stringOptions = new Dictionary<string, string>(options.Count);
+ List<KeyValuePair<string, ReadOnlyMemory<byte>>> binaryOptions = new List<KeyValuePair<string, ReadOnlyMemory<byte>>>();
+ foreach (KeyValuePair<string, object> pair in options)
+ {
+ if (AdbcOptionValue.TryGetBytes(pair.Value, out ReadOnlyMemory<byte> bytes))
+ {
+ binaryOptions.Add(new KeyValuePair<string, ReadOnlyMemory<byte>>(pair.Key, bytes));
+ }
+ else
+ {
+ stringOptions[pair.Key] = AdbcOptionValue.ToOptionString(pair.Key, pair.Value);
+ }
+ }
+
+ AdbcConnection connection = Connect(stringOptions);
+ try
+ {
+ foreach (KeyValuePair<string, ReadOnlyMemory<byte>> pair in binaryOptions)
+ {
+ connection.SetOptionBytes(pair.Key, pair.Value.Span);
+ }
+ }
+ catch
+ {
+ connection.Dispose();
+ throw;
+ }
+
+ return connection;
+ }
+
public virtual void Dispose() { }
}
}
diff --git a/csharp/src/Apache.Arrow.Adbc/AdbcDatabase11.cs b/csharp/src/Apache.Arrow.Adbc/AdbcDatabase11.cs
index 5d8ae6219..6e9dc4eed 100644
--- a/csharp/src/Apache.Arrow.Adbc/AdbcDatabase11.cs
+++ b/csharp/src/Apache.Arrow.Adbc/AdbcDatabase11.cs
@@ -28,6 +28,7 @@ namespace Apache.Arrow.Adbc
/// For example, in-memory databases can place ownership of the actual
/// database in this object.
/// </summary>
+ [Obsolete("Use AdbcDatabase instead. The ADBC 1.1 APIs have been added to AdbcDatabase in a backwards-compatible way.")]
public abstract class AdbcDatabase11 : IDisposable
{
~AdbcDatabase11() => Dispose(false);
diff --git a/csharp/src/Apache.Arrow.Adbc/AdbcDriver.cs b/csharp/src/Apache.Arrow.Adbc/AdbcDriver.cs
index 9ff666e3e..f07314787 100644
--- a/csharp/src/Apache.Arrow.Adbc/AdbcDriver.cs
+++ b/csharp/src/Apache.Arrow.Adbc/AdbcDriver.cs
@@ -26,6 +26,10 @@ namespace Apache.Arrow.Adbc
/// </summary>
public abstract class AdbcDriver : IDisposable
{
+ /// <summary>
+ /// Gets the version of the ADBC specification implemented by this driver,
+ /// as one of the constants defined by <see cref="AdbcVersion"/>.
+ /// </summary>
public virtual int DriverVersion => AdbcVersion.Version_1_0_0;
/// <summary>
@@ -40,11 +44,48 @@ namespace Apache.Arrow.Adbc
/// Open a database via this driver.
/// </summary>
/// <param name="parameters">
- /// Driver-specific parameters.
+ /// Driver-specific parameters. Values may be strings, booleans, integers,
+ /// floating-point numbers, byte arrays or <see cref="ReadOnlyMemory{T}"/> of bytes.
/// </param>
+ /// <remarks>
+ /// The default implementation converts the non-binary values to strings and calls
+ /// <see cref="Open(IReadOnlyDictionary{string, string})"/>, then applies binary values
+ /// to the new database through <see cref="AdbcDatabase.SetOptionBytes"/>. Drivers that
+ /// need binary options while opening should override this method.
+ /// </remarks>
public virtual AdbcDatabase Open(IReadOnlyDictionary<string, object> parameters)
{
- throw AdbcException.NotImplemented("Driver does not support non-string parameters");
+ if (parameters == null) throw new ArgumentNullException(nameof(parameters));
+
+ Dictionary<string, string> stringParameters = new Dictionary<string, string>(parameters.Count);
+ List<KeyValuePair<string, ReadOnlyMemory<byte>>> binaryParameters = new List<KeyValuePair<string, ReadOnlyMemory<byte>>>();
+ foreach (KeyValuePair<string, object> pair in parameters)
+ {
+ if (AdbcOptionValue.TryGetBytes(pair.Value, out ReadOnlyMemory<byte> bytes))
+ {
+ binaryParameters.Add(new KeyValuePair<string, ReadOnlyMemory<byte>>(pair.Key, bytes));
+ }
+ else
+ {
+ stringParameters[pair.Key] = AdbcOptionValue.ToOptionString(pair.Key, pair.Value);
+ }
+ }
+
+ AdbcDatabase database = Open(stringParameters);
+ try
+ {
+ foreach (KeyValuePair<string, ReadOnlyMemory<byte>> pair in binaryParameters)
+ {
+ database.SetOptionBytes(pair.Key, pair.Value.Span);
+ }
+ }
+ catch
+ {
+ database.Dispose();
+ throw;
+ }
+
+ return database;
}
public virtual void Dispose()
diff --git a/csharp/src/Apache.Arrow.Adbc/AdbcDriver11.cs b/csharp/src/Apache.Arrow.Adbc/AdbcDriver11.cs
index a36d61826..bf830ea83 100644
--- a/csharp/src/Apache.Arrow.Adbc/AdbcDriver11.cs
+++ b/csharp/src/Apache.Arrow.Adbc/AdbcDriver11.cs
@@ -24,6 +24,7 @@ namespace Apache.Arrow.Adbc
/// This provides a common interface for vendor-specific driver
/// initialization routines.
/// </summary>
+ [Obsolete("Use AdbcDriver instead. The ADBC 1.1 APIs have been added to AdbcDriver in a backwards-compatible way.")]
public abstract class AdbcDriver11 : IDisposable
{
~AdbcDriver11() => Dispose(false);
diff --git a/csharp/src/Apache.Arrow.Adbc/AdbcException.cs b/csharp/src/Apache.Arrow.Adbc/AdbcException.cs
index 885262e4a..81052883b 100644
--- a/csharp/src/Apache.Arrow.Adbc/AdbcException.cs
+++ b/csharp/src/Apache.Arrow.Adbc/AdbcException.cs
@@ -16,6 +16,7 @@
*/
using System;
+using System.Collections.Generic;
namespace Apache.Arrow.Adbc
{
@@ -92,5 +93,18 @@ namespace Apache.Arrow.Adbc
{
get => 0;
}
+
+ /// <summary>
+ /// Gets additional driver-specific details about the error, as
+ /// key-value pairs. The values are arbitrary binary data.
+ /// </summary>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. These are surfaced across the C API via
+ /// <c>AdbcErrorGetDetailCount</c> and <c>AdbcErrorGetDetail</c>.
+ /// </remarks>
+ public virtual IReadOnlyList<KeyValuePair<string, byte[]>> Details
+ {
+ get => System.Array.Empty<KeyValuePair<string, byte[]>>();
+ }
}
}
diff --git a/csharp/src/Apache.Arrow.Adbc/AdbcOptionValue.cs b/csharp/src/Apache.Arrow.Adbc/AdbcOptionValue.cs
new file mode 100644
index 000000000..895a25d6a
--- /dev/null
+++ b/csharp/src/Apache.Arrow.Adbc/AdbcOptionValue.cs
@@ -0,0 +1,139 @@
+/*
+ * 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.
+ */
+
+using System;
+using System.Globalization;
+
+namespace Apache.Arrow.Adbc
+{
+ /// <summary>
+ /// Conversions shared by the default implementations of the typed option
+ /// accessors on <see cref="AdbcDatabase"/>, <see cref="AdbcConnection"/>
+ /// and <see cref="AdbcStatement"/>.
+ /// </summary>
+ internal static class AdbcOptionValue
+ {
+ /// <summary>
+ /// Converts a value supplied through an <c>object</c>-typed API into the
+ /// string form used by <c>SetOption(string, string)</c>.
+ /// </summary>
+ public static string ToOptionString(string key, object? value)
+ {
+ switch (value)
+ {
+ case string s: return s;
+ case bool b: return AdbcOptions.GetEnabled(b);
+ case long l: return FormatInt64(l);
+ case int i: return FormatInt64(i);
+ case short s16: return FormatInt64(s16);
+ case sbyte s8: return FormatInt64(s8);
+ case uint u32: return FormatInt64(u32);
+ case ushort u16: return FormatInt64(u16);
+ case byte u8: return FormatInt64(u8);
+ case double d: return FormatDouble(d);
+ case float f: return FormatDouble(f);
+ case null: throw new ArgumentNullException(nameof(value), $"Value for option '{key}' must not be null");
+ default:
+ throw new AdbcException(
+ $"Value of type {value.GetType().Name} for option '{key}' cannot be converted to a string",
+ AdbcStatusCode.InvalidArgument);
+ }
+ }
+
+ /// <summary>
+ /// Gets the bytes of a binary value supplied through an <c>object</c>-typed API.
+ /// </summary>
+ public static bool TryGetBytes(object? value, out ReadOnlyMemory<byte> bytes)
+ {
+ switch (value)
+ {
+ case byte[] array: bytes = array; return true;
+ case ReadOnlyMemory<byte> memory: bytes = memory; return true;
+ case Memory<byte> memory: bytes = memory; return true;
+ case ArraySegment<byte> segment: bytes = segment; return true;
+ default: bytes = default; return false;
+ }
+ }
+
+ /// <summary>
+ /// Gets an integer value supplied through an <c>object</c>-typed API.
+ /// </summary>
+ public static bool TryGetInt64(object? value, out long result)
+ {
+ switch (value)
+ {
+ case long l: result = l; return true;
+ case int i: result = i; return true;
+ case short s16: result = s16; return true;
+ case sbyte s8: result = s8; return true;
+ case uint u32: result = u32; return true;
+ case ushort u16: result = u16; return true;
+ case byte u8: result = u8; return true;
+ default: result = 0; return false;
+ }
+ }
+
+ /// <summary>
+ /// Gets a floating-point value supplied through an <c>object</c>-typed API.
+ /// </summary>
+ public static bool TryGetDouble(object? value, out double result)
+ {
+ switch (value)
+ {
+ case double d: result = d; return true;
+ case float f: result = f; return true;
+ default: result = 0; return false;
+ }
+ }
+
+ public static string FormatInt64(long value) => value.ToString(CultureInfo.InvariantCulture);
+
+ public static string FormatDouble(double value)
+ {
+ string result = value.ToString("R", CultureInfo.InvariantCulture);
+
+ // On .NET Framework, "R" doesn't always round-trip; "G17" always does, but
+ // gives longer strings for values like 0.1, so it's only used when needed.
+ if (!value.Equals(double.Parse(result, NumberStyles.Float, CultureInfo.InvariantCulture)))
+ {
+ result = value.ToString("G17", CultureInfo.InvariantCulture);
+ }
+ return result;
+ }
+
+ public static long ParseInt64(string key, string value)
+ {
+ if (long.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out long result))
+ {
+ return result;
+ }
+ throw new AdbcException($"Value of option '{key}' is not an integer", AdbcStatusCode.InvalidArgument);
+ }
+
+ public static double ParseDouble(string key, string value)
+ {
+ if (double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out double result))
+ {
+ return result;
+ }
+ throw new AdbcException($"Value of option '{key}' is not a number", AdbcStatusCode.InvalidArgument);
+ }
+
+ public static AdbcException NotFound(string key) =>
+ new AdbcException($"Option '{key}' is not recognized or has no value", AdbcStatusCode.NotFound);
+ }
+}
diff --git a/csharp/src/Apache.Arrow.Adbc/AdbcStatement.cs b/csharp/src/Apache.Arrow.Adbc/AdbcStatement.cs
index 98eec4438..09866e3fb 100644
--- a/csharp/src/Apache.Arrow.Adbc/AdbcStatement.cs
+++ b/csharp/src/Apache.Arrow.Adbc/AdbcStatement.cs
@@ -151,6 +151,100 @@ namespace Apache.Arrow.Adbc
throw AdbcException.NotImplemented("Statement does not support setting options");
}
+ /// <summary>
+ /// Sets an integer-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <param name="value">Option value</param>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. The default implementation converts the value to
+ /// a string and calls <see cref="SetOption(string, string)"/>.
+ /// </remarks>
+ public virtual void SetOptionInt64(string key, long value)
+ {
+ SetOption(key, AdbcOptionValue.FormatInt64(value));
+ }
+
+ /// <summary>
+ /// Sets a floating-point-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <param name="value">Option value</param>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. The default implementation converts the value to
+ /// a string and calls <see cref="SetOption(string, string)"/>.
+ /// </remarks>
+ public virtual void SetOptionDouble(string key, double value)
+ {
+ SetOption(key, AdbcOptionValue.FormatDouble(value));
+ }
+
+ /// <summary>
+ /// Sets a binary-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <param name="value">Option value. Implementations must copy the data if they retain it.</param>
+ /// <remarks>Added in ADBC 1.1.0.</remarks>
+ public virtual void SetOptionBytes(string key, ReadOnlySpan<byte> value)
+ {
+ throw AdbcException.NotImplemented("Statement does not support binary options");
+ }
+
+ /// <summary>
+ /// Gets the value of a string-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <returns>The option value.</returns>
+ /// <exception cref="AdbcException">
+ /// With <see cref="AdbcStatusCode.NotFound"/> if the option is not recognized or has no value.
+ /// </exception>
+ /// <remarks>Added in ADBC 1.1.0.</remarks>
+ public virtual string GetOption(string key)
+ {
+ throw AdbcOptionValue.NotFound(key);
+ }
+
+ /// <summary>
+ /// Gets the value of an integer-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <returns>The option value.</returns>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. The default implementation parses the result of
+ /// <see cref="GetOption(string)"/>.
+ /// </remarks>
+ public virtual long GetOptionInt64(string key)
+ {
+ return AdbcOptionValue.ParseInt64(key, GetOption(key));
+ }
+
+ /// <summary>
+ /// Gets the value of a floating-point-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <returns>The option value.</returns>
+ /// <remarks>
+ /// Added in ADBC 1.1.0. The default implementation parses the result of
+ /// <see cref="GetOption(string)"/>. Drivers use this to report
+ /// <see cref="AdbcOptions.Statement.Progress"/> and
+ /// <see cref="AdbcOptions.Statement.MaxProgress"/>.
+ /// </remarks>
+ public virtual double GetOptionDouble(string key)
+ {
+ return AdbcOptionValue.ParseDouble(key, GetOption(key));
+ }
+
+ /// <summary>
+ /// Gets the value of a binary-valued option.
+ /// </summary>
+ /// <param name="key">Option name</param>
+ /// <returns>The option value.</returns>
+ /// <remarks>Added in ADBC 1.1.0.</remarks>
+ public virtual byte[] GetOptionBytes(string key)
+ {
+ throw AdbcOptionValue.NotFound(key);
+ }
+
public virtual void Dispose()
{
}
diff --git a/csharp/src/Apache.Arrow.Adbc/AdbcStatement11.cs b/csharp/src/Apache.Arrow.Adbc/AdbcStatement11.cs
index 1286ba244..bf245e643 100644
--- a/csharp/src/Apache.Arrow.Adbc/AdbcStatement11.cs
+++ b/csharp/src/Apache.Arrow.Adbc/AdbcStatement11.cs
@@ -27,6 +27,7 @@ namespace Apache.Arrow.Adbc
/// may be used multiple times and can be reconfigured (e.g. they can
/// be reused to execute multiple different queries).
/// </summary>
+ [Obsolete("Use AdbcStatement instead. The ADBC 1.1 APIs have been added to AdbcStatement in a backwards-compatible way.")]
public abstract class AdbcStatement11 : IDisposable
#if NET5_0_OR_GREATER
, IAsyncDisposable
diff --git a/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverExporter.cs b/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverExporter.cs
index 9ea257a76..8edd66a25 100644
--- a/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverExporter.cs
+++ b/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverExporter.cs
@@ -19,14 +19,31 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
+using System.Text;
using Apache.Arrow.Adbc.Extensions;
using Apache.Arrow.C;
using Apache.Arrow.Ipc;
namespace Apache.Arrow.Adbc.C
{
+ /// <summary>
+ /// Exposes a managed <see cref="AdbcDriver"/> through the ADBC C API.
+ /// </summary>
+ /// <remarks>
+ /// When a caller requests ADBC 1.1.0, every 1.1.0 entry point is populated
+ /// except <c>ErrorFromArrayStream</c>; exported streams report errors through
+ /// <c>get_last_error</c> instead. Functionality that the managed driver doesn't
+ /// override surfaces as <see cref="AdbcStatusCode.NotImplemented"/>, which is
+ /// the same status a driver manager reports for a missing entry point.
+ /// </remarks>
public class CAdbcDriverExporter
{
+ /// <summary>
+ /// The value of <c>ADBC_ERROR_VENDOR_CODE_PRIVATE_DATA</c>. A caller sets an error's
+ /// vendor code to this value to indicate that it supports the ADBC 1.1.0 error layout.
+ /// </summary>
+ internal const int ErrorVendorCodePrivateData = int.MinValue;
+
private static unsafe readonly NativeDelegate<ErrorRelease> s_releaseError = new NativeDelegate<ErrorRelease>(ReleaseError);
#if NET5_0_OR_GREATER
@@ -62,8 +79,37 @@ namespace Apache.Arrow.Adbc.C
private static unsafe delegate* unmanaged<CAdbcStatement*, byte*, int, CAdbcError*, AdbcStatusCode> StatementSetSubstraitPlanPtr => &SetStatementSubstraitPlan;
private static unsafe delegate* unmanaged<CAdbcStatement*, CArrowSchema*, CAdbcError*, AdbcStatusCode> StatementGetParameterSchemaPtr => &GetStatementParameterSchema;
+ // ADBC 1.1.0
+ private static unsafe delegate* unmanaged<CAdbcError*, int> ErrorGetDetailCountPtr => &GetErrorDetailCount;
+ private static unsafe delegate* unmanaged<CAdbcError*, int, CAdbcErrorDetail> ErrorGetDetailPtr => &GetErrorDetail;
+
+ private static unsafe delegate* unmanaged<CAdbcDatabase*, byte*, byte*, nint*, CAdbcError*, AdbcStatusCode> DatabaseGetOptionPtr => &GetDatabaseOption;
+ private static unsafe delegate* unmanaged<CAdbcDatabase*, byte*, byte*, nint*, CAdbcError*, AdbcStatusCode> DatabaseGetOptionBytesPtr => &GetDatabaseOptionBytes;
+ private static unsafe delegate* unmanaged<CAdbcDatabase*, byte*, double*, CAdbcError*, AdbcStatusCode> DatabaseGetOptionDoublePtr => &GetDatabaseOptionDouble;
+ private static unsafe delegate* unmanaged<CAdbcDatabase*, byte*, long*, CAdbcError*, AdbcStatusCode> DatabaseGetOptionIntPtr => &GetDatabaseOptionInt;
+ private static unsafe delegate* unmanaged<CAdbcDatabase*, byte*, byte*, nint, CAdbcError*, AdbcStatusCode> DatabaseSetOptionBytesPtr => &SetDatabaseOptionBytes;
+ private static unsafe delegate* unmanaged<CAdbcDatabase*, byte*, double, CAdbcError*, AdbcStatusCode> DatabaseSetOptionDoublePtr => &SetDatabaseOptionDouble;
+ private static unsafe delegate* unmanaged<CAdbcDatabase*, byte*, long, CAdbcError*, AdbcStatusCode> DatabaseSetOptionIntPtr => &SetDatabaseOptionInt;
+
private static unsafe delegate* unmanaged<CAdbcConnection*, CAdbcError*, AdbcStatusCode> ConnectionCancelPtr => &CancelConnection;
+ private static unsafe delegate* unmanaged<CAdbcConnection*, byte*, byte*, nint*, CAdbcError*, AdbcStatusCode> ConnectionGetOptionPtr => &GetConnectionOption;
+ private static unsafe delegate* unmanaged<CAdbcConnection*, byte*, byte*, nint*, CAdbcError*, AdbcStatusCode> ConnectionGetOptionBytesPtr => &GetConnectionOptionBytes;
+ private static unsafe delegate* unmanaged<CAdbcConnection*, byte*, double*, CAdbcError*, AdbcStatusCode> ConnectionGetOptionDoublePtr => &GetConnectionOptionDouble;
+ private static unsafe delegate* unmanaged<CAdbcConnection*, byte*, long*, CAdbcError*, AdbcStatusCode> ConnectionGetOptionIntPtr => &GetConnectionOptionInt;
+ private static unsafe delegate* unmanaged<CAdbcConnection*, byte*, byte*, byte*, byte, CArrowArrayStream*, CAdbcError*, AdbcStatusCode> ConnectionGetStatisticsPtr => &GetConnectionStatistics;
+ private static unsafe delegate* unmanaged<CAdbcConnection*, CArrowArrayStream*, CAdbcError*, AdbcStatusCode> ConnectionGetStatisticNamesPtr => &GetConnectionStatisticNames;
+ private static unsafe delegate* unmanaged<CAdbcConnection*, byte*, byte*, nint, CAdbcError*, AdbcStatusCode> ConnectionSetOptionBytesPtr => &SetConnectionOptionBytes;
+ private static unsafe delegate* unmanaged<CAdbcConnection*, byte*, double, CAdbcError*, AdbcStatusCode> ConnectionSetOptionDoublePtr => &SetConnectionOptionDouble;
+ private static unsafe delegate* unmanaged<CAdbcConnection*, byte*, long, CAdbcError*, AdbcStatusCode> ConnectionSetOptionIntPtr => &SetConnectionOptionInt;
+
private static unsafe delegate* unmanaged<CAdbcStatement*, CAdbcError*, AdbcStatusCode> StatementCancelPtr => &CancelStatement;
+ private static unsafe delegate* unmanaged<CAdbcStatement*, byte*, byte*, nint*, CAdbcError*, AdbcStatusCode> StatementGetOptionPtr => &GetStatementOption;
+ private static unsafe delegate* unmanaged<CAdbcStatement*, byte*, byte*, nint*, CAdbcError*, AdbcStatusCode> StatementGetOptionBytesPtr => &GetStatementOptionBytes;
+ private static unsafe delegate* unmanaged<CAdbcStatement*, byte*, double*, CAdbcError*, AdbcStatusCode> StatementGetOptionDoublePtr => &GetStatementOptionDouble;
+ private static unsafe delegate* unmanaged<CAdbcStatement*, byte*, long*, CAdbcError*, AdbcStatusCode> StatementGetOptionIntPtr => &GetStatementOptionInt;
+ private static unsafe delegate* unmanaged<CAdbcStatement*, byte*, byte*, nint, CAdbcError*, AdbcStatusCode> StatementSetOptionBytesPtr => &SetStatementOptionBytes;
+ private static unsafe delegate* unmanaged<CAdbcStatement*, byte*, double, CAdbcError*, AdbcStatusCode> StatementSetOptionDoublePtr => &SetStatementOptionDouble;
+ private static unsafe delegate* unmanaged<CAdbcStatement*, byte*, long, CAdbcError*, AdbcStatusCode> StatementSetOptionIntPtr => &SetStatementOptionInt;
#else
internal static unsafe IntPtr ReleaseErrorPtr => s_releaseError.Pointer;
private static unsafe IntPtr ReleaseDriverPtr = NativeDelegate<DriverRelease>.AsNativePointer(ReleaseDriver);
@@ -97,8 +143,37 @@ namespace Apache.Arrow.Adbc.C
private static unsafe IntPtr StatementSetSubstraitPlanPtr = NativeDelegate<StatementSetSubstraitPlan>.AsNativePointer(SetStatementSubstraitPlan);
private static unsafe IntPtr StatementGetParameterSchemaPtr = NativeDelegate<StatementGetParameterSchema>.AsNativePointer(GetStatementParameterSchema);
+ // ADBC 1.1.0
+ private static unsafe IntPtr ErrorGetDetailCountPtr = NativeDelegate<ErrorGetDetailCount>.AsNativePointer(GetErrorDetailCount);
+ private static unsafe IntPtr ErrorGetDetailPtr = NativeDelegate<ErrorGetDetail>.AsNativePointer(GetErrorDetail);
+
+ private static unsafe IntPtr DatabaseGetOptionPtr = NativeDelegate<DatabaseGetOption>.AsNativePointer(GetDatabaseOption);
+ private static unsafe IntPtr DatabaseGetOptionBytesPtr = NativeDelegate<DatabaseGetOptionBytes>.AsNativePointer(GetDatabaseOptionBytes);
+ private static unsafe IntPtr DatabaseGetOptionDoublePtr = NativeDelegate<DatabaseGetOptionDouble>.AsNativePointer(GetDatabaseOptionDouble);
+ private static unsafe IntPtr DatabaseGetOptionIntPtr = NativeDelegate<DatabaseGetOptionInt>.AsNativePointer(GetDatabaseOptionInt);
+ private static unsafe IntPtr DatabaseSetOptionBytesPtr = NativeDelegate<DatabaseSetOptionBytes>.AsNativePointer(SetDatabaseOptionBytes);
+ private static unsafe IntPtr DatabaseSetOptionDoublePtr = NativeDelegate<DatabaseSetOptionDouble>.AsNativePointer(SetDatabaseOptionDouble);
+ private static unsafe IntPtr DatabaseSetOptionIntPtr = NativeDelegate<DatabaseSetOptionInt>.AsNativePointer(SetDatabaseOptionInt);
+
private static unsafe IntPtr ConnectionCancelPtr = NativeDelegate<ConnectionFn>.AsNativePointer(CancelConnection);
+ private static unsafe IntPtr ConnectionGetOptionPtr = NativeDelegate<ConnectionGetOption>.AsNativePointer(GetConnectionOption);
+ private static unsafe IntPtr ConnectionGetOptionBytesPtr = NativeDelegate<ConnectionGetOptionBytes>.AsNativePointer(GetConnectionOptionBytes);
+ private static unsafe IntPtr ConnectionGetOptionDoublePtr = NativeDelegate<ConnectionGetOptionDouble>.AsNativePointer(GetConnectionOptionDouble);
+ private static unsafe IntPtr ConnectionGetOptionIntPtr = NativeDelegate<ConnectionGetOptionInt>.AsNativePointer(GetConnectionOptionInt);
+ private static unsafe IntPtr ConnectionGetStatisticsPtr = NativeDelegate<ConnectionGetStatistics>.AsNativePointer(GetConnectionStatistics);
+ private static unsafe IntPtr ConnectionGetStatisticNamesPtr = NativeDelegate<ConnectionGetStatisticNames>.AsNativePointer(GetConnectionStatisticNames);
+ private static unsafe IntPtr ConnectionSetOptionBytesPtr = NativeDelegate<ConnectionSetOptionBytes>.AsNativePointer(SetConnectionOptionBytes);
+ private static unsafe IntPtr ConnectionSetOptionDoublePtr = NativeDelegate<ConnectionSetOptionDouble>.AsNativePointer(SetConnectionOptionDouble);
+ private static unsafe IntPtr ConnectionSetOptionIntPtr = NativeDelegate<ConnectionSetOptionInt>.AsNativePointer(SetConnectionOptionInt);
+
private static unsafe IntPtr StatementCancelPtr = NativeDelegate<StatementFn>.AsNativePointer(CancelStatement);
+ private static unsafe IntPtr StatementGetOptionPtr = NativeDelegate<StatementGetOption>.AsNativePointer(GetStatementOption);
+ private static unsafe IntPtr StatementGetOptionBytesPtr = NativeDelegate<StatementGetOptionBytes>.AsNativePointer(GetStatementOptionBytes);
+ private static unsafe IntPtr StatementGetOptionDoublePtr = NativeDelegate<StatementGetOptionDouble>.AsNativePointer(GetStatementOptionDouble);
+ private static unsafe IntPtr StatementGetOptionIntPtr = NativeDelegate<StatementGetOptionInt>.AsNativePointer(GetStatementOptionInt);
+ private static unsafe IntPtr StatementSetOptionBytesPtr = NativeDelegate<StatementSetOptionBytes>.AsNativePointer(SetStatementOptionBytes);
+ private static unsafe IntPtr StatementSetOptionDoublePtr = NativeDelegate<StatementSetOptionDouble>.AsNativePointer(SetStatementOptionDouble);
+ private static unsafe IntPtr StatementSetOptionIntPtr = NativeDelegate<StatementSetOptionInt>.AsNativePointer(SetStatementOptionInt);
#endif
public unsafe static AdbcStatusCode AdbcDriverInit(int version, CAdbcDriver* nativeDriver, CAdbcError* error, AdbcDriver driver)
@@ -144,10 +219,39 @@ namespace Apache.Arrow.Adbc.C
if (version >= AdbcVersion.Version_1_1_0)
{
+ nativeDriver->ErrorGetDetailCount = ErrorGetDetailCountPtr;
+ nativeDriver->ErrorGetDetail = ErrorGetDetailPtr;
+ // ErrorFromArrayStream is left null: exported streams report errors
+ // through ArrowArrayStream.get_last_error only.
+
+ nativeDriver->DatabaseGetOption = DatabaseGetOptionPtr;
+ nativeDriver->DatabaseGetOptionBytes = DatabaseGetOptionBytesPtr;
+ nativeDriver->DatabaseGetOptionDouble = DatabaseGetOptionDoublePtr;
+ nativeDriver->DatabaseGetOptionInt = DatabaseGetOptionIntPtr;
+ nativeDriver->DatabaseSetOptionBytes = DatabaseSetOptionBytesPtr;
+ nativeDriver->DatabaseSetOptionDouble = DatabaseSetOptionDoublePtr;
+ nativeDriver->DatabaseSetOptionInt = DatabaseSetOptionIntPtr;
+
nativeDriver->ConnectionCancel = ConnectionCancelPtr;
+ nativeDriver->ConnectionGetOption = ConnectionGetOptionPtr;
+ nativeDriver->ConnectionGetOptionBytes = ConnectionGetOptionBytesPtr;
+ nativeDriver->ConnectionGetOptionDouble = ConnectionGetOptionDoublePtr;
+ nativeDriver->ConnectionGetOptionInt = ConnectionGetOptionIntPtr;
+ nativeDriver->ConnectionGetStatistics = ConnectionGetStatisticsPtr;
+ nativeDriver->ConnectionGetStatisticNames = ConnectionGetStatisticNamesPtr;
+ nativeDriver->ConnectionSetOptionBytes = ConnectionSetOptionBytesPtr;
+ nativeDriver->ConnectionSetOptionDouble = ConnectionSetOptionDoublePtr;
+ nativeDriver->ConnectionSetOptionInt = ConnectionSetOptionIntPtr;
nativeDriver->StatementCancel = StatementCancelPtr;
nativeDriver->StatementExecuteSchema = StatementExecuteSchemaPtr;
+ nativeDriver->StatementGetOption = StatementGetOptionPtr;
+ nativeDriver->StatementGetOptionBytes = StatementGetOptionBytesPtr;
+ nativeDriver->StatementGetOptionDouble = StatementGetOptionDoublePtr;
+ nativeDriver->StatementGetOptionInt = StatementGetOptionIntPtr;
+ nativeDriver->StatementSetOptionBytes = StatementSetOptionBytesPtr;
+ nativeDriver->StatementSetOptionDouble = StatementSetOptionDoublePtr;
+ nativeDriver->StatementSetOptionInt = StatementSetOptionIntPtr;
}
return 0;
@@ -166,6 +270,15 @@ namespace Apache.Arrow.Adbc.C
error->message = null;
}
+ // private_data only exists when the caller allocated an ADBC 1.1.0 error
+ if (error->vendor_code == ErrorVendorCodePrivateData && error->private_data != null)
+ {
+ GCHandle gch = GCHandle.FromIntPtr((IntPtr)error->private_data);
+ (gch.Target as ErrorDetails)?.Dispose();
+ gch.Free();
+ error->private_data = null;
+ }
+
error->release = default;
}
@@ -174,6 +287,7 @@ namespace Apache.Arrow.Adbc.C
AdbcStatusCode status = exception switch
{
AdbcException adbcException => adbcException.Status,
+ OperationCanceledException => AdbcStatusCode.Cancelled,
ArgumentException => AdbcStatusCode.InvalidArgument,
_ => AdbcStatusCode.UnknownError,
};
@@ -197,25 +311,10 @@ namespace Apache.Arrow.Adbc.C
return status;
}
- /// <summary>
- /// Exports a stream produced by the managed driver. The exported stream takes
- /// ownership on success; if exporting fails, nobody else will dispose it.
- /// </summary>
- private unsafe static void ExportStream(IArrowArrayStream? stream, CArrowArrayStream* cStream)
- {
- try
- {
- CArrowArrayStreamExporter.ExportArrayStream(stream!, cStream);
- }
- catch
- {
- stream?.Dispose();
- throw;
- }
- }
-
private unsafe static void FillError(CAdbcError* error, Exception exception)
{
+ bool supportsDetails = error->vendor_code == ErrorVendorCodePrivateData;
+
// The caller may pass an error that still holds a value, possibly from another
// driver, so release it through its own callback.
if (error->release != default)
@@ -228,6 +327,10 @@ namespace Apache.Arrow.Adbc.C
}
error->message = null;
error->release = default;
+ if (supportsDetails)
+ {
+ error->private_data = null;
+ }
error->message = (byte*)MarshalExtensions.StringToCoTaskMemUTF8(exception.Message);
error->sqlstate0 = (byte)0;
@@ -238,14 +341,14 @@ namespace Apache.Arrow.Adbc.C
error->vendor_code = 0;
error->release = ReleaseErrorPtr;
- if (exception is AdbcException adbcException)
+ if (exception is AdbcException adbc)
{
- if (adbcException.SqlState != null)
+ if (adbc.SqlState != null)
{
byte* dest = &error->sqlstate0;
- fixed (char* sqlState = adbcException.SqlState)
+ fixed (char* sqlState = adbc.SqlState)
{
- int len = Math.Min(5, adbcException.SqlState.Length);
+ int len = Math.Min(5, adbc.SqlState.Length);
for (int i = 0; i < len; i++)
{
dest[i] = unchecked((byte)sqlState[i]);
@@ -253,15 +356,39 @@ namespace Apache.Arrow.Adbc.C
}
}
- // INT32_MIN would tell an ADBC 1.1 caller that private_data holds error
- // details, which this exporter doesn't provide
- if (adbcException.NativeError != int.MinValue)
+ IReadOnlyList<KeyValuePair<string, byte[]>> details = adbc.Details;
+ if (supportsDetails && details.Count > 0)
+ {
+ // The caller's error struct has the 1.1.0 layout, so private_data is present.
+ error->vendor_code = ErrorVendorCodePrivateData;
+ error->private_data = (void*)GCHandle.ToIntPtr(GCHandle.Alloc(new ErrorDetails(details)));
+ }
+ else
{
- error->vendor_code = adbcException.NativeError;
+ // Never report the marker value without the private_data that goes with it;
+ // the struct may not even have room for private_data.
+ error->vendor_code = adbc.NativeError == ErrorVendorCodePrivateData ? 0 : adbc.NativeError;
}
}
}
+ /// <summary>
+ /// Exports a stream produced by the managed driver. The exported stream takes
+ /// ownership on success; if exporting fails, nobody else will dispose it.
+ /// </summary>
+ private unsafe static void ExportStream(IArrowArrayStream? stream, CArrowArrayStream* cStream)
+ {
+ try
+ {
+ CArrowArrayStreamExporter.ExportArrayStream(stream!, cStream);
+ }
+ catch
+ {
+ stream?.Dispose();
+ throw;
+ }
+ }
+
private static IntPtr FromDisposable(IDisposable d)
{
GCHandle gch = GCHandle.Alloc(d);
@@ -276,6 +403,15 @@ namespace Apache.Arrow.Adbc.C
p = IntPtr.Zero;
}
+ private static unsafe DatabaseStub GetStub(CAdbcDatabase* nativeDatabase) =>
+ (DatabaseStub)GCHandle.FromIntPtr((IntPtr)nativeDatabase->private_data).Target!;
+
+ private static unsafe ConnectionStub GetStub(CAdbcConnection* nativeConnection) =>
+ (ConnectionStub)GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data).Target!;
+
+ private static unsafe IOptionHolder GetOptions(CAdbcStatement* nativeStatement) =>
+ new StatementOptions((AdbcStatement)GCHandle.FromIntPtr((IntPtr)nativeStatement->private_data).Target!);
+
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
@@ -335,13 +471,445 @@ namespace Apache.Arrow.Adbc.C
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
- private unsafe static AdbcStatusCode InitDatabase(CAdbcDatabase* nativeDatabase, CAdbcError* error)
+ private unsafe static int GetErrorDetailCount(CAdbcError* error)
+ {
+ try
+ {
+ return GetErrorDetails(error)?.Count ?? 0;
+ }
+ catch (Exception)
+ {
+ return 0;
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static CAdbcErrorDetail GetErrorDetail(CAdbcError* error, int index)
+ {
+ try
+ {
+ return GetErrorDetails(error)?.GetDetail(index) ?? default;
+ }
+ catch (Exception)
+ {
+ return default;
+ }
+ }
+
+ private unsafe static ErrorDetails? GetErrorDetails(CAdbcError* error)
+ {
+ if (error == null || error->vendor_code != ErrorVendorCodePrivateData || error->private_data == null)
+ {
+ return null;
+ }
+ return GCHandle.FromIntPtr((IntPtr)error->private_data).Target as ErrorDetails;
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode InitDatabase(CAdbcDatabase* nativeDatabase, CAdbcError* error)
+ {
+ try
+ {
+ GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeDatabase->private_data);
+ DatabaseStub stub = (DatabaseStub)gch.Target!;
+ stub.Init();
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode ReleaseDatabase(CAdbcDatabase* nativeDatabase, CAdbcError* error)
+ {
+ try
+ {
+ GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeDatabase->private_data);
+ DatabaseStub stub = (DatabaseStub)gch.Target!;
+ stub.Dispose();
+ gch.Free();
+ nativeDatabase->private_data = null;
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode SetConnectionOption(CAdbcConnection* nativeConnection, byte* name, byte* value, CAdbcError* error)
+ {
+ try
+ {
+ SetStringOption(GetStub(nativeConnection), name, value);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode SetDatabaseOption(CAdbcDatabase* nativeDatabase, byte* name, byte* value, CAdbcError* error)
+ {
+ try
+ {
+ SetStringOption(GetStub(nativeDatabase), name, value);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode GetDatabaseOption(CAdbcDatabase* nativeDatabase, byte* name, byte* value, nint* length, CAdbcError* error)
+ {
+ try
+ {
+ GetStringOption(GetStub(nativeDatabase), name, value, length);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode GetDatabaseOptionBytes(CAdbcDatabase* nativeDatabase, byte* name, byte* value, nint* length, CAdbcError* error)
+ {
+ try
+ {
+ GetBytesOption(GetStub(nativeDatabase), name, value, length);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode GetDatabaseOptionDouble(CAdbcDatabase* nativeDatabase, byte* name, double* value, CAdbcError* error)
+ {
+ try
+ {
+ GetDoubleOption(GetStub(nativeDatabase), name, value);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode GetDatabaseOptionInt(CAdbcDatabase* nativeDatabase, byte* name, long* value, CAdbcError* error)
+ {
+ try
+ {
+ GetInt64Option(GetStub(nativeDatabase), name, value);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode SetDatabaseOptionBytes(CAdbcDatabase* nativeDatabase, byte* name, byte* value, nint length, CAdbcError* error)
+ {
+ try
+ {
+ SetBytesOption(GetStub(nativeDatabase), name, value, length);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode SetDatabaseOptionDouble(CAdbcDatabase* nativeDatabase, byte* name, double value, CAdbcError* error)
+ {
+ try
+ {
+ GetStub(nativeDatabase).SetOptionDouble(GetKey(name), value);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode SetDatabaseOptionInt(CAdbcDatabase* nativeDatabase, byte* name, long value, CAdbcError* error)
+ {
+ try
+ {
+ GetStub(nativeDatabase).SetOptionInt64(GetKey(name), value);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode InitConnection(CAdbcConnection* nativeConnection, CAdbcDatabase* database, CAdbcError* error)
+ {
+ try
+ {
+ GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
+ ConnectionStub stub = (ConnectionStub)gch.Target!;
+ stub.InitConnection(ref *database);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode GetConnectionObjects(CAdbcConnection* nativeConnection, int depth, byte* catalog, byte* db_schema, byte* table_name, byte** table_type, byte* column_name, CArrowArrayStream* stream, CAdbcError* error)
+ {
+ try
+ {
+ GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
+ ConnectionStub stub = (ConnectionStub)gch.Target!;
+ stub.GetObjects(depth, catalog, db_schema, table_name, table_type, column_name, stream);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode GetConnectionTableTypes(CAdbcConnection* nativeConnection, CArrowArrayStream* stream, CAdbcError* error)
+ {
+ try
+ {
+ GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
+ ConnectionStub stub = (ConnectionStub)gch.Target!;
+ stub.GetTableTypes(stream);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode GetConnectionTableSchema(CAdbcConnection* nativeConnection, byte* catalog, byte* db_schema, byte* table_name, CArrowSchema* schema, CAdbcError* error)
+ {
+ try
+ {
+ GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
+ ConnectionStub stub = (ConnectionStub)gch.Target!;
+ stub.GetTableSchema(catalog, db_schema, table_name, schema);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode RollbackConnection(CAdbcConnection* nativeConnection, CAdbcError* error)
+ {
+ try
+ {
+ GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
+ ConnectionStub stub = (ConnectionStub)gch.Target!;
+ stub.Rollback();
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode CommitConnection(CAdbcConnection* nativeConnection, CAdbcError* error)
+ {
+ try
+ {
+ GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
+ ConnectionStub stub = (ConnectionStub)gch.Target!;
+ stub.Commit();
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode ReleaseConnection(CAdbcConnection* nativeConnection, CAdbcError* error)
+ {
+ try
+ {
+ GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
+ ConnectionStub stub = (ConnectionStub)gch.Target!;
+ stub.Dispose();
+ gch.Free();
+ nativeConnection->private_data = null;
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode ReadConnectionPartition(CAdbcConnection* nativeConnection, byte* serialized_partition, int serialized_length, CArrowArrayStream* stream, CAdbcError* error)
+ {
+ try
+ {
+ GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
+ ConnectionStub stub = (ConnectionStub)gch.Target!;
+ stub.ReadPartition(serialized_partition, serialized_length, stream);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode GetConnectionInfo(CAdbcConnection* nativeConnection, int* info_codes, int info_codes_length, CArrowArrayStream* stream, CAdbcError* error)
+ {
+ try
+ {
+ GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
+ ConnectionStub stub = (ConnectionStub)gch.Target!;
+ stub.GetInfo(info_codes, info_codes_length, stream);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode GetConnectionStatistics(CAdbcConnection* nativeConnection, byte* catalog, byte* db_schema, byte* table_name, byte approximate, CArrowArrayStream* stream, CAdbcError* error)
+ {
+ try
+ {
+ GetStub(nativeConnection).GetStatistics(catalog, db_schema, table_name, approximate != 0, stream);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode GetConnectionStatisticNames(CAdbcConnection* nativeConnection, CArrowArrayStream* stream, CAdbcError* error)
+ {
+ try
+ {
+ GetStub(nativeConnection).GetStatisticNames(stream);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode GetConnectionOption(CAdbcConnection* nativeConnection, byte* name, byte* value, nint* length, CAdbcError* error)
+ {
+ try
+ {
+ GetStringOption(GetStub(nativeConnection), name, value, length);
+ return AdbcStatusCode.Success;
+ }
+ catch (Exception e)
+ {
+ return SetError(error, e);
+ }
+ }
+
+#if NET5_0_OR_GREATER
+ [UnmanagedCallersOnly]
+#endif
+ private unsafe static AdbcStatusCode GetConnectionOptionBytes(CAdbcConnection* nativeConnection, byte* name, byte* value, nint* length, CAdbcError* error)
{
try
{
- GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeDatabase->private_data);
- DatabaseStub stub = (DatabaseStub)gch.Target!;
- stub.Init();
+ GetBytesOption(GetStub(nativeConnection), name, value, length);
return AdbcStatusCode.Success;
}
catch (Exception e)
@@ -353,15 +921,11 @@ namespace Apache.Arrow.Adbc.C
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
- private unsafe static AdbcStatusCode ReleaseDatabase(CAdbcDatabase* nativeDatabase, CAdbcError* error)
+ private unsafe static AdbcStatusCode GetConnectionOptionDouble(CAdbcConnection* nativeConnection, byte* name, double* value, CAdbcError* error)
{
try
{
- GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeDatabase->private_data);
- DatabaseStub stub = (DatabaseStub)gch.Target!;
- stub.Dispose();
- gch.Free();
- nativeDatabase->private_data = null;
+ GetDoubleOption(GetStub(nativeConnection), name, value);
return AdbcStatusCode.Success;
}
catch (Exception e)
@@ -373,13 +937,11 @@ namespace Apache.Arrow.Adbc.C
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
- private unsafe static AdbcStatusCode SetConnectionOption(CAdbcConnection* nativeConnection, byte* name, byte* value, CAdbcError* error)
+ private unsafe static AdbcStatusCode GetConnectionOptionInt(CAdbcConnection* nativeConnection, byte* name, long* value, CAdbcError* error)
{
try
{
- GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
- ConnectionStub stub = (ConnectionStub)gch.Target!;
- stub.SetOption(name, value);
+ GetInt64Option(GetStub(nativeConnection), name, value);
return AdbcStatusCode.Success;
}
catch (Exception e)
@@ -391,13 +953,11 @@ namespace Apache.Arrow.Adbc.C
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
- private unsafe static AdbcStatusCode SetDatabaseOption(CAdbcDatabase* nativeDatabase, byte* name, byte* value, CAdbcError* error)
+ private unsafe static AdbcStatusCode SetConnectionOptionBytes(CAdbcConnection* nativeConnection, byte* name, byte* value, nint length, CAdbcError* error)
{
try
{
- GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeDatabase->private_data);
- DatabaseStub stub = (DatabaseStub)gch.Target!;
- stub.SetOption(name, value);
+ SetBytesOption(GetStub(nativeConnection), name, value, length);
return AdbcStatusCode.Success;
}
catch (Exception e)
@@ -409,13 +969,11 @@ namespace Apache.Arrow.Adbc.C
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
- private unsafe static AdbcStatusCode InitConnection(CAdbcConnection* nativeConnection, CAdbcDatabase* database, CAdbcError* error)
+ private unsafe static AdbcStatusCode SetConnectionOptionDouble(CAdbcConnection* nativeConnection, byte* name, double value, CAdbcError* error)
{
try
{
- GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
- ConnectionStub stub = (ConnectionStub)gch.Target!;
- stub.InitConnection(ref *database);
+ GetStub(nativeConnection).SetOptionDouble(GetKey(name), value);
return AdbcStatusCode.Success;
}
catch (Exception e)
@@ -424,17 +982,14 @@ namespace Apache.Arrow.Adbc.C
}
}
-
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
- private unsafe static AdbcStatusCode GetConnectionObjects(CAdbcConnection* nativeConnection, int depth, byte* catalog, byte* db_schema, byte* table_name, byte** table_type, byte* column_name, CArrowArrayStream* stream, CAdbcError* error)
+ private unsafe static AdbcStatusCode SetConnectionOptionInt(CAdbcConnection* nativeConnection, byte* name, long value, CAdbcError* error)
{
try
{
- GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
- ConnectionStub stub = (ConnectionStub)gch.Target!;
- stub.GetObjects(depth, catalog, db_schema, table_name, table_type, column_name, stream);
+ GetStub(nativeConnection).SetOptionInt64(GetKey(name), value);
return AdbcStatusCode.Success;
}
catch (Exception e)
@@ -446,13 +1001,15 @@ namespace Apache.Arrow.Adbc.C
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
- private unsafe static AdbcStatusCode GetConnectionTableTypes(CAdbcConnection* nativeConnection, CArrowArrayStream* stream, CAdbcError* error)
+ private unsafe static AdbcStatusCode SetStatementSqlQuery(CAdbcStatement* nativeStatement, byte* text, CAdbcError* error)
{
try
{
- GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
- ConnectionStub stub = (ConnectionStub)gch.Target!;
- stub.GetTableTypes(stream);
+ GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeStatement->private_data);
+ AdbcStatement stub = (AdbcStatement)gch.Target!;
+
+ stub.SqlQuery = MarshalExtensions.PtrToStringUTF8(text);
+
return AdbcStatusCode.Success;
}
catch (Exception e)
@@ -464,13 +1021,11 @@ namespace Apache.Arrow.Adbc.C
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
- private unsafe static AdbcStatusCode GetConnectionTableSchema(CAdbcConnection* nativeConnection, byte* catalog, byte* db_schema, byte* table_name, CArrowSchema* schema, CAdbcError* error)
+ private unsafe static AdbcStatusCode SetStatementOption(CAdbcStatement* nativeStatement, byte* name, byte* value, CAdbcError* error)
{
try
{
- GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
- ConnectionStub stub = (ConnectionStub)gch.Target!;
- stub.GetTableSchema(catalog, db_schema, table_name, schema);
+ SetStringOption(GetOptions(nativeStatement), name, value);
return AdbcStatusCode.Success;
}
catch (Exception e)
@@ -482,13 +1037,11 @@ namespace Apache.Arrow.Adbc.C
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
- private unsafe static AdbcStatusCode RollbackConnection(CAdbcConnection* nativeConnection, CAdbcError* error)
+ private unsafe static AdbcStatusCode GetStatementOption(CAdbcStatement* nativeStatement, byte* name, byte* value, nint* length, CAdbcError* error)
{
try
{
- GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
- ConnectionStub stub = (ConnectionStub)gch.Target!;
- stub.Rollback();
+ GetStringOption(GetOptions(nativeStatement), name, value, length);
return AdbcStatusCode.Success;
}
catch (Exception e)
@@ -500,13 +1053,11 @@ namespace Apache.Arrow.Adbc.C
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
- private unsafe static AdbcStatusCode CommitConnection(CAdbcConnection* nativeConnection, CAdbcError* error)
+ private unsafe static AdbcStatusCode GetStatementOptionBytes(CAdbcStatement* nativeStatement, byte* name, byte* value, nint* length, CAdbcError* error)
{
try
{
- GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
- ConnectionStub stub = (ConnectionStub)gch.Target!;
- stub.Commit();
+ GetBytesOption(GetOptions(nativeStatement), name, value, length);
return AdbcStatusCode.Success;
}
catch (Exception e)
@@ -518,15 +1069,11 @@ namespace Apache.Arrow.Adbc.C
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
- private unsafe static AdbcStatusCode ReleaseConnection(CAdbcConnection* nativeConnection, CAdbcError* error)
+ private unsafe static AdbcStatusCode GetStatementOptionDouble(CAdbcStatement* nativeStatement, byte* name, double* value, CAdbcError* error)
{
try
{
- GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
- ConnectionStub stub = (ConnectionStub)gch.Target!;
- stub.Dispose();
- gch.Free();
- nativeConnection->private_data = null;
+ GetDoubleOption(GetOptions(nativeStatement), name, value);
return AdbcStatusCode.Success;
}
catch (Exception e)
@@ -538,13 +1085,11 @@ namespace Apache.Arrow.Adbc.C
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
- private unsafe static AdbcStatusCode ReadConnectionPartition(CAdbcConnection* nativeConnection, byte* serialized_partition, int serialized_length, CArrowArrayStream* stream, CAdbcError* error)
+ private unsafe static AdbcStatusCode GetStatementOptionInt(CAdbcStatement* nativeStatement, byte* name, long* value, CAdbcError* error)
{
try
{
- GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
- ConnectionStub stub = (ConnectionStub)gch.Target!;
- stub.ReadPartition(serialized_partition, serialized_length, stream);
+ GetInt64Option(GetOptions(nativeStatement), name, value);
return AdbcStatusCode.Success;
}
catch (Exception e)
@@ -556,13 +1101,11 @@ namespace Apache.Arrow.Adbc.C
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
- private unsafe static AdbcStatusCode GetConnectionInfo(CAdbcConnection* nativeConnection, int* info_codes, int info_codes_length, CArrowArrayStream* stream, CAdbcError* error)
+ private unsafe static AdbcStatusCode SetStatementOptionBytes(CAdbcStatement* nativeStatement, byte* name, byte* value, nint length, CAdbcError* error)
{
try
{
- GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeConnection->private_data);
- ConnectionStub stub = (ConnectionStub)gch.Target!;
- stub.GetInfo(info_codes, info_codes_length, stream);
+ SetBytesOption(GetOptions(nativeStatement), name, value, length);
return AdbcStatusCode.Success;
}
catch (Exception e)
@@ -574,15 +1117,11 @@ namespace Apache.Arrow.Adbc.C
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
- private unsafe static AdbcStatusCode SetStatementSqlQuery(CAdbcStatement* nativeStatement, byte* text, CAdbcError* error)
+ private unsafe static AdbcStatusCode SetStatementOptionDouble(CAdbcStatement* nativeStatement, byte* name, double value, CAdbcError* error)
{
try
{
- GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeStatement->private_data);
- AdbcStatement stub = (AdbcStatement)gch.Target!;
-
- stub.SqlQuery = MarshalExtensions.PtrToStringUTF8(text);
-
+ GetOptions(nativeStatement).SetOptionDouble(GetKey(name), value);
return AdbcStatusCode.Success;
}
catch (Exception e)
@@ -594,17 +1133,11 @@ namespace Apache.Arrow.Adbc.C
#if NET5_0_OR_GREATER
[UnmanagedCallersOnly]
#endif
- private unsafe static AdbcStatusCode SetStatementOption(CAdbcStatement* nativeStatement, byte* name, byte* value, CAdbcError* error)
+ private unsafe static AdbcStatusCode SetStatementOptionInt(CAdbcStatement* nativeStatement, byte* name, long value, CAdbcError* error)
{
try
{
- if (name == null) throw new ArgumentNullException(nameof(name));
- if (value == null) throw new ArgumentNullException(nameof(value));
-
- GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeStatement->private_data);
- AdbcStatement stub = (AdbcStatement)gch.Target!;
-
- stub.SetOption(MarshalExtensions.PtrToStringUTF8(name)!, MarshalExtensions.PtrToStringUTF8(value)!);
+ GetOptions(nativeStatement).SetOptionInt64(GetKey(name), value);
return AdbcStatusCode.Success;
}
catch (Exception e)
@@ -901,6 +1434,211 @@ namespace Apache.Arrow.Adbc.C
}
}
+ private static unsafe string GetKey(byte* key)
+ {
+ if (key == null) throw new ArgumentNullException(nameof(key));
+ return MarshalExtensions.PtrToStringUTF8(key)!;
+ }
+
+ private static unsafe void SetStringOption(IOptionHolder holder, byte* key, byte* value)
+ {
+ if (value == null) throw new ArgumentNullException(nameof(value));
+ holder.SetOption(GetKey(key), MarshalExtensions.PtrToStringUTF8(value)!);
+ }
+
+ private static unsafe void SetBytesOption(IOptionHolder holder, byte* key, byte* value, nint length)
+ {
+ if (value == null && length != 0) throw new ArgumentNullException(nameof(value));
+ holder.SetOptionBytes(GetKey(key), new ReadOnlySpan<byte>(value, checked((int)length)));
+ }
+
+ private static unsafe void GetStringOption(IOptionHolder holder, byte* key, byte* value, nint* length)
+ {
+ string result = holder.GetOption(GetKey(key));
+ CopyOptionValue(Encoding.UTF8.GetBytes(result), nullTerminate: true, value, length);
+ }
+
+ private static unsafe void GetBytesOption(IOptionHolder holder, byte* key, byte* value, nint* length)
+ {
+ CopyOptionValue(holder.GetOptionBytes(GetKey(key)), nullTerminate: false, value, length);
+ }
+
+ private static unsafe void GetInt64Option(IOptionHolder holder, byte* key, long* value)
+ {
+ if (value == null) throw new ArgumentNullException(nameof(value));
+ *value = holder.GetOptionInt64(GetKey(key));
+ }
+
+ private static unsafe void GetDoubleOption(IOptionHolder holder, byte* key, double* value)
+ {
+ if (value == null) throw new ArgumentNullException(nameof(value));
+ *value = holder.GetOptionDouble(GetKey(key));
+ }
+
+ /// <summary>
+ /// Implements the buffer protocol of the AdbcXxxGetOption and AdbcXxxGetOptionBytes
+ /// functions: on input <paramref name="length"/> is the size of the buffer; on output it is
+ /// the size of the value. The value is only written if it fits.
+ /// </summary>
+ private static unsafe void CopyOptionValue(byte[] data, bool nullTerminate, byte* value, nint* length)
+ {
+ if (length == null) throw new ArgumentNullException(nameof(length));
+
+ int required = data.Length + (nullTerminate ? 1 : 0);
+ if (value != null && *length >= required)
+ {
+ data.AsSpan().CopyTo(new Span<byte>(value, data.Length));
+ if (nullTerminate)
+ {
+ value[data.Length] = 0;
+ }
+ }
+ *length = required;
+ }
+
+ /// <summary>
+ /// The operations shared by databases, connections and statements for
+ /// getting and setting options.
+ /// </summary>
+ private interface IOptionHolder
+ {
+ void SetOption(string key, string value);
+ void SetOptionInt64(string key, long value);
+ void SetOptionDouble(string key, double value);
+ void SetOptionBytes(string key, ReadOnlySpan<byte> value);
+ string GetOption(string key);
+ long GetOptionInt64(string key);
+ double GetOptionDouble(string key);
+ byte[] GetOptionBytes(string key);
+ }
+
+ /// <summary>
+ /// Options set on a database or connection before it is initialized. Values
+ /// are strings, longs, doubles or byte arrays.
+ /// </summary>
+ private sealed class PendingOptions
+ {
+ // GetOption must be thread-safe per the ADBC specification, so access to
+ // the recorded values is synchronized.
+ private readonly Dictionary<string, object> _options = new Dictionary<string, object>();
+
+ public void Set(string key, object value)
+ {
+ lock (_options)
+ {
+ _options[key] = value;
+ }
+ }
+
+ public KeyValuePair<string, object>[] Snapshot()
+ {
+ lock (_options)
+ {
+ return _options.ToArray();
+ }
+ }
+
+ public bool TryGet(string key, out object? value)
+ {
+ lock (_options)
+ {
+ return _options.TryGetValue(key, out value);
+ }
+ }
+
+ public static string AsString(string key, object value) =>
+ value is byte[]? throw new AdbcException($"Option '{key}' has a binary value", AdbcStatusCode.InvalidArgument) : AdbcOptionValue.ToOptionString(key, value);
+
+ public static long AsInt64(string key, object value) =>
+ value is long l ? l : AdbcOptionValue.ParseInt64(key, AsString(key, value));
+
+ public static double AsDouble(string key, object value) =>
+ value switch
+ {
+ double d => d,
+ long l => l,
+ _ => AdbcOptionValue.ParseDouble(key, AsString(key, value)),
+ };
+
+ public static byte[] AsBytes(string key, object value) =>
+ value as byte[] ?? throw new AdbcException($"Option '{key}' does not have a binary value", AdbcStatusCode.InvalidArgument);
+
+ /// <summary>
+ /// Gets an option value from <paramref name="getter"/>, falling back to the value
+ /// recorded here if the managed implementation doesn't know about the option.
+ /// </summary>
+ public T Get<T>(string key, Func<string, T>? getter, Func<string, object, T> convert)
+ {
+ if (getter != null)
+ {
+ try
+ {
+ return getter(key);
+ }
+ catch (AdbcException e) when (
+ (e.Status == AdbcStatusCode.NotFound || e.Status == AdbcStatusCode.NotImplemented) &&
+ TryGet(key, out _))
+ {
+ }
+ }
+
+ return TryGet(key, out object? value) ? convert(key, value!) : throw AdbcOptionValue.NotFound(key);
+ }
+ }
+
+ /// <summary>
+ /// Unmanaged copies of <see cref="AdbcException.Details"/>, owned by a <see cref="CAdbcError"/>.
+ /// </summary>
+ private sealed class ErrorDetails : IDisposable
+ {
+ private readonly IntPtr[] _keys;
+ private readonly IntPtr[] _values;
+ private readonly int[] _lengths;
+
+ public ErrorDetails(IReadOnlyList<KeyValuePair<string, byte[]>> details)
+ {
+ _keys = new IntPtr[details.Count];
+ _values = new IntPtr[details.Count];
+ _lengths = new int[details.Count];
+ for (int i = 0; i < details.Count; i++)
+ {
+ byte[] value = details[i].Value ?? System.Array.Empty<byte>();
+ _keys[i] = MarshalExtensions.StringToCoTaskMemUTF8(details[i].Key ?? string.Empty);
+ _values[i] = Marshal.AllocCoTaskMem(Math.Max(1, value.Length));
+ _lengths[i] = value.Length;
+ Marshal.Copy(value, 0, _values[i], value.Length);
+ }
+ }
+
+ public int Count => _keys.Length;
+
+ public unsafe CAdbcErrorDetail GetDetail(int index)
+ {
+ if (index < 0 || index >= _keys.Length)
+ {
+ return default;
+ }
+
+ return new CAdbcErrorDetail
+ {
+ key = (byte*)_keys[index],
+ value = (byte*)_values[index],
+ length = _lengths[index],
+ };
+ }
+
+ public void Dispose()
+ {
+ for (int i = 0; i < _keys.Length; i++)
+ {
+ Marshal.FreeCoTaskMem(_keys[i]);
+ Marshal.FreeCoTaskMem(_values[i]);
+ _keys[i] = IntPtr.Zero;
+ _values[i] = IntPtr.Zero;
+ }
+ }
+ }
+
private sealed class DriverStub : IDisposable
{
private readonly AdbcDriver _driver;
@@ -964,39 +1702,72 @@ namespace Apache.Arrow.Adbc.C
}
}
- sealed class DatabaseStub : IDisposable
+ sealed class DatabaseStub : IDisposable, IOptionHolder
{
readonly AdbcDriver _driver;
- readonly Dictionary<string, string> options;
+ readonly PendingOptions options;
AdbcDatabase? database;
public DatabaseStub(AdbcDriver driver)
{
_driver = driver;
- options = new Dictionary<string, string>();
+ options = new PendingOptions();
}
public void Init()
{
if (database != null) throw new InvalidOperationException("database already initialized");
- database = _driver.Open(options);
+ KeyValuePair<string, object>[] values = options.Snapshot();
+ if (values.All(pair => pair.Value is string))
+ {
+ Dictionary<string, string> stringOptions = new Dictionary<string, string>();
+ foreach (KeyValuePair<string, object> pair in values)
+ {
+ stringOptions[pair.Key] = (string)pair.Value;
+ }
+ database = _driver.Open(stringOptions);
+ }
+ else
+ {
+ database = _driver.Open(values.ToDictionary(pair => pair.Key, pair => pair.Value));
+ }
+ }
+
+ // Options are recorded even after initialization so that DatabaseGetOption can
+ // report them when the managed database doesn't implement GetOption itself.
+
+ public void SetOption(string key, string value)
+ {
+ database?.SetOption(key, value);
+ options.Set(key, value);
}
- public unsafe void SetOption(byte* name, byte* value)
+ public void SetOptionInt64(string key, long value)
{
- if (name == null) throw new ArgumentNullException(nameof(name));
- if (value == null) throw new ArgumentNullException(nameof(value));
+ database?.SetOptionInt64(key, value);
+ options.Set(key, value);
+ }
- options[MarshalExtensions.PtrToStringUTF8(name)!] = MarshalExtensions.PtrToStringUTF8(value)!;
+ public void SetOptionDouble(string key, double value)
+ {
+ database?.SetOptionDouble(key, value);
+ options.Set(key, value);
}
- public void OpenConnection(IReadOnlyDictionary<string, string> options, out AdbcConnection connection)
+ public void SetOptionBytes(string key, ReadOnlySpan<byte> value)
{
- if (database == null) throw new InvalidOperationException("database not initialized");
- connection = database.Connect(options);
+ database?.SetOptionBytes(key, value);
+ options.Set(key, value.ToArray());
}
+ public string GetOption(string key) => options.Get(key, database == null ? null : database.GetOption, PendingOptions.AsString);
+ public long GetOptionInt64(string key) => options.Get(key, database == null ? null : database.GetOptionInt64, PendingOptions.AsInt64);
+ public double GetOptionDouble(string key) => options.Get(key, database == null ? null : database.GetOptionDouble, PendingOptions.AsDouble);
+ public byte[] GetOptionBytes(string key) => options.Get(key, database == null ? null : database.GetOptionBytes, PendingOptions.AsBytes);
+
+ public AdbcDatabase Database => database ?? throw new InvalidOperationException("database not initialized");
+
public void Dispose()
{
database?.Dispose();
@@ -1004,51 +1775,114 @@ namespace Apache.Arrow.Adbc.C
}
}
- sealed class ConnectionStub : IDisposable
+ sealed class ConnectionStub : IDisposable, IOptionHolder
{
readonly AdbcDriver _driver;
- readonly Dictionary<string, string> options;
+ readonly PendingOptions options;
AdbcConnection? connection;
+ // Values of the transaction options that were passed to Connect. The base
+ // AdbcConnection reports defaults for these rather than failing, so a driver that
+ // honors them in Connect but doesn't override the property getters would otherwise
+ // report the wrong value. A key is removed once it's set through the property,
+ // because a driver with a working setter is expected to have a working getter.
+ // Replaced rather than mutated so that GetOption can read it without locking.
+ volatile Dictionary<string, string> consumedAtConnect = new Dictionary<string, string>();
+
public ConnectionStub(AdbcDriver driver)
{
_driver = driver;
- options = new Dictionary<string, string>();
+ options = new PendingOptions();
}
internal AdbcConnection Connection => connection == null ? throw new InvalidOperationException("connection is not open") : connection;
- public unsafe void SetOption(byte* name, byte* value)
+ public void SetOption(string key, string value)
{
- if (name == null) throw new ArgumentNullException(nameof(name));
- if (value == null) throw new ArgumentNullException(nameof(value));
-
- string stringName = MarshalExtensions.PtrToStringUTF8(name)!;
- string stringValue = MarshalExtensions.PtrToStringUTF8(value)!;
-
- if (connection == null)
+ if (connection != null)
{
- options[stringName] = stringValue;
- }
- else
- {
- switch (stringName)
+ switch (key)
{
case AdbcOptions.Connection.Autocommit:
- connection.AutoCommit = AdbcOptions.GetEnabled(stringValue);
+ connection.AutoCommit = ParseOptionValue(key, value, AdbcOptions.GetEnabled);
+ ForgetConsumedAtConnect(key);
break;
case AdbcOptions.Connection.IsolationLevel:
- connection.IsolationLevel = AdbcOptions.GetIsolationLevel(stringValue);
+ connection.IsolationLevel = ParseOptionValue(key, value, AdbcOptions.GetIsolationLevel);
+ ForgetConsumedAtConnect(key);
break;
case AdbcOptions.Connection.ReadOnly:
- connection.ReadOnly = AdbcOptions.GetEnabled(stringValue);
+ connection.ReadOnly = ParseOptionValue(key, value, AdbcOptions.GetEnabled);
+ ForgetConsumedAtConnect(key);
+ break;
+ case AdbcOptions.Connection.CurrentCatalog:
+ connection.CurrentCatalog = value;
+ break;
+ case AdbcOptions.Connection.CurrentDbSchema:
+ connection.CurrentDbSchema = value;
break;
default:
- connection.SetOption(stringName, stringValue);
+ connection.SetOption(key, value);
break;
}
}
+ options.Set(key, value);
+ }
+
+ public void SetOptionInt64(string key, long value)
+ {
+ connection?.SetOptionInt64(key, value);
+ options.Set(key, value);
+ }
+
+ public void SetOptionDouble(string key, double value)
+ {
+ connection?.SetOptionDouble(key, value);
+ options.Set(key, value);
+ }
+
+ public void SetOptionBytes(string key, ReadOnlySpan<byte> value)
+ {
+ connection?.SetOptionBytes(key, value);
+ options.Set(key, value.ToArray());
+ }
+
+ public string GetOption(string key)
+ {
+ if (consumedAtConnect.TryGetValue(key, out string? value))
+ {
+ return value;
+ }
+
+ AdbcConnection? current = connection;
+ return options.Get(key, current == null ? null : current.GetOption, PendingOptions.AsString);
+ }
+
+ private static T ParseOptionValue<T>(string key, string value, Func<string, T> parse)
+ {
+ try
+ {
+ return parse(value);
+ }
+ catch (NotSupportedException)
+ {
+ throw new AdbcException($"Invalid value '{value}' for option '{key}'", AdbcStatusCode.InvalidArgument);
+ }
+ }
+
+ private void ForgetConsumedAtConnect(string key)
+ {
+ Dictionary<string, string> current = consumedAtConnect;
+ if (current.ContainsKey(key))
+ {
+ Dictionary<string, string> updated = new Dictionary<string, string>(current);
+ updated.Remove(key);
+ consumedAtConnect = updated;
+ }
}
+ public long GetOptionInt64(string key) => options.Get(key, connection == null ? null : connection.GetOptionInt64, PendingOptions.AsInt64);
+ public double GetOptionDouble(string key) => options.Get(key, connection == null ? null : connection.GetOptionDouble, PendingOptions.AsDouble);
+ public byte[] GetOptionBytes(string key) => options.Get(key, connection == null ? null : connection.GetOptionBytes, PendingOptions.AsBytes);
public void Rollback() { Connection.Rollback(); }
public void Commit() { Connection.Commit(); }
@@ -1112,6 +1946,23 @@ namespace Apache.Arrow.Adbc.C
ExportStream(Connection.GetTableTypes(), cArrayStream);
}
+ public unsafe void GetStatistics(byte* catalog, byte* db_schema, byte* table_name, bool approximate, CArrowArrayStream* cArrayStream)
+ {
+ string? catalogPattern = MarshalExtensions.PtrToStringUTF8(catalog);
+ string? dbSchemaPattern = MarshalExtensions.PtrToStringUTF8(db_schema);
+ // The C API allows a null table name, meaning "all tables".
+ string tableNamePattern = MarshalExtensions.PtrToStringUTF8(table_name)!;
+
+ ExportStream(
+ Connection.GetStatistics(catalogPattern, dbSchemaPattern, tableNamePattern, approximate),
+ cArrayStream);
+ }
+
+ public unsafe void GetStatisticNames(CArrowArrayStream* cArrayStream)
+ {
+ ExportStream(Connection.GetStatisticsNames(), cArrayStream);
+ }
+
public unsafe void ReadPartition(byte* serializedPartition, int serialized_length, CArrowArrayStream* stream)
{
byte[] partition = new byte[serialized_length];
@@ -1137,9 +1988,44 @@ namespace Apache.Arrow.Adbc.C
public unsafe void InitConnection(ref CAdbcDatabase nativeDatabase)
{
+ if (connection != null) throw new InvalidOperationException("connection already initialized");
+
GCHandle gch = GCHandle.FromIntPtr((IntPtr)nativeDatabase.private_data);
DatabaseStub stub = (DatabaseStub)gch.Target!;
- stub.OpenConnection(options, out connection);
+
+ // Use the string overload when possible so that existing drivers keep going
+ // through the method they override.
+ KeyValuePair<string, object>[] values = options.Snapshot();
+ AdbcConnection opened;
+ if (values.All(pair => pair.Value is string))
+ {
+ Dictionary<string, string> stringOptions = new Dictionary<string, string>(values.Length);
+ foreach (KeyValuePair<string, object> pair in values)
+ {
+ stringOptions[pair.Key] = (string)pair.Value;
+ }
+ opened = stub.Database.Connect(stringOptions);
+ }
+ else
+ {
+ opened = stub.Database.ConnectWithTypedOptions(values.ToDictionary(pair => pair.Key, pair => pair.Value));
+ }
+
+ Dictionary<string, string> consumed = new Dictionary<string, string>();
+ foreach (KeyValuePair<string, object> pair in values)
+ {
+ switch (pair.Key)
+ {
+ case AdbcOptions.Connection.Autocommit:
+ case AdbcOptions.Connection.ReadOnly:
+ case AdbcOptions.Connection.IsolationLevel:
+ consumed[pair.Key] = PendingOptions.AsString(pair.Key, pair.Value);
+ break;
+ }
+ }
+
+ consumedAtConnect = consumed;
+ connection = opened;
}
public unsafe void NewStatement(ref CAdbcStatement nativeStatement)
@@ -1149,5 +2035,21 @@ namespace Apache.Arrow.Adbc.C
nativeStatement.private_data = (void*)GCHandle.ToIntPtr(handle);
}
}
+
+ sealed class StatementOptions : IOptionHolder
+ {
+ readonly AdbcStatement _statement;
+
+ public StatementOptions(AdbcStatement statement) { _statement = statement; }
+
+ public void SetOption(string key, string value) => _statement.SetOption(key, value);
+ public void SetOptionInt64(string key, long value) => _statement.SetOptionInt64(key, value);
+ public void SetOptionDouble(string key, double value) => _statement.SetOptionDouble(key, value);
+ public void SetOptionBytes(string key, ReadOnlySpan<byte> value) => _statement.SetOptionBytes(key, value);
+ public string GetOption(string key) => _statement.GetOption(key);
+ public long GetOptionInt64(string key) => _statement.GetOptionInt64(key);
+ public double GetOptionDouble(string key) => _statement.GetOptionDouble(key);
+ public byte[] GetOptionBytes(string key) => _statement.GetOptionBytes(key);
+ }
}
}
diff --git a/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.Options.cs b/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.Options.cs
new file mode 100644
index 000000000..3c1b5a492
--- /dev/null
+++ b/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.Options.cs
@@ -0,0 +1,312 @@
+/*
+ * 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.
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Runtime.InteropServices;
+using System.Text;
+
+#if NET5_0_OR_GREATER
+using unsafe GetOptionFn = delegate* unmanaged<void*, byte*, byte*, nint*, Apache.Arrow.Adbc.C.CAdbcError*, Apache.Arrow.Adbc.AdbcStatusCode>;
+using unsafe GetOptionDoubleFn = delegate* unmanaged<void*, byte*, double*, Apache.Arrow.Adbc.C.CAdbcError*, Apache.Arrow.Adbc.AdbcStatusCode>;
+using unsafe GetOptionIntFn = delegate* unmanaged<void*, byte*, long*, Apache.Arrow.Adbc.C.CAdbcError*, Apache.Arrow.Adbc.AdbcStatusCode>;
+using unsafe SetOptionBytesFn = delegate* unmanaged<void*, byte*, byte*, nint, Apache.Arrow.Adbc.C.CAdbcError*, Apache.Arrow.Adbc.AdbcStatusCode>;
+using unsafe SetOptionDoubleFn = delegate* unmanaged<void*, byte*, double, Apache.Arrow.Adbc.C.CAdbcError*, Apache.Arrow.Adbc.AdbcStatusCode>;
+using unsafe SetOptionIntFn = delegate* unmanaged<void*, byte*, long, Apache.Arrow.Adbc.C.CAdbcError*, Apache.Arrow.Adbc.AdbcStatusCode>;
+#else
+using GetOptionFn = Apache.Arrow.Adbc.C.HandleGetOption;
+using GetOptionDoubleFn = Apache.Arrow.Adbc.C.HandleGetOptionDouble;
+using GetOptionIntFn = Apache.Arrow.Adbc.C.HandleGetOptionInt;
+using SetOptionBytesFn = Apache.Arrow.Adbc.C.HandleSetOptionBytes;
+using SetOptionDoubleFn = Apache.Arrow.Adbc.C.HandleSetOptionDouble;
+using SetOptionIntFn = Apache.Arrow.Adbc.C.HandleSetOptionInt;
+#endif
+
+namespace Apache.Arrow.Adbc.C
+{
+ public static partial class CAdbcDriverImporter
+ {
+ /// <summary>
+ /// The ADBC 1.1.0 typed option functions for one kind of handle (database,
+ /// connection or statement). The C signatures differ only in the type of the
+ /// handle pointer, so they are stored here with the handle typed as void*.
+ /// </summary>
+ /// <remarks>
+ /// On .NET Framework, <see cref="Marshal.GetDelegateForFunctionPointer{TDelegate}(IntPtr)"/>
+ /// returns the original delegate when the pointer refers to a managed function (as it does
+ /// for an exported driver or the importer's default stubs), so each pointer must be bound
+ /// with its exact delegate type and then adapted to the shared shape.
+ /// </remarks>
+ internal sealed unsafe class OptionFunctions
+ {
+ public readonly GetOptionFn GetOption;
+ public readonly GetOptionFn GetOptionBytes;
+ public readonly GetOptionDoubleFn GetOptionDouble;
+ public readonly GetOptionIntFn GetOptionInt;
+ public readonly SetOptionBytesFn SetOptionBytes;
+ public readonly SetOptionDoubleFn SetOptionDouble;
+ public readonly SetOptionIntFn SetOptionInt;
+
+ private OptionFunctions(
+ GetOptionFn getOption,
+ GetOptionFn getOptionBytes,
+ GetOptionDoubleFn getOptionDouble,
+ GetOptionIntFn getOptionInt,
+ SetOptionBytesFn setOptionBytes,
+ SetOptionDoubleFn setOptionDouble,
+ SetOptionIntFn setOptionInt)
+ {
+ GetOption = getOption;
+ GetOptionBytes = getOptionBytes;
+ GetOptionDouble = getOptionDouble;
+ GetOptionInt = getOptionInt;
+ SetOptionBytes = setOptionBytes;
+ SetOptionDouble = setOptionDouble;
+ SetOptionInt = setOptionInt;
+ }
+
+#if NET5_0_OR_GREATER
+ public static OptionFunctions ForDatabase(ref CAdbcDriver driver) => new OptionFunctions(
+ (GetOptionFn)driver.DatabaseGetOption,
+ (GetOptionFn)driver.DatabaseGetOptionBytes,
+ (GetOptionDoubleFn)driver.DatabaseGetOptionDouble,
+ (GetOptionIntFn)driver.DatabaseGetOptionInt,
+ (SetOptionBytesFn)driver.DatabaseSetOptionBytes,
+ (SetOptionDoubleFn)driver.DatabaseSetOptionDouble,
+ (SetOptionIntFn)driver.DatabaseSetOptionInt);
+
+ public static OptionFunctions ForConnection(ref CAdbcDriver driver) => new OptionFunctions(
+ (GetOptionFn)driver.ConnectionGetOption,
+ (GetOptionFn)driver.ConnectionGetOptionBytes,
+ (GetOptionDoubleFn)driver.ConnectionGetOptionDouble,
+ (GetOptionIntFn)driver.ConnectionGetOptionInt,
+ (SetOptionBytesFn)driver.ConnectionSetOptionBytes,
+ (SetOptionDoubleFn)driver.ConnectionSetOptionDouble,
+ (SetOptionIntFn)driver.ConnectionSetOptionInt);
+
+ public static OptionFunctions ForStatement(ref CAdbcDriver driver) => new OptionFunctions(
+ (GetOptionFn)driver.StatementGetOption,
+ (GetOptionFn)driver.StatementGetOptionBytes,
+ (GetOptionDoubleFn)driver.StatementGetOptionDouble,
+ (GetOptionIntFn)driver.StatementGetOptionInt,
+ (SetOptionBytesFn)driver.StatementSetOptionBytes,
+ (SetOptionDoubleFn)driver.StatementSetOptionDouble,
+ (SetOptionIntFn)driver.StatementSetOptionInt);
+#else
+ public static OptionFunctions ForDatabase(ref CAdbcDriver driver)
+ {
+ var getOption = Marshal.GetDelegateForFunctionPointer<DatabaseGetOption>(driver.DatabaseGetOption);
+ var getOptionBytes = Marshal.GetDelegateForFunctionPointer<DatabaseGetOptionBytes>(driver.DatabaseGetOptionBytes);
+ var getOptionDouble = Marshal.GetDelegateForFunctionPointer<DatabaseGetOptionDouble>(driver.DatabaseGetOptionDouble);
+ var getOptionInt = Marshal.GetDelegateForFunctionPointer<DatabaseGetOptionInt>(driver.DatabaseGetOptionInt);
+ var setOptionBytes = Marshal.GetDelegateForFunctionPointer<DatabaseSetOptionBytes>(driver.DatabaseSetOptionBytes);
+ var setOptionDouble = Marshal.GetDelegateForFunctionPointer<DatabaseSetOptionDouble>(driver.DatabaseSetOptionDouble);
+ var setOptionInt = Marshal.GetDelegateForFunctionPointer<DatabaseSetOptionInt>(driver.DatabaseSetOptionInt);
+ return new OptionFunctions(
+ (h, k, v, l, e) => getOption((CAdbcDatabase*)h, k, v, l, e),
+ (h, k, v, l, e) => getOptionBytes((CAdbcDatabase*)h, k, v, l, e),
+ (h, k, v, e) => getOptionDouble((CAdbcDatabase*)h, k, v, e),
+ (h, k, v, e) => getOptionInt((CAdbcDatabase*)h, k, v, e),
+ (h, k, v, l, e) => setOptionBytes((CAdbcDatabase*)h, k, v, l, e),
+ (h, k, v, e) => setOptionDouble((CAdbcDatabase*)h, k, v, e),
+ (h, k, v, e) => setOptionInt((CAdbcDatabase*)h, k, v, e));
+ }
+
+ public static OptionFunctions ForConnection(ref CAdbcDriver driver)
+ {
+ var getOption = Marshal.GetDelegateForFunctionPointer<ConnectionGetOption>(driver.ConnectionGetOption);
+ var getOptionBytes = Marshal.GetDelegateForFunctionPointer<ConnectionGetOptionBytes>(driver.ConnectionGetOptionBytes);
+ var getOptionDouble = Marshal.GetDelegateForFunctionPointer<ConnectionGetOptionDouble>(driver.ConnectionGetOptionDouble);
+ var getOptionInt = Marshal.GetDelegateForFunctionPointer<ConnectionGetOptionInt>(driver.ConnectionGetOptionInt);
+ var setOptionBytes = Marshal.GetDelegateForFunctionPointer<ConnectionSetOptionBytes>(driver.ConnectionSetOptionBytes);
+ var setOptionDouble = Marshal.GetDelegateForFunctionPointer<ConnectionSetOptionDouble>(driver.ConnectionSetOptionDouble);
+ var setOptionInt = Marshal.GetDelegateForFunctionPointer<ConnectionSetOptionInt>(driver.ConnectionSetOptionInt);
+ return new OptionFunctions(
+ (h, k, v, l, e) => getOption((CAdbcConnection*)h, k, v, l, e),
+ (h, k, v, l, e) => getOptionBytes((CAdbcConnection*)h, k, v, l, e),
+ (h, k, v, e) => getOptionDouble((CAdbcConnection*)h, k, v, e),
+ (h, k, v, e) => getOptionInt((CAdbcConnection*)h, k, v, e),
+ (h, k, v, l, e) => setOptionBytes((CAdbcConnection*)h, k, v, l, e),
+ (h, k, v, e) => setOptionDouble((CAdbcConnection*)h, k, v, e),
+ (h, k, v, e) => setOptionInt((CAdbcConnection*)h, k, v, e));
+ }
+
+ public static OptionFunctions ForStatement(ref CAdbcDriver driver)
+ {
+ var getOption = Marshal.GetDelegateForFunctionPointer<StatementGetOption>(driver.StatementGetOption);
+ var getOptionBytes = Marshal.GetDelegateForFunctionPointer<StatementGetOptionBytes>(driver.StatementGetOptionBytes);
+ var getOptionDouble = Marshal.GetDelegateForFunctionPointer<StatementGetOptionDouble>(driver.StatementGetOptionDouble);
+ var getOptionInt = Marshal.GetDelegateForFunctionPointer<StatementGetOptionInt>(driver.StatementGetOptionInt);
+ var setOptionBytes = Marshal.GetDelegateForFunctionPointer<StatementSetOptionBytes>(driver.StatementSetOptionBytes);
+ var setOptionDouble = Marshal.GetDelegateForFunctionPointer<StatementSetOptionDouble>(driver.StatementSetOptionDouble);
+ var setOptionInt = Marshal.GetDelegateForFunctionPointer<StatementSetOptionInt>(driver.StatementSetOptionInt);
+ return new OptionFunctions(
+ (h, k, v, l, e) => getOption((CAdbcStatement*)h, k, v, l, e),
+ (h, k, v, l, e) => getOptionBytes((CAdbcStatement*)h, k, v, l, e),
+ (h, k, v, e) => getOptionDouble((CAdbcStatement*)h, k, v, e),
+ (h, k, v, e) => getOptionInt((CAdbcStatement*)h, k, v, e),
+ (h, k, v, l, e) => setOptionBytes((CAdbcStatement*)h, k, v, l, e),
+ (h, k, v, e) => setOptionDouble((CAdbcStatement*)h, k, v, e),
+ (h, k, v, e) => setOptionInt((CAdbcStatement*)h, k, v, e));
+ }
+#endif
+ }
+
+ private static unsafe string GetOptionString(ImportedAdbcDriver driver, GetOptionFn fn, void* handle, string key)
+ {
+ byte[] value = GetOptionBuffer(driver, fn, handle, key);
+
+ // The reported length includes the null terminator
+ int length = value.Length > 0 && value[value.Length - 1] == 0 ? value.Length - 1 : value.Length;
+ return Encoding.UTF8.GetString(value, 0, length);
+ }
+
+ /// <summary>
+ /// Implements the buffer protocol of the AdbcXxxGetOption and AdbcXxxGetOptionBytes
+ /// functions, retrying with a larger buffer if the value doesn't fit.
+ /// </summary>
+ private static unsafe byte[] GetOptionBuffer(ImportedAdbcDriver driver, GetOptionFn fn, void* handle, string key)
+ {
+ const int InitialLength = 256;
+
+ using (CallHelper caller = new CallHelper(driver))
+ using (Utf8Helper utf8Key = new Utf8Helper(key))
+ {
+ byte[] buffer = new byte[InitialLength];
+ while (true)
+ {
+ nint length = buffer.Length;
+ fixed (byte* value = buffer)
+ {
+ caller.TranslateCode(fn(handle, utf8Key, value, &length, &caller._error));
+ }
+
+ if (length <= buffer.Length)
+ {
+ byte[] result = new byte[length];
+ System.Array.Copy(buffer, result, result.Length);
+ return result;
+ }
+
+ buffer = new byte[checked((int)length)];
+ }
+ }
+ }
+
+ private static unsafe long GetOptionInt64(ImportedAdbcDriver driver, GetOptionIntFn fn, void* handle, string key)
+ {
+ using (CallHelper caller = new CallHelper(driver))
+ using (Utf8Helper utf8Key = new Utf8Helper(key))
+ {
+ long value = 0;
+ caller.TranslateCode(fn(handle, utf8Key, &value, &caller._error));
+ return value;
+ }
+ }
+
+ private static unsafe double GetOptionDouble(ImportedAdbcDriver driver, GetOptionDoubleFn fn, void* handle, string key)
+ {
+ using (CallHelper caller = new CallHelper(driver))
+ using (Utf8Helper utf8Key = new Utf8Helper(key))
+ {
+ double value = 0;
+ caller.TranslateCode(fn(handle, utf8Key, &value, &caller._error));
+ return value;
+ }
+ }
+
+ private static unsafe void SetOptionBytes(ImportedAdbcDriver driver, SetOptionBytesFn fn, void* handle, string key, ReadOnlySpan<byte> value)
+ {
+ using (CallHelper caller = new CallHelper(driver))
+ using (Utf8Helper utf8Key = new Utf8Helper(key))
+ {
+ fixed (byte* bytes = value)
+ {
+ caller.TranslateCode(fn(handle, utf8Key, bytes, value.Length, &caller._error));
+ }
+ }
+ }
+
+ private static unsafe void SetOptionDouble(ImportedAdbcDriver driver, SetOptionDoubleFn fn, void* handle, string key, double value)
+ {
+ using (CallHelper caller = new CallHelper(driver))
+ using (Utf8Helper utf8Key = new Utf8Helper(key))
+ {
+ caller.TranslateCode(fn(handle, utf8Key, value, &caller._error));
+ }
+ }
+
+ private static unsafe void SetOptionInt64(ImportedAdbcDriver driver, SetOptionIntFn fn, void* handle, string key, long value)
+ {
+ using (CallHelper caller = new CallHelper(driver))
+ using (Utf8Helper utf8Key = new Utf8Helper(key))
+ {
+ caller.TranslateCode(fn(handle, utf8Key, value, &caller._error));
+ }
+ }
+
+ /// <summary>
+ /// Reads the ADBC 1.1.0 error details attached to <paramref name="error"/>, if any.
+ /// Must be called before the error is released.
+ /// </summary>
+ private static unsafe IReadOnlyList<KeyValuePair<string, byte[]>>? ReadErrorDetails(ImportedAdbcDriver? driver, CAdbcError* error)
+ {
+ if (driver == null ||
+ driver.DriverVersion < AdbcVersion.Version_1_1_0 ||
+ error->vendor_code != CAdbcDriverExporter.ErrorVendorCodePrivateData ||
+ error->private_data == null)
+ {
+ return null;
+ }
+
+ ref CAdbcDriver nativeDriver = ref driver.DriverUnsafe;
+#if NET5_0_OR_GREATER
+ int count = nativeDriver.ErrorGetDetailCount(error);
+#else
+ int count = Marshal.GetDelegateForFunctionPointer<ErrorGetDetailCount>(nativeDriver.ErrorGetDetailCount)(error);
+#endif
+ if (count <= 0)
+ {
+ return null;
+ }
+
+ List<KeyValuePair<string, byte[]>> details = new List<KeyValuePair<string, byte[]>>(count);
+ for (int i = 0; i < count; i++)
+ {
+#if NET5_0_OR_GREATER
+ CAdbcErrorDetail detail = nativeDriver.ErrorGetDetail(error, i);
+#else
+ CAdbcErrorDetail detail = Marshal.GetDelegateForFunctionPointer<ErrorGetDetail>(nativeDriver.ErrorGetDetail)(error, i);
+#endif
+ // Skip malformed details rather than replacing the driver's error with our own
+ if (detail.key == null || detail.length < 0 || detail.length > int.MaxValue ||
+ (detail.value == null && detail.length > 0))
+ {
+ continue;
+ }
+
+ byte[] value = new byte[(int)detail.length];
+ if (value.Length > 0)
+ {
+ Marshal.Copy((IntPtr)detail.value, value, 0, value.Length);
+ }
+ details.Add(new KeyValuePair<string, byte[]>(Extensions.MarshalExtensions.PtrToStringUTF8(detail.key)!, value));
+ }
+ return details;
+ }
+ }
+}
diff --git a/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs b/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs
index 25710e865..41f5c48d7 100644
--- a/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs
+++ b/csharp/src/Apache.Arrow.Adbc/C/CAdbcDriverImporter.cs
@@ -281,6 +281,14 @@ namespace Apache.Arrow.Adbc.C
}
}
+ private OptionFunctions? _databaseOptions;
+ private OptionFunctions? _connectionOptions;
+ private OptionFunctions? _statementOptions;
+
+ internal OptionFunctions DatabaseOptions => _databaseOptions ??= OptionFunctions.ForDatabase(ref Driver11);
+ internal OptionFunctions ConnectionOptions => _connectionOptions ??= OptionFunctions.ForConnection(ref Driver11);
+ internal OptionFunctions StatementOptions => _statementOptions ??= OptionFunctions.ForStatement(ref Driver11);
+
internal unsafe ref CAdbcDriver DriverUnsafe
{
get
@@ -322,7 +330,7 @@ namespace Apache.Arrow.Adbc.C
ImportedAdbcDatabase? result = null;
try
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(this))
{
caller.Call(Driver.DatabaseNew, ref nativeDatabase);
@@ -362,25 +370,47 @@ namespace Apache.Arrow.Adbc.C
{
if (parameters == null) throw new ArgumentNullException(nameof(parameters));
+ if (_version < AdbcVersion.Version_1_1_0)
+ {
+ return base.Open(parameters);
+ }
+
CAdbcDatabase nativeDatabase = new CAdbcDatabase();
ImportedAdbcDatabase? result = null;
try
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(this))
{
caller.Call(Driver.DatabaseNew, ref nativeDatabase);
+ }
- foreach (KeyValuePair<string, object> pair in parameters)
+ OptionFunctions functions = DatabaseOptions;
+ CAdbcDatabase* database = &nativeDatabase;
+ foreach (KeyValuePair<string, object> pair in parameters)
+ {
+ if (AdbcOptionValue.TryGetInt64(pair.Value, out long l))
+ {
+ SetOptionInt64(this, functions.SetOptionInt, database, pair.Key, l);
+ }
+ else if (AdbcOptionValue.TryGetDouble(pair.Value, out double d))
+ {
+ SetOptionDouble(this, functions.SetOptionDouble, database, pair.Key, d);
+ }
+ else if (AdbcOptionValue.TryGetBytes(pair.Value, out ReadOnlyMemory<byte> bytes))
{
- switch (pair.Value)
+ SetOptionBytes(this, functions.SetOptionBytes, database, pair.Key, bytes.Span);
+ }
+ else
+ {
+ using (CallHelper caller = new CallHelper(this))
{
- case int intValue:
- case long longValue:
- // TODO!
- break;
+ caller.Call(Driver.DatabaseSetOption, ref nativeDatabase, pair.Key, AdbcOptionValue.ToOptionString(pair.Key, pair.Value));
}
}
+ }
+ using (CallHelper caller = new CallHelper(this))
+ {
caller.Call(Driver.DatabaseInit, ref nativeDatabase);
}
@@ -457,27 +487,86 @@ namespace Apache.Arrow.Adbc.C
}
public unsafe override AdbcConnection Connect(IReadOnlyDictionary<string, string>? options)
+ {
+ return Connect(connection =>
+ {
+ if (options != null)
+ {
+ foreach (KeyValuePair<string, string> pair in options)
+ {
+ SetConnectionOption(connection, pair.Key, pair.Value);
+ }
+ }
+ });
+ }
+
+ /// <inheritdoc />
+ public unsafe override AdbcConnection ConnectWithTypedOptions(IReadOnlyDictionary<string, object>? options)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0)
+ {
+ return base.ConnectWithTypedOptions(options);
+ }
+
+ return Connect(connection =>
+ {
+ if (options == null)
+ {
+ return;
+ }
+
+ OptionFunctions functions = _driver.ConnectionOptions;
+ foreach (KeyValuePair<string, object> pair in options)
+ {
+ if (AdbcOptionValue.TryGetInt64(pair.Value, out long l))
+ {
+ CAdbcDriverImporter.SetOptionInt64(_driver, functions.SetOptionInt, connection, pair.Key, l);
+ }
+ else if (AdbcOptionValue.TryGetDouble(pair.Value, out double d))
+ {
+ CAdbcDriverImporter.SetOptionDouble(_driver, functions.SetOptionDouble, connection, pair.Key, d);
+ }
+ else if (AdbcOptionValue.TryGetBytes(pair.Value, out ReadOnlyMemory<byte> bytes))
+ {
+ CAdbcDriverImporter.SetOptionBytes(_driver, functions.SetOptionBytes, connection, pair.Key, bytes.Span);
+ }
+ else
+ {
+ SetConnectionOption(connection, pair.Key, AdbcOptionValue.ToOptionString(pair.Key, pair.Value));
+ }
+ }
+ });
+ }
+
+ private unsafe delegate void ConfigureConnection(CAdbcConnection* connection);
+
+ private unsafe void SetConnectionOption(CAdbcConnection* connection, string key, string value)
+ {
+ using (CallHelper caller = new CallHelper(_driver))
+ {
+ caller.Call(Driver.ConnectionSetOption, ref *connection, key, value);
+ }
+ }
+
+ private unsafe AdbcConnection Connect(ConfigureConnection configure)
{
CAdbcConnection nativeConnection = new CAdbcConnection();
ImportedAdbcConnection? result = null;
try
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
caller.Call(Driver.ConnectionNew, ref nativeConnection);
+ }
- if (options != null)
- {
- foreach (KeyValuePair<string, string> pair in options)
- {
- caller.Call(Driver.ConnectionSetOption, ref nativeConnection, pair.Key, pair.Value);
- }
- }
+ configure(&nativeConnection);
+ using (CallHelper caller = new CallHelper(_driver))
+ {
caller.Call(Driver.ConnectionInit, ref nativeConnection, ref _nativeDatabase);
-
- result = new ImportedAdbcConnection(_driver, nativeConnection);
}
+
+ result = new ImportedAdbcConnection(_driver, nativeConnection);
}
finally
{
@@ -486,7 +575,7 @@ namespace Apache.Arrow.Adbc.C
// Don't let a failed release hide the error that got us here
try
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
caller.Call(Driver.ConnectionRelease, ref nativeConnection);
}
@@ -502,12 +591,87 @@ namespace Apache.Arrow.Adbc.C
public unsafe override void SetOption(string key, string value)
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
caller.Call(Driver.DatabaseSetOption, ref _nativeDatabase, key, value);
}
}
+ public unsafe override void SetOptionInt64(string key, long value)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { base.SetOptionInt64(key, value); return; }
+ fixed (CAdbcDatabase* database = &_nativeDatabase)
+ {
+ CAdbcDriverImporter.SetOptionInt64(_driver, Options.SetOptionInt, database, key, value);
+ }
+ }
+
+ public unsafe override void SetOptionDouble(string key, double value)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { base.SetOptionDouble(key, value); return; }
+ fixed (CAdbcDatabase* database = &_nativeDatabase)
+ {
+ CAdbcDriverImporter.SetOptionDouble(_driver, Options.SetOptionDouble, database, key, value);
+ }
+ }
+
+ public unsafe override void SetOptionBytes(string key, ReadOnlySpan<byte> value)
+ {
+ fixed (CAdbcDatabase* database = &_nativeDatabase)
+ {
+ CAdbcDriverImporter.SetOptionBytes(_driver, Options.SetOptionBytes, database, key, value);
+ }
+ }
+
+ public unsafe override string GetOption(string key)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { return base.GetOption(key); }
+
+ fixed (CAdbcDatabase* database = &_nativeDatabase)
+ {
+ return GetOptionString(_driver, Options.GetOption, database, key);
+ }
+ }
+
+ public unsafe override long GetOptionInt64(string key)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { return base.GetOptionInt64(key); }
+
+ fixed (CAdbcDatabase* database = &_nativeDatabase)
+ {
+ return CAdbcDriverImporter.GetOptionInt64(_driver, Options.GetOptionInt, database, key);
+ }
+ }
+
+ public unsafe override double GetOptionDouble(string key)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { return base.GetOptionDouble(key); }
+
+ fixed (CAdbcDatabase* database = &_nativeDatabase)
+ {
+ return CAdbcDriverImporter.GetOptionDouble(_driver, Options.GetOptionDouble, database, key);
+ }
+ }
+
+ public unsafe override byte[] GetOptionBytes(string key)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { return base.GetOptionBytes(key); }
+
+ fixed (CAdbcDatabase* database = &_nativeDatabase)
+ {
+ return GetOptionBuffer(_driver, Options.GetOptionBytes, database, key);
+ }
+ }
+
+ private OptionFunctions Options
+ {
+ get
+ {
+ if (_disposed) { throw new ObjectDisposedException(nameof(ImportedAdbcDatabase)); }
+ return _driver.DatabaseOptions;
+ }
+ }
+
public override void Dispose()
{
Dispose(true);
@@ -524,7 +688,7 @@ namespace Apache.Arrow.Adbc.C
{
if (_nativeDatabase.private_data != default)
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
caller.Call(_driver.DriverUnsafe.DatabaseRelease, ref _nativeDatabase);
}
@@ -584,9 +748,14 @@ namespace Apache.Arrow.Adbc.C
}
}
+ private bool IsVersion1_1 => _driver.DriverVersion >= AdbcVersion.Version_1_1_0;
+
public override bool AutoCommit
{
- get => _autoCommit ?? throw AdbcException.NotImplemented("no value has been set for AutoCommit");
+ get => _autoCommit ??
+ (TryGetNativeOption(AdbcOptions.Connection.Autocommit, AdbcOptions.GetEnabled, out bool value)
+ ? value
+ : throw AdbcException.NotImplemented("no value has been set for AutoCommit"));
set
{
SetOption(AdbcOptions.Connection.Autocommit, AdbcOptions.GetEnabled(value));
@@ -596,7 +765,10 @@ namespace Apache.Arrow.Adbc.C
public override IsolationLevel IsolationLevel
{
- get => _isolationLevel ?? IsolationLevel.Default;
+ get => _isolationLevel ??
+ (TryGetNativeOption(AdbcOptions.Connection.IsolationLevel, AdbcOptions.GetIsolationLevel, out IsolationLevel level)
+ ? level
+ : IsolationLevel.Default);
set
{
SetOption(AdbcOptions.Connection.IsolationLevel, AdbcOptions.GetIsolationLevel(value));
@@ -606,7 +778,10 @@ namespace Apache.Arrow.Adbc.C
public override bool ReadOnly
{
- get => _readOnly ?? throw AdbcException.NotImplemented("no value has been set for ReadOnly");
+ get => _readOnly ??
+ (TryGetNativeOption(AdbcOptions.Connection.ReadOnly, AdbcOptions.GetEnabled, out bool value)
+ ? value
+ : throw AdbcException.NotImplemented("no value has been set for ReadOnly"));
set
{
SetOption(AdbcOptions.Connection.ReadOnly, AdbcOptions.GetEnabled(value));
@@ -614,13 +789,182 @@ namespace Apache.Arrow.Adbc.C
}
}
+ public override string? CurrentCatalog
+ {
+ get => GetOptionOrNull(AdbcOptions.Connection.CurrentCatalog);
+ }
+
+ public override string? CurrentDbSchema
+ {
+ get => GetOptionOrNull(AdbcOptions.Connection.CurrentDbSchema);
+ }
+
+ /// <summary>
+ /// Asks an ADBC 1.1 driver for a value that hasn't been set through this object.
+ /// Any failure yields null so that callers keep their ADBC 1.0 behavior.
+ /// </summary>
+ private bool TryGetNativeOption<T>(string key, Func<string, T> parse, out T value)
+ {
+ if (IsVersion1_1)
+ {
+ try
+ {
+ value = parse(GetOption(key));
+ return true;
+ }
+ catch (AdbcException)
+ {
+ }
+ catch (NotSupportedException)
+ {
+ // The driver reported a value that isn't one of the standard ones
+ }
+ }
+
+ value = default!;
+ return false;
+ }
+
+ private string? GetOptionOrNull(string key)
+ {
+ // A 1.0 driver can't report the value. Don't call GetOption, whose 1.0
+ // fallback reads these properties.
+ if (!IsVersion1_1)
+ {
+ return null;
+ }
+
+ try
+ {
+ return GetOption(key);
+ }
+ catch (AdbcException e) when (e.Status == AdbcStatusCode.NotFound || e.Status == AdbcStatusCode.NotImplemented)
+ {
+ return null;
+ }
+ }
+
+ private OptionFunctions Options
+ {
+ get
+ {
+ if (_disposed) { throw new ObjectDisposedException(nameof(ImportedAdbcConnection)); }
+ return _driver.ConnectionOptions;
+ }
+ }
+
+ public unsafe override void SetOptionInt64(string key, long value)
+ {
+ if (!IsVersion1_1) { base.SetOptionInt64(key, value); return; }
+ fixed (CAdbcConnection* connection = &_nativeConnection)
+ {
+ CAdbcDriverImporter.SetOptionInt64(_driver, Options.SetOptionInt, connection, key, value);
+ }
+ }
+
+ public unsafe override void SetOptionDouble(string key, double value)
+ {
+ if (!IsVersion1_1) { base.SetOptionDouble(key, value); return; }
+ fixed (CAdbcConnection* connection = &_nativeConnection)
+ {
+ CAdbcDriverImporter.SetOptionDouble(_driver, Options.SetOptionDouble, connection, key, value);
+ }
+ }
+
+ public unsafe override void SetOptionBytes(string key, ReadOnlySpan<byte> value)
+ {
+ fixed (CAdbcConnection* connection = &_nativeConnection)
+ {
+ CAdbcDriverImporter.SetOptionBytes(_driver, Options.SetOptionBytes, connection, key, value);
+ }
+ }
+
+ public unsafe override string GetOption(string key)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { return base.GetOption(key); }
+
+ fixed (CAdbcConnection* connection = &_nativeConnection)
+ {
+ return GetOptionString(_driver, Options.GetOption, connection, key);
+ }
+ }
+
+ public unsafe override long GetOptionInt64(string key)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { return base.GetOptionInt64(key); }
+
+ fixed (CAdbcConnection* connection = &_nativeConnection)
+ {
+ return CAdbcDriverImporter.GetOptionInt64(_driver, Options.GetOptionInt, connection, key);
+ }
+ }
+
+ public unsafe override double GetOptionDouble(string key)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { return base.GetOptionDouble(key); }
+
+ fixed (CAdbcConnection* connection = &_nativeConnection)
+ {
+ return CAdbcDriverImporter.GetOptionDouble(_driver, Options.GetOptionDouble, connection, key);
+ }
+ }
+
+ public unsafe override byte[] GetOptionBytes(string key)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { return base.GetOptionBytes(key); }
+
+ fixed (CAdbcConnection* connection = &_nativeConnection)
+ {
+ return GetOptionBuffer(_driver, Options.GetOptionBytes, connection, key);
+ }
+ }
+
+ public unsafe override IArrowArrayStream GetStatistics(string? catalogPattern, string? schemaPattern, string tableNamePattern, bool approximate)
+ {
+ using (Utf8Helper utf8Catalog = new Utf8Helper(catalogPattern))
+ using (Utf8Helper utf8Schema = new Utf8Helper(schemaPattern))
+ using (Utf8Helper utf8Table = new Utf8Helper(tableNamePattern))
+ using (CallHelper caller = new CallHelper(_driver))
+ {
+ fixed (CAdbcConnection* connection = &_nativeConnection)
+ {
+ caller.TranslateCode(
+#if NET5_0_OR_GREATER
+ Driver11.ConnectionGetStatistics
+#else
+ Marshal.GetDelegateForFunctionPointer<ConnectionGetStatistics>(Driver11.ConnectionGetStatistics)
+#endif
+ (connection, utf8Catalog, utf8Schema, utf8Table, approximate ? (byte)1 : (byte)0, caller.CreateStream(), &caller._error));
+ return caller.ImportStream();
+ }
+ }
+ }
+
+ public unsafe override IArrowArrayStream GetStatisticsNames()
+ {
+ using (CallHelper caller = new CallHelper(_driver))
+ {
+ fixed (CAdbcConnection* connection = &_nativeConnection)
+ {
+ caller.TranslateCode(
+#if NET5_0_OR_GREATER
+ Driver11.ConnectionGetStatisticNames
+#else
+ Marshal.GetDelegateForFunctionPointer<ConnectionGetStatisticNames>(Driver11.ConnectionGetStatisticNames)
+#endif
+ (connection, caller.CreateStream(), &caller._error));
+ return caller.ImportStream();
+ }
+ }
+ }
+
public unsafe override AdbcStatement CreateStatement()
{
CAdbcStatement nativeStatement = new CAdbcStatement();
ImportedAdbcStatement? result = null;
try
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
fixed (CAdbcConnection* connection = &_nativeConnection)
{
@@ -643,7 +987,7 @@ namespace Apache.Arrow.Adbc.C
// Don't let a failed release hide the error that got us here
try
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
caller.Call(Driver.StatementRelease, ref nativeStatement);
}
@@ -659,7 +1003,7 @@ namespace Apache.Arrow.Adbc.C
public unsafe override IArrowArrayStream GetInfo(IReadOnlyList<AdbcInfoCode> codes)
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
Span<AdbcInfoCode> span = codes.AsSpan();
fixed (CAdbcConnection* connection = &_nativeConnection)
@@ -699,7 +1043,7 @@ namespace Apache.Arrow.Adbc.C
using (Utf8Helper utf8Schema = new Utf8Helper(dbSchemaPattern))
using (Utf8Helper utf8Table = new Utf8Helper(tableNamePattern))
using (Utf8Helper utf8Column = new Utf8Helper(columnNamePattern))
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
fixed (CAdbcConnection* connection = &_nativeConnection)
{
@@ -729,7 +1073,7 @@ namespace Apache.Arrow.Adbc.C
public unsafe override IArrowArrayStream GetTableTypes()
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
fixed (CAdbcConnection* connection = &_nativeConnection)
{
@@ -750,7 +1094,7 @@ namespace Apache.Arrow.Adbc.C
using (Utf8Helper utf8Catalog = new Utf8Helper(catalog))
using (Utf8Helper utf8Schema = new Utf8Helper(db_schema))
using (Utf8Helper utf8Table = new Utf8Helper(table_name))
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
fixed (CAdbcConnection* connection = &_nativeConnection)
{
@@ -819,7 +1163,7 @@ namespace Apache.Arrow.Adbc.C
public unsafe override void Commit()
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
caller.Call(Driver.ConnectionCommit, ref _nativeConnection);
}
@@ -827,7 +1171,7 @@ namespace Apache.Arrow.Adbc.C
public unsafe override void Rollback()
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
caller.Call(Driver.ConnectionRollback, ref _nativeConnection);
}
@@ -835,7 +1179,7 @@ namespace Apache.Arrow.Adbc.C
public unsafe override void Cancel()
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
caller.Call(Driver11.ConnectionCancel, ref _nativeConnection);
}
@@ -843,7 +1187,7 @@ namespace Apache.Arrow.Adbc.C
public unsafe override void SetOption(string key, string value)
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
caller.Call(Driver.ConnectionSetOption, ref _nativeConnection, key, value);
}
@@ -865,7 +1209,7 @@ namespace Apache.Arrow.Adbc.C
{
if (_nativeConnection.private_data != default)
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
caller.Call(_driver.DriverUnsafe.ConnectionRelease, ref _nativeConnection);
}
@@ -937,7 +1281,7 @@ namespace Apache.Arrow.Adbc.C
get => _substraitPlan;
set
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
fixed (CAdbcStatement* statement = &_nativeStatement)
fixed (byte* substraitPlan = value)
@@ -957,7 +1301,7 @@ namespace Apache.Arrow.Adbc.C
public unsafe override void Bind(RecordBatch batch, Schema schema)
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
fixed (CAdbcStatement* statement = &_nativeStatement)
{
@@ -981,7 +1325,7 @@ namespace Apache.Arrow.Adbc.C
public unsafe override void BindStream(IArrowArrayStream stream)
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
fixed (CAdbcStatement* statement = &_nativeStatement)
{
@@ -1002,7 +1346,7 @@ namespace Apache.Arrow.Adbc.C
public unsafe override QueryResult ExecuteQuery()
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
fixed (CAdbcStatement* statement = &_nativeStatement)
{
@@ -1022,7 +1366,7 @@ namespace Apache.Arrow.Adbc.C
public override unsafe Schema ExecuteSchema()
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
fixed (CAdbcStatement* statement = &_nativeStatement)
{
@@ -1041,7 +1385,7 @@ namespace Apache.Arrow.Adbc.C
public unsafe override UpdateResult ExecuteUpdate()
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
fixed (CAdbcStatement* statement = &_nativeStatement)
{
@@ -1061,7 +1405,7 @@ namespace Apache.Arrow.Adbc.C
public unsafe override PartitionedResult ExecutePartitioned()
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
fixed (CAdbcStatement* statement = &_nativeStatement)
{
@@ -1111,7 +1455,7 @@ namespace Apache.Arrow.Adbc.C
public unsafe override Schema GetParameterSchema()
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
fixed (CAdbcStatement* statement = &_nativeStatement)
{
@@ -1129,7 +1473,7 @@ namespace Apache.Arrow.Adbc.C
public unsafe override void Prepare()
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
fixed (CAdbcStatement* statement = &_nativeStatement)
{
@@ -1146,15 +1490,90 @@ namespace Apache.Arrow.Adbc.C
public unsafe override void SetOption(string key, string value)
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
caller.Call(Driver.StatementSetOption, ref _nativeStatement, key, value);
}
}
+ private OptionFunctions Options
+ {
+ get
+ {
+ if (_disposed) { throw new ObjectDisposedException(nameof(ImportedAdbcStatement)); }
+ return _driver.StatementOptions;
+ }
+ }
+
+ public unsafe override void SetOptionInt64(string key, long value)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { base.SetOptionInt64(key, value); return; }
+ fixed (CAdbcStatement* statement = &_nativeStatement)
+ {
+ CAdbcDriverImporter.SetOptionInt64(_driver, Options.SetOptionInt, statement, key, value);
+ }
+ }
+
+ public unsafe override void SetOptionDouble(string key, double value)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { base.SetOptionDouble(key, value); return; }
+ fixed (CAdbcStatement* statement = &_nativeStatement)
+ {
+ CAdbcDriverImporter.SetOptionDouble(_driver, Options.SetOptionDouble, statement, key, value);
+ }
+ }
+
+ public unsafe override void SetOptionBytes(string key, ReadOnlySpan<byte> value)
+ {
+ fixed (CAdbcStatement* statement = &_nativeStatement)
+ {
+ CAdbcDriverImporter.SetOptionBytes(_driver, Options.SetOptionBytes, statement, key, value);
+ }
+ }
+
+ public unsafe override string GetOption(string key)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { return base.GetOption(key); }
+
+ fixed (CAdbcStatement* statement = &_nativeStatement)
+ {
+ return GetOptionString(_driver, Options.GetOption, statement, key);
+ }
+ }
+
+ public unsafe override long GetOptionInt64(string key)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { return base.GetOptionInt64(key); }
+
+ fixed (CAdbcStatement* statement = &_nativeStatement)
+ {
+ return CAdbcDriverImporter.GetOptionInt64(_driver, Options.GetOptionInt, statement, key);
+ }
+ }
+
+ public unsafe override double GetOptionDouble(string key)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { return base.GetOptionDouble(key); }
+
+ fixed (CAdbcStatement* statement = &_nativeStatement)
+ {
+ return CAdbcDriverImporter.GetOptionDouble(_driver, Options.GetOptionDouble, statement, key);
+ }
+ }
+
+ public unsafe override byte[] GetOptionBytes(string key)
+ {
+ if (_driver.DriverVersion < AdbcVersion.Version_1_1_0) { return base.GetOptionBytes(key); }
+
+ fixed (CAdbcStatement* statement = &_nativeStatement)
+ {
+ return GetOptionBuffer(_driver, Options.GetOptionBytes, statement, key);
+ }
+ }
+
public unsafe override void Cancel()
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
caller.Call(Driver11.StatementCancel, ref _nativeStatement);
}
@@ -1176,7 +1595,7 @@ namespace Apache.Arrow.Adbc.C
{
if (_nativeStatement.private_data != default)
{
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
caller.Call(_driver.DriverUnsafe.StatementRelease, ref _nativeStatement);
}
@@ -1199,7 +1618,7 @@ namespace Apache.Arrow.Adbc.C
fixed (CAdbcStatement* statement = &_nativeStatement)
{
using (Utf8Helper query = new Utf8Helper(sqlQuery))
- using (CallHelper caller = new CallHelper())
+ using (CallHelper caller = new CallHelper(_driver))
{
caller.TranslateCode(
#if NET5_0_OR_GREATER
@@ -1217,8 +1636,9 @@ namespace Apache.Arrow.Adbc.C
{
private readonly string? _sqlState;
private readonly int _nativeError;
+ private readonly IReadOnlyList<KeyValuePair<string, byte[]>>? _details;
- public unsafe ImportedAdbcException(ref CAdbcError error, AdbcStatusCode statusCode)
+ public unsafe ImportedAdbcException(ref CAdbcError error, AdbcStatusCode statusCode, IReadOnlyList<KeyValuePair<string, byte[]>>? details = null)
: base(MarshalExtensions.PtrToStringUTF8(error.message) ?? "Undefined error", statusCode)
{
if (error.sqlstate0 != 0)
@@ -1228,16 +1648,16 @@ namespace Apache.Arrow.Adbc.C
_sqlState = Encoding.ASCII.GetString(&fixedError->sqlstate0, 5);
}
}
-
- // INT32_MIN marks an ADBC 1.1 error with details rather than a vendor code
- if (error.vendor_code != int.MinValue)
+ if (error.vendor_code != CAdbcDriverExporter.ErrorVendorCodePrivateData)
{
_nativeError = error.vendor_code;
}
+ _details = details;
}
public override string? SqlState => _sqlState;
public override int NativeError => _nativeError;
+ public override IReadOnlyList<KeyValuePair<string, byte[]>> Details => _details ?? base.Details;
}
/// <summary>
@@ -1265,62 +1685,6 @@ namespace Apache.Arrow.Adbc.C
}
}
- /// <summary>
- /// Assists with fetching values of arbitrary length
- /// </summary>
- private struct GetBufferHelper : IDisposable
- {
- const int DefaultLength = 256;
-
- private IntPtr _buffer;
- public IntPtr Length;
-
- public GetBufferHelper()
- {
- Length = (IntPtr)DefaultLength;
- _buffer = Marshal.AllocHGlobal(Length);
- }
-
- public bool RequiresRetry()
- {
- if (checked((uint)Length) <= DefaultLength)
- {
- return false;
- }
-
- Marshal.FreeHGlobal(_buffer);
- _buffer = IntPtr.Zero;
- _buffer = Marshal.AllocHGlobal(Length);
- return true;
- }
-
- public unsafe string AsString()
- {
- return Encoding.UTF8.GetString((byte*)_buffer, checked((int)Length));
- }
-
- public unsafe byte[] AsBytes()
- {
- byte[] result = new byte[checked((int)Length)];
- fixed (byte* ptr = result)
- {
- Buffer.MemoryCopy((byte*)_buffer, ptr, result.Length, result.Length);
- }
- return result;
- }
-
- public static unsafe implicit operator byte*(GetBufferHelper s) { return (byte*)s._buffer; }
-
- public void Dispose()
- {
- if (_buffer != IntPtr.Zero)
- {
- Marshal.FreeHGlobal(_buffer);
- _buffer = IntPtr.Zero;
- }
- }
- }
-
/// <summary>
/// Assists with delegate calls and handling error codes
/// </summary>
@@ -1330,6 +1694,21 @@ namespace Apache.Arrow.Adbc.C
private CArrowSchema* _schema;
private CArrowArray* _array;
private CArrowArrayStream* _arrayStream;
+ private readonly ImportedAdbcDriver? _driver;
+
+ /// <summary>
+ /// Creates a helper for calling into <paramref name="driver"/>. For an ADBC 1.1.0
+ /// driver this requests extended error information (see <see cref="AdbcException.Details"/>).
+ /// </summary>
+ public CallHelper(ImportedAdbcDriver driver)
+ {
+ this = default;
+ _driver = driver;
+ if (driver.DriverVersion >= AdbcVersion.Version_1_1_0)
+ {
+ _error.vendor_code = CAdbcDriverExporter.ErrorVendorCodePrivateData;
+ }
+ }
public CArrowSchema* Schema => _schema;
public CArrowArray* Array => _array;
@@ -1460,37 +1839,6 @@ namespace Apache.Arrow.Adbc.C
}
}
-#if NET5_0_OR_GREATER
- public unsafe string Call(delegate* unmanaged<CAdbcDatabase*, byte*, byte*, nint*, CAdbcError*, AdbcStatusCode> fn, ref CAdbcDatabase nativeDatabase, string key)
-#else
- public unsafe string Call(IntPtr fn, ref CAdbcDatabase nativeDatabase, string key)
-#endif
- {
- fixed (CAdbcDatabase* db = &nativeDatabase)
- fixed (CAdbcError* e = &_error)
- {
- using (Utf8Helper utf8Key = new Utf8Helper(key))
- using (GetBufferHelper value = new GetBufferHelper())
- {
-#if NET5_0_OR_GREATER
- TranslateCode(fn(db, utf8Key, value, &value.Length, e));
-#else
- TranslateCode(Marshal.GetDelegateForFunctionPointer<DatabaseGetOption>(fn)(db, utf8Key, value, &value.Length, e));
-#endif
- if (value.RequiresRetry())
- {
-#if NET5_0_OR_GREATER
- TranslateCode(fn(db, utf8Key, value, &value.Length, e));
-#else
- TranslateCode(Marshal.GetDelegateForFunctionPointer<DatabaseGetOption>(fn)(db, utf8Key, value, &value.Length, e));
-#endif
- }
-
- return value.AsString();
- }
- }
- }
-
#if NET5_0_OR_GREATER
public unsafe void Call(delegate* unmanaged<CAdbcConnection*, CAdbcError*, AdbcStatusCode> fn, ref CAdbcConnection nativeConnection)
{
@@ -1645,7 +1993,12 @@ namespace Apache.Arrow.Adbc.C
{
if (statusCode != AdbcStatusCode.Success)
{
- ImportedAdbcException exception = new ImportedAdbcException(ref _error, statusCode);
+ IReadOnlyList<KeyValuePair<string, byte[]>>? details;
+ fixed (CAdbcError* error = &_error)
+ {
+ details = ReadErrorDetails(_driver, error);
+ }
+ ImportedAdbcException exception = new ImportedAdbcException(ref _error, statusCode, details);
Dispose();
diff --git a/csharp/src/Apache.Arrow.Adbc/C/Delegates.cs b/csharp/src/Apache.Arrow.Adbc/C/Delegates.cs
index feef7f4a7..e2838129b 100644
--- a/csharp/src/Apache.Arrow.Adbc/C/Delegates.cs
+++ b/csharp/src/Apache.Arrow.Adbc/C/Delegates.cs
@@ -79,5 +79,14 @@ namespace Apache.Arrow.Adbc.C
internal unsafe delegate AdbcStatusCode StatementSetOptionInt(CAdbcStatement* statement, byte* name, long value, CAdbcError* error);
internal unsafe delegate AdbcStatusCode StatementSetSqlQuery(CAdbcStatement* statement, byte* text, CAdbcError* error);
internal unsafe delegate AdbcStatusCode StatementSetSubstraitPlan(CAdbcStatement* statement, byte* plan, int length, CAdbcError* error);
+
+ // Shapes shared by the database, connection and statement option functions, with the
+ // handle passed as void*. The importer uses these to call any of them through one code path.
+ internal unsafe delegate AdbcStatusCode HandleGetOption(void* handle, byte* name, byte* value, nint* length, CAdbcError* error);
+ internal unsafe delegate AdbcStatusCode HandleGetOptionDouble(void* handle, byte* name, double* value, CAdbcError* error);
+ internal unsafe delegate AdbcStatusCode HandleGetOptionInt(void* handle, byte* name, long* value, CAdbcError* error);
+ internal unsafe delegate AdbcStatusCode HandleSetOptionBytes(void* handle, byte* name, byte* value, nint length, CAdbcError* error);
+ internal unsafe delegate AdbcStatusCode HandleSetOptionDouble(void* handle, byte* name, double value, CAdbcError* error);
+ internal unsafe delegate AdbcStatusCode HandleSetOptionInt(void* handle, byte* name, long value, CAdbcError* error);
#endif
}
diff --git a/csharp/test/AotInterop/Apache.Arrow.Adbc.TestFixture.Native/FixtureDriver.cs b/csharp/test/AotInterop/Apache.Arrow.Adbc.TestFixture.Native/FixtureDriver.cs
index 9fd7da343..dcfba3364 100644
--- a/csharp/test/AotInterop/Apache.Arrow.Adbc.TestFixture.Native/FixtureDriver.cs
+++ b/csharp/test/AotInterop/Apache.Arrow.Adbc.TestFixture.Native/FixtureDriver.cs
@@ -78,8 +78,19 @@ namespace Apache.Arrow.Adbc.TestFixture.Native
public FixtureConnection(Dictionary<string, string> options) { _options = options; }
+ private readonly Dictionary<string, long> _intOptions = new Dictionary<string, long>();
+
public override void SetOption(string key, string value) => _options[key] = value;
+ // ADBC 1.1.0 typed and readable options
+ public override void SetOptionInt64(string key, long value) => _intOptions[key] = value;
+
+ public override long GetOptionInt64(string key) =>
+ _intOptions.TryGetValue(key, out long value) ? value : base.GetOptionInt64(key);
+
+ public override string GetOption(string key) =>
+ _options.TryGetValue(key, out string? value) ? value : base.GetOption(key);
+
public override void Cancel() => _cancelled = true;
// Echoes the driver options under a fixed schema so the consumer can assert
@@ -130,6 +141,10 @@ namespace Apache.Arrow.Adbc.TestFixture.Native
public override void SetOption(string key, string value) => _statementOptions[key] = value;
+ // Reports a fixed progress value so the consumer can exercise StatementGetOptionDouble.
+ public override double GetOptionDouble(string key) =>
+ key == AdbcOptions.Statement.Progress ? 0.5 : base.GetOptionDouble(key);
+
public override void Cancel() => _cancelled = true;
// Returns a fixed schema independent of the query. ExecuteSchema must NOT
@@ -138,6 +153,11 @@ namespace Apache.Arrow.Adbc.TestFixture.Native
public override QueryResult ExecuteQuery()
{
+ if (_sqlQuery == "FAIL")
+ {
+ throw new DetailedException();
+ }
+
_executed = true;
// The result encodes the inputs the C ABI carried into this method so the
@@ -194,6 +214,21 @@ namespace Apache.Arrow.Adbc.TestFixture.Native
internal bool WasExecuted => _executed;
}
+ /// <summary>
+ /// An error carrying ADBC 1.1.0 error details across the C ABI.
+ /// </summary>
+ internal sealed class DetailedException : AdbcException
+ {
+ public DetailedException() : base("fixture failure", AdbcStatusCode.InvalidArgument) { }
+
+ public override string? SqlState => "42000";
+
+ public override IReadOnlyList<KeyValuePair<string, byte[]>> Details => new[]
+ {
+ new KeyValuePair<string, byte[]>("fixture.detail", new byte[] { 1, 2, 3 }),
+ };
+ }
+
/// <summary>
/// Single-batch IArrowArrayStream used to return deterministic data.
/// </summary>
diff --git a/csharp/test/AotInterop/Apache.Arrow.Adbc.TestFixture.Tests/AotFixtureTests.cs b/csharp/test/AotInterop/Apache.Arrow.Adbc.TestFixture.Tests/AotFixtureTests.cs
index ccbc15a47..3a98bc5f6 100644
--- a/csharp/test/AotInterop/Apache.Arrow.Adbc.TestFixture.Tests/AotFixtureTests.cs
+++ b/csharp/test/AotInterop/Apache.Arrow.Adbc.TestFixture.Tests/AotFixtureTests.cs
@@ -230,5 +230,50 @@ namespace Apache.Arrow.Adbc.TestFixture.Tests
() => conn.GetObjects(AdbcConnection.GetObjectsDepth.All, null, null, null, null, null));
Assert.Equal(AdbcStatusCode.NotImplemented, ex.Status);
}
+
+ [SkippableFact]
+ public void ConnectionOptionsRoundTripThroughAotBoundary()
+ {
+ using AdbcDriver driver = LoadFixture();
+ using AdbcDatabase db = driver.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(new Dictionary<string, string> { { "conn.user", "alice" } });
+
+ Assert.Equal("alice", conn.GetOption("conn.user"));
+ Assert.True(conn.AutoCommit);
+
+ conn.SetOptionInt64("conn.timeout", 42);
+ Assert.Equal(42L, conn.GetOptionInt64("conn.timeout"));
+
+ AdbcException ex = Assert.ThrowsAny<AdbcException>(() => conn.GetOption("conn.missing"));
+ Assert.Equal(AdbcStatusCode.NotFound, ex.Status);
+ }
+
+ [SkippableFact]
+ public void StatementProgressIsReadable()
+ {
+ using AdbcDriver driver = LoadFixture();
+ using AdbcDatabase db = driver.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(null);
+ using AdbcStatement stmt = conn.CreateStatement();
+
+ Assert.Equal(0.5, stmt.GetOptionDouble(AdbcOptions.Statement.Progress));
+ }
+
+ [SkippableFact]
+ public void ErrorDetailsCrossAotBoundary()
+ {
+ using AdbcDriver driver = LoadFixture();
+ using AdbcDatabase db = driver.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(null);
+ using AdbcStatement stmt = conn.CreateStatement();
+ stmt.SqlQuery = "FAIL";
+
+ AdbcException ex = Assert.ThrowsAny<AdbcException>(() => stmt.ExecuteQuery());
+ Assert.Equal(AdbcStatusCode.InvalidArgument, ex.Status);
+ Assert.Equal("42000", ex.SqlState);
+ KeyValuePair<string, byte[]> detail = Assert.Single(ex.Details);
+ Assert.Equal("fixture.detail", detail.Key);
+ Assert.Equal(new byte[] { 1, 2, 3 }, detail.Value);
+ }
}
}
diff --git a/csharp/test/Apache.Arrow.Adbc.Tests/BaseClassDefaultsTests.cs b/csharp/test/Apache.Arrow.Adbc.Tests/BaseClassDefaultsTests.cs
new file mode 100644
index 000000000..dcf3e88a9
--- /dev/null
+++ b/csharp/test/Apache.Arrow.Adbc.Tests/BaseClassDefaultsTests.cs
@@ -0,0 +1,278 @@
+/*
+ * 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.
+ */
+
+using System;
+using System.Collections.Generic;
+using Apache.Arrow.Ipc;
+using Xunit;
+
+namespace Apache.Arrow.Adbc.Tests
+{
+ /// <summary>
+ /// Tests the default implementations of the ADBC 1.1.0 members of the ADBC
+ /// base classes.
+ /// </summary>
+ public class BaseClassDefaultsTests
+ {
+ [Fact]
+ public void DoubleOptionsRoundTripThroughStrings()
+ {
+ // "R" formatting doesn't round-trip this value on .NET Framework
+ const double value = 0.84551240822557006;
+ var stmt = new OptionsStatement();
+
+ stmt.SetOptionDouble("d", value);
+ Assert.Equal(value, stmt.GetOptionDouble("d"));
+ }
+
+ [Fact]
+ public void TypedOptionsDefaultToStringOptions()
+ {
+ var stmt = new OptionsStatement();
+
+ stmt.SetOptionInt64("i", -12);
+ stmt.SetOptionDouble("d", 0.1);
+
+ Assert.Equal("-12", stmt.Options["i"]);
+ Assert.Equal("0.1", stmt.Options["d"]);
+ Assert.Equal(-12L, stmt.GetOptionInt64("i"));
+ Assert.Equal(0.1, stmt.GetOptionDouble("d"));
+
+ stmt.SetOption("s", "not a number");
+ Assert.Equal(AdbcStatusCode.InvalidArgument, Assert.Throws<AdbcException>(() => stmt.GetOptionInt64("s")).Status);
+ Assert.Equal(AdbcStatusCode.NotImplemented, Assert.Throws<AdbcException>(() => stmt.SetOptionBytes("b", new byte[1])).Status);
+ Assert.Equal(AdbcStatusCode.NotFound, Assert.Throws<AdbcException>(() => stmt.GetOptionBytes("b")).Status);
+ }
+
+ [Fact]
+ public void ConnectionGetOptionAnswersStandardOptions()
+ {
+ var conn = new RecordingConnection();
+
+ Assert.Equal(AdbcOptions.Enabled, conn.GetOption(AdbcOptions.Connection.Autocommit));
+ Assert.Equal(AdbcOptions.Disabled, conn.GetOption(AdbcOptions.Connection.ReadOnly));
+ Assert.Equal(AdbcOptions.IsolationLevels.Default, conn.GetOption(AdbcOptions.Connection.IsolationLevel));
+ Assert.Equal(AdbcStatusCode.NotFound, Assert.Throws<AdbcException>(() => conn.GetOption(AdbcOptions.Connection.CurrentCatalog)).Status);
+ Assert.Equal(AdbcStatusCode.NotFound, Assert.Throws<AdbcException>(() => conn.GetOption("unknown")).Status);
+ }
+
+ [Fact]
+ public void CurrentCatalogSetterDefaultsToSetOption()
+ {
+ var conn = new RecordingConnection();
+
+ conn.CurrentCatalog = "cat";
+ conn.CurrentDbSchema = "schema";
+
+ Assert.Equal("cat", conn.Options[AdbcOptions.Connection.CurrentCatalog]);
+ Assert.Equal("schema", conn.Options[AdbcOptions.Connection.CurrentDbSchema]);
+ }
+
+ [Fact]
+ public void ConnectWithTypedOptionsDefaultsToConnect()
+ {
+ var db = new ConnectingDatabase();
+
+ using AdbcConnection conn = db.ConnectWithTypedOptions(new Dictionary<string, object>
+ {
+ { "s", "text" },
+ { "b", false },
+ { "l", 4L },
+ { "d", 2.5 },
+ { "bytes", new byte[] { 1, 2 } },
+ { "memory", new ReadOnlyMemory<byte>(new byte[] { 3 }) },
+ });
+
+ Assert.Equal(new Dictionary<string, string> { { "s", "text" }, { "b", "false" }, { "l", "4" }, { "d", "2.5" } }, db.LastStringOptions);
+ var binary = ((RecordingConnection)conn).BinaryOptions;
+ Assert.Equal(new byte[] { 1, 2 }, binary["bytes"]);
+ Assert.Equal(new byte[] { 3 }, binary["memory"]);
+
+ using AdbcConnection noOptions = db.ConnectWithTypedOptions(null);
+ Assert.Null(db.LastStringOptions);
+ }
+
+ [Fact]
+ public void ConnectWithTypedOptionsDisposesTheConnectionIfABinaryOptionFails()
+ {
+ var db = new ConnectingDatabase();
+
+ Assert.Throws<AdbcException>(() => db.ConnectWithTypedOptions(new Dictionary<string, object>
+ {
+ { RecordingConnection.RejectedKey, new byte[] { 1 } },
+ }));
+ Assert.True(db.LastConnection!.Disposed);
+ }
+
+ [Fact]
+ public void DatabaseObjectSetOptionDispatchesByType()
+ {
+ var db = new RecordingDatabase();
+
+ db.SetOption("s", (object)"text");
+ db.SetOption("b", (object)true);
+ db.SetOption("i", (object)3);
+ db.SetOption("l", (object)4L);
+ db.SetOption("f", (object)1.5f);
+ db.SetOption("d", (object)2.5);
+ db.SetOption("bytes", (object)new byte[] { 9 });
+
+ Assert.Equal("text", db.Options["s"]);
+ Assert.Equal("true", db.Options["b"]);
+ Assert.Equal(3L, db.Options["i"]);
+ Assert.Equal(4L, db.Options["l"]);
+ Assert.Equal(1.5, db.Options["f"]);
+ Assert.Equal(2.5, db.Options["d"]);
+ Assert.Equal(new byte[] { 9 }, db.Options["bytes"]);
+
+ Assert.Equal(AdbcStatusCode.InvalidArgument, Assert.Throws<AdbcException>(() => db.SetOption("x", (object)DateTime.Now)).Status);
+ }
+
+ [Fact]
+ public void DriverOpenWithObjectsConvertsToStrings()
+ {
+ var driver = new StringOnlyDriver();
+
+ driver.Open(new Dictionary<string, object> { { "n", 5 }, { "d", 0.5 }, { "s", "x" }, { "b", false } });
+
+ Assert.Equal("5", driver.Parameters!["n"]);
+ Assert.Equal("0.5", driver.Parameters["d"]);
+ Assert.Equal("x", driver.Parameters["s"]);
+ Assert.Equal("false", driver.Parameters["b"]);
+ }
+
+ [Fact]
+ public void DriverOpenWithObjectsAppliesBinaryValuesAfterOpening()
+ {
+ var driver = new StringOnlyDriver();
+
+ AdbcDatabase db = driver.Open(new Dictionary<string, object> { { "s", "x" }, { "bytes", new byte[] { 9 } } });
+
+ Assert.Equal("x", driver.Parameters!["s"]);
+ Assert.False(driver.Parameters.ContainsKey("bytes"));
+ Assert.Equal(new byte[] { 9 }, ((RecordingDatabase)db).Options["bytes"]);
+ }
+
+ [Fact]
+ public void DriverOpenWithObjectsDisposesTheDatabaseIfABinaryValueFails()
+ {
+ var driver = new StringOnlyDriver();
+
+ AdbcException ex = Assert.Throws<AdbcException>(() => driver.Open(new Dictionary<string, object>
+ {
+ { RecordingDatabase.RejectedKey, new byte[] { 1 } },
+ }));
+ Assert.Equal(AdbcStatusCode.InvalidArgument, ex.Status);
+ Assert.True(driver.LastDatabase!.Disposed);
+ }
+
+ private sealed class OptionsStatement : AdbcStatement
+ {
+ public Dictionary<string, string> Options { get; } = new Dictionary<string, string>();
+
+ public override void SetOption(string key, string value) => Options[key] = value;
+ public override string GetOption(string key) => Options.TryGetValue(key, out string? v) ? v : base.GetOption(key);
+
+ public override QueryResult ExecuteQuery() => throw new NotImplementedException();
+ public override UpdateResult ExecuteUpdate() => throw new NotImplementedException();
+ }
+
+ private sealed class RecordingConnection : AdbcConnection
+ {
+ public Dictionary<string, string> Options { get; } = new Dictionary<string, string>();
+
+ public const string RejectedKey = "test.rejected";
+
+ public Dictionary<string, byte[]> BinaryOptions { get; } = new Dictionary<string, byte[]>();
+ public bool Disposed { get; private set; }
+
+ public override void SetOptionBytes(string key, ReadOnlySpan<byte> value)
+ {
+ if (key == RejectedKey)
+ {
+ throw new AdbcException("rejected", AdbcStatusCode.InvalidArgument);
+ }
+ BinaryOptions[key] = value.ToArray();
+ }
+
+ public override void Dispose() => Disposed = true;
+
+ public override void SetOption(string key, string value) => Options[key] = value;
+
+ public override IArrowArrayStream GetTableTypes() => throw new NotImplementedException();
+
+ public override AdbcStatement CreateStatement() => throw new NotImplementedException();
+
+ public override IArrowArrayStream GetObjects(
+ GetObjectsDepth depth, string? catalogPattern, string? dbSchemaPattern,
+ string? tableNamePattern, IReadOnlyList<string>? tableTypes, string? columnNamePattern)
+ => throw new NotImplementedException();
+
+ public override Schema GetTableSchema(string? catalog, string? dbSchema, string tableName)
+ => throw new NotImplementedException();
+ }
+
+ private sealed class ConnectingDatabase : AdbcDatabase
+ {
+ public IReadOnlyDictionary<string, string>? LastStringOptions { get; private set; }
+ public RecordingConnection? LastConnection { get; private set; }
+
+ public override AdbcConnection Connect(IReadOnlyDictionary<string, string>? options)
+ {
+ LastStringOptions = options;
+ return LastConnection = new RecordingConnection();
+ }
+ }
+
+ private sealed class RecordingDatabase : AdbcDatabase
+ {
+ public const string RejectedKey = "test.rejected";
+
+ public Dictionary<string, object> Options { get; } = new Dictionary<string, object>();
+ public bool Disposed { get; private set; }
+
+ public override void SetOption(string key, string value) => Options[key] = value;
+ public override void SetOptionInt64(string key, long value) => Options[key] = value;
+ public override void SetOptionDouble(string key, double value) => Options[key] = value;
+
+ public override void SetOptionBytes(string key, ReadOnlySpan<byte> value)
+ {
+ if (key == RejectedKey)
+ {
+ throw new AdbcException("rejected", AdbcStatusCode.InvalidArgument);
+ }
+ Options[key] = value.ToArray();
+ }
+
+ public override void Dispose() => Disposed = true;
+
+ public override AdbcConnection Connect(IReadOnlyDictionary<string, string>? options) => throw new NotImplementedException();
+ }
+
+ private sealed class StringOnlyDriver : AdbcDriver
+ {
+ public IReadOnlyDictionary<string, string>? Parameters { get; private set; }
+ public RecordingDatabase? LastDatabase { get; private set; }
+
+ public override AdbcDatabase Open(IReadOnlyDictionary<string, string> parameters)
+ {
+ Parameters = parameters;
+ return LastDatabase = new RecordingDatabase();
+ }
+ }
+ }
+}
diff --git a/csharp/test/Apache.Arrow.Adbc.Tests/ExportedDriverRoundTrip11Tests.cs b/csharp/test/Apache.Arrow.Adbc.Tests/ExportedDriverRoundTrip11Tests.cs
new file mode 100644
index 000000000..6d8877e2a
--- /dev/null
+++ b/csharp/test/Apache.Arrow.Adbc.Tests/ExportedDriverRoundTrip11Tests.cs
@@ -0,0 +1,691 @@
+/*
+ * 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.
+ */
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using Apache.Arrow.Adbc.C;
+using Apache.Arrow.Ipc;
+using Apache.Arrow.Types;
+using Xunit;
+
+namespace Apache.Arrow.Adbc.Tests
+{
+ /// <summary>
+ /// Round-trips the ADBC 1.1.0 API surface through <see cref="CAdbcDriverExporter"/>
+ /// and <see cref="CAdbcDriverImporter"/>: typed options, statistics, error details
+ /// and the standard connection options.
+ /// </summary>
+ public class ExportedDriverRoundTrip11Tests
+ {
+ [Fact]
+ public void TypedOpenParametersAreMarshaledToProducer()
+ {
+ var fixture = new Driver11();
+ using AdbcDriver imported = Load(fixture);
+
+ using AdbcDatabase db = imported.Open(new Dictionary<string, object>
+ {
+ { "string", "value" },
+ { "int", 42L },
+ { "double", 2.5 },
+ { "bytes", new byte[] { 1, 2, 3 } },
+ { "bool", true },
+ });
+
+ Dictionary<string, object> options = fixture.LastDatabase!.Options;
+ Assert.Equal("value", options["string"]);
+ Assert.Equal(42L, options["int"]);
+ Assert.Equal(2.5, options["double"]);
+ Assert.Equal(new byte[] { 1, 2, 3 }, options["bytes"]);
+ Assert.Equal("true", options["bool"]);
+ }
+
+ [Fact]
+ public void DatabaseTypedOptionsRoundTrip()
+ {
+ var fixture = new Driver11();
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+
+ db.SetOption("s", "text");
+ db.SetOptionInt64("i", long.MinValue);
+ db.SetOptionDouble("d", -0.125);
+ db.SetOptionBytes("b", new byte[] { 0, 255 });
+
+ Assert.Equal("text", db.GetOption("s"));
+ Assert.Equal(long.MinValue, db.GetOptionInt64("i"));
+ Assert.Equal(-0.125, db.GetOptionDouble("d"));
+ Assert.Equal(new byte[] { 0, 255 }, db.GetOptionBytes("b"));
+ }
+
+ [Fact]
+ public void DatabaseGetOptionReportsOptionsTheProducerDoesNotTrack()
+ {
+ // A 1.0-style producer that never overrode GetOption still answers
+ // DatabaseGetOption for options set through the C API.
+ var fixture = new Driver11 { DatabaseTracksOptions = false };
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string> { { "uri", "memory://" } });
+
+ Assert.Equal("memory://", db.GetOption("uri"));
+ AdbcException ex = Assert.ThrowsAny<AdbcException>(() => db.GetOption("missing"));
+ Assert.Equal(AdbcStatusCode.NotFound, ex.Status);
+ }
+
+ [Fact]
+ public void LongStringOptionUsesBufferRetry()
+ {
+ var fixture = new Driver11();
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(null);
+ using AdbcStatement stmt = conn.CreateStatement();
+
+ string longValue = new string('x', 1000) + "é";
+ stmt.SetOption("long", longValue);
+ Assert.Equal(longValue, stmt.GetOption("long"));
+
+ byte[] longBytes = new byte[1000];
+ new Random(1).NextBytes(longBytes);
+ stmt.SetOptionBytes("long_bytes", longBytes);
+ Assert.Equal(longBytes, stmt.GetOptionBytes("long_bytes"));
+ }
+
+ [Fact]
+ public void ConnectionTypedOptionsRoundTrip()
+ {
+ var fixture = new Driver11();
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(new Dictionary<string, string> { { "at_connect", "yes" } });
+
+ conn.SetOptionInt64("timeout", 30);
+ conn.SetOptionDouble("ratio", 0.75);
+
+ Assert.Equal(30L, fixture.LastConnection!.Options["timeout"]);
+ Assert.Equal(0.75, fixture.LastConnection!.Options["ratio"]);
+ Assert.Equal(30L, conn.GetOptionInt64("timeout"));
+ Assert.Equal(0.75, conn.GetOptionDouble("ratio"));
+ Assert.Equal("yes", conn.GetOption("at_connect"));
+ }
+
+ [Fact]
+ public void StandardConnectionOptionsComeFromProperties()
+ {
+ var fixture = new Driver11();
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(null);
+
+ // The producer doesn't override GetOption for these keys; the base
+ // AdbcConnection implementation answers from its properties.
+ Assert.Equal(AdbcOptions.Enabled, conn.GetOption(AdbcOptions.Connection.Autocommit));
+ Assert.True(conn.AutoCommit);
+ Assert.Equal(IsolationLevel.Default, conn.IsolationLevel);
+
+ conn.CurrentCatalog = "main";
+ Assert.Equal("main", fixture.LastConnection!.CurrentCatalog);
+ Assert.Equal("main", conn.CurrentCatalog);
+ Assert.Equal("main", conn.GetOption(AdbcOptions.Connection.CurrentCatalog));
+
+ // The producer doesn't support a current schema at all
+ Assert.Null(conn.CurrentDbSchema);
+ }
+
+ [Fact]
+ public void TypedConnectOptionsReachTheProducerBeforeInit()
+ {
+ var fixture = new Driver11 { DatabaseHandlesTypedConnect = true };
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.ConnectWithTypedOptions(new Dictionary<string, object>
+ {
+ { "s", "text" },
+ { "n", 5L },
+ { "d", 0.5 },
+ { "cert", new byte[] { 1, 2, 3 } },
+ });
+
+ IReadOnlyDictionary<string, object> received = fixture.LastTypedConnectOptions!;
+ Assert.Equal("text", received["s"]);
+ Assert.Equal(5L, received["n"]);
+ Assert.Equal(0.5, received["d"]);
+ Assert.Equal(new byte[] { 1, 2, 3 }, received["cert"]);
+ }
+
+ [Fact]
+ public void TypedConnectOptionsFallBackToConnectForExistingProducers()
+ {
+ var fixture = new Driver11();
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.ConnectWithTypedOptions(new Dictionary<string, object>
+ {
+ { "n", 5L },
+ { "cert", new byte[] { 1, 2, 3 } },
+ });
+
+ Assert.Null(fixture.LastTypedConnectOptions);
+ Assert.Equal("5", fixture.LastConnection!.Options["n"]);
+ Assert.Equal(new byte[] { 1, 2, 3 }, fixture.LastConnection!.Options["cert"]);
+ }
+
+ [Fact]
+ public void AutoCommitSetBeforeInitIsReported()
+ {
+ var fixture = new Driver11();
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(new Dictionary<string, string>
+ {
+ { AdbcOptions.Connection.Autocommit, AdbcOptions.Disabled },
+ });
+
+ Assert.False(fixture.LastConnection!.AutoCommitAtConnect);
+ Assert.Equal(AdbcOptions.Disabled, conn.GetOption(AdbcOptions.Connection.Autocommit));
+ Assert.False(conn.AutoCommit);
+ }
+
+ [Fact]
+ public void ExecuteUpdateRoundTripsWithoutAResultStream()
+ {
+ var fixture = new Driver11();
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(null);
+ using AdbcStatement stmt = conn.CreateStatement();
+
+ stmt.SqlQuery = "UPDATE";
+ Assert.Equal(42, stmt.ExecuteUpdate().AffectedRows);
+ }
+
+ [Fact]
+ public void StatisticsAreMarshaled()
+ {
+ var fixture = new Driver11();
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(null);
+
+ using (IArrowArrayStream names = conn.GetStatisticsNames())
+ {
+ Assert.Equal("statistic_name", names.Schema.FieldsList[0].Name);
+ }
+
+ using (IArrowArrayStream stats = conn.GetStatistics("cat", null, "tbl", approximate: true))
+ {
+ Assert.Equal("statistic_name", stats.Schema.FieldsList[0].Name);
+ }
+ Assert.Equal(("cat", (string?)null, "tbl", true), fixture.LastConnection!.LastStatisticsRequest);
+ }
+
+ [Fact]
+ public void ErrorDetailsArePropagated()
+ {
+ var fixture = new Driver11();
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(null);
+ using AdbcStatement stmt = conn.CreateStatement();
+ stmt.SqlQuery = "FAIL";
+
+ AdbcException ex = Assert.ThrowsAny<AdbcException>(() => stmt.ExecuteQuery());
+ Assert.Equal(AdbcStatusCode.InvalidData, ex.Status);
+ Assert.Equal("42000", ex.SqlState);
+ Assert.Contains("query failed", ex.Message);
+ Assert.Equal(2, ex.Details.Count);
+ Assert.Equal("query_id", ex.Details[0].Key);
+ Assert.Equal("q-123", Encoding.UTF8.GetString(ex.Details[0].Value));
+ Assert.Equal("empty", ex.Details[1].Key);
+ Assert.Empty(ex.Details[1].Value);
+ }
+
+ [Fact]
+ public void ExecutePartitionedRoundTrips()
+ {
+ var fixture = new Driver11();
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(null);
+ using AdbcStatement stmt = conn.CreateStatement();
+
+ stmt.SqlQuery = "PARTITIONS";
+ PartitionedResult result = stmt.ExecutePartitioned();
+
+ Assert.Equal(5, result.AffectedRows);
+ Assert.Equal("x", Assert.Single(result.Schema.FieldsList).Name);
+ Assert.Equal(2, result.PartitionDescriptors.Count);
+ Assert.Equal(new byte[] { 1, 2 }, result.PartitionDescriptors[0].Descriptor.ToArray());
+ Assert.Equal(new byte[] { 3 }, result.PartitionDescriptors[1].Descriptor.ToArray());
+ }
+
+ [Fact]
+ public void ExecutePartitionedFailureDoesNotReleaseGarbage()
+ {
+ var fixture = new Driver11();
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(null);
+ using AdbcStatement stmt = conn.CreateStatement();
+
+ // The producer fails without writing the partitions struct
+ stmt.SqlQuery = "OTHER";
+ for (int i = 0; i < 20; i++)
+ {
+ Assert.Equal(AdbcStatusCode.NotImplemented, Assert.ThrowsAny<AdbcException>(() => stmt.ExecutePartitioned()).Status);
+ }
+ }
+
+ [Fact]
+ public void ErrorWithoutDetailsReportsVendorCode()
+ {
+ var fixture = new Driver11();
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(null);
+ using AdbcStatement stmt = conn.CreateStatement();
+ stmt.SqlQuery = "FAIL_PLAIN";
+
+ AdbcException ex = Assert.ThrowsAny<AdbcException>(() => stmt.ExecuteQuery());
+ Assert.Equal("HY000", ex.SqlState);
+ Assert.Equal(1234, ex.NativeError);
+ Assert.Empty(ex.Details);
+ }
+
+ [Fact]
+ public void UnknownOptionIsNotFound()
+ {
+ var fixture = new Driver11();
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(null);
+ using AdbcStatement stmt = conn.CreateStatement();
+
+ Assert.Equal(AdbcStatusCode.NotFound, Assert.ThrowsAny<AdbcException>(() => conn.GetOption("nope")).Status);
+ Assert.Equal(AdbcStatusCode.NotFound, Assert.ThrowsAny<AdbcException>(() => stmt.GetOptionInt64("nope")).Status);
+ }
+
+ [Fact]
+ public void OperationCanceledExceptionIsReportedAsCancelled()
+ {
+ using AdbcDriver imported = Load(new Driver11());
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(null);
+ using AdbcStatement stmt = conn.CreateStatement();
+ stmt.SqlQuery = "FAIL_CANCELED";
+
+ AdbcException ex = Assert.ThrowsAny<AdbcException>(() => stmt.ExecuteQuery());
+ Assert.Equal(AdbcStatusCode.Cancelled, ex.Status);
+ }
+
+ [Fact]
+ public async Task CancelReachesImportedStatement()
+ {
+ var fixture = new Driver11();
+ using AdbcDriver imported = Load(fixture);
+ using AdbcDatabase db = imported.Open(new Dictionary<string, string>());
+ using AdbcConnection conn = db.Connect(null);
+ using AdbcStatement stmt = conn.CreateStatement();
+ stmt.SqlQuery = "BLOCK";
+
+ Task<QueryResult> pending = Task.Run(() => stmt.ExecuteQuery());
+ Assert.True(fixture.LastStatement!.Started.Wait(TimeSpan.FromSeconds(10)));
+ stmt.Cancel();
+
+ AdbcException ex = await Assert.ThrowsAnyAsync<AdbcException>(() => pending);
+ Assert.Equal(AdbcStatusCode.Cancelled, ex.Status);
+ }
+
+ [Fact]
+ public void Version1_0DriverFallsBackForTypedSetters()
+ {
+ var fixture = new Driver11();
+ AdbcDriverInit adapter = (int version, ref CAdbcDriver nativeDriver, ref CAdbcError error) =>
+ {
+ if (version != AdbcVersion.Version_1_0_0) { return AdbcStatusCode.NotImplemented; }
+ return CallExporter(version, ref nativeDriver, ref error, fixture);
+ };
+
+ using AdbcDriver imported = CAdbcDriverImporter.Load(adapter);
+ Assert.Equal(AdbcVersion.Version_1_0_0, imported.DriverVersion);
+
+ using AdbcDatabase db = imported.Open(new Dictionary<string, object> { { "n", 7L } });
+ Assert.Equal("7", fixture.LastDatabase!.Options["n"]);
+
+ using AdbcConnection conn = db.Connect(null);
+ conn.SetOptionInt64("timeout", 5);
+ Assert.Equal("5", fixture.LastConnection!.Options["timeout"]);
+
+ // A 1.0 driver can't report option values, so the getters fall back to the
+ // base class defaults, as the C driver manager does
+ Assert.Equal(AdbcStatusCode.NotFound, Assert.ThrowsAny<AdbcException>(() => conn.GetOption("timeout")).Status);
+ Assert.Equal(AdbcStatusCode.NotFound, Assert.ThrowsAny<AdbcException>(() => conn.GetOptionInt64("timeout")).Status);
+ Assert.Equal(AdbcStatusCode.NotFound, Assert.ThrowsAny<AdbcException>(() => conn.GetOption(AdbcOptions.Connection.Autocommit)).Status);
+ Assert.Equal(AdbcStatusCode.NotFound, Assert.ThrowsAny<AdbcException>(() => db.GetOption("n")).Status);
+ Assert.Null(conn.CurrentCatalog);
+
+ using AdbcStatement stmt = conn.CreateStatement();
+ Assert.Equal(AdbcStatusCode.NotFound, Assert.ThrowsAny<AdbcException>(() => stmt.GetOptionDouble(AdbcOptions.Statement.Progress)).Status);
+ Assert.Equal(AdbcStatusCode.NotFound, Assert.ThrowsAny<AdbcException>(() => stmt.GetOptionBytes("b")).Status);
+
+ // The importer never calls a 1.1 function pointer on a 1.0 driver
+ Assert.Equal(AdbcStatusCode.NotImplemented, Assert.ThrowsAny<AdbcException>(() => db.SetOptionBytes("b", new byte[] { 1 })).Status);
+ Assert.Equal(AdbcStatusCode.NotImplemented, Assert.ThrowsAny<AdbcException>(() => conn.SetOptionBytes("b", new byte[] { 1 })).Status);
+ Assert.Equal(AdbcStatusCode.NotImplemented, Assert.ThrowsAny<AdbcException>(() => stmt.SetOptionBytes("b", new byte[] { 1 })).Status);
+ Assert.Equal(AdbcStatusCode.NotImplemented, Assert.ThrowsAny<AdbcException>(() => conn.GetStatistics(null, null, "t", true)).Status);
+ Assert.Equal(AdbcStatusCode.NotImplemented, Assert.ThrowsAny<AdbcException>(() => conn.GetStatisticsNames()).Status);
+ Assert.Equal(AdbcStatusCode.NotImplemented, Assert.ThrowsAny<AdbcException>(() => stmt.ExecuteSchema()).Status);
+ }
+
+ [Fact]
+ public void Version1_0DriverRejectsBinaryOpenParameters()
+ {
+ var fixture = new Driver11();
+ AdbcDriverInit adapter = (int version, ref CAdbcDriver nativeDriver, ref CAdbcError error) =>
+ {
+ if (version != AdbcVersion.Version_1_0_0) { return AdbcStatusCode.NotImplemented; }
+ return CallExporter(version, ref nativeDriver, ref error, fixture);
+ };
+
+ using AdbcDriver imported = CAdbcDriverImporter.Load(adapter);
+
+ AdbcException ex = Assert.ThrowsAny<AdbcException>(() => imported.Open(new Dictionary<string, object>
+ {
+ { "n", 7L },
+ { "b", new byte[] { 1 } },
+ }));
+ Assert.Equal(AdbcStatusCode.NotImplemented, ex.Status);
+ Assert.True(fixture.LastDatabase!.Disposed);
+ }
+
+ private static AdbcDriver Load(AdbcDriver driver)
+ {
+ AdbcDriver imported = CAdbcDriverImporter.Load(
+ (int version, ref CAdbcDriver nativeDriver, ref CAdbcError error) => CallExporter(version, ref nativeDriver, ref error, driver));
+ Assert.Equal(AdbcVersion.Version_1_1_0, imported.DriverVersion);
+ return imported;
+ }
+
+ private static AdbcStatusCode CallExporter(int version, ref CAdbcDriver nativeDriver, ref CAdbcError error, AdbcDriver driver)
+ {
+ unsafe
+ {
+ fixed (CAdbcDriver* dp = &nativeDriver)
+ fixed (CAdbcError* ep = &error)
+ {
+ return CAdbcDriverExporter.AdbcDriverInit(version, dp, ep, driver);
+ }
+ }
+ }
+
+ /// <summary>
+ /// Stores options with their types so tests can see which typed setter was used.
+ /// </summary>
+ private sealed class OptionBag
+ {
+ public Dictionary<string, object> Values { get; } = new Dictionary<string, object>();
+
+ public object Get(string key) =>
+ Values.TryGetValue(key, out object? value) ? value : throw new AdbcException($"no option {key}", AdbcStatusCode.NotFound);
+ }
+
+ private sealed class Driver11 : AdbcDriver
+ {
+ public bool DatabaseTracksOptions { get; set; } = true;
+ public bool DatabaseHandlesTypedConnect { get; set; }
+ public IReadOnlyDictionary<string, object>? LastTypedConnectOptions { get; set; }
+ public Database11? LastDatabase { get; private set; }
+ public Connection11? LastConnection { get; set; }
+ public Statement11? LastStatement { get; set; }
+
+ public override int DriverVersion => AdbcVersion.Version_1_1_0;
+
+ public override AdbcDatabase Open(IReadOnlyDictionary<string, string> parameters)
+ {
+ var options = new Dictionary<string, object>();
+ foreach (KeyValuePair<string, string> pair in parameters) { options[pair.Key] = pair.Value; }
+ return LastDatabase = new Database11(this, options);
+ }
+
+ public override AdbcDatabase Open(IReadOnlyDictionary<string, object> parameters)
+ {
+ var options = new Dictionary<string, object>();
+ foreach (KeyValuePair<string, object> pair in parameters) { options[pair.Key] = pair.Value; }
+ return LastDatabase = new Database11(this, options);
+ }
+ }
+
+ private sealed class Database11 : AdbcDatabase
+ {
+ private readonly Driver11 _driver;
+ public Dictionary<string, object> Options { get; }
+
+ public Database11(Driver11 driver, Dictionary<string, object> options)
+ {
+ _driver = driver;
+ Options = options;
+ }
+
+ public override void SetOption(string key, string value) => Options[key] = value;
+ public override void SetOptionInt64(string key, long value) => Options[key] = value;
+ public override void SetOptionDouble(string key, double value) => Options[key] = value;
+ public override void SetOptionBytes(string key, ReadOnlySpan<byte> value) => Options[key] = value.ToArray();
+
+ public bool Disposed { get; private set; }
+ public override void Dispose() => Disposed = true;
+
+ public override string GetOption(string key) =>
+ _driver.DatabaseTracksOptions && Options.TryGetValue(key, out object? v) ? (string)v : base.GetOption(key);
+ public override long GetOptionInt64(string key) => (long)Options[key];
+ public override double GetOptionDouble(string key) => (double)Options[key];
+ public override byte[] GetOptionBytes(string key) => (byte[])Options[key];
+
+ public override AdbcConnection ConnectWithTypedOptions(IReadOnlyDictionary<string, object>? options)
+ {
+ if (!_driver.DatabaseHandlesTypedConnect)
+ {
+ return base.ConnectWithTypedOptions(options);
+ }
+
+ _driver.LastTypedConnectOptions = options;
+ return _driver.LastConnection = new Connection11(_driver);
+ }
+
+ public override AdbcConnection Connect(IReadOnlyDictionary<string, string>? options)
+ {
+ var conn = new Connection11(_driver);
+ if (options != null)
+ {
+ foreach (KeyValuePair<string, string> pair in options)
+ {
+ // Like many drivers, consume autocommit at connect time without
+ // overriding the AutoCommit property.
+ if (pair.Key == AdbcOptions.Connection.Autocommit)
+ {
+ conn.AutoCommitAtConnect = AdbcOptions.GetEnabled(pair.Value);
+ continue;
+ }
+ conn.Options[pair.Key] = pair.Value;
+ }
+ }
+ return _driver.LastConnection = conn;
+ }
+ }
+
+ private sealed class Connection11 : AdbcConnection
+ {
+ private readonly Driver11 _driver;
+ private readonly OptionBag _options = new OptionBag();
+ private string? _catalog;
+
+ public Connection11(Driver11 driver) { _driver = driver; }
+
+ public Dictionary<string, object> Options => _options.Values;
+ public bool? AutoCommitAtConnect { get; set; }
+ public (string?, string?, string, bool)? LastStatisticsRequest { get; private set; }
+
+ public override string? CurrentCatalog
+ {
+ get => _catalog;
+ set => _catalog = value;
+ }
+
+ public override void SetOption(string key, string value) => Options[key] = value;
+ public override void SetOptionInt64(string key, long value) => Options[key] = value;
+ public override void SetOptionDouble(string key, double value) => Options[key] = value;
+ public override void SetOptionBytes(string key, ReadOnlySpan<byte> value) => Options[key] = value.ToArray();
+
+ public override string GetOption(string key) =>
+ Options.TryGetValue(key, out object? v) && v is string s ? s : base.GetOption(key);
+ public override long GetOptionInt64(string key) => (long)_options.Get(key);
+ public override double GetOptionDouble(string key) => (double)_options.Get(key);
+
+ public override AdbcStatement CreateStatement() => _driver.LastStatement = new Statement11();
+
+ public override IArrowArrayStream GetStatisticsNames() => NameStream();
+
+ public override IArrowArrayStream GetStatistics(string? catalogPattern, string? schemaPattern, string tableNamePattern, bool approximate)
+ {
+ LastStatisticsRequest = (catalogPattern, schemaPattern, tableNamePattern, approximate);
+ return NameStream();
+ }
+
+ public override IArrowArrayStream GetObjects(
+ GetObjectsDepth depth, string? catalogPattern, string? dbSchemaPattern,
+ string? tableNamePattern, IReadOnlyList<string>? tableTypes, string? columnNamePattern)
+ => throw AdbcException.NotImplemented("GetObjects");
+
+ public override Schema GetTableSchema(string? catalog, string? dbSchema, string tableName)
+ => throw AdbcException.NotImplemented("GetTableSchema");
+
+ public override IArrowArrayStream GetTableTypes()
+ => throw AdbcException.NotImplemented("GetTableTypes");
+
+ private static IArrowArrayStream NameStream()
+ {
+ Schema schema = new Schema.Builder()
+ .Field(f => f.Name("statistic_name").DataType(StringType.Default).Nullable(false))
+ .Build();
+ StringArray names = new StringArray.Builder().Append("custom").Build();
+ return new SingleBatchStream(schema, new RecordBatch(schema, new IArrowArray[] { names }, 1));
+ }
+ }
+
+ private sealed class Statement11 : AdbcStatement
+ {
+ private readonly OptionBag _options = new OptionBag();
+ private readonly ManualResetEventSlim _cancelled = new ManualResetEventSlim();
+
+ public ManualResetEventSlim Started { get; } = new ManualResetEventSlim();
+
+ public override void SetOption(string key, string value) => _options.Values[key] = value;
+ public override void SetOptionBytes(string key, ReadOnlySpan<byte> value) => _options.Values[key] = value.ToArray();
+ public override string GetOption(string key) => (string)_options.Get(key);
+ public override byte[] GetOptionBytes(string key) => (byte[])_options.Get(key);
+
+ public override void Cancel() => _cancelled.Set();
+
+ public override QueryResult ExecuteQuery()
+ {
+ switch (SqlQuery)
+ {
+ case "FAIL":
+ throw new DetailedException();
+ case "FAIL_PLAIN":
+ throw new PlainException();
+ case "FAIL_CANCELED":
+ throw new OperationCanceledException();
+ case "BLOCK":
+ Started.Set();
+ if (_cancelled.Wait(TimeSpan.FromSeconds(30)))
+ {
+ throw new AdbcException("cancelled", AdbcStatusCode.Cancelled);
+ }
+ throw new TimeoutException();
+ default:
+ throw AdbcException.NotImplemented("ExecuteQuery");
+ }
+ }
+
+ public override UpdateResult ExecuteUpdate() =>
+ SqlQuery == "UPDATE" ? new UpdateResult(42) : throw AdbcException.NotImplemented("ExecuteUpdate");
+
+ public override PartitionedResult ExecutePartitioned()
+ {
+ if (SqlQuery != "PARTITIONS")
+ {
+ return base.ExecutePartitioned();
+ }
+
+ Schema schema = new Schema.Builder()
+ .Field(f => f.Name("x").DataType(Int32Type.Default).Nullable(false))
+ .Build();
+ return new PartitionedResult(schema, 5, new[]
+ {
+ new PartitionDescriptor(new byte[] { 1, 2 }),
+ new PartitionDescriptor(new byte[] { 3 }),
+ });
+ }
+ }
+
+ private sealed class DetailedException : AdbcException
+ {
+ public DetailedException() : base("query failed", AdbcStatusCode.InvalidData) { }
+
+ public override string? SqlState => "42000";
+
+ public override IReadOnlyList<KeyValuePair<string, byte[]>> Details => new[]
+ {
+ new KeyValuePair<string, byte[]>("query_id", Encoding.UTF8.GetBytes("q-123")),
+ new KeyValuePair<string, byte[]>("empty", new byte[0]),
+ };
+ }
+
+ private sealed class PlainException : AdbcException
+ {
+ public PlainException() : base("plain failure", AdbcStatusCode.IOError) { }
+
+ public override string? SqlState => "HY000";
+ public override int NativeError => 1234;
+ }
+
+ private sealed class SingleBatchStream : IArrowArrayStream
+ {
+ private RecordBatch? _batch;
+
+ public SingleBatchStream(Schema schema, RecordBatch batch)
+ {
+ Schema = schema;
+ _batch = batch;
+ }
+
+ public Schema Schema { get; }
+
+ public ValueTask<RecordBatch?> ReadNextRecordBatchAsync(CancellationToken cancellationToken = default)
+ {
+ RecordBatch? result = _batch;
+ _batch = null;
+ return new ValueTask<RecordBatch?>(result);
+ }
+
+ public void Dispose() { _batch?.Dispose(); _batch = null; }
+ }
+ }
+}