Skip to content

Commit bd3f520

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

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
@@ -1842,9 +1842,15 @@ func (e *ShowExec) fetchShowBuiltins() error {
18421842
// Because view's underlying table's column could change or recreate, so view's column type may change over time.
18431843
// To avoid this situation we need to generate a logical plan and extract current column types from Schema.
18441844
func tryFillViewColumnType(ctx context.Context, sctx sessionctx.Context, is infoschema.InfoSchema, dbName model.CIStr, tbl *model.TableInfo) error {
1845-
if tbl.IsView() {
1845+
if !tbl.IsView() {
1846+
return nil
1847+
}
1848+
// We need to run the build plan process in another session because there may be
1849+
// multiple goroutines running at the same time while session is not goroutine-safe.
1850+
// Take joining system table as an example, `fetchBuildSideRows` and `fetchProbeSideChunks` can be run concurrently.
1851+
return runWithSystemSession(sctx, func(s sessionctx.Context) error {
18461852
// Retrieve view columns info.
1847-
planBuilder, _ := plannercore.NewPlanBuilder().Init(sctx, is, &hint.BlockHintProcessor{})
1853+
planBuilder, _ := plannercore.NewPlanBuilder().Init(s, is, &hint.BlockHintProcessor{})
18481854
if viewLogicalPlan, err := planBuilder.BuildDataSourceFromView(ctx, dbName, tbl); err == nil {
18491855
viewSchema := viewLogicalPlan.Schema()
18501856
viewOutputNames := viewLogicalPlan.OutputNames()
@@ -1860,6 +1866,23 @@ func tryFillViewColumnType(ctx context.Context, sctx sessionctx.Context, is info
18601866
} else {
18611867
return err
18621868
}
1869+
return nil
1870+
})
1871+
}
1872+
1873+
func runWithSystemSession(sctx sessionctx.Context, fn func(sessionctx.Context) error) error {
1874+
b := &baseExecutor{ctx: sctx}
1875+
sysCtx, err := b.getSysSession()
1876+
if err != nil {
1877+
return err
18631878
}
1864-
return nil
1879+
// TODO(tangenta): remove the CurrentDB assignment after
1880+
// https://github.com/pingcap/tidb/issues/34090 is fixed.
1881+
originDB := sysCtx.GetSessionVars().CurrentDB
1882+
sysCtx.GetSessionVars().CurrentDB = sctx.GetSessionVars().CurrentDB
1883+
defer func() {
1884+
sysCtx.GetSessionVars().CurrentDB = originDB
1885+
}()
1886+
defer b.releaseSysSession(sysCtx)
1887+
return fn(sysCtx)
18651888
}

0 commit comments

Comments
 (0)