简体   繁体   English

无法使用 function (GCP) 保存视图

[英]Can´t save view with function (GCP)

I´m working with BIGQUERY trying to produce a view using a query with a Function (This is an example not the query itself)我正在使用 BIGQUERY 尝试使用带有 Function 的查询生成视图(这是一个示例,而不是查询本身)

CREATE TEMP FUNCTION validate_rut(s string) CREATE TEMP FUNCTION validate_rut(s string)

RETURNS string返回字符串

AS (作为 (

if(length(s) = 10 or length(s) = 12, left(regexp_replace(s, r'[.-]', ''), 8) if(length(s) = 10 或 length(s) = 12, left(regexp_replace(s, r'[.-]', ''), 8)

, if(length(s) = 11 or length(s) = 9, left(regexp_replace(s, r'[.-]', ''), 7) , if(length(s) = 11 或 length(s) = 9, left(regexp_replace(s, r'[.-]', ''), 7)

, null) , 无效的)

) )

); );

select rut, validate_rut(rut) select 车辙,验证车辙(车辙)

from (select '11.111.111-8' rut union all从(选择'11.111.111-8'车辙联合所有

select '11111111-8' union all select '11111111-8' 联合所有

select '2.222.222-9' union all select '2.222.222-9' 联合所有

select '2222222-9'union all select '2222222-9'联合所有

select '33333333' union all select '33333333' 联合所有

select '7777777' select '7777777'

) )

The problem is that when I try to save the query as a view I get this message.问题是,当我尝试将查询另存为视图时,我收到此消息。

"No support For create Temporary Function statements inside views" “不支持在视图中创建临时 Function 语句”

This is the first time i´m trying to do something like this.这是我第一次尝试做这样的事情。

Thank You.谢谢你。

You can create your function as permanant udf with the following syntax:您可以使用以下语法将 function 创建为永久 udf:

CREATE FUNCTION `my_project.my_dataset.validate_rut`(s string)

RETURNS string
AS (
if(length(s) = 10 or length(s) = 12 , left(regexp_replace(s, r'[.-]', ''), 8)
, if(length(s) = 11 or length(s) = 9, left(regexp_replace(s, r'[.-]', ''), 7)
, null)
)
);

In this example your permanent udf is created on an existing Bigquery dataset called my_dataset .在此示例中,您的永久 udf 是在名为udf的现有Bigquery datasetmy_dataset的。

Then you can query your function as expected with the following syntax:然后,您可以使用以下语法按预期查询您的 function:

select rut, `my_project.my_dataset.validate_rut`(rut)
from (select '11.111.111-8' rut union all
select '11111111-8' union all
select '2.222.222-9' union all
select '2222222-9'union all
select '33333333' union all
select '7777777'
);

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

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