From 32a8192d87c6a6668ff959b51cc42a8b8be0fe68 Mon Sep 17 00:00:00 2001 From: 3AceShowHand Date: Tue, 19 Mar 2024 14:33:13 +0800 Subject: [PATCH] fix tests. --- cdc/entry/mounter.go | 17 ++++++++++------- cdc/entry/mounter_test.go | 12 ++++++------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/cdc/entry/mounter.go b/cdc/entry/mounter.go index 57c7c2bcfe1..dae83cc4320 100644 --- a/cdc/entry/mounter.go +++ b/cdc/entry/mounter.go @@ -699,7 +699,10 @@ func formatColVal(datum types.Datum, col *timodel.ColumnInfo) ( // Supported type is: nil, basic type(Int, Int8,..., Float32, Float64, String), Slice(uint8), other types not support // TODO: Check default expr support func getDefaultOrZeroValue(col *timodel.ColumnInfo) (types.Datum, any, int, string, error) { - var d types.Datum + var ( + d types.Datum + err error + ) // NOTICE: SHOULD use OriginDefaultValue here, more info pls ref to // https://github.com/pingcap/tiflow/issues/4048 // FIXME: Too many corner cases may hit here, like type truncate, timezone @@ -707,12 +710,12 @@ func getDefaultOrZeroValue(col *timodel.ColumnInfo) (types.Datum, any, int, stri // (2) If not fix here, will cause data inconsistency in Scenarios(3) directly // Ref: https://github.com/pingcap/tidb/blob/d2c352980a43bb593db81fd1db996f47af596d91/table/column.go#L489 if col.GetOriginDefaultValue() != nil { - d = types.NewDatum(col.GetOriginDefaultValue()) - d, err := d.ConvertTo(types.DefaultStmtNoWarningContext, &col.FieldType) - return d, d.GetValue(), sizeOfDatum(d), "", errors.Trace(err) - } - - if !mysql.HasNotNullFlag(col.GetFlag()) { + datum := types.NewDatum(col.GetOriginDefaultValue()) + d, err = datum.ConvertTo(types.DefaultStmtNoWarningContext, &col.FieldType) + if err != nil { + return d, d.GetValue(), sizeOfDatum(d), "", errors.Trace(err) + } + } else if !mysql.HasNotNullFlag(col.GetFlag()) { // NOTICE: NotNullCheck need do after OriginDefaultValue check, as when TiDB meet "amend + add column default xxx", // ref: https://github.com/pingcap/ticdc/issues/3929 // must use null if TiDB not write the column value when default value is null diff --git a/cdc/entry/mounter_test.go b/cdc/entry/mounter_test.go index 205ecd6076a..e6b6368eaea 100644 --- a/cdc/entry/mounter_test.go +++ b/cdc/entry/mounter_test.go @@ -804,7 +804,7 @@ func TestGetDefaultZeroValue(t *testing.T) { OriginDefaultValue: "e0", FieldType: *ftTypeVarcharNotNull, }, - Res: "e0", + Res: []byte("e0"), }, { Name: "mysql.TypeTinyBlob", @@ -886,7 +886,7 @@ func TestGetDefaultZeroValue(t *testing.T) { decimal := new(types.MyDecimal) err := decimal.FromString([]byte("-3.14")) require.NoError(t, err) - require.Equal(t, decimal, val, "mysql.TypeNewDecimal + notnull + default") + require.Equal(t, decimal.String(), val, "mysql.TypeNewDecimal + notnull + default") colInfo = timodel.ColumnInfo{ OriginDefaultValue: "2020-11-19 12:12:12", @@ -897,7 +897,7 @@ func TestGetDefaultZeroValue(t *testing.T) { types.DefaultStmtNoWarningContext, "2020-11-19 12:12:12", colInfo.FieldType.GetType(), colInfo.FieldType.GetDecimal()) require.NoError(t, err) - require.Equal(t, expected, val, "mysql.TypeTimestamp + notnull + default") + require.Equal(t, expected.String(), val, "mysql.TypeTimestamp + notnull + default") colInfo = timodel.ColumnInfo{ OriginDefaultValue: "2020-11-19 12:12:12", @@ -908,7 +908,7 @@ func TestGetDefaultZeroValue(t *testing.T) { types.DefaultStmtNoWarningContext, "2020-11-19 12:12:12", colInfo.FieldType.GetType(), colInfo.FieldType.GetDecimal()) require.NoError(t, err) - require.Equal(t, expected, val, "mysql.TypeTimestamp + null + default") + require.Equal(t, expected.String(), val, "mysql.TypeTimestamp + null + default") colInfo = timodel.ColumnInfo{ OriginDefaultValue: "e1", @@ -917,7 +917,7 @@ func TestGetDefaultZeroValue(t *testing.T) { _, val, _, _, _ = getDefaultOrZeroValue(&colInfo) expectedEnum, err := types.ParseEnumName(colInfo.FieldType.GetElems(), "e1", colInfo.FieldType.GetCollate()) require.NoError(t, err) - require.Equal(t, expectedEnum, val, "mysql.TypeEnum + notnull + default") + require.Equal(t, expectedEnum.Value, val, "mysql.TypeEnum + notnull + default") colInfo = timodel.ColumnInfo{ OriginDefaultValue: "1,e", @@ -926,7 +926,7 @@ func TestGetDefaultZeroValue(t *testing.T) { _, val, _, _, _ = getDefaultOrZeroValue(&colInfo) expectedSet, err := types.ParseSetName(colInfo.FieldType.GetElems(), "1,e", colInfo.FieldType.GetCollate()) require.NoError(t, err) - require.Equal(t, expectedSet, val, "mysql.TypeSet + notnull + default") + require.Equal(t, expectedSet.Value, val, "mysql.TypeSet + notnull + default") } func TestE2ERowLevelChecksum(t *testing.T) {