Skip to content

Commit d4c91e6

Browse files
authored
Merge pull request #915 from cenkore/iss909
fix: issue 909
2 parents 62ce678 + db51387 commit d4c91e6

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

go/logic/inspect.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ func (this *Inspector) applyColumnTypes(databaseName, tableName string, columnsL
555555
err := sqlutils.QueryRowsMap(this.db, query, func(m sqlutils.RowMap) error {
556556
columnName := m.GetString("COLUMN_NAME")
557557
columnType := m.GetString("COLUMN_TYPE")
558+
columnOctetLength := m.GetUint("CHARACTER_OCTET_LENGTH")
558559
for _, columnsList := range columnsLists {
559560
column := columnsList.GetColumn(columnName)
560561
if column == nil {
@@ -582,6 +583,10 @@ func (this *Inspector) applyColumnTypes(databaseName, tableName string, columnsL
582583
if strings.HasPrefix(columnType, "enum") {
583584
column.Type = sql.EnumColumnType
584585
}
586+
if strings.HasPrefix(columnType, "binary") {
587+
column.Type = sql.BinaryColumnType
588+
column.BinaryOctetLength = columnOctetLength
589+
}
585590
if charset := m.GetString("CHARACTER_SET_NAME"); charset != "" {
586591
column.Charset = charset
587592
}

go/sql/builder.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func BuildDMLDeleteQuery(databaseName, tableName string, tableColumns, uniqueKey
396396
}
397397
for _, column := range uniqueKeyColumns.Columns() {
398398
tableOrdinal := tableColumns.Ordinals[column.Name]
399-
arg := column.convertArg(args[tableOrdinal])
399+
arg := column.convertArg(args[tableOrdinal], true)
400400
uniqueKeyArgs = append(uniqueKeyArgs, arg)
401401
}
402402
databaseName = EscapeName(databaseName)
@@ -433,7 +433,7 @@ func BuildDMLInsertQuery(databaseName, tableName string, tableColumns, sharedCol
433433

434434
for _, column := range sharedColumns.Columns() {
435435
tableOrdinal := tableColumns.Ordinals[column.Name]
436-
arg := column.convertArg(args[tableOrdinal])
436+
arg := column.convertArg(args[tableOrdinal], false)
437437
sharedArgs = append(sharedArgs, arg)
438438
}
439439

@@ -481,13 +481,13 @@ func BuildDMLUpdateQuery(databaseName, tableName string, tableColumns, sharedCol
481481

482482
for _, column := range sharedColumns.Columns() {
483483
tableOrdinal := tableColumns.Ordinals[column.Name]
484-
arg := column.convertArg(valueArgs[tableOrdinal])
484+
arg := column.convertArg(valueArgs[tableOrdinal], false)
485485
sharedArgs = append(sharedArgs, arg)
486486
}
487487

488488
for _, column := range uniqueKeyColumns.Columns() {
489489
tableOrdinal := tableColumns.Ordinals[column.Name]
490-
arg := column.convertArg(whereArgs[tableOrdinal])
490+
arg := column.convertArg(whereArgs[tableOrdinal], true)
491491
uniqueKeyArgs = append(uniqueKeyArgs, arg)
492492
}
493493

go/sql/types.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package sql
77

88
import (
9+
"bytes"
910
"fmt"
1011
"reflect"
1112
"strconv"
@@ -22,6 +23,7 @@ const (
2223
MediumIntColumnType
2324
JSONColumnType
2425
FloatColumnType
26+
BinaryColumnType
2527
)
2628

2729
const maxMediumintUnsigned int32 = 16777215
@@ -31,19 +33,36 @@ type TimezoneConversion struct {
3133
}
3234

3335
type Column struct {
34-
Name string
35-
IsUnsigned bool
36-
Charset string
37-
Type ColumnType
36+
Name string
37+
IsUnsigned bool
38+
Charset string
39+
Type ColumnType
40+
41+
// add Octet length for binary type, fix bytes with suffix "00" get clipped in mysql binlog.
42+
// https://github.com/github/gh-ost/issues/909
43+
BinaryOctetLength uint
3844
timezoneConversion *TimezoneConversion
3945
}
4046

41-
func (this *Column) convertArg(arg interface{}) interface{} {
47+
func (this *Column) convertArg(arg interface{}, isUniqueKeyColumn bool) interface{} {
4248
if s, ok := arg.(string); ok {
4349
// string, charset conversion
4450
if encoding, ok := charsetEncodingMap[this.Charset]; ok {
4551
arg, _ = encoding.NewDecoder().String(s)
4652
}
53+
54+
if this.Type == BinaryColumnType && isUniqueKeyColumn {
55+
arg2Bytes := []byte(arg.(string))
56+
size := len(arg2Bytes)
57+
if uint(size) < this.BinaryOctetLength {
58+
buf := bytes.NewBuffer(arg2Bytes)
59+
for i := uint(0); i < (this.BinaryOctetLength - uint(size)); i++ {
60+
buf.Write([]byte{0})
61+
}
62+
arg = buf.String()
63+
}
64+
}
65+
4766
return arg
4867
}
4968

0 commit comments

Comments
 (0)