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 convert number base for hybrid type #21554

Merged
merged 3 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 30 additions & 4 deletions expression/builtin_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sync"

"github.com/cznic/mathutil"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -1163,11 +1164,25 @@ func (b *builtinConvSig) Clone() builtinFunc {
// evalString evals CONV(N,from_base,to_base).
// See https://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html#function_conv.
func (b *builtinConvSig) evalString(row chunk.Row) (res string, isNull bool, err error) {
str, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return res, isNull, err
var str string
switch x := b.args[0].(type) {
case *Constant:
if x.Value.Kind() == types.KindBinaryLiteral {
str = x.Value.GetBinaryLiteral().ToBitLiteralString(true)
}
case *ScalarFunction:
if x.FuncName.L == ast.Cast {
arg0 := x.GetArgs()[0]
if arg0.GetType().Hybrid() || IsBinaryLiteral(arg0) {
str, isNull, err = arg0.EvalString(b.ctx, row)
if isNull || err != nil {
return str, isNull, err
}
d := types.NewStringDatum(str)
str = d.GetBinaryLiteral().ToBitLiteralString(true)
}
}
Comment on lines +1174 to +1184
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if the func is not Cast but produces string that needs to be converted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we build conv function. If the first argument is NOT string type. The cast would be wrapped. No other cases. You can refer to this code.

func (c *convFunctionClass) getFunction(ctx sessionctx.Context, args []Expression) (builtinFunc, error) {
	if err := c.verifyArgs(args); err != nil {
		return nil, err
	}

	bf, err := newBaseBuiltinFuncWithTp(ctx, c.funcName, args, types.ETString, types.ETString, types.ETInt, types.ETInt)
	if err != nil {
		return nil, err
	}
	bf.tp.Charset, bf.tp.Collate = ctx.GetSessionVars().GetCharsetInfo()
	bf.tp.Flen = 64
	sig := &builtinConvSig{bf}
	sig.setPbCode(tipb.ScalarFuncSig_Conv)
	return sig, nil
}

}

fromBase, isNull, err := b.args[1].EvalInt(b.ctx, row)
if isNull || err != nil {
return res, isNull, err
Expand All @@ -1177,6 +1192,17 @@ func (b *builtinConvSig) evalString(row chunk.Row) (res string, isNull bool, err
if isNull || err != nil {
return res, isNull, err
}
if len(str) == 0 {
str, isNull, err = b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
return res, isNull, err
}
} else {
str, isNull, err = b.conv(str[2:], 2, fromBase)
if err != nil {
return str, isNull, err
}
}
return b.conv(str, fromBase, toBase)
}
func (b *builtinConvSig) conv(str string, fromBase, toBase int64) (res string, isNull bool, err error) {
Expand Down
3 changes: 2 additions & 1 deletion expression/builtin_math_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,8 @@ func (b *builtinSignSig) vecEvalInt(input *chunk.Chunk, result *chunk.Column) er
}

func (b *builtinConvSig) vectorized() bool {
return true
// TODO: change the vecEval match hybrid type fixing. Then open the vectorized evaluation.
return false
Comment on lines +1073 to +1074
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you like to create an issue for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update #12106, since the PR merged.

}

func (b *builtinConvSig) vecEvalString(input *chunk.Chunk, result *chunk.Column) error {
Expand Down
2 changes: 2 additions & 0 deletions expression/builtin_math_vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import (
)

var vecBuiltinMathCases = map[string][]vecExprBenchCase{
/* TODO: Because of https://github.com/pingcap/tidb/issues/5817, we don't enable it now.
ast.Conv: {
{
retEvalType: types.ETString,
childrenTypes: []types.EvalType{types.ETString, types.ETInt, types.ETInt},
},
},
*/
ast.Sign: {
{retEvalType: types.ETInt, childrenTypes: []types.EvalType{types.ETReal}},
},
Expand Down
36 changes: 36 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,42 @@ func (s *testIntegrationSuite2) TestMathBuiltin(c *C) {
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("SELECT CONV('a', 37, 10);")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("SELECT CONV(0x0020, 2, 2);")
result.Check(testkit.Rows("100000"))
result = tk.MustQuery("SELECT CONV(0b10, 16, 2)")
result.Check(testkit.Rows("10"))
result = tk.MustQuery("SELECT CONV(0b10, 16, 8)")
result.Check(testkit.Rows("2"))
tk.MustExec("drop table if exists bit")
tk.MustExec("create table bit(b bit(10))")
tk.MustExec(`INSERT INTO bit (b) VALUES
(0b0000010101),
(0b0000010101),
(NULL),
(0b0000000001),
(0b0000000000),
(0b1111111111),
(0b1111111111),
(0b1111111111),
(0b0000000000),
(0b0000000000),
(0b0000000000),
(0b0000000000),
(0b0000100000);`)
tk.MustQuery("select conv(b, 2, 2) from `bit`").Check(testkit.Rows(
"10101",
"10101",
"<nil>",
"1",
"0",
"1111111111",
"1111111111",
"1111111111",
"0",
"0",
"0",
"0",
"100000"))

// for abs
result = tk.MustQuery("SELECT ABS(-1);")
Expand Down