Skip to content

Commit 10d7c8c

Browse files
authored
Merge pull request #5 from fivetran/deserializer-fix-precesion
fix(AbstractRowsEventDataDeserializer): correct fsp accuracy for datetime/timestamp v2
2 parents 3f1bc55 + e26e2d7 commit 10d7c8c

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/AbstractRowsEventDataDeserializer.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
import java.io.IOException;
2323
import java.io.Serializable;
2424
import java.math.BigDecimal;
25+
import java.sql.Time;
2526
import java.util.BitSet;
2627
import java.util.Calendar;
27-
import java.util.Date;
2828
import java.util.Map;
2929
import java.util.TimeZone;
3030

@@ -266,7 +266,7 @@ protected Serializable deserializeTime(ByteArrayInputStream inputStream) throws
266266
if (deserializeDateAndTimeAsLong) {
267267
return castTimestamp(timestamp, 0);
268268
}
269-
return timestamp != null ? new java.sql.Time(timestamp) : null;
269+
return timestamp != null ? new java.sql.Timestamp(timestamp) : null;
270270
}
271271

272272
protected Serializable deserializeTimeV2(int meta, ByteArrayInputStream inputStream) throws IOException {
@@ -294,7 +294,7 @@ protected Serializable deserializeTimeV2(int meta, ByteArrayInputStream inputStr
294294
if (deserializeDateAndTimeAsLong) {
295295
return castTimestamp(timestamp, fsp);
296296
}
297-
return timestamp != null ? new java.sql.Time(timestamp) : null;
297+
return timestamp != null ? convertLongTimestamptWithFSP(timestamp, fsp) : null;
298298
}
299299

300300
protected Serializable deserializeTimestamp(ByteArrayInputStream inputStream) throws IOException {
@@ -312,7 +312,7 @@ protected Serializable deserializeTimestampV2(int meta, ByteArrayInputStream inp
312312
if (deserializeDateAndTimeAsLong) {
313313
return castTimestamp(timestamp, fsp);
314314
}
315-
return new java.sql.Timestamp(timestamp);
315+
return convertLongTimestamptWithFSP(timestamp, fsp);
316316
}
317317

318318
protected Serializable deserializeDatetime(ByteArrayInputStream inputStream) throws IOException {
@@ -321,7 +321,7 @@ protected Serializable deserializeDatetime(ByteArrayInputStream inputStream) thr
321321
if (deserializeDateAndTimeAsLong) {
322322
return castTimestamp(timestamp, 0);
323323
}
324-
return timestamp != null ? new java.util.Date(timestamp) : null;
324+
return timestamp != null ? new java.sql.Timestamp(timestamp) : null;
325325
}
326326

327327
protected Serializable deserializeDatetimeV2(int meta, ByteArrayInputStream inputStream) throws IOException {
@@ -354,7 +354,14 @@ protected Serializable deserializeDatetimeV2(int meta, ByteArrayInputStream inpu
354354
if (deserializeDateAndTimeAsLong) {
355355
return castTimestamp(timestamp, fsp);
356356
}
357-
return timestamp != null ? new java.util.Date(timestamp) : null;
357+
358+
return timestamp != null ? convertLongTimestamptWithFSP(timestamp, fsp) : null;
359+
}
360+
361+
private java.sql.Timestamp convertLongTimestamptWithFSP(Long timestamp, int fsp) {
362+
java.sql.Timestamp ts = new java.sql.Timestamp(timestamp);
363+
ts.setNanos(fsp * 1000);
364+
return ts;
358365
}
359366

360367
protected Serializable deserializeYear(ByteArrayInputStream inputStream) throws IOException {

0 commit comments

Comments
 (0)