|
27 | 27 | import org.apache.calcite.rel.core.Aggregate; |
28 | 28 | import org.apache.calcite.rel.core.AggregateCall; |
29 | 29 | import org.apache.calcite.rel.metadata.RelMetadataQuery; |
30 | | -import org.apache.calcite.sql.SqlKind; |
31 | | -import org.apache.calcite.sql.type.SqlTypeName; |
32 | 30 | import org.apache.calcite.util.BitSets; |
33 | 31 | import org.apache.calcite.util.ImmutableBitSet; |
34 | 32 | import org.apache.drill.common.expression.ExpressionPosition; |
@@ -82,24 +80,13 @@ public LogicalOperator implement(DrillImplementor implementor) { |
82 | 80 |
|
83 | 81 | @Override |
84 | 82 | public RelOptCost computeSelfCost(RelOptPlanner planner, RelMetadataQuery mq) { |
85 | | - for (AggregateCall aggCall : getAggCallList()) { |
86 | | - String name = aggCall.getAggregation().getName(); |
87 | | - // For avg, stddev_pop, stddev_samp, var_pop and var_samp, the ReduceAggregatesRule is supposed |
88 | | - // to convert them to use sum and count. Here, we make the cost of the original functions high |
89 | | - // enough such that the planner does not choose them and instead chooses the rewritten functions. |
90 | | - // Except when AVG, STDDEV_POP, STDDEV_SAMP, VAR_POP and VAR_SAMP are used with DECIMAL type. |
91 | | - // For Calcite 1.35+ compatibility: Also allow ANY type since Drill's type system may infer ANY |
92 | | - // during the logical planning phase before types are fully resolved |
93 | | - if ((name.equals(SqlKind.AVG.name()) |
94 | | - || name.equals(SqlKind.STDDEV_POP.name()) |
95 | | - || name.equals(SqlKind.STDDEV_SAMP.name()) |
96 | | - || name.equals(SqlKind.VAR_POP.name()) |
97 | | - || name.equals(SqlKind.VAR_SAMP.name())) |
98 | | - && aggCall.getType().getSqlTypeName() != SqlTypeName.DECIMAL |
99 | | - && aggCall.getType().getSqlTypeName() != SqlTypeName.ANY) { |
100 | | - return planner.getCostFactory().makeHugeCost(); |
101 | | - } |
102 | | - } |
| 83 | + // For Calcite 1.35+ compatibility: The ReduceAggregatesRule behavior has changed. |
| 84 | + // In earlier versions, AVG/STDDEV/VAR were always rewritten to SUM/COUNT. |
| 85 | + // In Calcite 1.35+, these functions are kept as-is in many cases. |
| 86 | + // We no longer penalize these functions with huge cost, allowing the planner |
| 87 | + // to use them directly when appropriate. |
| 88 | + // The rewriting still happens when beneficial via DrillReduceAggregatesRule, |
| 89 | + // but it's no longer mandatory through cost-based forcing. |
103 | 90 |
|
104 | 91 | return computeLogicalAggCost(planner, mq); |
105 | 92 | } |
|
0 commit comments