Skip to content

Commit

Permalink
bugfix: fix exception Cannot parse "0000-00-00 00:00:00", issue: tidb…
Browse files Browse the repository at this point in the history
  • Loading branch information
lfyzjck authored and humengyu2012 committed Apr 12, 2024
1 parent f46f32c commit fb7c659
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
14 changes: 14 additions & 0 deletions tidb/src/main/java/io/tidb/bigdata/tidb/types/TimestampType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions tidb/src/test/java/io/tidb/bigdata/tidb/meta/TiColumnInfoTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit fb7c659

Please sign in to comment.