Error 70: CANNOT_CONVERT_TYPE
This error occurs when ClickHouse cannot convert data from one type to another due to incompatibility or invalid values. It indicates that a type conversion operation failed because the source data cannot be safely or logically converted to the target type.
Most common causes
-
Enum value mismatches during schema evolution
- Enum values changed between table schema and stored data
- Enum element has different numeric value in current schema vs. data parts
- Adding or removing enum values without proper migration
- Reordering enum values causing value conflicts
-
Invalid string to numeric conversions
- Trying to parse empty strings as numbers
- String contains non-numeric characters
- String value out of range for target numeric type
-
Field value out of range
- Numeric value exceeds the maximum/minimum for target type
- Large integers don't fit into smaller integer types
- Settings values outside valid range
-
Type incompatibility in comparisons or casts
- Comparing incompatible types without explicit conversion
- Implicit type conversions that ClickHouse doesn't support
- Wrong data type in partition column operations
-
Data corruption or schema conflicts
- Stored data doesn't match current table schema
- Metadata inconsistency between data parts
- Broken data parts after incomplete schema changes
What to do when you encounter this error
1. Check the error message for specific details
The error message usually includes:
- What value failed to convert
- Source and target types
- The context (column name, operation)
2. For Enum conversion errors - check schema history
3. For string to number conversions - validate your data
4. Check for data type mismatches
Quick fixes
1. For Enum schema changes - use safe conversion
2. For string to numeric conversions - use safe functions
3. For range issues - use appropriate types
4. For corrupted data parts - rebuild affected parts
5. Handle type conversions explicitly in queries
Common specific scenarios
Scenario 1: Enum conversion during merge
Cause: Data was written with one enum definition, but the schema changed and now the same element has a different numeric value.
Solution:
- Never reorder or change numeric values of existing enum elements
- Always add new enum values at the end
- If you must change enum values, recreate the table with data migration
Scenario 2: Empty string to integer conversion
Cause: Trying to convert an empty string to a numeric type.
Solution:
Scenario 3: Field value out of range
Cause: Setting or value exceeds the maximum value for the target type.
Solution:
Scenario 4: ClickPipe/Replication type mismatch
Cause: Column order mismatch between source and destination, or wrong type mapping.
Solution:
- Ensure column mapping uses names, not positions
- Verify data types match between source and target
- Check replication configuration for correct type mapping
Prevention best practices
- Always add enum values at the end - never reorder or change existing values
- Use safe conversion functions (
toInt32OrNull,toInt32OrZero) when data quality is uncertain - Validate data before insertion - use input format settings to handle bad data
- Choose appropriate data types - use types large enough for your data range
- Test schema changes carefully - especially with Enum types
- Monitor for conversion errors - set up alerts on error code 70