Possible to break out of a reduce operator in presto?

时间:2019-01-09 21:58:05

标签: sql lambda reduce presto

Wondering if it's possible to break out of a reduce operator in presto. Example use case:

I have a table where one column is an array of bigints, and I want to return all columns where the magnitude of the array is less than say 1000. So I could write

select 
* 
from table 
where reduce(array_col, 0, (s,x) -> s + power(x,2), s -> if(s < power(1000,2), TRUE, FALSE))

but if there are a lot of rows and the arrays are big, this can take a while. I would like the operator to break and return FALSE as soon as the sum exceeds 1000. Currently I have:

select 
* 
from table 
where reduce(array_col, 0, if(s >= power(1000,2), power(1000,2), s + power(x,2), s -> if(s < power(1000,2), TRUE, FALSE))

which at least saves some computation once the sum exceeds the target value, but still has to iterate through each array element.

1 个答案:

答案 0 :(得分:1)

不支持从数组精简中“中断”。

注意:从技术上讲,当您想休息一下并用1/0捕获时,可能会通过生成失败(例如try)来破解。我怀疑这是否值得。