From 1708905bff1dd422347bfbbf6efc0d3347266bc9 Mon Sep 17 00:00:00 2001 From: Zhuhe Fang Date: Thu, 16 Apr 2020 22:27:16 +0800 Subject: [PATCH] cherry pick #16444 to release-2.1 Signed-off-by: sre-bot --- planner/core/logical_plan_test.go | 69 ++++++++++++++++++++++++ planner/core/rule_predicate_push_down.go | 4 ++ 2 files changed, 73 insertions(+) diff --git a/planner/core/logical_plan_test.go b/planner/core/logical_plan_test.go index f136ca14d876a..b2b9b80081093 100644 --- a/planner/core/logical_plan_test.go +++ b/planner/core/logical_plan_test.go @@ -2215,3 +2215,72 @@ func (s *testPlanSuite) TestFastPlanContextTables(c *C) { } } } +<<<<<<< HEAD +======= + +func (s *testPlanSuite) TestUpdateEQCond(c *C) { + defer testleak.AfterTest(c)() + tests := []struct { + sql string + best string + }{ + { + sql: "select t1.a from t t1, t t2 where t1.a = t2.a+1", + best: "Join{DataScan(t1)->DataScan(t2)->Projection}(test.t.a,Column#25)->Projection", + }, + } + ctx := context.TODO() + for i, tt := range tests { + comment := Commentf("case:%v sql:%s", i, tt.sql) + stmt, err := s.ParseOneStmt(tt.sql, "", "") + c.Assert(err, IsNil, comment) + Preprocess(s.ctx, stmt, s.is) + builder := NewPlanBuilder(MockContext(), s.is, &hint.BlockHintProcessor{}) + p, err := builder.Build(ctx, stmt) + c.Assert(err, IsNil) + p, err = logicalOptimize(ctx, builder.optFlag, p.(LogicalPlan)) + c.Assert(err, IsNil) + c.Assert(ToString(p), Equals, tt.best, comment) + } +} + +func (s *testPlanSuite) TestConflictedJoinTypeHints(c *C) { + defer testleak.AfterTest(c)() + sql := "select /*+ INL_JOIN(t1) HASH_JOIN(t1) */ * from t t1, t t2 where t1.e = t2.e" + ctx := context.TODO() + stmt, err := s.ParseOneStmt(sql, "", "") + c.Assert(err, IsNil) + Preprocess(s.ctx, stmt, s.is) + builder := NewPlanBuilder(MockContext(), s.is, &hint.BlockHintProcessor{}) + p, err := builder.Build(ctx, stmt) + c.Assert(err, IsNil) + p, err = logicalOptimize(ctx, builder.optFlag, p.(LogicalPlan)) + c.Assert(err, IsNil) + proj, ok := p.(*LogicalProjection) + c.Assert(ok, IsTrue) + join, ok := proj.Children()[0].(*LogicalJoin) + c.Assert(ok, IsTrue) + c.Assert(join.hintInfo, IsNil) + c.Assert(join.preferJoinType, Equals, uint(0)) +} + +func (s *testPlanSuite) TestSimplyOuterJoinWithOnlyOuterExpr(c *C) { + defer testleak.AfterTest(c)() + sql := "select * from t t1 right join t t0 ON TRUE where CONCAT_WS(t0.e=t0.e, 0, NULL) IS NULL" + ctx := context.TODO() + stmt, err := s.ParseOneStmt(sql, "", "") + c.Assert(err, IsNil) + Preprocess(s.ctx, stmt, s.is) + builder := NewPlanBuilder(MockContext(), s.is, &hint.BlockHintProcessor{}) + p, err := builder.Build(ctx, stmt) + c.Assert(err, IsNil) + p, err = logicalOptimize(ctx, builder.optFlag, p.(LogicalPlan)) + c.Assert(err, IsNil) + proj, ok := p.(*LogicalProjection) + c.Assert(ok, IsTrue) + join, ok := proj.Children()[0].(*LogicalJoin) + c.Assert(ok, IsTrue) + // previous wrong JoinType is InnerJoin + c.Assert(join.JoinType, Equals, RightOuterJoin) +} +>>>>>>> e3b635e... planner: cannot simply outer join if a predicate just refers to the outer table (#16444) diff --git a/planner/core/rule_predicate_push_down.go b/planner/core/rule_predicate_push_down.go index 1bd34b3425804..1fddcc2875b66 100644 --- a/planner/core/rule_predicate_push_down.go +++ b/planner/core/rule_predicate_push_down.go @@ -297,6 +297,10 @@ func simplifyOuterJoin(p *LogicalJoin, predicates []expression.Expression) { // then simplify embedding outer join. canBeSimplified := false for _, expr := range predicates { + // avoid the case where the expr only refers to the schema of outerTable + if expression.ExprFromSchema(expr, outerTable.Schema()) { + continue + } isOk := isNullRejected(p.ctx, innerTable.Schema(), expr) if isOk { canBeSimplified = true