Skip to content

Commit

Permalink
cherry pick #19259 to release-4.0 (#19403)
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>

Co-authored-by: Kenan Yao <cauchy1992@gmail.com>
  • Loading branch information
ti-srebot and eurekaka authored Aug 25, 2020
1 parent 3fe0255 commit 3f0740e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -772,3 +772,16 @@ label = "cop"
}

drop table if exists t;
create table t(a binary(16) not null, b varchar(2) default null, c varchar(100) default 'aaaa', key (a,b));
explain select * from t where a=x'FA34E1093CB428485734E3917F000000' and b='xb';
id estRows task access object operator info
IndexLookUp_10 0.10 root
├─IndexRangeScan_8(Build) 0.10 cop[tikv] table:t, index:a(a, b) range:["[250 52 225 9 60 180 40 72 87 52 227 145 127 0 0 0]" "xb","[250 52 225 9 60 180 40 72 87 52 227 145 127 0 0 0]" "xb"], keep order:false, stats:pseudo
└─TableRowIDScan_9(Probe) 0.10 cop[tikv] table:t keep order:false, stats:pseudo
explain update t set c = 'ssss' where a=x'FA34E1093CB428485734E3917F000000' and b='xb';
id estRows task access object operator info
Update_4 N/A root N/A
└─IndexLookUp_11 0.10 root
├─IndexRangeScan_9(Build) 0.10 cop[tikv] table:t, index:a(a, b) range:["[250 52 225 9 60 180 40 72 87 52 227 145 127 0 0 0]" "xb","[250 52 225 9 60 180 40 72 87 52 227 145 127 0 0 0]" "xb"], keep order:false, stats:pseudo
└─TableRowIDScan_10(Probe) 0.10 cop[tikv] table:t keep order:false, stats:pseudo
drop table if exists t;
6 changes: 6 additions & 0 deletions cmd/explaintest/t/explain_easy.test
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,9 @@ drop table if exists t;
create table t(a int, b int);
explain format="dot" select * from t where a < 2;
drop table if exists t;

# select / update should choose same access path for table t.
create table t(a binary(16) not null, b varchar(2) default null, c varchar(100) default 'aaaa', key (a,b));
explain select * from t where a=x'FA34E1093CB428485734E3917F000000' and b='xb';
explain update t set c = 'ssss' where a=x'FA34E1093CB428485734E3917F000000' and b='xb';
drop table if exists t;
20 changes: 20 additions & 0 deletions types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,10 @@ func (d *Datum) compareMysqlDecimal(sc *stmtctx.StatementContext, dec *MyDecimal

func (d *Datum) compareMysqlDuration(sc *stmtctx.StatementContext, dur Duration) (int, error) {
switch d.k {
case KindNull, KindMinNotNull:
return -1, nil
case KindMaxValue:
return 1, nil
case KindMysqlDuration:
return d.GetMysqlDuration().Compare(dur), nil
case KindString, KindBytes:
Expand All @@ -720,6 +724,10 @@ func (d *Datum) compareMysqlDuration(sc *stmtctx.StatementContext, dur Duration)

func (d *Datum) compareMysqlEnum(sc *stmtctx.StatementContext, enum Enum) (int, error) {
switch d.k {
case KindNull, KindMinNotNull:
return -1, nil
case KindMaxValue:
return 1, nil
case KindString, KindBytes, KindMysqlEnum, KindMysqlSet:
return CompareString(d.GetString(), enum.String(), d.collation), nil
default:
Expand All @@ -729,6 +737,10 @@ func (d *Datum) compareMysqlEnum(sc *stmtctx.StatementContext, enum Enum) (int,

func (d *Datum) compareBinaryLiteral(sc *stmtctx.StatementContext, b BinaryLiteral) (int, error) {
switch d.k {
case KindNull, KindMinNotNull:
return -1, nil
case KindMaxValue:
return 1, nil
case KindString, KindBytes:
return CompareString(d.GetString(), b.ToString(), d.collation), nil
case KindBinaryLiteral, KindMysqlBit:
Expand All @@ -745,6 +757,10 @@ func (d *Datum) compareBinaryLiteral(sc *stmtctx.StatementContext, b BinaryLiter

func (d *Datum) compareMysqlSet(sc *stmtctx.StatementContext, set Set) (int, error) {
switch d.k {
case KindNull, KindMinNotNull:
return -1, nil
case KindMaxValue:
return 1, nil
case KindString, KindBytes, KindMysqlEnum, KindMysqlSet:
return CompareString(d.GetString(), set.String(), d.collation), nil
default:
Expand All @@ -762,6 +778,10 @@ func (d *Datum) compareMysqlJSON(sc *stmtctx.StatementContext, target json.Binar

func (d *Datum) compareMysqlTime(sc *stmtctx.StatementContext, time Time) (int, error) {
switch d.k {
case KindNull, KindMinNotNull:
return -1, nil
case KindMaxValue:
return 1, nil
case KindString, KindBytes:
dt, err := ParseDatetime(sc, d.GetString())
return dt.Compare(time), errors.Trace(err)
Expand Down

0 comments on commit 3f0740e

Please sign in to comment.