Skip to content

SHOW STATS_TOPN and SHOW STATS_BUCKETS does not display the actual values correctly for [VAR]CHAR types #66307

@mjonss

Description

@mjonss

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

create table t
(b int,
 c varchar(255) charset utf8mb4 collate utf8mb4_0900_ai_ci);
insert into t values (1,1),(1,1);
analyze table t;
select * from t where c = '9' or b = 9; -- to load stats
-- These shows '>' instead of '1'
show stats_topn where table_name = 't';
show stats_buckets where table_name = 't';
-- For reference:
select topn.* from mysql.stats_top_n topn, information_schema.tables tbls where topn.table_id = tbls.tidb_table_id and tbls.table_name = 't';
select buckets.* from mysql.stats_buckets buckets, information_schema.tables tbls where buckets.table_id = tbls.tidb_table_id and tbls.table_name = 't';

2. What did you expect to see? (Required)

The varchar value of column 'c' to be '1', not '>'

3. What did you see instead (Required)

tidb> create table t
    -> (b int,
    ->  c varchar(255) charset utf8mb4 collate utf8mb4_0900_ai_ci);
Query OK, 0 rows affected (0.031 sec)

tidb> insert into t values (1,1),(1,1);
Query OK, 2 rows affected (0.009 sec)
Records: 2  Duplicates: 0  Warnings: 0

tidb> analyze table t;
Query OK, 0 rows affected, 1 warning (0.020 sec)

tidb> select * from t where c = '9' or b = 9; -- to load stats
Empty set (0.002 sec)

tidb> -- These shows '>' instead of '1'
Query OK, 0 rows affected (0.000 sec)

tidb> show stats_topn where table_name = 't';
+---------+------------+----------------+-------------+----------+-------+-------+
| Db_name | Table_name | Partition_name | Column_name | Is_index | Value | Count |
+---------+------------+----------------+-------------+----------+-------+-------+
| test    | t          |                | b           |        0 | 1     |     2 |
| test    | t          |                | c           |        0 | >    |     2 |
+---------+------------+----------------+-------------+----------+-------+-------+
2 rows in set (0.006 sec)

tidb> show stats_buckets where table_name = 't';
Empty set (0.007 sec)

tidb> -- For reference:
Query OK, 0 rows affected (0.000 sec)

tidb> select topn.* from mysql.stats_top_n topn, information_schema.tables tbls where topn.table_id = tbls.tidb_table_id and tbls.table_name = 't';
+----------+----------+---------+------------------------+-------+
| table_id | is_index | hist_id | value                  | count |
+----------+----------+---------+------------------------+-------+
|      112 |        0 |       1 | 0x038000000000000001   |     2 |
|      112 |        0 |       2 | 0x011C3E000000000000F9 |     2 |
+----------+----------+---------+------------------------+-------+
2 rows in set (0.001 sec)

4. What is your TiDB version? (Required)

tidb_version(): Release Version: v8.5.5
Edition: Community
Git Commit Hash: 1fa258b833ff113883beeba40bc130be7ce66610
Git Branch: HEAD
UTC Build Time: 2026-01-14 22:19:19
GoVersion: go1.25.5
Race Enabled: false
Check Table Before Drop: false
Store: tikv

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions