Error 258: UNION_ALL_RESULT_STRUCTURES_MISMATCH
This error occurs when the result sets of queries combined with UNION ALL have incompatible structures—different number of columns, different column types, or mismatched column names. All SELECT queries in a UNION ALL must return the same number of columns with compatible types.
Quick reference
What you'll see:
Or in recent versions, this may manifest as:
Most common causes:
- Different number of columns in UNION ALL queries
- Incompatible column types (e.g., String vs Int64)
- NULL type inference issues (NULL in one query, typed value in another)
- Column order mismatch between SELECT statements
Quick diagnostic:
Quick fixes:
Most common causes
1. Different number of columns
The most straightforward cause—each SELECT in the UNION ALL must return the same number of columns.
2. Incompatible column types
Even if column names match, types must be compatible or convertible.
3. NULL type inference issues (version-specific)
Before version 21.9, NULL handling was more lenient. Starting from 21.9+, ClickHouse is stricter about NULL type inference.
4. Column order mismatch
Column positions matter, not names. UNION ALL matches columns by position.
5. Projection optimization conflicts (24.10+ version-specific)
In versions 24.10+, there's a known issue where projection optimization can cause block structure mismatches in UNION operations, particularly with:
- Tables that have PROJECTION defined
- ARRAY JOIN operations
- Complex WHERE clauses with projections
Common solutions
1. Match column counts
2. Cast to compatible types
3. Fix NULL type ambiguity
4. Use UNION DISTINCT mode for automatic type coercion
5. Verify column order
6. Disable projection optimization (24.10+ workaround)
If you're encountering "Block structure mismatch in UnionStep stream" errors related to projections:
7. Debug with DESCRIBE
Prevention tips
-
Always match column counts: Every SELECT in UNION ALL must return the same number of columns.
-
Be explicit with types: Use explicit casts rather than relying on implicit type conversion, especially with NULL values.
-
Use consistent column order: Column positions matter more than names in UNION ALL.
-
Test each query separately: Before combining with UNION ALL, verify each SELECT works independently and returns expected types.
-
Avoid NULL-only queries: Don't use
SELECT NULL, NULLwithout explicit type casting. -
Document your schema: When combining data from multiple tables, document expected column types in comments.
-
Use table aliases for clarity:
-
Consider using UNION instead of UNION ALL if you need automatic type coercion (but be aware of performance implications).
Related error codes
- Error 49:
LOGICAL_ERROR- Related to internal block structure mismatches - Error 352:
AMBIGUOUS_COLUMN_NAME- Can occur with UNION and column name conflicts - Error 386:
NO_COMMON_TYPE- When types cannot be unified