| 
1 | 1 | package no.nrk.bigquery  | 
2 | 2 | 
 
  | 
3 | 3 | import cats.syntax.all._  | 
 | 4 | +import com.google.cloud.bigquery.Field.Mode  | 
 | 5 | +import com.google.cloud.bigquery.StandardSQLTypeName  | 
4 | 6 | import munit.FunSuite  | 
5 | 7 | import no.nrk.bigquery.BQPartitionType.DatePartitioned  | 
6 | 8 | import no.nrk.bigquery.syntax._  | 
@@ -70,4 +72,44 @@ class BQSqlFragTest extends FunSuite {  | 
70 | 72 |     assertEquals(tableIds, t2.tableId :: t1.tableId :: t3.tableId :: Nil)  | 
71 | 73 |   }  | 
72 | 74 | 
 
  | 
 | 75 | +  test("collect UDF used in body in other fragments, but not child BQFill") {  | 
 | 76 | +    case class JobKey(value: String) extends JobKeyBQ  | 
 | 77 | + | 
 | 78 | +    val outerUdf1 = UDF.temporary(  | 
 | 79 | +      Ident("outer1"),  | 
 | 80 | +      List(UDF.Param("input", BQType.INT64)),  | 
 | 81 | +      UDF.Body.Sql(bqsql"(2.0)"),  | 
 | 82 | +      Some(BQType.FLOAT64))  | 
 | 83 | + | 
 | 84 | +    val outerUdf2 = UDF.temporary(  | 
 | 85 | +      Ident("outer2"),  | 
 | 86 | +      List(UDF.Param("input", BQType.INT64)),  | 
 | 87 | +      UDF.Body.Sql(bqsql"(1.0)"),  | 
 | 88 | +      Some(BQType.FLOAT64))  | 
 | 89 | + | 
 | 90 | +    val fill2 = BQFill(JobKey("hello2"), mkTable("bree"), bqsql"select ${outerUdf2(1)}", LocalDate.of(2023, 1, 1))  | 
 | 91 | + | 
 | 92 | +    val fill1 = BQFill(  | 
 | 93 | +      JobKey("hello"),  | 
 | 94 | +      mkTable("baz"),  | 
 | 95 | +      bqsql"select ${outerUdf1(1)} from $fill2",  | 
 | 96 | +      LocalDate.of(2023, 1, 1)  | 
 | 97 | +    )  | 
 | 98 | + | 
 | 99 | +    val udfIdents = fill1.query  | 
 | 100 | +      .collect { case BQSqlFrag.Call(udf, _) => udf }  | 
 | 101 | +      .map(_.name)  | 
 | 102 | + | 
 | 103 | +    assertEquals(udfIdents, outerUdf1.name :: Nil)  | 
 | 104 | +  }  | 
 | 105 | + | 
 | 106 | +  def mkTable(name: String) = {  | 
 | 107 | +    val partitionField = BQField("partitionDate", StandardSQLTypeName.DATE, Mode.REQUIRED)  | 
 | 108 | + | 
 | 109 | +    BQTableDef.Table(  | 
 | 110 | +      BQTableId.of(ProjectId("foo"), "bar", name),  | 
 | 111 | +      BQSchema.of(partitionField, BQField("num", StandardSQLTypeName.FLOAT64, Mode.REQUIRED)),  | 
 | 112 | +      BQPartitionType.DatePartitioned(partitionField.ident)  | 
 | 113 | +    )  | 
 | 114 | +  }  | 
73 | 115 | }  | 
0 commit comments