Repository: pingcap/tidb (Go)

Issue:
range columns partition table does not support the binary data type
## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)
```sql
CREATE TABLE PK_RCP9281 (COL1 BINARY,COL2 VARCHAR(20),COL4 DATETIME,COL3 BIGINT,COL5 FLOAT,PRIMARY KEY(COL1)) PARTITION BY RANGE COLUMNS (COL1) (
    PARTITION P0 VALUES LESS THAN (X'0C'),
    PARTITION P1 VALUES LESS THAN (X'33'),
    PARTITION P2 VALUES LESS THAN (X'6D'), 
    PARTITION P3 VALUES LESS THAN (X'95'), 
    PARTITION P4 VALUES LESS THAN (X'EB')
);
```
<!-- a step by step guide for reproducing the bug. -->

### 2. What did you expect to see? (Required)
```sql
mysql > CREATE TABLE PK_RCP9281 (COL1 BINARY,COL2 VARCHAR(20),COL4 DATETIME,COL3 BIGINT,COL5 FLOAT,PRIMARY KEY(COL1)) PARTITION BY RANGE COLUMNS (COL1) ( PARTITION P0 VALUES LESS THAN (X'0C'), PARTITION P1 VALUES LESS THAN (X'33'), PARTITION P2 VALUES LESS THAN (X'6D'), PARTITION P3 VALUES LESS THAN (X'95'), PARTITION P4 VALUES LESS THAN (X'EB'));
Query OK, 0 rows affected
Time: 0.185s
```
### 3. What did you see instead (Required)
```sql
mysql > CREATE TABLE PK_RCP9281 (COL1 BINARY,COL2 VARCHAR(20),COL4 DATETIME,COL3 BIGINT,COL5 FLOAT,PRIMARY KEY(COL1)) PARTITION BY RANGE COLUMNS (COL1) (
   PARTITION P0 VALUES LESS THAN (X'0C'),
   PARTITION P1 VALUES LESS THAN (X'33'),
  PARTITION P2 VALUES LESS THAN (X'6D'), 
  PARTITION P3 VALUES LESS THAN (X'95'), 
  PARTITION P4 VALUES LESS THAN (X'EB')
);
(1654, 'Partition column values of incorrect type')
```
### 4. What is your TiDB version? (Required)
master: fd706ab76bd09ac859
release-4.0: f0550862604
release-5.0: 03f11d24c716b

<!-- Paste the output of SELECT tidb_version() -->




Discussion:
/assign
## Please edit this comment or add a new comment to complete the following information

### Not a bug

1. Remove the 'type/bug' label
2. Add notes to indicate why it is not a bug

### Duplicate bug

1. Add the 'type/duplicate' label
2. Add the link to the original bug

### Bug

Note: Make Sure that 'component', and 'severity' labels are added
Example for how to fill out the template: https://github.com/pingcap/tidb/issues/20100

#### 1. Root Cause Analysis (RCA) (optional) 
<!-- Write down the reason why this bug occurs -->

#### 2. Symptom (optional)

<!-- What will the user see when this bug occurs. The error message may be in the terminal, log or monitoring -->

#### 3. All Trigger Conditions (optional)

<!-- All the user scenarios that may trigger this bug -->

#### 4. Workaround (optional)

#### 5. Affected versions

<!--
In the format of [start_version:end_version], multiple version ranges are
accepted. If the bug only affects the unreleased version, please input:
"unreleased". For example:

Notes:
  1. Do not use any white spaces in '[]'.
  2. The range in '[]' is a closed interval
  3. The version format is `v$Major.$Minor.$Patch`, the $Majoy and $Minor
     number in a version range should be the same. [v3.0.1:v3.1.2] is
     invalid because the $Minor number of the version range is different.

Example 1: [v3.0.1:v3.0.5], [v4.0.1:v4.0.5]
Example 2: unreleased
-->

#### 6. Fixed versions

<!--
The first released version that contains this fix in each minor version. If the bug's affected version has been released, the fixed version should be a detailed version number; If the bug doesn't affect any released version, the fixed version can be "master";  

Example 1: v3.0.13, v4.0.5
Example 2: master
-->



Patch:
diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go
index 67b51f7a3750e..31de57a6e572e 100644
--- a/ddl/ddl_api.go
+++ b/ddl/ddl_api.go
@@ -5479,7 +5479,7 @@ func checkColumnsTypeAndValuesMatch(ctx sessionctx.Context, meta *model.TableInf
 			}
 		case mysql.TypeString, mysql.TypeVarString:
 			switch vkind {
-			case types.KindString, types.KindBytes, types.KindNull:
+			case types.KindString, types.KindBytes, types.KindNull, types.KindBinaryLiteral:
 			default:
 				return ErrWrongTypeColumnValue.GenWithStackByArgs()
 			}


Test patch:
diff --git a/ddl/db_partition_test.go b/ddl/db_partition_test.go
index 6481fd9d8fe11..59bd721536d59 100644
--- a/ddl/db_partition_test.go
+++ b/ddl/db_partition_test.go
@@ -583,6 +583,12 @@ create table log_message_1 (
 
 	tk.MustExec("drop table if exists t;")
 	tk.MustExec(`create table t(a int) partition by range (a) (partition p0 values less than (18446744073709551615));`)
+
+	tk.MustExec("drop table if exists t;")
+	tk.MustExec(`create table t(a binary) partition by range columns (a) (partition p0 values less than (X'0C'));`)
+	tk.MustExec(`alter table t add partition (partition p1 values less than (X'0D'), partition p2 values less than (X'0E'));`)
+	tk.MustExec(`insert into t values (X'0B'), (X'0C'), (X'0D')`)
+	tk.MustQuery(`select * from t where a < X'0D'`).Check(testkit.Rows("\x0B", "\x0C"))
 }
 
 func (s *testIntegrationSuite1) TestDisableTablePartition(c *C) {
@@ -743,6 +749,7 @@ func (s *testIntegrationSuite1) TestCreateTableWithListPartition(c *C) {
 		"create table t (a bigint) partition by list (a) (partition p0 values in (to_seconds('2020-09-28 17:03:38'),to_seconds('2020-09-28 17:03:39')));",
 		"create table t (a datetime) partition by list (to_seconds(a)) (partition p0 values in (to_seconds('2020-09-28 17:03:38'),to_seconds('2020-09-28 17:03:39')));",
 		"create table t (a int, b int generated always as (a+1) virtual) partition by list (b + 1) (partition p0 values in (1));",
+		"create table t(a binary) partition by list columns (a) (partition p0 values in (X'0C'));",
 		s.generatePartitionTableByNum(ddl.PartitionCountLimit),
 	}
 
@@ -3334,7 +3341,7 @@ func (s *testIntegrationSuite7) TestAddPartitionForTableWithWrongType(c *C) {
 
 	_, err = tk.Exec("alter table t_char add partition (partition p1 values less than (0x20))")
 	c.Assert(err, NotNil)
-	c.Assert(ddl.ErrWrongTypeColumnValue.Equal(err), IsTrue)
+	c.Assert(ddl.ErrRangeNotIncreasing.Equal(err), IsTrue)
 
 	_, err = tk.Exec("alter table t_char add partition (partition p1 values less than (10))")
 	c.Assert(err, NotNil)