Skip to content

Commit 145e2ed

Browse files
authored
codec(ticdc): correct default value for debezium (#12475)
close #12474
1 parent 7f34b86 commit 145e2ed

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

pkg/sink/codec/debezium/codec.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -518,37 +518,43 @@ func (c *dbzCodec) writeDebeziumFieldValue(
518518
return nil
519519

520520
case mysql.TypeEnum:
521-
v, ok := value.(uint64)
522-
if !ok {
521+
switch v := value.(type) {
522+
case uint64:
523+
enumVar, err := types.ParseEnumValue(ft.GetElems(), v)
524+
if err != nil {
525+
// Invalid enum value inserted in non-strict mode.
526+
writer.WriteStringField(col.GetName(), "")
527+
return nil
528+
}
529+
writer.WriteStringField(col.GetName(), enumVar.Name)
530+
case string:
531+
writer.WriteStringField(col.GetName(), v)
532+
default:
523533
return cerror.ErrDebeziumEncodeFailed.GenWithStack(
524534
"unexpected column value type %T for enum column %s",
525535
value,
526536
col.GetName())
527537
}
528-
enumVar, err := types.ParseEnumValue(ft.GetElems(), v)
529-
if err != nil {
530-
// Invalid enum value inserted in non-strict mode.
531-
writer.WriteStringField(col.GetName(), "")
532-
return nil
533-
}
534-
writer.WriteStringField(col.GetName(), enumVar.Name)
535538
return nil
536539

537540
case mysql.TypeSet:
538-
v, ok := value.(uint64)
539-
if !ok {
541+
switch v := value.(type) {
542+
case uint64:
543+
setVar, err := types.ParseSetValue(ft.GetElems(), v)
544+
if err != nil {
545+
// Invalid enum value inserted in non-strict mode.
546+
writer.WriteStringField(col.GetName(), "")
547+
return nil
548+
}
549+
writer.WriteStringField(col.GetName(), setVar.Name)
550+
case string:
551+
writer.WriteStringField(col.GetName(), v)
552+
default:
540553
return cerror.ErrDebeziumEncodeFailed.GenWithStack(
541554
"unexpected column value type %T for set column %s",
542555
value,
543556
col.GetName())
544557
}
545-
setVar, err := types.ParseSetValue(ft.GetElems(), v)
546-
if err != nil {
547-
// Invalid enum value inserted in non-strict mode.
548-
writer.WriteStringField(col.GetName(), "")
549-
return nil
550-
}
551-
writer.WriteStringField(col.GetName(), setVar.Name)
552558
return nil
553559

554560
case mysql.TypeNewDecimal:

tests/integration_tests/debezium/sql/data_types.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,8 @@ INSERT INTO t_json VALUES ('["foo"]', 1);
390390
*/
391391

392392
CREATE TABLE t_enum(
393-
col_e ENUM('a', 'b', 'c'),
394-
col_s SET('a', 'b', 'c'),
393+
col_e ENUM('a', 'b', 'c') DEFAULT 'a',
394+
col_s SET('a', 'b', 'c') DEFAULT 'a',
395395
pk INT PRIMARY KEY
396396
);
397397

@@ -400,3 +400,5 @@ INSERT INTO t_enum VALUES ('a', 'c', 1);
400400
SET sql_mode='';
401401
INSERT INTO t_enum VALUES ('d', 'e', 2);
402402
SET sql_mode='strict_trans_tables';
403+
404+
INSERT INTO t_enum VALUES (null, null, 3);

tests/integration_tests/debezium_basic/data/test.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ USE test;
55
create table t1 (id int primary key, account_id int not null);
66
alter table t1 add unique key(account_id);
77
insert into t1 values (12,34);
8+
9+
CREATE TABLE test.vec(id int primary key, data VECTOR(5));
10+
INSERT INTO test.vec(id, data) VALUES (1, "[1,2,3,4,5]");
11+

0 commit comments

Comments
 (0)