diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index c1ad241f45913..0dea6dc846c1d 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -3873,3 +3873,26 @@ func (s *testIntegrationSuite) TestIssue27797(c *C) { result = tk.MustQuery("select col2 from IDT_HP24172 where col1 = 8388607 and col1 in (select col1 from IDT_HP24172);") result.Check(testkit.Rows("")) } + +// https://github.com/pingcap/tidb/issues/24095 +func (s *testIntegrationSuite) TestIssue24095(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test;") + tk.MustExec("drop table if exists t;") + tk.MustExec("create table t (id int, value decimal(10,5));") + tk.MustExec("desc format = 'brief' select count(*) from t join (select t.id, t.value v1 from t join t t1 on t.id = t1.id order by t.value limit 1) v on v.id = t.id and v.v1 = t.value;") + + var input []string + var output []struct { + SQL string + Plan []string + } + s.testData.GetTestCases(c, &input, &output) + for i, tt := range input { + s.testData.OnRecord(func() { + output[i].SQL = tt + output[i].Plan = s.testData.ConvertRowsToStrings(tk.MustQuery("explain format = 'brief' " + tt).Rows()) + }) + tk.MustQuery("explain format = 'brief' " + tt).Check(testkit.Rows(output[i].Plan...)) + } +} diff --git a/planner/core/rule_join_reorder.go b/planner/core/rule_join_reorder.go index 08aacdbc8683a..2aa0ced4c1f7d 100644 --- a/planner/core/rule_join_reorder.go +++ b/planner/core/rule_join_reorder.go @@ -91,10 +91,14 @@ func (s *joinReOrderSolver) optimizeRecursive(ctx sessionctx.Context, p LogicalP return nil, err } schemaChanged := false - for i, col := range p.Schema().Columns { - if !col.Equal(nil, originalSchema.Columns[i]) { - schemaChanged = true - break + if len(p.Schema().Columns) != len(originalSchema.Columns) { + schemaChanged = true + } else { + for i, col := range p.Schema().Columns { + if !col.Equal(nil, originalSchema.Columns[i]) { + schemaChanged = true + break + } } } if schemaChanged { diff --git a/planner/core/testdata/integration_suite_in.json b/planner/core/testdata/integration_suite_in.json index 1be0dd432914a..c2243bbd7e71a 100644 --- a/planner/core/testdata/integration_suite_in.json +++ b/planner/core/testdata/integration_suite_in.json @@ -300,5 +300,11 @@ "select a from ta group by @n:=@n+1", "select a from ta group by @n:=@n+a" ] + }, + { + "name": "TestIssue24095", + "cases": [ + "select count(*) from t join (select t.id, t.value v1 from t join t t1 on t.id = t1.id order by t.value limit 1) v on v.id = t.id and v.v1 = t.value;" + ] } ] diff --git a/planner/core/testdata/integration_suite_out.json b/planner/core/testdata/integration_suite_out.json index 15cf14d68e871..7890aa2965664 100644 --- a/planner/core/testdata/integration_suite_out.json +++ b/planner/core/testdata/integration_suite_out.json @@ -1614,5 +1614,29 @@ ] } ] + }, + { + "Name": "TestIssue24095", + "Cases": [ + { + "SQL": "select count(*) from t join (select t.id, t.value v1 from t join t t1 on t.id = t1.id order by t.value limit 1) v on v.id = t.id and v.v1 = t.value;", + "Plan": [ + "StreamAgg 1.00 root funcs:count(1)->Column#10", + "└─HashJoin 1.00 root inner join, equal:[eq(test.t.id, test.t.id) eq(test.t.value, test.t.value)]", + " ├─Selection(Build) 0.80 root not(isnull(test.t.id)), not(isnull(test.t.value))", + " │ └─TopN 1.00 root test.t.value, offset:0, count:1", + " │ └─HashJoin 12487.50 root inner join, equal:[eq(test.t.id, test.t.id)]", + " │ ├─TableReader(Build) 9990.00 root data:Selection", + " │ │ └─Selection 9990.00 cop[tikv] not(isnull(test.t.id))", + " │ │ └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo", + " │ └─TableReader(Probe) 9990.00 root data:Selection", + " │ └─Selection 9990.00 cop[tikv] not(isnull(test.t.id))", + " │ └─TableFullScan 10000.00 cop[tikv] table:t keep order:false, stats:pseudo", + " └─TableReader(Probe) 9980.01 root data:Selection", + " └─Selection 9980.01 cop[tikv] not(isnull(test.t.id)), not(isnull(test.t.value))", + " └─TableFullScan 10000.00 cop[tikv] table:t keep order:false, stats:pseudo" + ] + } + ] } ]