NO_COMMON_TYPE ClickHouse error

This error usually means you're trying to combine or compare values of incompatible types. ClickHouse can't automatically convert between these types, so you need to use explicit type casting.

The NO_COMMON_TYPE error in ClickHouse (and Tinybird) happens when you try to perform operations between values of incompatible types, and ClickHouse can't determine a common type for the operation. This commonly occurs in comparisons, arithmetic operations, or when combining different data types.

What causes this error

You'll typically see it when:

  • Comparing values of different types (e.g., String vs Int)
  • Performing arithmetic operations on mixed types
  • Using UNION with incompatible column types
  • Combining columns with different data types in expressions
  • Using functions that expect specific types but receive others

Example errors

Fails: mixing types in IF function
SELECT if(user_id = 123, 'user_123', user_id) FROM events
Fails: arithmetic with mixed types
SELECT value + '10' FROM events
Fails: UNION with different types
SELECT user_id FROM events
UNION ALL
SELECT email FROM users
Fails: mixing types in CASE
SELECT CASE 
    WHEN user_id = 123 THEN 'user_123'
    WHEN user_id = 456 THEN user_id
END FROM events

How to fix it

Use explicit type casting

Convert values to compatible types using CAST or type conversion functions:

Fix: cast String to Int for comparison
SELECT * FROM events WHERE user_id = toInt32('123')
Fix: cast Int to String for arithmetic
SELECT value + toInt32('10') FROM events

Use proper type conversion functions

ClickHouse provides specific conversion functions for different types:

Fix: use toInt32 for conversion
SELECT * FROM events WHERE user_id = toInt32(user_id_string)
Fix: use toString for string conversion
SELECT toString(user_id) as user_id_str FROM events

Fix UNION queries

Ensure all columns in UNION have compatible types:

Fix: cast to common type in UNION
SELECT toString(user_id) as id FROM events
UNION ALL
SELECT email as id FROM users

Common patterns and solutions

String vs Numeric comparisons

When comparing strings with numbers:

Wrong: mixing types in IF
SELECT if(user_id = 123, 'user_123', user_id) FROM events
Correct: cast string to number
WHERE user_id = toInt32('123')
Correct: cast number to string
WHERE toString(user_id) = '123'

Date and DateTime operations

When working with dates and timestamps:

Wrong: mixing Date and DateTime
WHERE date_column = now()
Correct: convert to same type
WHERE toDate(date_column) = toDate(now())

Array operations

When working with arrays of different types:

Wrong: mixing array types
SELECT [1, 2, 3] + ['a', 'b', 'c']
Correct: convert to common type
SELECT [toString(1), toString(2), toString(3)] + ['a', 'b', 'c']

Advanced solutions

Using CASE statements with mixed types

Convert all branches to a common type:

Fix: convert all CASE branches to String
SELECT CASE 
    WHEN user_id = 123 THEN 'user_123'
    WHEN user_id = 456 THEN 'user_456'
    ELSE toString(user_id)
END as user_label FROM events

Working with JSON data

When extracting values from JSON:

Fix: cast JSON values to proper types
SELECT 
    toInt32(JSONExtractRaw(data, 'user_id')) as user_id,
    toString(JSONExtractRaw(data, 'event_type')) as event_type
FROM events

Handling NULL values

Be careful with NULL values in type conversions:

Fix: handle NULL in type conversion
SELECT toInt32OrZero(user_id_string) as user_id FROM events

Tinybird-specific notes

In Tinybird, this error often occurs when:

  • Data Sources have inconsistent data types in the same column
  • Pipes try to combine data from different sources with incompatible schemas
  • Schema inference assigns wrong types to columns
  • JSON data contains mixed types

To debug in Tinybird:

  1. Check your Data Source schema for type consistency
  2. Use Schema Hints to enforce proper types
  3. Add explicit CAST operations in your Pipes
  4. Validate incoming data types in Data Sources

Type conversion functions

Common ClickHouse type conversion functions:

  • toInt8(), toInt16(), toInt32(), toInt64() - Convert to integers
  • toUInt8(), toUInt16(), toUInt32(), toUInt64() - Convert to unsigned integers
  • toFloat32(), toFloat64() - Convert to floating point
  • toString() - Convert to string
  • toDate(), toDateTime() - Convert to date/time types
  • toUUID() - Convert to UUID

See also