Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#49644
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
xzhangxian1008 authored and ti-chi-bot committed Dec 29, 2023
1 parent 4d53073 commit c181179
Show file tree
Hide file tree
Showing 5 changed files with 2,867 additions and 4 deletions.
3 changes: 3 additions & 0 deletions executor/aggfuncs/aggfunc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ func testAggFunc(t *testing.T, p aggTest) {
result, err = dt.Compare(ctx.GetSessionVars().StmtCtx, &p.results[1], ctor)
require.NoError(t, err)
require.Equalf(t, 0, result, "%v != %v", dt.String(), p.results[1])
<<<<<<< HEAD:executor/aggfuncs/aggfunc_test.go

// test the empty input
resultChk.Reset()
Expand All @@ -596,6 +597,8 @@ func testAggFunc(t *testing.T, p aggTest) {
result, err = dt.Compare(ctx.GetSessionVars().StmtCtx, &p.results[0], ctor)
require.NoError(t, err)
require.Equalf(t, 0, result, "%v != %v", dt.String(), p.results[0])
=======
>>>>>>> 4a47236db9d (executor: Fix the panic in `approx_percentile` function (#49644)):pkg/executor/aggfuncs/aggfunc_test.go
}

func testAggFuncWithoutDistinct(t *testing.T, p aggTest) {
Expand Down
20 changes: 16 additions & 4 deletions executor/aggfuncs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ 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`.
// For mysql.TypeBit, just same as other aggregate function.
if argType == mysql.TypeEnum || argType == mysql.TypeSet || argType == mysql.TypeBit {
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 +174,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 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) {
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)
}
}
60 changes: 60 additions & 0 deletions pkg/executor/test/executor/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
load("@io_bazel_rules_go//go:def.bzl", "go_test")

go_test(
name = "executor_test",
timeout = "short",
srcs = [
"executor_test.go",
"main_test.go",
],
flaky = True,
shard_count = 48,
deps = [
"//pkg/config",
"//pkg/ddl",
"//pkg/domain",
"//pkg/domain/infosync",
"//pkg/executor",
"//pkg/executor/internal/exec",
"//pkg/expression",
"//pkg/infoschema",
"//pkg/kv",
"//pkg/meta",
"//pkg/meta/autoid",
"//pkg/parser",
"//pkg/parser/model",
"//pkg/parser/mysql",
"//pkg/parser/terror",
"//pkg/planner",
"//pkg/planner/core",
"//pkg/session",
"//pkg/sessionctx",
"//pkg/sessionctx/stmtctx",
"//pkg/sessionctx/variable",
"//pkg/sessiontxn",
"//pkg/store/mockstore",
"//pkg/table/tables",
"//pkg/tablecodec",
"//pkg/testkit",
"//pkg/testkit/testdata",
"//pkg/types",
"//pkg/util",
"//pkg/util/dbterror/exeerrors",
"//pkg/util/memory",
"//pkg/util/mock",
"//pkg/util/replayer",
"//pkg/util/rowcodec",
"//pkg/util/sqlexec",
"//pkg/util/timeutil",
"@com_github_golang_protobuf//proto",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_pingcap_tipb//go-tipb",
"@com_github_stretchr_testify//require",
"@com_github_tikv_client_go_v2//oracle",
"@com_github_tikv_client_go_v2//testutils",
"@com_github_tikv_client_go_v2//tikv",
"@io_opencensus_go//stats/view",
"@org_uber_go_goleak//:goleak",
],
)
Loading

0 comments on commit c181179

Please sign in to comment.