Repository: pingcap/tidb (Go)

Issue:
the result of hash partition table for `not between x and x` is incorrect 
## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)
```sql
create table t1(a int) partition by hash (a) partitions 5;
insert into t1 values (0);
select * from t1 where a not between 1 and 2;
```
<!-- a step by step guide for reproducing the bug. -->

### 2. What did you expect to see? (Required)
```sql
mysql test> select * from t1 where a not between 1 and 2;
+---+
| a |
+---+
| 0 |
+---+

```

### 3. What did you see instead (Required)
```sql
mysql test> select * from t1 where a not between 1 and 2;
+---+
| a |
+---+

0 rows in set
Time: 0.007s
mysql test> desc select * from t1 where a not between 1 and 2;
+-------------+---------+------+---------------+---------------+
| id          | estRows | task | access object | operator info |
+-------------+---------+------+---------------+---------------+
| TableDual_7 | 0.00    | root |               | rows:0        |
+-------------+---------+------+---------------+---------------+

```
### 4. What is your TiDB version? (Required)

<!-- Paste the output of SELECT tidb_version() -->
```sql
mysql root@127.0.0.1:test> select tidb_version()\G
***************************[ 1. row ]***************************
tidb_version() | Release Version: v4.0.0-beta.2-2106-g9e3c0649e
Edition: Community
Git Commit Hash: 9e3c0649e43db48816e005e76c1c9d6bdd5a80ad
Git Branch: HEAD
UTC Build Time: 2021-02-03 19:56:00
GoVersion: go1.13.4
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
```



Discussion:
## 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/util/ranger/points.go b/util/ranger/points.go
index 26359213f5c22..cf2356af9273d 100644
--- a/util/ranger/points.go
+++ b/util/ranger/points.go
@@ -638,6 +638,8 @@ func (r *builder) buildFromNot(expr *expression.ScalarFunction) []*point {
 		startPoint := &point{value: types.MinNotNullDatum(), start: true}
 		endPoint := &point{value: types.MaxValueDatum()}
 		return []*point{startPoint, endPoint}
+	case ast.LogicAnd:
+		return r.intersection(r.build(expr.GetArgs()[0]), r.build(expr.GetArgs()[1]))
 	}
 	return nil
 }


Test patch:
diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go
index 9f03a3cf405b5..983dc9cac3130 100644
--- a/planner/core/integration_test.go
+++ b/planner/core/integration_test.go
@@ -2626,6 +2626,15 @@ func (s *testIntegrationSuite) TestIssue22199(c *C) {
 	tk.MustGetErrMsg("select t1.*, (select t2.* from t1) from t1", "[planner:1051]Unknown table 't2'")
 }
 
+func (s *testIntegrationSuite) TestIssue22892(c *C) {
+	tk := testkit.NewTestKit(c, s.store)
+	tk.MustExec("use test")
+	tk.MustExec("drop table if exists t1")
+	tk.MustExec("create table t1(a int) partition by hash (a) partitions 5;")
+	tk.MustExec("insert into t1 values (0);")
+	tk.MustQuery("select * from t1 where a not between 1 and 2;").Check(testkit.Rows("0"))
+}
+
 func (s *testIntegrationSerialSuite) TestPushDownProjectionForTiFlash(c *C) {
 	tk := testkit.NewTestKit(c, s.store)
 	tk.MustExec("use test")