Skip to content

Commit

Permalink
This is an automated cherry-pick of #46433
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <[email protected]>
  • Loading branch information
hawkingrei authored and ti-chi-bot committed Jun 4, 2024
1 parent e1067d1 commit 4ec740d
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 21 deletions.
1 change: 0 additions & 1 deletion br/pkg/lightning/checkpoints/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ go_library(
"//util/sqlexec",
"@com_github_joho_sqltocsv//:sqltocsv",
"@com_github_pingcap_errors//:errors",
"@org_golang_x_exp//slices",
"@org_uber_go_zap//:zap",
],
)
Expand Down
14 changes: 11 additions & 3 deletions br/pkg/lightning/checkpoints/checkpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
package checkpoints

import (
"cmp"
"context"
"database/sql"
"encoding/json"
"fmt"
"io"
"math"
"path"
"slices"
"sort"
"strings"
"sync"
Expand All @@ -38,7 +40,6 @@ import (
"github.com/pingcap/tidb/br/pkg/version/build"
"github.com/pingcap/tidb/util/mathutil"
"go.uber.org/zap"
"golang.org/x/exp/slices"
)

type CheckpointStatus uint8
Expand Down Expand Up @@ -229,6 +230,13 @@ func (key *ChunkCheckpointKey) String() string {
return fmt.Sprintf("%s:%d", key.Path, key.Offset)
}

func (key *ChunkCheckpointKey) compare(other *ChunkCheckpointKey) int {
if c := cmp.Compare(key.Path, other.Path); c != 0 {
return c
}
return cmp.Compare(key.Offset, other.Offset)
}

func (key *ChunkCheckpointKey) less(other *ChunkCheckpointKey) bool {
switch {
case key.Path < other.Path:
Expand Down Expand Up @@ -1257,8 +1265,8 @@ func (cpdb *FileCheckpointsDB) Get(_ context.Context, tableName string) (*TableC
})
}

slices.SortFunc(engine.Chunks, func(i, j *ChunkCheckpoint) bool {
return i.Key.less(&j.Key)
slices.SortFunc(engine.Chunks, func(i, j *ChunkCheckpoint) int {
return i.Key.compare(&j.Key)
})

cp.Engines[engineID] = engine
Expand Down
3 changes: 3 additions & 0 deletions executor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,11 @@ go_library(
"@org_golang_google_grpc//codes",
"@org_golang_google_grpc//credentials",
"@org_golang_google_grpc//status",
<<<<<<< HEAD
"@org_golang_x_exp//maps",
"@org_golang_x_exp//slices",
=======
>>>>>>> c11a9992882 (*: use std/slices to replace exp/slices (#46433))
"@org_golang_x_sync//errgroup",
"@org_uber_go_atomic//:atomic",
"@org_uber_go_zap//:zap",
Expand Down
25 changes: 14 additions & 11 deletions executor/inspection_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package executor

import (
"cmp"
"context"
"fmt"
"math"
"slices"
"strconv"
"strings"

Expand All @@ -34,7 +36,6 @@ import (
"github.com/pingcap/tidb/util/set"
"github.com/pingcap/tidb/util/size"
"github.com/pingcap/tidb/util/sqlexec"
"golang.org/x/exp/slices"
)

type (
Expand Down Expand Up @@ -169,20 +170,22 @@ func (e *inspectionResultRetriever) retrieve(ctx context.Context, sctx sessionct
continue
}
// make result stable
slices.SortFunc(results, func(i, j inspectionResult) bool {
if i.degree != j.degree {
return i.degree > j.degree
slices.SortFunc(results, func(i, j inspectionResult) int {
if c := cmp.Compare(i.degree, j.degree); c != 0 {
return -c
}
if lhs, rhs := i.item, j.item; lhs != rhs {
return lhs < rhs
// lhs and rhs
if c := cmp.Compare(i.item, j.item); c != 0 {
return c
}
if i.actual != j.actual {
return i.actual < j.actual
if c := cmp.Compare(i.actual, j.actual); c != 0 {
return c
}
if lhs, rhs := i.tp, j.tp; lhs != rhs {
return lhs < rhs
// lhs and rhs
if c := cmp.Compare(i.tp, j.tp); c != 0 {
return c
}
return i.instance < j.instance
return cmp.Compare(i.instance, j.instance)
})
for _, result := range results {
if len(result.instance) == 0 {
Expand Down
51 changes: 51 additions & 0 deletions executor/mem_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package executor

import (
"context"
"slices"

"github.com/opentracing/opentracing-go"
"github.com/pingcap/errors"
Expand All @@ -34,6 +35,10 @@ import (
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/rowcodec"
<<<<<<< HEAD
=======
"github.com/pingcap/tidb/util/tracing"
>>>>>>> c11a9992882 (*: use std/slices to replace exp/slices (#46433))
)

type memReader interface {
Expand Down Expand Up @@ -137,9 +142,22 @@ func (m *memIndexReader) getMemRows(ctx context.Context) ([][]types.Datum, error
if err != nil {
return nil, err
}
<<<<<<< HEAD
// TODO: After refine `IterReverse`, remove below logic and use `IterReverse` when do reverse scan.
if m.desc {
reverseDatumSlice(m.addedRows)
=======

if m.keepOrder && m.table.GetPartitionInfo() != nil {
slices.SortFunc(m.addedRows, func(a, b []types.Datum) int {
ret, err1 := m.compare(m.ctx.GetSessionVars().StmtCtx, a, b)
if err1 != nil {
err = err1
}
return ret
})
return m.addedRows, err
>>>>>>> c11a9992882 (*: use std/slices to replace exp/slices (#46433))
}
return m.addedRows, nil
}
Expand Down Expand Up @@ -278,9 +296,21 @@ func (m *memTableReader) getMemRows(ctx context.Context) ([][]types.Datum, error
return nil, err
}

<<<<<<< HEAD
// TODO: After refine `IterReverse`, remove below logic and use `IterReverse` when do reverse scan.
if m.desc {
reverseDatumSlice(m.addedRows)
=======
if m.keepOrder && m.table.GetPartitionInfo() != nil {
slices.SortFunc(m.addedRows, func(a, b []types.Datum) int {
ret, err1 := m.compare(m.ctx.GetSessionVars().StmtCtx, a, b)
if err1 != nil {
err = err1
}
return ret
})
return m.addedRows, err
>>>>>>> c11a9992882 (*: use std/slices to replace exp/slices (#46433))
}
return m.addedRows, nil
}
Expand Down Expand Up @@ -736,7 +766,28 @@ func (m *memIndexMergeReader) getMemRows(ctx context.Context) ([][]types.Datum,
},
}

<<<<<<< HEAD
return memTblReader.getMemRows(ctx)
=======
rows, err := memTblReader.getMemRows(ctx)
if err != nil {
return nil, err
}

// Didn't set keepOrder = true for memTblReader,
// In indexMerge, non-partitioned tables are also need reordered.
if m.keepOrder {
slices.SortFunc(rows, func(a, b []types.Datum) int {
ret, err1 := m.compare(m.ctx.GetSessionVars().StmtCtx, a, b)
if err1 != nil {
err = err1
}
return ret
})
}

return rows, err
>>>>>>> c11a9992882 (*: use std/slices to replace exp/slices (#46433))
}

// Union all handles of all partial paths.
Expand Down
33 changes: 33 additions & 0 deletions executor/union_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,15 @@ func (us *UnionScanExec) getOneRow(ctx context.Context) ([]types.Datum, error) {
} else if snapshotRow == nil {
row = addedRow
} else {
<<<<<<< HEAD
isSnapshotRow, err = us.shouldPickFirstRow(snapshotRow, addedRow)
=======
isSnapshotRowInt, err := us.compare(us.Ctx().GetSessionVars().StmtCtx, snapshotRow, addedRow)
>>>>>>> c11a9992882 (*: use std/slices to replace exp/slices (#46433))
if err != nil {
return nil, err
}
isSnapshotRow = isSnapshotRowInt < 0
if isSnapshotRow {
row = snapshotRow
} else {
Expand Down Expand Up @@ -300,9 +305,24 @@ func (us *UnionScanExec) shouldPickFirstRow(a, b []types.Datum) (bool, error) {
return isFirstRow, nil
}

<<<<<<< HEAD
func (us *UnionScanExec) compare(a, b []types.Datum) (int, error) {
sc := us.ctx.GetSessionVars().StmtCtx
for _, colOff := range us.usedIndex {
=======
type compareExec struct {
collators []collate.Collator
// usedIndex is the column offsets of the index which Src executor has used.
usedIndex []int
desc bool
// handleCols is the handle's position of the below scan plan.
handleCols plannercore.HandleCols
}

func (ce compareExec) compare(sctx *stmtctx.StatementContext, a, b []types.Datum) (ret int, err error) {
var cmp int
for _, colOff := range ce.usedIndex {
>>>>>>> c11a9992882 (*: use std/slices to replace exp/slices (#46433))
aColumn := a[colOff]
bColumn := b[colOff]
cmp, err := aColumn.Compare(sc, &bColumn, us.collators[colOff])
Expand All @@ -312,6 +332,19 @@ func (us *UnionScanExec) compare(a, b []types.Datum) (int, error) {
if cmp != 0 {
return cmp, nil
}
<<<<<<< HEAD
}
return us.belowHandleCols.Compare(a, b, us.collators)
=======
if ce.desc {
return -cmp, nil
}
return cmp, nil
}
cmp, err = ce.handleCols.Compare(a, b, ce.collators)
if ce.desc {
return -cmp, err
}
return cmp, err
>>>>>>> c11a9992882 (*: use std/slices to replace exp/slices (#46433))
}
Loading

0 comments on commit 4ec740d

Please sign in to comment.