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

sink(ticdc): fix incorrect encoding default value in Avro protocol (#11995) #12010

Merged
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
10 changes: 10 additions & 0 deletions pkg/sink/codec/avro/avro.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,18 @@
}
} else {
if col.Flag.IsNullable() {
// the string literal "null" must be coerced to a `nil`
// see https://github.com/linkedin/goavro/blob/5ec5a5ee7ec82e16e6e2b438d610e1cab2588393/record.go#L109-L114
// https://stackoverflow.com/questions/22938124/avro-field-default-values
defaultFirst := false
if defaultValue == nil {
defaultFirst = true
} else if s, ok := defaultValue.(string); ok && s == "null" {
defaultFirst = true
} else if b, ok := defaultValue.([]byte); ok && string(b) == "null" {
defaultFirst = true
}

Check warning on line 568 in pkg/sink/codec/avro/avro.go

View check run for this annotation

Codecov / codecov/patch

pkg/sink/codec/avro/avro.go#L565-L568

Added lines #L565 - L568 were not covered by tests
if defaultFirst {
field["type"] = []interface{}{"null", avroType}
} else {
field["type"] = []interface{}{avroType, "null"}
Expand Down
13 changes: 13 additions & 0 deletions tests/integration_tests/avro_basic/data/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,19 @@ insert into t(c_tinyint, c_mediumint, c_int, c_bigint, a) values (4, 5, 6, 7, 8)
alter table t modify c_mediumint varchar(10) null;
insert into t(c_tinyint, c_mediumint, c_int, c_bigint, a) values (5, "234", 6, 7, 8);

create table t1(
id int primary key,
c1 varchar(255) default "null",
c2 varchar(255) default "NULL",
c3 varchar(255) default null
);

insert into t1(id) values(1);
update t1 set c1 = "null", c2 = "NULL", c3 = null where id = 1;
alter table t1 add column col json not null;
alter table t1 modify column col json default null;
insert into t1(id) values(2);

create table finish_mark
(
id int PRIMARY KEY
Expand Down
Loading