Skip to content

Commit 55030ed

Browse files
tangentati-srebot
tangenta
authored andcommitted
cherry pick pingcap#33946 to release-6.0
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
1 parent 36a9810 commit 55030ed

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

executor/infoschema_reader_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -631,3 +631,61 @@ func TestTablesPKType(t *testing.T) {
631631
tk.MustQuery("SELECT TIDB_PK_TYPE FROM information_schema.tables where table_schema = 'test' and table_name = 't_common'").Check(testkit.Rows("CLUSTERED"))
632632
tk.MustQuery("SELECT TIDB_PK_TYPE FROM information_schema.tables where table_schema = 'INFORMATION_SCHEMA' and table_name = 'TABLES'").Check(testkit.Rows("NONCLUSTERED"))
633633
}
634+
635+
// https://github.com/pingcap/tidb/issues/32459.
636+
func TestJoinSystemTableContainsView(t *testing.T) {
637+
store, clean := testkit.CreateMockStore(t)
638+
defer clean()
639+
tk := testkit.NewTestKit(t, store)
640+
tk.MustExec("use test")
641+
tk.MustExec("create table t (a timestamp, b int);")
642+
tk.MustExec("insert into t values (null, 100);")
643+
tk.MustExec("create view v as select * from t;")
644+
// This is used by grafana when TiDB is specified as the data source.
645+
// See https://github.com/grafana/grafana/blob/e86b6662a187c77656f72bef3b0022bf5ced8b98/public/app/plugins/datasource/mysql/meta_query.ts#L31
646+
for i := 0; i < 10; i++ {
647+
tk.MustQuery(`
648+
SELECT
649+
table_name as table_name,
650+
( SELECT
651+
column_name as column_name
652+
FROM information_schema.columns c
653+
WHERE
654+
c.table_schema = t.table_schema AND
655+
c.table_name = t.table_name AND
656+
c.data_type IN ('timestamp', 'datetime')
657+
ORDER BY ordinal_position LIMIT 1
658+
) AS time_column,
659+
( SELECT
660+
column_name AS column_name
661+
FROM information_schema.columns c
662+
WHERE
663+
c.table_schema = t.table_schema AND
664+
c.table_name = t.table_name AND
665+
c.data_type IN('float', 'int', 'bigint')
666+
ORDER BY ordinal_position LIMIT 1
667+
) AS value_column
668+
FROM information_schema.tables t
669+
WHERE
670+
t.table_schema = database() AND
671+
EXISTS
672+
( SELECT 1
673+
FROM information_schema.columns c
674+
WHERE
675+
c.table_schema = t.table_schema AND
676+
c.table_name = t.table_name AND
677+
c.data_type IN ('timestamp', 'datetime')
678+
) AND
679+
EXISTS
680+
( SELECT 1
681+
FROM information_schema.columns c
682+
WHERE
683+
c.table_schema = t.table_schema AND
684+
c.table_name = t.table_name AND
685+
c.data_type IN('float', 'int', 'bigint')
686+
)
687+
LIMIT 1
688+
;
689+
`).Check(testkit.Rows("t a b"))
690+
}
691+
}

executor/show.go

+26-3
Original file line numberDiff line numberDiff line change
@@ -1873,9 +1873,15 @@ func (e *ShowExec) fetchShowBuiltins() error {
18731873
// Because view's underlying table's column could change or recreate, so view's column type may change over time.
18741874
// To avoid this situation we need to generate a logical plan and extract current column types from Schema.
18751875
func tryFillViewColumnType(ctx context.Context, sctx sessionctx.Context, is infoschema.InfoSchema, dbName model.CIStr, tbl *model.TableInfo) error {
1876-
if tbl.IsView() {
1876+
if !tbl.IsView() {
1877+
return nil
1878+
}
1879+
// We need to run the build plan process in another session because there may be
1880+
// multiple goroutines running at the same time while session is not goroutine-safe.
1881+
// Take joining system table as an example, `fetchBuildSideRows` and `fetchProbeSideChunks` can be run concurrently.
1882+
return runWithSystemSession(sctx, func(s sessionctx.Context) error {
18771883
// Retrieve view columns info.
1878-
planBuilder, _ := plannercore.NewPlanBuilder().Init(sctx, is, &hint.BlockHintProcessor{})
1884+
planBuilder, _ := plannercore.NewPlanBuilder().Init(s, is, &hint.BlockHintProcessor{})
18791885
if viewLogicalPlan, err := planBuilder.BuildDataSourceFromView(ctx, dbName, tbl); err == nil {
18801886
viewSchema := viewLogicalPlan.Schema()
18811887
viewOutputNames := viewLogicalPlan.OutputNames()
@@ -1891,6 +1897,23 @@ func tryFillViewColumnType(ctx context.Context, sctx sessionctx.Context, is info
18911897
} else {
18921898
return err
18931899
}
1900+
return nil
1901+
})
1902+
}
1903+
1904+
func runWithSystemSession(sctx sessionctx.Context, fn func(sessionctx.Context) error) error {
1905+
b := &baseExecutor{ctx: sctx}
1906+
sysCtx, err := b.getSysSession()
1907+
if err != nil {
1908+
return err
18941909
}
1895-
return nil
1910+
// TODO(tangenta): remove the CurrentDB assignment after
1911+
// https://github.com/pingcap/tidb/issues/34090 is fixed.
1912+
originDB := sysCtx.GetSessionVars().CurrentDB
1913+
sysCtx.GetSessionVars().CurrentDB = sctx.GetSessionVars().CurrentDB
1914+
defer func() {
1915+
sysCtx.GetSessionVars().CurrentDB = originDB
1916+
}()
1917+
defer b.releaseSysSession(sysCtx)
1918+
return fn(sysCtx)
18961919
}

0 commit comments

Comments
 (0)