forked from nazarii-piontko/datafusion-sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFusionSharpDataReader.cs
More file actions
355 lines (295 loc) · 11.7 KB
/
Copy pathDataFusionSharpDataReader.cs
File metadata and controls
355 lines (295 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#pragma warning disable CA1010 // Collections should implement generic interface
using System.Collections;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Apache.Arrow;
using Apache.Arrow.Arrays;
namespace DataFusionSharp.Data;
/// <summary>
/// Provides a forward-only, read-only, streaming row reader over a DataFusion query result.
/// </summary>
/// <remarks>
/// <para>
/// Internally this reader consumes a <see cref="DataFrameStream"/> one Arrow <see cref="RecordBatch"/> at a time,
/// exposing a familiar row-by-row ADO.NET interface on top of the columnar Arrow data.
/// </para>
/// <para>
/// Because Arrow data lives in native (Rust-allocated) memory, disposing the reader is important:
/// it releases both the stream and the underlying <see cref="DataFrame"/>.
/// Never access values through an already-disposed reader.
/// </para>
/// </remarks>
public sealed class DataFusionSharpDataReader : DbDataReader
{
private readonly DataFrame _dataFrame;
private readonly DataFrameStream _stream;
private readonly IAsyncEnumerator<RecordBatch> _enumerator;
private readonly Schema _schema;
private readonly Dictionary<string, int> _fieldIndex;
private RecordBatch? _currentBatch;
private int _rowIndex = -1;
private int _rowIndexComm = -1;
private bool _isClosed;
internal DataFusionSharpDataReader(DataFrame dataFrame, DataFrameStream stream)
{
_dataFrame = dataFrame;
_stream = stream;
_enumerator = stream.GetAsyncEnumerator();
_schema = stream.Schema;
_fieldIndex = new Dictionary<string, int>(_schema.FieldsList.Count, StringComparer.OrdinalIgnoreCase);
for (var i = 0; i < _schema.FieldsList.Count; i++)
_fieldIndex[_schema.FieldsList[i].Name] = i;
}
/// <inheritdoc />
public override int FieldCount => _schema.FieldsList.Count;
/// <inheritdoc />
public override int Depth => 0;
/// <inheritdoc />
public override bool IsClosed => _isClosed;
/// <inheritdoc />
public override int RecordsAffected => -1;
/// <inheritdoc />
public override bool HasRows => _currentBatch is { Length: > 0 };
/// <inheritdoc />
public override object this[int ordinal] => GetValue(ordinal);
/// <inheritdoc />
public override object this[string name] => GetValue(GetOrdinal(name));
/// <inheritdoc />
public override string GetName(int ordinal) => _schema.FieldsList[ordinal].Name;
/// <inheritdoc />
public override int GetOrdinal(string name)
{
if (!_fieldIndex.TryGetValue(name, out var ordinal))
throw new ArgumentException($"Column name '{name}' does not exist in the result set.", nameof(name));
return ordinal;
}
/// <inheritdoc />
public override string GetDataTypeName(int ordinal) => _schema.FieldsList[ordinal].DataType.Name;
/// <inheritdoc />
public override Type GetFieldType(int ordinal)
{
return _schema.FieldsList[ordinal].DataType.ToNetType() ??
throw new InvalidOperationException($"Unsupported data type {_schema.FieldsList[ordinal].DataType} in column {ordinal}.");
}
/// <inheritdoc />
public override bool Read() => ReadAsync(CancellationToken.None).GetAwaiter().GetResult();
/// <inheritdoc />
public override async Task<bool> ReadAsync(CancellationToken cancellationToken)
{
EnsureOpen();
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
if (_currentBatch is not null && _rowIndex + 1 < _currentBatch.Length)
{
++_rowIndex;
++_rowIndexComm;
return true;
}
if (!await _enumerator.MoveNextAsync().ConfigureAwait(false))
return false;
_currentBatch = _enumerator.Current;
_rowIndex = -1;
}
}
/// <inheritdoc />
public override bool NextResult() => false;
/// <inheritdoc />
public override Task<bool> NextResultAsync(CancellationToken cancellationToken) => Task.FromResult(false);
/// <inheritdoc />
public override bool IsDBNull(int ordinal)
{
EnsureRow();
return _currentBatch!.Column(ordinal).IsNull(_rowIndex);
}
/// <inheritdoc />
public override object GetValue(int ordinal)
{
EnsureRow();
return _currentBatch!.Column(ordinal).GetValue(_rowIndex) ?? DBNull.Value;
}
/// <inheritdoc />
public override int GetValues(object[] values)
{
ArgumentNullException.ThrowIfNull(values);
EnsureRow();
var count = Math.Min(values.Length, FieldCount);
for (var i = 0; i < count; i++)
values[i] = GetValue(i);
return count;
}
/// <inheritdoc />
public override bool GetBoolean(int ordinal)
{
EnsureRow();
if (_currentBatch!.Column(ordinal) is BooleanArray boolArray)
return boolArray.GetValue(_rowIndex) ?? ThrowNullValue<bool>(ordinal);
throw new InvalidCastException($"Column {ordinal} is not a boolean array.");
}
/// <inheritdoc />
public override byte GetByte(int ordinal)
{
EnsureRow();
if (_currentBatch!.Column(ordinal) is UInt8Array uint8Array)
return uint8Array.GetValue(_rowIndex) ?? ThrowNullValue<byte>(ordinal);
throw new InvalidCastException($"Column {ordinal} is not a byte array.");
}
/// <inheritdoc />
public override char GetChar(int ordinal)
{
var str = GetString(ordinal);
if (str.Length != 1)
throw new InvalidCastException($"Column {ordinal} contains a string value '{str}' that cannot be cast to char because it has length {str.Length}.");
return str[0];
}
/// <inheritdoc />
public override short GetInt16(int ordinal)
{
EnsureRow();
if (_currentBatch!.Column(ordinal) is Int16Array a)
return a.GetValue(_rowIndex) ?? ThrowNullValue<short>(ordinal);
throw new InvalidCastException($"Column {ordinal} is not a 16-bit integer array.");
}
/// <inheritdoc />
public override int GetInt32(int ordinal)
{
EnsureRow();
if (_currentBatch!.Column(ordinal) is Int32Array a)
return a.GetValue(_rowIndex) ?? ThrowNullValue<int>(ordinal);
throw new InvalidCastException($"Column {ordinal} is not a 32-bit integer array.");
}
/// <inheritdoc />
public override long GetInt64(int ordinal)
{
EnsureRow();
if (_currentBatch!.Column(ordinal) is Int64Array a)
return a.GetValue(_rowIndex) ?? ThrowNullValue<long>(ordinal);
throw new InvalidCastException($"Column {ordinal} is not a 64-bit integer array.");
}
/// <inheritdoc />
public override float GetFloat(int ordinal)
{
EnsureRow();
if (_currentBatch!.Column(ordinal) is FloatArray a)
return a.GetValue(_rowIndex) ?? ThrowNullValue<float>(ordinal);
throw new InvalidCastException($"Column {ordinal} is not a float array.");
}
/// <inheritdoc />
public override double GetDouble(int ordinal)
{
EnsureRow();
if (_currentBatch!.Column(ordinal) is DoubleArray a)
return a.GetValue(_rowIndex) ?? ThrowNullValue<double>(ordinal);
throw new InvalidCastException($"Column {ordinal} is not a double array.");
}
/// <inheritdoc />
public override decimal GetDecimal(int ordinal)
{
EnsureRow();
return _currentBatch!.Column(ordinal) switch
{
Decimal128Array a => a.GetValue(_rowIndex) ?? ThrowNullValue<decimal>(ordinal),
Decimal256Array a => a.GetValue(_rowIndex) ?? ThrowNullValue<decimal>(ordinal),
_ => throw new InvalidCastException($"Column {ordinal} is not a double array.")
};
}
/// <inheritdoc />
public override string GetString(int ordinal)
{
EnsureRow();
return _currentBatch!.Column(ordinal) switch
{
StringArray a => a.GetString(_rowIndex) ?? ThrowNullValue<string>(ordinal),
StringViewArray a => a.GetString(_rowIndex) ?? ThrowNullValue<string>(ordinal),
LargeStringArray a => a.GetString(_rowIndex) ?? ThrowNullValue<string>(ordinal),
_ => throw new InvalidCastException($"Column {ordinal} is not a double array.")
};
}
/// <inheritdoc />
public override DateTime GetDateTime(int ordinal)
{
EnsureRow();
return _currentBatch!.Column(ordinal) switch
{
Date32Array a => a.GetDateTime(_rowIndex) ?? ThrowNullValue<DateTime>(ordinal),
Date64Array a => a.GetDateTime(_rowIndex) ?? ThrowNullValue<DateTime>(ordinal),
TimestampArray a => a.GetTimestamp(_rowIndex)?.UtcDateTime ?? ThrowNullValue<DateTime>(ordinal),
_ => throw new InvalidCastException($"Column {ordinal} does not represent a DateTime")
};
}
/// <inheritdoc />
public override Guid GetGuid(int ordinal)
{
if (!Guid.TryParse(GetString(ordinal), CultureInfo.InvariantCulture, out var g))
throw new InvalidCastException($"Column {ordinal} does not represent a GUID");
return g;
}
/// <inheritdoc />
public override long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOffset, int length)
{
EnsureRow();
var bytes = _currentBatch!.Column(ordinal) switch
{
BinaryArray a => a.GetBytes(_rowIndex),
LargeBinaryArray a => a.GetBytes(_rowIndex),
FixedSizeBinaryArray a => a.GetBytes(_rowIndex),
BinaryViewArray a => a.GetBytes(_rowIndex),
_ => throw new InvalidCastException($"Column {ordinal} is not a binary array.")
};
if (buffer is null)
return bytes.Length;
var bytesToCopy = (int)Math.Min(length, bytes.Length - dataOffset);
var outputSpan = buffer.AsSpan(bufferOffset, bytesToCopy);
bytes.CopyTo(outputSpan);
return bytesToCopy;
}
/// <inheritdoc />
public override long GetChars(int ordinal, long dataOffset, char[]? buffer, int bufferOffset, int length)
{
var str = GetString(ordinal);
if (buffer is null)
return str.Length;
var charsToCopy = Math.Min(length, str.Length - (int) dataOffset);
str.CopyTo((int)dataOffset, buffer, bufferOffset, charsToCopy);
return charsToCopy;
}
/// <inheritdoc />
public override IEnumerator GetEnumerator() => new DbEnumerator(this, closeReader: false);
/// <inheritdoc />
public override void Close()
{
if (_isClosed)
return;
_isClosed = true;
_currentBatch = null;
var disposeTask = _enumerator.DisposeAsync();
if (!disposeTask.IsCompleted)
disposeTask.AsTask().GetAwaiter().GetResult();
_stream.Dispose();
_dataFrame.Dispose();
}
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
if (disposing)
Close();
base.Dispose(disposing);
}
private void EnsureOpen()
{
if (_isClosed)
throw new InvalidOperationException("The data reader is closed.");
}
private void EnsureRow()
{
EnsureOpen();
if (_currentBatch is null || _rowIndex < 0)
throw new InvalidOperationException("No current row. Call Read() or ReadAsync() before accessing data.");
}
[DoesNotReturn]
private T ThrowNullValue<T>(int ordinal)
{
throw new InvalidCastException($"Column {ordinal} at row {_rowIndexComm} contains a NULL value");
}
}