Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: Fix the panic in approx_percentile function #49644

Merged
merged 8 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions pkg/executor/aggfuncs/aggfunc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,16 +586,6 @@ func testAggFunc(t *testing.T, p aggTest) {
result, err = dt.Compare(ctx.GetSessionVars().StmtCtx.TypeCtx(), &p.results[1], ctor)
require.NoError(t, err)
require.Equalf(t, 0, result, "%v != %v", dt.String(), p.results[1])

// test the empty input
resultChk.Reset()
finalFunc.ResetPartialResult(finalPr)
err = finalFunc.AppendFinalResult2Chunk(ctx, finalPr, resultChk)
require.NoError(t, err)
dt = resultChk.GetRow(0).GetDatum(0, desc.RetTp)
result, err = dt.Compare(ctx.GetSessionVars().StmtCtx.TypeCtx(), &p.results[0], ctor)
require.NoError(t, err)
require.Equalf(t, 0, result, "%v != %v", dt.String(), p.results[0])
xzhangxian1008 marked this conversation as resolved.
Show resolved Hide resolved
}

func testAggFuncWithoutDistinct(t *testing.T, p aggTest) {
Expand Down
19 changes: 15 additions & 4 deletions pkg/executor/aggfuncs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ func buildApproxCountDistinct(aggFuncDesc *aggregation.AggFuncDesc, ordinal int)
return nil
}

func getEvalTypeForApproxPercentile(aggFuncDesc *aggregation.AggFuncDesc) types.EvalType {
evalType := aggFuncDesc.Args[0].GetType().EvalType()
argType := aggFuncDesc.Args[0].GetType().GetType()

// Sometimes `mysql.EnumSetAsIntFlag` may be set to true, such as when join,
// which is unexpected for `buildApproxPercentile` and `mysql.TypeEnum` and `mysql.TypeSet` will return unexpected `ETInt` here,
// so here `evalType` are forcibly set to `ETString`.
if argType == mysql.TypeEnum || argType == mysql.TypeSet || argType == mysql.TypeBit {
xzhangxian1008 marked this conversation as resolved.
Show resolved Hide resolved
evalType = types.ETString
}

return evalType
}

func buildApproxPercentile(sctx sessionctx.Context, aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
if aggFuncDesc.Mode == aggregation.DedupMode {
return nil
Expand All @@ -159,10 +173,7 @@ func buildApproxPercentile(sctx sessionctx.Context, aggFuncDesc *aggregation.Agg

base := basePercentile{percent: int(percent), baseAggFunc: baseAggFunc{args: aggFuncDesc.Args, ordinal: ordinal}}

evalType := aggFuncDesc.Args[0].GetType().EvalType()
if aggFuncDesc.Args[0].GetType().GetType() == mysql.TypeBit {
evalType = types.ETString // same as other aggregate function
}
evalType := getEvalTypeForApproxPercentile(aggFuncDesc)
switch aggFuncDesc.Mode {
case aggregation.CompleteMode, aggregation.Partial1Mode, aggregation.FinalMode:
switch evalType {
Expand Down
11 changes: 11 additions & 0 deletions pkg/executor/aggfuncs/func_percentile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func TestPercentile(t *testing.T) {
testAggFunc(t, test)
})
}
}

func TestFix26807(t *testing.T) {
data := testSlice{}
want := 28
for i := 1; i <= want; i++ {
Expand All @@ -57,3 +59,12 @@ func TestPercentile(t *testing.T) {
require.Equal(t, want, data[index])
}
}

func TestFix40463(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

types := []byte{mysql.TypeEnum, mysql.TypeSet}
for _, tp := range types {
test := buildAggTester(ast.AggFuncApproxPercentile, tp, 5, nil, nil)
test.dataType.AddFlag(mysql.EnumSetAsIntFlag)
testAggFunc(t, test)
}
}