|
| 1 | +using System.Runtime.InteropServices; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Apache.Arrow; |
| 5 | +using Apache.Arrow.C; |
| 6 | +using Apache.Arrow.Ipc; |
| 7 | +using DuckDB.NET.Native; |
| 8 | + |
| 9 | +namespace DuckDB.NET.Data.Arrow; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Streams the rows of a DuckDB query result as Apache Arrow <see cref="RecordBatch"/> values using |
| 13 | +/// DuckDB's Arrow C Data Interface (<c>duckdb_to_arrow_schema</c> / <c>duckdb_data_chunk_to_arrow</c>). |
| 14 | +/// Each DuckDB data chunk is converted into one Arrow record batch and imported with no row-by-row marshaling. |
| 15 | +/// </summary> |
| 16 | +internal sealed class DuckDBArrowArrayStream : IArrowArrayStream |
| 17 | +{ |
| 18 | + private DuckDBResult result; |
| 19 | + private readonly DuckDBArrowOptions arrowOptions; |
| 20 | + private readonly bool streaming; |
| 21 | + private bool disposed; |
| 22 | + |
| 23 | + public Schema Schema { get; } |
| 24 | + |
| 25 | + internal DuckDBArrowArrayStream(DuckDBResult result) |
| 26 | + { |
| 27 | + this.result = result; |
| 28 | + |
| 29 | + arrowOptions = NativeMethods.Arrow.DuckDBResultGetArrowOptions(ref this.result); |
| 30 | + if (arrowOptions.IsInvalid) |
| 31 | + { |
| 32 | + this.result.Close(); |
| 33 | + throw new InvalidOperationException("Failed to obtain Arrow options from the DuckDB result."); |
| 34 | + } |
| 35 | + |
| 36 | + streaming = NativeMethods.Types.DuckDBResultIsStreaming(this.result) > 0; |
| 37 | + |
| 38 | + try |
| 39 | + { |
| 40 | + Schema = BuildSchema(); |
| 41 | + } |
| 42 | + catch |
| 43 | + { |
| 44 | + arrowOptions.Dispose(); |
| 45 | + this.result.Close(); |
| 46 | + throw; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private unsafe Schema BuildSchema() |
| 51 | + { |
| 52 | + var columnCount = NativeMethods.Query.DuckDBColumnCount(ref result); |
| 53 | + |
| 54 | + var logicalTypes = new DuckDBLogicalType[columnCount]; |
| 55 | + var typeHandles = new IntPtr[columnCount]; |
| 56 | + var namePointers = new IntPtr[columnCount]; |
| 57 | + |
| 58 | + try |
| 59 | + { |
| 60 | + for (var index = 0UL; index < columnCount; index++) |
| 61 | + { |
| 62 | + var logicalType = NativeMethods.Query.DuckDBColumnLogicalType(ref result, (long)index); |
| 63 | + logicalTypes[index] = logicalType; |
| 64 | + typeHandles[index] = logicalType.DangerousGetHandle(); |
| 65 | + |
| 66 | + var name = NativeMethods.Query.DuckDBColumnName(ref result, (long)index); |
| 67 | + namePointers[index] = Marshal.StringToCoTaskMemUTF8(name); |
| 68 | + } |
| 69 | + |
| 70 | + var cSchema = CArrowSchema.Create(); |
| 71 | + |
| 72 | + try |
| 73 | + { |
| 74 | + fixed (IntPtr* typesPointer = typeHandles) |
| 75 | + fixed (IntPtr* namesPointer = namePointers) |
| 76 | + { |
| 77 | + var error = NativeMethods.Arrow.DuckDBToArrowSchema(arrowOptions, (IntPtr)typesPointer, (IntPtr)namesPointer, columnCount, (IntPtr)cSchema); |
| 78 | + error.ThrowOnError("Failed to convert the DuckDB result schema to an Arrow schema."); |
| 79 | + } |
| 80 | + |
| 81 | + return CArrowSchemaImporter.ImportSchema(cSchema); |
| 82 | + } |
| 83 | + finally |
| 84 | + { |
| 85 | + CArrowSchema.Free(cSchema); |
| 86 | + } |
| 87 | + } |
| 88 | + finally |
| 89 | + { |
| 90 | + foreach (var pointer in namePointers) |
| 91 | + { |
| 92 | + if (pointer != IntPtr.Zero) |
| 93 | + { |
| 94 | + Marshal.FreeCoTaskMem(pointer); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + foreach (var logicalType in logicalTypes) |
| 99 | + { |
| 100 | + logicalType?.Dispose(); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public ValueTask<RecordBatch?> ReadNextRecordBatchAsync(CancellationToken cancellationToken = default) |
| 106 | + { |
| 107 | + ObjectDisposedException.ThrowIf(disposed, this); |
| 108 | + |
| 109 | + if (cancellationToken.IsCancellationRequested) |
| 110 | + { |
| 111 | + return new ValueTask<RecordBatch?>(Task.FromCanceled<RecordBatch?>(cancellationToken)); |
| 112 | + } |
| 113 | + |
| 114 | + var chunk = streaming |
| 115 | + ? NativeMethods.StreamingResult.DuckDBStreamFetchChunk(result) |
| 116 | + : NativeMethods.Query.DuckDBFetchChunk(result); |
| 117 | + |
| 118 | + if (chunk.IsInvalid) |
| 119 | + { |
| 120 | + chunk.Dispose(); |
| 121 | + return new ValueTask<RecordBatch?>((RecordBatch?)null); |
| 122 | + } |
| 123 | + |
| 124 | + try |
| 125 | + { |
| 126 | + return new ValueTask<RecordBatch?>(ConvertChunk(chunk)); |
| 127 | + } |
| 128 | + finally |
| 129 | + { |
| 130 | + chunk.Dispose(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private unsafe RecordBatch ConvertChunk(DuckDBDataChunk chunk) |
| 135 | + { |
| 136 | + var cArray = CArrowArray.Create(); |
| 137 | + |
| 138 | + try |
| 139 | + { |
| 140 | + var error = NativeMethods.Arrow.DuckDBDataChunkToArrow(arrowOptions, chunk, (IntPtr)cArray); |
| 141 | + error.ThrowOnError("Failed to convert a DuckDB data chunk to an Arrow array."); |
| 142 | + |
| 143 | + return CArrowArrayImporter.ImportRecordBatch(cArray, Schema); |
| 144 | + } |
| 145 | + finally |
| 146 | + { |
| 147 | + CArrowArray.Free(cArray); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + public void Dispose() |
| 152 | + { |
| 153 | + if (disposed) |
| 154 | + { |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + disposed = true; |
| 159 | + |
| 160 | + arrowOptions.Dispose(); |
| 161 | + result.Close(); |
| 162 | + } |
| 163 | +} |
0 commit comments