GH-44800: [C#] Implement Flight SQL Client (#44783)

GH-44800: [C#] Implement Flight SQL Client

## Rationale for this Change

This pull request introduces a **new implementation of `FlightSqlClient` and `PreparedStatement` in C#**. Previously, there was no C# client for Flight SQL, leaving a significant gap for .NET developers who wished to interact with Flight SQL servers.

The implementation aligns with the existing **C++ Flight SQL client** API, ensuring consistent and familiar behavior across languages and providing a robust client for the Apache Arrow ecosystem in .NET.

---

## What's Included in this PR?

### Key Features

1. **`FlightSqlClient`**:
   - Provides query execution (`ExecuteAsync`, `ExecuteUpdateAsync`) and schema retrieval (`GetCatalogsAsync`, `GetDbSchemasAsync`, etc.).
   - Implements metadata operations for catalogs, schemas, tables, and more.
   - Fully integrated with gRPC and Apache Arrow ecosystems.
   - Supports extensibility for advanced features like transactions.

2. **`PreparedStatement`**:
   - Implements parameterized query execution (`SetParameters`, `ExecuteAsync`, and `ExecuteUpdateAsync`).
   - Supports lifecycle management (`CloseAsync`) for effective resource handling.
   - Aligns with the prepared statement design in the C++ client.

### API Consistency

This implementation mirrors the **C++ Flight SQL client** to ensure API alignment across supported languages:
- Consistent naming conventions and parameter semantics.
- Ensures .NET developers can work seamlessly with existing Flight SQL servers.

---

## Are These Changes Tested?

### Testing Overview

1. **Unit Tests**:
   - Added tests for query execution and parameter binding in `PreparedStatement`.
   - Verified schema retrieval methods like `GetCatalogsAsync` and `GetDbSchemasAsync`.

2. **Integration Tests**:
   - Tested against a live Flight SQL server to validate query execution, schema retrieval, and metadata operations.

3. **End-to-End Tests**:
   - Covered real-world scenarios for parameterized updates and queries, ensuring robustness.

### Example Test Cases
- Verify that parameterized queries return correct results with valid input.
- Ensure schema retrieval throws appropriate exceptions for invalid descriptors.
- Validate row counts after `ExecuteUpdateAsync`.

---

## Are There Any Breaking Changes?

This PR introduces **new functionality** and does not affect any existing features. There are **no breaking changes**.

---

## Are There Any User-Facing Changes?

### New Capabilities
1. **FlightSqlClient**:
   - Query execution and schema retrieval for SQL queries on Flight SQL servers.
   - Metadata retrieval for catalogs, schemas, tables, and more.

2. **PreparedStatement**:
   - Supports parameterized queries with proper parameter binding.
   - Provides robust lifecycle management and execution.

### API Consistency
- Aligns with the C++ Flight SQL client for interoperability and familiar API design.

---

## Additional Notes

- This PR **does not include transaction support** at this stage, as it requires additional server-side capabilities.
- All methods follow idiomatic C# practices, including `async/await` for non-blocking operations.
- Extensible for future enhancements, including advanced features like savepoints.

---

## Resources

- **C++ Flight SQL Client Reference**: [Apache Arrow Flight SQL Documentation](https://arrow.apache.org/docs/)
- **Apache Arrow Contribution Guide**: [Contributing to Apache Arrow](https://arrow.apache.org/docs/dev/developers/guide/)

---

## Feedback and Suggestions

Thank you for reviewing this contribution! Suggestions and feedback are welcome to ensure the implementation meets the project's standards and requirements.
* GitHub Issue: #44800

Lead-authored-by: HackPoint <genashm@ge.com>
Co-authored-by: Genady Shmunik <genady.shmunik@ge.com>
Co-authored-by: HackP0!nt <genashm@gmail.com>
Co-authored-by: HackPoint <genashm@gmail.com>
Co-authored-by: Curt Hagenlocher <curt@hagenlocher.org>
Signed-off-by: Curt Hagenlocher <curt@hagenlocher.org>
23 files changed
tree: 8168904a857c2c29a152e6c12c1b7ef5b524760e
  1. examples/
  2. src/
  3. test/
  4. .asf.yaml
  5. .editorconfig
  6. .gitattributes
  7. .gitignore
  8. Apache.Arrow.sln
  9. ApacheArrow.snk
  10. Directory.Build.props
  11. Directory.Build.targets
  12. feather.png
  13. README.md
README.md

Apache Arrow

An implementation of Arrow targeting .NET Standard.

See our current feature matrix for currently available features.

Implementation

  • Arrow specification 1.0.0. (Support for reading 0.11+.)
  • C# 11
  • .NET Standard 2.0 and .NET 6.0
  • Asynchronous I/O
  • Uses modern .NET runtime features such as Span<T>, Memory<T>, MemoryManager<T>, and System.Buffers primitives for memory allocation, memory storage, and fast serialization.
  • Uses Acyclic Visitor Pattern for array types and arrays to facilitate serialization, record batch traversal, and format growth.

Known Issues

  • Cannot read Arrow files containing tensors.
  • Cannot easily modify allocation strategy without implementing a custom memory pool. All allocations are currently 64-byte aligned and padded to 8-bytes.
  • Default memory allocation strategy uses an over-allocation strategy with pointer fixing, which results in significant memory overhead for small buffers. A buffer that requires a single byte for storage may be backed by an allocation of up to 64-bytes to satisfy alignment requirements.
  • There are currently few builder APIs available for specific array types. Arrays must be built manually with an arrow buffer builder abstraction.
  • FlatBuffer code generation is not included in the build process.
  • Serialization implementation does not perform exhaustive validation checks during deserialization in every scenario.
  • Throws exceptions with vague, inconsistent, or non-localized messages in many situations
  • Throws exceptions that are non-specific to the Arrow implementation in some circumstances where it probably should (eg. does not throw ArrowException exceptions)
  • Lack of code documentation
  • Lack of usage examples

Usage

using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Apache.Arrow;
using Apache.Arrow.Ipc;

public static async Task<RecordBatch> ReadArrowAsync(string filename)
{
    using (var stream = File.OpenRead(filename))
    using (var reader = new ArrowFileReader(stream))
    {
        var recordBatch = await reader.ReadNextRecordBatchAsync();
        Debug.WriteLine("Read record batch with {0} column(s)", recordBatch.ColumnCount);
        return recordBatch;
    }
}

Status

Memory Management

  • Allocations are 64-byte aligned and padded to 8-bytes.
  • Allocations are automatically garbage collected

Arrays

Primitive Types

  • Int8, Int16, Int32, Int64
  • UInt8, UInt16, UInt32, UInt64
  • Float, Double, Half-float (.NET 6+)
  • Binary (variable-length)
  • String (utf-8)
  • Null

Parametric Types

  • Timestamp
  • Date32
  • Date64
  • Decimal
  • Time32
  • Time64
  • Binary (fixed-length)
  • List
  • Struct
  • Union
  • Map
  • Duration
  • Interval

Type Metadata

  • Data Types
  • Fields
  • Schema

Serialization

  • File
  • Stream

IPC Format

Compression

  • Buffer compression and decompression is supported, but requires installing the Apache.Arrow.Compression package. When reading compressed data, you must pass an Apache.Arrow.Compression.CompressionCodecFactory instance to the ArrowFileReader or ArrowStreamReader constructor, and when writing compressed data a CompressionCodecFactory must be set in the IpcOptions. Alternatively, a custom implementation of ICompressionCodecFactory can be used.

Not Implemented

  • Serialization
    • Exhaustive validation
    • Run End Encoding
  • Types
    • Tensor
  • Arrays
    • Large Arrays. There are large array types provided to help with interoperability with other libraries, but these do not support buffers larger than 2 GiB and an exception will be raised if trying to import an array that is too large.
      • Large Binary
      • Large List
      • Large String
    • Views
      • Binary
      • List
      • String
      • Large Binary
      • Large List
      • Large String
  • Array Operations
    • Equality / Comparison
    • Casting
  • Compute
    • There is currently no API available for a compute / kernel abstraction.

Build

Install the latest .NET Core SDK from https://dotnet.microsoft.com/download.

dotnet build

NuGet Build

To build the NuGet package run the following command to build a debug flavor, preview package into the artifacts folder.

dotnet pack

When building the officially released version run: (see Note below about current git repository)

dotnet pack -c Release

Which will build the final/stable package.

NOTE: When building the officially released version, ensure that your git repository has the origin remote set to https://github.com/apache/arrow.git, which will ensure Source Link is set correctly. See https://github.com/dotnet/sourcelink/blob/main/docs/README.md for more information.

There are two output artifacts:

  1. Apache.Arrow.<version>.nupkg - this contains the executable assemblies
  2. Apache.Arrow.<version>.snupkg - this contains the debug symbols files

Both of these artifacts can then be uploaded to https://www.nuget.org/packages/manage/upload.

Docker Build

Build from the Apache Arrow project root.

docker build -f csharp/build/docker/Dockerfile .

Testing

dotnet test

All build artifacts are placed in the artifacts folder in the project root.

Coding Style

This project follows the coding style specified in Coding Style.

Updating FlatBuffers code

See https://google.github.io/flatbuffers/flatbuffers_guide_use_java_c-sharp.html for how to get the flatc executable.

Run flatc --csharp on each .fbs file in the format folder. And replace the checked in .cs files under FlatBuf with the generated files.

Update the non-generated FlatBuffers .cs files with the files from the google/flatbuffers repo.