executor, expression: fix the incorrect result of AVG function (#23285)
This commit is contained in:
@ -1373,3 +1373,34 @@ func (s *testSerialSuite) TestRandomPanicAggConsume(c *C) {
|
||||
c.Assert(err.Error(), Equals, "failpoint panic: ERROR 1105 (HY000): Out Of Memory Quota![conn_id=1]")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *testSuiteAgg) TestIssue23277(c *C) {
|
||||
tk := testkit.NewTestKitWithInit(c, s.store)
|
||||
tk.MustExec("use test;")
|
||||
tk.MustExec("drop table if exists t;")
|
||||
|
||||
tk.MustExec("create table t(a tinyint(1));")
|
||||
tk.MustExec("insert into t values (-120), (127);")
|
||||
tk.MustQuery("select avg(a) from t group by a").Sort().Check(testkit.Rows("-120.0000", "127.0000"))
|
||||
tk.MustExec("drop table t;")
|
||||
|
||||
tk.MustExec("create table t(a smallint(1));")
|
||||
tk.MustExec("insert into t values (-120), (127);")
|
||||
tk.MustQuery("select avg(a) from t group by a").Sort().Check(testkit.Rows("-120.0000", "127.0000"))
|
||||
tk.MustExec("drop table t;")
|
||||
|
||||
tk.MustExec("create table t(a mediumint(1));")
|
||||
tk.MustExec("insert into t values (-120), (127);")
|
||||
tk.MustQuery("select avg(a) from t group by a").Sort().Check(testkit.Rows("-120.0000", "127.0000"))
|
||||
tk.MustExec("drop table t;")
|
||||
|
||||
tk.MustExec("create table t(a int(1));")
|
||||
tk.MustExec("insert into t values (-120), (127);")
|
||||
tk.MustQuery("select avg(a) from t group by a").Sort().Check(testkit.Rows("-120.0000", "127.0000"))
|
||||
tk.MustExec("drop table t;")
|
||||
|
||||
tk.MustExec("create table t(a bigint(1));")
|
||||
tk.MustExec("insert into t values (-120), (127);")
|
||||
tk.MustQuery("select avg(a) from t group by a").Sort().Check(testkit.Rows("-120.0000", "127.0000"))
|
||||
tk.MustExec("drop table t;")
|
||||
}
|
||||
|
||||
@ -208,7 +208,12 @@ func (a *baseFuncDesc) typeInfer4Sum(ctx sessionctx.Context) {
|
||||
// Because child returns integer or decimal type.
|
||||
func (a *baseFuncDesc) typeInfer4Avg(ctx sessionctx.Context) {
|
||||
switch a.Args[0].GetType().Tp {
|
||||
case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong, mysql.TypeYear, mysql.TypeNewDecimal:
|
||||
case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
|
||||
a.RetTp = types.NewFieldType(mysql.TypeNewDecimal)
|
||||
a.RetTp.Decimal = types.DivFracIncr
|
||||
flen, _ := mysql.GetDefaultFieldLengthAndDecimal(a.Args[0].GetType().Tp)
|
||||
a.RetTp.Flen = flen + types.DivFracIncr
|
||||
case mysql.TypeYear, mysql.TypeNewDecimal:
|
||||
a.RetTp = types.NewFieldType(mysql.TypeNewDecimal)
|
||||
if a.Args[0].GetType().Decimal < 0 {
|
||||
a.RetTp.Decimal = mysql.MaxDecimalScale
|
||||
|
||||
@ -2339,7 +2339,7 @@
|
||||
"Group#2 Schema:[Column#13,Column#14,Column#15,test.t.b,Column#16,Column#17,Column#18,Column#19,Column#20,Column#14,Column#13]",
|
||||
" Selection_4 input:[Group#3], ge(Column#13, 0), ge(Column#14, 0)",
|
||||
"Group#3 Schema:[Column#13,Column#14,Column#15,test.t.b,Column#16,Column#17,Column#18,Column#19,Column#20,Column#14,Column#13]",
|
||||
" Projection_8 input:[Group#4], 1->Column#13, cast(test.t.b, decimal(65,0) BINARY)->Column#14, cast(test.t.b, decimal(65,30) BINARY)->Column#15, test.t.b, cast(test.t.b, int(11))->Column#16, cast(test.t.b, int(11))->Column#17, ifnull(cast(test.t.b, bigint(21) UNSIGNED BINARY), 18446744073709551615)->Column#18, ifnull(cast(test.t.b, bigint(21) UNSIGNED BINARY), 0)->Column#19, ifnull(cast(test.t.b, bigint(21) UNSIGNED BINARY), 0)->Column#20, cast(test.t.b, decimal(65,0) BINARY)->Column#14, 1->Column#13",
|
||||
" Projection_8 input:[Group#4], 1->Column#13, cast(test.t.b, decimal(65,0) BINARY)->Column#14, cast(test.t.b, decimal(15,4) BINARY)->Column#15, test.t.b, cast(test.t.b, int(11))->Column#16, cast(test.t.b, int(11))->Column#17, ifnull(cast(test.t.b, bigint(21) UNSIGNED BINARY), 18446744073709551615)->Column#18, ifnull(cast(test.t.b, bigint(21) UNSIGNED BINARY), 0)->Column#19, ifnull(cast(test.t.b, bigint(21) UNSIGNED BINARY), 0)->Column#20, cast(test.t.b, decimal(65,0) BINARY)->Column#14, 1->Column#13",
|
||||
"Group#4 Schema:[test.t.a,test.t.b], UniqueKey:[test.t.a]",
|
||||
" DataSource_1 table:t"
|
||||
]
|
||||
@ -2348,7 +2348,7 @@
|
||||
"SQL": "select count(b), sum(b), avg(b), f, max(c), min(c), bit_and(c), bit_or(d), bit_xor(g) from t group by a",
|
||||
"Result": [
|
||||
"Group#0 Schema:[Column#13,Column#14,Column#15,test.t.f,Column#16,Column#17,Column#18,Column#19,Column#20]",
|
||||
" Projection_5 input:[Group#1], 1->Column#13, cast(test.t.b, decimal(65,0) BINARY)->Column#14, cast(test.t.b, decimal(65,30) BINARY)->Column#15, test.t.f, cast(test.t.c, int(11))->Column#16, cast(test.t.c, int(11))->Column#17, ifnull(cast(test.t.c, bigint(21) UNSIGNED BINARY), 18446744073709551615)->Column#18, ifnull(cast(test.t.d, bigint(21) UNSIGNED BINARY), 0)->Column#19, ifnull(cast(test.t.g, bigint(21) UNSIGNED BINARY), 0)->Column#20",
|
||||
" Projection_5 input:[Group#1], 1->Column#13, cast(test.t.b, decimal(65,0) BINARY)->Column#14, cast(test.t.b, decimal(15,4) BINARY)->Column#15, test.t.f, cast(test.t.c, int(11))->Column#16, cast(test.t.c, int(11))->Column#17, ifnull(cast(test.t.c, bigint(21) UNSIGNED BINARY), 18446744073709551615)->Column#18, ifnull(cast(test.t.d, bigint(21) UNSIGNED BINARY), 0)->Column#19, ifnull(cast(test.t.g, bigint(21) UNSIGNED BINARY), 0)->Column#20",
|
||||
"Group#1 Schema:[test.t.a,test.t.b,test.t.c,test.t.d,test.t.f,test.t.g], UniqueKey:[test.t.f,test.t.f,test.t.g,test.t.a]",
|
||||
" DataSource_1 table:t"
|
||||
]
|
||||
@ -2424,7 +2424,7 @@
|
||||
"Group#1 Schema:[Column#13,Column#14,Column#15]",
|
||||
" Aggregation_5 input:[Group#2], group by:Column#17, funcs:max(test.t.a), min(test.t.b), avg(Column#16)",
|
||||
"Group#2 Schema:[test.t.a,test.t.b,Column#16,Column#17]",
|
||||
" Projection_4 input:[Group#3], test.t.a, test.t.b, cast(test.t.c, decimal(65,30) BINARY)->Column#16, plus(test.t.a, test.t.b)->Column#17",
|
||||
" Projection_4 input:[Group#3], test.t.a, test.t.b, cast(test.t.c, decimal(15,4) BINARY)->Column#16, plus(test.t.a, test.t.b)->Column#17",
|
||||
"Group#3 Schema:[test.t.a,test.t.b,test.t.c]",
|
||||
" DataSource_1 table:t"
|
||||
]
|
||||
|
||||
@ -183,11 +183,11 @@
|
||||
{
|
||||
"Name": "TestWindowFunction",
|
||||
"Cases": [
|
||||
"TableReader(Table(t))->Window(avg(cast(test.t.a, decimal(65,30) BINARY))->Column#14 over(partition by test.t.a))->Projection",
|
||||
"TableReader(Table(t))->Sort->Window(avg(cast(test.t.a, decimal(65,30) BINARY))->Column#14 over(partition by test.t.b))->Projection",
|
||||
"TableReader(Table(t))->Window(avg(cast(test.t.a, decimal(15,4) BINARY))->Column#14 over(partition by test.t.a))->Projection",
|
||||
"TableReader(Table(t))->Sort->Window(avg(cast(test.t.a, decimal(15,4) BINARY))->Column#14 over(partition by test.t.b))->Projection",
|
||||
"IndexReader(Index(t.f)[[NULL,+inf]])->Projection->Sort->Window(avg(cast(Column#16, decimal(24,4) BINARY))->Column#17 over(partition by Column#15))->Projection",
|
||||
"TableReader(Table(t))->Sort->Window(avg(cast(test.t.a, decimal(65,30) BINARY))->Column#14 over(order by test.t.a, test.t.b desc range between unbounded preceding and current row))->Projection",
|
||||
"TableReader(Table(t))->Window(avg(cast(test.t.a, decimal(65,30) BINARY))->Column#14 over(partition by test.t.a))->Projection",
|
||||
"TableReader(Table(t))->Sort->Window(avg(cast(test.t.a, decimal(15,4) BINARY))->Column#14 over(order by test.t.a, test.t.b desc range between unbounded preceding and current row))->Projection",
|
||||
"TableReader(Table(t))->Window(avg(cast(test.t.a, decimal(15,4) BINARY))->Column#14 over(partition by test.t.a))->Projection",
|
||||
"[planner:1054]Unknown column 'z' in 'field list'",
|
||||
"TableReader(Table(t))->Window(sum(cast(test.t.b, decimal(65,0) BINARY))->Column#14 over())->Sort->Projection",
|
||||
"IndexReader(Index(t.f)[[NULL,+inf]]->StreamAgg)->StreamAgg->Window(sum(Column#13)->Column#15 over())->Projection",
|
||||
@ -206,7 +206,7 @@
|
||||
"IndexReader(Index(t.f)[[NULL,+inf]])->Window(sum(cast(test.t.a, decimal(65,0) BINARY))->Column#14 over(rows between 1 preceding and 1 following))->Projection",
|
||||
"[planner:3583]Window '<unnamed window>' cannot inherit 'w' since both contain an ORDER BY clause.",
|
||||
"[planner:3591]Window 'w1' is defined twice.",
|
||||
"TableReader(Table(t))->Window(avg(cast(test.t.a, decimal(65,30) BINARY))->Column#14 over(partition by test.t.a))->Projection",
|
||||
"TableReader(Table(t))->Window(avg(cast(test.t.a, decimal(15,4) BINARY))->Column#14 over(partition by test.t.a))->Projection",
|
||||
"TableReader(Table(t))->Window(sum(cast(test.t.a, decimal(65,0) BINARY))->Column#14 over(partition by test.t.a))->Sort->Projection",
|
||||
"[planner:1235]This version of TiDB doesn't yet support 'GROUPS'",
|
||||
"[planner:3584]Window '<unnamed window>': frame start cannot be UNBOUNDED FOLLOWING.",
|
||||
@ -227,7 +227,7 @@
|
||||
"[planner:1210]Incorrect arguments to nth_value",
|
||||
"[planner:1210]Incorrect arguments to ntile",
|
||||
"IndexReader(Index(t.f)[[NULL,+inf]])->Window(ntile(<nil>)->Column#14 over())->Projection",
|
||||
"TableReader(Table(t))->Sort->Window(avg(cast(test.t.a, decimal(65,30) BINARY))->Column#14 over(partition by test.t.b))->Projection",
|
||||
"TableReader(Table(t))->Sort->Window(avg(cast(test.t.a, decimal(15,4) BINARY))->Column#14 over(partition by test.t.b))->Projection",
|
||||
"TableReader(Table(t))->Window(nth_value(test.t.i_date, 1)->Column#14 over())->Projection",
|
||||
"TableReader(Table(t))->Window(sum(cast(test.t.b, decimal(65,0) BINARY))->Column#15, sum(cast(test.t.c, decimal(65,0) BINARY))->Column#16 over(order by test.t.a range between unbounded preceding and current row))->Projection",
|
||||
"[planner:3593]You cannot use the window function 'sum' in this context.'",
|
||||
@ -256,11 +256,11 @@
|
||||
{
|
||||
"Name": "TestWindowParallelFunction",
|
||||
"Cases": [
|
||||
"TableReader(Table(t))->Window(avg(cast(test.t.a, decimal(65,30) BINARY))->Column#14 over(partition by test.t.a))->Projection",
|
||||
"TableReader(Table(t))->Sort->Window(avg(cast(test.t.a, decimal(65,30) BINARY))->Column#14 over(partition by test.t.b))->Partition(execution info: concurrency:4, data sources:[TableReader_10])->Projection",
|
||||
"TableReader(Table(t))->Window(avg(cast(test.t.a, decimal(15,4) BINARY))->Column#14 over(partition by test.t.a))->Projection",
|
||||
"TableReader(Table(t))->Sort->Window(avg(cast(test.t.a, decimal(15,4) BINARY))->Column#14 over(partition by test.t.b))->Partition(execution info: concurrency:4, data sources:[TableReader_10])->Projection",
|
||||
"IndexReader(Index(t.f)[[NULL,+inf]])->Projection->Sort->Window(avg(cast(Column#16, decimal(24,4) BINARY))->Column#17 over(partition by Column#15))->Partition(execution info: concurrency:4, data sources:[Projection_8])->Projection",
|
||||
"TableReader(Table(t))->Sort->Window(avg(cast(test.t.a, decimal(65,30) BINARY))->Column#14 over(order by test.t.a, test.t.b desc range between unbounded preceding and current row))->Projection",
|
||||
"TableReader(Table(t))->Window(avg(cast(test.t.a, decimal(65,30) BINARY))->Column#14 over(partition by test.t.a))->Projection",
|
||||
"TableReader(Table(t))->Sort->Window(avg(cast(test.t.a, decimal(15,4) BINARY))->Column#14 over(order by test.t.a, test.t.b desc range between unbounded preceding and current row))->Projection",
|
||||
"TableReader(Table(t))->Window(avg(cast(test.t.a, decimal(15,4) BINARY))->Column#14 over(partition by test.t.a))->Projection",
|
||||
"[planner:1054]Unknown column 'z' in 'field list'",
|
||||
"TableReader(Table(t))->Window(sum(cast(test.t.b, decimal(65,0) BINARY))->Column#14 over())->Sort->Projection",
|
||||
"IndexReader(Index(t.f)[[NULL,+inf]]->StreamAgg)->StreamAgg->Window(sum(Column#13)->Column#15 over())->Projection",
|
||||
@ -279,7 +279,7 @@
|
||||
"IndexReader(Index(t.f)[[NULL,+inf]])->Window(sum(cast(test.t.a, decimal(65,0) BINARY))->Column#14 over(rows between 1 preceding and 1 following))->Projection",
|
||||
"[planner:3583]Window '<unnamed window>' cannot inherit 'w' since both contain an ORDER BY clause.",
|
||||
"[planner:3591]Window 'w1' is defined twice.",
|
||||
"TableReader(Table(t))->Window(avg(cast(test.t.a, decimal(65,30) BINARY))->Column#14 over(partition by test.t.a))->Projection",
|
||||
"TableReader(Table(t))->Window(avg(cast(test.t.a, decimal(15,4) BINARY))->Column#14 over(partition by test.t.a))->Projection",
|
||||
"TableReader(Table(t))->Window(sum(cast(test.t.a, decimal(65,0) BINARY))->Column#14 over(partition by test.t.a))->Sort->Projection",
|
||||
"[planner:1235]This version of TiDB doesn't yet support 'GROUPS'",
|
||||
"[planner:3584]Window '<unnamed window>': frame start cannot be UNBOUNDED FOLLOWING.",
|
||||
@ -300,7 +300,7 @@
|
||||
"[planner:1210]Incorrect arguments to nth_value",
|
||||
"[planner:1210]Incorrect arguments to ntile",
|
||||
"IndexReader(Index(t.f)[[NULL,+inf]])->Window(ntile(<nil>)->Column#14 over())->Projection",
|
||||
"TableReader(Table(t))->Sort->Window(avg(cast(test.t.a, decimal(65,30) BINARY))->Column#14 over(partition by test.t.b))->Partition(execution info: concurrency:4, data sources:[TableReader_10])->Projection",
|
||||
"TableReader(Table(t))->Sort->Window(avg(cast(test.t.a, decimal(15,4) BINARY))->Column#14 over(partition by test.t.b))->Partition(execution info: concurrency:4, data sources:[TableReader_10])->Projection",
|
||||
"TableReader(Table(t))->Window(nth_value(test.t.i_date, 1)->Column#14 over())->Projection",
|
||||
"TableReader(Table(t))->Window(sum(cast(test.t.b, decimal(65,0) BINARY))->Column#15, sum(cast(test.t.c, decimal(65,0) BINARY))->Column#16 over(order by test.t.a range between unbounded preceding and current row))->Projection",
|
||||
"[planner:3593]You cannot use the window function 'sum' in this context.'",
|
||||
|
||||
Reference in New Issue
Block a user