1

我在 postgres 中有以下功能:

create function my_function(price numeric, qty numeric, min_charge numeric, other_fee numeric) returns numeric
    language plpgsql
as
$$
DECLARE
    _charge numeric;
BEGIN
    IF qty = 0 THEN
        RETURN 0.00::numeric(19, 2);
    END IF;
    _charge := GREATEST(price * qty, min_charge)::numeric(19, 2);
    RETURN (_charge + COALESCE(other_fee, 0.00))::numeric(19, 2);
END;
$$;

alter function my_function(numeric, numeric, numeric, numeric) owner to my_location;

我想将其转换为 BigQuery 以获得相同的功能,到目前为止我已经尝试过:

create procedure my_schema.my_function(price numeric, qty numeric, min_charge numeric, other_fee numeric)
BEGIN
    IF qty = 0 THEN
        RETURN 0.00::numeric(19, 2);
    END IF;
    _charge := GREATEST(price * qty, min_charge)::numeric(19, 2);
    RETURN (_charge + COALESCE(other_fee, 0.00))::numeric(19, 2);
END;

但我收到以下错误:

Syntax error: Expected ";" but got floating point literal "0.00" at [4:16]

在 BigQuery 中成功生成与过程相同的 postgres-style-function 的正确命令是什么?

4

2 回答 2

0

我想将其转换为 BigQuery 以获得相同的功能

考虑下面的例子

create temp function my_function(price numeric, qty numeric, min_charge numeric, other_fee numeric) 
returns numeric as (
  case when qty = 0 then 0
    else greatest(price * qty, min_charge) + coalesce(other_fee, 0)
  end
);
select my_function(1, 1, 1, 1)      

如果你想要永久功能 - 只需删除 temp increate temp function

于 2021-10-29T04:06:29.887 回答
0

通过将返回数据类型替换为FLOAT64. 试试这个脚本:

CREATE PROCEDURE mydataset.my_function(price numeric, qty numeric, min_charge numeric, other_fee numeric)
  BEGIN
    IF qty = 0 THEN 
        SELECT 0.00 AS FLOAT64;
    END IF;
     SELECT (GREATEST(price * qty, min_charge)  + COALESCE(other_fee, 0.00)) AS FLOAT64;
  END;

在此处输入图像描述

于 2021-10-29T03:39:05.170 回答