From a93ce4ff6b0e4fd9db6627252f6cf49eea965112 Mon Sep 17 00:00:00 2001 From: AndrewDi Date: Wed, 28 Nov 2018 13:35:14 +0800 Subject: [PATCH] planner, executor: fix tp.Flen size when union with castIntAsString --- executor/executor_test.go | 6 ++++++ planner/core/logical_plan_builder.go | 3 +++ 2 files changed, 9 insertions(+) diff --git a/executor/executor_test.go b/executor/executor_test.go index 13d85079cb858..e46bea5980c52 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -1065,6 +1065,12 @@ func (s *testSuite) TestUnion(c *C) { for i := 0; i < 4; i++ { tk.MustQuery("SELECT(SELECT 0 AS a FROM dual UNION SELECT 1 AS a FROM dual ORDER BY a ASC LIMIT 1) AS dev").Check(testkit.Rows("0")) } + + // #issue 8231 + tk.MustExec("drop table if exists t1") + tk.MustExec("CREATE TABLE t1 (uid int(1))") + tk.MustExec("INSERT INTO t1 SELECT 150") + tk.MustQuery("SELECT 'a' UNION SELECT uid FROM t1 order by 1 desc;").Check(testkit.Rows("a", "150")) } func (s *testSuite) TestNeighbouringProj(c *C) { diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 2eec9c13b4838..13b01612bbef4 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -640,6 +640,9 @@ func unionJoinFieldType(a, b *types.FieldType) *types.FieldType { resultTp.Decimal = mathutil.Max(a.Decimal, b.Decimal) // `Flen - Decimal` is the fraction before '.' resultTp.Flen = mathutil.Max(a.Flen-a.Decimal, b.Flen-b.Decimal) + resultTp.Decimal + if resultTp.EvalType() != types.ETInt && (a.EvalType() == types.ETInt || b.EvalType() == types.ETInt) && resultTp.Flen < mysql.MaxIntWidth { + resultTp.Flen = mysql.MaxIntWidth + } resultTp.Charset = a.Charset resultTp.Collate = a.Collate expression.SetBinFlagOrBinStr(b, resultTp)