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

*: replace compareDatum by compare, PR 10 #30456

Merged
merged 6 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2811,23 +2811,29 @@ func (b *executorBuilder) buildIndexLookUpJoin(v *plannercore.PhysicalIndexJoin)
}
innerKeyCols := make([]int, len(v.InnerJoinKeys))
innerKeyColIDs := make([]int64, len(v.InnerJoinKeys))
keyCollators := make([]collate.Collator, 0, len(v.InnerJoinKeys))
for i := 0; i < len(v.InnerJoinKeys); i++ {
innerKeyCols[i] = v.InnerJoinKeys[i].Index
innerKeyColIDs[i] = v.InnerJoinKeys[i].ID
keyCollators = append(keyCollators, collate.GetCollator(v.InnerJoinKeys[i].RetType.Collate))
}
e.outerCtx.keyCols = outerKeyCols
e.innerCtx.keyCols = innerKeyCols
e.innerCtx.keyColIDs = innerKeyColIDs
e.innerCtx.keyCollators = keyCollators

outerHashCols, innerHashCols := make([]int, len(v.OuterHashKeys)), make([]int, len(v.InnerHashKeys))
hashCollators := make([]collate.Collator, 0, len(v.InnerHashKeys))
for i := 0; i < len(v.OuterHashKeys); i++ {
outerHashCols[i] = v.OuterHashKeys[i].Index
}
for i := 0; i < len(v.InnerHashKeys); i++ {
innerHashCols[i] = v.InnerHashKeys[i].Index
hashCollators = append(hashCollators, collate.GetCollator(v.InnerHashKeys[i].RetType.Collate))
}
e.outerCtx.hashCols = outerHashCols
e.innerCtx.hashCols = innerHashCols
e.innerCtx.hashCollators = hashCollators

e.joinResult = newFirstChunk(e)
executorCounterIndexLookUpJoin.Inc()
Expand Down Expand Up @@ -2879,8 +2885,10 @@ func (b *executorBuilder) buildIndexLookUpMergeJoin(v *plannercore.PhysicalIndex
outerKeyCols[i] = v.OuterJoinKeys[i].Index
}
innerKeyCols := make([]int, len(v.InnerJoinKeys))
keyCollators := make([]collate.Collator, 0, len(v.InnerJoinKeys))
for i := 0; i < len(v.InnerJoinKeys); i++ {
innerKeyCols[i] = v.InnerJoinKeys[i].Index
keyCollators = append(keyCollators, collate.GetCollator(v.InnerJoinKeys[i].RetType.Collate))
}
executorCounterIndexLookUpJoin.Inc()

Expand All @@ -2899,6 +2907,7 @@ func (b *executorBuilder) buildIndexLookUpMergeJoin(v *plannercore.PhysicalIndex
rowTypes: innerTypes,
joinKeys: v.InnerJoinKeys,
keyCols: innerKeyCols,
keyCollators: keyCollators,
compareFuncs: v.CompareFuncs,
colLens: v.IdxColLens,
desc: v.Desc,
Expand Down
13 changes: 8 additions & 5 deletions executor/index_lookup_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/execdetails"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/memory"
Expand Down Expand Up @@ -101,8 +102,10 @@ type innerCtx struct {
rowTypes []*types.FieldType
keyCols []int
keyColIDs []int64 // the original ID in its table, used by dynamic partition pruning
keyCollators []collate.Collator
hashTypes []*types.FieldType
hashCols []int
hashCollators []collate.Collator
colLens []int
hasPrefixCol bool
}
Expand Down Expand Up @@ -629,7 +632,7 @@ func (iw *innerWorker) constructDatumLookupKey(task *lookUpJoinTask, chkIdx, row
}
return nil, nil, err
}
cmp, err := outerValue.CompareDatum(sc, &innerValue)
cmp, err := outerValue.Compare(sc, &innerValue, iw.hashCollators[i])
if err != nil {
return nil, nil, err
}
Expand All @@ -651,25 +654,25 @@ func (iw *innerWorker) sortAndDedupLookUpContents(lookUpContents []*indexJoinLoo
}
sc := iw.ctx.GetSessionVars().StmtCtx
sort.Slice(lookUpContents, func(i, j int) bool {
cmp := compareRow(sc, lookUpContents[i].keys, lookUpContents[j].keys)
cmp := compareRow(sc, lookUpContents[i].keys, lookUpContents[j].keys, iw.keyCollators)
if cmp != 0 || iw.nextColCompareFilters == nil {
return cmp < 0
}
return iw.nextColCompareFilters.CompareRow(lookUpContents[i].row, lookUpContents[j].row) < 0
})
deDupedLookupKeys := lookUpContents[:1]
for i := 1; i < len(lookUpContents); i++ {
cmp := compareRow(sc, lookUpContents[i].keys, lookUpContents[i-1].keys)
cmp := compareRow(sc, lookUpContents[i].keys, lookUpContents[i-1].keys, iw.keyCollators)
if cmp != 0 || (iw.nextColCompareFilters != nil && iw.nextColCompareFilters.CompareRow(lookUpContents[i].row, lookUpContents[i-1].row) != 0) {
deDupedLookupKeys = append(deDupedLookupKeys, lookUpContents[i])
}
}
return deDupedLookupKeys
}

func compareRow(sc *stmtctx.StatementContext, left, right []types.Datum) int {
func compareRow(sc *stmtctx.StatementContext, left, right []types.Datum, ctors []collate.Collator) int {
for idx := 0; idx < len(left); idx++ {
cmp, err := left[idx].CompareDatum(sc, &right[idx])
cmp, err := left[idx].Compare(sc, &right[idx], ctors[idx])
// We only compare rows with the same type, no error to return.
terror.Log(err)
if cmp > 0 {
Expand Down
6 changes: 4 additions & 2 deletions executor/index_lookup_merge_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/execdetails"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/memory"
Expand Down Expand Up @@ -88,6 +89,7 @@ type innerMergeCtx struct {
rowTypes []*types.FieldType
joinKeys []*expression.Column
keyCols []int
keyCollators []collate.Collator
compareFuncs []expression.CompareFunc
colLens []int
desc bool
Expand Down Expand Up @@ -697,7 +699,7 @@ func (imw *innerMergeWorker) constructDatumLookupKey(task *lookUpMergeJoinTask,
}
return nil, err
}
cmp, err := outerValue.CompareDatum(sc, &innerValue)
cmp, err := outerValue.Compare(sc, &innerValue, imw.keyCollators[i])
if err != nil {
return nil, err
}
Expand All @@ -717,7 +719,7 @@ func (imw *innerMergeWorker) dedupDatumLookUpKeys(lookUpContents []*indexJoinLoo
sc := imw.ctx.GetSessionVars().StmtCtx
deDupedLookUpContents := lookUpContents[:1]
for i := 1; i < len(lookUpContents); i++ {
cmp := compareRow(sc, lookUpContents[i].keys, lookUpContents[i-1].keys)
cmp := compareRow(sc, lookUpContents[i].keys, lookUpContents[i-1].keys, imw.keyCollators)
if cmp != 0 || (imw.nextColCompareFilters != nil && imw.nextColCompareFilters.CompareRow(lookUpContents[i].row, lookUpContents[i-1].row) != 0) {
deDupedLookUpContents = append(deDupedLookUpContents, lookUpContents[i])
}
Expand Down