You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/04/16 08:22:04 UTC

(shardingsphere) branch master updated: Pipeline: Fix MySQL datetime(4) zero-value binlog decoding in migration (#38629)

This is an automated email from the ASF dual-hosted git repository.

zhonghongsheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new cdcc0f470f4 Pipeline: Fix MySQL datetime(4) zero-value binlog decoding in migration (#38629)
cdcc0f470f4 is described below

commit cdcc0f470f408f1538f5cff8d3859420a6566426
Author: Hongsheng Zhong <zh...@apache.org>
AuthorDate: Thu Apr 16 16:21:57 2026 +0800

    Pipeline: Fix MySQL datetime(4) zero-value binlog decoding in migration (#38629)
    
    * test: add MySQL fractional datetime migration E2E
    
    * protocol-mysql: consume DATETIME2 fractional bytes for zero value
    
    * release-notes: add MySQL datetime(4) migration fix entry
    
    * test: merge MySQL fractional datetime migration E2E
    
    * Revert "protocol-mysql: consume DATETIME2 fractional bytes for zero value"
    
    * test: stabilize MySQL time migration E2E reproduction
    
    * Reapply "protocol-mysql: consume DATETIME2 fractional bytes for zero value"
---
 RELEASE-NOTES.md                                   |  1 +
 .../time/MySQLDatetime2BinlogProtocolValue.java    | 13 ++++++-
 .../MySQLDatetime2BinlogProtocolValueTest.java     | 21 ++++++++++++
 .../general/MySQLTimeTypesMigrationE2EIT.java      | 40 ++++++++++++++++++----
 4 files changed, 68 insertions(+), 7 deletions(-)

diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index 154f7184c1c..9fac156794e 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -20,6 +20,7 @@
 1. Proxy: Fix primary key metadata loss for aliased columns in mysql prepare probe path - [#38517](https://github.com/apache/shardingsphere/pull/38517)
 1. Sharding: Fix incorrect routing when irrelevant sharding conditions are present - [#38527](https://github.com/apache/shardingsphere/pull/38527)
 1. Pipeline: Fix MySQL JSON literal decoding in migration - [#38622](https://github.com/apache/shardingsphere/pull/38622)
+1. Pipeline: Fix MySQL datetime(4) zero-value binlog decoding in migration - [#35531](https://github.com/apache/shardingsphere/issues/35531)
 
 ### Enhancements
 
diff --git a/database/protocol/dialect/mysql/src/main/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/row/column/value/time/MySQLDatetime2BinlogProtocolValue.java b/database/protocol/dialect/mysql/src/main/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/row/column/value/time/MySQLDatetime2BinlogProtocolValue.java
index 5359885d130..9520f17b91c 100644
--- a/database/protocol/dialect/mysql/src/main/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/row/column/value/time/MySQLDatetime2BinlogProtocolValue.java
+++ b/database/protocol/dialect/mysql/src/main/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/row/column/value/time/MySQLDatetime2BinlogProtocolValue.java
@@ -37,7 +37,11 @@ public final class MySQLDatetime2BinlogProtocolValue implements MySQLBinlogProto
     @Override
     public Serializable read(final MySQLBinlogColumnDef columnDef, final MySQLPacketPayload payload) {
         long datetime = readDatetimeV2FromPayload(payload);
-        return 0L == datetime ? MySQLTimeValueUtils.DATETIME_OF_ZERO : readDatetime(columnDef, datetime, payload);
+        if (0L == datetime) {
+            skipFractionalSeconds(columnDef, payload);
+            return MySQLTimeValueUtils.DATETIME_OF_ZERO;
+        }
+        return readDatetime(columnDef, datetime, payload);
     }
     
     private long readDatetimeV2FromPayload(final MySQLPacketPayload payload) {
@@ -51,6 +55,7 @@ public final class MySQLDatetime2BinlogProtocolValue implements MySQLBinlogProto
     private Serializable readDatetime(final MySQLBinlogColumnDef columnDef, final long datetime, final MySQLPacketPayload payload) {
         long datetimeWithoutSign = datetime & (0x8000000000L - 1L);
         if (0 == datetimeWithoutSign) {
+            skipFractionalSeconds(columnDef, payload);
             return MySQLTimeValueUtils.DATETIME_OF_ZERO;
         }
         long date = datetimeWithoutSign >> 17;
@@ -65,4 +70,10 @@ public final class MySQLDatetime2BinlogProtocolValue implements MySQLBinlogProto
         MySQLFractionalSeconds fractionalSeconds = new MySQLFractionalSeconds(columnDef.getColumnMeta(), payload);
         return Timestamp.valueOf(LocalDateTime.of(year, month, day, hour, minute, second, fractionalSeconds.getNanos()));
     }
+    
+    private void skipFractionalSeconds(final MySQLBinlogColumnDef columnDef, final MySQLPacketPayload payload) {
+        if (columnDef.getColumnMeta() > 0) {
+            new MySQLFractionalSeconds(columnDef.getColumnMeta(), payload);
+        }
+    }
 }
diff --git a/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/row/column/value/time/MySQLDatetime2BinlogProtocolValueTest.java b/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/row/column/value/time/MySQLDatetime2BinlogProtocolValueTest.java
index 8eeb6b95abd..33b394171cf 100644
--- a/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/row/column/value/time/MySQLDatetime2BinlogProtocolValueTest.java
+++ b/database/protocol/dialect/mysql/src/test/java/org/apache/shardingsphere/database/protocol/mysql/packet/binlog/row/column/value/time/MySQLDatetime2BinlogProtocolValueTest.java
@@ -37,6 +37,7 @@ import java.util.stream.Stream;
 
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 @ExtendWith(MockitoExtension.class)
@@ -88,12 +89,32 @@ class MySQLDatetime2BinlogProtocolValueTest {
         assertThat(protocolValue.read(columnDef, payload), is(MySQLTimeValueUtils.DATETIME_OF_ZERO));
     }
     
+    @Test
+    void assertReadWithZeroDatetimeAndFraction() {
+        columnDef.setColumnMeta(4);
+        when(payload.getByteBuf()).thenReturn(byteBuf);
+        when(payload.readInt1()).thenReturn(0x00, 0x00, 0x00, 0x00, 0x00);
+        when(byteBuf.readUnsignedShort()).thenReturn(0);
+        assertThat(protocolValue.read(columnDef, payload), is(MySQLTimeValueUtils.DATETIME_OF_ZERO));
+        verify(byteBuf).readUnsignedShort();
+    }
+    
     @Test
     void assertReadWithSignedZeroDatetime() {
         when(payload.readInt1()).thenReturn(0x80, 0x00, 0x00, 0x00, 0x00);
         assertThat(protocolValue.read(columnDef, payload), is(MySQLTimeValueUtils.DATETIME_OF_ZERO));
     }
     
+    @Test
+    void assertReadWithSignedZeroDatetimeAndFraction() {
+        columnDef.setColumnMeta(4);
+        when(payload.getByteBuf()).thenReturn(byteBuf);
+        when(payload.readInt1()).thenReturn(0x80, 0x00, 0x00, 0x00, 0x00);
+        when(byteBuf.readUnsignedShort()).thenReturn(0);
+        assertThat(protocolValue.read(columnDef, payload), is(MySQLTimeValueUtils.DATETIME_OF_ZERO));
+        verify(byteBuf).readUnsignedShort();
+    }
+    
     private static Stream<Arguments> readCases() {
         return Stream.of(
                 Arguments.of("no_fraction", 0, new int[]{0xfe, 0xf3, 0xff, 0x7e, 0xfb}, null, null, LocalDateTime.of(9999, 12, 31, 23, 59, 59)),
diff --git a/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/operation/pipeline/cases/migration/general/MySQLTimeTypesMigrationE2EIT.java b/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/operation/pipeline/cases/migration/general/MySQLTimeTypesMigrationE2EIT.java
index 3d4aa57205c..c54e57335c4 100644
--- a/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/operation/pipeline/cases/migration/general/MySQLTimeTypesMigrationE2EIT.java
+++ b/test/e2e/operation/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/operation/pipeline/cases/migration/general/MySQLTimeTypesMigrationE2EIT.java
@@ -33,10 +33,17 @@ import org.junit.jupiter.params.provider.ArgumentsSource;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
+import java.util.List;
+import java.util.Map;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
 
 /**
  * E2E IT for time types of MySQL, includes.
- * 1) datetime,timestamp,date default null
+ * 1) timestamp, datetime, date, year zero values
+ * 2) datetime(4) zero default value in incremental migration
+ * 3) trailing date default value after datetime(4)
  */
 @PipelineE2ESettings(fetchSingle = true, database = @PipelineE2ESettings.PipelineE2EDatabaseSettings(type = "MySQL"))
 class MySQLTimeTypesMigrationE2EIT extends AbstractMigrationE2EIT {
@@ -47,23 +54,38 @@ class MySQLTimeTypesMigrationE2EIT extends AbstractMigrationE2EIT {
     void assertIllegalTimeTypesValueMigrationSuccess(final PipelineTestParameter testParam) throws Exception {
         try (PipelineContainerComposer containerComposer = new PipelineContainerComposer(testParam)) {
             String sql = "CREATE TABLE `time_e2e` ( `id` int NOT NULL, `t_timestamp` timestamp NULL DEFAULT NULL, `t_datetime` datetime DEFAULT NULL, `t_date` date DEFAULT NULL, "
-                    + "`t_year` year DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;";
+                    + "`t_year` year DEFAULT NULL, `t_datetime_4` datetime(4) DEFAULT '0000-00-00 00:00:00.0000', "
+                    + "`t_date_tail` date NOT NULL DEFAULT '2024-01-01', PRIMARY KEY (`id`)) ENGINE=InnoDB;";
             containerComposer.sourceExecuteWithLog(sql);
-            insertOneRecordWithZeroValue(containerComposer, 1);
+            insertInventoryRecordWithZeroValue(containerComposer, 1);
             addMigrationSourceResource(containerComposer);
             addMigrationTargetResource(containerComposer);
             startMigration(containerComposer, "time_e2e", "time_e2e");
             PipelineE2EDistSQLFacade distSQLFacade = new PipelineE2EDistSQLFacade(containerComposer, new MigrationJobType());
             String jobId = distSQLFacade.listJobIds().get(0);
-            distSQLFacade.waitJobPreparingStageFinished(jobId);
-            insertOneRecordWithZeroValue(containerComposer, 2);
+            distSQLFacade.waitJobIncrementalStageStarted(jobId);
+            insertIncrementalRecordWithZeroDefaultValue(containerComposer, 2);
             distSQLFacade.waitJobIncrementalStageFinished(jobId);
             distSQLFacade.loadAllSingleTables();
+            assertTargetRowCount(containerComposer, 2);
             distSQLFacade.startCheckAndVerify(jobId, "DATA_MATCH");
         }
     }
     
-    private void insertOneRecordWithZeroValue(final PipelineContainerComposer containerComposer, final int id) throws SQLException {
+    private void insertInventoryRecordWithZeroValue(final PipelineContainerComposer containerComposer, final int id) throws SQLException {
+        try (Connection connection = containerComposer.getSourceDataSource().getConnection()) {
+            PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `time_e2e`(id, t_timestamp, t_datetime, t_date, t_year, t_datetime_4) VALUES (?, ?, ?, ?, ?, ?)");
+            preparedStatement.setObject(1, id);
+            preparedStatement.setObject(2, "0000-00-00 00:00:00");
+            preparedStatement.setObject(3, "0000-00-00 00:00:00");
+            preparedStatement.setObject(4, "0000-00-00");
+            preparedStatement.setObject(5, "0000");
+            preparedStatement.setObject(6, null);
+            preparedStatement.execute();
+        }
+    }
+    
+    private void insertIncrementalRecordWithZeroDefaultValue(final PipelineContainerComposer containerComposer, final int id) throws SQLException {
         try (Connection connection = containerComposer.getSourceDataSource().getConnection()) {
             PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO `time_e2e`(id, t_timestamp, t_datetime, t_date, t_year) VALUES (?, ?, ?, ?, ?)");
             preparedStatement.setObject(1, id);
@@ -75,6 +97,12 @@ class MySQLTimeTypesMigrationE2EIT extends AbstractMigrationE2EIT {
         }
     }
     
+    private void assertTargetRowCount(final PipelineContainerComposer containerComposer, final int expectedRowCount) {
+        List<Map<String, Object>> result = containerComposer.queryForListWithLog("SELECT COUNT(1) AS count FROM time_e2e");
+        int actualRowCount = ((Number) result.get(0).get("count")).intValue();
+        assertThat(actualRowCount, is(expectedRowCount));
+    }
+    
     private static boolean isEnabled(final ExtensionContext context) {
         return PipelineE2ECondition.isEnabled(context);
     }