@@ -78,11 +78,7 @@ sealed trait BQSqlFrag {
7878 case BQSqlFrag .Frag (_) => Nil
7979 case BQSqlFrag .Call (_, args) => args
8080 case BQSqlFrag .Combined (values) => values.toList
81- case BQSqlFrag .PartitionRef (ref) =>
82- ref.wholeTable match {
83- case tableDef : BQTableDef .ViewLike [_] => List (tableDef.query)
84- case _ => Nil
85- }
81+ case BQSqlFrag .PartitionRef (_) => Nil
8682 case BQSqlFrag .FillRef (_) => Nil
8783 case BQSqlFrag .FilledTableRef (_) => Nil
8884 }
@@ -116,46 +112,43 @@ sealed trait BQSqlFrag {
116112 run(innerBodies(this :: Nil , Nil ), Nil , pf.lift) ++ run(this :: Nil , Nil , pf.lift)
117113 }
118114
119- final def allReferencedAsPartitions : Seq [BQPartitionId [Any ]] = allReferencedTablesAsPartitions(ignoreLabels = TableLabels .Empty )
120- final def allReferencedAsPartitions (ignoreLabels : TableLabels ): Seq [BQPartitionId [Any ]] =
115+ final def allReferencedAsPartitions : Seq [BQPartitionId [Any ]] =
116+ allReferencedAsPartitions(ignoreLabels = TableLabels .Empty , expandAndExcludeViews = true )
117+
118+ final def allReferencedAsPartitions (ignoreLabels : TableLabels , expandAndExcludeViews : Boolean ): Seq [BQPartitionId [Any ]] = {
119+ def pf : PartialFunction [BQSqlFrag , List [BQPartitionId [Any ]]] = {
120+ case BQSqlFrag .PartitionRef (ref) =>
121+ ref.wholeTable match {
122+ case tableDef : BQTableDef .View [_] if expandAndExcludeViews => tableDef.query.collect(pf).flatten
123+ case _ => List (ref)
124+ }
125+ case BQSqlFrag .FillRef (fill) => List (fill.destination)
126+ case BQSqlFrag .FilledTableRef (fill) => List (fill.tableDef.unpartitioned.assertPartition)
127+ }
128+
121129 this
122- .collect {
123- case BQSqlFrag .PartitionRef (ref) => ref
124- case BQSqlFrag .FillRef (fill) => fill.destination
125- case BQSqlFrag .FilledTableRef (fill) => fill.tableDef.unpartitioned.assertPartition
126- }
130+ .collect(pf)
131+ .flatten
127132 .distinct
128133 .collect { pid =>
129134 pid.wholeTable match {
130135 case tableDef : BQTableDef [_] if ! tableDef.labels.contains(ignoreLabels) => pid
131136 case ref : BQTableRef [_] if ! ref.labels.contains(ignoreLabels) => pid
132137 }
133138 }
139+ }
134140
135141 final def allReferencedTables : Seq [BQTableLike [Any ]] = allReferencedTables(ignoreLabels = TableLabels .Empty )
136142 final def allReferencedTables (ignoreLabels : TableLabels ): Seq [BQTableLike [Any ]] =
137- this
138- .collect {
139- case BQSqlFrag .PartitionRef (ref) => ref.wholeTable
140- case BQSqlFrag .FillRef (fill) => fill.tableDef
141- case BQSqlFrag .FilledTableRef (fill) => fill.tableDef
142- }
143- .distinct
144- .collect {
145- case ref : BQTableRef [_] if ! ref.labels.contains(ignoreLabels) => ref
146- case tableDef : BQTableDef .Table [_] if ! tableDef.labels.contains(ignoreLabels) => tableDef
147- }
148-
149- final def allReferencedTablesAsPartitions : Seq [BQPartitionId [Any ]] = allReferencedTablesAsPartitions(ignoreLabels = TableLabels .Empty )
150- final def allReferencedTablesAsPartitions (ignoreLabels : TableLabels = TableLabels .Empty ): Seq [BQPartitionId [Any ]] =
151- allReferencedAsPartitions(ignoreLabels)
152- .collect { pid =>
153- pid.wholeTable match {
154- case _ : BQTableDef .Table [_] => pid
155- case _ : BQTableDef .MaterializedView [_] => pid
156- case _ : BQTableRef [_] => pid
157- }
158- }
143+ allReferencedAsPartitions(ignoreLabels, expandAndExcludeViews = true )
144+ .map(_.wholeTable)
145+ .filterNot(tableLike => tableLike.isInstanceOf [BQTableDef .View [_]])
146+
147+ final def allReferencedTablesAsPartitions : Seq [BQPartitionId [Any ]] =
148+ allReferencedTablesAsPartitions(ignoreLabels = TableLabels .Empty )
149+ final def allReferencedTablesAsPartitions (ignoreLabels : TableLabels ): Seq [BQPartitionId [Any ]] =
150+ allReferencedAsPartitions(ignoreLabels, expandAndExcludeViews = true )
151+ .filterNot(pid => pid.wholeTable.isInstanceOf [BQTableDef .View [_]])
159152
160153 final def allReferencedUDFs : Seq [UDF [UDF .UDFId ]] =
161154 this .collect { case BQSqlFrag .Call (udf, _) => udf }.distinct
0 commit comments