Repository: pingcap/tidb
File: pkg/privilege/privileges/cache.go (Go)

Code before:
	})
	for k, priv := range tablePrivTable {
		g := tablePrivToString(priv)
		if len(g) > 0 {
			var s string
			if (priv & mysql.GrantPriv) > 0 {
				s = fmt.Sprintf(`GRANT %s ON %s TO '%s'@'%s' WITH GRANT OPTION`, g, k, user, host)
			} else {
				s = fmt.Sprintf(`GRANT %s ON %s TO '%s'@'%s'`, g, k, user, host)
			}
			gs = append(gs, s)
		} else if len(g) == 0 && (priv&mysql.GrantPriv) > 0 {
			// We have GRANT OPTION on the table, but no privilege granted.
			// Wo we need to print a special USAGE line.
			s := fmt.Sprintf(`GRANT USAGE ON %s TO '%s'@'%s' WITH GRANT OPTION`, k, user, host)
			gs = append(gs, s)
		}
	}
	slices.Sort(gs[sortFromIdx:])

	// Show column scope grants, column and table are combined.
	// A map of "DB.Table" => Priv(col1, col2 ...)
	sortFromIdx = len(gs)
	columnPrivTable := make(map[string]privOnColumns)
	p.columnsPriv.Ascend(func(itm itemColumnsPriv) bool {
		logutil.BgLogger().Debug("show column privilege record in cache", zap.String("user", itm.username), zap.Int("len", len(itm.data)))
		for _, record := range itm.data {
			logutil.BgLogger().Debug("show column privilege record in cache",
				zap.String("user", record.User),
				zap.String("user addr", fmt.Sprintf("%p", unsafe.StringData(record.User))),
				zap.String("host", record.Host),
				zap.String("host addr", fmt.Sprintf("%p", unsafe.StringData(record.Host))),
				zap.String("db", record.DB),
				zap.String("db addr", fmt.Sprintf("%p", unsafe.StringData(record.DB))),
				zap.String("table_name", record.TableName),
				zap.String("table_name addr", fmt.Sprintf("%p", unsafe.StringData(record.TableName))),
				zap.String("column_name", record.ColumnName),
				zap.String("column_name addr", fmt.Sprintf("%p", unsafe.StringData(record.ColumnName))),
				zap.String("privileges", record.ColumnPriv.String()),
				zap.Time("timestamp", record.Timestamp),
				zap.Int("len(allRoles)", len(allRoles)),
			)
			if !collectColumnGrant(&record, user, host, columnPrivTable, sqlMode) {
				for _, r := range allRoles {
					collectColumnGrant(&record, r.Username, r.Hostname, columnPrivTable, sqlMode)
				}
			}
		}
		return true
	})
	for k, v := range columnPrivTable {

Review comment (bug):
```suggestion
		logutil.BgLogger().Debug("show column privilege record in btree node", zap.String("user", itm.username), zap.Int("len", len(itm.data)))
```

Code after:
				}
			}
		}
		return true
	})
	for k, priv := range tablePrivTable {
		g := tablePrivToString(priv)
		if len(g) > 0 {
			var s string
			if (priv & mysql.GrantPriv) > 0 {
				s = fmt.Sprintf(`GRANT %s ON %s TO '%s'@'%s' WITH GRANT OPTION`, g, k, user, host)
			} else {
				s = fmt.Sprintf(`GRANT %s ON %s TO '%s'@'%s'`, g, k, user, host)
			}
			gs = append(gs, s)
		} else if len(g) == 0 && (priv&mysql.GrantPriv) > 0 {
			// We have GRANT OPTION on the table, but no privilege granted.
			// Wo we need to print a special USAGE line.
			s := fmt.Sprintf(`GRANT USAGE ON %s TO '%s'@'%s' WITH GRANT OPTION`, k, user, host)
			gs = append(gs, s)
		}
	}
	slices.Sort(gs[sortFromIdx:])

	// Show column scope grants, column and table are combined.
	// A map of "DB.Table" => Priv(col1, col2 ...)
	sortFromIdx = len(gs)
	columnPrivTable := make(map[string]privOnColumns)
	p.columnsPriv.Ascend(func(itm itemColumnsPriv) bool {
		logutil.BgLogger().Debug("show column privilege record in btree node", zap.String("user", itm.username), zap.Int("len", len(itm.data)))
		for _, record := range itm.data {
			if log.GetLevel() <= zapcore.DebugLevel {
				logutil.BgLogger().Debug("column privilege record in cache",
					zap.String("user", record.User),
					zap.String("user addr", fmt.Sprintf("%p", unsafe.StringData(record.User))),
					zap.String("host", record.Host),
					zap.String("host addr", fmt.Sprintf("%p", unsafe.StringData(record.Host))),
					zap.String("db", record.DB),
					zap.String("db addr", fmt.Sprintf("%p", unsafe.StringData(record.DB))),
					zap.String("table_name", record.TableName),
					zap.String("table_name addr", fmt.Sprintf("%p", unsafe.StringData(record.TableName))),
					zap.String("column_name", record.ColumnName),
					zap.String("column_name addr", fmt.Sprintf("%p", unsafe.StringData(record.ColumnName))),
					zap.String("privileges", record.ColumnPriv.String()),
					zap.Time("timestamp", record.Timestamp),
					zap.Int("len(allRoles)", len(allRoles)),
				)
			}
			if !collectColumnGrant(&record, user, host, columnPrivTable, sqlMode) {
				for _, r := range allRoles {
					collectColumnGrant(&record, r.Username, r.Hostname, columnPrivTable, sqlMode)