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

schemadiff: assume default collation for textual column when collation is undefined #16000

Merged
merged 4 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 26 additions & 1 deletion go/vt/schemadiff/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,18 @@ func (c *ColumnDefinitionEntity) ColumnDiff(
) (*ModifyColumnDiff, error) {
if c.IsTextual() || other.IsTextual() {
// We will now denormalize the columns charset & collate as needed (if empty, populate from table.)

// Normalizing _this_ column definition:
if c.columnDefinition.Type.Charset.Name != "" && c.columnDefinition.Type.Options.Collate == "" {
// Charset defined without collation. Assign the default collation for that charset.
collation := env.CollationEnv().DefaultCollationForCharset(c.columnDefinition.Type.Charset.Name)
if collation == collations.Unknown {
return nil, &UnknownColumnCharsetCollationError{Column: c.columnDefinition.Name.String(), Charset: t1cc.charset}
}
defer func() {
c.columnDefinition.Type.Options.Collate = ""
}()
c.columnDefinition.Type.Options.Collate = env.CollationEnv().LookupName(collation)
}
if c.columnDefinition.Type.Charset.Name == "" && c.columnDefinition.Type.Options.Collate != "" {
// Column has explicit collation but no charset. We can infer the charset from the collation.
collationID := env.CollationEnv().LookupByName(c.columnDefinition.Type.Options.Collate)
Expand All @@ -118,6 +129,7 @@ func (c *ColumnDefinitionEntity) ColumnDiff(
c.columnDefinition.Type.Charset.Name = charset
}
if c.columnDefinition.Type.Charset.Name == "" {
// Still nothing? Assign the table's charset/collation.
defer func() {
c.columnDefinition.Type.Charset.Name = ""
c.columnDefinition.Type.Options.Collate = ""
Expand All @@ -137,6 +149,18 @@ func (c *ColumnDefinitionEntity) ColumnDiff(
c.columnDefinition.Type.Options.Collate = env.CollationEnv().LookupName(collation)
}
}
// Normalizing _the other_ column definition:
if other.columnDefinition.Type.Charset.Name != "" && other.columnDefinition.Type.Options.Collate == "" {
// Charset defined without collation. Assign the default collation for that charset.
collation := env.CollationEnv().DefaultCollationForCharset(other.columnDefinition.Type.Charset.Name)
if collation == collations.Unknown {
return nil, &UnknownColumnCharsetCollationError{Column: other.columnDefinition.Name.String(), Charset: t2cc.charset}
}
defer func() {
other.columnDefinition.Type.Options.Collate = ""
}()
other.columnDefinition.Type.Options.Collate = env.CollationEnv().LookupName(collation)
}
if other.columnDefinition.Type.Charset.Name == "" && other.columnDefinition.Type.Options.Collate != "" {
// Column has explicit collation but no charset. We can infer the charset from the collation.
collationID := env.CollationEnv().LookupByName(other.columnDefinition.Type.Options.Collate)
Expand All @@ -151,6 +175,7 @@ func (c *ColumnDefinitionEntity) ColumnDiff(
}

if other.columnDefinition.Type.Charset.Name == "" {
// Still nothing? Assign the table's charset/collation.
defer func() {
other.columnDefinition.Type.Charset.Name = ""
other.columnDefinition.Type.Options.Collate = ""
Expand Down
35 changes: 35 additions & 0 deletions go/vt/schemadiff/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1896,6 +1896,36 @@ func TestCreateTableDiff(t *testing.T) {
from: "create table t (id int primary key, v varchar(64) character set utf8mb3 collate utf8mb3_bin)",
to: "create table t (id int primary key, v varchar(64) collate utf8mb3_bin)",
},
{
name: "ignore identical implicit ascii charset",
from: "create table t (id int primary key, v varchar(64) character set ascii collate ascii_general_ci)",
to: "create table t (id int primary key, v varchar(64) collate ascii_general_ci)",
},
{
name: "ignore identical implicit collation",
from: "create table t (id int primary key, v varchar(64) character set utf8mb3 collate utf8mb3_general_ci)",
to: "create table t (id int primary key, v varchar(64) character set utf8mb3)",
},
{
name: "ignore identical implicit collation, reverse",
from: "create table t (id int primary key, v varchar(64) character set utf8mb3)",
to: "create table t (id int primary key, v varchar(64) character set utf8mb3 collate utf8mb3_general_ci)",
},
{
name: "implicit charset and implciit collation",
from: "create table t (id int primary key, v varchar(64) character set utf8mb3)",
to: "create table t (id int primary key, v varchar(64) collate utf8mb3_general_ci)",
},
{
name: "ignore identical implicit ascii collation",
from: "create table t (id int primary key, v varchar(64) character set ascii collate ascii_general_ci)",
to: "create table t (id int primary key, v varchar(64) character set ascii)",
},
{
name: "implicit charset and implciit collation, ascii",
from: "create table t (id int primary key, v varchar(64) collate ascii_general_ci)",
to: "create table t (id int primary key, v varchar(64) character set ascii)",
},
{
name: "normalized unsigned attribute",
from: "create table t1 (id int primary key)",
Expand Down Expand Up @@ -2925,6 +2955,11 @@ func TestNormalize(t *testing.T) {
from: "create table t (id int primary key, v varchar(255) charset utf8mb4 collate utf8mb4_german2_ci)",
to: "CREATE TABLE `t` (\n\t`id` int,\n\t`v` varchar(255) COLLATE utf8mb4_german2_ci,\n\tPRIMARY KEY (`id`)\n)",
},
{
name: "ascii charset and collation",
from: "create table t (id int primary key, v varchar(255) charset ascii collate ascii_general_ci) charset utf8mb3 collate utf8_general_ci",
to: "CREATE TABLE `t` (\n\t`id` int,\n\t`v` varchar(255) CHARACTER SET ascii COLLATE ascii_general_ci,\n\tPRIMARY KEY (`id`)\n) CHARSET utf8mb3,\n COLLATE utf8mb3_general_ci",
},
{
name: "correct case table options for engine",
from: "create table t (id int signed primary key) engine innodb",
Expand Down
Loading