Skip to content

Commit

Permalink
*: code preparations for supporting build range for like function on …
Browse files Browse the repository at this point in the history
…new collation columns (pingcap#48972)

ref pingcap#48181
  • Loading branch information
time-and-fate committed Dec 5, 2023
1 parent 93b8c27 commit b9f04e1
Show file tree
Hide file tree
Showing 10 changed files with 1,344 additions and 65 deletions.
18 changes: 9 additions & 9 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ explain format = 'brief' delete from t1 where t1.c2 = 1;
id estRows task access object operator info
Delete N/A root N/A
└─SelectLock 10.00 root for update 0
└─IndexLookUp 10.00 root
└─IndexLookUp 10.00 root
├─IndexRangeScan(Build) 10.00 cop[tikv] table:t1, index:c2(c2) range:[1,1], keep order:false, stats:pseudo
└─TableRowIDScan(Probe) 10.00 cop[tikv] table:t1 keep order:false, stats:pseudo
explain format = 'brief' select count(b.c2) from t1 a, t2 b where a.c1 = b.c2 group by a.c1;
Expand Down Expand Up @@ -174,11 +174,11 @@ Union 26000.00 root
│ └─Union 16000.00 root
│ ├─StreamAgg 8000.00 root group by:test.t2.c1, funcs:firstrow(test.t2.c1)->Column#12, funcs:firstrow(test.t2.c1)->Column#10
│ │ └─IndexReader 8000.00 root index:StreamAgg
│ │ └─StreamAgg 8000.00 cop[tikv] group by:test.t2.c1,
│ │ └─StreamAgg 8000.00 cop[tikv] group by:test.t2.c1,
│ │ └─IndexFullScan 10000.00 cop[tikv] table:t2, index:c1(c1) keep order:true, stats:pseudo
│ └─StreamAgg 8000.00 root group by:test.t2.c1, funcs:firstrow(test.t2.c1)->Column#12, funcs:firstrow(test.t2.c1)->Column#10
│ └─IndexReader 8000.00 root index:StreamAgg
│ └─StreamAgg 8000.00 cop[tikv] group by:test.t2.c1,
│ └─StreamAgg 8000.00 cop[tikv] group by:test.t2.c1,
│ └─IndexFullScan 10000.00 cop[tikv] table:t2, index:c1(c1) keep order:true, stats:pseudo
└─IndexReader 10000.00 root index:IndexFullScan
└─IndexFullScan 10000.00 cop[tikv] table:t2, index:c1(c1) keep order:false, stats:pseudo
Expand All @@ -188,15 +188,15 @@ HashAgg 24000.00 root group by:Column#10, funcs:firstrow(Column#11)->Column#10
└─Union 24000.00 root
├─StreamAgg 8000.00 root group by:test.t2.c1, funcs:firstrow(test.t2.c1)->Column#11, funcs:firstrow(test.t2.c1)->Column#10
│ └─IndexReader 8000.00 root index:StreamAgg
│ └─StreamAgg 8000.00 cop[tikv] group by:test.t2.c1,
│ └─StreamAgg 8000.00 cop[tikv] group by:test.t2.c1,
│ └─IndexFullScan 10000.00 cop[tikv] table:t2, index:c1(c1) keep order:true, stats:pseudo
├─StreamAgg 8000.00 root group by:test.t2.c1, funcs:firstrow(test.t2.c1)->Column#11, funcs:firstrow(test.t2.c1)->Column#10
│ └─IndexReader 8000.00 root index:StreamAgg
│ └─StreamAgg 8000.00 cop[tikv] group by:test.t2.c1,
│ └─StreamAgg 8000.00 cop[tikv] group by:test.t2.c1,
│ └─IndexFullScan 10000.00 cop[tikv] table:t2, index:c1(c1) keep order:true, stats:pseudo
└─StreamAgg 8000.00 root group by:test.t2.c1, funcs:firstrow(test.t2.c1)->Column#11, funcs:firstrow(test.t2.c1)->Column#10
└─IndexReader 8000.00 root index:StreamAgg
└─StreamAgg 8000.00 cop[tikv] group by:test.t2.c1,
└─StreamAgg 8000.00 cop[tikv] group by:test.t2.c1,
└─IndexFullScan 10000.00 cop[tikv] table:t2, index:c1(c1) keep order:true, stats:pseudo
select * from information_schema.tidb_indexes where table_name='t4';
TABLE_SCHEMA TABLE_NAME NON_UNIQUE KEY_NAME SEQ_IN_INDEX COLUMN_NAME SUB_PART INDEX_COMMENT Expression INDEX_ID IS_VISIBLE CLUSTERED
Expand Down Expand Up @@ -797,14 +797,14 @@ create table t(a binary(16) not null, b varchar(2) default null, c varchar(100)
explain format = 'brief' select * from t where a=x'FA34E1093CB428485734E3917F000000' and b='xb';
id estRows task access object operator info
IndexLookUp 0.10 root
├─IndexRangeScan(Build) 0.10 cop[tikv] table:t, index:a(a, b) range:[0xFA34E1093CB428485734E3917F000000 "xb",0xFA34E1093CB428485734E3917F000000 "xb"], keep order:false, stats:pseudo
├─IndexRangeScan(Build) 0.10 cop[tikv] table:t, index:a(a, b) range:["\xfa4\xe1\t<\xb4(HW4\xe3\x91\x7f\x00\x00\x00" "xb","\xfa4\xe1\t<\xb4(HW4\xe3\x91\x7f\x00\x00\x00" "xb"], keep order:false, stats:pseudo
└─TableRowIDScan(Probe) 0.10 cop[tikv] table:t keep order:false, stats:pseudo
explain format = 'brief' update t set c = 'ssss' where a=x'FA34E1093CB428485734E3917F000000' and b='xb';
id estRows task access object operator info
Update N/A root N/A
└─SelectLock 0.10 root for update 0
└─IndexLookUp 0.10 root
├─IndexRangeScan(Build) 0.10 cop[tikv] table:t, index:a(a, b) range:[0xFA34E1093CB428485734E3917F000000 "xb",0xFA34E1093CB428485734E3917F000000 "xb"], keep order:false, stats:pseudo
└─IndexLookUp 0.10 root
├─IndexRangeScan(Build) 0.10 cop[tikv] table:t, index:a(a, b) range:["\xfa4\xe1\t<\xb4(HW4\xe3\x91\x7f\x00\x00\x00" "xb","\xfa4\xe1\t<\xb4(HW4\xe3\x91\x7f\x00\x00\x00" "xb"], keep order:false, stats:pseudo
└─TableRowIDScan(Probe) 0.10 cop[tikv] table:t keep order:false, stats:pseudo
drop table if exists t;
create table t(a int, b int);
Expand Down
6 changes: 4 additions & 2 deletions executor/point_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,11 +540,13 @@ func EncodeUniqueIndexValuesForKey(ctx sessionctx.Context, tblInfo *model.TableI
colInfo := tblInfo.Columns[idxInfo.Columns[i].Offset]
// table.CastValue will append 0x0 if the string value's length is smaller than the BINARY column's length.
// So we don't use CastValue for string value for now.
// TODO: merge two if branch.
// TODO: The first if branch should have been removed, because the functionality of set the collation of the datum
// have been moved to util/ranger (normal path) and getNameValuePairs/getPointGetValue (fast path). But this change
// will be cherry-picked to a hotfix, so we choose to be a bit conservative and keep this for now.
if colInfo.GetType() == mysql.TypeString || colInfo.GetType() == mysql.TypeVarString || colInfo.GetType() == mysql.TypeVarchar {
var str string
str, err = idxVals[i].ToString()
idxVals[i].SetString(str, colInfo.FieldType.GetCollate())
idxVals[i].SetString(str, idxVals[i].Collation())
} else if colInfo.GetType() == mysql.TypeEnum && (idxVals[i].Kind() == types.KindString || idxVals[i].Kind() == types.KindBytes || idxVals[i].Kind() == types.KindBinaryLiteral) {
var str string
var e types.Enum
Expand Down
15 changes: 14 additions & 1 deletion planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,15 @@ func getNameValuePairs(ctx sessionctx.Context, tbl *model.TableInfo, tblName mod
col := model.FindColumnInfo(tbl.Cols(), colName.Name.Name.L)
if col == nil { // Handling the case when the column is _tidb_rowid.
return append(nvPairs, nameValuePair{colName: colName.Name.Name.L, colFieldType: types.NewFieldType(mysql.TypeLonglong), value: d, con: con}), false
} else if col.GetType() == mysql.TypeString && col.GetCollate() == charset.CollationBin { // This type we needn't to pad `\0` in here.
}

// As in buildFromBinOp in util/ranger, when we build key from the expression to do range scan or point get on
// a string column, we should set the collation of the string datum to collation of the column.
if col.FieldType.EvalType() == types.ETString && (d.Kind() == types.KindString || d.Kind() == types.KindBinaryLiteral) {
d.SetString(d.GetString(), col.FieldType.GetCollate())
}

if col.GetType() == mysql.TypeString && col.GetCollate() == charset.CollationBin { // This type we needn't to pad `\0` in here.
return append(nvPairs, nameValuePair{colName: colName.Name.Name.L, colFieldType: &col.FieldType, value: d, con: con}), false
}
if !checkCanConvertInPointGet(col, d) {
Expand Down Expand Up @@ -1464,6 +1472,11 @@ func getPointGetValue(stmtCtx *stmtctx.StatementContext, col *model.ColumnInfo,
if !checkCanConvertInPointGet(col, *d) {
return nil
}
// As in buildFromBinOp in util/ranger, when we build key from the expression to do range scan or point get on
// a string column, we should set the collation of the string datum to collation of the column.
if col.FieldType.EvalType() == types.ETString && (d.Kind() == types.KindString || d.Kind() == types.KindBinaryLiteral) {
d.SetString(d.GetString(), col.FieldType.GetCollate())
}
dVal, err := d.ConvertTo(stmtCtx, &col.FieldType)
if err != nil {
return nil
Expand Down
Loading

0 comments on commit b9f04e1

Please sign in to comment.