Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud platform 在BigQuery中使用UDF时,是否可以在窗口之间保持共享状态?_Google Cloud Platform_Google Bigquery_Bigquery Udf - Fatal编程技术网

Google cloud platform 在BigQuery中使用UDF时,是否可以在窗口之间保持共享状态?

Google cloud platform 在BigQuery中使用UDF时,是否可以在窗口之间保持共享状态?,google-cloud-platform,google-bigquery,bigquery-udf,Google Cloud Platform,Google Bigquery,Bigquery Udf,这是我关于能够在BigQuery中模拟聚合函数(如PGSQL)的后续问题 前一个问题中提出的解决方案确实适用于应用于每个窗口的函数与前一个窗口无关的情况,如计算简单平均值等,但在计算递归函数(如指数移动平均值)时,公式为: EMA[i]=价格[i]*k+EMA[i-1]×(1)−k) 使用上一个问题中的相同示例 CREATE OR REPLACE FUNCTION temp_db.ema_func(arr ARRAY<int64>, window_size int8) RETURN

这是我关于能够在BigQuery中模拟聚合函数(如PGSQL)的后续问题

前一个问题中提出的解决方案确实适用于应用于每个窗口的函数与前一个窗口无关的情况,如计算简单平均值等,但在计算递归函数(如指数移动平均值)时,公式为:
EMA[i]=价格[i]*k+EMA[i-1]×(1)−k) 

使用上一个问题中的相同示例

CREATE OR REPLACE FUNCTION temp_db.ema_func(arr ARRAY<int64>, window_size int8)
RETURNS int64 LANGUAGE js AS """
    if(arr.length<=window_size){
        // calculate a simple moving average till end of first window
        var SMA = 0;
        for(var i = 0;i < arr.length; i++){
            SMA = SMA + arr[i]
        }
        return SMA/arr.length
    }else{
        // start calculation of EMA where EMA[i-1] is the SMA we calculated for the first window
        // note: hard-coded constant (k) for the sake of simplicity
        // the problem: where do I get EMA[i-1] or prev_EMA from?
        // in this example, we only need the most recent value, but in general case, we would 
        // potentially have to do other calculations with the new value 
        return curr[curr.length-1]*(0.05) + prev_ema*(1−0.05)
    }
""";

select s_id, temp_db.ema_func(ARRAY_AGG(s_price) over (partition by s_id order by s_date rows 40 preceding), 40) as temp_col
from temp_db.s_table;
创建或替换函数temp\u db.ema\u func(arr数组,窗口大小int8)
将int64语言js返回为“”

if(arr.length我不认为BigQuery可以通用,而是想看看具体的情况,看看是否有合理的解决办法。同时,递归性和聚合UDF在BQ中是不受支持的(希望现在还不受支持),所以您可能需要提交相应的解决方案


同时结帐,但我认为您的案例不适合那里

您的
temp\u db.s\u表中有多少行
表?目前约有6500万行。分区在一个名为“date\u month”的字段上,该字段对所有日期都是“YEAR-month-01”。这是一个约2200个股票的EOD价格表。我没有提到任何分区或过滤查询,不是o简化问题。因此EMA计算将在特定分区内-对吗?大小(行数)是多少对于/每个分区?我不会说一个分区,但可能是一组分区。因为每个分区都是表中所有符号的1个月EOD数据,我会说每个分区大约有20000*25个交易日=~500000条记录。如果EMA的周期大于25左右,自然会使用更多分区,对吗?当然。我的意思是计算对于所有分区,但每个分区/分区内。p.s.并不意味着我还有建议:o)只是试图澄清用例我现在正在编写功能请求。我刚刚在看BQ脚本,它可能能够解决我遇到的其他用例。感谢讨论!