Skip to content

Commit

Permalink
planner: fix wrong schema when to solve the schema name in CTE (#57253)…
Browse files Browse the repository at this point in the history
… (#57333)

close #54582
  • Loading branch information
ti-chi-bot authored Nov 13, 2024
1 parent 3b61650 commit 5af33d9
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
4 changes: 3 additions & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7598,7 +7598,9 @@ func (b *PlanBuilder) adjustCTEPlanOutputName(p LogicalPlan, def *ast.CommonTabl
outPutNames := p.OutputNames()
for _, name := range outPutNames {
name.TblName = def.Name
name.DBName = model.NewCIStr(b.ctx.GetSessionVars().CurrentDB)
if name.DBName.String() == "" {
name.DBName = model.NewCIStr(b.ctx.GetSessionVars().CurrentDB)
}
}
if len(def.ColNameList) > 0 {
if len(def.ColNameList) != len(p.OutputNames()) {
Expand Down
16 changes: 16 additions & 0 deletions 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 = [
"//testkit",
"//testkit/testsetup",
"@org_uber_go_goleak//:goleak",
],
)
54 changes: 54 additions & 0 deletions 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/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 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/testkit/testsetup"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
testsetup.SetupForCommonTest()
flag.Parse()
opts := []goleak.Option{
goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).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...)
}

0 comments on commit 5af33d9

Please sign in to comment.