Skip to content

Commit b7e32bc

Browse files
authored
planner: fix the wrong result caused by year_col cmp out-of-range-uint (#53395)
close #50235
1 parent 68219b9 commit b7e32bc

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
@@ -1218,6 +1218,15 @@ func TestRepeatPushDownToTiFlash(t *testing.T) {
12181218
tk.MustQuery("explain select repeat(a,b) from t;").CheckAt([]int{0, 2, 4}, rows)
12191219
}
12201220

1221+
func TestIssue50235(t *testing.T) {
1222+
store := testkit.CreateMockStore(t)
1223+
tk := testkit.NewTestKit(t, store)
1224+
tk.MustExec(`use test`)
1225+
tk.MustExec(`create table tt (c year(4) NOT NULL DEFAULT '2016', primary key(c));`)
1226+
tk.MustExec(`insert into tt values (2016);`)
1227+
tk.MustQuery(`select * from tt where c < 16212511333665770580`).Check(testkit.Rows("2016"))
1228+
}
1229+
12211230
func TestIssue36194(t *testing.T) {
12221231
store, dom := testkit.CreateMockStoreAndDomain(t)
12231232
tk := testkit.NewTestKit(t, store)

pkg/util/ranger/points.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,12 @@ func (r *builder) buildFromBinOp(
330330
}
331331
// If nulleq with null value, values.ToInt64 will return err
332332
if col.GetType().GetType() == mysql.TypeYear && !value.IsNull() {
333+
// Convert the out-of-range uint number to int and then let the following logic can handle it correctly.
334+
// Since the max value of year is 2155, `col op MaxUint` should have the same result with `col op MaxInt`.
335+
if value.Kind() == types.KindUint64 && value.GetUint64() > math.MaxInt64 {
336+
value.SetInt64(math.MaxInt64)
337+
}
338+
333339
// If the original value is adjusted, we need to change the condition.
334340
// For example, col < 2156. Since the max year is 2155, 2156 is changed to 2155.
335341
// col < 2155 is wrong. It should be col <= 2155.

0 commit comments

Comments
 (0)