-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArrayExtensions.cs
More file actions
445 lines (381 loc) · 19.5 KB
/
Copy pathArrayExtensions.cs
File metadata and controls
445 lines (381 loc) · 19.5 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
using Apache.Arrow;
using Apache.Arrow.Arrays;
using Apache.Arrow.Types;
namespace DataFusionSharp;
/// <summary>
/// Extension methods for working with Arrow column arrays returned from DataFusion queries.
/// </summary>
public static class ArrayExtensions
{
/// <summary>
/// Enumerates the values of an Arrow array as nullable booleans.
/// </summary>
/// <param name="array">The Arrow array to enumerate.</param>
/// <returns>An enumerator of nullable booleans representing the values in the array.</returns>
/// <exception cref="ArgumentException">Thrown if the provided array is not a Boolean array.</exception>
public static IEnumerable<bool?> AsBool(this IArrowArray array)
{
ArgumentNullException.ThrowIfNull(array);
if (array is BooleanArray typedArray)
return typedArray.AsBool();
throw new ArgumentException($"Column is not Boolean Array, actual type: {array.GetType().Name}");
}
/// <summary>
/// Enumerates the values of an BooleanArray as nullable booleans.
/// </summary>
/// <param name="array">The BooleanArray to enumerate.</param>
/// <returns>An enumerator of nullable long integers representing the values in the array.</returns>
public static IEnumerable<bool?> AsBool(this BooleanArray array)
{
ArgumentNullException.ThrowIfNull(array);
for (int i = 0; i < array.Length; i++)
yield return array.GetValue(i);
}
/// <summary>
/// Enumerates the values of an Arrow array as nullable long integers.
/// </summary>
/// <param name="array">The Arrow array to enumerate.</param>
/// <returns>An enumerator of nullable long integers representing the values in the array.</returns>
/// <exception cref="ArgumentException">Thrown if the provided array is not an Int64 array.</exception>
public static IEnumerable<long?> AsInt64(this IArrowArray array)
{
ArgumentNullException.ThrowIfNull(array);
if (array is Int64Array typedArray)
return typedArray.AsInt64();
throw new ArgumentException($"Column is not Int64 Array, actual type: {array.GetType().Name}");
}
/// <summary>
/// Enumerates the values of an Int64Array as nullable long integers.
/// </summary>
/// <param name="array">The Int64Array to enumerate.</param>
/// <returns>An enumerator of nullable long integers representing the values in the array.</returns>
public static IEnumerable<long?> AsInt64(this Int64Array array)
{
ArgumentNullException.ThrowIfNull(array);
for (int i = 0; i < array.Length; i++)
yield return array.GetValue(i);
}
/// <summary>
/// Enumerates the values of an Arrow array as nullable double-precision floating-point numbers.
/// </summary>
/// <param name="array">The Arrow array to enumerate.</param>
/// <returns>An enumerator of nullable doubles representing the values in the array.</returns>
/// <exception cref="ArgumentException">Thrown if the provided array is not a Double array.</exception>
public static IEnumerable<double?> AsDouble(this IArrowArray array)
{
ArgumentNullException.ThrowIfNull(array);
if (array is DoubleArray typedArray)
return typedArray.AsDouble();
throw new ArgumentException($"Column is not Double Array, actual type: {array.GetType().Name}");
}
/// <summary>
/// Enumerates the values of a DoubleArray as nullable double-precision floating-point numbers.
/// </summary>
/// <param name="array">The DoubleArray to enumerate.</param>
/// <returns>An enumerator of nullable doubles representing the values in the array.</returns>
public static IEnumerable<double?> AsDouble(this DoubleArray array)
{
ArgumentNullException.ThrowIfNull(array);
for (int i = 0; i < array.Length; i++)
yield return array.GetValue(i);
}
/// <summary>
/// Enumerates the values of an Arrow array as nullable integers.
/// </summary>
/// <param name="array">The Arrow array to enumerate.</param>
/// <returns>An enumerator of nullable integers representing the values in the array.</returns>
/// <exception cref="ArgumentException">Thrown if the provided array is not an Int32 array.</exception>
public static IEnumerable<int?> AsInt32(this IArrowArray array)
{
ArgumentNullException.ThrowIfNull(array);
if (array is Int32Array typedArray)
return typedArray.AsInt32();
throw new ArgumentException($"Column is not Int32 Array, actual type: {array.GetType().Name}");
}
/// <summary>
/// Enumerates the values of an Int32Array as nullable integers.
/// </summary>
/// <param name="array">The Int32Array to enumerate.</param>
/// <returns>An enumerator of nullable integers representing the values in the array.</returns>
public static IEnumerable<int?> AsInt32(this Int32Array array)
{
ArgumentNullException.ThrowIfNull(array);
for (int i = 0; i < array.Length; i++)
yield return array.GetValue(i);
}
/// <summary>
/// Enumerates the values of an Arrow array as nullable single-precision floating-point numbers.
/// </summary>
/// <param name="array">The Arrow array to enumerate.</param>
/// <returns>An enumerator of nullable floats representing the values in the array.</returns>
/// <exception cref="ArgumentException">Thrown if the provided array is not a Float array.</exception>
public static IEnumerable<float?> AsFloat(this IArrowArray array)
{
ArgumentNullException.ThrowIfNull(array);
if (array is FloatArray typedArray)
return typedArray.AsFloat();
throw new ArgumentException($"Column is not Float Array, actual type: {array.GetType().Name}");
}
/// <summary>
/// Enumerates the values of a FloatArray as nullable single-precision floating-point numbers.
/// </summary>
/// <param name="array">The FloatArray to enumerate.</param>
/// <returns>An enumerator of nullable floats representing the values in the array.</returns>
public static IEnumerable<float?> AsFloat(this FloatArray array)
{
ArgumentNullException.ThrowIfNull(array);
for (int i = 0; i < array.Length; i++)
yield return array.GetValue(i);
}
/// <summary>
/// Enumerates the values of an Arrow array as nullable decimals.
/// </summary>
/// <param name="array">The Arrow array to enumerate.</param>
/// <returns>An enumerator of nullable decimals representing the values in the array.</returns>
/// <exception cref="ArgumentException">Thrown if the provided array is not a Decimal128 or Decimal256 array.</exception>
public static IEnumerable<decimal?> AsDecimal(this IArrowArray array)
{
ArgumentNullException.ThrowIfNull(array);
if (array is Decimal128Array decimal128Array)
return decimal128Array.AsDecimal();
if (array is Decimal256Array decimal256Array)
return decimal256Array.AsDecimal();
throw new ArgumentException($"Column is not Decimal Array, actual type: {array.GetType().Name}");
}
/// <summary>
/// Enumerates the values of a Decimal128Array as nullable decimals.
/// </summary>
/// <param name="array">The Decimal128Array to enumerate.</param>
/// <returns>An enumerator of nullable decimals representing the values in the array.</returns>
public static IEnumerable<decimal?> AsDecimal(this Decimal128Array array)
{
ArgumentNullException.ThrowIfNull(array);
for (int i = 0; i < array.Length; i++)
yield return array.GetValue(i);
}
/// <summary>
/// Enumerates the values of a Decimal256Array as nullable decimals.
/// </summary>
/// <param name="array">The Decimal256Array to enumerate.</param>
/// <returns>An enumerator of nullable decimals representing the values in the array.</returns>
public static IEnumerable<decimal?> AsDecimal(this Decimal256Array array)
{
ArgumentNullException.ThrowIfNull(array);
for (int i = 0; i < array.Length; i++)
yield return array.GetValue(i);
}
/// <summary>
/// Enumerates the values of an Arrow array as nullable <see cref="DateOnly"/> values.
/// </summary>
/// <param name="array">The Arrow array to enumerate.</param>
/// <returns>An enumerator of nullable <see cref="DateOnly"/> values representing the values in the array.</returns>
/// <exception cref="ArgumentException">Thrown if the provided array is not a Date32 or Date64 array.</exception>
public static IEnumerable<DateOnly?> AsDateOnly(this IArrowArray array)
{
ArgumentNullException.ThrowIfNull(array);
if (array is Date32Array date32Array)
return date32Array.AsDateOnly();
if (array is Date64Array date64Array)
return date64Array.AsDateOnly();
throw new ArgumentException($"Column is not Date Array, actual type: {array.GetType().Name}");
}
/// <summary>
/// Enumerates the values of a Date32Array as nullable <see cref="DateOnly"/> values.
/// </summary>
/// <param name="array">The Date32Array to enumerate.</param>
/// <returns>An enumerator of nullable <see cref="DateOnly"/> values representing the values in the array.</returns>
public static IEnumerable<DateOnly?> AsDateOnly(this Date32Array array)
{
ArgumentNullException.ThrowIfNull(array);
for (int i = 0; i < array.Length; i++)
yield return array.GetDateOnly(i);
}
/// <summary>
/// Enumerates the values of a Date64Array as nullable <see cref="DateOnly"/> values.
/// </summary>
/// <param name="array">The Date64Array to enumerate.</param>
/// <returns>An enumerator of nullable <see cref="DateOnly"/> values representing the values in the array.</returns>
public static IEnumerable<DateOnly?> AsDateOnly(this Date64Array array)
{
ArgumentNullException.ThrowIfNull(array);
for (int i = 0; i < array.Length; i++)
yield return array.GetDateOnly(i);
}
/// <summary>
/// Enumerates the values of an Arrow array as nullable <see cref="DateTimeOffset"/> values.
/// </summary>
/// <param name="array">The Arrow array to enumerate.</param>
/// <returns>An enumerator of nullable <see cref="DateTimeOffset"/> values representing the values in the array.</returns>
/// <exception cref="ArgumentException">Thrown if the provided array is not a Timestamp array.</exception>
public static IEnumerable<DateTimeOffset?> AsTimestamp(this IArrowArray array)
{
ArgumentNullException.ThrowIfNull(array);
if (array is TimestampArray typedArray)
return typedArray.AsTimestamp();
throw new ArgumentException($"Column is not Timestamp Array, actual type: {array.GetType().Name}");
}
/// <summary>
/// Enumerates the values of a TimestampArray as nullable <see cref="DateTimeOffset"/> values.
/// </summary>
/// <param name="array">The TimestampArray to enumerate.</param>
/// <returns>An enumerator of nullable <see cref="DateTimeOffset"/> values representing the values in the array.</returns>
public static IEnumerable<DateTimeOffset?> AsTimestamp(this TimestampArray array)
{
ArgumentNullException.ThrowIfNull(array);
for (int i = 0; i < array.Length; i++)
yield return array.GetTimestamp(i);
}
/// <summary>
/// Enumerates the values of an Arrow array as nullable strings.
/// </summary>
/// <param name="column">The Arrow array to enumerate.</param>
/// <returns>>An enumerator of nullable strings representing the values in the column.</returns>
/// <exception cref="ArgumentException">Thrown if the provided column is not a String array.</exception>
public static IEnumerable<string?> AsString(this IArrowArray column)
{
ArgumentNullException.ThrowIfNull(column);
if (column is StringArray stringColumn)
return stringColumn.AsString();
if (column is StringViewArray typedViewColumn)
return typedViewColumn.AsString();
if (column is LargeStringArray largeStringArray)
return largeStringArray.AsString();
throw new ArgumentException($"Column is not String Array, actual type: {column.GetType().Name}");
}
/// <summary>
/// Enumerates the values of a StringArray as nullable strings.
/// </summary>
/// <param name="column">The StringArray to enumerate.</param>
/// <returns>>An enumerator of nullable strings representing the values in the column.</returns>
public static IEnumerable<string?> AsString(this StringArray column)
{
ArgumentNullException.ThrowIfNull(column);
for (int i = 0; i < column.Length; i++)
yield return column.GetString(i);
}
/// <summary>
/// Enumerates the values of a StringViewArray as nullable strings.
/// </summary>
/// <param name="column">The StringViewArray to enumerate.</param>
/// <returns>>An enumerator of nullable strings representing the values in the column.</returns>
public static IEnumerable<string?> AsString(this StringViewArray column)
{
ArgumentNullException.ThrowIfNull(column);
for (int i = 0; i < column.Length; i++)
yield return column.GetString(i);
}
/// <summary>
/// Enumerates the values of a LargeStringArray as nullable strings.
/// </summary>
/// <param name="column">The LargeStringArray to enumerate.</param>
/// <returns>>An enumerator of nullable strings representing the values in the column.</returns>
public static IEnumerable<string?> AsString(this LargeStringArray column)
{
ArgumentNullException.ThrowIfNull(column);
for (int i = 0; i < column.Length; i++)
yield return column.GetString(i);
}
/// <summary>
/// Returns the value at the specified rowIndex from any Arrow array as an <see cref="object"/>,
/// mapping each Arrow type to its idiomatic .NET equivalent.
/// Returns <c>null</c> if the element at <paramref name="rowIndex"/> is null.
/// </summary>
/// <param name="array">The Arrow array to read from.</param>
/// <param name="rowIndex">The zero-based rowIndex of the element to retrieve.</param>
/// <returns>The .NET value at the given rowIndex, or <c>null</c> if the element is null.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="array"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when the array type is not supported.</exception>
public static object? GetValue(this IArrowArray array, int rowIndex)
{
ArgumentNullException.ThrowIfNull(array);
if (array.IsNull(rowIndex))
return null;
return array switch
{
NullArray => null,
BooleanArray a => a.GetValue(rowIndex)!.Value,
Int8Array a => a.GetValue(rowIndex)!.Value,
Int16Array a => a.GetValue(rowIndex)!.Value,
Int32Array a => a.GetValue(rowIndex)!.Value,
Int64Array a => a.GetValue(rowIndex)!.Value,
UInt8Array a => a.GetValue(rowIndex)!.Value,
UInt16Array a => a.GetValue(rowIndex)!.Value,
UInt32Array a => a.GetValue(rowIndex)!.Value,
UInt64Array a => a.GetValue(rowIndex)!.Value,
HalfFloatArray a => a.GetValue(rowIndex)!.Value,
FloatArray a => a.GetValue(rowIndex)!.Value,
DoubleArray a => a.GetValue(rowIndex)!.Value,
Decimal128Array a => a.GetValue(rowIndex)!.Value,
Decimal256Array a => a.GetValue(rowIndex)!.Value,
StringArray a => a.GetString(rowIndex),
StringViewArray a => a.GetString(rowIndex),
LargeStringArray a => a.GetString(rowIndex),
BinaryArray a => a.GetBytes(rowIndex).ToArray(),
BinaryViewArray a => a.GetBytes(rowIndex).ToArray(),
FixedSizeBinaryArray a => a.GetBytes(rowIndex).ToArray(),
LargeBinaryArray a => a.GetBytes(rowIndex).ToArray(),
Date32Array a => a.GetDateOnly(rowIndex)!.Value,
Date64Array a => a.GetDateOnly(rowIndex)!.Value,
TimestampArray a => a.GetTimestamp(rowIndex)!.Value,
Time32Array a => a.GetTime(rowIndex)!.Value,
Time64Array a => a.GetTime(rowIndex)!.Value,
DurationArray a => a.GetTimeSpan(rowIndex)!.Value,
YearMonthIntervalArray a => a.GetValue(rowIndex)!.Value,
DayTimeIntervalArray a => a.GetValue(rowIndex)!.Value,
MonthDayNanosecondIntervalArray a => a.GetValue(rowIndex)!.Value,
MapArray a => a.GetSlicedValues(rowIndex),
ListArray a => a.GetSlicedValues(rowIndex),
LargeListArray a => a.GetSlicedValues(rowIndex),
FixedSizeListArray a => a.GetSlicedValues(rowIndex),
StructArray a => a.Fields.Select(f => f.GetValue(rowIndex)).ToArray(),
// DictionaryArray a => ...
// SparseUnionArray a => ...
// DenseUnionArray a => ...
_ => throw new ArgumentException($"Unsupported Arrow array type: {array.GetType().Name}", nameof(array))
};
}
/// <summary>
/// Gets the corresponding .NET type for a given Arrow type, if it exists.
/// The instance of that type will be returned by <see cref="GetValue"/>
/// </summary>
/// <param name="arrowType">The Arrow type to map to a .NET type.</param>
/// <returns>The .NET type corresponding to the given Arrow type, or <c>null</c> if no suitable mapping exists.</returns>
public static Type? ToNetType(this IArrowType arrowType)
{
return arrowType switch
{
NullType => typeof(DBNull),
BooleanType => typeof(bool),
Int8Type => typeof(sbyte),
Int16Type => typeof(short),
Int32Type => typeof(int),
Int64Type => typeof(long),
UInt8Type => typeof(byte),
UInt16Type => typeof(ushort),
UInt32Type => typeof(uint),
UInt64Type => typeof(ulong),
HalfFloatType => typeof(Half),
FloatType => typeof(float),
DoubleType => typeof(double),
Decimal128Type or Decimal256Type => typeof(decimal),
StringType or StringViewType or LargeStringType => typeof(string),
BinaryType or BinaryViewType or FixedSizeBinaryType or LargeBinaryType => typeof(byte[]),
Date32Type or Date64Type => typeof(DateOnly),
TimestampType => typeof(DateTimeOffset),
Time32Type or Time64Type => typeof(TimeOnly),
DurationType => typeof(TimeSpan),
IntervalType t => t.Unit switch
{
IntervalUnit.YearMonth => typeof(Apache.Arrow.Scalars.YearMonthInterval),
IntervalUnit.DayTime => typeof(Apache.Arrow.Scalars.DayTimeInterval),
IntervalUnit.MonthDayNanosecond => typeof(Apache.Arrow.Scalars.MonthDayNanosecondInterval),
_ => null
},
MapType or ListType or LargeListType or FixedSizeListType => typeof(IEnumerable<IArrowArray>),
StructType => typeof(object?[]),
// DictionaryType => ...
// UnionType => ...
_ => null
};
}
}