【问题标题】:presto function to check if array contains subarraypresto 函数检查数组是否包含子数组
【发布时间】:2018-08-21 12:19:18
【问题描述】:

我有一个 Presto 数据库,其中包含一个列数组,例如:

  1. id1,[1,2,3,4]
  2. id2,[3,4,5,6]
  3. id3,[3,4,7,8]
  4. id4,[5,4,3,6]

我需要以正确的顺序搜索包含数组 [3,4,5] 的行。 因此,例如结果应该只返回 id2 而不是 id4。

我可以结合使用 array_intersect 和基数来查找 id2,id4 但我不知道如何验证 id2 或 id4 的顺序是否正确。

我能想到的唯一丑陋的解决方案就是将两个数组转换成字符串,然后进行类似字符串的操作。

有更好的想法吗?

遵循以下建议并使用 AWS Athena:

WITH dataset AS (
    (values array[1,2,3,4], 
    array[3,4,5,6], 
    array[3,4,7,8], 
    array[5,4,3,6])
)
SELECT ngrams FROM dataset t(ngrams) where reduce(
    transform(array[3,4,5], a -> array_position(ngrams, a)),
    0, 
    (s, n) -> if( s < 0, -1, if ( n > s, n, -1)),
    s -> s >= 0) ;

我得到的错误是:

SYNTAX_ERROR:第 7:44 行:意外参数(数组(bigint), 整数, com.facebook.presto.sql.analyzer.TypeSignatureProvider@1d8b3792, com.facebook.presto.sql.analyzer.TypeSignatureProvider@563900c2) 为 功能减少。预期:减少(数组(T),S,函数(S,T,S), 函数(S,R)) T, S, R

【问题讨论】:

    标签: sql presto


    【解决方案1】:

    你的魔法来了:

    select x 
    from (values 
        array[1,2,3,4], 
        array[3,4,5,6], 
        array[3,4,7,8], 
        array[5,4,3,6]) t(x)
    where reduce(
        transform(array[3,4,5], a -> array_position(x, a)),
        0, 
        (s, n) -> if( s < 0, -1, if ( n > s, n, -1)),
        s -> s >= 0) 
    

    上面找到查询数组中的每个元素,如果位置数组正在增加,则返回true。 这仍然有很多极端情况需要解决(处理重复或间隙),但我希望这是你可以开始使用的东西。

    详情请见https://prestosql.io/docs/current/functions/array.html

    【讨论】:

    • 您好,我正在 Athena 上运行查询,但出现错误,请参阅我原来的问题。
    • 您使用的是哪个版本的 Presto?我在 Starburst Presto Distribution 上对其进行了测试,效果很好。
    • Interesting Athena 使用 Presto 0.172,所以可能与 Starburst 不同。
    猜你喜欢
    • 1970-01-01
    • 2019-08-10
    • 2021-05-27
    • 1970-01-01
    • 2011-04-23
    • 2020-10-10
    • 2016-10-13
    • 2015-01-01
    • 2021-07-15
    相关资源
    最近更新 更多