Added unit test case and logic to limit the DateTime values to clickhouse supported datetime ranges.

This commit is contained in:
Kanthi Subramanian 2022-05-05 12:13:42 -04:00
parent e1cf390123
commit 3d7fb07072
4 changed files with 56 additions and 14 deletions

View File

@ -1,6 +1,6 @@
package com.altinity.clickhouse.sink.connector.converters;
import com.altinity.clickhouse.sink.connector.db.Constants;
import com.altinity.clickhouse.sink.connector.db.DataTypeRange;
import java.sql.Date;
import java.text.SimpleDateFormat;
@ -39,9 +39,26 @@ public class DebeziumConverter {
*/
public static String convert(Object value) {
LocalDateTime date = LocalDateTime.ofInstant(Instant.ofEpochMilli((long) value), ZoneId.systemDefault());
LocalDateTime modifiedDate = checkIfDateTimeExceedsSupportedRange(date);
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
return date.format(formatter);
return modifiedDate.format(formatter);
}
public static LocalDateTime checkIfDateTimeExceedsSupportedRange(LocalDateTime providedDateTime) {
LocalDateTime minSupportedDateTime = LocalDateTime.parse(DataTypeRange.CLICKHOUSE_MIN_SUPPORTED_DATETIME);
LocalDateTime maxSupportedDateTime = LocalDateTime.parse(DataTypeRange.CLICKHOUSE_MAX_SUPPORTED_DATETIME);
if(providedDateTime.isBefore(minSupportedDateTime)) {
return minSupportedDateTime;
} else if (providedDateTime.isAfter(maxSupportedDateTime)){
return maxSupportedDateTime;
}
return providedDateTime;
}
}
@ -67,8 +84,8 @@ public class DebeziumConverter {
}
public static java.util.Date checkIfDateExceedsSupportedRange(java.util.Date providedDate) {
java.util.Date minSupportedDate = Date.valueOf(Constants.CLICKHOUSE_MIN_SUPPORTED_DATE);
java.util.Date maxSupportedDate = Date.valueOf(Constants.CLICKHOUSE_MAX_SUPPORTED_DATE);
java.util.Date minSupportedDate = Date.valueOf(DataTypeRange.CLICKHOUSE_MIN_SUPPORTED_DATE);
java.util.Date maxSupportedDate = Date.valueOf(DataTypeRange.CLICKHOUSE_MAX_SUPPORTED_DATE);
if(providedDate.before(minSupportedDate)) {
return minSupportedDate;

View File

@ -1,7 +0,0 @@
package com.altinity.clickhouse.sink.connector.db;
public class Constants
{
public static final String CLICKHOUSE_MIN_SUPPORTED_DATE = "1970-01-01";
public static final String CLICKHOUSE_MAX_SUPPORTED_DATE = "2149-06-06";
}

View File

@ -0,0 +1,14 @@
package com.altinity.clickhouse.sink.connector.db;
public class DataTypeRange
{
// Date
public static final String CLICKHOUSE_MIN_SUPPORTED_DATE = "1970-01-01";
public static final String CLICKHOUSE_MAX_SUPPORTED_DATE = "2149-06-06";
// DateTime
public static final String CLICKHOUSE_MIN_SUPPORTED_DATETIME = "1970-01-01T00:00:00";
public static final String CLICKHOUSE_MAX_SUPPORTED_DATETIME = "2106-02-07T06:28:15";
}

View File

@ -1,6 +1,6 @@
package com.altinity.clickhouse.sink.connector.converters;
import com.altinity.clickhouse.sink.connector.db.Constants;
import com.altinity.clickhouse.sink.connector.db.DataTypeRange;
import org.junit.Assert;
import org.junit.Test;
@ -24,6 +24,24 @@ public class DebeziumConverterTest {
Assert.assertTrue(formattedTimestamp.equalsIgnoreCase("2021-12-31T19:01:00"));
}
@Test
public void testTimestampConverterMinRange() {
Object timestampEpoch = -2166681362000L;
String formattedTimestamp = DebeziumConverter.TimestampConverter.convert(timestampEpoch);
Assert.assertTrue(formattedTimestamp.equalsIgnoreCase(DataTypeRange.CLICKHOUSE_MIN_SUPPORTED_DATETIME));
}
@Test
public void testTimestampConverterMaxRange() {
Object timestampEpoch = 4807440238000L;
String formattedTimestamp = DebeziumConverter.TimestampConverter.convert(timestampEpoch);
Assert.assertTrue(formattedTimestamp.equalsIgnoreCase(DataTypeRange.CLICKHOUSE_MAX_SUPPORTED_DATETIME));
}
@Test
public void testDateConverter() {
@ -39,7 +57,7 @@ public class DebeziumConverterTest {
Integer date = -144450000;
java.sql.Date formattedDate = DebeziumConverter.DateConverter.convert(date);
Assert.assertTrue(formattedDate.toString().equalsIgnoreCase(Constants.CLICKHOUSE_MIN_SUPPORTED_DATE));
Assert.assertTrue(formattedDate.toString().equalsIgnoreCase(DataTypeRange.CLICKHOUSE_MIN_SUPPORTED_DATE));
}
@Test
public void testDateConverterMaxRange() {
@ -47,7 +65,7 @@ public class DebeziumConverterTest {
Integer date = 450000;
java.sql.Date formattedDate = DebeziumConverter.DateConverter.convert(date);
Assert.assertTrue(formattedDate.toString().equalsIgnoreCase(Constants.CLICKHOUSE_MAX_SUPPORTED_DATE));
Assert.assertTrue(formattedDate.toString().equalsIgnoreCase(DataTypeRange.CLICKHOUSE_MAX_SUPPORTED_DATE));
}
@Test