Fixed
Status Update
Comments
ho...@google.com <ho...@google.com> #2
+1 because this doesn't work:
#standardSQL
CREATE TEMP FUNCTION barchart(v ARRAY<FLOAT64>, mm STRUCT<min FLOAT64, max FLOAT64>) AS ((
SELECT STRING_AGG(SUBSTR('🏿🏾🏽🏼🏻', 1+CAST(ROUND(y) AS INT64), 1), '')
FROM (SELECT IFNULL(SAFE_DIVIDE((e-mm.min),(mm.max-mm.min))*4, 0) y FROM UNNEST(v) e)));
CREATE TEMP FUNCTION vbar(v ARRAY<FLOAT64>) AS (
barchart(v, (SELECT AS STRUCT MIN(a), MAX(a) FROM UNNEST(v) a))
);
SELECT vbar([1,2,3])
but this does:
SELECT vbar([1,2,3.0])
#standardSQL
CREATE TEMP FUNCTION barchart(v ARRAY<FLOAT64>, mm STRUCT<min FLOAT64, max FLOAT64>) AS ((
SELECT STRING_AGG(SUBSTR('🏿🏾🏽🏼🏻', 1+CAST(ROUND(y) AS INT64), 1), '')
FROM (SELECT IFNULL(SAFE_DIVIDE((e-mm.min),(mm.max-mm.min))*4, 0) y FROM UNNEST(v) e)));
CREATE TEMP FUNCTION vbar(v ARRAY<FLOAT64>) AS (
barchart(v, (SELECT AS STRUCT MIN(a), MAX(a) FROM UNNEST(v) a))
);
SELECT vbar([1,2,3])
but this does:
SELECT vbar([1,2,3.0])
el...@google.com <el...@google.com> #3
Templating is now supported for SQL UDFs (using "ANY TYPE"): https://cloud.google.com/bigquery/docs/reference/standard-sql/user-defined-functions#templated-sql-udf-parameters .
For JavaScript UDFs, you can apply TO_JSON_STRING to the inputs, e.g. MyJsFunction(TO_JSON_STRING(STRUCT(x, y, z))) to avoid having to specify the input types; see alsohttps://cloud.google.com/bigquery/docs/reference/standard-sql/user-defined-functions#external-udf-examples
For JavaScript UDFs, you can apply TO_JSON_STRING to the inputs, e.g. MyJsFunction(TO_JSON_STRING(STRUCT(x, y, z))) to avoid having to specify the input types; see also
Description
CREATE TEMP FUNCTION StructAsJson(s ANY STRUCT) RETURNS STRING LANGUAGE js AS 'return s.stringify()';
And then use it with a table:
SELECT StructAsJson(t) AS json
FROM MyTable AS t;
Based on the StackOverflow post: