title: WITH RECURSIVE in bigquery udf
body: <p>I try to write bigquery udf with recursive cte as following code:</p>
  <pre><code>create or replace function accounting.monthly_sum(arr array&lt;struct&lt;aq numeric,ap numeric,m int64&gt;&gt;) returns numeric 
  as (
      (
      WITH RECURSIVE 
      rec AS (
        SELECT arr[0].aq, arr[0].m,least(arr[0].aq,arr[0].ap) ar 
        UNION ALL
        SELECT series.aq,series.m, least(series.aq + rec.ar, series.ap) 
        FROM UNNEST(arr) series 
        JOIN rec ON series.m = rec.m + 1
      )
      SELECT ar
      from rec
      order by m desc
      limit 1
      )
      );
  </code></pre>
  <p>This returns an error:</p>
  <p>SQL Error [100032] [HY000]: [Simba]BigQueryJDBCDriver Error executing query job. Message: WITH RECURSIVE is only allowed at the top level of the SELECT, CREATE TABLE AS SELECT, CREATE VIEW, INSERT, EXPORT DATA statements.</p>
  <p>I cannot find what is the problem.</p>