Skip to content

Commit df4c4e0

Browse files
committed
handle decimals, add tests
1 parent 7551888 commit df4c4e0

File tree

3 files changed

+142
-1
lines changed

3 files changed

+142
-1
lines changed

enginetest/queries/function_queries.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,3 +1529,16 @@ var FunctionQueryTests = []QueryTest{
15291529
},
15301530
},
15311531
}
1532+
1533+
// BrokenFunctionQueryTests contains SQL function call queries that don't match MySQL behavior
1534+
var BrokenFunctionQueryTests = []QueryTest{
1535+
// https://github.com/dolthub/dolt/issues/9735
1536+
{
1537+
Query: "select log('10asdf', '100f')",
1538+
Expected: []sql.Row{{float64(2)}},
1539+
},
1540+
{
1541+
Query: "select log('a10asdf', 'b100f')",
1542+
Expected: []sql.Row{{nil}},
1543+
},
1544+
}

enginetest/queries/script_queries.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11439,6 +11439,134 @@ select * from t1 except (
1143911439
},
1144011440
},
1144111441
},
11442+
{
11443+
// https://github.com/dolthub/go-mysql-server/pull/3176
11444+
Name: "strings cast to numbers",
11445+
SetUpScript: []string{
11446+
"create table test01(pk varchar(20) primary key)",
11447+
`insert into test01 values (' 3 12 4'),
11448+
(' 3.2 12 4'),('-3.1234'),('-3.1a'),('-5+8'),('+3.1234'),
11449+
('11d'),('11wha?'),('11'),('12'),('1a1'),('a1a1'),('11-5'),
11450+
('3. 12 4'),('5.932887e+07'),('5.932887e+07abc'),('5.932887e7'),('5.932887e7abc')`,
11451+
},
11452+
Assertions: []ScriptTestAssertion{
11453+
{
11454+
Query: "select pk, cast(pk as float) from test01",
11455+
Expected: []sql.Row{
11456+
{" 3 12 4", float32(3)},
11457+
{" 3.2 12 4", float32(3.2)},
11458+
{"-3.1234", float32(-3.1234)},
11459+
{"-3.1a", float32(-3.1)},
11460+
{"-5+8", float32(-5)},
11461+
{"+3.1234", float32(3.1234)},
11462+
{"11", float32(11)},
11463+
{"11-5", float32(11)},
11464+
{"11d", float32(11)},
11465+
{"11wha?", float32(11)},
11466+
{"12", float32(12)},
11467+
{"1a1", float32(1)},
11468+
{"3. 12 4", float32(3)},
11469+
{"5.932887e+07", float32(5.932887e+07)},
11470+
{"5.932887e+07abc", float32(5.932887e+07)},
11471+
{"5.932887e7", float32(5.932887e+07)},
11472+
{"5.932887e7abc", float32(5.932887e+07)},
11473+
{"a1a1", float32(0)},
11474+
},
11475+
},
11476+
{
11477+
Query: "select pk, cast(pk as double) from test01",
11478+
Expected: []sql.Row{
11479+
{" 3 12 4", 3.0},
11480+
{" 3.2 12 4", 3.2},
11481+
{"-3.1234", -3.1234},
11482+
{"-3.1a", -3.1},
11483+
{"-5+8", -5.0},
11484+
{"+3.1234", 3.1234},
11485+
{"11", 11.0},
11486+
{"11-5", 11.0},
11487+
{"11d", 11.0},
11488+
{"11wha?", 11.0},
11489+
{"12", 12.0},
11490+
{"1a1", 1.0},
11491+
{"3. 12 4", 3.0},
11492+
{"5.932887e+07", 5.932887e+07},
11493+
{"5.932887e+07abc", 5.932887e+07},
11494+
{"5.932887e7", 5.932887e+07},
11495+
{"5.932887e7abc", 5.932887e+07},
11496+
{"a1a1", 0.0},
11497+
},
11498+
},
11499+
{
11500+
Query: "select pk, cast(pk as signed) from test01",
11501+
Expected: []sql.Row{
11502+
{" 3 12 4", 3},
11503+
{" 3.2 12 4", 3},
11504+
{"-3.1234", -3},
11505+
{"-3.1a", -3},
11506+
{"-5+8", -5},
11507+
{"+3.1234", 3},
11508+
{"11", 11},
11509+
{"11-5", 11},
11510+
{"11d", 11},
11511+
{"11wha?", 11},
11512+
{"12", 12},
11513+
{"1a1", 1},
11514+
{"3. 12 4", 3},
11515+
{"5.932887e+07", 5},
11516+
{"5.932887e+07abc", 5},
11517+
{"5.932887e7", 5},
11518+
{"5.932887e7abc", 5},
11519+
{"a1a1", 0},
11520+
},
11521+
},
11522+
{
11523+
Query: "select pk, cast(pk as unsigned) from test01",
11524+
Expected: []sql.Row{
11525+
{" 3 12 4", uint64(3)},
11526+
{" 3.2 12 4", uint64(3)},
11527+
{"-3.1234", uint64(18446744073709551613)},
11528+
{"-3.1a", uint64(18446744073709551613)},
11529+
{"-5+8", uint64(18446744073709551611)},
11530+
{"+3.1234", uint64(3)},
11531+
{"11", uint64(11)},
11532+
{"11-5", uint64(11)},
11533+
{"11d", uint64(11)},
11534+
{"11wha?", uint64(11)},
11535+
{"12", uint64(12)},
11536+
{"1a1", uint64(1)},
11537+
{"3. 12 4", uint64(3)},
11538+
{"5.932887e+07", uint64(5)},
11539+
{"5.932887e+07abc", uint64(5)},
11540+
{"5.932887e7", uint64(5)},
11541+
{"5.932887e7abc", uint64(5)},
11542+
{"a1a1", uint64(0)},
11543+
},
11544+
},
11545+
{
11546+
Query: "select pk, cast(pk as decimal(12,3)) from test01",
11547+
Expected: []sql.Row{
11548+
{" 3 12 4", "3.000"},
11549+
{" 3.2 12 4", "3.200"},
11550+
{"-3.1234", "-3.123"},
11551+
{"-3.1a", "-3.100"},
11552+
{"-5+8", "-5.000"},
11553+
{"+3.1234", "3.123"},
11554+
{"11", "11.000"},
11555+
{"11-5", "11.000"},
11556+
{"11d", "11.000"},
11557+
{"11wha?", "11.000"},
11558+
{"12", "12.000"},
11559+
{"1a1", "1.000"},
11560+
{"3. 12 4", "3.000"},
11561+
{"5.932887e+07", "59328870.000"},
11562+
{"5.932887e+07abc", "59328870.000"},
11563+
{"5.932887e7", "59328870.000"},
11564+
{"5.932887e7abc", "59328870.000"},
11565+
{"a1a1", "0.000"},
11566+
},
11567+
},
11568+
},
11569+
},
1144211570
}
1144311571

1144411572
var SpatialScriptTests = []ScriptTest{

sql/expression/convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func convertValue(ctx *sql.Context, val interface{}, castTo string, originType s
343343
}
344344
return d, nil
345345
case ConvertToDecimal:
346-
value, err := convertHexBlobToDecimalForNumericContext(val, originType)
346+
value, err := prepareForNumericContext(val, originType, false)
347347
if err != nil {
348348
return nil, err
349349
}

0 commit comments

Comments
 (0)