Skip to content

Commit 3072091

Browse files
authored
match notation for decimal parsing (#1875)
1 parent 30a5b2b commit 3072091

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

enginetest/queries/script_queries.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,6 +2804,18 @@ var ScriptTests = []ScriptTest{
28042804
},
28052805
},
28062806
},
2807+
{
2808+
Name: "scientific notation for floats",
2809+
SetUpScript: []string{
2810+
"create table t (b bigint unsigned);",
2811+
},
2812+
Assertions: []ScriptTestAssertion{
2813+
{
2814+
Query: "insert into t values (5.2443381514267e+18);",
2815+
Expected: []sql.Row{{types.NewOkResult(1)}},
2816+
},
2817+
},
2818+
},
28072819
{
28082820
Name: "INSERT IGNORE throws an error when json is badly formatted",
28092821
SetUpScript: []string{

sql/parse/parse.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4346,7 +4346,11 @@ func convertVal(ctx *sql.Context, v *sqlparser.SQLVal) (sql.Expression, error) {
43464346
// use the value as string format to keep precision and scale as defined for DECIMAL data type to avoid rounded up float64 value
43474347
if ps := strings.Split(string(v.Val), "."); len(ps) == 2 {
43484348
ogVal := string(v.Val)
4349-
floatVal := strconv.FormatFloat(val, 'f', -1, 64)
4349+
var fmtStr byte = 'f'
4350+
if strings.Contains(ogVal, "e") {
4351+
fmtStr = 'e'
4352+
}
4353+
floatVal := strconv.FormatFloat(val, fmtStr, -1, 64)
43504354
if len(ogVal) >= len(floatVal) && ogVal != floatVal {
43514355
p, s := expression.GetDecimalPrecisionAndScale(ogVal)
43524356
dt, err := types.CreateDecimalType(p, s)

0 commit comments

Comments
 (0)