gustavodemorais commented on code in PR #27733:
URL: https://github.com/apache/flink/pull/27733#discussion_r2913942139


##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/rules/logical/SimplifyCoalesceWithEquiJoinConditionRuleTest.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * 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.table.api.TableConfig;
+import org.apache.flink.table.planner.utils.StreamTableTestUtil;
+import org.apache.flink.table.planner.utils.TableTestBase;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/** Test rule {@link SimplifyCoalesceWithEquiJoinConditionRule}. */
+class SimplifyCoalesceWithEquiJoinConditionRuleTest extends TableTestBase {
+
+    private StreamTableTestUtil util;
+
+    @BeforeEach
+    void before() {
+        util = streamTestUtil(TableConfig.getDefault());
+
+        util.tableEnv()
+                .executeSql(
+                        "CREATE TABLE orders ("
+                                + "  order_id BIGINT NOT NULL,"
+                                + "  user_id BIGINT NOT NULL,"
+                                + "  amount DOUBLE,"
+                                + "  PRIMARY KEY (order_id) NOT ENFORCED"
+                                + ") WITH ('connector' = 'values')");
+
+        util.tableEnv()
+                .executeSql(
+                        "CREATE TABLE order_details ("
+                                + "  order_id BIGINT NOT NULL,"
+                                + "  detail STRING,"
+                                + "  PRIMARY KEY (order_id) NOT ENFORCED"
+                                + ") WITH ('connector' = 'values')");
+
+        util.tableEnv()
+                .executeSql(
+                        "CREATE TABLE composite_key_table ("
+                                + "  k1 BIGINT NOT NULL,"
+                                + "  k2 BIGINT NOT NULL,"
+                                + "  val STRING,"
+                                + "  PRIMARY KEY (k1, k2) NOT ENFORCED"
+                                + ") WITH ('connector' = 'values')");
+
+        util.tableEnv()
+                .executeSql(
+                        "CREATE TABLE composite_key_details ("
+                                + "  k1 BIGINT NOT NULL,"
+                                + "  k2 BIGINT NOT NULL,"
+                                + "  info STRING,"
+                                + "  PRIMARY KEY (k1, k2) NOT ENFORCED"
+                                + ") WITH ('connector' = 'values')");
+    }
+
+    @Test
+    void testCoalesceOnLeftJoinEquiKey() {
+        util.verifyRelPlan(
+                "SELECT COALESCE(b.order_id, a.order_id) AS order_id, a.amount 
"
+                        + "FROM orders a LEFT JOIN order_details b ON 
a.order_id = b.order_id");
+    }
+
+    @Test
+    void testCoalesceReversedArgsOnLeftJoin() {
+        util.verifyRelPlan(
+                "SELECT COALESCE(a.order_id, b.order_id) AS order_id, a.amount 
"
+                        + "FROM orders a LEFT JOIN order_details b ON 
a.order_id = b.order_id");
+    }
+
+    @Test
+    void testCoalesceOnInnerJoinEquiKey() {
+        util.verifyRelPlan(
+                "SELECT COALESCE(b.order_id, a.order_id) AS order_id "
+                        + "FROM orders a INNER JOIN order_details b ON 
a.order_id = b.order_id");
+    }
+
+    @Test
+    void testCoalesceOnRightJoinEquiKey() {
+        util.verifyRelPlan(
+                "SELECT COALESCE(a.order_id, b.order_id) AS order_id "
+                        + "FROM orders a RIGHT JOIN order_details b ON 
a.order_id = b.order_id");
+    }
+
+    @Test
+    void testCoalesceOnFullJoinNotSimplified() {
+        util.verifyRelPlan(
+                "SELECT COALESCE(b.order_id, a.order_id) AS order_id "
+                        + "FROM orders a FULL JOIN order_details b ON 
a.order_id = b.order_id");
+    }
+
+    @Test
+    void testCoalesceOnNonEquiColumnsNotSimplified() {
+        util.verifyRelPlan(
+                "SELECT COALESCE(b.detail, CAST(a.amount AS STRING)) AS val "
+                        + "FROM orders a LEFT JOIN order_details b ON 
a.order_id = b.order_id");
+    }
+
+    @Test
+    void testCoalesceWithThreeArgs() {
+        util.verifyRelPlan(
+                "SELECT COALESCE(b.order_id, a.order_id, 0) AS order_id "
+                        + "FROM orders a LEFT JOIN order_details b ON 
a.order_id = b.order_id");

Review Comment:
   Hey @snuyanzin, this is a good catch. This is more of a new optimization 
that wasn't part of the scope here, since it's not related to joins or columns 
being equal. (also hope users do not often have NULLs in their coalesce calls)
   
   That said, I agree we can remove NULLs from coalesce but the best place 
would be the other rule we have `RemoveUnreachableCoalesceArgumentsRule`. I 
actually drafted the code locally already but think it makes sense to do it in 
another PR since it's not related. Wdyt?



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