GetPartitionTableIDs get partition tableIDs through histograms. SHOW STATS_HISTOGRAMS has db_name,table_name,partition_name but doesn't have partition id mysql.stats_histograms has partition_id but doesn't have db_name,table_name,partition_name So we combine the results from these two sqls to get p
(db *sql.Conn, tables map[string]map[string]struct{})
| 1469 | // Because TiDB v3.0.0's information_schema.partition table doesn't have partition name or partition id info |
| 1470 | // return (dbName -> tbName -> partitionName -> partitionID, error) |
| 1471 | func GetPartitionTableIDs(db *sql.Conn, tables map[string]map[string]struct{}) (map[string]map[string]map[string]int64, error) { |
| 1472 | const ( |
| 1473 | showStatsHistogramsSQL = "SHOW STATS_HISTOGRAMS" |
| 1474 | selectStatsHistogramsSQL = "SELECT TABLE_ID,FROM_UNIXTIME(VERSION DIV 262144 DIV 1000,'%Y-%m-%d %H:%i:%s') AS UPDATE_TIME,DISTINCT_COUNT FROM mysql.stats_histograms" |
| 1475 | ) |
| 1476 | partitionIDs := make(map[string]map[string]map[string]int64, len(tables)) |
| 1477 | rows, err := db.QueryContext(context.Background(), showStatsHistogramsSQL) |
| 1478 | if err != nil { |
| 1479 | return nil, errors.Annotatef(err, "sql: %s", showStatsHistogramsSQL) |
| 1480 | } |
| 1481 | results, err := GetSpecifiedColumnValuesAndClose(rows, "DB_NAME", "TABLE_NAME", "PARTITION_NAME", "UPDATE_TIME", "DISTINCT_COUNT") |
| 1482 | if err != nil { |
| 1483 | return nil, errors.Annotatef(err, "sql: %s", showStatsHistogramsSQL) |
| 1484 | } |
| 1485 | type partitionInfo struct { |
| 1486 | dbName, tbName, partitionName string |
| 1487 | } |
| 1488 | saveMap := make(map[string]map[string]partitionInfo) |
| 1489 | for _, oneRow := range results { |
| 1490 | dbName, tbName, partitionName, updateTime, distinctCount := oneRow[0], oneRow[1], oneRow[2], oneRow[3], oneRow[4] |
| 1491 | if len(partitionName) == 0 { |
| 1492 | continue |
| 1493 | } |
| 1494 | if tbm, ok := tables[dbName]; ok { |
| 1495 | if _, ok = tbm[tbName]; ok { |
| 1496 | if _, ok = saveMap[updateTime]; !ok { |
| 1497 | saveMap[updateTime] = make(map[string]partitionInfo) |
| 1498 | } |
| 1499 | saveMap[updateTime][distinctCount] = partitionInfo{ |
| 1500 | dbName: dbName, |
| 1501 | tbName: tbName, |
| 1502 | partitionName: partitionName, |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | } |
| 1507 | if len(saveMap) == 0 { |
| 1508 | return map[string]map[string]map[string]int64{}, nil |
| 1509 | } |
| 1510 | err = simpleQuery(db, selectStatsHistogramsSQL, func(rows *sql.Rows) error { |
| 1511 | var ( |
| 1512 | tableID int64 |
| 1513 | updateTime, distinctCount string |
| 1514 | ) |
| 1515 | err2 := rows.Scan(&tableID, &updateTime, &distinctCount) |
| 1516 | if err2 != nil { |
| 1517 | return errors.Trace(err2) |
| 1518 | } |
| 1519 | if mpt, ok := saveMap[updateTime]; ok { |
| 1520 | if partition, ok := mpt[distinctCount]; ok { |
| 1521 | dbName, tbName, partitionName := partition.dbName, partition.tbName, partition.partitionName |
| 1522 | if _, ok := partitionIDs[dbName]; !ok { |
| 1523 | partitionIDs[dbName] = make(map[string]map[string]int64) |
| 1524 | } |
| 1525 | if _, ok := partitionIDs[dbName][tbName]; !ok { |
| 1526 | partitionIDs[dbName][tbName] = make(map[string]int64) |
| 1527 | } |
| 1528 | partitionIDs[dbName][tbName][partitionName] = tableID |
no test coverage detected