Skip to content

Commit 7bf89d5

Browse files
Merge pull request #114 from nrkno/more-helpers-bqsqlfrag
More helpers bqsqlfrag
2 parents ddafcb1 + 1786b6b commit 7bf89d5

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

core/src/main/scala/no/nrk/bigquery/BQSqlFrag.scala

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,29 @@ sealed trait BQSqlFrag {
113113
}
114114

115115
final def allReferencedAsPartitions: Seq[BQPartitionId[Any]] =
116-
this.collect {
117-
case BQSqlFrag.PartitionRef(ref) => ref
118-
case BQSqlFrag.FillRef(fill) => fill.destination
119-
case BQSqlFrag.FilledTableRef(fill) => fill.tableDef.unpartitioned.assertPartition
120-
}.distinct
116+
allReferencedAsPartitions(expandAndExcludeViews = true)
117+
final def allReferencedAsPartitions(expandAndExcludeViews: Boolean): Seq[BQPartitionId[Any]] = {
118+
def pf: PartialFunction[BQSqlFrag, List[BQPartitionId[Any]]] = {
119+
case BQSqlFrag.PartitionRef(ref) =>
120+
ref.wholeTable match {
121+
case tableDef: BQTableDef.View[_] if expandAndExcludeViews => tableDef.query.collect(pf).flatten
122+
case _ => List(ref)
123+
}
124+
case BQSqlFrag.FillRef(fill) => List(fill.destination)
125+
case BQSqlFrag.FilledTableRef(fill) => List(fill.tableDef.unpartitioned.assertPartition)
126+
}
127+
128+
this.collect(pf).flatten.distinct
129+
}
130+
131+
final def allReferencedTables: Seq[BQTableLike[Any]] =
132+
allReferencedAsPartitions(expandAndExcludeViews = true)
133+
.map(_.wholeTable)
134+
.filterNot(tableLike => tableLike.isInstanceOf[BQTableDef.View[_]])
135+
136+
final def allReferencedTablesAsPartitions: Seq[BQPartitionId[Any]] =
137+
allReferencedAsPartitions(expandAndExcludeViews = true)
138+
.filterNot(pid => pid.wholeTable.isInstanceOf[BQTableDef.View[_]])
121139

122140
final def allReferencedUDFs: Seq[UDF[UDF.UDFId]] =
123141
this.collect { case BQSqlFrag.Call(udf, _) => udf }.distinct

core/src/main/scala/no/nrk/bigquery/BQTableLike.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ object BQTableLike {
7373
* a non-partitioned and a date-partitioned table into a list (or otherwise lose precise types), you also lose the
7474
* ability to construct legal [[BQPartitionId]] s with `assertPartition`
7575
*/
76-
case class BQTableRef[+P](tableId: BQTableId, partitionType: BQPartitionType[P]) extends BQTableLike[P] {
76+
case class BQTableRef[+P](
77+
tableId: BQTableId,
78+
partitionType: BQPartitionType[P],
79+
labels: TableLabels = TableLabels.Empty)
80+
extends BQTableLike[P] {
7781
override def unpartitioned: BQTableRef[Unit] =
7882
withTableType(BQPartitionType.ignoredPartitioning(partitionType))
7983

core/src/main/scala/no/nrk/bigquery/TableLabels.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ case class TableLabels(values: SortedMap[String, String]) {
4848
def ++(other: TableLabels): TableLabels =
4949
withAll(other.values)
5050

51+
def contains(tableLabels: TableLabels): Boolean =
52+
if (tableLabels.values.nonEmpty) {
53+
tableLabels.values.forall { case (key, value) =>
54+
values.get(key).contains(value)
55+
}
56+
} else false
57+
5158
/** This method is needed for the case where we delete a label. It is deleted by setting it to `null`.
5259
*
5360
* As such, we need to know the labels of a table in production before we compute the new set of labels to use when

testing/src/main/scala/no/nrk/bigquery/testing/BQSmokeTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ object BQSmokeTest {
424424
case p @ BQSqlFrag.PartitionRef(pid) =>
425425
val schemaOpt: Option[BQSchema] =
426426
pid.wholeTable match {
427-
case BQTableRef(_, _) => None
427+
case BQTableRef(_, _, _) => None
428428
case x: BQTableDef[Any] => Some(x.schema)
429429
}
430430

0 commit comments

Comments
 (0)