Skip to content

Commit

Permalink
Bugfix: fix exception Cannot parse "0000-00-00 00:00:00" (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
lfyzjck authored Apr 12, 2024
1 parent f46f32c commit 22a3a86
Show file tree
Hide file tree
Showing 3 changed files with 59 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
13 changes: 13 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,9 @@ 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 +93,16 @@ 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 (TIMESTAMP_NULL_DEFAULT.equals(value)) {
value = null;
}
return super.getOriginDefaultValue(value, version);
}

@Override
public DateTime getOriginDefaultValueNonNull(String value, long version) {
if (version >= DataType.COLUMN_VERSION_FLAG) {
Expand Down
44 changes: 44 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,44 @@
/*
* Copyright 2024 TiDB Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.tidb.bigdata.tidb.meta;

import static org.junit.Assert.assertEquals;

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",
"2024-04-01 00:00:00",
"0000-00-00 00:00:00",
"timestamp",
1,
"",
false);
assertEquals("\000", columnInfo.getOriginDefaultValueAsByteString().toStringUtf8());
}
}

0 comments on commit 22a3a86

Please sign in to comment.