Skip to content

Commit

Permalink
*: optimize SortExec (pingcap#4622)
Browse files Browse the repository at this point in the history
* *: change key of orderByRow from "key []types.Datum" to "key []*types.Datum"

* address comment
  • Loading branch information
zz-jason authored and coocood committed Sep 25, 2017
1 parent 9a78f19 commit 6f84392
Show file tree
Hide file tree
Showing 35 changed files with 82 additions and 77 deletions.
8 changes: 4 additions & 4 deletions distsql/xeval/eval_compare_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (e *Evaluator) compareTwoChildren(expr *tipb.Expr) (int, error) {
if left.IsNull() || right.IsNull() {
return compareResultNull, nil
}
return left.CompareDatum(e.StatementCtx, right)
return left.CompareDatum(e.StatementCtx, &right)
}

func (e *Evaluator) evalLT(cmp int) (types.Datum, error) {
Expand Down Expand Up @@ -140,7 +140,7 @@ func (e *Evaluator) evalNullEQ(expr *tipb.Expr) (types.Datum, error) {
if err != nil {
return types.Datum{}, errors.Trace(err)
}
cmp, err := left.CompareDatum(e.StatementCtx, right)
cmp, err := left.CompareDatum(e.StatementCtx, &right)
if err != nil {
return types.Datum{}, errors.Trace(err)
}
Expand Down Expand Up @@ -267,7 +267,7 @@ func (e *Evaluator) checkIn(target types.Datum, list []types.Datum) (bool, error
var outerErr error
n := sort.Search(len(list), func(i int) bool {
val := list[i]
cmp, err := val.CompareDatum(e.StatementCtx, target)
cmp, err := val.CompareDatum(e.StatementCtx, &target)
if err != nil {
outerErr = errors.Trace(err)
return false
Expand All @@ -280,7 +280,7 @@ func (e *Evaluator) checkIn(target types.Datum, list []types.Datum) (bool, error
if n < 0 || n >= len(list) {
return false, nil
}
cmp, err := list[n].CompareDatum(e.StatementCtx, target)
cmp, err := list[n].CompareDatum(e.StatementCtx, &target)
if err != nil {
return false, errors.Trace(err)
}
Expand Down
2 changes: 1 addition & 1 deletion distsql/xeval/eval_control_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (e *Evaluator) evalNullIf(expr *tipb.Expr) (d types.Datum, err error) {
if left.IsNull() || right.IsNull() {
return left, nil
}
x, err := left.CompareDatum(e.StatementCtx, right)
x, err := left.CompareDatum(e.StatementCtx, &right)
if err != nil {
return d, errors.Trace(err)
}
Expand Down
8 changes: 4 additions & 4 deletions distsql/xeval/eval_control_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (s *testEvalSuite) TestEvalCaseWhen(c *C) {
result, err := xevaluator.Eval(tt.expr)
c.Assert(err, IsNil)
c.Assert(result.Kind(), Equals, tt.result.Kind())
cmp, err := result.CompareDatum(xevaluator.StatementCtx, tt.result)
cmp, err := result.CompareDatum(xevaluator.StatementCtx, &tt.result)
c.Assert(err, IsNil)
c.Assert(cmp, Equals, 0)
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func (s *testEvalSuite) TestEvalIf(c *C) {
result, err := xevaluator.Eval(tt.expr)
c.Assert(err, IsNil)
c.Assert(result.Kind(), Equals, tt.result.Kind())
cmp, err := result.CompareDatum(xevaluator.StatementCtx, tt.result)
cmp, err := result.CompareDatum(xevaluator.StatementCtx, &tt.result)
c.Assert(err, IsNil)
c.Assert(cmp, Equals, 0)
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func (s *testEvalSuite) TestEvalNullIf(c *C) {
result, err := xevaluator.Eval(tt.expr)
c.Assert(err, IsNil)
c.Assert(result.Kind(), Equals, tt.result.Kind())
cmp, err := result.CompareDatum(xevaluator.StatementCtx, tt.result)
cmp, err := result.CompareDatum(xevaluator.StatementCtx, &tt.result)
c.Assert(err, IsNil)
c.Assert(cmp, Equals, 0)
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func (s *testEvalSuite) TestEvalIfNull(c *C) {
result, err := xevaluator.Eval(tt.expr)
c.Assert(err, IsNil)
c.Assert(result.Kind(), Equals, tt.result.Kind())
cmp, err := result.CompareDatum(xevaluator.StatementCtx, tt.result)
cmp, err := result.CompareDatum(xevaluator.StatementCtx, &tt.result)
c.Assert(err, IsNil)
c.Assert(cmp, Equals, 0)
}
Expand Down
2 changes: 1 addition & 1 deletion distsql/xeval/eval_other_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *testEvalSuite) TestEvalCoalesce(c *C) {
result, err := xevaluator.Eval(tt.expr)
c.Assert(err, IsNil)
c.Assert(result.Kind(), Equals, tt.result.Kind())
cmp, err := result.CompareDatum(xevaluator.StatementCtx, tt.result)
cmp, err := result.CompareDatum(xevaluator.StatementCtx, &tt.result)
c.Assert(err, IsNil)
c.Assert(cmp, Equals, 0)
}
Expand Down
4 changes: 2 additions & 2 deletions distsql/xeval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func (s *testEvalSuite) TestEval(c *C) {
result, err := xevaluator.Eval(tt.expr)
c.Assert(err, IsNil)
c.Assert(result.Kind(), Equals, tt.result.Kind())
cmp, err := result.CompareDatum(xevaluator.StatementCtx, tt.result)
cmp, err := result.CompareDatum(xevaluator.StatementCtx, &tt.result)
c.Assert(err, IsNil)
c.Assert(cmp, Equals, 0)
}
Expand Down Expand Up @@ -495,7 +495,7 @@ func (s *testEvalSuite) TestEvalIsNull(c *C) {
result, err := xevaluator.Eval(tt.expr)
c.Assert(err, IsNil)
c.Assert(result.Kind(), Equals, tt.result.Kind())
cmp, err := result.CompareDatum(xevaluator.StatementCtx, tt.result)
cmp, err := result.CompareDatum(xevaluator.StatementCtx, &tt.result)
c.Assert(err, IsNil)
c.Assert(cmp, Equals, 0)
}
Expand Down
2 changes: 1 addition & 1 deletion executor/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (e *StreamAggExec) meetNewGroup(row Row) (bool, error) {
return false, errors.Trace(err)
}
if matched {
c, err := v.CompareDatum(e.StmtCtx, e.curGroupKey[i])
c, err := v.CompareDatum(e.StmtCtx, &e.curGroupKey[i])
if err != nil {
return false, errors.Trace(err)
}
Expand Down
4 changes: 2 additions & 2 deletions executor/distsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func convertIndexRangeTypes(sc *variable.StatementContext, ran *types.IndexRange
if err != nil {
return errors.Trace(err)
}
cmp, err := converted.CompareDatum(sc, ran.LowVal[i])
cmp, err := converted.CompareDatum(sc, &ran.LowVal[i])
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func convertIndexRangeTypes(sc *variable.StatementContext, ran *types.IndexRange
if err != nil {
return errors.Trace(err)
}
cmp, err := converted.CompareDatum(sc, ran.HighVal[i])
cmp, err := converted.CompareDatum(sc, &ran.HighVal[i])
if err != nil {
return errors.Trace(err)
}
Expand Down
2 changes: 1 addition & 1 deletion executor/merge_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func compareKeys(stmtCtx *variable.StatementContext,
return 0, errors.Trace(err)
}

ret, err := lVal.CompareDatum(stmtCtx, rVal)
ret, err := lVal.CompareDatum(stmtCtx, &rVal)
if err != nil {
return 0, errors.Trace(err)
}
Expand Down
12 changes: 7 additions & 5 deletions executor/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

// orderByRow binds a row to its order values, so it can be sorted.
type orderByRow struct {
key []types.Datum
key []*types.Datum
row Row
}

Expand Down Expand Up @@ -105,13 +105,14 @@ func (e *SortExec) Next() (Row, error) {
}
orderRow := &orderByRow{
row: srcRow,
key: make([]types.Datum, len(e.ByItems)),
key: make([]*types.Datum, len(e.ByItems)),
}
for i, byItem := range e.ByItems {
orderRow.key[i], err = byItem.Expr.Eval(srcRow)
key, err := byItem.Expr.Eval(srcRow)
if err != nil {
return nil, errors.Trace(err)
}
orderRow.key[i] = &key
}
e.Rows = append(e.Rows, orderRow)
}
Expand Down Expand Up @@ -204,13 +205,14 @@ func (e *TopNExec) Next() (Row, error) {
// build orderRow from srcRow.
orderRow := &orderByRow{
row: srcRow,
key: make([]types.Datum, len(e.ByItems)),
key: make([]*types.Datum, len(e.ByItems)),
}
for i, byItem := range e.ByItems {
orderRow.key[i], err = byItem.Expr.Eval(srcRow)
key, err := byItem.Expr.Eval(srcRow)
if err != nil {
return nil, errors.Trace(err)
}
orderRow.key[i] = &key
}
if e.totalCount == e.heapSize {
// An equivalent of Push and Pop. We don't use the standard Push and Pop
Expand Down
4 changes: 2 additions & 2 deletions executor/union_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (us *UnionScanExec) twoRowsAreEqual(a, b Row) (bool, error) {
}
sc := us.ctx.GetSessionVars().StmtCtx
for i := 0; i < len(a); i++ {
cmp, err := a[i].CompareDatum(sc, b[i])
cmp, err := a[i].CompareDatum(sc, &b[i])
if err != nil {
return false, errors.Trace(err)
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func (us *UnionScanExec) compare(a, b Row) (int, error) {
for _, colOff := range us.usedIndex {
aColumn := a[colOff]
bColumn := b[colOff]
cmp, err := aColumn.CompareDatum(sc, bColumn)
cmp, err := aColumn.CompareDatum(sc, &bColumn)
if err != nil {
return 0, errors.Trace(err)
}
Expand Down
2 changes: 1 addition & 1 deletion executor/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func updateRecord(ctx context.Context, h int64, oldData, newData []types.Datum,
}
t.RebaseAutoID(val, true)
}
cmp, err := newData[i].CompareDatum(sc, oldData[i])
cmp, err := newData[i].CompareDatum(sc, &oldData[i])
if err != nil {
return false, errors.Trace(err)
}
Expand Down
2 changes: 1 addition & 1 deletion expression/aggregation/max_min.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (mmf *maxMinFunction) Update(ctx *AggEvaluateContext, sc *variable.Statemen
return nil
}
var c int
c, err = ctx.Value.CompareDatum(sc, value)
c, err = ctx.Value.CompareDatum(sc, &value)
if err != nil {
return errors.Trace(err)
}
Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ func refineConstantArg(con *Constant, op opcode.Op, ctx context.Context) *Consta
return con
}
datumInt := types.NewIntDatum(i64)
c, err := datumInt.CompareDatum(sc, con.Value)
c, err := datumInt.CompareDatum(sc, &con.Value)
if err != nil {
return con
}
Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_other_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (s *testEvaluatorSuite) TestValues(c *C) {
s.ctx.GetSessionVars().CurrInsertValues = currInsertValues
ret, err := sig.eval(nil)
c.Assert(err, IsNil)
cmp, err := ret.CompareDatum(nil, currInsertValues[1])
cmp, err := ret.CompareDatum(nil, &currInsertValues[1])
c.Assert(err, IsNil)
c.Assert(cmp, Equals, 0)
}
2 changes: 1 addition & 1 deletion expression/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (c *Constant) Equal(b Expression, ctx context.Context) bool {
if !ok {
return false
}
con, err := c.Value.CompareDatum(ctx.GetSessionVars().StmtCtx, y.Value)
con, err := c.Value.CompareDatum(ctx.GetSessionVars().StmtCtx, &y.Value)
if err != nil || con != 0 {
return false
}
Expand Down
4 changes: 2 additions & 2 deletions expression/distsql_builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (s *testEvalSuite) TestEval(c *C) {
result, err := expr.Eval(row)
c.Assert(err, IsNil)
c.Assert(result.Kind(), Equals, tt.result.Kind())
cmp, err := result.CompareDatum(sc, tt.result)
cmp, err := result.CompareDatum(sc, &tt.result)
c.Assert(err, IsNil)
c.Assert(cmp, Equals, 0)
}
Expand Down Expand Up @@ -379,7 +379,7 @@ func (s *testEvalSuite) TestEvalIsNull(c *C) {
result, err := expr.Eval(nil)
c.Assert(err, IsNil, Commentf("%v", tt))
c.Assert(result.Kind(), Equals, tt.result.Kind(), Commentf("%v", tt))
cmp, err := result.CompareDatum(sc, tt.result)
cmp, err := result.CompareDatum(sc, &tt.result)
c.Assert(err, IsNil, Commentf("%v", tt))
c.Assert(cmp, Equals, 0, Commentf("%v", tt))
}
Expand Down
3 changes: 2 additions & 1 deletion expression/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,8 @@ func (s *testEvaluatorSuite) TestUnaryOp(c *C) {
result, err := f.eval(nil)
c.Assert(err, IsNil)

ret, err := result.CompareDatum(s.ctx.GetSessionVars().StmtCtx, types.NewDatum(t.result))
expect := types.NewDatum(t.result)
ret, err := result.CompareDatum(s.ctx.GetSessionVars().StmtCtx, &expect)
c.Assert(err, IsNil)
c.Assert(ret, Equals, 0, Commentf("%v %s", t.arg, t.op))
}
Expand Down
4 changes: 2 additions & 2 deletions statistics/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (b *SortedBuilder) Hist() *Histogram {

// Iterate updates the histogram incrementally.
func (b *SortedBuilder) Iterate(data types.Datum) error {
cmp, err := b.hist.Buckets[b.bucketIdx].UpperBound.CompareDatum(b.sc, data)
cmp, err := b.hist.Buckets[b.bucketIdx].UpperBound.CompareDatum(b.sc, &data)
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func BuildColumn(ctx context.Context, numBuckets, id int64, collector *SampleCol
var lastCount int64
hg.Buckets[0].LowerBound = samples[0]
for i := int64(0); i < int64(len(samples)); i++ {
cmp, err := hg.Buckets[bucketIdx].UpperBound.CompareDatum(sc, samples[i])
cmp, err := hg.Buckets[bucketIdx].UpperBound.CompareDatum(sc, &samples[i])
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
10 changes: 5 additions & 5 deletions statistics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (hg *Histogram) equalRowCount(sc *variable.StatementContext, value types.Da
if match {
return float64(hg.Buckets[index].Repeats), nil
}
c, err := value.CompareDatum(sc, hg.Buckets[index].LowerBound)
c, err := value.CompareDatum(sc, &hg.Buckets[index].LowerBound)
if err != nil {
return 0, errors.Trace(err)
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func (hg *Histogram) lessRowCount(sc *variable.StatementContext, value types.Dat
if match {
return lessThanBucketValueCount, nil
}
c, err := value.CompareDatum(sc, hg.Buckets[index].LowerBound)
c, err := value.CompareDatum(sc, &hg.Buckets[index].LowerBound)
if err != nil {
return 0, errors.Trace(err)
}
Expand Down Expand Up @@ -295,7 +295,7 @@ func (hg *Histogram) inBucketBetweenCount() float64 {

func (hg *Histogram) lowerBound(sc *variable.StatementContext, target types.Datum) (index int, match bool, err error) {
index = sort.Search(len(hg.Buckets), func(i int) bool {
cmp, err1 := hg.Buckets[i].UpperBound.CompareDatum(sc, target)
cmp, err1 := hg.Buckets[i].UpperBound.CompareDatum(sc, &target)
if err1 != nil {
err = errors.Trace(err1)
return false
Expand Down Expand Up @@ -385,7 +385,7 @@ func MergeHistograms(sc *variable.StatementContext, lh *Histogram, rh *Histogram
}
lh.NDV += rh.NDV
lLen := len(lh.Buckets)
cmp, err := lh.Buckets[lLen-1].UpperBound.CompareDatum(sc, rh.Buckets[0].LowerBound)
cmp, err := lh.Buckets[lLen-1].UpperBound.CompareDatum(sc, &rh.Buckets[0].LowerBound)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down Expand Up @@ -477,7 +477,7 @@ func (c *Column) getIntColumnRowCount(sc *variable.StatementContext, intRanges [
func (c *Column) getColumnRowCount(sc *variable.StatementContext, ranges []*types.ColumnRange) (float64, error) {
var rowCount float64
for _, rg := range ranges {
cmp, err := rg.Low.CompareDatum(sc, rg.High)
cmp, err := rg.Low.CompareDatum(sc, &rg.High)
if err != nil {
return 0, errors.Trace(err)
}
Expand Down
6 changes: 4 additions & 2 deletions statistics/statistics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,12 @@ func (s *testStatisticsSuite) TestMergeHistogram(c *C) {
c.Assert(h.NDV, Equals, t.ndv)
c.Assert(len(h.Buckets), Equals, t.bucketNum)
c.Assert(h.Buckets[len(h.Buckets)-1].Count, Equals, t.leftNum+t.rightNum)
cmp, err := h.Buckets[0].LowerBound.CompareDatum(sc, types.NewIntDatum(t.leftLower))
expectLower := types.NewIntDatum(t.leftLower)
cmp, err := h.Buckets[0].LowerBound.CompareDatum(sc, &expectLower)
c.Assert(err, IsNil)
c.Assert(cmp, Equals, 0)
cmp, err = h.Buckets[len(h.Buckets)-1].UpperBound.CompareDatum(sc, types.NewIntDatum(t.rightLower+t.rightNum-1))
expectUpper := types.NewIntDatum(t.rightLower + t.rightNum - 1)
cmp, err = h.Buckets[len(h.Buckets)-1].UpperBound.CompareDatum(sc, &expectUpper)
c.Assert(err, IsNil)
c.Assert(cmp, Equals, 0)
}
Expand Down
2 changes: 1 addition & 1 deletion statistics/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func getPseudoRowCountByColumnRanges(sc *variable.StatementContext, tableRowCoun
} else if ran.High.Kind() == types.KindMaxValue {
rowCount += tableRowCount / pseudoLessRate
} else {
compare, err1 := ran.Low.CompareDatum(sc, ran.High)
compare, err1 := ran.Low.CompareDatum(sc, &ran.High)
if err1 != nil {
return 0, errors.Trace(err1)
}
Expand Down
2 changes: 1 addition & 1 deletion store/localstore/local_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (n *aggregateFuncExpr) updateMaxMin(ctx *selectContext, args []types.Datum,
aggItem.value = arg
return nil
}
c, err := aggItem.value.CompareDatum(ctx.sc, arg)
c, err := aggItem.value.CompareDatum(ctx.sc, &arg)
if err != nil {
return errors.Trace(err)
}
Expand Down
4 changes: 2 additions & 2 deletions store/localstore/local_region.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (t *topnSorter) Less(i, j int) bool {
v1 := t.rows[i].key[index]
v2 := t.rows[j].key[index]

ret, err := v1.CompareDatum(t.ctx.sc, v2)
ret, err := v1.CompareDatum(t.ctx.sc, &v2)
if err != nil {
t.err = errors.Trace(err)
return true
Expand Down Expand Up @@ -123,7 +123,7 @@ func (t *topnHeap) Less(i, j int) bool {
v1 := t.rows[i].key[index]
v2 := t.rows[j].key[index]

ret, err := v1.CompareDatum(t.ctx.sc, v2)
ret, err := v1.CompareDatum(t.ctx.sc, &v2)
if err != nil {
t.err = errors.Trace(err)
return true
Expand Down
Loading

0 comments on commit 6f84392

Please sign in to comment.