From 899c2f05f67f67e3b1c191e7b14ea9591338b256 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Thu, 12 Aug 2021 23:39:17 +0800 Subject: [PATCH] expression: fix wrong flen infer for bit constant (#23867) (#24267) --- expression/integration_test.go | 5 +++++ expression/typeinfer_test.go | 6 +++--- types/field_type.go | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index e5dfe15894c0f..fd90a59ac5fb8 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -4618,6 +4618,11 @@ func (s *testIntegrationSuite) TestIssues(c *C) { tk.MustExec(`insert into t2 values(1,"1111"),(2,"2222"),(3,"3333"),(4,"4444"),(5,"5555"),(6,"6666"),(7,"7777"),(8,"8888"),(9,"9999"),(10,"0000")`) tk.MustQuery(`select (@j := case when substr(t2.b,1,3)=@i then 1 else @j+1 end) from t2, (select @j := 0, @i := "0") tt limit 10`).Check(testkit.Rows( "1", "2", "3", "4", "5", "6", "7", "8", "9", "10")) + + // for issue #23479 + tk.MustQuery("select b'10000000' DIV 10").Check(testkit.Rows("12")) + tk.MustQuery("select cast(b'10000000' as unsigned) / 10").Check(testkit.Rows("12.8000")) + tk.MustQuery("select b'10000000' / 10").Check(testkit.Rows("12.8000")) } func (s *testIntegrationSuite) TestInPredicate4UnsignedInt(c *C) { diff --git a/expression/typeinfer_test.go b/expression/typeinfer_test.go index 321598891d374..10e146d6189ab 100644 --- a/expression/typeinfer_test.go +++ b/expression/typeinfer_test.go @@ -170,9 +170,9 @@ func (s *testInferTypeSuite) createTestCase4Constants() []typeInferTestCase { {"'1234'", mysql.TypeVarString, charset.CharsetUTF8MB4, 0 | mysql.NotNullFlag, 4, types.UnspecifiedLength}, {"_utf8'1234'", mysql.TypeVarString, charset.CharsetUTF8, 0 | mysql.NotNullFlag, 4, types.UnspecifiedLength}, {"_binary'1234'", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag | mysql.NotNullFlag, 4, types.UnspecifiedLength}, - {"b'0001'", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag | mysql.NotNullFlag, 1, 0}, - {"b'000100001'", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag | mysql.NotNullFlag, 2, 0}, - {"b'0000000000010000'", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag | mysql.NotNullFlag, 2, 0}, + {"b'0001'", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag | mysql.NotNullFlag, 3, 0}, + {"b'000100001'", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag | mysql.NotNullFlag, 6, 0}, + {"b'0000000000010000'", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag | mysql.NotNullFlag, 6, 0}, {"x'10'", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag | mysql.UnsignedFlag | mysql.NotNullFlag, 3, 0}, {"x'ff10'", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag | mysql.UnsignedFlag | mysql.NotNullFlag, 6, 0}, {"x'0000000000000000ff10'", mysql.TypeVarString, charset.CharsetBin, mysql.BinaryFlag | mysql.UnsignedFlag | mysql.NotNullFlag, 30, 0}, diff --git a/types/field_type.go b/types/field_type.go index d73b05cbf8365..025b01ba8badf 100644 --- a/types/field_type.go +++ b/types/field_type.go @@ -250,7 +250,7 @@ func DefaultTypeForValue(value interface{}, tp *FieldType, char string, collate SetBinChsClnFlag(tp) case BitLiteral: tp.Tp = mysql.TypeVarString - tp.Flen = len(x) + tp.Flen = len(x) * 3 tp.Decimal = 0 SetBinChsClnFlag(tp) case HexLiteral: