title: Running a python function in BigQuery
body: <p>Would it be possible to run a python function in BigQuery?</p>
  
  <p>It seems like C can be compiled to WebAssembly and run in BQ, per <a href="https://medium.com/@hoffa/bigquery-beyond-sql-and-js-running-c-and-rust-code-at-scale-33021763ee1f" rel="noreferrer">this blog post from Felipe</a>.</p>
  
  <p>And of course Python can be compiled to C or C++ using cython or some other tools (or it could even be transpiled to javascript). So then my question is does anyone have any experience executing a python function in BigQuery. If so, what is the flow that you're using to do it?</p>
  
  <p>Possible options here are:</p>
  
  <ul>
  <li>"Transform" the python into javascript to run.</li>
  <li>Compile the python into c or cpp and compile that into wasm</li>
  </ul>
  
  <p>Here is an example input to work with:</p>
  
  <p>(1) Source</p>
  
  <pre><code>id         product 
  1          box     
  2          bottle
  </code></pre>
  
  <p>(2) Python functions to use</p>
  
  <pre><code>def double_id(row):
      return row['id'] * 2
  
  def product_code(row):
      # B3
      return row['product'].upper()[0] + str(len(row['product']))
  </code></pre>
  
  <p>(3) Expected output</p>
  
  <pre><code>id     product      double_id       product_code
  1      box          2               B3
  2      bottle       4               B6
  </code></pre>
  
  <p>I'm not just looking to re-write the above using javascript (which would probably be the easiest way to do this), but I'm looking for a more generalized solution, if there is one that exists -- where I can take a python (standard library) function and use it in a BigQuery query.</p>