Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions go/sqltypes/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ func NewIntegral(val string) (n Value, err error) {
return MakeTrusted(Uint64, strconv.AppendUint(nil, unsigned, 10)), nil
}

// NewNumber builds a numeric type from a string representation.
// The type will be Int64, Uint64, or Float64. Integer types will be preferred.
func NewNumber(val string) (n Value, err error) {
signed, err := strconv.ParseInt(val, 0, 64)
if err == nil {
return MakeTrusted(Int64, strconv.AppendInt(nil, signed, 10)), nil
}
unsigned, err := strconv.ParseUint(val, 0, 64)
if err == nil {
return MakeTrusted(Uint64, strconv.AppendUint(nil, unsigned, 10)), nil
}
decimal, err := strconv.ParseFloat(val, 64)
if err == nil {
return MakeTrusted(Float64, strconv.AppendFloat(nil, decimal, 'g', -1, 64)), nil
}
return Value{}, err
}

// InterfaceToValue builds a value from a go type.
// Supported types are nil, int64, uint64, float64,
// string and []byte.
Expand Down
2 changes: 1 addition & 1 deletion go/vt/sqlparser/normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (nz *normalizer) sqlToBindvar(node SQLNode) *querypb.BindVariable {
case StrVal:
v, err = sqltypes.NewValue(sqltypes.VarBinary, node.Val)
case IntVal:
v, err = sqltypes.NewValue(sqltypes.Int64, node.Val)
v, err = sqltypes.NewNumber(string(node.Val))
case FloatVal:
v, err = sqltypes.NewValue(sqltypes.Float64, node.Val)
default:
Expand Down
10 changes: 6 additions & 4 deletions go/vt/sqlparser/normalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ func TestNormalize(t *testing.T) {
}, {
// bad int
in: "select * from t where v1 = 12345678901234567890",
outstmt: "select * from t where v1 = 12345678901234567890",
outbv: map[string]*querypb.BindVariable{},
outstmt: "select * from t where v1 = :bv1",
outbv: map[string]*querypb.BindVariable{
"bv1": sqltypes.Uint64BindVariable(12345678901234567890),
},
}, {
// comparison with no vals
in: "select * from t where v1 = v2",
Expand Down Expand Up @@ -179,10 +181,10 @@ func TestNormalize(t *testing.T) {
Normalize(stmt, bv, prefix)
outstmt := String(stmt)
if outstmt != tc.outstmt {
t.Errorf("Query:\n%s:\n%s, want\n%s", tc.in, outstmt, tc.outstmt)
t.Errorf("\nQuery: %s\nGot: %s\nWant:%s", tc.in, outstmt, tc.outstmt)
}
if !reflect.DeepEqual(tc.outbv, bv) {
t.Errorf("Query:\n%s:\n%v, want\n%v", tc.in, bv, tc.outbv)
t.Errorf("\nQuery: %s\nGot: %v\nWant:%v", tc.in, bv, tc.outbv)
}
}
}
Expand Down