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

expression: fix wrong retType for reverse function #30829

Merged
merged 6 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
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 @@ -174,10 +174,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 @@ -770,12 +772,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