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: support MAX/MIN in new evaluation framework partially #6971

Merged
merged 17 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 11 additions & 7 deletions executor/aggfuncs/aggfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,24 @@ import (
var (
// All the AggFunc implementations for "COUNT" are listed here.
// All the AggFunc implementations for "SUM" are listed here.
// All the AggFunc implementations for "FIRSTROW" are listed here.
// All the AggFunc implementations for "MAX"/"MIN" are listed here.
_ AggFunc = (*maxMin4Int)(nil)
_ AggFunc = (*maxMin4Float32)(nil)
_ AggFunc = (*maxMin4Float64)(nil)
_ AggFunc = (*maxMin4Decimal)(nil)

// All the AggFunc implementations for "AVG" are listed here.
_ AggFunc = (*avgOriginal4Decimal)(nil)
_ AggFunc = (*avgPartial4Decimal)(nil)

_ AggFunc = (*avgOriginal4Float64)(nil)
_ AggFunc = (*avgPartial4Float64)(nil)

// All the AggFunc implementations for "FIRSTROW" are listed here.
// All the AggFunc implementations for "MAX" are listed here.
// All the AggFunc implementations for "MIN" are listed here.
// All the AggFunc implementations for "GROUP_CONCAT" are listed here.
// All the AggFunc implementations for "BIT_OR" are listed here.
// All the AggFunc implementations for "BIT_XOR" are listed here.
// All the AggFunc implementations for "BIT_AND" are listed here.
// All the AggFunc implementations for "GROUP_CONCAT" are listed here.
// All the AggFunc implementations for "BIT_OR" are listed here.
// All the AggFunc implementations for "BIT_XOR" are listed here.
// All the AggFunc implementations for "BIT_AND" are listed here.
)

// PartialResult represents data structure to store the partial result for the
Expand Down
49 changes: 34 additions & 15 deletions executor/aggfuncs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/expression/aggregation"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/types"
)

