Skip to content

Commit 8500016

Browse files
tangentati-srebot
tangenta
authored andcommitted
cherry pick pingcap#33946 to release-5.4
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
1 parent 20de896 commit 8500016

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
@@ -986,3 +986,61 @@ func (s *testInfoschemaTableSuite) TestTablesPKType(c *C) {
986986
tk.MustQuery("SELECT TIDB_PK_TYPE FROM information_schema.tables where table_schema = 'test' and table_name = 't_common'").Check(testkit.Rows("CLUSTERED"))
987987
tk.MustQuery("SELECT TIDB_PK_TYPE FROM information_schema.tables where table_schema = 'INFORMATION_SCHEMA' and table_name = 'TABLES'").Check(testkit.Rows("NONCLUSTERED"))
988988
}
989+
990+
// https://github.com/pingcap/tidb/issues/32459.
991+
func TestJoinSystemTableContainsView(t *testing.T) {
992+
store, clean := testkit.CreateMockStore(t)
993+
defer clean()
994+
tk := testkit.NewTestKit(t, store)
995+
tk.MustExec("use test")
996+
tk.MustExec("create table t (a timestamp, b int);")
997+
tk.MustExec("insert into t values (null, 100);")
998+
tk.MustExec("create view v as select * from t;")
999+
// This is used by grafana when TiDB is specified as the data source.
1000+
// See https://github.com/grafana/grafana/blob/e86b6662a187c77656f72bef3b0022bf5ced8b98/public/app/plugins/datasource/mysql/meta_query.ts#L31
1001+
for i := 0; i < 10; i++ {
1002+
tk.MustQuery(`
1003+
SELECT
1004+
table_name as table_name,
1005+
( SELECT
1006+
column_name as column_name
1007+
FROM information_schema.columns c
1008+
WHERE
1009+
c.table_schema = t.table_schema AND
1010+
c.table_name = t.table_name AND
1011+
c.data_type IN ('timestamp', 'datetime')
1012+
ORDER BY ordinal_position LIMIT 1
1013+
) AS time_column,
1014+
( SELECT
1015+
column_name AS column_name
1016+
FROM information_schema.columns c
1017+
WHERE
1018+
c.table_schema = t.table_schema AND
1019+
c.table_name = t.table_name AND
1020+
c.data_type IN('float', 'int', 'bigint')
1021+
ORDER BY ordinal_position LIMIT 1
1022+
) AS value_column
1023+
FROM information_schema.tables t
1024+
WHERE
1025+
t.table_schema = database() AND
1026+
EXISTS
1027+
( SELECT 1
1028+
FROM information_schema.columns c
1029+
WHERE
1030+
c.table_schema = t.table_schema AND
1031+
c.table_name = t.table_name AND
1032+
c.data_type IN ('timestamp', 'datetime')
1033+
) AND
1034+
EXISTS
1035+
( SELECT 1
1036+
FROM information_schema.columns c
1037+
WHERE
1038+
c.table_schema = t.table_schema AND
1039+
c.table_name = t.table_name AND
1040+
c.data_type IN('float', 'int', 'bigint')
1041+
)
1042+
LIMIT 1
1043+
;
1044+
`).Check(testkit.Rows("t a b"))
1045+
}
1046+
}

executor/show.go

+26-3
Original file line numberDiff line numberDiff line change
@@ -1882,9 +1882,15 @@ func (e *ShowExec) fetchShowBuiltins() error {
18821882
// Because view's underlying table's column could change or recreate, so view's column type may change over time.
18831883
// To avoid this situation we need to generate a logical plan and extract current column types from Schema.
18841884
func tryFillViewColumnType(ctx context.Context, sctx sessionctx.Context, is infoschema.InfoSchema, dbName model.CIStr, tbl *model.TableInfo) error {
1885-
if tbl.IsView() {
1885+
if !tbl.IsView() {
1886+
return nil
1887+
}
1888+
// We need to run the build plan process in another session because there may be
1889+
// multiple goroutines running at the same time while session is not goroutine-safe.
1890+
// Take joining system table as an example, `fetchBuildSideRows` and `fetchProbeSideChunks` can be run concurrently.
1891+
return runWithSystemSession(sctx, func(s sessionctx.Context) error {
18861892
// Retrieve view columns info.
1887-
planBuilder, _ := plannercore.NewPlanBuilder().Init(sctx, is, &hint.BlockHintProcessor{})
1893+
planBuilder, _ := plannercore.NewPlanBuilder().Init(s, is, &hint.BlockHintProcessor{})
18881894
if viewLogicalPlan, err := planBuilder.BuildDataSourceFromView(ctx, dbName, tbl); err == nil {
18891895
viewSchema := viewLogicalPlan.Schema()
18901896
viewOutputNames := viewLogicalPlan.OutputNames()
@@ -1900,6 +1906,23 @@ func tryFillViewColumnType(ctx context.Context, sctx sessionctx.Context, is info
19001906
} else {
19011907
return err
19021908
}
1909+
return nil
1910+
})
1911+
}
1912+
1913+
func runWithSystemSession(sctx sessionctx.Context, fn func(sessionctx.Context) error) error {
1914+
b := &baseExecutor{ctx: sctx}
1915+
sysCtx, err := b.getSysSession()
1916+
if err != nil {
1917+
return err
19031918
}
1904-
return nil
1919+
// TODO(tangenta): remove the CurrentDB assignment after
1920+
// https://github.com/pingcap/tidb/issues/34090 is fixed.
1921+
originDB := sysCtx.GetSessionVars().CurrentDB
1922+
sysCtx.GetSessionVars().CurrentDB = sctx.GetSessionVars().CurrentDB
1923+
defer func() {
1924+
sysCtx.GetSessionVars().CurrentDB = originDB
1925+
}()
1926+
defer b.releaseSysSession(sysCtx)
1927+
return fn(sysCtx)
19051928
}

0 commit comments

Comments
 (0)