大家想要的 ClickHouse UDF 這次終於來啦

大家一直想要的自定義函數,這次終於來啦,雖然目前還有點簡單,不過 CH 的功能進一步豐富了。

這幾天發現,一直有關注的自定義 simple lambda function 這個 feature 被合併到主線了,那還不趕緊試一試?(文末會放上 PR 地址)

自定義 lambda 函數的語法如下:

CREATE FUNCTION {fn_name} as ({parameters}) -> {code}

例如創建一個自定義函數 testFunc:

CREATE FUNCTION testFunc AS (a, b) -> (a + b)
Query id: da558806-259f-428b-b777-5ecbbea8bae1
Ok.
0 rows in set. Elapsed: 0.004 sec.

創建之後,就可以在查詢中使用了:

SELECT testFunc(1, 2)
Query id: 9fba3a87-e577-44ae-bfe2-332c587aa180
┌─plus(1, 2)─┐
│          3 │
└────────────┘
1 rows in set. Elapsed: 0.003 sec.

讓我們來一點複雜的情況:

CREATE FUNCTION testFunc1 AS (a, b, c) -> a + b > c AND c < 20

驗證結果:

SELECT
    testFunc1(5, 6, 1),
    testFunc1(5, 6, 19),
    testFunc1(20, 40, 30)
Query id: 17cfe0a7-681f-4b28-81fa-718fe38f8a2a
┌─and(greater(plus(5, 6), 1), less(1, 20))─┬─and(greater(plus(5, 6), 19), less(19, 20))─┬─and(greater(plus(20, 40), 30), less(30, 20))─┐
│                                        100 │
└──────────────────────────────────────────┴────────────────────────────────────────────┴──────────────────────────────────────────────┘
1 rows in set. Elapsed: 0.003 sec.

在自定義函數中,嘗試調用系統函數:

create function createInfo as (perfix,state) -> multiIf(state==0,concat(perfix,'未開始'),state>0 and state<1 ,concat(perfix,'進行中'),state>=1 ,concat(perfix,'已完成'),'未知狀態')

查詢是 OK 的,但是好像列名沒有辦法設置別名:

SELECT create_info('任務', 0) AS n1
Query id: 46c12694-3335-4d0b-8d55-e60802c68d2e
┌─multiIf(equals(0, 0), concat('任務', '未開始'), and(greater(0, 0), less(0, 1)), concat('任務', '進行中'), greaterOrEquals(0, 1), concat('任務', '已完成'), '未知狀態')─┐
│ 任務未開始                                                                                                                                                             │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
1 rows in set. Elapsed: 0.003 sec.

直接使用表字段也沒有問題:

SELECT
    testFunc(JavaEnable, GoodEvent),
    JavaEnable,
    GoodEvent
FROM hits_100m_obfuscated
LIMIT 5
Query id: 30e05cdd-da21-46db-8a11-49c5fb348668
┌─plus(JavaEnable, GoodEvent)─┬─JavaEnable─┬─GoodEvent─┐
│                           101 │
│                           101 │
│                           101 │
│                           101 │
│                           101 │
└─────────────────────────────┴────────────┴───────────┘
5 rows in set. Elapsed: 0.006 sec.

進到 ClickHouse 的存儲目錄,會發現多了一個 user_defined 目錄,專門用於保存 UDF 的元數據:

% data/user_defined % ls -l
total 16
-rw-r-----  1 nauu  staff  46  9 18 08:00 function_testFunc.sql
-rw-r-----  1 nauu  staff  71  9 18 08:03 function_testFunc1.sql
% cat ./function_testFunc.sql 
CREATE FUNCTION testFunc AS (a, b) -> (a + b)

我們也可以使用 Drop Function 刪除 UDF:

DROP FUNCTION testFunc1
Query id: 41b61849-a186-47b9-8072-c4af18be6bab
Ok.
0 rows in set. Elapsed: 0.006 sec.

自定義的函數也會被添加到系統表:

SELECT *
FROM system.functions
WHERE name = 'testFunc'
Query id: 97037df5-cda6-471f-933f-278670f3b113
┌─name─────┬─is_aggregate─┬─case_insensitive─┬─alias_to─┬─create_query──────────────────────────────────┐
│ testFunc │            00 │          │ CREATE FUNCTION testFunc AS (a, b) -> (a + b) │
└──────────┴──────────────┴──────────────────┴──────────┴───────────────────────────────────────────────┘
1 rows in set. Elapsed: 0.003 sec. Processed 1.06 thousand rows, 46.30 KB (304.53 thousand rows/s., 13.24 MB/s.)

好了,今天就分享到這裏吧,PR 在此:

https://github.com/ClickHouse/ClickHouse/pull/23978

ClickHouse 的祕密基地

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/gGITtstqyWDiKmPywsFf4w