Skip to content

Commit

Permalink
fix: remove some vitess types (arana-db#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjeffcaii authored Jun 26, 2022
1 parent 7bf2975 commit 162dd69
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 75 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ services:
arana:
build: .
container_name: arana
image: aranadb/arana:latest
image: aranadb/arana:master
networks:
- local
ports:
Expand Down
53 changes: 3 additions & 50 deletions pkg/constants/mysql/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ const (
FieldTypeBit
)

const (
FieldTypeUint8 FieldType = iota + 0x85
FieldTypeUint16
FieldTypeUint24
FieldTypeUint32
FieldTypeUint64
)

const (
FieldTypeJSON FieldType = iota + 0xf5
FieldTypeNewDecimal
Expand Down Expand Up @@ -118,48 +110,14 @@ var mysqlToType = map[int64]FieldType{
255: FieldTypeGeometry,
}

// modifyType modifies the vitess type based on the
// mysql flag. The function checks specific flags based
// on the type. This allows us to ignore stray flags
// that MySQL occasionally sets.
func modifyType(typ FieldType, flags int64) FieldType {
switch typ {
case FieldTypeTiny:
if uint(flags)&UnsignedFlag != 0 {
return FieldTypeUint8
}
return FieldTypeTiny
case FieldTypeShort:
if uint(flags)&UnsignedFlag != 0 {
return FieldTypeUint16
}
return FieldTypeShort
case FieldTypeLong:
if uint(flags)&UnsignedFlag != 0 {
return FieldTypeUint32
}
return FieldTypeLong
case FieldTypeLongLong:
if uint(flags)&UnsignedFlag != 0 {
return FieldTypeUint64
}
return FieldTypeLongLong
case FieldTypeInt24:
if uint(flags)&UnsignedFlag != 0 {
return FieldTypeUint24
}
return FieldTypeInt24
}
return typ
}

// MySQLToType computes the vitess type from mysql type and flags.
func MySQLToType(mysqlType, flags int64) (typ FieldType, err error) {
func MySQLToType(mysqlType, flags int64) (FieldType, error) {
_ = flags
result, ok := mysqlToType[mysqlType]
if !ok {
return 0, fmt.Errorf("unsupported type: %d", mysqlType)
}
return modifyType(result, flags), nil
return result, nil
}

// typeToMySQL is the reverse of MysqlToType.
Expand All @@ -168,19 +126,14 @@ var typeToMySQL = map[FieldType]struct {
flags int64
}{
FieldTypeTiny: {typ: 1},
FieldTypeUint8: {typ: 1, flags: int64(UnsignedFlag)},
FieldTypeShort: {typ: 2},
FieldTypeUint16: {typ: 2, flags: int64(UnsignedFlag)},
FieldTypeLong: {typ: 3},
FieldTypeUint32: {typ: 3, flags: int64(UnsignedFlag)},
FieldTypeFloat: {typ: 4},
FieldTypeDouble: {typ: 5},
FieldTypeNULL: {typ: 6, flags: int64(BinaryFlag)},
FieldTypeTimestamp: {typ: 7},
FieldTypeLongLong: {typ: 8},
FieldTypeUint64: {typ: 8, flags: int64(UnsignedFlag)},
FieldTypeInt24: {typ: 9},
FieldTypeUint24: {typ: 9, flags: int64(UnsignedFlag)},
FieldTypeDate: {typ: 10, flags: int64(BinaryFlag)},
FieldTypeTime: {typ: 11, flags: int64(BinaryFlag)},
FieldTypeDateTime: {typ: 12, flags: int64(BinaryFlag)},
Expand Down
9 changes: 0 additions & 9 deletions pkg/constants/mysql/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ func TestMySQLToType(t *testing.T) {
{0, 0, FieldTypeDecimal},
{0, 32, FieldTypeDecimal},
{1, 0, FieldTypeTiny},
{1, 32, FieldTypeUint8},
{2, 0, FieldTypeShort},
{2, 32, FieldTypeUint16},
{3, 0, FieldTypeLong},
{3, 32, FieldTypeUint32},
{4, 0, FieldTypeFloat},
{4, 32, FieldTypeFloat},
{5, 0, FieldTypeDouble},
Expand All @@ -48,9 +45,7 @@ func TestMySQLToType(t *testing.T) {
{7, 0, FieldTypeTimestamp},
{7, 32, FieldTypeTimestamp},
{8, 0, FieldTypeLongLong},
{8, 32, FieldTypeUint64},
{9, 0, FieldTypeInt24},
{9, 32, FieldTypeUint24},
{10, 0, FieldTypeDate},
{10, 32, FieldTypeDate},
{11, 0, FieldTypeTime},
Expand Down Expand Up @@ -108,18 +103,14 @@ func TestTypeToMySQL(t *testing.T) {
expectedFlags int64
}{
{FieldTypeTiny, int64(1), int64(0)},
{FieldTypeUint8, int64(1), int64(UnsignedFlag)},
{FieldTypeShort, int64(2), int64(0)},
{FieldTypeLong, int64(3), int64(0)},
{FieldTypeUint32, int64(3), int64(UnsignedFlag)},
{FieldTypeFloat, int64(4), int64(0)},
{FieldTypeDouble, int64(5), int64(0)},
{FieldTypeNULL, int64(6), int64(BinaryFlag)},
{FieldTypeTimestamp, int64(7), int64(0)},
{FieldTypeLongLong, int64(8), int64(0)},
{FieldTypeUint64, int64(8), int64(UnsignedFlag)},
{FieldTypeInt24, int64(9), int64(0)},
{FieldTypeUint24, int64(9), int64(UnsignedFlag)},
{FieldTypeDate, int64(10), int64(BinaryFlag)},
{FieldTypeTime, int64(11), int64(BinaryFlag)},
{FieldTypeDateTime, int64(12), int64(BinaryFlag)},
Expand Down
4 changes: 2 additions & 2 deletions pkg/mysql/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ func (te TextRow) Scan(dest []proto.Value) error {
switch te.fields[i].(*Field).fieldType {
case mysql.FieldTypeString, mysql.FieldTypeVarString, mysql.FieldTypeVarChar:
dest[i] = string(b)
case mysql.FieldTypeTiny, mysql.FieldTypeShort, mysql.FieldTypeLong, mysql.FieldTypeInt24,
mysql.FieldTypeLongLong, mysql.FieldTypeYear:
case mysql.FieldTypeTiny, mysql.FieldTypeShort, mysql.FieldTypeLong,
mysql.FieldTypeInt24, mysql.FieldTypeLongLong, mysql.FieldTypeYear:
if te.fields[i].(*Field).flags&mysql.UnsignedFlag > 0 {
dest[i], err = strconv.ParseUint(string(b), 10, 64)
} else {
Expand Down
14 changes: 1 addition & 13 deletions pkg/mysql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,27 +745,15 @@ func (c *Conn) parseStmtArgs(data []byte, typ mysql.FieldType, pos int) (interfa
case mysql.FieldTypeTiny:
val, pos, ok := readByte(data, pos)
return int64(int8(val)), pos, ok
case mysql.FieldTypeUint8:
val, pos, ok := readByte(data, pos)
return int64(int8(val)), pos, ok
case mysql.FieldTypeUint16:
val, pos, ok := readUint16(data, pos)
return int64(int16(val)), pos, ok
case mysql.FieldTypeShort, mysql.FieldTypeYear:
val, pos, ok := readUint16(data, pos)
return int64(int16(val)), pos, ok
case mysql.FieldTypeUint24, mysql.FieldTypeUint32:
val, pos, ok := readUint32(data, pos)
return int64(val), pos, ok
case mysql.FieldTypeInt24, mysql.FieldTypeLong:
val, pos, ok := readUint32(data, pos)
return int64(int32(val)), pos, ok
case mysql.FieldTypeFloat:
val, pos, ok := readUint32(data, pos)
return math.Float32frombits(uint32(val)), pos, ok
case mysql.FieldTypeUint64:
val, pos, ok := readUint64(data, pos)
return val, pos, ok
return math.Float32frombits(val), pos, ok
case mysql.FieldTypeLongLong:
val, pos, ok := readUint64(data, pos)
return int64(val), pos, ok
Expand Down

0 comments on commit 162dd69

Please sign in to comment.