Skip to content

Commit d1e31b0

Browse files
committed
add test ability to see bit nums
1 parent 47dc55b commit d1e31b0

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

enginetest/server_engine.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,16 @@ func rowIterForGoSqlRows(ctx *sql.Context, sch sql.Schema, rows *gosql.Rows) (sq
378378
func convertValue(ctx *sql.Context, sch sql.Schema, row sql.Row) sql.Row {
379379
for i, col := range sch {
380380
switch col.Type.Type() {
381+
case query.Type_BIT:
382+
if row[i] != nil {
383+
if bytes, ok := row[i].([]byte); ok {
384+
if bt, ok := col.Type.(types.BitType); ok {
385+
if v, _, err := bt.Convert(ctx, bytes); err == nil {
386+
row[i] = v
387+
}
388+
}
389+
}
390+
}
381391
case query.Type_GEOMETRY:
382392
if row[i] != nil {
383393
r, _, err := types.GeometryType{}.Convert(ctx, row[i].([]byte))
@@ -562,9 +572,12 @@ func emptyRowForSchema(sch sql.Schema) ([]any, error) {
562572
func emptyValuePointerForType(t sql.Type) (any, error) {
563573
switch t.Type() {
564574
case query.Type_INT8, query.Type_INT16, query.Type_INT24, query.Type_INT64,
565-
query.Type_BIT, query.Type_YEAR:
575+
query.Type_YEAR:
566576
var i gosql.NullInt64
567577
return &i, nil
578+
case query.Type_BIT:
579+
var b []byte
580+
return &b, nil
568581
case query.Type_INT32:
569582
var i gosql.NullInt32
570583
return &i, nil
@@ -624,8 +637,10 @@ func schemaForRows(rows *gosql.Rows) (sql.Schema, error) {
624637

625638
func convertGoSqlType(columnType *gosql.ColumnType) (sql.Type, error) {
626639
switch strings.ToLower(columnType.DatabaseTypeName()) {
627-
case "tinyint", "smallint", "mediumint", "int", "bigint", "bit":
640+
case "tinyint", "smallint", "mediumint", "int", "bigint":
628641
return types.Int64, nil
642+
case "bit":
643+
return types.MustCreateBitType(1), nil
629644
case "unsigned tinyint", "unsigned smallint", "unsigned mediumint", "unsigned int", "unsigned bigint":
630645
return types.Uint64, nil
631646
case "float", "double":

sql/types/bit.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,16 @@ func (t BitType_) SQL(ctx *sql.Context, dest []byte, v interface{}) (sqltypes.Va
199199
}
200200
bitVal := value.(uint64)
201201

202-
return sqltypes.NewUint64(bitVal), nil
202+
var data []byte
203+
for i := uint64(0); i < uint64(t.numOfBits); i += 8 {
204+
data = append(data, byte(bitVal>>i))
205+
}
206+
for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 {
207+
data[i], data[j] = data[j], data[i]
208+
}
209+
val := data
210+
211+
return sqltypes.MakeTrusted(sqltypes.Bit, val), nil
203212
}
204213

205214
// String implements Type interface.
@@ -209,8 +218,7 @@ func (t BitType_) String() string {
209218

210219
// Type implements Type interface.
211220
func (t BitType_) Type() query.Type {
212-
// Use Uint64 for MySQL driver compatibility
213-
return sqltypes.Uint64
221+
return sqltypes.Bit
214222
}
215223

216224
// ValueType implements Type interface.

sql/types/conversion.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,18 @@ func GeneralizeTypes(a, b sql.Type) sql.Type {
692692
// TODO: match blob length to max of the blob lengths
693693
return LongBlob
694694
}
695-
695+
aIsBit := IsBit(a)
696+
bIsBit := IsBit(b)
697+
if aIsBit && bIsBit {
698+
// TODO: match max bits to max of max bits between a and b
699+
return a.Promote()
700+
}
701+
if aIsBit {
702+
a = Int64
703+
}
704+
if bIsBit {
705+
b = Int64
706+
}
696707
aIsYear := IsYear(a)
697708
bIsYear := IsYear(b)
698709
if aIsYear && bIsYear {

0 commit comments

Comments
 (0)