The "DB::Exception: Illegal type of argument" error is one of the most common ClickHouse errors. It occurs when you pass a column or literal of the wrong data type to a function. ClickHouse is strongly typed and does not perform implicit conversions in many situations, so a function expecting a numeric argument will reject a String, and vice versa.
Impact
The query fails immediately. This error is widespread in ad-hoc analytics, dashboards, and application code because it often stems from simple type mismatches that go unnoticed until execution. In automated pipelines the failed query can block downstream processing.
Common Causes
- Passing a String to a numeric function — for example, calling
toDate('not-a-date')orsum(string_column). - Using the wrong date/time function variant —
toDatevstoDateTime, or passing a DateTime64 where DateTime is expected. - Aggregate functions on non-numeric types —
avg(),sum(), and similar aggregates require numeric input. - Array functions receiving non-array arguments — calling
arrayJoin(),arrayMap(), orlength()on a scalar when an Array is expected. - Nullable vs non-Nullable mismatch — some functions do not accept Nullable types directly.
- Type confusion after JSON extraction — values extracted with
JSONExtractRawor similar functions return String, which then needs explicit casting.
Troubleshooting and Resolution Steps
Read the full error message. It specifies which function rejected which type. For example:
Illegal type String of argument of function toInt32.Check the column's actual type:
SELECT name, type FROM system.columns WHERE database = 'your_db' AND table = 'your_table' AND name = 'suspect_column';Cast the argument to the expected type. ClickHouse provides a rich set of conversion functions:
-- Convert String to Int SELECT toInt32(string_column) FROM your_table; -- Convert String to Date SELECT toDate(date_string_column) FROM your_table;Use
toTypeName()to debug types in complex expressions:SELECT toTypeName(some_expression) FROM your_table LIMIT 1;Handle Nullable types. If a function does not accept Nullable, unwrap it:
SELECT yourFunction(assumeNotNull(nullable_column)) FROM your_table; -- Or provide a default SELECT yourFunction(ifNull(nullable_column, 0)) FROM your_table;Use the correct function variant. Some functions have specialized versions for different types:
toDatevstoDateTimevstoDateTime64reinterpretAsStringvstoStringtoInt32vstoInt32OrNullvstoInt32OrZero
Check for LowCardinality wrappers. Most functions handle LowCardinality transparently, but in edge cases you may need to unwrap with
toLowCardinality()or cast explicitly.
Best Practices
- Use
toTypeName()liberally when debugging type errors — it reveals the exact type ClickHouse sees, including Nullable and LowCardinality wrappers. - Prefer explicit casts over relying on implicit conversions. This makes queries self-documenting and avoids surprises.
- When extracting values from JSON, always cast the result to the appropriate type immediately.
- Use the
OrNullandOrZerovariants of conversion functions (e.g.,toInt32OrNull) to handle invalid data gracefully instead of throwing errors.
Frequently Asked Questions
Q: Why does ClickHouse not just convert types automatically like MySQL does?
A: ClickHouse favors explicitness and performance. Implicit conversions can hide bugs and reduce query performance by adding unexpected casts. By requiring explicit types, ClickHouse ensures you get exactly the behavior you intend.
Q: How do I convert a String column that contains numbers to an integer?
A: Use toInt32(column) for strict conversion, or toInt32OrZero(column) / toInt32OrNull(column) if the column may contain non-numeric values.
Q: I get this error with arrayJoin. What's going on?
A: arrayJoin requires an Array argument. If your column is a String that looks like an array (e.g., '[1,2,3]'), you need to parse it first with JSONExtract or splitByChar before passing it to arrayJoin.
Q: Can Nullable cause this error even when the base type is correct?
A: Yes. Some functions explicitly reject Nullable arguments. Wrap the value with assumeNotNull() or ifNull() to provide a non-null value. The error message will mention Nullable(X) when this is the cause.