Skip to content

UNION ALL BY NAME ​

UNION ALL BY NAME merges the rows of several queries. It matches the columns by name, not by position. Use it on datasets that share column names. The datasets can differ in column order, in optional variables and in numeric precision.

sql
SELECT * FROM read_netcdf(['argo/**/*.nc'])
UNION ALL BY NAME
SELECT * FROM read_netcdf(['wod/**/*.nc'])
UNION ALL BY NAME
SELECT * FROM read_netcdf(['cora/**/*.nc'])

The difference from plain UNION ALL ​

UNION ALLUNION ALL BY NAME
Column matchingBy positionBy name
Column order must matchYesNo
Missing columnsErrorBeacon sets them to NULL
Type mismatchesErrorBeacon widens the type

Missing columns become NULL ​

A column can exist in one input and not in the other. Beacon sets the missing side to NULL. The column is then nullable in the result:

sql
-- argo has 'salinity', wod does not
SELECT * FROM argo_table
UNION ALL BY NAME
SELECT * FROM wod_table
-- salinity is NULL for all wod rows

Automatic type widening ​

One column name can have different numeric types in the inputs. Beacon then widens the type to a common supertype:

LeftRightResult
Float32Float64Float64
Int8Int32Int32
Int32Int64Int64
Int32Float64Float64
Utf8LargeUtf8LargeUtf8
Date32Date64Date64
anyNullthe non-null type

Two incompatible types give a planning error. Boolean and Int32 are an example.

Reduce to a shared schema ​

Select only the columns that you need before the union. The output schema then stays clean. Extra variables in a source file do not matter:

sql
SELECT time, latitude, longitude, temperature, salinity
FROM read_netcdf(['argo/**/*.nc'])

UNION ALL BY NAME

SELECT time, latitude, longitude, temperature, salinity
FROM read_netcdf(['wod/**/*.nc'])

Store the result as a view ​

Wrap the union in a CREATE VIEW to give it a stable name:

sql
CREATE VIEW all_profiles AS
    SELECT time, latitude, longitude, temperature, salinity
    FROM read_netcdf(['argo/**/*.nc'])
    UNION ALL BY NAME
    SELECT time, latitude, longitude, temperature, salinity
    FROM read_netcdf(['wod/**/*.nc'])

Released under the AGPL-3.0 License.