Question

Datatype mess when UNION ALL

  • 10 October 2016
  • 3 replies
  • 1942 views

Userlevel 3

Anyone else having datatype mess with Redshift’s weird casting of integer/timezones/boolean datatypes into varchar when UNION ALL?


These are the typical errors:

column … is of type integer but expression is of type character varying

is of type boolean but expression is of type timestamp without time zone


3 replies

Userlevel 2

@segahm Are you doing the casting on the top-level projection in the union all? Better yet—are you casting uniformly and consistently on all projections in the union all? You may also have to cast any top-level null values.

Userlevel 3

@scott.hoover looks like after using BigQuery for awhile, I forgot the nuances of Redshift:



  • column order matters

  • outer queries sometimes need their own casting

Userlevel 3

To elaborate on this. In BigQuery’s legacy SQL, column order did not matter:


So, you could UNION 2 tables as follows:


SELECT
x,
y
FROM
(SELECT 1 AS x, "foo" AS y),
(SELECT "bar" AS y, 2 AS x);

It was a great feature that is missing from many dialects. Cautionary note: In BigQuery’s new Standard SQL, the above will no longer work:


SELECT
x,
y
FROM
(SELECT 1 AS x, "foo" AS y UNION ALL
SELECT "bar" AS y, 2 AS x);

Instead, you have to match columns as in Redshift:


SELECT
x,
y
FROM
(SELECT 1 AS x, "foo" AS y UNION ALL
SELECT 2 AS x, "bar" AS y);

Reply