-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDeltaUtils.scala
More file actions
479 lines (439 loc) · 19.1 KB
/
DeltaUtils.scala
File metadata and controls
479 lines (439 loc) · 19.1 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package bio.ferlab.datalake.spark3.utils
import bio.ferlab.datalake.commons.config.{Configuration, DatasetConf}
import io.delta.tables.DeltaTable
import org.apache.spark.sql.delta.DeltaLog
import org.apache.spark.sql.functions._
import org.apache.spark.sql.{Column, DataFrame, SparkSession}
import java.sql.Timestamp
import java.time.LocalDateTime
import java.time.temporal.{ChronoUnit, Temporal}
object DeltaUtils {
/**
* @deprecated Use [[DeltaUtils#compact(datasetConf: DatasetConf, partitionFilter: Option[String])]] instead.
*/
def compact(datasetConf: DatasetConf, repartition: DataFrame => DataFrame)(implicit spark: SparkSession, conf: Configuration): Unit = {
val df = spark.read
.format(datasetConf.format.sparkFormat)
.load(datasetConf.location)
repartition(df)
.write
.partitionBy(datasetConf.partitionby: _*)
.option("dataChange", "false")
.format(datasetConf.format.sparkFormat)
.mode("overwrite")
.save(datasetConf.location)
}
/**
* Compact the data by coalescing small files into larger ones.
*
* @param datasetConf Dataset to compact
* @param partitionFilter Optional partition predicate to only compact a subset of data
* @param spark Spark session
* @param conf Configuration
* @example
* Compact the whole dataset.
* {{{
* compact(ds)
* }}}
* @example
* Compact a specific partition. Useful for compaction jobs running everyday on the same dataset.
* {{{
* compact(ds, Some("date='2020-01-01'"))
* }}}
*/
def compact(datasetConf: DatasetConf, partitionFilter: Option[String] = None)(implicit spark: SparkSession, conf: Configuration): Unit = {
val deltaTable = DeltaTable.forPath(datasetConf.location)
partitionFilter match {
case Some(pf) => deltaTable.optimize().where(pf).executeCompaction()
case None => deltaTable.optimize().executeCompaction()
}
}
/**
* Vacuum based on the number of versions we wants keep. Notes :
* - If there is versions younger than 2 weeks then these versions will be kept and the retention period will be set to 336 hours (2 weeks)
* - If there is less versions than numberOfVersions param then vacuum will not be executed
*
* @param datasetConf dataset to vacuum
* @param numberOfVersions number of versions to kept
* @param spark spark session
* @param conf conf
*/
def vacuum(datasetConf: DatasetConf, numberOfVersions: Int)(implicit spark: SparkSession, conf: Configuration): Unit = {
import spark.implicits._
val timestamps: Seq[Timestamp] = DeltaTable
.forPath(datasetConf.location)
.history(numberOfVersions)
.select("timestamp")
.as[Timestamp].collect().toSeq
if (timestamps.size == numberOfVersions) {
val retentionHours = Seq(336, getRetentionHours(timestamps)).max // 336 hours = 2 weeks
DeltaTable.forPath(datasetConf.location).vacuum(retentionHours)
}
}
def getRetentionHours(timestamps: Seq[Timestamp], clock: Temporal = LocalDateTime.now()): Long = {
val oldest = timestamps.min((x: Timestamp, y: Timestamp) => if (x.before(y)) -1 else if (x.after(y)) 1 else 0)
oldest.toLocalDateTime.minusHours(1).until(clock, ChronoUnit.HOURS)
}
/**
* Retrieves statistics per file for the specified Delta table.
*
* @param path Path of the Delta table
* @param spark Spark session
* @return A DataFrame with the following schema:
* - `path`: Path of the file.
* - `partitionValues`: Map of partition column names to their values.
* - `size`: Size of the file in bytes.
* - `modificationTime`: Last modification timestamp.
* - `dataChange`: Indicates whether the file represents a data change.
* - `tags`: Map of additional metadata tags.
* - `stats`: Nested struct with:
* - `numRecords`: Number of records in the file.
* - `minValues`: Minimum values for columns.
* - `maxValues`: Maximum values for columns.
* - `nullCount`: Count of null values for columns.
* @example Result schema example:
* {{{
* root
* |-- path: string (nullable = true)
* |-- partitionValues: map (nullable = true)
* | |-- key: string
* | |-- value: string (valueContainsNull = true)
* |-- size: long (nullable = false)
* |-- modificationTime: long (nullable = false)
* |-- dataChange: boolean (nullable = false)
* |-- stats: struct (nullable = true)
* | |-- numRecords: long (nullable = true)
* | |-- minValues: struct (nullable = true)
* | | |-- AA_ID: decimal(14,0) (nullable = true)
* | | |-- ID: string (nullable = true)
* | |-- maxValues: struct (nullable = true)
* | | |-- AA_ID: decimal(14,0) (nullable = true)
* | | |-- ID: string (nullable = true)
* | |-- nullCount: struct (nullable = true)
* | | |-- AA_ID: long (nullable = true)
* | | |-- ID: long (nullable = true)
* |-- tags: map (nullable = true)
* | |-- key: string
* | |-- value: string (valueContainsNull = true)
* }}}
*/
def getTableStats(path: String)(implicit spark: SparkSession): DataFrame = {
val (_, snapshot) = DeltaLog.forTableWithSnapshot(spark, path)
snapshot.withStats
}
private def getStatPerPartition(stats: DataFrame, statAggregations: Column*): DataFrame = {
stats
.select(
explode(col("partitionValues")) as Seq("partitionColumn", "partitionValue"),
stats("*")
)
.groupBy("partitionColumn", "partitionValue")
.agg(statAggregations.head, statAggregations.tail: _*)
}
def getMap[T](df: DataFrame, statsColumns: Array[Column]): Map[String, T] = {
val statsDf = df
.select(statsColumns: _*)
.limit(1)
val statsRow = statsDf.head()
val nonNullColumns = statsDf.columns.filterNot(c => statsRow.isNullAt(statsRow.fieldIndex(c)))
statsRow
.getValuesMap[T](nonNullColumns)
}
/**
* Extracts partition column names and their distinct sorted values from the Delta table's metadata.
*
* @param path Path of the Delta table
* @param spark Spark session
* @return A DataFrame with columns:
* - `partitionColumn`: Name of the partition columns.
* - `values`: A sorted sequence of distinct values for the partition column.
* @example Result DataFrame example:
* {{{
* +---------------+-----------------------------------+
* |partitionColumn|values |
* +---------------+-----------------------------------+
* |ORIGIN |[ORDER, PATIENT, STAY, TEST_RESULT]|
* |ingested_on_dte|[2024-11-26, 2024-11-27] |
* +---------------+-----------------------------------+
* }}}
*/
def getPartitionValues(path: String)(implicit spark: SparkSession): DataFrame =
getPartitionValues(getTableStats(path))
/**
* @see Overload of [[getPartitionValues]].
* @param statsDf Pre-computed stats DataFrame from [[getTableStats]]. Pass this to reuse
* an already-loaded snapshot across multiple calls.
*/
def getPartitionValues(statsDf: DataFrame)(implicit spark: SparkSession): DataFrame = {
import spark.implicits._
statsDf
.select(explode($"partitionValues") as Seq("partitionColumn", "value"))
.groupBy("partitionColumn")
.agg(array_sort(collect_set("value")) as "values")
}
/**
* Computes the total number of records in a Delta table using the table's metadata.
*
* @param path Path of the Delta table
* @param spark Spark session
* @return The total number of records in the table.
*/
def getNumRecords(path: String)(implicit spark: SparkSession): Long =
getNumRecords(getTableStats(path))
/**
* @see Overload of [[getNumRecords]].
* @param statsDf Pre-computed stats DataFrame from [[getTableStats]]. Pass this to reuse
* an already-loaded snapshot across multiple calls.
*/
def getNumRecords(statsDf: DataFrame)(implicit spark: SparkSession): Long = {
import spark.implicits._
statsDf
.select(sum("stats.numRecords"))
.as[Long]
.head()
}
/**
* Computes the total number of records for each partition value in a Delta table using the table's metadata.
*
* @param path Path of the Delta table
* @param spark Spark session
* @return A DataFrame with columns:
* - `partitionColumn`: Name of the partition column.
* - `partitionValue`: Value of the partition.
* - `numRecords`: Total number of records for the partition value.
* @example Result DataFrame example:
* {{{
* +---------------+--------------+----------+
* |partitionColumn|partitionValue|numRecords|
* +---------------+--------------+----------+
* |ORIGIN |ORDER |68636088 |
* |ORIGIN |TEST_RESULT |22787146 |
* |ORIGIN |PATIENT |104806 |
* |ORIGIN |STAY |2024526 |
* |ingested_on_dte|2024-11-26 |46776282 |
* |ingested_on_dte|2024-11-27 |46776284 |
* +---------------+--------------+----------+
* }}}
*/
def getNumRecordsPerPartition(path: String)(implicit spark: SparkSession): DataFrame =
getNumRecordsPerPartition(getTableStats(path))
/**
* @see Overload of [[getNumRecordsPerPartition]].
* @param statsDf Pre-computed stats DataFrame from [[getTableStats]]. Pass this to reuse
* an already-loaded snapshot across multiple calls.
*/
def getNumRecordsPerPartition(statsDf: DataFrame)(implicit spark: SparkSession): DataFrame = {
val numRecords = sum("stats.numRecords") as "numRecords"
getStatPerPartition(statsDf, numRecords)
}
/**
* Computes the minimum value for each non-partition column in a Delta table using the table's metadata.
*
* @param path Path of the Delta table
* @param spark Spark session
* @return A Map where:
* - Keys are column names.
* - Values are the minimum values observed in the corresponding columns.
* @example Example result:
* {{{
* Map(
* "ID" -> 1,
* "UPDATE_DATE" -> 1900-01-01,
* )
* }}}
*/
def getMinValues(path: String)(implicit spark: SparkSession): Map[String, Any] =
getMinValues(getTableStats(path))
/**
* @see Overload of [[getMinValues]].
* @param statsDf Pre-computed stats DataFrame from [[getTableStats]]. Pass this to reuse
* an already-loaded snapshot across multiple calls.
*/
def getMinValues(statsDf: DataFrame)(implicit spark: SparkSession): Map[String, Any] = {
val minValuesDf = statsDf.select("stats.minValues.*")
val minValues = minValuesDf.columns.map(c => min(c) as c)
getMap[String](minValuesDf, minValues)
}
/**
* Computes the minimum value for each column for each partition value in a Delta table using the table's metadata.
*
* @param path Path of the Delta table
* @param spark Spark session
* @return A DataFrame with columns:
* - `partitionColumn`: Name of the partition column.
* - `partitionValue`: Value of the partition.
* - Each column in the Delta table: The minimum value for each column.
* @example For the given Delta table:
* {{{
* +----------+---------+-----------+--------+-------------------+
* | AA_ID | ID | UPDATE_DATE| ORIGIN | ingested_on_dte |
* +----------+---------+-----------+--------+-------------------+
* | 10001 | 'ABC123'| 2024-01-01 | ORDER | 2024-11-26 |
* | 10002 | 'XYZ456'| 2024-01-02 | ORDER | 2024-11-27 |
* +----------+---------+-----------+--------+-------------------+
* }}}
*
* The result DataFrame would look like:
* {{{
* +---------------+--------------+------------+------------+------------+
* |partitionColumn|partitionValue|AA_ID |ID |UPDATE_DATE |
* +---------------+--------------+------------+------------+------------+
* |ORIGIN |ORDER |10001 |'ABC123' |2024-01-01 |
* |ingested_on_dte|2024-11-26 |10001 |'ABC123' |2024-01-01 |
* |ingested_on_dte|2024-11-27 |10002 |'XYZ456' |2024-01-02 |
* +---------------+--------------+------------+------------+------------+
* }}}
*/
def getMinValuesPerPartition(path: String)(implicit spark: SparkSession): DataFrame =
getMinValuesPerPartition(getTableStats(path))
/**
* @see Overload of [[getMinValuesPerPartition]].
* @param statsDf Pre-computed stats DataFrame from [[getTableStats]]. Pass this to reuse
* an already-loaded snapshot across multiple calls.
*/
def getMinValuesPerPartition(statsDf: DataFrame)(implicit spark: SparkSession): DataFrame = {
val columnNames = statsDf.select("stats.minValues.*").columns
val minValues = columnNames.map(c => min(s"stats.minValues.$c") as c)
getStatPerPartition(statsDf, minValues: _*)
}
/**
* Computes the maximum value for each non-partition column in a Delta table using the table's metadata.
*
* @param path Path of the Delta table
* @param spark Spark session
* @return A Map where:
* - Keys are column names.
* - Values are the maximum values observed in the corresponding columns.
* @example Example result:
* {{{
* Map(
* "ID" -> 9999,
* "UPDATE_DATE" -> 2024-01-01,
* )
* }}}
*/
def getMaxValues(path: String)(implicit spark: SparkSession): Map[String, Any] =
getMaxValues(getTableStats(path))
/**
* @see Overload of [[getMaxValues]].
* @param statsDf Pre-computed stats DataFrame from [[getTableStats]]. Pass this to reuse
* an already-loaded snapshot across multiple calls.
*/
def getMaxValues(statsDf: DataFrame)(implicit spark: SparkSession): Map[String, Any] = {
val maxValuesDf = statsDf.select("stats.maxValues.*")
val maxValues = maxValuesDf.columns.map(c => max(c) as c)
getMap[Any](maxValuesDf, maxValues)
}
/**
* Computes the maximum value for each column for each partition value in a Delta table using the table's metadata.
*
* @param path Path of the Delta table
* @param spark Spark session
* @return A DataFrame with columns:
* - `partitionColumn`: Name of the partition column.
* - `partitionValue`: Value of the partition.
* - Each column in the Delta table: The maximum value for each column.
* @example For the given Delta table:
* {{{
* +----------+---------+-----------+--------+-------------------+
* | AA_ID | ID | UPDATE_DATE| ORIGIN | ingested_on_dte |
* +----------+---------+-----------+--------+-------------------+
* | 10001 | 'ABC123'| 2024-01-01 | ORDER | 2024-11-26 |
* | 10002 | 'XYZ456'| 2024-01-02 | ORDER | 2024-11-27 |
* +----------+---------+-----------+--------+-------------------+
* }}}
*
* The result DataFrame would look like:
* {{{
* +---------------+--------------+------------+------------+------------+
* |partitionColumn|partitionValue|AA_ID |ID |UPDATE_DATE |
* +---------------+--------------+------------+------------+------------+
* |ORIGIN |ORDER |10002 |'XYZ456' |2024-01-02 |
* |ingested_on_dte|2024-11-26 |10001 |'ABC123' |2024-01-01 |
* |ingested_on_dte|2024-11-27 |10002 |'XYZ456' |2024-01-02 |
* +---------------+--------------+------------+------------+------------+
* }}}
*/
def getMaxValuesPerPartition(path: String)(implicit spark: SparkSession): DataFrame =
getMaxValuesPerPartition(getTableStats(path))
/**
* @see Overload of [[getMaxValuesPerPartition]].
* @param statsDf Pre-computed stats DataFrame from [[getTableStats]]. Pass this to reuse
* an already-loaded snapshot across multiple calls.
*/
def getMaxValuesPerPartition(statsDf: DataFrame)(implicit spark: SparkSession): DataFrame = {
val columnNames = statsDf.select("stats.maxValues.*").columns
val maxValues = columnNames.map(c => max(s"stats.maxValues.$c") as c)
getStatPerPartition(statsDf, maxValues: _*)
}
/**
* Computes the total number of nulls for each non-partition column in a Delta table using the table's metadata.
*
* @param path Path of the Delta table
* @param spark Spark session
* @return A Map where:
* - Keys are column names.
* - Values are the number of nulls observed in the corresponding columns.
* @example Example result:
* {{{
* Map(
* "ID" -> 0,
* "UPDATE_DATE" -> 256,
* )
* }}}
*/
def getNullCounts(path: String)(implicit spark: SparkSession): Map[String, Long] =
getNullCounts(getTableStats(path))
/**
* @see Overload of [[getNullCounts]].
* @param statsDf Pre-computed stats DataFrame from [[getTableStats]]. Pass this to reuse
* an already-loaded snapshot across multiple calls.
*/
def getNullCounts(statsDf: DataFrame)(implicit spark: SparkSession): Map[String, Long] = {
val nullCountsDf = statsDf.select("stats.nullCount.*")
val nullCounts = nullCountsDf.columns.map(c => sum(c) as c)
getMap[Long](nullCountsDf, nullCounts)
}
/**
* Computes the total number of nulls for each column for each partition value in a Delta table using the table's metadata.
*
* @param path Path of the Delta table
* @param spark Spark session
* @return A DataFrame with columns:
* - `partitionColumn`: Name of the partition column.
* - `partitionValue`: Value of the partition.
* - Each column in the Delta table: The number of nulls for each column.
* @example For the given Delta table:
* {{{
* +----------+---------+------------+--------+------------------+
* | AA_ID | ID | UPDATE_DATE| ORIGIN | ingested_on_dte |
* +----------+---------+------------+--------+------------------+
* | 10001 | 'ABC123'| null | ORDER | 2024-11-26 |
* | 10002 | null | null | ORDER | 2024-11-27 |
* +----------+---------+------------+--------+------------------+
* }}}
*
* The result DataFrame would look like:
* {{{
* +---------------+--------------+-----+---+------------+
* |partitionColumn|partitionValue|AA_ID|ID |UPDATE_DATE |
* +---------------+--------------+-----+---+------------+
* |ORIGIN |ORDER |0 |1 |2 |
* |ingested_on_dte|2024-11-26 |0 |0 |1 |
* |ingested_on_dte|2024-11-27 |0 |1 |1 |
* +---------------+--------------+-----+---+------------+
* }}}
*/
def getNullCountsPerPartition(path: String)(implicit spark: SparkSession): DataFrame =
getNullCountsPerPartition(getTableStats(path))
/**
* @see Overload of [[getNullCountsPerPartition]].
* @param statsDf Pre-computed stats DataFrame from [[getTableStats]]. Pass this to reuse
* an already-loaded snapshot across multiple calls.
*/
def getNullCountsPerPartition(statsDf: DataFrame)(implicit spark: SparkSession): DataFrame = {
val columnNames = statsDf.select("stats.nullCount.*").columns
val nullCounts = columnNames.map(c => sum(s"stats.nullCount.$c") as c)
getStatPerPartition(statsDf, nullCounts: _*)
}
}