Skip to content

Commit 3d3c227

Browse files
committed
normalize on string in-mem representation
1 parent 91e9a54 commit 3d3c227

4 files changed

Lines changed: 21 additions & 25 deletions

File tree

server/ast/expr.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,11 @@ func nodeExpr(ctx *Context, node tree.Expr) (vitess.Expr, error) {
476476
case *tree.DArray:
477477
return nil, errors.Errorf("the statement is not yet supported")
478478
case *tree.DBitArray:
479+
// We convert bitarray to string representation for engine representation purposes so that we don't have to
480+
// represent another fundamental type golang type. This means our representation in memory is more verbose.
481+
bitStr := tree.AsStringWithFlags(node, tree.FmtPgwireText)
479482
return vitess.InjectedExpr{
480-
Expression: pgexprs.NewUnsafeLiteral(node, pgtypes.Bit),
483+
Expression: pgexprs.NewUnsafeLiteral(bitStr, pgtypes.Bit),
481484
}, nil
482485
case *tree.DBool:
483486
return vitess.InjectedExpr{

server/functions/bit.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var bitin = framework.Function3{
4545
input := val1.(string)
4646
typmod := val3.(int32)
4747

48+
// validation and normalization
4849
array, err := tree.ParseDBitArray(input)
4950
if err != nil {
5051
return nil, err
@@ -55,7 +56,7 @@ var bitin = framework.Function3{
5556
return nil, pgtypes.ErrWrongLengthBit.New(len(input), expectedLength)
5657
}
5758

58-
return array, nil
59+
return tree.AsStringWithFlags(array, tree.FmtPgwireText), nil
5960
},
6061
}
6162

@@ -66,8 +67,8 @@ var bitout = framework.Function1{
6667
Parameters: [1]*pgtypes.DoltgresType{pgtypes.Bit},
6768
Strict: true,
6869
Callable: func(ctx *sql.Context, t [2]*pgtypes.DoltgresType, val any) (any, error) {
69-
bitStr := val.(*tree.DBitArray)
70-
return tree.AsStringWithFlags(bitStr, tree.FmtPgwireText), nil
70+
bitStr := val.(string)
71+
return bitStr, nil
7172
},
7273
}
7374

@@ -83,7 +84,7 @@ var bitrecv = framework.Function3{
8384
return nil, nil
8485
}
8586
reader := utils.NewReader(data)
86-
return tree.ParseDBitArray(reader.String())
87+
return reader.String(), nil
8788
},
8889
}
8990

@@ -94,8 +95,7 @@ var bitsend = framework.Function1{
9495
Parameters: [1]*pgtypes.DoltgresType{pgtypes.Bit},
9596
Strict: true,
9697
Callable: func(ctx *sql.Context, _ [2]*pgtypes.DoltgresType, val any) (any, error) {
97-
bitStr := val.(*tree.DBitArray)
98-
str := tree.AsStringWithFlags(bitStr, tree.FmtPgwireText)
98+
str := val.(string)
9999
wr := utils.NewWriter(uint64(4 + len(str)))
100100
wr.String(str)
101101
return wr.Data(), nil

server/functions/varbit.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var varbitin = framework.Function3{
4545
input := val1.(string)
4646
typmod := val3.(int32)
4747

48+
// validation and normalization
4849
bitStr, err := tree.ParseDBitArray(input)
4950
if err != nil {
5051
return nil, err
@@ -58,7 +59,7 @@ var varbitin = framework.Function3{
5859
}
5960
}
6061

61-
return bitStr, nil
62+
return tree.AsStringWithFlags(bitStr, tree.FmtPgwireText), nil
6263
},
6364
}
6465

@@ -69,20 +70,14 @@ var varbitout = framework.Function1{
6970
Parameters: [1]*pgtypes.DoltgresType{pgtypes.VarBit},
7071
Strict: true,
7172
Callable: func(ctx *sql.Context, t [2]*pgtypes.DoltgresType, val any) (any, error) {
72-
var bitArray *tree.DBitArray
7373
bitStr, ok, err := sql.Unwrap[string](ctx, val)
7474
if err != nil {
7575
return nil, err
7676
}
77-
if ok {
78-
bitArray, err = tree.ParseDBitArray(bitStr)
79-
if err != nil {
80-
return nil, err
81-
}
82-
} else {
83-
bitArray = val.(*tree.DBitArray)
77+
if !ok {
78+
return nil, fmt.Errorf("varbit_out function returned false")
8479
}
85-
return tree.AsStringWithFlags(bitArray, tree.FmtPgwireText), nil
80+
return bitStr, nil
8681
},
8782
}
8883

@@ -98,7 +93,7 @@ var varbitrecv = framework.Function3{
9893
return nil, nil
9994
}
10095
reader := utils.NewReader(data)
101-
return tree.ParseDBitArray(reader.String())
96+
return reader.String(), nil
10297
},
10398
}
10499

@@ -109,9 +104,9 @@ var varbitsend = framework.Function1{
109104
Parameters: [1]*pgtypes.DoltgresType{pgtypes.VarBit},
110105
Strict: true,
111106
Callable: func(ctx *sql.Context, _ [2]*pgtypes.DoltgresType, val any) (any, error) {
112-
bitStr := val.(*tree.DBitArray)
113-
writer := utils.NewWriter(uint64(bitStr.BitLen() + 4))
114-
writer.String(tree.AsStringWithFlags(bitStr, tree.FmtPgwireText))
107+
bitStr := val.(string)
108+
writer := utils.NewWriter(uint64(len(bitStr) + 4))
109+
writer.String(bitStr)
115110
return writer.Data(), nil
116111
},
117112
}

testing/go/types_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,7 @@ var typesTests = []ScriptTest{
302302
},
303303
},
304304
{
305-
Name: "Bit varying type",
306-
Focus: true,
305+
Name: "Bit varying type",
307306
SetUpScript: []string{
308307
"CREATE TABLE t_bit_varying (id INTEGER primary key, v1 BIT VARYING(16));",
309308
"INSERT INTO t_bit_varying VALUES (1, B'1101101010101010'), (2, B'0010101101010101');",
@@ -323,8 +322,7 @@ var typesTests = []ScriptTest{
323322
},
324323
},
325324
{
326-
Name: "Bit varying type, unbounded",
327-
Focus: true,
325+
Name: "Bit varying type, unbounded",
328326
SetUpScript: []string{
329327
"CREATE TABLE t_bit_varying (id INTEGER primary key, v1 BIT VARYING);",
330328
"INSERT INTO t_bit_varying VALUES (1, B'1101101010101010'), (2, B'0010101101010101');",

0 commit comments

Comments
 (0)