Class DruidAggregateCaseToFilterRule

java.lang.Object
org.apache.calcite.plan.RelOptRule
org.apache.druid.sql.calcite.rule.DruidAggregateCaseToFilterRule
All Implemented Interfaces:
org.apache.calcite.rel.rules.SubstitutionRule, org.apache.calcite.rel.rules.TransformationRule

public class DruidAggregateCaseToFilterRule extends org.apache.calcite.plan.RelOptRule implements org.apache.calcite.rel.rules.SubstitutionRule
Druid extension of AggregateCaseToFilterRule. Turning on extendedFilteredSumRewrite enables rewrites of:
    SUM(CASE WHEN COND THEN COL1 ELSE 0 END)
 
to:
    SUM(COL1) FILTER (WHERE COND)
 

This rewrite improves performance but introduces a known inconsistency when the condition never matches, as the expected result (0) is replaced with `null`.

Example behavior:

 +-----------------+--------------+----------+------+--------------+
 | input row count | cond matches | valueCol | orig | filtered-SUM |
 +-----------------+--------------+----------+------+--------------+
 | 0               | *            | *        | null | null         |
 | >0              | none         | *        | 0    | null         |
 | >0              | all          | null     | null | null         |
 | >0              | N>0          | 1        | N    | N            |
 +-----------------+--------------+----------+------+--------------+
 
  • Nested Class Summary

    Nested classes/interfaces inherited from class org.apache.calcite.plan.RelOptRule

    org.apache.calcite.plan.RelOptRule.ConverterRelOptRuleOperand
  • Field Summary

    Fields inherited from class org.apache.calcite.plan.RelOptRule

    description, operands, relBuilderFactory
  • Constructor Summary

    Constructors
    Constructor
    Description
    DruidAggregateCaseToFilterRule(boolean extendedFilteredSumRewrite)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    matches(org.apache.calcite.plan.RelOptRuleCall call)
     
    void
    onMatch(org.apache.calcite.plan.RelOptRuleCall call)
     

    Methods inherited from class org.apache.calcite.plan.RelOptRule

    any, convert, convert, convert, convert, convertList, convertOperand, convertOperand, equals, equals, getOperand, getOperands, getOutConvention, getOutTrait, hashCode, none, operand, operand, operand, operand, operand, operandJ, operandJ, some, toString, unordered

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.apache.calcite.rel.rules.SubstitutionRule

    autoPruneOld
  • Constructor Details

    • DruidAggregateCaseToFilterRule

      public DruidAggregateCaseToFilterRule(boolean extendedFilteredSumRewrite)
  • Method Details

    • matches

      public boolean matches(org.apache.calcite.plan.RelOptRuleCall call)
      Overrides:
      matches in class org.apache.calcite.plan.RelOptRule
    • onMatch

      public void onMatch(org.apache.calcite.plan.RelOptRuleCall call)
      Specified by:
      onMatch in class org.apache.calcite.plan.RelOptRule