Presto Functions


1 求數組長度

cardinality(x) → bigint
Returns the cardinality (size) of the array x
eg: select cardinality(array[1,24,3])  
result: 3

2 獲取數組第一個元素(下標從1開始)

The [] operator is used to access an element of an array and is indexed starting from one

eg:select array[1,24,3][1]
result: 1

3,數組是否包含某個元素

contains(x, element) → boolean
Returns true if the array x contains the element

eg:select contains(array[1,24,3],3)
result: true

4,數組拼接

concat(array1, array2, ..., arrayN) → array
Concatenates the arrays array1, array2, ..., arrayN. 
This function provides the same functionality as the SQL-standard 
concatenation operator (||)
eg:select concat(array[1,2,3], array[2,3])
result:    [1, 2, 3, 2, 3]

5, 刪除數組中元素

array_remove(x, element) → array
Remove all elements that equal element from array x.
eg:select array_remove(array[1,2,3], 2)
result: [1, 3]

6,數組拼接爲字符串

array_join(x, delimiter, null_replacement) → varchar
Concatenates the elements of the given array using the delimiter 
and an optional string to replace nulls.
eg:select array_join(array[1,2,3],',')
result:1,2,3
eg:select select array_join(array[1,2,3, null],',','-')
result:1,2,3,-

7, json 解析函數

json_extract(json, json_path) → json
Evaluates the JSONPath-like expression json_path on 
json (a string containing JSON) and returns the result as a JSON string:
eg:SELECT json_extract('{"store":{"book":2}}', '$.store.book');
result:2
eg:SELECT json_extract(json_extract('{"store":{"book":{"book1":3}}}', '$.store.book'),'$.book1');
result:3

8, json 解析函數

json_extract_scalar(json, json_path) → varchar
Like json_extract(), but returns the result value as a string
 (as opposed to being encoded as JSON). The value referenced by json_path
 must be a scalar (boolean, number or string):
eg:SELECT json_extract_scalar('{"store":{"book":{"book1":"hello"}}}', '$.store.book.book1');
result:hello
eg:SELECT json_extract_scalar('{"store":{"book":{"book1":[1,2,3]}}}', '$.store.book.book1[0]');
result:1
eg:SELECT json_extract_scalar('{"store":{"book":{"book1":[1,2,3]}}}', '$.store.book.book1'); 
reuslt:空白

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章