Class PushAggregationThroughOuterJoin

java.lang.Object
io.trino.sql.planner.iterative.rule.PushAggregationThroughOuterJoin
All Implemented Interfaces:
Rule<AggregationNode>

public class PushAggregationThroughOuterJoin extends Object implements Rule<AggregationNode>
This optimizer pushes aggregations below outer joins when: the aggregation is on top of the outer join, it groups by all columns in the outer table, and the outer rows are guaranteed to be distinct.

When the aggregation is pushed down, we still need to perform aggregations on the null values that come out of the absent values in an outer join. We add a cross join with a row of aggregations on null literals, and coalesce the aggregation that results from the left outer join with the result of the aggregation over nulls.

Example:

 - Filter ("nationkey" > "avg")
  - Aggregate(Group by: all columns from the left table, aggregation:
    avg("n2.nationkey"))
      - LeftJoin("regionkey" = "regionkey")
          - AssignUniqueId (nation)
              - Tablescan (nation)
          - Tablescan (nation)
 

Is rewritten to:

 - Filter ("nationkey" > "avg")
  - project(regionkey, coalesce("avg", "avg_over_null")
      - CrossJoin
          - LeftJoin("regionkey" = "regionkey")
              - AssignUniqueId (nation)
                  - Tablescan (nation)
              - Aggregate(Group by: regionkey, aggregation:
                avg(nationkey))
                  - Tablescan (nation)
          - Aggregate
            avg(null_literal)
              - Values (null_literal)