Skip to content

Commit 22a3a86

Browse files
authored
Bugfix: fix exception Cannot parse "0000-00-00 00:00:00" (#270)
1 parent f46f32c commit 22a3a86

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

tidb/src/main/java/io/tidb/bigdata/tidb/meta/TiColumnInfo.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ String getOriginDefaultValue() {
215215
}
216216
}
217217

218-
private ByteString getOriginDefaultValueAsByteString() {
218+
@VisibleForTesting
219+
public ByteString getOriginDefaultValueAsByteString() {
219220
CodecDataOutput cdo = new CodecDataOutput();
220221
type.encode(
221222
cdo, EncodeType.VALUE, type.getOriginDefaultValue(getOriginDefaultValue(), version));

tidb/src/main/java/io/tidb/bigdata/tidb/types/TimestampType.java

+13
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public class TimestampType extends AbstractDateTimeType {
4848

4949
public static final MySQLType[] subTypes = new MySQLType[] {MySQLType.TypeTimestamp};
5050

51+
/** Default value for timestamp type is 0000-00-00 00:00:00 */
52+
public static final String TIMESTAMP_NULL_DEFAULT = "0000-00-00 00:00:00";
53+
5154
TimestampType(MySQLType tp) {
5255
super(tp);
5356
}
@@ -90,6 +93,16 @@ protected Timestamp decodeNotNullForBatchWrite(int flag, CodecDataInput cdi) {
9093
return decodeDateTimeForBatchWrite(flag, cdi);
9194
}
9295

96+
@Override
97+
public Object getOriginDefaultValue(String value, long version) {
98+
// avoid exception: org.joda.time.IllegalFieldValueException: Cannot parse "0000-00-00
99+
// 00:00:00": Value 0 for monthOfYear must be in the range [1,12]
100+
if (TIMESTAMP_NULL_DEFAULT.equals(value)) {
101+
value = null;
102+
}
103+
return super.getOriginDefaultValue(value, version);
104+
}
105+
93106
@Override
94107
public DateTime getOriginDefaultValueNonNull(String value, long version) {
95108
if (version >= DataType.COLUMN_VERSION_FLAG) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2024 TiDB Project Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.tidb.bigdata.tidb.meta;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import io.tidb.bigdata.tidb.types.TimestampType;
22+
import org.junit.Test;
23+
24+
public class TiColumnInfoTest {
25+
26+
@Test
27+
public void testToProto() {
28+
TiColumnInfo columnInfo =
29+
new TiColumnInfo(
30+
1L,
31+
"name",
32+
0,
33+
TimestampType.TIMESTAMP,
34+
SchemaState.StatePublic,
35+
"0000-00-00 00:00:00",
36+
"2024-04-01 00:00:00",
37+
"0000-00-00 00:00:00",
38+
"timestamp",
39+
1,
40+
"",
41+
false);
42+
assertEquals("\000", columnInfo.getOriginDefaultValueAsByteString().toStringUtf8());
43+
}
44+
}

0 commit comments

Comments
 (0)