Skip to content

Commit

Permalink
executor: trace memory usage of ntile() (#19756)
Browse files Browse the repository at this point in the history
  • Loading branch information
Howie59 authored Sep 10, 2020
1 parent 80e04b5 commit 5d52a34
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions executor/aggfuncs/aggfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ var (
const (
// DefUint32Size is the size of uint32
DefUint32Size = int64(unsafe.Sizeof(uint32(0)))
// DefUint64Size is the size of uint64
DefUint64Size = int64(unsafe.Sizeof(uint64(0)))
// DefInt64Size is the size of int64
DefInt64Size = int64(unsafe.Sizeof(int64(0)))
// DefFloat64Size is the size of float64
Expand Down
9 changes: 8 additions & 1 deletion executor/aggfuncs/func_ntile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@
package aggfuncs

import (
"unsafe"

"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/util/chunk"
)

const (
// DefPartialResult4Ntile is the size of partialResult4Ntile
DefPartialResult4Ntile = int64(unsafe.Sizeof(partialResult4Ntile{}))
)

// ntile divides the partition into n ranked groups and returns the group number a row belongs to.
// e.g. We have 11 rows and n = 3. They will be divided into 3 groups.
// First 4 rows belongs to group 1. Following 4 rows belongs to group 2. The last 3 rows belongs to group 3.
Expand All @@ -35,7 +42,7 @@ type partialResult4Ntile struct {
}

func (n *ntile) AllocPartialResult() (pr PartialResult, memDelta int64) {
return PartialResult(&partialResult4Ntile{curGroupIdx: 1}), 0
return PartialResult(&partialResult4Ntile{curGroupIdx: 1}), DefPartialResult4Ntile
}

func (n *ntile) ResetPartialResult(pr PartialResult) {
Expand Down
36 changes: 36 additions & 0 deletions executor/aggfuncs/func_ntile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package aggfuncs_test

import (
. "github.com/pingcap/check"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/mysql"

"github.com/pingcap/tidb/executor/aggfuncs"
)

func (s *testSuite) TestMemNtile(c *C) {
tests := []windowMemTest{
buildWindowMemTester(ast.WindowFuncNtile, mysql.TypeLonglong, 1, 1, 1,
aggfuncs.DefPartialResult4Ntile, defaultUpdateMemDeltaGens),
buildWindowMemTester(ast.WindowFuncNtile, mysql.TypeLonglong, 1, 3, 0,
aggfuncs.DefPartialResult4Ntile, defaultUpdateMemDeltaGens),
buildWindowMemTester(ast.WindowFuncNtile, mysql.TypeLonglong, 1, 4, 1,
aggfuncs.DefPartialResult4Ntile, defaultUpdateMemDeltaGens),
}
for _, test := range tests {
s.testWindowAggMemFunc(c, test)
}
}

0 comments on commit 5d52a34

Please sign in to comment.