// Build is used to build a specific AggFunc implementation according to the
Expand All @@ -32,9 +33,9 @@ func Build(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
case ast.AggFuncFirstRow:
return buildFirstRow(aggFuncDesc, ordinal)
case ast.AggFuncMax:
return buildMax(aggFuncDesc, ordinal)
return buildMaxMin(aggFuncDesc, ordinal, true)
case ast.AggFuncMin:
return buildMin(aggFuncDesc, ordinal)
return buildMaxMin(aggFuncDesc, ordinal, false)
case ast.AggFuncGroupConcat:
return buildGroupConcat(aggFuncDesc, ordinal)
case ast.AggFuncBitOr:
Expand All @@ -52,12 +53,12 @@ func buildCount(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
return nil
}

// buildCount builds the AggFunc implementation for function "SUM".
// buildSum builds the AggFunc implementation for function "SUM".
func buildSum(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
return nil
}

// buildCount builds the AggFunc implementation for function "AVG".
// buildAvg builds the AggFunc implementation for function "AVG".
func buildAvg(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
base := baseAggFunc{
args: aggFuncDesc.Args,
Expand Down Expand Up @@ -98,37 +99,55 @@ func buildAvg(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
return nil
}

// buildCount builds the AggFunc implementation for function "FIRST_ROW".
// buildFirstRow builds the AggFunc implementation for function "FIRST_ROW".
func buildFirstRow(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
return nil
}

// buildCount builds the AggFunc implementation for function "MAX".
func buildMax(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
return nil
}
// buildMaxMin builds the AggFunc implementation for function "MAX" and "MIN".
func buildMaxMin(aggFuncDesc *aggregation.AggFuncDesc, ordinal int, isMax bool) AggFunc {
base := baseAggFunc{
args: aggFuncDesc.Args,
ordinal: ordinal,
}

// buildCount builds the AggFunc implementation for function "MIN".
func buildMin(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
evalType, fieldType := aggFuncDesc.RetTp.EvalType(), aggFuncDesc.RetTp
switch aggFuncDesc.Mode {
case aggregation.DedupMode:
default:
switch evalType {
case types.ETInt:
return &maxMin4Int{base, isMax, mysql.HasUnsignedFlag(fieldType.Flag)}
case types.ETReal:
switch fieldType.Tp {
case mysql.TypeFloat:
return &maxMin4Float32{base, isMax}
case mysql.TypeDouble:
return &maxMin4Float64{base, isMax}
}
case types.ETDecimal:
return &maxMin4Decimal{base, isMax}
}
}
return nil
}

// buildCount builds the AggFunc implementation for function "GROUP_CONCAT".
// buildGroupConcat builds the AggFunc implementation for function "GROUP_CONCAT".
func buildGroupConcat(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
return nil
}

// buildCount builds the AggFunc implementation for function "BIT_OR".
// buildBitOr builds the AggFunc implementation for function "BIT_OR".
func buildBitOr(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
return nil
}

// buildCount builds the AggFunc implementation for function "BIT_XOR".
// buildBitXor builds the AggFunc implementation for function "BIT_XOR".
func buildBitXor(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
return nil
}

// buildCount builds the AggFunc implementation for function "BIT_AND".
// buildBitAnd builds the AggFunc implementation for function "BIT_AND".
func buildBitAnd(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {
return nil
}
191 changes: 191 additions & 0 deletions executor/aggfuncs/func_max_min.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
// Copyright 2018 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

import (
"github.com/juju/errors"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
)

type partialResult4MaxMinInt int64
Copy link
Contributor

@crazycs520 crazycs520 Jul 5, 2018

Choose a reason for hiding this comment

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

How aboult use type partialResult4MaxMinInt = int64,
then we can use *(*partialResult4MaxMinInt)(pr) instead of int64(*(*partialResult4MaxMinInt)(pr))

below partialResult4MaxMinDecimal, partialResult4MaxMinFloat32, partialResult4MaxMinFloat64 is same too.

here is a benchmark

type num1 = int
type num2 int

func BenchmarkT1(b *testing.B) {
	var a1 num1
	a1 = 1
	for i := 0; i < b.N; {
		i += a1
	}
}

func BenchmarkT2(b *testing.B) {
	var a2 num2
	a2 = 1
	for i := 0; i < b.N; {
		i += int(a2)
	}
}
▶ go test -bench=. 
goos: darwin
goarch: amd64
pkg: test2
BenchmarkT1-8           2000000000               0.27 ns/op
BenchmarkT2-8           2000000000               0.54 ns/op
PASS
ok      test2   1.757s

type partialResult4MaxMinDecimal *types.MyDecimal
type partialResult4MaxMinFloat32 float32
type partialResult4MaxMinFloat64 float64

// Todo
type partialResult4MaxMinString string
type partialResult4MaxMinDatetime types.Time
type partialResult4MaxMinTimestamp types.Time
type partialResult4MaxMinDuration types.Duration

type maxMin4Int struct {
baseAggFunc

isMax bool
isUnsigned bool
}

func (e *maxMin4Int) AllocPartialResult() PartialResult {
return PartialResult(new(partialResult4MaxMinInt))
}

func (e *maxMin4Int) ResetPartialResult(pr PartialResult) {
*(*partialResult4MaxMinInt)(pr) = 0
}

func (e *maxMin4Int) AppendFinalResult2Chunk(sctx sessionctx.Context, pr PartialResult, chk *chunk.Chunk) error {
if !e.isUnsigned {
chk.AppendInt64(e.ordinal, int64(*(*partialResult4MaxMinInt)(pr)))
} else {
chk.AppendUint64(e.ordinal, uint64(*(*partialResult4MaxMinInt)(pr)))
}

return nil
}

func (e *maxMin4Int) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) error {
tmp := (*partialResult4MaxMinInt)(pr)
Copy link
Member

Choose a reason for hiding this comment

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

to keep consistent with avg implementations: s/tmp/p/

for _, row := range rowsInGroup {
input, isNull, err := e.args[0].EvalInt(sctx, row)
if err != nil {
return errors.Trace(err)
}
if isNull {
continue
}
if !e.isUnsigned {
Copy link
Member

Choose a reason for hiding this comment

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

put this if branch outside the for loop

if f0, f1 := int64(*tmp), int64(input); e.isMax && f1 > f0 || !e.isMax && f1 < f0 {
Copy link
Member

Choose a reason for hiding this comment

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

no need to wrap a int64

*tmp = partialResult4MaxMinInt(f1)
}
} else {
if f0, f1 := uint64(*tmp), uint64(input); e.isMax && f1 > f0 || !e.isMax && f1 < f0 {
*tmp = partialResult4MaxMinInt(f1)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

        uint64Input := uint64(input)
		if !e.executed {
			*p = uint64Input
			e.executed = true
			continue
		}
		if e.isMax && uint64Input > *p || !e.isMax && uint64Input < *p {
			*p = uint64Input
		}

}
return nil
}

// maxMin4Float32 gets a float32 input and returns a float32 result.
type maxMin4Float32 struct {
baseAggFunc

isMax bool
}

func (e *maxMin4Float32) AllocPartialResult() PartialResult {
return PartialResult(new(partialResult4MaxMinFloat32))
}

func (e *maxMin4Float32) ResetPartialResult(pr PartialResult) {
*(*partialResult4MaxMinFloat32)(pr) = 0
}

func (e *maxMin4Float32) AppendFinalResult2Chunk(sctx sessionctx.Context, pr PartialResult, chk *chunk.Chunk) error {
chk.AppendFloat32(e.ordinal, float32(*(*partialResult4MaxMinFloat32)(pr)))
Copy link
Contributor

Choose a reason for hiding this comment

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

chk.AppendFloat32(e.ordinal, *(*partialResult4MaxMinFloat32)(pr))

return nil
}

func (e *maxMin4Float32) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) error {
tmp := (*partialResult4MaxMinFloat32)(pr)
for _, row := range rowsInGroup {
input, isNull, err := e.args[0].EvalReal(sctx, row)
if err != nil {
return errors.Trace(err)
}
if isNull {
continue
}
if f0, f1 := float32(*tmp), float32(input); e.isMax && f1 > f0 || !e.isMax && f1 < f0 {
*tmp = partialResult4MaxMinFloat32(f1)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

        float32Input := float32(input)
		if !e.executed {
			*p = float32Input
			e.executed = true
			continue
		}
		if  e.isMax && float32Input > *p || !e.isMax && float32Input < *p {
			*p = float32Input
		}

}
return nil
}

type maxMin4Float64 struct {
baseAggFunc

isMax bool
}

func (e *maxMin4Float64) AllocPartialResult() PartialResult {
return PartialResult(new(partialResult4MaxMinFloat64))
}

func (e *maxMin4Float64) ResetPartialResult(pr PartialResult) {
*(*partialResult4MaxMinFloat64)(pr) = 0
}

func (e *maxMin4Float64) AppendFinalResult2Chunk(sctx sessionctx.Context, pr PartialResult, chk *chunk.Chunk) error {
chk.AppendFloat64(e.ordinal, float64(*(*partialResult4MaxMinFloat64)(pr)))
Copy link
Contributor

Choose a reason for hiding this comment

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

chk.AppendFloat64(e.ordinal, *(*partialResult4MaxMinFloat64)(pr))

return nil
}

func (e *maxMin4Float64) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) error {
tmp := (*partialResult4MaxMinFloat64)(pr)
for _, row := range rowsInGroup {
input, isNull, err := e.args[0].EvalReal(sctx, row)
if err != nil {
return errors.Trace(err)
}
if isNull {
continue
}
if f0, f1 := float64(*tmp), float64(input); e.isMax && f1 > f0 || !e.isMax && f1 < f0 {
*tmp = partialResult4MaxMinFloat64(f1)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

        if e.isMax && input > *p || !e.isMax && input < *p {
			*p = input
		}

}
return nil
}

type maxMin4Decimal struct {
baseAggFunc

isMax bool
}

func (e *maxMin4Decimal) AllocPartialResult() PartialResult {
return PartialResult(new(partialResult4MaxMinDecimal))
}

func (e *maxMin4Decimal) ResetPartialResult(pr PartialResult) {
*(*partialResult4MaxMinDecimal)(pr) = partialResult4MaxMinDecimal(*new(partialResult4MaxMinDecimal))
}

func (e *maxMin4Decimal) AppendFinalResult2Chunk(sctx sessionctx.Context, pr PartialResult, chk *chunk.Chunk) error {
chk.AppendMyDecimal(e.ordinal, (*types.MyDecimal)(*(*partialResult4MaxMinDecimal)(pr)))
Copy link
Contributor

Choose a reason for hiding this comment

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

chk.AppendMyDecimal(e.ordinal, *(*partialResult4MaxMinDecimal)(pr))

return nil
}

func (e *maxMin4Decimal) UpdatePartialResult(sctx sessionctx.Context, rowsInGroup []chunk.Row, pr PartialResult) error {
tmp := (*partialResult4MaxMinDecimal)(pr)
for _, row := range rowsInGroup {
input, isNull, err := e.args[0].EvalDecimal(sctx, row)
if err != nil {
return errors.Trace(err)
}
if isNull {
continue
}
f0, f1 := (*types.MyDecimal)(*tmp), (*types.MyDecimal)(input)
cmp := f1.Compare(f0)
if e.isMax && cmp == 1 || !e.isMax && cmp == -1 {
*tmp = partialResult4MaxMinDecimal(f1)
}
Copy link
Contributor

@crazycs520 crazycs520 Jul 6, 2018

Choose a reason for hiding this comment

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

                cmp := input.Compare(*p)
		if e.isMax && cmp == 1 || !e.isMax && cmp == -1 {
			*p = input
		}

}
return nil
}