-- Disabled query_plan_remove_redundant_distinct Expression (Projection) Distinct Distinct (Preliminary DISTINCT) Expression ((Before ORDER BY + Projection)) Distinct Distinct (Preliminary DISTINCT) Expression ((Before ORDER BY + Projection)) Distinct Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) ReadFromSystemNumbers -- Enabled query_plan_remove_redundant_distinct -- DISTINCT is only in most inner subquery -- query SELECT DISTINCT * FROM ( SELECT DISTINCT * FROM ( SELECT DISTINCT * FROM numbers(3) ) ) -- explain Expression ((Projection + (Before ORDER BY + (Projection + (Before ORDER BY + Projection))))) Distinct Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) ReadFromSystemNumbers -- execute 0 1 2 -- do _not_ remove DISTINCT after UNION -- query SELECT DISTINCT number FROM ( (SELECT DISTINCT number FROM numbers(1)) UNION ALL (SELECT DISTINCT number FROM numbers(2)) ) ORDER BY number -- explain Expression (Projection) Distinct Sorting (Sorting for ORDER BY) Distinct (Preliminary DISTINCT) Union Expression ((Before ORDER BY + Projection)) Distinct Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) ReadFromSystemNumbers Expression (( + Projection)) Distinct Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) ReadFromSystemNumbers -- execute 0 1 -- do _not_ remove DISTINCT after JOIN -- query SELECT DISTINCT * FROM ( SELECT DISTINCT number AS n FROM numbers(2) ) as x, ( SELECT DISTINCT number AS n FROM numbers(2) ) as y ORDER BY x.n, y.n -- explain Expression (Projection) Distinct Sorting (Sorting for ORDER BY) Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) Join (JOIN FillRightFirst) Expression ((Before JOIN + Projection)) Distinct Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) ReadFromSystemNumbers Expression ((Joined actions + (Rename joined columns + Projection))) Distinct Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) ReadFromSystemNumbers -- execute 0 0 0 1 1 0 1 1 -- DISTINCT duplicates with several columns -- query SELECT DISTINCT * FROM ( SELECT DISTINCT * FROM ( SELECT DISTINCT number as a, 2*number as b FROM numbers(3) ) ) ORDER BY a, b -- explain Expression (Projection) Sorting (Sorting for ORDER BY) Expression ((Before ORDER BY + (Projection + (Before ORDER BY + Projection)))) Distinct Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) ReadFromSystemNumbers -- execute 0 0 1 2 2 4 -- DISTINCT duplicates with constant columns -- query SELECT DISTINCT 2, a, b FROM ( SELECT DISTINCT a, b FROM ( SELECT DISTINCT 1, number as a, 2*number as b FROM numbers(3) ) ) ORDER BY a, b -- explain Expression (Projection) Sorting (Sorting for ORDER BY) Expression ((Before ORDER BY + (Projection + (Before ORDER BY + Projection)))) Distinct Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) ReadFromSystemNumbers -- execute 2 0 0 2 1 2 2 2 4 -- ARRAY JOIN: do _not_ remove outer DISTINCT because new rows are generated between inner and outer DISTINCTs -- query SELECT DISTINCT * FROM ( SELECT DISTINCT * FROM VALUES('Hello', 'World', 'Goodbye') ) AS words ARRAY JOIN [0, 1] AS arr ORDER BY c1, arr -- explain Expression (Projection) Distinct Sorting (Sorting for ORDER BY) Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) ArrayJoin (ARRAY JOIN) Expression ((Before ARRAY JOIN + Projection)) Distinct Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) ReadFromStorage (Values) -- execute Goodbye Hello World -- WITH FILL: do _not_ remove outer DISTINCT because new rows are generated between inner and outer DISTINCTs -- query SELECT DISTINCT * FROM ( SELECT DISTINCT * FROM values('id UInt8', 0, 2) ORDER BY id ASC WITH FILL ) -- explain Expression (Projection) Distinct Distinct (Preliminary DISTINCT) Expression ((Before ORDER BY + Projection)) Filling Distinct Sorting (Sorting for ORDER BY) Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) ReadFromStorage (Values) -- execute 0 1 2 -- WHERE with arrayJoin(): do _not_ remove outer DISTINCT because new rows are generated between inner and outer DISTINCTs -- query SELECT DISTINCT * FROM ( SELECT DISTINCT ['Istanbul', 'Berlin', 'Bensheim'] AS cities ) WHERE arrayJoin(cities) IN ['Berlin', 'Bensheim'] ORDER BY cities -- explain Expression (( + Projection)) Distinct Sorting (Sorting for ORDER BY) Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) Filter ((WHERE + Projection)) Distinct Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) ReadFromStorage (SystemOne) -- execute ['Istanbul','Berlin','Bensheim'] -- GROUP BY before DISTINCT with on the same columns => remove DISTINCT -- query SELECT DISTINCT a FROM ( SELECT a, sum(b) AS c FROM ( SELECT x.number AS a, y.number AS b FROM numbers(3) AS x, numbers(3, 3) AS y ) GROUP BY a ORDER BY a ) -- explain Expression ((Projection + (Before ORDER BY + Projection))) Sorting (Sorting for ORDER BY) Expression (Before ORDER BY) Aggregating Expression ((Before GROUP BY + (Projection + Before ORDER BY))) Join (JOIN FillRightFirst) Expression (Before JOIN) ReadFromSystemNumbers Expression ((Joined actions + (Rename joined columns + (Projection + Before ORDER BY)))) ReadFromSystemNumbers -- execute 0 1 2 -- GROUP BY before DISTINCT with on different columns => do _not_ remove DISTINCT -- query SELECT DISTINCT c FROM ( SELECT a, sum(b) AS c FROM ( SELECT x.number AS a, y.number AS b FROM numbers(3) AS x, numbers(3, 3) AS y ) GROUP BY a ORDER BY a ) -- explain Expression (Projection) Distinct Distinct (Preliminary DISTINCT) Expression ((Before ORDER BY + Projection)) Sorting (Sorting for ORDER BY) Expression (Before ORDER BY) Aggregating Expression ((Before GROUP BY + (Projection + Before ORDER BY))) Join (JOIN FillRightFirst) Expression (Before JOIN) ReadFromSystemNumbers Expression ((Joined actions + (Rename joined columns + (Projection + Before ORDER BY)))) ReadFromSystemNumbers -- execute 12 -- GROUP BY WITH ROLLUP before DISTINCT with on different columns => do _not_ remove DISTINCT -- query SELECT DISTINCT c FROM ( SELECT a, sum(b) AS c FROM ( SELECT x.number AS a, y.number AS b FROM numbers(3) AS x, numbers(3, 3) AS y ) GROUP BY a WITH ROLLUP ORDER BY a ) -- explain Expression (Projection) Distinct Distinct (Preliminary DISTINCT) Expression ((Before ORDER BY + Projection)) Sorting (Sorting for ORDER BY) Expression (Before ORDER BY) Rollup Aggregating Expression ((Before GROUP BY + (Projection + Before ORDER BY))) Join (JOIN FillRightFirst) Expression (Before JOIN) ReadFromSystemNumbers Expression ((Joined actions + (Rename joined columns + (Projection + Before ORDER BY)))) ReadFromSystemNumbers -- execute 12 36 -- GROUP BY WITH ROLLUP before DISTINCT with on the same columns => remove DISTINCT -- query SELECT DISTINCT a FROM ( SELECT a, sum(b) AS c FROM ( SELECT x.number AS a, y.number AS b FROM numbers(3) AS x, numbers(3, 3) AS y ) GROUP BY a WITH ROLLUP ORDER BY a ) -- explain Expression ((Projection + (Before ORDER BY + Projection))) Sorting (Sorting for ORDER BY) Expression (Before ORDER BY) Rollup Aggregating Expression ((Before GROUP BY + (Projection + Before ORDER BY))) Join (JOIN FillRightFirst) Expression (Before JOIN) ReadFromSystemNumbers Expression ((Joined actions + (Rename joined columns + (Projection + Before ORDER BY)))) ReadFromSystemNumbers -- execute 0 0 1 2 -- GROUP BY WITH CUBE before DISTINCT with on different columns => do _not_ remove DISTINCT -- query SELECT DISTINCT c FROM ( SELECT a, sum(b) AS c FROM ( SELECT x.number AS a, y.number AS b FROM numbers(3) AS x, numbers(3, 3) AS y ) GROUP BY a WITH CUBE ORDER BY a ) -- explain Expression (Projection) Distinct Distinct (Preliminary DISTINCT) Expression ((Before ORDER BY + Projection)) Sorting (Sorting for ORDER BY) Expression (Before ORDER BY) Cube Aggregating Expression ((Before GROUP BY + (Projection + Before ORDER BY))) Join (JOIN FillRightFirst) Expression (Before JOIN) ReadFromSystemNumbers Expression ((Joined actions + (Rename joined columns + (Projection + Before ORDER BY)))) ReadFromSystemNumbers -- execute 12 36 -- GROUP BY WITH CUBE before DISTINCT with on the same columns => remove DISTINCT -- query SELECT DISTINCT a FROM ( SELECT a, sum(b) AS c FROM ( SELECT x.number AS a, y.number AS b FROM numbers(3) AS x, numbers(3, 3) AS y ) GROUP BY a WITH CUBE ORDER BY a ) -- explain Expression ((Projection + (Before ORDER BY + Projection))) Sorting (Sorting for ORDER BY) Expression (Before ORDER BY) Cube Aggregating Expression ((Before GROUP BY + (Projection + Before ORDER BY))) Join (JOIN FillRightFirst) Expression (Before JOIN) ReadFromSystemNumbers Expression ((Joined actions + (Rename joined columns + (Projection + Before ORDER BY)))) ReadFromSystemNumbers -- execute 0 0 1 2 -- GROUP BY WITH TOTALS before DISTINCT with on different columns => do _not_ remove DISTINCT -- query SELECT DISTINCT c FROM ( SELECT a, sum(b) AS c FROM ( SELECT x.number AS a, y.number AS b FROM numbers(3) AS x, numbers(3, 3) AS y ) GROUP BY a WITH TOTALS ORDER BY a ) -- explain Expression (Projection) Distinct Distinct (Preliminary DISTINCT) Expression ((Before ORDER BY + Projection)) Sorting (Sorting for ORDER BY) Expression (Before ORDER BY) TotalsHaving Aggregating Expression ((Before GROUP BY + (Projection + Before ORDER BY))) Join (JOIN FillRightFirst) Expression (Before JOIN) ReadFromSystemNumbers Expression ((Joined actions + (Rename joined columns + (Projection + Before ORDER BY)))) ReadFromSystemNumbers -- execute 12 36 -- GROUP BY WITH TOTALS before DISTINCT with on the same columns => remove DISTINCT -- query SELECT DISTINCT a FROM ( SELECT a, sum(b) AS c FROM ( SELECT x.number AS a, y.number AS b FROM numbers(3) AS x, numbers(3, 3) AS y ) GROUP BY a WITH TOTALS ORDER BY a ) -- explain Expression ((Projection + (Before ORDER BY + Projection))) Sorting (Sorting for ORDER BY) Expression (Before ORDER BY) TotalsHaving Aggregating Expression ((Before GROUP BY + (Projection + Before ORDER BY))) Join (JOIN FillRightFirst) Expression (Before JOIN) ReadFromSystemNumbers Expression ((Joined actions + (Rename joined columns + (Projection + Before ORDER BY)))) ReadFromSystemNumbers -- execute 0 1 2 0 -- DISTINCT COUNT() with GROUP BY => do _not_ remove DISTINCT -- query select distinct count() from numbers(10) group by number -- explain Expression (Projection) Distinct Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) Aggregating Expression (Before GROUP BY) ReadFromSystemNumbers -- execute 1 -- UNION ALL with DISTINCT => do _not_ remove DISTINCT -- query SELECT DISTINCT number FROM ( SELECT DISTINCT number FROM numbers(1) UNION ALL SELECT DISTINCT number FROM numbers(2) ) ORDER BY number -- explain Expression (Projection) Distinct Sorting (Sorting for ORDER BY) Distinct (Preliminary DISTINCT) Union Expression ((Before ORDER BY + Projection)) Distinct Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) ReadFromSystemNumbers Expression (( + Projection)) Distinct Distinct (Preliminary DISTINCT) Expression (Before ORDER BY) ReadFromSystemNumbers -- execute 0 1