snuyanzin commented on code in PR #29285: URL: https://github.com/apache/flink/pull/29285#discussion_r4106752059
########## flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/plan/batch/sql/join/NestedLoopJoinCostEstimationTest.scala: ########## @@ -0,0 +1,112 @@ +/* + * 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.batch.sql.join + +import org.apache.flink.api.common.typeinfo.{BasicTypeInfo, PrimitiveArrayTypeInfo, TypeInformation} +import org.apache.flink.table.api.config.ExecutionConfigOptions +import org.apache.flink.table.plan.stats.TableStats +import org.apache.flink.table.planner.plan.stats.FlinkStatistic +import org.apache.flink.table.planner.utils.TableTestBase + +import org.assertj.core.api.Assertions.{assertThat, assertThatCode} +import org.junit.jupiter.api.Test + +/** + * For an equi-join, nested-loop join must never be picked over an equi-capable strategy, even when + * the cost-based cardinality estimate makes it look cheap. + */ +class NestedLoopJoinCostEstimationTest extends TableTestBase { + + private val util = batchTestUtil() + + private val intInfo: TypeInformation[_] = BasicTypeInfo.INT_TYPE_INFO + + @Test + def testEquiJoinWithSkewedCardinalityDoesNotUseNestedLoopJoin(): Unit = { + // Left side's estimated row count is tiny relative to the right side. + // NestedLoopJoin's L*R cost (~2M) looks cheaper than HashJoin's 8*(L+R) cost (~8M), + // even though the join condition is a plain equality. + util.addTableSource( + "TinySide", + Array[TypeInformation[_]](intInfo, intInfo), + Array("a", "b"), + FlinkStatistic.builder().tableStats(new TableStats(2L)).build()) + util.addTableSource( + "HugeSide", + Array[TypeInformation[_]](intInfo, intInfo), + Array("d", "e"), + FlinkStatistic.builder().tableStats(new TableStats(1000000L)).build()) + + val plan = util.tableEnv.explainSql("SELECT a, d FROM TinySide, HugeSide WHERE a = d") + + assertThat(plan).doesNotContain("NestedLoopJoin") + } + + @Test + def testEquiJoinOnComplexKeyStillProducesValidPlan(): Unit = { + // An equi-join on a complex (ARRAY) key still yields a valid plan via an + // equi-capable strategy rather than being stranded. + val arrayInfo: TypeInformation[_] = PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO + util.addTableSource("ArrLeft", Array[TypeInformation[_]](intInfo, arrayInfo), Array("a", "r")) + util.addTableSource("ArrRight", Array[TypeInformation[_]](intInfo, arrayInfo), Array("d", "s")) + + val query = "SELECT a, d FROM ArrLeft, ArrRight WHERE r = s" + assertThatCode(() => util.tableEnv.explainSql(query)).doesNotThrowAnyException() + assertThat(util.tableEnv.explainSql(query)).doesNotContain("NestedLoopJoin") Review Comment: even more why do we need to check for contains/not contains instead of checking full plan? -- 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]
