Class CombineSimpleEquivalenceRule


@Enclosing public class CombineSimpleEquivalenceRule extends RelRule<CombineSimpleEquivalenceRule.Config>
Rule that optimizes a Combine operator by detecting shared sub-expressions across its inputs and introducing Spools to avoid redundant computation.

This rule identifies structurally equivalent sub-plans within a Combine's inputs and replaces them with a spool pattern: the first occurrence becomes a producer (TableSpool that materializes the result), and subsequent occurrences become consumers (TableScan reading from the spooled data).

Example

Consider two queries combined that share a common filtered table scan:


 -- Query 1: Count high earners
 SELECT COUNT(*) FROM EMP WHERE SAL > 2000
 -- Query 2: Average salary of high earners
 SELECT AVG(SAL) FROM EMP WHERE SAL > 2000
 

Before this rule applies, the plan looks like:


 Combine
   LogicalAggregate(group=[{}], CNT=[COUNT()])
     LogicalFilter(condition=[>(SAL, 2000)])
       LogicalTableScan(table=[EMP])
   LogicalAggregate(group=[{}], AVG_SAL=[AVG(SAL)])
     LogicalFilter(condition=[>(SAL, 2000)])
       LogicalTableScan(table=[EMP])
 

After this rule identifies the shared Filter(SAL > 2000) -> TableScan(EMP) sub-expression, the plan becomes:


 Combine
   LogicalAggregate(group=[{}], CNT=[COUNT()])
     LogicalTableSpool(table=[spool_0])        -- Producer: materializes filtered rows
       LogicalFilter(condition=[>(SAL, 2000)])
         LogicalTableScan(table=[EMP])
   LogicalAggregate(group=[{}], AVG_SAL=[AVG(SAL)])
     LogicalTableScan(table=[spool_0])         -- Consumer: reads from spool
 
See Also: