处理trino / presto中的数组(remove, sort)



in trino or presto I want to

取一个数组,删除一个模式类型,例如remove "b"重新排序数组,然后从数组中创建一个散列。

我知道如何获得长度:选择json_array_length("["b","a","b","c"]");但我不确定如何做剩下的

您可以使用各种函数对值数组进行操作:filter,array_sort等:

SELECT xxhash64(cast(json_format(cast(array_sort(filter(data, v -> v <> 'b')) as json)) as varbinary))
FROM (VALUES array['b', 'a', 'c', 'c']) AS t(data);

由于没有办法直接计算值数组的散列(例如,通过xxhash64,这需要varbinary值),您必须首先将过滤/排序的数组转换为varbinary。这可以很容易地完成,首先将其转换为具有文本表示的JSON,并且可以很容易地转换为varbinary:

xxhash64(
cast(
json_format(
cast(<array value> as json) 
as varbinary))

在Martin的帮助下,这是我想到的

select xxhash64(cast(json_format(cast(array_sort(filter(CAST(JSON '["b", "a", "b", "c"]' AS ARRAY(varchar)), v -> v <> 'b')) as json)) as varbinary));

最新更新