Skip to content

Commit

Permalink
parser,config: Correct handling of display width deprecation except f…
Browse files Browse the repository at this point in the history
…or zerofill and tinyint(1) (#46651)

close pingcap/tidb#46650
  • Loading branch information
dveeden authored Sep 7, 2023
1 parent 275f350 commit 83e3565
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
12 changes: 11 additions & 1 deletion executor/test/showtest/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1647,12 +1647,22 @@ func TestShowCreateTableWithIntegerDisplayLengthWarnings(t *testing.T) {
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1064 You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use [parser:1681]Integer display width is deprecated and will be removed in a future release.",
"Warning 1064 You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use [parser:1681]Integer display width is deprecated and will be removed in a future release."))
tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
" `a` tinyint DEFAULT NULL,\n" +
" `a` tinyint(1) DEFAULT NULL,\n" +
" `b` smallint DEFAULT NULL,\n" +
" `c` mediumint DEFAULT NULL,\n" +
" `d` int DEFAULT NULL,\n" +
" `e` bigint DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(id int primary key, c1 bool, c2 int(10) zerofill)")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1064 You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use [parser:1681]Integer display width is deprecated and will be removed in a future release."))
tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
" `id` int NOT NULL,\n" +
" `c1` tinyint(1) DEFAULT NULL,\n" +
" `c2` int(10) unsigned zerofill DEFAULT NULL,\n" +
" PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
}

func TestShowVar(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion parser/types/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ go_test(
],
embed = [":types"],
flaky = True,
shard_count = 5,
shard_count = 6,
deps = [
"//parser",
"//parser/ast",
Expand Down
14 changes: 12 additions & 2 deletions parser/types/field_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,20 @@ func (ft *FieldType) CompactStr() string {
suffix = fmt.Sprintf("(%d,%d)", displayFlen, displayDecimal)
case mysql.TypeBit, mysql.TypeVarchar, mysql.TypeString, mysql.TypeVarString:
suffix = fmt.Sprintf("(%d)", displayFlen)
case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
case mysql.TypeTiny:
// With display length deprecation active tinyint(1) still has
// a display length to indicate this might have been a BOOL.
// Connectors expect this.
//
// See also:
// https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-19.html
if !TiDBStrictIntegerDisplayWidth || (mysql.HasZerofillFlag(ft.flag) || displayFlen == 1) {
suffix = fmt.Sprintf("(%d)", displayFlen)
}
case mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
// Referring this issue #6688, the integer max display length is deprecated in MySQL 8.0.
// Since the length doesn't take any effect in TiDB storage or showing result, we remove it here.
if !TiDBStrictIntegerDisplayWidth {
if !TiDBStrictIntegerDisplayWidth || mysql.HasZerofillFlag(ft.flag) {
suffix = fmt.Sprintf("(%d)", displayFlen)
}
case mysql.TypeYear:
Expand Down
30 changes: 30 additions & 0 deletions parser/types/field_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,33 @@ func TestFieldTypeEqual(t *testing.T) {
ft1.SetFlen(23)
require.Equal(t, true, ft1.Equal(ft2))
}

func TestCompactStr(t *testing.T) {
cases := []struct {
t byte // Field Type
flen int // Field Length
flags uint // Field Flags, e.g. ZEROFILL
e1 string // Expected string with TiDBStrictIntegerDisplayWidth disabled
e2 string // Expected string with TiDBStrictIntegerDisplayWidth enabled
}{
// TINYINT(1) is considered a bool by connectors, this should always display
// the display length.
{mysql.TypeTiny, 1, 0, `tinyint(1)`, `tinyint(1)`},
{mysql.TypeTiny, 2, 0, `tinyint(2)`, `tinyint`},

// If the ZEROFILL flag is set the display length should not be hidden.
{mysql.TypeLong, 10, 0, `int(10)`, `int`},
{mysql.TypeLong, 10, mysql.ZerofillFlag, `int(10)`, `int(10)`},
}
for _, cc := range cases {
ft := NewFieldType(cc.t)
ft.SetFlen(cc.flen)
ft.SetFlag(cc.flags)

TiDBStrictIntegerDisplayWidth = false
require.Equal(t, cc.e1, ft.CompactStr())

TiDBStrictIntegerDisplayWidth = true
require.Equal(t, cc.e2, ft.CompactStr())
}
}

0 comments on commit 83e3565

Please sign in to comment.