Repository: pingcap/tidb-binlog
Pull request #1106 (2021-12-23 09:07:05)

reparo: fix can't decode bit type data (#1103)
This is an automated cherry-pick of #1103

<!--
Thank you for contributing to TiDB! Please read TiDB's [CONTRIBUTING](https://github.com/pingcap/tidb/blob/master/CONTRIBUTING.md) document **BEFORE** filing this PR.
-->

### What problem does this PR solve? <!--add issue link with summary if exists-->
close https://github.com/pingcap/tidb-binlog/issues/1096

### What is changed and how it works?
in https://github.com/pingcap/tidb-binlog/pull/655, drainer will write Uint64 datum for mysql bit type. align reparo's behaviour

### Check List <!--REMOVE the items that are not applicable-->

Tests <!-- At least one of them must be included. -->

 - Unit test
 - Integration test

Code changes


Side effects


Related changes

 - Need to cherry-pick to the release branch
 - Need to be included in the release note

ti-chi-bot: [REVIEW NOTIFICATION]

This pull request has not been approved.


To complete the [pull request process](https://book.prow.tidb.io/#/en/workflows/pr), please ask the reviewers in the [list](https://prow.tidb.io/tichi/repos/pingcap/tidb-binlog/pulls/1106/owners) to review by filling `/cc @reviewer` in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the [list](https://prow.tidb.io/tichi/repos/pingcap/tidb-binlog/pulls/1106/owners) by filling  `/assign @committer` in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found [here](https://prow.tidb.io/command-help?repo=pingcap%2Ftidb-binlog).

<details>

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.
</details>

<!--Review Notification Identifier-->
ti-chi-bot: @ti-chi-bot: This cherry pick PR is for a release branch and has not yet been approved by release team.
Adding the `do-not-merge/cherry-pick-not-approved` label.

To merge this cherry pick, it must first be approved by the collaborators.

**AFTER** it has been approved by collaborators, please ping the release team in a comment to request a cherry pick review.


<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
</details>
CLAassistant: [![CLA assistant check](https://cla-assistant.io/pull/badge/not_signed)](https://cla-assistant.io/pingcap/tidb-binlog?pullRequest=1106) <br/>Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our [Contributor License Agreement](https://cla-assistant.io/pingcap/tidb-binlog?pullRequest=1106) before we can accept your contribution.<br/><sub>You have signed the CLA already but the status is still pending? Let us [recheck](https://cla-assistant.io/check/pingcap/tidb-binlog?pullRequest=1106) it.</sub>
lichunzhu: /run-all-tests
lance6716: /run-integration-test
niubell: /run-integration-test 
lichunzhu: /run-all-tests


lichunzhu: /run-all-tests


lichunzhu: /merge
ti-chi-bot: This pull request has been accepted and is ready to merge. <details>Commit hash: 0c3aee3bc660353c9596cd1d1fae25ca15b8bec5</details>

Patch:
diff --git a/reparo/syncer/util.go b/reparo/syncer/util.go
index 3b172c6f..d08274ff 100644
--- a/reparo/syncer/util.go
+++ b/reparo/syncer/util.go
@@ -46,7 +46,8 @@ func formatValue(value types.Datum, tp byte) types.Datum {
 	case mysql.TypeSet:
 		value = types.NewDatum(value.GetMysqlSet().Value)
 	case mysql.TypeBit:
-		value = types.NewDatum(value.GetMysqlBit())
+		// see drainer/translator/mysql.go formatData
+		value = types.NewDatum(value.GetUint64())
 	}
 
 	return value


Test patch:
diff --git a/reparo/syncer/util_test.go b/reparo/syncer/util_test.go
index a84f2226..7c593252 100644
--- a/reparo/syncer/util_test.go
+++ b/reparo/syncer/util_test.go
@@ -15,8 +15,7 @@ var _ = check.Suite(&testUtilSuite{})
 func (s *testUtilSuite) TestFormatValue(c *check.C) {
 	datetime, err := time.Parse("20060102150405", "20190415121212")
 	c.Assert(err, check.IsNil)
-	bitVal, err2 := types.NewBitLiteral("b'10010'")
-	c.Assert(err2, check.IsNil)
+	bitVal := uint64(12)
 
 	testCases := []struct {
 		value     interface{}
@@ -81,8 +80,8 @@ func (s *testUtilSuite) TestFormatValue(c *check.C) {
 		{
 			value:     bitVal,
 			tp:        mysql.TypeBit,
-			expectStr: "0x12",
-			expectVal: types.BinaryLiteral([]byte{0x12}),
+			expectStr: "12",
+			expectVal: uint64(12),
 		},
 	}
 
diff --git a/tests/dailytest/db.go b/tests/dailytest/db.go
index 27c90887..820ec485 100644
--- a/tests/dailytest/db.go
+++ b/tests/dailytest/db.go
@@ -221,6 +221,11 @@ func genColumnData(table *table, column *column) (string, error) {
 	isUnsigned := mysql.HasUnsignedFlag(tp.Flag)
 
 	switch tp.Tp {
+	case mysql.TypeBit:
+		if randInt(0, 1) == 0 {
+			return "b'0'", nil
+		}
+		return "b'1'", nil
 	case mysql.TypeTiny:
 		var data int64
 		if isUnique {
diff --git a/tests/reparo/binlog.go b/tests/reparo/binlog.go
index 7df0bc5a..3f6fb887 100644
--- a/tests/reparo/binlog.go
+++ b/tests/reparo/binlog.go
@@ -20,6 +20,7 @@ import (
 	_ "github.com/go-sql-driver/mysql"
 	"github.com/pingcap/errors"
 	"github.com/pingcap/log"
+
 	"github.com/pingcap/tidb-binlog/tests/dailytest"
 	"github.com/pingcap/tidb-binlog/tests/util"
 )
@@ -70,6 +71,14 @@ func main() {
 		c varchar(10) NOT NULL,
 		d time unique
 	);
+	`,
+		`
+	create table ntest2(
+		a int,
+		b double NOT NULL DEFAULT 2.0,
+		c varchar(10) NOT NULL,
+		d bit(20)
+	);
 	`}
 
 	dailytest.RunDailyTest(sourceDB, tableSQLs, cfg.WorkerCount, cfg.JobCount, cfg.Batch)