From 878e48a93b0625cf3091d5785c5a6adfb2b818d0 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Tue, 12 Nov 2024 23:51:50 +0800 Subject: [PATCH] This is an automated cherry-pick of #57253 Signed-off-by: ti-chi-bot --- pkg/planner/core/logical_plan_builder.go | 6 +++ pkg/planner/core/tests/cte/BUILD.bazel | 16 +++++++ pkg/planner/core/tests/cte/cte_test.go | 54 ++++++++++++++++++++++++ pkg/planner/core/tests/cte/main_test.go | 34 +++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 pkg/planner/core/tests/cte/BUILD.bazel create mode 100644 pkg/planner/core/tests/cte/cte_test.go create mode 100644 pkg/planner/core/tests/cte/main_test.go diff --git a/pkg/planner/core/logical_plan_builder.go b/pkg/planner/core/logical_plan_builder.go index efec0862b0b7c..f8f34cb609f9a 100644 --- a/pkg/planner/core/logical_plan_builder.go +++ b/pkg/planner/core/logical_plan_builder.go @@ -7616,7 +7616,13 @@ func (b *PlanBuilder) adjustCTEPlanOutputName(p LogicalPlan, def *ast.CommonTabl outPutNames := p.OutputNames() for _, name := range outPutNames { name.TblName = def.Name +<<<<<<< HEAD name.DBName = model.NewCIStr(b.ctx.GetSessionVars().CurrentDB) +======= + if name.DBName.String() == "" { + name.DBName = pmodel.NewCIStr(b.ctx.GetSessionVars().CurrentDB) + } +>>>>>>> 2d70425ccc9 (planner: fix wrong schema when to solve the schema name in CTE (#57253)) } if len(def.ColNameList) > 0 { if len(def.ColNameList) != len(p.OutputNames()) { diff --git a/pkg/planner/core/tests/cte/BUILD.bazel b/pkg/planner/core/tests/cte/BUILD.bazel new file mode 100644 index 0000000000000..4ca7030885b41 --- /dev/null +++ b/pkg/planner/core/tests/cte/BUILD.bazel @@ -0,0 +1,16 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_test") + +go_test( + name = "cte_test", + timeout = "short", + srcs = [ + "cte_test.go", + "main_test.go", + ], + flaky = True, + deps = [ + "//pkg/testkit", + "//pkg/testkit/testsetup", + "@org_uber_go_goleak//:goleak", + ], +) diff --git a/pkg/planner/core/tests/cte/cte_test.go b/pkg/planner/core/tests/cte/cte_test.go new file mode 100644 index 0000000000000..fa7ebb5d964af --- /dev/null +++ b/pkg/planner/core/tests/cte/cte_test.go @@ -0,0 +1,54 @@ +// Copyright 2024 PingCAP, Inc. +// +// Licensed 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 cte + +import ( + "testing" + + "github.com/pingcap/tidb/pkg/testkit" +) + +func TestCTEWithDifferentSchema(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("CREATE USER 'db_a'@'%';") + tk.MustExec("CREATE USER 'db_b'@'%';") + tk.MustExec("GRANT ALL PRIVILEGES ON `db_a`.* TO 'db_a'@'%';") + tk.MustExec("GRANT ALL PRIVILEGES ON `db_b`.* TO 'db_a'@'%';") + tk.MustExec("GRANT ALL PRIVILEGES ON `db_b`.* TO 'db_b'@'%';") + tk.MustExec("GRANT ALL PRIVILEGES ON `db_b`.* TO 'db_b'@'%';") + tk.MustExec("create database db_a;") + tk.MustExec("create database db_b;") + tk.MustExec("use db_a;") + tk.MustExec(`CREATE TABLE tmp_table1 ( + id decimal(18,0) NOT NULL, + row_1 varchar(255) DEFAULT NULL, + PRIMARY KEY (id) /*T![clustered_index] CLUSTERED */ + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin`) + tk.MustExec(`create ALGORITHM=UNDEFINED DEFINER=db_a@'%' SQL SECURITY DEFINER VIEW view_test_v1 as ( + with rs1 as( + select otn.* + from tmp_table1 otn + ) + select ojt.* from rs1 ojt + )`) + tk.MustExec("use db_b;") + tk.MustQuery("explain select * from db_a.view_test_v1;").Check(testkit.Rows( + "CTEFullScan_11 10000.00 root CTE:rs1 AS ojt data:CTE_0", + "CTE_0 10000.00 root Non-Recursive CTE", + "└─TableReader_9(Seed Part) 10000.00 root data:TableFullScan_8", + " └─TableFullScan_8 10000.00 cop[tikv] table:otn keep order:false, stats:pseudo")) +} diff --git a/pkg/planner/core/tests/cte/main_test.go b/pkg/planner/core/tests/cte/main_test.go new file mode 100644 index 0000000000000..191fa0ace9e5e --- /dev/null +++ b/pkg/planner/core/tests/cte/main_test.go @@ -0,0 +1,34 @@ +// Copyright 2024 PingCAP, Inc. +// +// Licensed 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 cte + +import ( + "flag" + "testing" + + "github.com/pingcap/tidb/pkg/testkit/testsetup" + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + testsetup.SetupForCommonTest() + flag.Parse() + opts := []goleak.Option{ + goleak.IgnoreTopFunction("github.com/golang/glog.(*fileSink).flushDaemon"), + goleak.IgnoreTopFunction("github.com/bazelbuild/rules_go/go/tools/bzltestutil.RegisterTimeoutHandler.func1"), + goleak.IgnoreTopFunction("github.com/lestrrat-go/httprc.runFetchWorker"), + } + goleak.VerifyTestMain(m, opts...) +}