Skip to content

Commit f162dcc

Browse files
authored
planner: fix the wrong result caused by year_col cmp out-of-range-uint (#53395) (#53411)
close #50235
1 parent 00a460e commit f162dcc

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

pkg/planner/core/integration_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,15 @@ func TestRepeatPushDownToTiFlash(t *testing.T) {
15411541
tk.MustQuery("explain select repeat(a,b) from t;").CheckAt([]int{0, 2, 4}, rows)
15421542
}
15431543

1544+
func TestIssue50235(t *testing.T) {
1545+
store := testkit.CreateMockStore(t)
1546+
tk := testkit.NewTestKit(t, store)
1547+
tk.MustExec(`use test`)
1548+
tk.MustExec(`create table tt (c year(4) NOT NULL DEFAULT '2016', primary key(c));`)
1549+
tk.MustExec(`insert into tt values (2016);`)
1550+
tk.MustQuery(`select * from tt where c < 16212511333665770580`).Check(testkit.Rows("2016"))
1551+
}
1552+
15441553
func TestIssue36194(t *testing.T) {
15451554
store, dom := testkit.CreateMockStoreAndDomain(t)
15461555
tk := testkit.NewTestKit(t, store)

pkg/util/ranger/points.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,12 @@ func (r *builder) buildFromBinOp(
315315
}
316316
// If nulleq with null value, values.ToInt64 will return err
317317
if col.GetType().GetType() == mysql.TypeYear && !value.IsNull() {
318+
// Convert the out-of-range uint number to int and then let the following logic can handle it correctly.
319+
// Since the max value of year is 2155, `col op MaxUint` should have the same result with `col op MaxInt`.
320+
if value.Kind() == types.KindUint64 && value.GetUint64() > math.MaxInt64 {
321+
value.SetInt64(math.MaxInt64)
322+
}
323+
318324
// If the original value is adjusted, we need to change the condition.
319325
// For example, col < 2156. Since the max year is 2155, 2156 is changed to 2155.
320326
// col < 2155 is wrong. It should be col <= 2155.

0 commit comments

Comments
 (0)