How to Use the CAST Operator for Data Type Conversion in ClickHouse
In ClickHouse, the CAST
operator is used to convert a value from one data type to another. This can be particularly useful in scenarios where you need to ensure that data types match between different parts of your queries, such as when comparing columns or inserting data into a table with a strict schema.
Syntax of the CAST Operator
The CAST
operator in ClickHouse can be used in two main syntax forms:
Using the
CAST()
FunctionCAST(expression AS type)
Using the
::
Operatorexpression :: type
Examples of Using CAST in ClickHouse
Here are several examples to demonstrate how to use the CAST
operator in ClickHouse:
1. Casting a String to an Integer
If you have a column or a value that is stored as a string but you need to perform numerical operations on it, you can cast it to an integer.
SELECT CAST('123' AS Int32) AS string_to_int;
Or using the ::
shorthand:
SELECT '123' :: Int32 AS string_to_int;
2. Casting a Float to a Decimal
For precise financial calculations, you might want to cast a floating point number to a decimal.
SELECT CAST(123.456 AS Decimal(10, 2)) AS float_to_decimal;
Or using the shorthand:
SELECT 123.456 :: Decimal(10, 2) AS float_to_decimal;
3. Casting an Integer to a String
When you need to concatenate numbers with strings, you may first need to cast the numbers to strings.
SELECT CAST(123 AS String) AS int_to_string;
Or using the shorthand:
SELECT 123 :: String AS int_to_string;
4. Using CAST in Table Creation
When creating a table, you might need to cast a type explicitly to match the expected schema, especially when importing data from different sources or formats.
CREATE TABLE example_table
(
id Int32,
created_at Date,
payload String
) ENGINE = MergeTree
ORDER BY id;
INSERT INTO example_table VALUES (1, CAST('2022-01-01' AS Date), 'data');
Best Practices and Considerations
Data Loss: Be mindful of data loss when casting from a higher precision or larger capacity type to a lower one (e.g., casting from
Float64
toInt32
).Performance: Frequent use of
CAST
can impact query performance, especially if casting occurs in a part of the query that is executed many times (like in aWHERE
clause or a join condition).Compatibility: Ensure the target type is compatible with the value being cast. For example, attempting to cast a non-numeric string to an Integer will result in an error.
By using the CAST
operator appropriately, you can handle a wide range of data manipulation tasks in ClickHouse more effectively, ensuring data types align correctly for your analytical queries.
Subscribe to my newsletter
Read articles from Shiv Iyer directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Shiv Iyer
Shiv Iyer
Over two decades of experience as a Database Architect and Database Engineer with core expertize in Database Systems Architecture/Internals, Performance Engineering, Scalability, Distributed Database Systems, SQL Tuning, Index Optimization, Cloud Database Infrastructure Optimization, Disk I/O Optimization, Data Migration and Database Security. I am the founder CEO of MinervaDB Inc. and ChistaDATA Inc.