Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: fix wrong schema when to solve the schema name in CTE (#57253) #57330

Open
wants to merge 1 commit into
base: release-8.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
16 changes: 16 additions & 0 deletions pkg/planner/core/tests/cte/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
],
)
54 changes: 54 additions & 0 deletions pkg/planner/core/tests/cte/cte_test.go
Original file line number Diff line number Diff line change
@@ -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"))
}
34 changes: 34 additions & 0 deletions pkg/planner/core/tests/cte/main_test.go
Original file line number Diff line number Diff line change
@@ -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...)
}