Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DBZ-901 Handle default date/time values without micros #627

Merged
merged 1 commit into from Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -15,6 +15,8 @@
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.regex.Pattern;

import io.debezium.annotation.Immutable;
Expand Down Expand Up @@ -122,18 +124,9 @@ private Object convertToLocalDateTime(Column column, String value) {
}

value = EPOCH_TIMESTAMP;
// Align fraction zeros according to the database schema
if (column.length() > 0) {
final StringBuilder sb = new StringBuilder(EPOCH_TIMESTAMP).append('.');
for (int i = 0; i < column.length(); i++) {
sb.append('0');
}
value = sb.toString();
}
}

final String timestampFormat = timestampFormat(column.length());
return LocalDateTime.from(DateTimeFormatter.ofPattern(timestampFormat).parse(value));
return LocalDateTime.from(timestampFormat(column.length()).parse(value));
}

/**
Expand Down Expand Up @@ -165,8 +158,7 @@ private Object convertToTimestamp(Column column, String value) {
* @return the converted value;
*/
private Object convertToDuration(Column column, String value) {
String timeFormat = timeFormat(column.length());
return Duration.between(LocalTime.MIN, LocalTime.from(DateTimeFormatter.ofPattern(timeFormat).parse(value)));
return Duration.between(LocalTime.MIN, LocalTime.from(timeFormat(column.length()).parse(value)));
}

/**
Expand Down Expand Up @@ -267,29 +259,21 @@ private Object convertToBoolean(String value) {
}
}

private String timeFormat(int length) {
String defaultFormat = "HH:mm:ss";
if (length <= 0) {
return defaultFormat;
}
StringBuilder format = new StringBuilder(defaultFormat);
format.append(".");
for (int i = 0; i < length; i++) {
format.append("S");
private DateTimeFormatter timeFormat(int length) {
final DateTimeFormatterBuilder dtf = new DateTimeFormatterBuilder()
.appendPattern("HH:mm:ss");
if (length !=-1) {
dtf.appendFraction(ChronoField.MICRO_OF_SECOND, 0, length, true);
}
return format.toString();
return dtf.toFormatter();
}

private String timestampFormat(int length) {
String defaultFormat = "yyyy-MM-dd HH:mm:ss";
if (length <= 0) {
return defaultFormat;
}
StringBuilder format = new StringBuilder(defaultFormat);
format.append(".");
for (int i = 0; i < length; i++) {
format.append("S");
private DateTimeFormatter timestampFormat(int length) {
final DateTimeFormatterBuilder dtf = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd HH:mm:ss");
if (length !=-1) {
dtf.appendFraction(ChronoField.MICRO_OF_SECOND, 0, length, true);
}
return format.toString();
return dtf.toFormatter();
}
}
Expand Up @@ -19,6 +19,7 @@
import org.junit.Before;
import org.junit.Test;

import io.debezium.doc.FixFor;
import io.debezium.jdbc.JdbcValueConverters;
import io.debezium.jdbc.TemporalPrecisionMode;
import io.debezium.relational.Table;
Expand Down Expand Up @@ -356,5 +357,18 @@ public void parseTimeDefaultValue() {
assertThat(table.columnWithName("L").defaultValue()).isEqualTo(Date.from(ZonedDateTime.of(2018, 6, 26, 12, 34, 56, 780_000_000, ZoneOffset.UTC).toInstant()));
}

@Test
@FixFor("DBZ-901")
public void parseAlterTableTruncatedDefaulDateTime() {
String sql = "CREATE TABLE TIME_TABLE (" +
" A datetime(3) NOT NULL DEFAULT '0000-00-00 00:00:00.000'" +
");";
String alterSql = "ALTER TABLE TIME_TABLE ADD COLUMN B DATETIME(3) NOT NULL DEFAULT '1970-01-01 00:00:00';";
parser.parse(sql, tables);
parser.parse(alterSql, tables);
Table table = tables.forTable(new TableId(null, null, "TIME_TABLE"));
assertThat(table.columnWithName("A").defaultValue()).isEqualTo((Date.from(Instant.ofEpochMilli(0))));
assertThat(table.columnWithName("B").defaultValue()).isEqualTo((Date.from(Instant.ofEpochMilli(0))));
}

}