forked from awslabs/deequ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VerificationSuite.scala
315 lines (271 loc) · 11.9 KB
/
VerificationSuite.scala
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
/**
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
* use this file except in compliance with the License. A copy of the License
* is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*
*/
package com.amazon.deequ
import com.amazon.deequ.analyzers._
import com.amazon.deequ.analyzers.applicability.{AnalyzersApplicability, Applicability, CheckApplicability}
import com.amazon.deequ.analyzers.runners.{AnalysisRunner, AnalysisRunnerRepositoryOptions, AnalyzerContext}
import com.amazon.deequ.checks.{Check, CheckStatus}
import com.amazon.deequ.io.DfsUtils
import com.amazon.deequ.metrics.Metric
import com.amazon.deequ.repository.{MetricsRepository, ResultKey}
import org.apache.spark.sql.types.StructType
import org.apache.spark.sql.{DataFrame, SparkSession}
private[deequ] case class VerificationMetricsRepositoryOptions(
metricsRepository: Option[MetricsRepository] = None,
reuseExistingResultsForKey: Option[ResultKey] = None,
failIfResultsForReusingMissing: Boolean = false,
saveOrAppendResultsWithKey: Option[ResultKey] = None)
private[deequ] case class VerificationFileOutputOptions(
sparkSession: Option[SparkSession] = None,
saveCheckResultsJsonToPath: Option[String] = None,
saveSuccessMetricsJsonToPath: Option[String] = None,
overwriteOutputFiles: Boolean = false)
/** Responsible for running checks and required analysis and return the results */
class VerificationSuite {
/**
* Starting point to construct a VerificationRun.
*
* @param data tabular data on which the checks should be verified
*/
def onData(data: DataFrame): VerificationRunBuilder = {
new VerificationRunBuilder(data)
}
/**
* Runs all check groups and returns the verification result.
* Verification result includes all the metrics computed during the run.
*
* @param data tabular data on which the checks should be verified
* @param checks A sequence of check objects to be executed
* @param requiredAnalysis can be used to enforce the calculation of some some metrics
* regardless of if there are constraints on them (optional)
* @param aggregateWith loader from which we retrieve an initial states to aggregate (optional)
* @param saveStatesWith persist resulting states for the configured analyzers (optional)
* @return Result for every check including the overall status, detailed status for each
* constraints and all metrics produced
*/
@deprecated("Use onData instead for a fluent API", "10-07-2019")
def run(
data: DataFrame,
checks: Seq[Check],
requiredAnalysis: Analysis = Analysis(),
aggregateWith: Option[StateLoader] = None,
saveStatesWith: Option[StatePersister] = None,
metricsRepository: Option[MetricsRepository] = None,
saveOrAppendResultsWithKey: Option[ResultKey] = None)
: VerificationResult = {
val analyzers = requiredAnalysis.analyzers ++ checks.flatMap { _.requiredAnalyzers() }
doVerificationRun(
data,
checks,
analyzers,
aggregateWith,
saveStatesWith,
metricsRepositoryOptions =
VerificationMetricsRepositoryOptions(
metricsRepository,
saveOrAppendResultsWithKey = saveOrAppendResultsWithKey
)
)
}
/**
* Runs all check groups and returns the verification result.
* Verification result includes all the metrics computed during the run.
*
* @param data tabular data on which the checks should be verified
* @param checks A sequence of check objects to be executed
* @param requiredAnalyzers can be used to enforce the calculation of some some metrics
* regardless of if there are constraints on them (optional)
* @param aggregateWith loader from which we retrieve initial states to aggregate (optional)
* @param saveStatesWith persist resulting states for the configured analyzers (optional)
* @param metricsRepositoryOptions Options related to the MetricsRepository
* @param fileOutputOptions Options related to FileOuput using a SparkSession
* @return Result for every check including the overall status, detailed status for each
* constraints and all metrics produced
*/
private[deequ] def doVerificationRun(
data: DataFrame,
checks: Seq[Check],
requiredAnalyzers: Seq[Analyzer[_, Metric[_]]],
aggregateWith: Option[StateLoader] = None,
saveStatesWith: Option[StatePersister] = None,
metricsRepositoryOptions: VerificationMetricsRepositoryOptions =
VerificationMetricsRepositoryOptions(),
fileOutputOptions: VerificationFileOutputOptions =
VerificationFileOutputOptions())
: VerificationResult = {
val analyzers = requiredAnalyzers ++ checks.flatMap { _.requiredAnalyzers() }
val analysisResults = AnalysisRunner.doAnalysisRun(
data,
analyzers.distinct,
aggregateWith,
saveStatesWith,
metricsRepositoryOptions = AnalysisRunnerRepositoryOptions(
metricsRepositoryOptions.metricsRepository,
metricsRepositoryOptions.reuseExistingResultsForKey,
metricsRepositoryOptions.failIfResultsForReusingMissing,
saveOrAppendResultsWithKey = None))
val verificationResult = evaluate(checks, analysisResults)
val analyzerContext = AnalyzerContext(verificationResult.metrics)
saveOrAppendResultsIfNecessary(
analyzerContext,
metricsRepositoryOptions.metricsRepository,
metricsRepositoryOptions.saveOrAppendResultsWithKey)
saveJsonOutputsToFilesystemIfNecessary(fileOutputOptions, verificationResult)
verificationResult
}
private[this] def saveJsonOutputsToFilesystemIfNecessary(
fileOutputOptions: VerificationFileOutputOptions,
verificationResult: VerificationResult)
: Unit = {
fileOutputOptions.sparkSession.foreach { session =>
fileOutputOptions.saveCheckResultsJsonToPath.foreach { profilesOutput =>
DfsUtils.writeToTextFileOnDfs(session, profilesOutput,
overwrite = fileOutputOptions.overwriteOutputFiles) { writer =>
writer.append(VerificationResult.checkResultsAsJson(verificationResult))
writer.newLine()
}
}
}
fileOutputOptions.sparkSession.foreach { session =>
fileOutputOptions.saveSuccessMetricsJsonToPath.foreach { profilesOutput =>
DfsUtils.writeToTextFileOnDfs(session, profilesOutput,
overwrite = fileOutputOptions.overwriteOutputFiles) { writer =>
writer.append(VerificationResult.successMetricsAsJson(verificationResult))
writer.newLine()
}
}
}
}
private[this] def saveOrAppendResultsIfNecessary(
resultingAnalyzerContext: AnalyzerContext,
metricsRepository: Option[MetricsRepository],
saveOrAppendResultsWithKey: Option[ResultKey])
: Unit = {
metricsRepository.foreach{repository =>
saveOrAppendResultsWithKey.foreach { key =>
val currentValueForKey = repository.loadByKey(key)
// AnalyzerContext entries on the right side of ++ will overwrite the ones on the left
// if there are two different metric results for the same analyzer
val valueToSave = currentValueForKey.getOrElse(AnalyzerContext.empty) ++
resultingAnalyzerContext
repository.save(saveOrAppendResultsWithKey.get, valueToSave)
}
}
}
/**
* Runs all check groups and returns the verification result. Metrics are computed from
* aggregated states. Verification result includes all the metrics generated during the run.
*
* @param schema schema of the tabular data on which the checks should be verified
* @param checks A sequence of check objects to be executed
* @param stateLoaders loaders from which we retrieve the states to aggregate
* @param requiredAnalysis can be used to enforce the some metrics regardless of if
* there are constraints on them (optional)
* @param saveStatesWith persist resulting states for the configured analyzers (optional)
* @return Result for every check including the overall status, detailed status for each
* constraints and all metrics produced
*/
def runOnAggregatedStates(
schema: StructType,
checks: Seq[Check],
stateLoaders: Seq[StateLoader],
requiredAnalysis: Analysis = Analysis(),
saveStatesWith: Option[StatePersister] = None,
metricsRepository: Option[MetricsRepository] = None,
saveOrAppendResultsWithKey: Option[ResultKey] = None)
: VerificationResult = {
val analysis = requiredAnalysis.addAnalyzers(checks.flatMap { _.requiredAnalyzers() })
val analysisResults = AnalysisRunner.runOnAggregatedStates(
schema,
analysis,
stateLoaders,
saveStatesWith,
metricsRepository = metricsRepository,
saveOrAppendResultsWithKey = saveOrAppendResultsWithKey)
evaluate(checks, analysisResults)
}
/**
* Check whether a check is applicable to some data using the schema of the data.
*
* @param check A check that may be applicable to some data
* @param schema The schema of the data the checks are for
* @param sparkSession The spark session in order to be able to create fake data
*/
def isCheckApplicableToData(
check: Check,
schema: StructType,
sparkSession: SparkSession)
: CheckApplicability = {
new Applicability(sparkSession).isApplicable(check, schema)
}
/**
* Check whether analyzers are applicable to some data using the schema of the data.
*
* @param analyzers Analyzers that may be applicable to some data
* @param schema The schema of the data the analyzers are for
* @param sparkSession The spark session in order to be able to create fake data
*/
def areAnalyzersApplicableToData(
analyzers: Seq[Analyzer[_ <: State[_], Metric[_]]],
schema: StructType,
sparkSession: SparkSession)
: AnalyzersApplicability = {
new Applicability(sparkSession).isApplicable(analyzers, schema)
}
private[this] def evaluate(
checks: Seq[Check],
analysisContext: AnalyzerContext)
: VerificationResult = {
val checkResults = checks
.map { check => check -> check.evaluate(analysisContext) }
.toMap
val verificationStatus = if (checkResults.isEmpty) {
CheckStatus.Success
} else {
checkResults.values
.map { _.status }
.max
}
VerificationResult(verificationStatus, checkResults, analysisContext.metricMap)
}
}
/** Convenience functions for using the VerificationSuite */
object VerificationSuite {
def apply(): VerificationSuite = {
new VerificationSuite()
}
@deprecated("Use the fluent API instead", "10-07-2019")
def run(
data: DataFrame,
checks: Seq[Check],
requiredAnalysis: Analysis = Analysis())
: VerificationResult = {
val analyzers = requiredAnalysis.analyzers ++ checks.flatMap { _.requiredAnalyzers() }
VerificationSuite().doVerificationRun(data, checks, analyzers)
}
def runOnAggregatedStates(
schema: StructType,
checks: Seq[Check],
stateLoaders: Seq[StateLoader],
requiredAnalysis: Analysis = Analysis(),
saveStatesWith: Option[StatePersister] = None,
metricsRepository: Option[MetricsRepository] = None,
saveOrAppendResultsWithKey: Option[ResultKey] = None )
: VerificationResult = {
VerificationSuite().runOnAggregatedStates(schema, checks, stateLoaders, requiredAnalysis,
saveStatesWith, metricsRepository, saveOrAppendResultsWithKey)
}
}