Skip to content

Commit

Permalink
check for zero values
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzix committed Apr 8, 2021
1 parent 659a4ef commit 8195277
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions cdc/sink/codec/avro.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ func getAvroDataTypeFromColumn(col *model.Column) (interface{}, error) {
}
}

var (
zeroTimeStr = types.NewTime(types.ZeroCoreTime, mysql.TypeTimestamp, 0).String()
zeroDateStr = types.NewTime(types.ZeroCoreTime, mysql.TypeDate, 0).String()
)

func columnToAvroNativeData(col *model.Column, tz *time.Location) (interface{}, string, error) {
if col.Value == nil {
return nil, "null", nil
Expand All @@ -387,6 +392,16 @@ func columnToAvroNativeData(col *model.Column, tz *time.Location) (interface{},

switch col.Type {
case mysql.TypeDate, mysql.TypeDatetime, mysql.TypeNewDate, mysql.TypeTimestamp:
// Refer to `unflatten` in cdc/entry/codec.go for why this piece of code is like this.
const fullType = "long." + timestampMillis
str := col.Value.(string)

if (col.Type == mysql.TypeDate && str == zeroDateStr) ||
(col.Type != mysql.TypeDate && str == zeroTimeStr) {

return time.Time{}, string(fullType), nil
}

var actualTz *time.Location
if col.Type != mysql.TypeTimestamp {
var err error
Expand All @@ -397,9 +412,9 @@ func columnToAvroNativeData(col *model.Column, tz *time.Location) (interface{},
} else {
actualTz = tz
}
str := col.Value.(string)

t, err := time.ParseInLocation(types.DateFormat, str, actualTz)
const fullType = "long." + timestampMillis

if err == nil {
return t, string(fullType), nil
}
Expand Down

0 comments on commit 8195277

Please sign in to comment.