简体   繁体   English

标准 SQL (BigQuery) 中的用户定义数据类型?

[英]User defined data types in standard SQL (BigQuery)?

Is it possible to create user defined data types in standard SQL, which can then be referenced from within the SQL code itself?是否可以在标准 SQL 中创建用户定义的数据类型,然后可以从 SQL 代码本身中引用这些数据类型?

As an example, let's say I wanted to create a function that would take two instances of STRUCT - each containing three entries of type STRING .例如,假设我想创建一个函数,该函数将采用两个STRUCT实例 - 每个实例包含三个STRING类型的条目。 This would require writing something like this;这需要写这样的东西;

CREATE TEMP FUNCTION myFunc(x STRUCT<STRING, STRING, STRING>, y STRUCT<STRING, STRING, STRING>) AS (...);

What I want to do though, is something like this (pseudo-code);不过,我想做的是这样的(伪代码);

CREATE TYPE myType STRUCT<STRING, STRING, STRING>
CREATE TEMP FUNCTION myFunc(x myType, y myType) AS (...)

Something similar would be using ANY TYPE to create your function like:类似的事情是使用 ANY TYPE 创建您的函数,例如:

CREATE TEMP FUNCTION myFunc(x ANY TYPE, y ANY TYPE) AS (...);

To answer question in comments, if strong type check is needed, you can still do something like this and use to check parameter of myFunc() to report an error if type is incompatible:在评论中回答问题,如果需要强类型检查,你仍然可以做这样的事情,如果类型不兼容,你仍然可以使用检查 myFunc() 的参数来报告错误:

CREATE TEMP FUNCTION check_type_x(x ANY TYPE) AS (STRUCT(x.a, x.b, x.c));


SELECT myFunc(check_type_x(col1), check_type_x(col2));

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

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