bvarghese1 commented on code in PR #30038:
URL: https://github.com/apache/flink/pull/30038#discussion_r4645697920


##########
flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/plan/stream/sql/join/SemiAntiJoinTest.scala:
##########
@@ -573,4 +574,34 @@ class SemiAntiJoinTest extends TableTestBase {
       " AND NOT EXISTS (SELECT * FROM r WHERE l.b <> r.e)"
     util.verifyExecPlan(sqlQuery)
   }
+
+  @Test
+  def testNullAwareNotInStaysAboveJoin(): Unit = {
+    // A null-aware NOT IN over nullable columns becomes an anti join on
+    // (a = b) OR ((a = b) IS NULL), which has no equi-join keys.
+    SemiAntiJoinTestStats.addTables(util)
+    val sqlQuery =
+      """
+        |SELECT p_brand, ps_suppkey FROM partsupp, part
+        |WHERE p_partkey = ps_partkey AND p_brand <> 'Brand#45'
+        |AND ps_suppkey NOT IN (
+        |    SELECT s_suppkey FROM supplier WHERE s_comment LIKE 
'%Complaints%')
+        |""".stripMargin
+    util.verifyRelPlan(sqlQuery)
+  }
+
+  @Test
+  def testNonEquiExistsStaysAboveJoin(): Unit = {

Review Comment:
    Added `testEquiExistsPushedBelowJoin` to the stream test



##########
flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/nodes/logical/FlinkLogicalJoin.scala:
##########
@@ -66,7 +66,12 @@ class FlinkLogicalJoin(
       case JoinRelType.SEMI | JoinRelType.ANTI =>
         val rightRowSize = mq.getAverageRowSize(getRight)
         val ioCost = (leftRowCnt * leftRowSize) + (rightRowCnt * rightRowSize)
-        val cpuCost = leftRowCnt + rightRowCnt
+        // without equi-join keys, every left row is compared with every right 
row
+        val cpuCost = if (analyzeCondition().leftKeys.isEmpty) {

Review Comment:
   Agreed.
   Does the following release note sound good to you? I can add it to the JIRA 
ticket:
   ```
   The planner now estimates the CPU cost of a semi or anti join without 
equi-join keys as quadratic in its inputs, instead of linear.
   Queries with such a join may get a different join order when table 
statistics are available.
   For a streaming job, a changed plan means a savepoint taken before the 
upgrade may not restore. Use a compiled plan to keep the previous plan.
   ```



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/common/SemiAntiJoinTestStats.java:
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.common;
+
+import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.table.plan.stats.ColumnStats;
+import org.apache.flink.table.plan.stats.TableStats;
+import org.apache.flink.table.planner.plan.stats.FlinkStatistic;
+import org.apache.flink.table.planner.utils.TableTestUtil;
+
+import java.util.Map;
+
+/**
+ * Tables modeled on TPC-H q16: partsupp, part and supplier. The key columns 
carry NDVs, so the

Review Comment:
   Yes, thats right. I ran experiments with SF1000 with row counts and NDV for 
the join keys (estimated from the row count of the table each key is equated 
to). There were no stats on filter columns.
   What you saw with full column stats in SF10 is correct.
   With the L+R cost, the placement of the join depends on the filter estimate.
   With L*R cost, it stays above whenever the join reduces rows.
   Also, updated the PR description to include this info.



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