Skip to content

Commit

Permalink
*: fix a bug that collation is not handle for text type (#23045) (#23092
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ti-srebot authored Mar 12, 2021
1 parent 5edebf9 commit 8b9ed63
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 29 deletions.
14 changes: 14 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7200,6 +7200,20 @@ func (s *testIntegrationSerialSuite) TestIssue18702(c *C) {
tk.MustQuery("SELECT * FROM t FORCE INDEX(idx_bc);").Check(testkit.Rows("1 A 10 1", "2 B 20 1"))
}

func (s *testIntegrationSerialSuite) TestCollationText(c *C) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a TINYTEXT collate UTF8MB4_GENERAL_CI, UNIQUE KEY `a`(`a`(10)));")
tk.MustExec("insert into t (a) values ('A');")
tk.MustQuery("select * from t t1 inner join t t2 on t1.a = t2.a where t1.a = 'A';").Check(testkit.Rows("A A"))
tk.MustExec("update t set a = 'B';")
tk.MustExec("admin check table t;")
}

func (s *testIntegrationSuite) TestIssue18850(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
4 changes: 2 additions & 2 deletions session/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *testBootstrapSuite) TestBootstrap(c *C) {
c.Assert(err, IsNil)
c.Assert(req.NumRows() == 0, IsFalse)
datums := statistics.RowToDatums(req.GetRow(0), r.Fields())
match(c, datums, `%`, "root", []byte(""), "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "N", "Y", "Y", "Y", "Y")
match(c, datums, `%`, "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "N", "Y", "Y", "Y", "Y")

c.Assert(se.Auth(&auth.UserIdentity{Username: "root", Hostname: "anyhost"}, []byte(""), []byte("")), IsTrue)
mustExecSQL(c, se, "USE test;")
Expand Down Expand Up @@ -159,7 +159,7 @@ func (s *testBootstrapSuite) TestBootstrapWithError(c *C) {
c.Assert(req.NumRows() == 0, IsFalse)
row := req.GetRow(0)
datums := statistics.RowToDatums(row, r.Fields())
match(c, datums, `%`, "root", []byte(""), "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "N", "Y", "Y", "Y", "Y")
match(c, datums, `%`, "root", "", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "N", "Y", "Y", "Y", "Y")
c.Assert(r.Close(), IsNil)

mustExecSQL(c, se, "USE test;")
Expand Down
4 changes: 1 addition & 3 deletions table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,8 @@ func GetZeroValue(col *model.ColumnInfo) types.Datum {
} else {
d.SetString("", col.Collate)
}
case mysql.TypeVarString, mysql.TypeVarchar:
case mysql.TypeVarString, mysql.TypeVarchar, mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
d.SetString("", col.Collate)
case mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
d.SetBytes([]byte{})
case mysql.TypeDuration:
d.SetMysqlDuration(types.ZeroDuration)
case mysql.TypeDate:
Expand Down
2 changes: 1 addition & 1 deletion table/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (t *testTableSuite) TestGetZeroValue(c *C) {
},
{
types.NewFieldType(mysql.TypeBlob),
types.NewBytesDatum([]byte{}),
types.NewStringDatum(""),
},
{
types.NewFieldType(mysql.TypeDuration),
Expand Down
12 changes: 2 additions & 10 deletions types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -2136,14 +2136,10 @@ func GetMaxValue(ft *FieldType) (max Datum) {
max.SetFloat32(float32(GetMaxFloat(ft.Flen, ft.Decimal)))
case mysql.TypeDouble:
max.SetFloat64(GetMaxFloat(ft.Flen, ft.Decimal))
case mysql.TypeString, mysql.TypeVarString, mysql.TypeVarchar:
case mysql.TypeString, mysql.TypeVarString, mysql.TypeVarchar, mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
// codec.Encode KindMaxValue, to avoid import circle
bytes := []byte{250}
max.SetString(string(bytes), ft.Collate)
case mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
// codec.Encode KindMaxValue, to avoid import circle
bytes := []byte{250}
max.SetBytes(bytes)
case mysql.TypeNewDecimal:
max.SetMysqlDecimal(NewMaxOrMinDec(false, ft.Flen, ft.Decimal))
case mysql.TypeDuration:
Expand Down Expand Up @@ -2171,14 +2167,10 @@ func GetMinValue(ft *FieldType) (min Datum) {
min.SetFloat32(float32(-GetMaxFloat(ft.Flen, ft.Decimal)))
case mysql.TypeDouble:
min.SetFloat64(-GetMaxFloat(ft.Flen, ft.Decimal))
case mysql.TypeString, mysql.TypeVarString, mysql.TypeVarchar:
case mysql.TypeString, mysql.TypeVarString, mysql.TypeVarchar, mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
// codec.Encode KindMinNotNull, to avoid import circle
bytes := []byte{1}
min.SetString(string(bytes), ft.Collate)
case mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
// codec.Encode KindMinNotNull, to avoid import circle
bytes := []byte{1}
min.SetBytes(bytes)
case mysql.TypeNewDecimal:
min.SetMysqlDecimal(NewMaxOrMinDec(true, ft.Flen, ft.Decimal))
case mysql.TypeDuration:
Expand Down
6 changes: 1 addition & 5 deletions util/chunk/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,10 @@ func (r Row) GetDatum(colIdx int, tp *types.FieldType) types.Datum {
if !r.IsNull(colIdx) {
d.SetFloat64(r.GetFloat64(colIdx))
}
case mysql.TypeVarchar, mysql.TypeVarString, mysql.TypeString:
case mysql.TypeVarchar, mysql.TypeVarString, mysql.TypeString, mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
if !r.IsNull(colIdx) {
d.SetString(r.GetString(colIdx), tp.Collate)
}
case mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
if !r.IsNull(colIdx) {
d.SetBytes(r.GetBytes(colIdx))
}
case mysql.TypeDate, mysql.TypeDatetime, mysql.TypeTimestamp:
if !r.IsNull(colIdx) {
d.SetMysqlTime(r.GetTime(colIdx))
Expand Down
4 changes: 1 addition & 3 deletions util/rowcodec/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,8 @@ func (decoder *DatumMapDecoder) decodeColDatum(col *ColInfo, colData []byte) (ty
return d, err
}
d.SetFloat64(fVal)
case mysql.TypeVarString, mysql.TypeVarchar, mysql.TypeString:
case mysql.TypeVarString, mysql.TypeVarchar, mysql.TypeString, mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
d.SetString(string(colData), col.Collate)
case mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
d.SetBytes(colData)
case mysql.TypeNewDecimal:
_, dec, precision, frac, err := codec.DecodeDecimal(colData)
if err != nil {
Expand Down
12 changes: 7 additions & 5 deletions util/rowcodec/rowcodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ func (s *testSuite) TestTypesNewRowCodec(c *C) {
c.Assert(len(remain), Equals, 0)
if d.Kind() == types.KindMysqlDecimal {
c.Assert(d.GetMysqlDecimal(), DeepEquals, t.bt.GetMysqlDecimal())
} else if d.Kind() == types.KindBytes {
c.Assert(d.GetBytes(), DeepEquals, t.bt.GetBytes())
} else {
c.Assert(d, DeepEquals, t.bt)
}
Expand Down Expand Up @@ -341,9 +343,9 @@ func (s *testSuite) TestTypesNewRowCodec(c *C) {
},
{
24,
types.NewFieldType(mysql.TypeBlob),
types.NewBytesDatum([]byte("abc")),
types.NewBytesDatum([]byte("abc")),
types.NewFieldTypeWithCollation(mysql.TypeBlob, mysql.DefaultCollationName, types.UnspecifiedLength),
types.NewStringDatum("abc"),
types.NewStringDatum("abc"),
nil,
false,
},
Expand Down Expand Up @@ -470,8 +472,8 @@ func (s *testSuite) TestTypesNewRowCodec(c *C) {
testData[0].id = 1

// test large data
testData[3].dt = types.NewBytesDatum([]byte(strings.Repeat("a", math.MaxUint16+1)))
testData[3].bt = types.NewBytesDatum([]byte(strings.Repeat("a", math.MaxUint16+1)))
testData[3].dt = types.NewStringDatum(strings.Repeat("a", math.MaxUint16+1))
testData[3].bt = types.NewStringDatum(strings.Repeat("a", math.MaxUint16+1))
encodeAndDecode(c, testData)
}

Expand Down

0 comments on commit 8b9ed63

Please sign in to comment.