简体   繁体   English

BigQuery 类型:如何定义任何类型的数组?

[英]BigQuery Types: How to define a array of any type?

Bigquery SQL-UDFs are quite convenient, but is there a possibility to define array arguments without specifying the type of its elements? Bigquery SQL-UDF 非常方便,但是是否有可能在不指定其元素类型的情况下定义数组 arguments? At least sometimes it would be nice to define operations on all arrays irrespective of the specific type.至少有时最好在所有 arrays 上定义操作,而不管具体类型如何。 For example, one could create a function to get the most frequent elements of an array like this:例如,可以创建一个 function 来获取数组中最常见的元素,如下所示:

CREATE TEMPORARY FUNCTION anyHEAVY(arr Array<ANY TYPE>) AS ((
  SELECT APPROX_TOP_COUNT(a, 1)[OFFSET(0)].value
  FROM UNNEST(arr) as a
));

However, it seems like BQ expects here a specific type and the generic "ANY TYPE" placeholder does not work anymore.但是,BQ 似乎在这里需要一个特定的类型,而通用的“ANY TYPE”占位符不再起作用。 Up to now, I am just using the type "any type" without forcing the argument to be an array.到目前为止,我只是使用“任何类型”类型,而没有强制参数是一个数组。 This works, but is IMHO not really clean and would require an additional check.这可行,但恕我直言,它不是很干净,需要额外检查。 I can imagine, that any type would cause some troubles, especially, in case of nested arrays or structs.我可以想象,任何类型都会引起一些麻烦,尤其是在嵌套 arrays 或结构的情况下。 However, would be great if one could define functions of arrays containing only "elementary" types (excluding arrays and structs).但是,如果可以定义仅包含“基本”类型(不包括 arrays 和结构)的 arrays 函数,那就太好了。

Currently there's no support for ARRAYS of ANY TYPE in SQL-UDF.目前在 SQL-UDF 中不支持ANY TYPEARRAYS The workaround is, as you mention, to declare the function as expecting ANY TYPE:正如您所提到的,解决方法是将 function 声明为期望任何类型:

CREATE TEMPORARY FUNCTION anyHEAVY(arr ANY TYPE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM