v.21.1New Features
Implement UNION DISTINCT as Default and Add union_default_mode Setting
ImplementUNION DISTINCTand treat the plainUNIONclause asUNION DISTINCTby default. Add a settingunion_default_modethat allows to treat it asUNION ALLor require explicit mode specification. #16338 (flynn).
Why it matters
The feature addresses ambiguity in the default behavior of theUNION clause by explicitly supporting UNION DISTINCT. It provides users with control over the default set operation mode, improving query clarity and predictability. This reduces accidental data duplication or unexpected result sets when using UNION without specifying ALL or DISTINCT.How to use it
By default, plainUNION is treated as UNION DISTINCT. To change this behavior, set the union_default_mode setting to one of the following values:-
'distinct' to treat UNION as UNION DISTINCT (default)-
'all' to treat UNION as UNION ALL-
'explicit' to require explicit specification of ALL or DISTINCT in queriesExample to set the mode in a session:
SET union_default_mode = 'all';Then you can use:
SELECT FROM table1
UNION
SELECT FROM table2where
UNION behavior corresponds to your mode setting.