Skip to content

Commit

Permalink
metric: fix panic when status.record-db-label enable (#41692) (#50858)
Browse files Browse the repository at this point in the history
close #41688
  • Loading branch information
ti-chi-bot authored Apr 1, 2024
1 parent 902758d commit 6dff2a8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
37 changes: 25 additions & 12 deletions executor/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,26 +288,37 @@ func getStmtDbLabel(stmtNode ast.StmtNode) map[string]struct{} {

switch x := stmtNode.(type) {
case *ast.AlterTableStmt:
dbLabel := x.Table.Schema.O
dbLabelSet[dbLabel] = struct{}{}
if x.Table != nil {
dbLabel := x.Table.Schema.O
dbLabelSet[dbLabel] = struct{}{}
}
case *ast.CreateIndexStmt:
dbLabel := x.Table.Schema.O
dbLabelSet[dbLabel] = struct{}{}
if x.Table != nil {
dbLabel := x.Table.Schema.O
dbLabelSet[dbLabel] = struct{}{}
}
case *ast.CreateTableStmt:
dbLabel := x.Table.Schema.O
dbLabelSet[dbLabel] = struct{}{}
if x.Table != nil {
dbLabel := x.Table.Schema.O
dbLabelSet[dbLabel] = struct{}{}
}
case *ast.InsertStmt:
dbLabels := getDbFromResultNode(x.Table.TableRefs)
for _, db := range dbLabels {
dbLabelSet[db] = struct{}{}
var dbLabels []string
if x.Table != nil {
dbLabels = getDbFromResultNode(x.Table.TableRefs)
for _, db := range dbLabels {
dbLabelSet[db] = struct{}{}
}
}
dbLabels = getDbFromResultNode(x.Select)
for _, db := range dbLabels {
dbLabelSet[db] = struct{}{}
}
case *ast.DropIndexStmt:
dbLabel := x.Table.Schema.O
dbLabelSet[dbLabel] = struct{}{}
if x.Table != nil {
dbLabel := x.Table.Schema.O
dbLabelSet[dbLabel] = struct{}{}
}
case *ast.DropTableStmt:
tables := x.Tables
for _, table := range tables {
Expand Down Expand Up @@ -401,7 +412,9 @@ func getDbFromResultNode(resultNode ast.ResultSetNode) []string { // may have du
return getDbFromResultNode(x.From.TableRefs)
}
case *ast.TableName:
dbLabels = append(dbLabels, x.DBInfo.Name.O)
if x.DBInfo != nil {
dbLabels = append(dbLabels, x.DBInfo.Name.O)
}
case *ast.Join:
if x.Left != nil {
dbs := getDbFromResultNode(x.Left)
Expand Down
4 changes: 3 additions & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2188,13 +2188,15 @@ func (cli *testServerClient) runTestStmtCount(t *testing.T) {
dbt.MustExec("prepare stmt2 from 'select * from test'")
dbt.MustExec("execute stmt2")
dbt.MustExec("replace into test(a) values(6);")
// test for CTE
dbt.MustExec("WITH RECURSIVE cte (n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM cte WHERE n < 5) SELECT * FROM cte;")

currentStmtCnt := getStmtCnt(string(cli.getMetrics(t)))
require.Equal(t, originStmtCnt["CreateTable"]+1, currentStmtCnt["CreateTable"])
require.Equal(t, originStmtCnt["Insert"]+5, currentStmtCnt["Insert"])
require.Equal(t, originStmtCnt["Delete"]+1, currentStmtCnt["Delete"])
require.Equal(t, originStmtCnt["Update"]+2, currentStmtCnt["Update"])
require.Equal(t, originStmtCnt["Select"]+3, currentStmtCnt["Select"])
require.Equal(t, originStmtCnt["Select"]+4, currentStmtCnt["Select"])
require.Equal(t, originStmtCnt["Prepare"]+2, currentStmtCnt["Prepare"])
require.Equal(t, originStmtCnt["Execute"]+0, currentStmtCnt["Execute"])
require.Equal(t, originStmtCnt["Replace"]+1, currentStmtCnt["Replace"])
Expand Down

0 comments on commit 6dff2a8

Please sign in to comment.