Airblader commented on a change in pull request #17256:
URL: https://github.com/apache/flink/pull/17256#discussion_r712721877



##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/RemoveUnreachableCoalesceArgumentsRule.java
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.flink.table.planner.plan.rules.logical;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.planner.plan.utils.FlinkRexUtil;
+
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Calc;
+import org.apache.calcite.rel.core.Filter;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexShuttle;
+
+import java.util.List;
+import java.util.function.Predicate;
+
+/**
+ * Removes unreachable {@link BuiltInFunctionDefinitions#COALESCE} arguments.
+ *
+ * <p>An unreachable COALESCE argument is defined as any argument after the 
first argument in the
+ * argument list with a non-null type.
+ */
+@Internal
+public class RemoveUnreachableCoalesceArgumentsRule
+        extends RelRule<RemoveUnreachableCoalesceArgumentsRule.Config> {
+
+    public static final RelOptRule PROJECT_INSTANCE =
+            Config.EMPTY.as(Config.class).withProject().toRule();
+    public static final RelOptRule FILTER_INSTANCE =
+            Config.EMPTY.as(Config.class).withFilter().toRule();
+    public static final RelOptRule JOIN_INSTANCE =
+            Config.EMPTY.as(Config.class).withJoin().toRule();
+    public static final RelOptRule CALC_INSTANCE =
+            Config.EMPTY.as(Config.class).withCalc().toRule();
+
+    private static final UnreachableCoalesceArgumentsRemoveRexShuttle 
REX_SHUTTLE_INSTANCE =
+            new UnreachableCoalesceArgumentsRemoveRexShuttle();
+
+    public RemoveUnreachableCoalesceArgumentsRule(Config config) {
+        super(config);
+    }
+
+    @Override
+    public void onMatch(RelOptRuleCall call) {
+        final RelNode relNode = call.rel(0);
+        call.transformTo(relNode.accept(REX_SHUTTLE_INSTANCE));
+    }
+
+    private static class UnreachableCoalesceArgumentsRemoveRexShuttle extends 
RexShuttle {
+
+        @Override
+        public RexNode visitCall(RexCall call) {
+            call = (RexCall) super.visitCall(call);
+
+            // Not a coalesce invocation, skip it
+            if (!call.getOperator()
+                    .getName()
+                    .equals(BuiltInFunctionDefinitions.COALESCE.getName())) {
+                return call;
+            }
+
+            int firstNonNullableArgIndex = 
getFirstNonNullableArgumentIndex(call);

Review comment:
       nit: final ;-) 

##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/RemoveUnreachableCoalesceArgumentsRule.java
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.flink.table.planner.plan.rules.logical;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.planner.plan.utils.FlinkRexUtil;
+
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Calc;
+import org.apache.calcite.rel.core.Filter;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexShuttle;
+
+import java.util.List;
+import java.util.function.Predicate;
+
+/**
+ * Removes unreachable {@link BuiltInFunctionDefinitions#COALESCE} arguments.
+ *
+ * <p>An unreachable COALESCE argument is defined as any argument after the 
first argument in the
+ * argument list with a non-null type.
+ */
+@Internal
+public class RemoveUnreachableCoalesceArgumentsRule
+        extends RelRule<RemoveUnreachableCoalesceArgumentsRule.Config> {
+
+    public static final RelOptRule PROJECT_INSTANCE =
+            Config.EMPTY.as(Config.class).withProject().toRule();
+    public static final RelOptRule FILTER_INSTANCE =
+            Config.EMPTY.as(Config.class).withFilter().toRule();
+    public static final RelOptRule JOIN_INSTANCE =
+            Config.EMPTY.as(Config.class).withJoin().toRule();
+    public static final RelOptRule CALC_INSTANCE =
+            Config.EMPTY.as(Config.class).withCalc().toRule();
+
+    private static final UnreachableCoalesceArgumentsRemoveRexShuttle 
REX_SHUTTLE_INSTANCE =
+            new UnreachableCoalesceArgumentsRemoveRexShuttle();
+
+    public RemoveUnreachableCoalesceArgumentsRule(Config config) {
+        super(config);
+    }
+
+    @Override
+    public void onMatch(RelOptRuleCall call) {
+        final RelNode relNode = call.rel(0);
+        call.transformTo(relNode.accept(REX_SHUTTLE_INSTANCE));
+    }
+
+    private static class UnreachableCoalesceArgumentsRemoveRexShuttle extends 
RexShuttle {
+
+        @Override
+        public RexNode visitCall(RexCall call) {
+            call = (RexCall) super.visitCall(call);
+
+            // Not a coalesce invocation, skip it
+            if (!call.getOperator()
+                    .getName()
+                    .equals(BuiltInFunctionDefinitions.COALESCE.getName())) {
+                return call;
+            }
+
+            int firstNonNullableArgIndex = 
getFirstNonNullableArgumentIndex(call);
+
+            // If it's the first argument, just return the argument without 
the coalesce invocation
+            if (firstNonNullableArgIndex == 0) {
+                return call.operands.get(0);
+            }
+
+            // If it's the last argument, or no non-null argument was found, 
return the original
+            // call
+            if (firstNonNullableArgIndex == call.operands.size() - 1
+                    || firstNonNullableArgIndex == -1) {
+                return call;
+            }
+
+            // Return the coalesce invocation with a trimmed argument list
+            final List<RexNode> trimmedOperandsList =
+                    call.operands.subList(0, firstNonNullableArgIndex + 1);
+            return call.clone(call.getType(), trimmedOperandsList);
+        }
+
+        private int getFirstNonNullableArgumentIndex(RexCall call) {
+            for (int argIndex = 0; argIndex < call.operands.size(); 
argIndex++) {
+                if (!call.operands.get(argIndex).getType().isNullable()) {
+                    return argIndex;
+                }
+            }
+            return -1;
+        }
+    }
+
+    private static boolean hasCoalesceInvocation(RexNode node) {
+        return FlinkRexUtil.hasOperatorCallMatching(
+                node, op -> 
op.getName().equals(BuiltInFunctionDefinitions.COALESCE.getName()));

Review comment:
       It'd actually be nice not to have to match on the name. I think the 
operator here will be a `BridgingSqlFunction`, and then we could actually look 
into it and compare on the actual function definition (those implement 
`equals`).




-- 
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]


Reply via email to