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 reverse function on bit type column #50146

Merged
merged 10 commits into from
Feb 5, 2024
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
3 changes: 2 additions & 1 deletion pkg/expression/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ func (c *reverseFunctionClass) getFunction(ctx sessionctx.Context, args []Expres
if err := c.verifyArgs(args); err != nil {
return nil, err
}
bitTypeFlag := types.IsTypeBit(args[0].GetType())
YangKeao marked this conversation as resolved.
Show resolved Hide resolved
bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETString, types.ETString)
YangKeao marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
Expand All @@ -768,7 +769,7 @@ func (c *reverseFunctionClass) getFunction(ctx sessionctx.Context, args []Expres
bf.tp.SetFlen(args[0].GetType().GetFlen())
addBinFlag(bf.tp)
var sig builtinFunc
if types.IsBinaryStr(argTp) {
if types.IsBinaryStr(argTp) || bitTypeFlag {
sig = &builtinReverseSig{bf}
sig.setPbCode(tipb.ScalarFuncSig_Reverse)
} else {
Expand Down
6 changes: 6 additions & 0 deletions pkg/expression/collation.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ func deriveCoercibilityForColumn(c *Column) Coercibility {
// For specified type null, it should return CoercibilityIgnorable, which means it got the lowest priority in DeriveCollationFromExprs.
if c.RetType.GetType() == mysql.TypeNull {
return CoercibilityIgnorable
} else if types.IsTypeBit(c.RetType) {
return CoercibilityImplicit
}

switch c.RetType.EvalType() {
Expand Down Expand Up @@ -374,6 +376,8 @@ func inferCollation(exprs ...Expression) *ExprCollation {
dstCharset, dstCollation := exprs[0].GetType().GetCharset(), exprs[0].GetType().GetCollate()
if exprs[0].GetType().EvalType() == types.ETJson {
dstCharset, dstCollation = charset.CharsetUTF8MB4, charset.CollationUTF8MB4
} else if types.IsTypeBit(exprs[0].GetType()) {
dstCharset, dstCollation = charset.CharsetBin, charset.CollationBin
}
unknownCS := false

Expand All @@ -384,6 +388,8 @@ func inferCollation(exprs ...Expression) *ExprCollation {
// see details https://github.com/pingcap/tidb/issues/31320#issuecomment-1010599311
if arg.GetType().EvalType() == types.ETJson {
argCharset, argCollation = charset.CharsetUTF8MB4, charset.CollationUTF8MB4
} else if types.IsTypeBit(arg.GetType()) {
argCharset, argCharset = charset.CharsetBin, charset.CollationBin
YangKeao marked this conversation as resolved.
Show resolved Hide resolved
}
// If one of the arguments is binary charset, we allow it can be used with other charsets.
// If they have the same coercibility, let the binary charset one to be the winner because binary has more precedence.
Expand Down
10 changes: 10 additions & 0 deletions tests/integrationtest/r/expression/builtin.result
Original file line number Diff line number Diff line change
Expand Up @@ -3287,3 +3287,13 @@ RELEASE_LOCK('test_lock3')
SELECT RELEASE_ALL_LOCKS();
RELEASE_ALL_LOCKS()
0
drop table if exists t;
create table t (col bit(37) NOT NULL);
insert into t (col) values(x'05d3a46d88'), (x'04dba3570c'), (x'0ed284dfee'), (x'12657141bf');
select hex(col), hex(reverse(col)) from t order by reverse(col);
hex(col) hex(reverse(col))
4DBA3570C 0C57A3DB04
5D3A46D88 886DA4D305
12657141BF BF41716512
ED284DFEE EEDF84D20E
drop table t;
6 changes: 6 additions & 0 deletions tests/integrationtest/t/expression/builtin.test
Original file line number Diff line number Diff line change
Expand Up @@ -1560,3 +1560,9 @@ SELECT RELEASE_LOCK('test_lock1');
SELECT RELEASE_LOCK('test_lock3');
SELECT RELEASE_ALL_LOCKS();

# Issue #49566
drop table if exists t;
create table t (col bit(37) NOT NULL);
insert into t (col) values(x'05d3a46d88'), (x'04dba3570c'), (x'0ed284dfee'), (x'12657141bf');
select hex(col), hex(reverse(col)) from t order by reverse(col);
drop table t;