Error message pattern:
ODPS-0130241:[m,n] Illegal union operation - type mismatch for column xx of UNION, left is YY while right is ZZ
Cause
UNION and UNION ALL require every column pair across the two subqueries to have matching data types. When column xx on the left has type YY and the corresponding column on the right has type ZZ, and the types do not match, MaxCompute returns this error.
The error message tells you exactly which column failed (column xx) and the two conflicting types (left is YY, right is ZZ), so you know where to apply the fix.
Solution
Cast the mismatched column to a compatible type before the UNION.
The following example reproduces the error and shows the corrected query:
-- Create two tables with incompatible column types
CREATE TABLE mc_test1 (a STRING);
CREATE TABLE mc_test2 (a BIGINT);
-- Incorrect: UNION ALL fails because STRING and BIGINT are incompatible
SELECT a FROM mc_test1
UNION ALL
SELECT a FROM mc_test2;
-- FAILED: ODPS-0130241:[4,9] Illegal union operation - type mismatch for column 0 of UNION, left is STRING while right is BIGINT
-- Correct: cast BIGINT to STRING so both sides share the same type
SELECT a FROM mc_test1
UNION ALL
SELECT CAST(a AS STRING) FROM mc_test2;
Note: Match column meanings, not just types. If you reverse column order—for example,SELECT last_name, first_nameon the left andSELECT first_name, last_nameon the right—the types align and no error is raised, but the data will be incorrect. Verify that corresponding columns carry the same semantic meaning on both sides of the UNION.
该文章对您有帮助吗?