goccy / bigquery-emulator

BigQuery emulator server implemented in Go
MIT License
840 stars 107 forks source link

Support for the `bqutil` community user-defined functions #318

Open ohaibbq opened 6 months ago

ohaibbq commented 6 months ago

What happened?

The emulator cannot find a user-defined function. bqutil is filtered out from the zetasqlite's NamePath, so the analyzer cannot find it.

What did you expect to happen?

User-defined function executes successfully.

How can we reproduce it (as minimally and precisely as possible)?

Create a project with an example bqutil UDF:

  bq_client.run_query_async(
        query_str="""CREATE OR REPLACE FUNCTION bqutil.fn.json_extract_keys(input STRING)
RETURNS Array<String>
LANGUAGE js 
OPTIONS (description=\"\"\"Returns all keys in the input JSON as an array of string.
Returns NULL if invalid JSON string is passed.
\"\"\"
)
AS \"\"\"
  try {
    return Object.keys(JSON.parse(input));
  } catch {
    return null;
  }
\"\"\";
    """,
        use_query_cache=False,
    ).result()

Submit a query which uses this:

SELECT bqutil.fn.json_extract_keys(
  '{"foo" : "cat", "bar": "dog", "hat": "rat"}'
) AS keys_array

Anything else we need to know?

The bqutil functions are packaged with all installations of BigQuery. The emulator should support calling them, if not also packaging these UDFs into the emulator.