diff --git a/executor/aggfuncs/builder.go b/executor/aggfuncs/builder.go index 1a47a50fa1822..3035cfb059ecb 100644 --- a/executor/aggfuncs/builder.go +++ b/executor/aggfuncs/builder.go @@ -176,6 +176,9 @@ func buildFirstRow(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc { } evalType, fieldType := aggFuncDesc.RetTp.EvalType(), aggFuncDesc.RetTp + if fieldType.Tp == mysql.TypeBit { + evalType = types.ETString + } switch aggFuncDesc.Mode { case aggregation.DedupMode: default: @@ -215,6 +218,9 @@ func buildMaxMin(aggFuncDesc *aggregation.AggFuncDesc, ordinal int, isMax bool) } evalType, fieldType := aggFuncDesc.RetTp.EvalType(), aggFuncDesc.RetTp + if fieldType.Tp == mysql.TypeBit { + evalType = types.ETString + } switch aggFuncDesc.Mode { case aggregation.DedupMode: default: diff --git a/executor/aggregate_test.go b/executor/aggregate_test.go index d5973745c55d3..5dde78e136595 100644 --- a/executor/aggregate_test.go +++ b/executor/aggregate_test.go @@ -309,6 +309,17 @@ func (s *testSuite) TestAggregation(c *C) { result = tk.MustQuery("select t.id, count(95), sum(95), avg(95), bit_or(95), bit_and(95), bit_or(95), max(95), min(95), group_concat(95) from t left join s on t.id = s.id") result.Check(testkit.Rows("1 1 95 95.0000 95 95 95 95 95 95")) tk.MustExec("set @@tidb_hash_join_concurrency=5") + + // test agg bit col + tk.MustExec("drop table t") + tk.MustExec("CREATE TABLE `t` (`a` bit(1) NOT NULL, PRIMARY KEY (`a`))") + tk.MustExec("insert into t value(1), (0)") + tk.MustQuery("select a from t group by 1") + // This result is compatible with MySQL, the readable result is shown in the next case. + result = tk.MustQuery("select max(a) from t group by a") + result.Check(testkit.Rows(string([]byte{0x0}), string([]byte{0x1}))) + result = tk.MustQuery("select cast(a as signed) as idx, cast(max(a) as signed), cast(min(a) as signed) from t group by 1 order by idx") + result.Check(testkit.Rows("0 0 0", "1 1 1")) } func (s *testSuite) TestStreamAggPushDown(c *C) {