kgyrtkirk commented on code in PR #17378: URL: https://github.com/apache/druid/pull/17378#discussion_r1866989866
########## sql/src/main/java/org/apache/druid/sql/calcite/rule/DruidAggregateCaseToFilterRule.java: ########## @@ -0,0 +1,317 @@ +/* + * 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.rex.RexOver; +import org.apache.calcite.rex.RexShuttle; +import org.apache.calcite.rex.RexWindow; +import org.apache.calcite.rex.RexWindowBound; +import org.apache.calcite.sql.SqlAggFunction; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.sql.SqlOperator; +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 version of {@link AggregateCaseToFilterRule} + * after enhancing the upstream one to support cases like + * SUM(CASE WHEN COND THEN COL1 ELSE 0 END) + * without introducing an inner case this should be removed. + */ +public class DruidAggregateCaseToFilterRule extends RelOptRule implements SubstitutionRule Review Comment: yes - it kinda does that I was exploring to do it differently - one way is to do a more complicated rewrite: did made it to be able to rewrite it back to a `CASE COUNT() = 0 THEN NULL ELSE COALESCE(SUM() FILTER (F), 0) END` ; not sure about that path - it really makes it more complicated... I'm a bit skeptical about its long term pros/cons; we can have it - I have a version about it here: https://github.com/kgyrtkirk/calcite/pull/7/files If there would be more such rewrites it might possibly worth it - but right now I don't thin there will be more the interesting cases for a `SUM(CASE WHEN filter THEN valueCol ELSE 0 END)` are: | | original expression | filtered-SUM | filtered-SUM0 | | --- | - | - | - | | empty table | null | null | 0 | | filtered row | 0 | null | 0 | | selected; valueCol is null | null | null | 0 | | selected; valueCol is 1 | 1 | 1 | 1 | I wonder if we could possibly tweak the filtering to return zero in case there are filtered rows - or restore the null-handling bug -- 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]
