From 536e17e8d0419d410d33aff486030d07b9100a06 Mon Sep 17 00:00:00 2001 From: HuaiyuXu <391585975@qq.com> Date: Thu, 29 Nov 2018 14:15:23 +0800 Subject: [PATCH] executor: do not build range for NullOuterVal in IndexLookUpJoin (#8505) --- executor/index_lookup_join.go | 7 ++++++- executor/join_test.go | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/executor/index_lookup_join.go b/executor/index_lookup_join.go index ff8b2d1f239e3..5c7bc303f84ac 100644 --- a/executor/index_lookup_join.go +++ b/executor/index_lookup_join.go @@ -472,7 +472,12 @@ func (iw *innerWorker) constructDatumLookupKey(task *lookUpJoinTask, rowIdx int) dLookupKey := make([]types.Datum, 0, keyLen) for i, keyCol := range iw.outerCtx.keyCols { outerValue := outerRow.GetDatum(keyCol, iw.outerCtx.rowTypes[keyCol]) - + // Join-on-condition can be promised to be equal-condition in + // IndexNestedLoopJoin, thus the filter will always be false if + // outerValue is null, and we don't need to lookup it. + if outerValue.IsNull() { + return nil, nil + } innerColType := iw.rowTypes[iw.keyCols[i]] innerValue, err := outerValue.ConvertTo(sc, innerColType) if err != nil { diff --git a/executor/join_test.go b/executor/join_test.go index 2a740d7255f20..a6a60807d0290 100644 --- a/executor/join_test.go +++ b/executor/join_test.go @@ -866,6 +866,23 @@ func (s *testSuite) TestIndexLookupJoin(c *C) { `1 5 1 1`, `1 6 1 2`, )) + + tk.MustExec(`drop table if exists t1, t2, t3;`) + tk.MustExec("create table t1(a int primary key, b int)") + tk.MustExec("insert into t1 values(1, 0), (2, null)") + tk.MustExec("create table t2(a int primary key)") + tk.MustExec("insert into t2 values(0)") + tk.MustQuery("select /*+ TIDB_INLJ(t2)*/ * from t1 left join t2 on t1.b = t2.a;").Check(testkit.Rows( + `1 0 0`, + `2 `, + )) + + tk.MustExec("create table t3(a int, key(a))") + tk.MustExec("insert into t3 values(0)") + tk.MustQuery("select /*+ TIDB_INLJ(t3)*/ * from t1 left join t3 on t1.b = t3.a;").Check(testkit.Rows( + `1 0 0`, + `2 `, + )) } func (s *testSuite) TestMergejoinOrder(c *C) {