Skip to content

Commit 04572c6

Browse files
author
James Cor
committed
fix some functions
1 parent d8f6566 commit 04572c6

File tree

5 files changed

+60
-48
lines changed

5 files changed

+60
-48
lines changed

enginetest/queries/queries.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,13 +1569,13 @@ SELECT * FROM cte WHERE d = 2;`,
15691569
{
15701570
Query: `SELECT * FROM (values row(1+1,2+2), row(floor(1.5),concat("a","b"))) a order by 1`,
15711571
Expected: []sql.Row{
1572-
{1, "ab"},
1573-
{2, "4"},
1572+
{1.0, "ab"},
1573+
{2.0, "4"},
15741574
},
15751575
ExpectedColumns: sql.Schema{
15761576
{
15771577
Name: "column_0",
1578-
Type: types.Int64,
1578+
Type: types.Float64,
15791579
},
15801580
{
15811581
Name: "column_1",
@@ -1586,13 +1586,13 @@ SELECT * FROM cte WHERE d = 2;`,
15861586
{
15871587
Query: `SELECT * FROM (values row(1+1,2+2), row(floor(1.5),concat("a","b"))) a (c,d) order by 1`,
15881588
Expected: []sql.Row{
1589-
{1, "ab"},
1590-
{2, "4"},
1589+
{1.0, "ab"},
1590+
{2.0, "4"},
15911591
},
15921592
ExpectedColumns: sql.Schema{
15931593
{
15941594
Name: "c",
1595-
Type: types.Int64,
1595+
Type: types.Float64,
15961596
},
15971597
{
15981598
Name: "d",
@@ -1603,8 +1603,8 @@ SELECT * FROM cte WHERE d = 2;`,
16031603
{
16041604
Query: `SELECT column_0 FROM (values row(1+1,2+2), row(floor(1.5),concat("a","b"))) a order by 1`,
16051605
Expected: []sql.Row{
1606-
{1},
1607-
{2},
1606+
{1.0},
1607+
{2.0},
16081608
},
16091609
},
16101610
{
@@ -1882,8 +1882,8 @@ SELECT * FROM cte WHERE d = 2;`,
18821882
join (values row(2,4), row(1.0,"ab")) b on a.column_0 = b.column_0 and a.column_0 = b.column_0
18831883
order by 1`,
18841884
Expected: []sql.Row{
1885-
{1, "ab"},
1886-
{2, "4"},
1885+
{1.0, "ab"},
1886+
{2.0, "4"},
18871887
},
18881888
},
18891889
{

enginetest/queries/script_queries.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,9 @@ var ScriptTests = []ScriptTest{
378378
},
379379
{
380380
// Valid float strings used as arguments to functions are truncated not rounded
381-
//Skip: true,
382381
Query: "SELECT LENGTH(SPACE('1.9'));",
383382
Expected: []sql.Row{{1}},
384-
ExpectedWarningsCount: 2, // MySQL throws two warnings for some reason
383+
ExpectedWarningsCount: 1, // TODO: MySQL throws two warnings for some reason
385384
ExpectedWarning: mysql.ERTruncatedWrongValue,
386385
},
387386
{

sql/expression/function/ceil_round_floor.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/dolthub/go-mysql-server/sql"
2424
"github.com/dolthub/go-mysql-server/sql/expression"
2525
"github.com/dolthub/go-mysql-server/sql/types"
26+
"github.com/dolthub/vitess/go/mysql"
2627
)
2728

2829
// Ceil returns the smallest integer value not less than X.
@@ -51,10 +52,13 @@ func (c *Ceil) Description() string {
5152
// Type implements the Expression interface.
5253
func (c *Ceil) Type() sql.Type {
5354
childType := c.Child.Type()
54-
if types.IsInteger(childType) {
55-
return childType
55+
if types.IsSigned(childType) {
56+
return types.Int64
5657
}
57-
return types.Int32
58+
if types.IsUnsigned(childType) {
59+
return types.Uint64
60+
}
61+
return types.Float64
5862
}
5963

6064
// CollationCoercibility implements the interface sql.CollationCoercible.
@@ -77,35 +81,32 @@ func (c *Ceil) WithChildren(children ...sql.Expression) (sql.Expression, error)
7781
// Eval implements the Expression interface.
7882
func (c *Ceil) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
7983
child, err := c.Child.Eval(ctx, row)
80-
8184
if err != nil {
8285
return nil, err
8386
}
84-
8587
if child == nil {
8688
return nil, nil
8789
}
88-
89-
// non number type will be caught here
9090
if !types.IsNumber(c.Child.Type()) {
9191
child, _, err = types.Float64.Convert(ctx, child)
9292
if err != nil {
93-
return int32(0), nil
93+
if !sql.ErrTruncatedIncorrect.Is(err) {
94+
return int32(0), nil
95+
}
96+
ctx.Warn(mysql.ERTruncatedWrongValue, "%s", err.Error())
9497
}
95-
96-
return int32(math.Ceil(child.(float64))), nil
9798
}
98-
9999
// if it's number type and not float value, it does not need ceil-ing
100100
switch num := child.(type) {
101+
case float32:
102+
return math.Ceil(float64(num)), nil
101103
case float64:
102104
return math.Ceil(num), nil
103-
case float32:
104-
return float32(math.Ceil(float64(num))), nil
105105
case decimal.Decimal:
106106
return num.Ceil(), nil
107107
default:
108-
return child, nil
108+
num, _, _ = c.Type().Convert(ctx, child)
109+
return num, nil
109110
}
110111
}
111112

@@ -135,10 +136,13 @@ func (f *Floor) Description() string {
135136
// Type implements the Expression interface.
136137
func (f *Floor) Type() sql.Type {
137138
childType := f.Child.Type()
138-
if types.IsInteger(childType) {
139-
return childType
139+
if types.IsSigned(childType) {
140+
return types.Int64
140141
}
141-
return types.Int32
142+
if types.IsUnsigned(childType) {
143+
return types.Uint64
144+
}
145+
return types.Float64
142146
}
143147

144148
// CollationCoercibility implements the interface sql.CollationCoercible.
@@ -161,35 +165,32 @@ func (f *Floor) WithChildren(children ...sql.Expression) (sql.Expression, error)
161165
// Eval implements the Expression interface.
162166
func (f *Floor) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
163167
child, err := f.Child.Eval(ctx, row)
164-
165168
if err != nil {
166169
return nil, err
167170
}
168-
169171
if child == nil {
170172
return nil, nil
171173
}
172-
173-
// non number type will be caught here
174174
if !types.IsNumber(f.Child.Type()) {
175175
child, _, err = types.Float64.Convert(ctx, child)
176176
if err != nil {
177-
return int32(0), nil
177+
if !sql.ErrTruncatedIncorrect.Is(err) {
178+
return int32(0), nil
179+
}
180+
ctx.Warn(mysql.ERTruncatedWrongValue, "%s", err.Error())
178181
}
179-
180-
return int32(math.Floor(child.(float64))), nil
181182
}
182-
183183
// if it's number type and not float value, it does not need floor-ing
184184
switch num := child.(type) {
185+
case float32:
186+
return math.Floor(float64(num)), nil
185187
case float64:
186188
return math.Floor(num), nil
187-
case float32:
188-
return float32(math.Floor(float64(num))), nil
189189
case decimal.Decimal:
190190
return num.Floor(), nil
191191
default:
192-
return child, nil
192+
num, _, _ = f.Type().Convert(ctx, child)
193+
return num, nil
193194
}
194195
}
195196

sql/expression/function/ceil_round_floor_test.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ func TestCeil(t *testing.T) {
3636
{"float64 is nil", types.Float64, sql.NewRow(nil), nil, nil},
3737
{"float64 is ok", types.Float64, sql.NewRow(5.8), float64(6), nil},
3838
{"float32 is nil", types.Float32, sql.NewRow(nil), nil, nil},
39-
{"float32 is ok", types.Float32, sql.NewRow(float32(5.8)), float32(6), nil},
39+
{"float32 is ok", types.Float32, sql.NewRow(float32(5.8)), float64(6), nil},
4040
{"int32 is nil", types.Int32, sql.NewRow(nil), nil, nil},
41-
{"int32 is ok", types.Int32, sql.NewRow(int32(6)), int32(6), nil},
41+
{"int32 is ok", types.Int32, sql.NewRow(int32(6)), int64(6), nil},
4242
{"int64 is nil", types.Int64, sql.NewRow(nil), nil, nil},
4343
{"int64 is ok", types.Int64, sql.NewRow(int64(6)), int64(6), nil},
4444
{"blob is nil", types.Blob, sql.NewRow(nil), nil, nil},
45-
{"blob is ok", types.Blob, sql.NewRow([]byte{1, 2, 3}), int32(66051), nil},
46-
{"string int is ok", types.Text, sql.NewRow("1"), int32(1), nil},
47-
{"string float is ok", types.Text, sql.NewRow("1.2"), int32(2), nil},
45+
{"blob is ok", types.Blob, sql.NewRow([]byte{1, 2, 3}), float64(66051), nil},
46+
{"string int is ok", types.Text, sql.NewRow("1"), float64(1), nil},
47+
{"string float is ok", types.Text, sql.NewRow("1.2"), float64(2), nil},
48+
{"invalid strings are truncated but still ok", types.Text, sql.NewRow("1.2abc"), float64(2), nil},
4849
}
4950

5051
for _, tt := range testCases {
@@ -65,7 +66,15 @@ func TestCeil(t *testing.T) {
6566
require.Equal(tt.expected, result)
6667
}
6768

68-
require.True(types.IsInteger(f.Type()))
69+
// signed -> signed, unsigned -> unsigned, everything else -> double
70+
resType := f.Type()
71+
if types.IsSigned(tt.rowType) {
72+
require.True(types.IsSigned(resType))
73+
} else if types.IsUnsigned(resType) {
74+
require.True(types.IsUnsigned(resType))
75+
} else {
76+
require.True(types.IsFloat(resType))
77+
}
6978
require.False(f.IsNullable())
7079
})
7180
}

sql/expression/function/space.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package function
1717
import (
1818
"github.com/dolthub/go-mysql-server/sql"
1919
"github.com/dolthub/go-mysql-server/sql/types"
20+
"github.com/dolthub/vitess/go/mysql"
2021
)
2122

2223
// Space implements the sql function "space" which returns a string with the number of spaces specified by the argument
@@ -55,8 +56,10 @@ func (s *Space) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
5556
// TODO: better truncate integer handling
5657
v, _, err := types.Int64.Convert(ctx, val)
5758
if err != nil {
58-
ctx.Warn(1292, "Truncated incorrect INTEGER value: '%v'", val)
59-
v = int64(0)
59+
if !sql.ErrTruncatedIncorrect.Is(err) {
60+
return nil, err
61+
}
62+
ctx.Warn(mysql.ERTruncatedWrongValue, "%s", err.Error())
6063
}
6164

6265
num := int(v.(int64))

0 commit comments

Comments
 (0)