|
7 | 7 | package no.nrk.bigquery |
8 | 8 |
|
9 | 9 | import cats.syntax.all.* |
10 | | -import no.nrk.bigquery.syntax.* |
11 | 10 | import no.nrk.bigquery.BQSqlFrag.asSubQuery |
| 11 | +import no.nrk.bigquery.syntax.* |
12 | 12 |
|
13 | 13 | import scala.annotation.tailrec |
14 | 14 |
|
@@ -127,43 +127,49 @@ sealed trait BQSqlFrag { |
127 | 127 | final def allReferencedAsPartitions: Seq[BQPartitionId[Any]] = |
128 | 128 | allReferencedAsPartitions(expandAndExcludeViews = true) |
129 | 129 | final def allReferencedAsPartitions(expandAndExcludeViews: Boolean): Seq[BQPartitionId[Any]] = { |
130 | | - def pf(outerRef: Option[BQPartitionId[Any]]): PartialFunction[BQSqlFrag, List[BQPartitionId[Any]]] = { |
131 | | - case BQSqlFrag.PartitionRef(partitionRef) => |
132 | | - partitionRef.wholeTable match { |
133 | | - case tableDef: BQTableDef.View[?] if expandAndExcludeViews => |
134 | | - tableDef.query.collect(pf(Some(partitionRef))).flatten |
135 | | - case tvf: BQAppliedTableValuedFunction[?] if expandAndExcludeViews => |
136 | | - tvf.query.collect(pf(Some(partitionRef))).flatten |
137 | | - case _ => List(partitionRef) |
138 | | - } |
139 | | - |
140 | | - case BQSqlFrag.TableRef(table) => |
141 | | - (table.partitionType, outerRef) match { |
142 | | - case (partitionType: BQPartitionType.DatePartitioned, Some(partitionRef: BQPartitionId.DatePartitioned)) => |
143 | | - List(table.withTableType(partitionType).assertPartition(partitionRef.partition)) |
144 | | - case (partitionType: BQPartitionType.HourPartitioned, Some(partitionRef: BQPartitionId.HourPartitioned)) => |
145 | | - List(table.withTableType(partitionType).assertPartition(partitionRef.partition)) |
146 | | - case (_, _) => List(table.unpartitioned.assertPartition) |
147 | | - } |
| 130 | + def expandAndApplyOuterRef( |
| 131 | + table: BQTableLike[Any], |
| 132 | + outerRef: Option[BQPartitionId[Any]]): List[BQPartitionId[Any]] = |
| 133 | + table match { |
| 134 | + case tableDef: BQTableDef.View[?] if expandAndExcludeViews => tableDef.query.collect(pf(outerRef)).flatten |
| 135 | + case tvf: BQAppliedTableValuedFunction[?] if expandAndExcludeViews => tvf.query.collect(pf(outerRef)).flatten |
| 136 | + case _ => |
| 137 | + (table.partitionType, outerRef) match { |
| 138 | + case (partitionType: BQPartitionType.DatePartitioned, Some(partitionRef: BQPartitionId.DatePartitioned)) => |
| 139 | + List(table.withTableType(partitionType).assertPartition(partitionRef.partition)) |
| 140 | + case (partitionType: BQPartitionType.HourPartitioned, Some(partitionRef: BQPartitionId.HourPartitioned)) => |
| 141 | + List(table.withTableType(partitionType).assertPartition(partitionRef.partition)) |
| 142 | + case (_, _) => List(table.unpartitioned.assertPartition) |
| 143 | + } |
| 144 | + } |
148 | 145 |
|
| 146 | + def pf(outerRef: Option[BQPartitionId[Any]]): PartialFunction[BQSqlFrag, List[BQPartitionId[Any]]] = { |
| 147 | + case BQSqlFrag.PartitionRef(partitionRef) => expandAndApplyOuterRef(partitionRef.wholeTable, Some(partitionRef)) |
| 148 | + case BQSqlFrag.TableRef(table) => expandAndApplyOuterRef(table, outerRef) |
149 | 149 | case BQSqlFrag.FillRef(fill) => List(fill.destination) |
150 | | - case BQSqlFrag.FilledTableRef(fill) => |
151 | | - (fill.tableDef.partitionType, outerRef) match { |
152 | | - case (partitionType: BQPartitionType.DatePartitioned, Some(partitionRef: BQPartitionId.DatePartitioned)) => |
153 | | - List(fill.tableDef.withTableType(partitionType).assertPartition(partitionRef.partition)) |
154 | | - case (partitionType: BQPartitionType.HourPartitioned, Some(partitionRef: BQPartitionId.HourPartitioned)) => |
155 | | - List(fill.tableDef.withTableType(partitionType).assertPartition(partitionRef.partition)) |
156 | | - case (_, _) => List(fill.tableDef.unpartitioned.assertPartition) |
157 | | - } |
| 150 | + case BQSqlFrag.FilledTableRef(fill) => expandAndApplyOuterRef(fill.tableDef, outerRef) |
158 | 151 | } |
159 | 152 |
|
160 | 153 | this.collect(pf(None)).flatten.distinct |
161 | 154 | } |
162 | 155 |
|
163 | | - final def allReferencedTables: Seq[BQTableLike[Any]] = |
164 | | - allReferencedAsPartitions |
165 | | - .map(_.wholeTable) |
166 | | - .filterNot(tableLike => tableLike.isInstanceOf[BQTableDef.View[?]]) |
| 156 | + final def allReferencedTables(expandAndExcludeViews: Boolean): Seq[BQTableLike[Any]] = { |
| 157 | + def expand(table: BQTableLike[Any]): List[BQTableLike[Any]] = |
| 158 | + table match { |
| 159 | + case tableDef: BQTableDef.View[?] if expandAndExcludeViews => tableDef.query.collect(pf).flatten |
| 160 | + case tvf: BQAppliedTableValuedFunction[?] if expandAndExcludeViews => tvf.query.collect(pf).flatten |
| 161 | + case _ => List(table) |
| 162 | + } |
| 163 | + |
| 164 | + def pf: PartialFunction[BQSqlFrag, List[BQTableLike[Any]]] = { |
| 165 | + case BQSqlFrag.PartitionRef(partitionRef) => expand(partitionRef.wholeTable) |
| 166 | + case BQSqlFrag.TableRef(table) => expand(table) |
| 167 | + case BQSqlFrag.FillRef(fill) => List(fill.destination.wholeTable) |
| 168 | + case BQSqlFrag.FilledTableRef(fill) => List(fill.tableDef) |
| 169 | + } |
| 170 | + this.collect(pf).flatten.distinct |
| 171 | + } |
| 172 | + final def allReferencedTables: Seq[BQTableLike[Any]] = allReferencedTables(expandAndExcludeViews = true) |
167 | 173 |
|
168 | 174 | final def allReferencedTablesAsPartitions: Seq[BQPartitionId[Any]] = |
169 | 175 | allReferencedAsPartitions(expandAndExcludeViews = true) |
|
0 commit comments