• Data Warehouse Service

dws
  1. Help Center
  2. Data Warehouse Service
  3. Developer Guide
  4. SQL Reference
  5. Type Conversion
  6. UNION, CASE, and Related Constructs

UNION, CASE, and Related Constructs

SQL UNION constructs must match up possibly dissimilar types to become a single result set. The resolution algorithm is applied separately to each output column of a union query. The INTERSECT and EXCEPT constructs resolve dissimilar types in the same way as UNION. The CASE, ARRAY, VALUES, GREATEST and LEAST constructs use the identical algorithm to match up their component expressions and select a result data type.

Type Resolution for UNION, CASE, and Related Constructs

  • If all inputs are of the same type, and it is not unknown, resolve as that type.
  • If all inputs are of type unknown, resolve as type text (the preferred type of the string category). Otherwise, unknown inputs are ignored.
  • If the non-unknown inputs are not all of the same type category, fail. (Type unknown is not included.)
  • If the non-unknown inputs are all of the same type category, choose the first non-unknown input type which is a preferred type in that category, if there is one. (Exception: The UNION operation regards the type of the first branch as the selected type.)
    NOTE:

    typcategory in the pg_type system catalog indicates the data type category. typispreferred indicates whether a type is preferred in typcategory.

  • All the input is converted to the selected type. (The original length of a string is retained). Fail if there is not an implicit conversion from a given input to the selected type.

Type Resolution for CASE and COALESCE in TD Compatibility Type

  • If all inputs are of the same type, and it is not unknown, resolve as that type.
  • If all inputs are of type unknown, resolve as type text.
  • If inputs are of string type (including unknown which is resolved as type text) and digit type, resolve as the string type. If the inputs are not of the two types, fail.
  • If the non-unknown inputs are all of the same type category, choose the input type which is a preferred type in that category, if there is one.
  • Convert all inputs to the selected type. Fail if there is not an implicit conversion from a given input to the selected type.

Example

Example 1: Use type resolution with underspecified types in a union as the first example. Here, the unknown-type literal 'b' will be resolved to type text.

SELECT text 'a' AS "text" UNION SELECT 'b';
 text
------
 a
 b
(2 rows)

Example 2: Use type resolution in a simple union as the second example. The literal 1.2 is of type numeric, and the integer value 1 can be cast implicitly to numeric, so that type is used.

SELECT 1.2 AS "numeric" UNION SELECT 1;
 numeric
---------
       1
     1.2
(2 rows)

Example 3: Use type resolution in a transposed union as the third example. Since type real cannot be implicitly cast to integer, but integer can be implicitly cast to real, the union result type is resolved as real.

SELECT 1 AS "real" UNION SELECT CAST('2.2' AS REAL);
 real
------
    1
  2.2
(2 rows)

Example 4: In the TD type, if input parameters for COALESCE are of int and varchar types, resolve as type varchar. Fail in the ORA type.

-- In Oracle mode, create the oracle_1 database compatible with Oracle:
CREATE DATABASE oracle_1 dbcompatibility = 'ORA';

-- Switch to the oracle_1 database:
\c oracle_1

-- Create the t1 table:
oracle_1=# CREATE TABLE t1(a int, b varchar(10));

-- View the execution plan of the query statement that uses the coalesce parameter to enter data of int and varchar types:                 oracle_1=# EXPLAIN SELECT coalesce (a, b) FROM t1;
ERROR:  COALESCE types integer and character varying cannot be matched
CONTEXT:  referenced column: coalesce

-- Delete the tables.
oracle_1=# DROP TABLE t1;

-- Switch to the postgres database:
oracle_1=# \c postgres

-- In TD mode, create the td_1 database compatible with Teradata:
CREATE DATABASE td_1 dbcompatibility = 'TD';

-- Switch to the td_1 database:
\c td_1

-- Create the t2 table:
td_1=# CREATE TABLE t2(a int, b varchar(10));

-- Show the execution plan of a statement for querying the types int and varchar of input parameters for COALESCE:
td_1=# EXPLAIN VERBOSE select coalesce(a, b) from t2;
                                      QUERY PLAN
---------------------------------------------------------------------------------------
 Data Node Scan  (cost=0.00..0.00 rows=0 width=0)
   Output: (COALESCE((t1.a)::character varying, t1.b))
   Node/s: All datanodes
   Remote query: SELECT COALESCE(a::character varying, b) AS "coalesce" FROM public.t1
(4 rows)

-- Delete the tables.
td_1=# DROP TABLE t2;

-- Switch to the postgres database:
td_1=# \c postgres

-- Delete databases in Oracle and TD mode:
DROP DATABASE oracle_1;
DROP DATABASE td_1;