1. Home
  2. Knowledge Base
  3. ClickHouse
  4. User Defined Functions (UDF)
  1. Home
  2. Knowledge Base
  3. ClickHouse Cloud
  4. User Defined Functions (UDF)
  1. Home
  2. Knowledge Base
  3. ClickHouse DBA
  4. User Defined Functions (UDF)

User Defined Functions (UDF)

ClickHouse comes shipped with tons of useful functions that can make the lives easier for the users. ClickHouse also allows users to create and register their own custom functions, which are also known as user defined functions (UDF). This could come in handy in cases where a particular function is required which are not already available in ClickHouse.

We can create a new UDF using the CREATE statement.

Syntax

CREATE FUNCTION function_name AS (input_parameter_1, input_parameter_2, ...) -> function_expression

The function_name must be unique and must not be already available in ClickHouse. The expression can have inbuilt ClickHouse function without being recursive.

Example

Let us create a function to convert the temperature units from Celsius to Fahrenheit.

chistadata :) CREATE FUNCTION fahrenheit AS (celsius) -> plus(multiply(celsius, 1.8), 32);
              

CREATE FUNCTION fahrenheit AS celsius -> ((celsius * 1.8) + 32)

Query id: cf82f93b-32af-4997-bd58-7cc6958afd91

Ok.

0 rows in set. Elapsed: 0.002 sec. 

Let us test this function.

chistadata :) SELECT fahrenheit(35);

SELECT fahrenheit(35)

Query id: dd827c61-4bab-4035-b010-9aaca73c7594

┌─plus(multiply(35, 1.8), 32)─┐
│                          95 │
└─────────────────────────────┘

1 row in set. Elapsed: 0.001 sec. 

To remove the function, we can use the DROP statement.

chistadata :) DROP FUNCTION fahrenheit;
              

DROP FUNCTION fahrenheit

Query id: 17f7846a-86eb-4436-85be-8b93c069f700

Ok.

0 rows in set. Elapsed: 0.001 sec.
chistadata :) SELECT fahrenheit(35);

SELECT fahrenheit(35)

Query id: 258087bf-cc6f-4b20-b5b2-7936ff97b014


0 rows in set. Elapsed: 0.003 sec. 

Received exception from server (version 22.12.1):
Code: 46. DB::Exception: Received from localhost:9000. DB::Exception: Unknown function fahrenheit: While processing fahrenheit(35). (UNKNOWN_FUNCTION)

References

https://clickhouse.com/docs/en/sql-reference/statements/create/function/

 

Was this article helpful?

Related Articles

CHISTADATA IS COMMITTED TO OPEN SOURCE SOFTWARE AND BUILDING HIGH PERFORMANCE COLUMNSTORES

In the spirit of freedom, independence and innovation. ChistaDATA Corporation is not affiliated with ClickHouse Corporation 

Need Support?

Can't find the answer you're looking for?
Contact Support

ChistaDATA Inc. Knowledge base is licensed under the Apache License, Version 2.0 (the “License”)

Copyright 2022 ChistaDATA Inc

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.