ERROR: each UNION query must have the same number of columns
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Stado |
Fix Committed
|
Undecided
|
Unassigned |
Bug Description
select * from
(select stime, val, 0::bigint as val2, t1, t2 from tableA
union
select stime, val, val2, ''::text as t1, ''::text as t2 from tableB) t
order by stime;
got "ERROR: each UNION query must have the same number of columns",
while
select stime, val, 0::bigint as val2, t1, t2 from tableA
union
select stime, val, val2, ''::text as t1, ''::text as t2 from tableB
is ok.
seems the query plan has some bug? Below are logs:
###################
xtest=> show table tableA;
COLUMN_NAME | SQL_DATA_TYPE | TYPE_NAME | IS_NULLABLE | KEY | DEFAULT
-------
id | 4 | INTEGER | YES | NO |
stime | 93 | TIMESTAMP | YES | NO |
val | -5 | BIGINT | YES | NO |
t1 | 2005 | CLOB | YES | NO |
t2 | 2005 | CLOB | YES | NO |
(5 rows)
xtest=> show table tableB;
COLUMN_NAME | SQL_DATA_TYPE | TYPE_NAME | IS_NULLABLE | KEY | DEFAULT
-------
id | 4 | INTEGER | YES | NO |
stime | 93 | TIMESTAMP | YES | NO |
val | -5 | BIGINT | YES | NO |
val2 | -5 | BIGINT | YES | NO |
(4 rows)
xtest=> select * from tableA;
id | stime | val | t1 | t2
----+--
1 | 2013-10-16 16:08:30.203 | 12345 | text1 | text2
(1 row)
xtest=> select * from tableB;
id | stime | val | val2
----+--
1 | 2013-10-16 16:09:05.078 | 67890 | 1234567890
(1 row)
xtest=> select stime, val, 0::bigint as val2, t1, t2 from tableA union select stime, val, val2, ''::text as t1, ''::text as t2 from tableB;
stime | val | val2 | t1 | t2
-------
2013-10-16 16:08:30.203 | 12345 | 0 | text1 | text2
2013-10-16 16:09:05.078 | 67890 | 1234567890 | |
(2 rows)
xtest=> select * from (select stime, val, 0::bigint as val2, t1, t2 from tableA union select stime, val, val2, ''::text as t1, ''::text as t2 from tableB) t order by stime;
ERROR: each UNION query must have the same number of columns
###################
The same SQL executes without problem in PostgreSQL:
__xtest__N3=# select *from (select stime, val, 0::bigint as val2, t1, t2 from tableA union select stime, val, val2, ''::text as t1, ''::text as t2 from tableB) a order by stime;
stime | val | val2 | t1 | t2
-------
2013-10-16 16:08:30.203 | 12345 | 0 | text1 | text2
2013-10-16 16:09:05.078 | 67890 | 1234567890 | |
(2 rows)
Below SQL is OK:
xtest=> show table tableC; ------+ ------- ------- -+----- ------+ ------- ------+ -----+- ------- -
COLUMN_NAME | SQL_DATA_TYPE | TYPE_NAME | IS_NULLABLE | KEY | DEFAULT
-------
id | 4 | INTEGER | YES | NO |
stime | 93 | TIMESTAMP | YES | NO |
val | -5 | BIGINT | YES | NO |
(3 rows)
xtest=> select * from tableC; ------- ------- ------- --+---- --- ------- ------- ----+-- -----+- ------- ----+-- --+----
id | stime | val
----+--
1 | 2013-10-16 16:56:20.953 | 11111
(1 row)
xtest=> select * from (select stime, val,val2,''::text as t1, ''::text as t2 from tableB
union
select stime, val,0::bigint as val2,''::text as t1, ''::text as t2 from tableC) t order by stime;
stime | val | val2 | t1 | t2
-------
2013-10-16 16:09:05.078 | 67890 | 1234567890 | |
2013-10-16 16:56:20.953 | 11111 | 0 | |
(2 rows)