Skip to content

Commit 730dc65

Browse files
committed
Fixed fractional seconds deserialization (#103)
1 parent efd6096 commit 730dc65

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -270,34 +270,14 @@ private java.util.Date deserializeDatetimeV2(int meta, ByteArrayInputStream inpu
270270
}
271271

272272
private int getFractionalSeconds(int meta, ByteArrayInputStream inputStream) throws IOException {
273-
int fractionalSecondsStorageSize = getFractionalSecondsStorageSize(meta);
274-
if (fractionalSecondsStorageSize > 0) {
275-
long fractionalSeconds = bigEndianLong(inputStream.read(fractionalSecondsStorageSize), 0,
276-
fractionalSecondsStorageSize);
277-
if (meta % 2 == 1) {
278-
fractionalSeconds /= 10;
279-
}
280-
return (int) (fractionalSeconds / 1000);
273+
int length = (meta + 1) / 2;
274+
if (length > 0) {
275+
long fraction = bigEndianLong(inputStream.read(length), 0, length);
276+
return (int) (fraction / (0.1 * Math.pow(100, length - 1)));
281277
}
282278
return 0;
283279
}
284280

285-
private static int getFractionalSecondsStorageSize(int fsp) {
286-
switch (fsp) {
287-
case 1:
288-
case 2:
289-
return 1;
290-
case 3:
291-
case 4:
292-
return 2;
293-
case 5:
294-
case 6:
295-
return 3;
296-
default:
297-
return 0;
298-
}
299-
}
300-
301281
private static int extractBits(long value, int bitOffset, int numberOfBits, int payloadSize) {
302282
long result = value >> payloadSize - (bitOffset + numberOfBits);
303283
return (int) (result & ((1 << numberOfBits) - 1));

src/test/java/com/github/shyiko/mysql/binlog/BinaryLogClientIntegrationTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.github.shyiko.mysql.binlog.network.AuthenticationException;
3434
import com.github.shyiko.mysql.binlog.network.ServerException;
3535
import org.mockito.InOrder;
36+
import org.testng.SkipException;
3637
import org.testng.annotations.AfterClass;
3738
import org.testng.annotations.AfterMethod;
3839
import org.testng.annotations.BeforeClass;
@@ -49,6 +50,7 @@
4950
import java.sql.Connection;
5051
import java.sql.DriverManager;
5152
import java.sql.SQLException;
53+
import java.sql.SQLSyntaxErrorException;
5254
import java.sql.Statement;
5355
import java.util.BitSet;
5456
import java.util.Calendar;
@@ -240,6 +242,36 @@ public void testDeserializationOfDifferentColumnTypes() throws Exception {
240242
assertEquals(writeAndCaptureRow("set('a','b','c')", "'a,c'"), new Serializable[]{5L});
241243
}
242244

245+
@Test
246+
public void testFSP() throws Exception {
247+
try {
248+
master.execute(new Callback<Statement>() {
249+
@Override
250+
public void execute(Statement statement) throws SQLException {
251+
statement.execute("create table fsp_check (column_ datetime(0))");
252+
}
253+
});
254+
} catch (SQLSyntaxErrorException e) {
255+
throw new SkipException("MySQL < 5.6.4+");
256+
}
257+
assertEquals(writeAndCaptureRow("datetime(0)", "'1989-03-21 01:02:03.777777'"), new Serializable[]{
258+
new java.util.Date(generateTime(1989, 3, 21, 1, 2, 4, 0))});
259+
assertEquals(writeAndCaptureRow("datetime(1)", "'1989-03-21 01:02:03.777777'"), new Serializable[]{
260+
new java.util.Date(generateTime(1989, 3, 21, 1, 2, 3, 800))});
261+
assertEquals(writeAndCaptureRow("datetime(2)", "'1989-03-21 01:02:03.777777'"), new Serializable[]{
262+
new java.util.Date(generateTime(1989, 3, 21, 1, 2, 3, 780))});
263+
assertEquals(writeAndCaptureRow("datetime(3)", "'1989-03-21 01:02:03.777777'"), new Serializable[]{
264+
new java.util.Date(generateTime(1989, 3, 21, 1, 2, 3, 778))});
265+
assertEquals(writeAndCaptureRow("datetime(3)", "'1989-03-21 01:02:03.777'"), new Serializable[]{
266+
new java.util.Date(generateTime(1989, 3, 21, 1, 2, 3, 777))});
267+
assertEquals(writeAndCaptureRow("datetime(4)", "'1989-03-21 01:02:03.777777'"), new Serializable[]{
268+
new java.util.Date(generateTime(1989, 3, 21, 1, 2, 3, 777))});
269+
assertEquals(writeAndCaptureRow("datetime(5)", "'1989-03-21 01:02:03.777777'"), new Serializable[]{
270+
new java.util.Date(generateTime(1989, 3, 21, 1, 2, 3, 777))});
271+
assertEquals(writeAndCaptureRow("datetime(6)", "'1989-03-21 01:02:03.777777'"), new Serializable[]{
272+
new java.util.Date(generateTime(1989, 3, 21, 1, 2, 3, 777))});
273+
}
274+
243275
private BitSet bitSet(int... bitsToSetTrue) {
244276
BitSet result = new BitSet(bitsToSetTrue.length);
245277
for (int bit : bitsToSetTrue) {

0 commit comments

Comments
 (0)