Skip to content

Commit

Permalink
expression: fix wrong retType for reverse function (#30829)
Browse files Browse the repository at this point in the history
close #30809
  • Loading branch information
wjhuang2016 authored Dec 21, 2021
1 parent cbe7492 commit f1e5877
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
7 changes: 7 additions & 0 deletions cmd/explaintest/r/explain_generate_column_substitute.result
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,10 @@ a
select * from t004 ignore index (eidx) where timestampadd(microsecond, 1, a) = timestampadd(microsecond, 1, '2021-08-20');
a
2021-08-20
drop table if exists t;
create table t ( c_int int, c_str varchar(40) character set utf8 collate utf8_general_ci, primary key(c_int, c_str(9)) clustered, key idx((reverse(c_str))));
replace into t (c_int, c_str) values (9, "beautiful hermann");
select reverse(c_str) from t use index(idx);
reverse(c_str)
nnamreh lufituaeb
drop table t;
7 changes: 7 additions & 0 deletions cmd/explaintest/t/explain_generate_column_substitute.test
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,10 @@ select * from t004 where timestampadd(microsecond, 1, a) = timestampadd(microsec
alter table t004 add index eidx ((timestampadd(microsecond, 1, a)));
select * from t004 use index(eidx) where timestampadd(microsecond, 1, a) = timestampadd(microsecond, 1, '2021-08-20');
select * from t004 ignore index (eidx) where timestampadd(microsecond, 1, a) = timestampadd(microsecond, 1, '2021-08-20');

drop table if exists t;
create table t ( c_int int, c_str varchar(40) character set utf8 collate utf8_general_ci, primary key(c_int, c_str(9)) clustered, key idx((reverse(c_str))));
replace into t (c_int, c_str) values (9, "beautiful hermann");
select reverse(c_str) from t use index(idx);

drop table t;
5 changes: 5 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5360,6 +5360,11 @@ func buildHiddenColumnInfo(ctx sessionctx.Context, indexPartSpecifications []*as
Hidden: true,
FieldType: *expr.GetType(),
}
// Reset some flag, it may be caused by wrong type infer. But it's not easy to fix them all, so reset them here for safety.
colInfo.Flag &= ^mysql.PriKeyFlag
colInfo.Flag &= ^mysql.UniqueKeyFlag
colInfo.Flag &= ^mysql.AutoIncrementFlag

if colInfo.Tp == mysql.TypeDatetime || colInfo.Tp == mysql.TypeDate || colInfo.Tp == mysql.TypeTimestamp || colInfo.Tp == mysql.TypeDuration {
if colInfo.FieldType.Decimal == types.UnspecifiedLength {
colInfo.FieldType.Decimal = int(types.MaxFsp)
Expand Down
14 changes: 8 additions & 6 deletions expression/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,12 @@ func reverseRunes(origin []rune) []rune {

// SetBinFlagOrBinStr sets resTp to binary string if argTp is a binary string,
// if not, sets the binary flag of resTp to true if argTp has binary flag.
// We need to check if the tp is enum or set, if so, don't add binary flag directly unless it has binary flag.
func SetBinFlagOrBinStr(argTp *types.FieldType, resTp *types.FieldType) {
nonEnumOrSet := !(argTp.Tp == mysql.TypeEnum || argTp.Tp == mysql.TypeSet)
if types.IsBinaryStr(argTp) {
types.SetBinChsClnFlag(resTp)
} else if mysql.HasBinaryFlag(argTp.Flag) || !types.IsNonBinaryStr(argTp) {
} else if mysql.HasBinaryFlag(argTp.Flag) || (!types.IsNonBinaryStr(argTp) && nonEnumOrSet) {
resTp.Flag |= mysql.BinaryFlag
}
}
Expand Down Expand Up @@ -765,12 +767,12 @@ func (c *reverseFunctionClass) getFunction(ctx sessionctx.Context, args []Expres
if err != nil {
return nil, err
}
retTp := *args[0].GetType()
retTp.Tp = mysql.TypeVarString
retTp.Decimal = types.UnspecifiedLength
bf.tp = &retTp

argTp := args[0].GetType()
bf.tp.Flen = args[0].GetType().Flen
addBinFlag(bf.tp)
var sig builtinFunc
if types.IsBinaryStr(bf.tp) {
if types.IsBinaryStr(argTp) {
sig = &builtinReverseSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_Reverse)
} else {
Expand Down

0 comments on commit f1e5877

Please sign in to comment.