From fb7c65970d7031dd7d88093d0aee4f5a265d761c Mon Sep 17 00:00:00 2001 From: lfyzjck Date: Thu, 28 Mar 2024 12:13:07 +0800 Subject: [PATCH] bugfix: fix exception Cannot parse "0000-00-00 00:00:00", issue: https://github.com/tidb-incubator/TiBigData/issues/269 --- .../tidb/bigdata/tidb/meta/TiColumnInfo.java | 3 ++- .../bigdata/tidb/types/TimestampType.java | 14 ++++++++++ .../bigdata/tidb/meta/TiColumnInfoTest.java | 26 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tidb/src/test/java/io/tidb/bigdata/tidb/meta/TiColumnInfoTest.java diff --git a/tidb/src/main/java/io/tidb/bigdata/tidb/meta/TiColumnInfo.java b/tidb/src/main/java/io/tidb/bigdata/tidb/meta/TiColumnInfo.java index 8ceffd12..da36f52e 100644 --- a/tidb/src/main/java/io/tidb/bigdata/tidb/meta/TiColumnInfo.java +++ b/tidb/src/main/java/io/tidb/bigdata/tidb/meta/TiColumnInfo.java @@ -215,7 +215,8 @@ String getOriginDefaultValue() { } } - private ByteString getOriginDefaultValueAsByteString() { + @VisibleForTesting + public ByteString getOriginDefaultValueAsByteString() { CodecDataOutput cdo = new CodecDataOutput(); type.encode( cdo, EncodeType.VALUE, type.getOriginDefaultValue(getOriginDefaultValue(), version)); diff --git a/tidb/src/main/java/io/tidb/bigdata/tidb/types/TimestampType.java b/tidb/src/main/java/io/tidb/bigdata/tidb/types/TimestampType.java index 94e5d9b6..4a7efb90 100644 --- a/tidb/src/main/java/io/tidb/bigdata/tidb/types/TimestampType.java +++ b/tidb/src/main/java/io/tidb/bigdata/tidb/types/TimestampType.java @@ -48,6 +48,11 @@ public class TimestampType extends AbstractDateTimeType { public static final MySQLType[] subTypes = new MySQLType[] {MySQLType.TypeTimestamp}; + /** + * Default value for timestamp type is 0000-00-00 00:00:00 + */ + public static final String TIMESTAMP_NULL_DEFAULT = "0000-00-00 00:00:00"; + TimestampType(MySQLType tp) { super(tp); } @@ -90,6 +95,15 @@ protected Timestamp decodeNotNullForBatchWrite(int flag, CodecDataInput cdi) { return decodeDateTimeForBatchWrite(flag, cdi); } + @Override + public Object getOriginDefaultValue(String value, long version) { + // avoid exception: org.joda.time.IllegalFieldValueException: Cannot parse "0000-00-00 00:00:00": Value 0 for monthOfYear must be in the range [1,12] + if (value != null && value.equals(TIMESTAMP_NULL_DEFAULT)) { + value = null; + } + return super.getOriginDefaultValue(value, version); + } + @Override public DateTime getOriginDefaultValueNonNull(String value, long version) { if (version >= DataType.COLUMN_VERSION_FLAG) { diff --git a/tidb/src/test/java/io/tidb/bigdata/tidb/meta/TiColumnInfoTest.java b/tidb/src/test/java/io/tidb/bigdata/tidb/meta/TiColumnInfoTest.java new file mode 100644 index 00000000..840c4191 --- /dev/null +++ b/tidb/src/test/java/io/tidb/bigdata/tidb/meta/TiColumnInfoTest.java @@ -0,0 +1,26 @@ +package io.tidb.bigdata.tidb.meta; + +import io.tidb.bigdata.tidb.types.TimestampType; +import org.junit.Test; + +public class TiColumnInfoTest { + + @Test + public void testToProto() { + TiColumnInfo columnInfo = + new TiColumnInfo( + 1L, + "name", + 0, + TimestampType.TIMESTAMP, + SchemaState.StatePublic, + "0000-00-00 00:00:00", + "0000-00-00 00:00:00", + "0000-00-00 00:00:00", + "timestamp", + 1, + "", + false); + System.out.println(columnInfo.getOriginDefaultValueAsByteString()); + } +}