Skip to content

Commit

Permalink
Ensure that float values parsed into expressions will always be parse…
Browse files Browse the repository at this point in the history
…d back into floats, not decimals.
  • Loading branch information
nicktobey committed Mar 20, 2024
1 parent 89f7db8 commit 1cfa73b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
20 changes: 15 additions & 5 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"fmt"
"io"
"sort"
"strconv"
"strings"
"sync"
"unicode"
Expand Down Expand Up @@ -4325,10 +4326,10 @@ type Into struct {
Variables Variables
Dumpfile string

Outfile string
Charset string
Fields *Fields
Lines *Lines
Outfile string
Charset string
Fields *Fields
Lines *Lines
}

func (i *Into) Format(buf *TrackedBuffer) {
Expand Down Expand Up @@ -5166,7 +5167,16 @@ func ExprFromValue(value sqltypes.Value) (Expr, error) {
return &NullVal{}, nil
case value.IsIntegral():
return NewIntVal(value.ToBytes()), nil
case value.IsFloat() || value.Type() == sqltypes.Decimal:
case value.IsFloat():
// Ensure that the resulting expression will be parsed back as a float, not a decimal.
// We do this by parsing the float, then reserializing it with exponential notation.
floatValue, err := strconv.ParseFloat(string(value.ToBytes()), 64)
if err != nil {
return nil, err
}
newValue := sqltypes.MakeTrusted(sqltypes.Float64, strconv.AppendFloat(nil, floatValue, 'e', -1, 64))
return NewFloatVal(newValue.ToBytes()), nil
case value.Type() == sqltypes.Decimal:
return NewFloatVal(value.ToBytes()), nil
case value.IsQuoted():
return NewStrVal(value.ToBytes()), nil
Expand Down
6 changes: 3 additions & 3 deletions go/vt/sqlparser/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ func TestExprFromValue(t *testing.T) {
out: NewIntVal([]byte("1")),
}, {
in: sqltypes.NewFloat64(1.1),
out: NewFloatVal([]byte("1.1")),
out: NewFloatVal([]byte("1.1e+00")),
}, {
in: sqltypes.MakeTrusted(sqltypes.Decimal, []byte("1.1")),
out: NewFloatVal([]byte("1.1")),
Expand Down Expand Up @@ -887,8 +887,8 @@ func TestSplitStatementToPieces(t *testing.T) {

func TestVarScopeForColName(t *testing.T) {
testcases := []struct {
colName ColName
expectedName ColName
colName ColName
expectedName ColName
expectedScope string
expectedSpecifiedScope string
}{
Expand Down

0 comments on commit 1cfa73b

Please sign in to comment.