我对 JS 非常陌生,尤其是在雪花中,我正在尝试实现一个非常简单的事情——我想根据每一行的索引更新一个数组。
这是我的代码:
CREATE TABLE T1(C1 INT); -- test table
INSERT INTO T1(C1) VALUES (1),(2),(3),(4),(5),(6),(7);
-- MY UDTF:
CREATE OR REPLACE FUNCTION "SUMMER"(INF FLOAT)
RETURNS TABLE (NUM float, NUM2 array)
LANGUAGE JAVASCRIPT
AS '{
processRow: function (row, rowWriter, context) {
this.ar[this.index]=-1;
rowWriter.writeRow( {NUM: this.index, NUM2: this.ar} );
this.index=(this.index+1)%3;
},
finalize: function (rowWriter, context) {
rowWriter.writeRow({NUM: 0,NUM2: [0,0,0,0]});
},
initialize: function(argumentInfo, context) {
this.ar=[0,0,0,0]; --array i want to update at specific index
this.index = 0; --index changing every row
}}';
SELECT * FROM T1,TABLE(SUMMER(C1::FLOAT));
我收到的结果是:
1 0 [ -1, -1, -1, 0 ]
2 1 [ -1, -1, -1, 0 ]
3 2 [ -1, -1, -1, 0 ]
4 0 [ -1, -1, -1, 0 ]
5 1 [ -1, -1, -1, 0 ]
6 2 [ -1, -1, -1, 0 ]
7 0 [ -1, -1, -1, 0 ]
0 [ 0, 0, 0, 0 ]
而我希望能够根据索引更新数组,因此收到以下数组:
[0,0,0,0]
[-1,0,0,0]
[-1,-1,0,0]
[-1,-1,-1,0]
[-1,-1,-1,-1]
[-1,-1,-1,-1]
[-1,-1,-1,-1]