Repository: pingcap/tidb (Go)

Issue:
Format of bit default value on `INFORMATION_SCHEMA.COLUMNS` is wrong/inconsistent
## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)
```SQL
test=> create table "bug" ("bit" bit(10) DEFAULT b'100');
CREATE TABLE
test=> show create table bug;
 Table |                        Create Table                         
-------+-------------------------------------------------------------
 bug   | CREATE TABLE "bug" (                                       +
       |   "bit" bit(10) DEFAULT b'100'                             +
       | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin 
(1 row)
test=> SELECT COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'bug';
 COLUMN_DEFAULT 
----------------
 \x04 
(1 row)
```

### 2. What did you expect to see? (Required)
The following is MySQL output:
```SQL
test=> CREATE TABLE "bug" ("tBit" bit(10) DEFAULT b'100');
CREATE TABLE
test=> SELECT COLUMN_DEFAULT FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE TABLE_NAME = 'bug';
 COLUMN_DEFAULT 
----------------
 b'100' 
(1 row)
```

### 3. What did you see instead (Required)
```SQL
test=> SELECT COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'bug';
 COLUMN_DEFAULT 
----------------
 \x04 
(1 row)
```
### 4. What is your TiDB version? (Required)

```SQL
test=> SELECT tidb_version();
tidb_version()                          
-------------------------------------------------------------------
 Release Version: v5.4.0                                          
 Edition: Community                                               
 Git Commit Hash: 55f3b24c1c9f506bd652ef1d162283541e428872        
 Git Branch: heads/refs/tags/v5.4.0                               
 UTC Build Time: 2022-01-25 08:39:26                              
 GoVersion: go1.16.4                                             
 Race Enabled: false                                              
 TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
 Check Table Before Drop: false 
(1 row)
```




Discussion:
Also worth noting, is that the output of `show create table` is correct (ran on TiDB):
```
test=> show create table bug;
 Table |                        Create Table                         
-------+-------------------------------------------------------------
 bug   | CREATE TABLE "bug" (                                       +
       |   "bit" bit(10) DEFAULT b'100'                             +
       | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin 
(1 row)
```
compare to mysql 8.0.28, seems only the record in `information_schema.columns` is different, others are same.
mysql 8.0.28
```sql
mysql>  create table `bug` (`bit` bit(10) DEFAULT b'100');
Query OK, 0 rows affected (0.08 sec)

mysql> insert into bug values (DEFAULT);
Query OK, 1 row affected (0.05 sec)

mysql> select * from bug;
+------------+
| bit        |
+------------+
| 0x0004     |
+------------+
1 row in set (0.04 sec)

mysql> show create table bug;
+-------+-----------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                    |
+-------+-----------------------------------------------------------------------------------------------------------------+
| bug   | CREATE TABLE `bug` (
  `bit` bit(10) DEFAULT b'100'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |
+-------+-----------------------------------------------------------------------------------------------------------------+
1 row in set (0.06 sec)

mysql> SELECT COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'bug';
+----------------+
| COLUMN_DEFAULT |
+----------------+
| b'100'         |
+----------------+
1 row in set (0.06 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.28    |
+-----------+
1 row in set (0.05 sec)

```

tidb
```sql

mysql> create table `bug` (`bit` bit(10) DEFAULT b'100');
Query OK, 0 rows affected (0.10 sec)

mysql> insert into bug values (DEFAULT);
Query OK, 1 row affected (0.06 sec)

mysql>   select * from bug;
+------------+
| bit        |
+------------+
| 0x0004     |
+------------+
1 row in set (0.01 sec)

mysql> show create table bug;
+-------+-----------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                    |
+-------+-----------------------------------------------------------------------------------------------------------------+
| bug   | CREATE TABLE `bug` (
  `bit` bit(10) DEFAULT b'100'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin |
+-------+-----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'bug';
+----------------+
| COLUMN_DEFAULT |
+----------------+
|               |
+----------------+
1 row in set (0.06 sec)
```
/severity moderate



Patch:
diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go
index 8c68d1f8ffc9d..a3f5dde31ebe9 100644
--- a/executor/infoschema_reader.go
+++ b/executor/infoschema_reader.go
@@ -792,6 +792,11 @@ ForColumnsTag:
 		var columnDefault interface{}
 		if columnDesc.DefaultValue != nil {
 			columnDefault = fmt.Sprintf("%v", columnDesc.DefaultValue)
+			if col.Tp == mysql.TypeBit {
+				defaultStr := fmt.Sprintf("%v", columnDesc.DefaultValue)
+				defaultValBinaryLiteral := types.BinaryLiteral(defaultStr)
+				columnDefault = defaultValBinaryLiteral.ToBitLiteralString(true)
+			}
 		}
 		record := types.MakeDatums(
 			infoschema.CatalogVal,                // TABLE_CATALOG


Test patch:
diff --git a/executor/infoschema_reader_test.go b/executor/infoschema_reader_test.go
index 849fa839c4b9a..81813238832e7 100644
--- a/executor/infoschema_reader_test.go
+++ b/executor/infoschema_reader_test.go
@@ -150,6 +150,18 @@ func TestViews(t *testing.T) {
 	tk.MustQuery("SELECT table_catalog, table_schema, table_name, table_type, engine, version, row_format, table_rows, avg_row_length, data_length, max_data_length, index_length, data_free, auto_increment, update_time, check_time, table_collation, checksum, create_options, table_comment FROM information_schema.tables WHERE table_schema='test' AND table_name='v1'").Check(testkit.Rows("def test v1 VIEW <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> VIEW"))
 }
 
+func TestColumnsTables(t *testing.T) {
+	store, clean := testkit.CreateMockStore(t)
+	defer clean()
+	tk := testkit.NewTestKit(t, store)
+	tk.MustExec("use test")
+	tk.MustExec("drop table if exists t")
+	tk.MustExec("create table t (bit bit(10) DEFAULT b'100')")
+	tk.MustQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 't'").Check(testkit.Rows(
+		"def test t bit 1 b'100' YES bit <nil> <nil> 10 0 <nil> <nil> <nil> bit(10) unsigned   select,insert,update,references  "))
+	tk.MustExec("drop table if exists t")
+}
+
 func TestEngines(t *testing.T) {
 	store, clean := testkit.CreateMockStore(t)
 	defer clean()