diff --git a/expression/builtin_arithmetic_vec.go b/expression/builtin_arithmetic_vec.go index 7bc9ea867a0c1..7981eabe6e835 100644 --- a/expression/builtin_arithmetic_vec.go +++ b/expression/builtin_arithmetic_vec.go @@ -881,7 +881,7 @@ func (b *builtinArithmeticPlusIntSig) plusUS(result *chunk.Column, lhi64s, rhi64 if rh < 0 && uint64(-rh) > uint64(lh) { return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s + %s)", b.args[0].String(), b.args[1].String())) } - if rh > 0 && uint64(lh) > math.MaxUint64-uint64(lh) { + if rh > 0 && uint64(lh) > math.MaxUint64-uint64(rh) { return types.ErrOverflow.GenWithStackByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s + %s)", b.args[0].String(), b.args[1].String())) } diff --git a/expression/integration_test.go b/expression/integration_test.go index 4a35f580e30c9..f9557b1c574cd 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -9216,6 +9216,12 @@ func (s *testIntegrationSuite) TestRefineArgNullValues(c *C) { )) } +func (s *testIntegrationSuite) TestIssue26977(c *C) { + tk := testkit.NewTestKit(c, s.store) + result := tk.MustQuery("select a + 1 as f from (select cast(0xfffffffffffffff0 as unsigned) as a union select cast(1 as unsigned)) t having f != 2;") + result.Check(testkit.Rows("18446744073709551601")) +} + func (s *testIntegrationSuite) TestIssue26958(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test;")