Airblader commented on a change in pull request #17256: URL: https://github.com/apache/flink/pull/17256#discussion_r712031629
########## File path: flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/UnreachableCoalesceArgumentsRemoveRule.java ########## @@ -0,0 +1,197 @@ +/* + * 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; + +/** + * 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 UnreachableCoalesceArgumentsRemoveRule + extends RelRule<UnreachableCoalesceArgumentsRemoveRule.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 UnreachableCoalesceArgumentsRemoveRule(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; + } + + for (int firstNullableIndex = 0; + firstNullableIndex < call.operands.size(); + firstNullableIndex++) { + RexNode argument = call.operands.get(firstNullableIndex); Review comment: nit: `final` ########## File path: flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/UnreachableCoalesceArgumentsRemoveRule.java ########## @@ -0,0 +1,197 @@ +/* + * 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; + +/** + * 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 UnreachableCoalesceArgumentsRemoveRule + extends RelRule<UnreachableCoalesceArgumentsRemoveRule.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 UnreachableCoalesceArgumentsRemoveRule(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; + } + + for (int firstNullableIndex = 0; + firstNullableIndex < call.operands.size(); + firstNullableIndex++) { + RexNode argument = call.operands.get(firstNullableIndex); + // Check if it's non null + if (!argument.getType().isNullable()) { + // If it's the first argument, just return the argument without the coalesce + // invocation + if (firstNullableIndex == 0) { + return argument; + } + + // If it's the last argument, no change is required + if (firstNullableIndex == call.operands.size() - 1) { + break; + } + + // Return the coalesce invocation with a trimmed argument list + List<RexNode> trimmedOperandsList = Review comment: nit: `final` ########## File path: flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/UnreachableCoalesceArgumentsRemoveRule.java ########## @@ -0,0 +1,197 @@ +/* + * 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; + +/** + * 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 UnreachableCoalesceArgumentsRemoveRule + extends RelRule<UnreachableCoalesceArgumentsRemoveRule.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 UnreachableCoalesceArgumentsRemoveRule(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; + } + + for (int firstNullableIndex = 0; + firstNullableIndex < call.operands.size(); + firstNullableIndex++) { + RexNode argument = call.operands.get(firstNullableIndex); + // Check if it's non null + if (!argument.getType().isNullable()) { + // If it's the first argument, just return the argument without the coalesce + // invocation + if (firstNullableIndex == 0) { + return argument; + } + + // If it's the last argument, no change is required + if (firstNullableIndex == call.operands.size() - 1) { + break; + } + + // Return the coalesce invocation with a trimmed argument list + List<RexNode> trimmedOperandsList = + call.operands.subList(0, firstNullableIndex + 1); + return call.clone(call.getType(), trimmedOperandsList); + } + } + + // Either no arguments are non-null, or only the last one is non-null + return call; + } + } + + // --------------------------------------------------------------------------------------------- + + /** Configuration for {@link UnreachableCoalesceArgumentsRemoveRule}. */ + public interface Config extends RelRule.Config { + + @Override + default RelOptRule toRule() { + return new UnreachableCoalesceArgumentsRemoveRule(this); + } + + default Config withProject() { Review comment: For all the `with*` methods it'd be nice to extract the inner predicates to make the code more readable (see e.g. `PushFilterPastChangelogNormalizeRule`). ########## File path: flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/UnreachableCoalesceArgumentsRemoveRule.java ########## @@ -0,0 +1,197 @@ +/* + * 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; + +/** + * 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 UnreachableCoalesceArgumentsRemoveRule + extends RelRule<UnreachableCoalesceArgumentsRemoveRule.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 UnreachableCoalesceArgumentsRemoveRule(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; + } + + for (int firstNullableIndex = 0; Review comment: `firstNullableIndex` is a misnomer to me, since it will actually take on several values during the iteration. I would just name it `argumentIndex` or something like that. However, we could also extract a method which (almost) does what your variable claims to be: return the index of the first non-nullable argument: ``` final int cutOffIndex = getFirstNonNullableArgumentIndex(call); if (cutOffIndex == 0) { return argument; } else if (cutOffIndex == call.operands.size() - 1) { return call; } else { final List<RexNode> newOperands = …; return call.clone(…); } ``` ########## File path: flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/utils/FlinkRexUtil.scala ########## @@ -535,6 +534,29 @@ object FlinkRexUtil { throw new IllegalArgumentException(s"Unknown expression type '${expr.getClass}': $expr") } } + + /** + * Similar to [[RexUtil#findOperatorCall(SqlOperator, RexNode)]] but for flink's udf. + */ + def hasUdfInvocation(expr: RexNode, functionName: String): Boolean = { Review comment: Maybe pass a `BuiltInFunctionDefinition` here instead of the string? ########## File path: flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/utils/FlinkRexUtil.scala ########## @@ -535,6 +534,29 @@ object FlinkRexUtil { throw new IllegalArgumentException(s"Unknown expression type '${expr.getClass}': $expr") } } + + /** + * Similar to [[RexUtil#findOperatorCall(SqlOperator, RexNode)]] but for flink's udf. Review comment: nit: > Similar to [[RexUtil#findOperatorCall(SqlOperator, RexNode)]], but for Flink's UDFs. ########## File path: flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/UnreachableCoalesceArgumentsRemoveRule.java ########## @@ -0,0 +1,197 @@ +/* + * 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; + +/** + * 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 UnreachableCoalesceArgumentsRemoveRule Review comment: nit: maybe name it `RemoveUnreachableCoalesceArgumentsRule`? ########## File path: flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/UnreachableCoalesceArgumentsRemoveRule.java ########## @@ -0,0 +1,197 @@ +/* + * 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; + +/** + * 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 UnreachableCoalesceArgumentsRemoveRule + extends RelRule<UnreachableCoalesceArgumentsRemoveRule.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 UnreachableCoalesceArgumentsRemoveRule(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; + } + + for (int firstNullableIndex = 0; + firstNullableIndex < call.operands.size(); + firstNullableIndex++) { + RexNode argument = call.operands.get(firstNullableIndex); + // Check if it's non null Review comment: nit: this comment only restates what the code does, and the code is already very expressive, so I would remove it. -- 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]
