-
Notifications
You must be signed in to change notification settings - Fork 0
/
Analyzer.cs
220 lines (175 loc) · 7.96 KB
/
Analyzer.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using deequ.Analyzers.States;
using deequ.Extensions;
using deequ.Metrics;
using deequ.Util;
using Microsoft.Spark.Sql;
using Microsoft.Spark.Sql.Types;
using static Microsoft.Spark.Sql.Functions;
namespace deequ.Analyzers
{
internal abstract class Analyzer<S, M> : IAnalyzer<M> where S : State<S> where M : IMetric
{
public abstract M ToFailureMetric(Exception e);
public virtual IEnumerable<Action<StructType>> Preconditions() => Enumerable.Empty<Action<StructType>>();
public virtual M Calculate(DataFrame data, Option<IStateLoader> aggregateWith = default, Option<IStatePersister> saveStateWith = default)
{
try
{
foreach (Action<StructType> condition in Preconditions())
{
condition(data.Schema());
}
Option<S> state = ComputeStateFrom(data);
return CalculateMetric(state, aggregateWith, saveStateWith);
}
catch (Exception e)
{
return ToFailureMetric(e);
}
}
public void AggregateStateTo(IStateLoader sourceA, IStateLoader sourceB, IStatePersister target)
{
Option<S> maybeStateA = sourceA.Load<S>(new Option<IAnalyzer<IMetric>>((IAnalyzer<IMetric>)this));
Option<S> maybeStateB = sourceB.Load<S>(new Option<IAnalyzer<IMetric>>((IAnalyzer<IMetric>)this));
S aggregated = (maybeStateA.HasValue, maybeStateB.HasValue) switch
{
(true, true) => maybeStateA.Value.Sum(maybeStateB.Value),
(true, false) => maybeStateA.Value,
(false, true) => maybeStateB.Value,
_ => null
};
target.Persist(new Option<IAnalyzer<IMetric>>((IAnalyzer<IMetric>)this), new Option<S>(aggregated));
}
public M LoadStateAndComputeMetric(IStateLoader source) =>
ComputeMetricFrom(source.Load<S>(new Option<IAnalyzer<IMetric>>((IAnalyzer<IMetric>)this)));
public void CopyStateTo(IStateLoader source, IStatePersister target) =>
target.Persist(new Option<IAnalyzer<IMetric>>((IAnalyzer<IMetric>)this), source.Load<S>(new Option<IAnalyzer<IMetric>>((IAnalyzer<IMetric>)this)));
public abstract Option<S> ComputeStateFrom(DataFrame dataFrame);
public abstract M ComputeMetricFrom(Option<S> state);
public M CalculateMetric(Option<S> state, Option<IStateLoader> aggregateWith,
Option<IStatePersister> saveStateWith)
{
Option<S> loadedState = aggregateWith
.Select(value => value.Load<S>(new Option<IAnalyzer<IMetric>>((IAnalyzer<IMetric>)this)).Value);
Option<S> stateToComputeMetricFrom = AnalyzersExt.Merge(loadedState, state);
saveStateWith
.Select(persister =>
persister.Persist(new Option<IAnalyzer<IMetric>>((IAnalyzer<IMetric>)this), stateToComputeMetricFrom));
return ComputeMetricFrom(stateToComputeMetricFrom);
}
}
internal abstract class ScanShareableAnalyzer<S, M> : Analyzer<S, M>, IScanSharableAnalyzer<S, M>
where S : State<S>, IState where M : IMetric
{
public abstract IEnumerable<Column> AggregationFunctions();
public M MetricFromAggregationResult(Row result, int offset, Option<IStateLoader> aggregateWith,
Option<IStatePersister> saveStatesWith)
{
Option<S> state = FromAggregationResult(result, offset);
return CalculateMetric(state, aggregateWith, saveStatesWith);
}
public abstract Option<S> FromAggregationResult(Row result, int offset);
public override Option<S> ComputeStateFrom(DataFrame dataFrame)
{
IEnumerable<Column> aggregations = AggregationFunctions();
Row result = dataFrame
.Agg(aggregations.First(), aggregations.Skip(1).ToArray())
.Collect()
.FirstOrDefault();
return FromAggregationResult(result, 0);
}
}
internal abstract class StandardScanShareableAnalyzer<S> : ScanShareableAnalyzer<S, DoubleMetric>,
IScanSharableAnalyzer<IState, DoubleMetric>
where S : DoubleValuedState<S>, IState
{
public Entity Entity = Entity.Column;
public StandardScanShareableAnalyzer(string name, string instance, Entity entity)
{
Name = name;
Instance = instance;
Entity = entity;
}
public string Name { get; set; }
public string Instance { get; set; }
public override IEnumerable<Action<StructType>> Preconditions() =>
AdditionalPreconditions().Concat(base.Preconditions());
public override DoubleMetric ToFailureMetric(Exception e) =>
AnalyzersExt.MetricFromFailure(e, Name, Instance, Entity);
public virtual IEnumerable<Action<StructType>> AdditionalPreconditions() =>
Enumerable.Empty<Action<StructType>>();
public override DoubleMetric ComputeMetricFrom(Option<S> state)
{
DoubleMetric metric = state.HasValue switch
{
true => AnalyzersExt.MetricFromValue(new Try<double>(state.Value.MetricValue()), Name, Instance,
Entity),
_ => AnalyzersExt.MetricFromEmpty(this, Name, Instance, Entity)
};
return metric;
}
}
internal class NumMatchesAndCount : DoubleValuedState<NumMatchesAndCount>, IState
{
public long Count;
public long NumMatches;
public NumMatchesAndCount(long numMatches, long count)
{
NumMatches = numMatches;
Count = count;
}
public IState Sum(IState other) => throw new NotImplementedException();
public override NumMatchesAndCount Sum(NumMatchesAndCount other) =>
new NumMatchesAndCount(NumMatches + other.NumMatches, Count + other.Count);
public override double MetricValue()
{
if (Count == 0L)
{
return double.NaN;
}
return (double)NumMatches / Count;
}
}
internal abstract class PredicateMatchingAnalyzer : StandardScanShareableAnalyzer<NumMatchesAndCount>
{
protected PredicateMatchingAnalyzer(string name, string instance, Entity entity,
Column predicate, Option<string> where) : base(name, instance, entity)
{
Predicate = predicate;
Where = where;
}
public Column Predicate { get; }
public Option<string> Where { get; }
public override Option<NumMatchesAndCount> FromAggregationResult(Row result, int offset)
{
if (result[offset] == null || result[offset + 1] == null)
{
return Option<NumMatchesAndCount>.None;
}
NumMatchesAndCount state = new NumMatchesAndCount((long)result[offset], (long)result[offset + 1]);
return new Option<NumMatchesAndCount>(state);
}
public override IEnumerable<Column> AggregationFunctions()
{
Column selection = AnalyzersExt.ConditionalSelection(Predicate, Where);
return new[] { selection, Count("*") }.AsEnumerable();
}
}
internal abstract class GroupingAnalyzer<S, M> : Analyzer<S, M>, IGroupingAnalyzer<M> where S : State<S> where M : IMetric
{
public abstract IEnumerable<string> GroupingColumns();
public override IEnumerable<Action<StructType>> Preconditions() =>
GroupingColumns().Select(HasColumn).Concat(base.Preconditions());
public static Action<StructType> HasColumn(string column) =>
schema =>
{
if (!AnalyzersExt.HasColumn(schema, column))
{
throw new Exception("Input data does not include column!");
}
};
}
}