gianm commented on code in PR #17378:
URL: https://github.com/apache/druid/pull/17378#discussion_r1870376818


##########
sql/src/main/java/org/apache/druid/sql/calcite/rule/DruidAggregateCaseToFilterRule.java:
##########
@@ -0,0 +1,342 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.sql.calcite.rule;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.rules.AggregateCaseToFilterRule;
+import org.apache.calcite.rel.rules.SubstitutionRule;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlPostfixOperator;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.tools.RelBuilder;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Druid extension of {@link AggregateCaseToFilterRule}.
+ *
+ * Turning on extendedFilteredSumRewrite enables rewrites of:
+ *    SUM(CASE WHEN COND THEN COL1 ELSE 0 END)
+ * to
+ *    SUM(COL1) FILTER (COND)
+ *
+ * +-----------------+--------------+----------+------+--------------+
+ * | 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            |
+ * +-----------------+--------------+----------+------+--------------+
+ *
+ * The only inconsistency this causes is when then condition matches all rows; 
and for all o

Review Comment:
   Incomplete javadoc?



##########
processing/src/main/java/org/apache/druid/query/QueryContexts.java:
##########
@@ -88,6 +88,8 @@ public class QueryContexts
   public static final String UNCOVERED_INTERVALS_LIMIT_KEY = 
"uncoveredIntervalsLimit";
   public static final String MIN_TOP_N_THRESHOLD = "minTopNThreshold";
   public static final String CATALOG_VALIDATION_ENABLED = 
"catalogValidationEnabled";
+  public static final String EXTENDED_FILTERED_SUM_REWRITE_ENABLED = 
"extendedFilteredSumRewrite";

Review Comment:
   I see the new context parameter in this PR is undocumented and defaults to 
`true` (the old behavior, more performant, yet incorrect in the edge cases). 
IMO it's OK to have it be undocumented, but we should have a javadoc here 
explaining what is going on. Can you please add one and link it both to the 
rule (using `@link`) and also says something like: this is here for testing 
purposes, it is a known issue that the production configuration is incorrect in 
certain edge cases, and this context parameter can be removed once we have a 
way to do a rewrite of these case statements that is both high-performance and 
correct.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to