-
Notifications
You must be signed in to change notification settings - Fork 4
/
AggregateOperator.cs
78 lines (66 loc) · 2.56 KB
/
AggregateOperator.cs
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
using System;
using TileDB.Interop;
namespace TileDB.CSharp;
/// <summary>
/// Represents an aggregate operator that can be applied over a query field.
/// </summary>
/// <remarks>
/// This class abstracts the <c>tiledb_channel_operator_t</c> type of the TileDB C API.
/// </remarks>
public abstract class AggregateOperator
{
// Prevent inheriting the class outside of this assembly.
private protected AggregateOperator() { }
internal abstract unsafe tiledb_channel_operator_t* GetHandle(tiledb_ctx_t* ctx);
/// <summary>
/// Computes the sum of all values in the field.
/// </summary>
public static AggregateOperator Sum { get; } = new SimpleAggregateOperator(OperatorKind.Sum);
/// <summary>
/// Computes the minimum value in the field.
/// </summary>
public static AggregateOperator Min { get; } = new SimpleAggregateOperator(OperatorKind.Min);
/// <summary>
/// Computes the maximum value in the field.
/// </summary>
public static AggregateOperator Max { get; } = new SimpleAggregateOperator(OperatorKind.Max);
/// <summary>
/// Computes the mean of all values in the field.
/// </summary>
public static AggregateOperator Mean { get; } = new SimpleAggregateOperator(OperatorKind.Mean);
/// <summary>
/// Counts the number of null values in the field.
/// </summary>
public static AggregateOperator NullCount { get; } = new SimpleAggregateOperator(OperatorKind.NullCount);
private enum OperatorKind
{
Sum,
Min,
Max,
Mean,
NullCount
}
private sealed class SimpleAggregateOperator : AggregateOperator
{
private readonly OperatorKind _kind;
public SimpleAggregateOperator(OperatorKind kind)
{
_kind = kind;
}
internal override unsafe tiledb_channel_operator_t* GetHandle(tiledb_ctx_t* ctx)
{
tiledb_channel_operator_t* op;
var ret = _kind switch
{
OperatorKind.Sum => Methods.tiledb_channel_operator_sum_get(ctx, &op),
OperatorKind.Min => Methods.tiledb_channel_operator_min_get(ctx, &op),
OperatorKind.Max => Methods.tiledb_channel_operator_max_get(ctx, &op),
OperatorKind.Mean => Methods.tiledb_channel_operator_mean_get(ctx, &op),
OperatorKind.NullCount => Methods.tiledb_channel_operator_null_count_get(ctx, &op),
_ => throw new InvalidOperationException(),
};
ErrorHandling.ThrowOnError(ret);
return op;
}
}
}