This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathceil_round_floor_test.go
268 lines (246 loc) · 13 KB
/
ceil_round_floor_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package function
import (
"testing"
"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/expression"
"github.com/stretchr/testify/require"
"gopkg.in/src-d/go-errors.v1"
)
func TestCeil(t *testing.T) {
testCases := []struct {
name string
rowType sql.Type
row sql.Row
expected interface{}
err *errors.Kind
}{
{"float64 is nil", sql.Float64, sql.NewRow(nil), nil, nil},
{"float64 is ok", sql.Float64, sql.NewRow(5.8), float64(6), nil},
{"float32 is nil", sql.Float32, sql.NewRow(nil), nil, nil},
{"float32 is ok", sql.Float32, sql.NewRow(float32(5.8)), float32(6), nil},
{"int32 is nil", sql.Int32, sql.NewRow(nil), nil, nil},
{"int32 is ok", sql.Int32, sql.NewRow(int32(6)), int32(6), nil},
{"int64 is nil", sql.Int64, sql.NewRow(nil), nil, nil},
{"int64 is ok", sql.Int64, sql.NewRow(int64(6)), int64(6), nil},
{"blob is nil", sql.Blob, sql.NewRow(nil), nil, nil},
{"blob is ok", sql.Blob, sql.NewRow([]byte{1, 2, 3}), int32(0), nil},
{"string int is ok", sql.Text, sql.NewRow("1"), int32(1), nil},
{"string float is ok", sql.Text, sql.NewRow("1.2"), int32(2), nil},
}
for _, tt := range testCases {
f := NewCeil(expression.NewGetField(0, tt.rowType, "", false))
t.Run(tt.name, func(t *testing.T) {
require := require.New(t)
exprs := f.Children()
require.True(len(exprs) > 0 && len(exprs) < 3)
require.NotNil(exprs[0])
result, err := f.Eval(sql.NewEmptyContext(), tt.row)
if tt.err != nil {
require.Error(err)
require.True(tt.err.Is(err))
} else {
require.NoError(err)
require.Equal(tt.expected, result)
}
switch {
case sql.IsDecimal(tt.rowType):
require.True(sql.IsDecimal(f.Type()))
require.False(f.IsNullable())
case sql.IsInteger(tt.rowType):
require.True(sql.IsInteger(f.Type()))
require.False(f.IsNullable())
default:
require.True(sql.IsInteger(f.Type()))
require.False(f.IsNullable())
}
})
}
}
func TestFloor(t *testing.T) {
testCases := []struct {
name string
rowType sql.Type
row sql.Row
expected interface{}
err *errors.Kind
}{
{"float64 is nil", sql.Float64, sql.NewRow(nil), nil, nil},
{"float64 is ok", sql.Float64, sql.NewRow(5.8), float64(5), nil},
{"float32 is nil", sql.Float32, sql.NewRow(nil), nil, nil},
{"float32 is ok", sql.Float32, sql.NewRow(float32(5.8)), float32(5), nil},
{"int32 is nil", sql.Int32, sql.NewRow(nil), nil, nil},
{"int32 is ok", sql.Int32, sql.NewRow(int32(6)), int32(6), nil},
{"int64 is nil", sql.Int64, sql.NewRow(nil), nil, nil},
{"int64 is ok", sql.Int64, sql.NewRow(int64(6)), int64(6), nil},
{"blob is nil", sql.Blob, sql.NewRow(nil), nil, nil},
{"blob is ok", sql.Blob, sql.NewRow([]byte{1, 2, 3}), int32(0), nil},
{"string int is ok", sql.Text, sql.NewRow("1"), int32(1), nil},
{"string float is ok", sql.Text, sql.NewRow("1.2"), int32(1), nil},
}
for _, tt := range testCases {
f := NewFloor(expression.NewGetField(0, tt.rowType, "", false))
t.Run(tt.name, func(t *testing.T) {
require := require.New(t)
exprs := f.Children()
require.True(len(exprs) > 0 && len(exprs) < 3)
require.NotNil(exprs[0])
result, err := f.Eval(sql.NewEmptyContext(), tt.row)
if tt.err != nil {
require.Error(err)
require.True(tt.err.Is(err))
} else {
require.NoError(err)
require.Equal(tt.expected, result)
}
switch {
case sql.IsDecimal(tt.rowType):
require.True(sql.IsDecimal(f.Type()))
require.False(f.IsNullable())
case sql.IsInteger(tt.rowType):
require.True(sql.IsInteger(f.Type()))
require.False(f.IsNullable())
default:
require.True(sql.IsInteger(f.Type()))
require.False(f.IsNullable())
}
})
}
}
func TestRound(t *testing.T) {
testCases := []struct {
name string
xType sql.Type
dType sql.Type
row sql.Row
expected interface{}
err *errors.Kind
}{
{"float64 is nil", sql.Float64, sql.Int32, sql.NewRow(nil, nil), nil, nil},
{"float64 without d", sql.Float64, sql.Int32, sql.NewRow(5.8, nil), float64(6), nil},
{"float64 with d", sql.Float64, sql.Int32, sql.NewRow(5.855, 2), float64(5.86), nil},
{"float64 with negative d", sql.Float64, sql.Int32, sql.NewRow(52.855, -1), float64(50), nil},
{"float64 with float d", sql.Float64, sql.Float64, sql.NewRow(5.855, float64(2.123)), float64(5.86), nil},
{"float64 with float negative d", sql.Float64, sql.Float64, sql.NewRow(52.855, float64(-1)), float64(50), nil},
{"float64 with blob d", sql.Float64, sql.Blob, sql.NewRow(5.855, []byte{1, 2, 3}), float64(6), nil},
{"float32 is nil", sql.Float32, sql.Int32, sql.NewRow(nil, nil), nil, nil},
{"float32 without d", sql.Float32, sql.Int32, sql.NewRow(float32(5.8), nil), float32(6), nil},
{"float32 with d", sql.Float32, sql.Int32, sql.NewRow(float32(5.855), 2), float32(5.86), nil},
{"float32 with negative d", sql.Float32, sql.Int32, sql.NewRow(float32(52.855), -1), float32(50), nil},
{"float32 with float d", sql.Float32, sql.Float64, sql.NewRow(float32(5.855), float32(2.123)), float32(5.86), nil},
{"float32 with float negative d", sql.Float32, sql.Float64, sql.NewRow(float32(52.855), float32(-1)), float32(50), nil},
{"float32 with blob d", sql.Float32, sql.Blob, sql.NewRow(float32(5.855), []byte{1, 2, 3}), float32(6), nil},
{"int64 is nil", sql.Int64, sql.Int32, sql.NewRow(nil, nil), nil, nil},
{"int64 without d", sql.Int64, sql.Int32, sql.NewRow(int64(5), nil), int64(5), nil},
{"int64 with d", sql.Int64, sql.Int32, sql.NewRow(int64(5), 2), int64(5), nil},
{"int64 with negative d", sql.Int64, sql.Int32, sql.NewRow(int64(52), -1), int64(50), nil},
{"int64 with float d", sql.Int64, sql.Float64, sql.NewRow(int64(5), float32(2.123)), int64(5), nil},
{"int64 with float negative d", sql.Int64, sql.Float64, sql.NewRow(int64(52), float32(-1)), int64(50), nil},
{"int32 with blob d", sql.Int32, sql.Blob, sql.NewRow(int32(5), []byte{1, 2, 3}), int32(5), nil},
{"int32 is nil", sql.Int32, sql.Int32, sql.NewRow(nil, nil), nil, nil},
{"int32 without d", sql.Int32, sql.Int32, sql.NewRow(int32(5), nil), int32(5), nil},
{"int32 with d", sql.Int32, sql.Int32, sql.NewRow(int32(5), 2), int32(5), nil},
{"int32 with negative d", sql.Int32, sql.Int32, sql.NewRow(int32(52), -1), int32(50), nil},
{"int32 with float d", sql.Int32, sql.Float64, sql.NewRow(int32(5), float32(2.123)), int32(5), nil},
{"int32 with float negative d", sql.Int32, sql.Float64, sql.NewRow(int32(52), float32(-1)), int32(50), nil},
{"int32 with blob d", sql.Int32, sql.Blob, sql.NewRow(int32(5), []byte{1, 2, 3}), int32(5), nil},
{"int16 is nil", sql.Int16, sql.Int16, sql.NewRow(nil, nil), nil, nil},
{"int16 without d", sql.Int16, sql.Int16, sql.NewRow(int16(5), nil), int16(5), nil},
{"int16 with d", sql.Int16, sql.Int16, sql.NewRow(int16(5), 2), int16(5), nil},
{"int16 with negative d", sql.Int16, sql.Int16, sql.NewRow(int16(52), -1), int16(50), nil},
{"int16 with float d", sql.Int16, sql.Float64, sql.NewRow(int16(5), float32(2.123)), int16(5), nil},
{"int16 with float negative d", sql.Int16, sql.Float64, sql.NewRow(int16(52), float32(-1)), int16(50), nil},
{"int16 with blob d", sql.Int16, sql.Blob, sql.NewRow(int16(5), []byte{1, 2, 3}), int16(5), nil},
{"int8 is nil", sql.Int8, sql.Int8, sql.NewRow(nil, nil), nil, nil},
{"int8 without d", sql.Int8, sql.Int8, sql.NewRow(int8(5), nil), int8(5), nil},
{"int8 with d", sql.Int8, sql.Int8, sql.NewRow(int8(5), 2), int8(5), nil},
{"int8 with negative d", sql.Int8, sql.Int8, sql.NewRow(int8(52), -1), int8(50), nil},
{"int8 with float d", sql.Int8, sql.Float64, sql.NewRow(int8(5), float32(2.123)), int8(5), nil},
{"int8 with float negative d", sql.Int8, sql.Float64, sql.NewRow(int8(52), float32(-1)), int8(50), nil},
{"int8 with blob d", sql.Int8, sql.Blob, sql.NewRow(int8(5), []byte{1, 2, 3}), int8(5), nil},
{"uint64 is nil", sql.Uint64, sql.Int32, sql.NewRow(nil, nil), nil, nil},
{"uint64 without d", sql.Uint64, sql.Int32, sql.NewRow(uint64(5), nil), uint64(5), nil},
{"uint64 with d", sql.Uint64, sql.Int32, sql.NewRow(uint64(5), 2), uint64(5), nil},
{"uint64 with negative d", sql.Uint64, sql.Int32, sql.NewRow(uint64(52), -1), uint64(50), nil},
{"uint64 with float d", sql.Uint64, sql.Float64, sql.NewRow(uint64(5), float32(2.123)), uint64(5), nil},
{"uint64 with float negative d", sql.Uint64, sql.Float64, sql.NewRow(uint64(52), float32(-1)), uint64(50), nil},
{"uint32 with blob d", sql.Uint32, sql.Blob, sql.NewRow(uint32(5), []byte{1, 2, 3}), uint32(5), nil},
{"uint32 is nil", sql.Uint32, sql.Int32, sql.NewRow(nil, nil), nil, nil},
{"uint32 without d", sql.Uint32, sql.Int32, sql.NewRow(uint32(5), nil), uint32(5), nil},
{"uint32 with d", sql.Uint32, sql.Int32, sql.NewRow(uint32(5), 2), uint32(5), nil},
{"uint32 with negative d", sql.Uint32, sql.Int32, sql.NewRow(uint32(52), -1), uint32(50), nil},
{"uint32 with float d", sql.Uint32, sql.Float64, sql.NewRow(uint32(5), float32(2.123)), uint32(5), nil},
{"uint32 with float negative d", sql.Uint32, sql.Float64, sql.NewRow(uint32(52), float32(-1)), uint32(50), nil},
{"uint32 with blob d", sql.Uint32, sql.Blob, sql.NewRow(uint32(5), []byte{1, 2, 3}), uint32(5), nil},
{"uint16 with blob d", sql.Uint16, sql.Blob, sql.NewRow(uint16(5), []byte{1, 2, 3}), uint16(5), nil},
{"uint16 is nil", sql.Uint16, sql.Int16, sql.NewRow(nil, nil), nil, nil},
{"uint16 without d", sql.Uint16, sql.Int16, sql.NewRow(uint16(5), nil), uint16(5), nil},
{"uint16 with d", sql.Uint16, sql.Int16, sql.NewRow(uint16(5), 2), uint16(5), nil},
{"uint16 with negative d", sql.Uint16, sql.Int16, sql.NewRow(uint16(52), -1), uint16(50), nil},
{"uint16 with float d", sql.Uint16, sql.Float64, sql.NewRow(uint16(5), float32(2.123)), uint16(5), nil},
{"uint16 with float negative d", sql.Uint16, sql.Float64, sql.NewRow(uint16(52), float32(-1)), uint16(50), nil},
{"uint16 with blob d", sql.Uint16, sql.Blob, sql.NewRow(uint16(5), []byte{1, 2, 3}), uint16(5), nil},
{"uint8 with blob d", sql.Uint8, sql.Blob, sql.NewRow(uint8(5), []byte{1, 2, 3}), uint8(5), nil},
{"uint8 is nil", sql.Uint8, sql.Int8, sql.NewRow(nil, nil), nil, nil},
{"uint8 without d", sql.Uint8, sql.Int8, sql.NewRow(uint8(5), nil), uint8(5), nil},
{"uint8 with d", sql.Uint8, sql.Int8, sql.NewRow(uint8(5), 2), uint8(5), nil},
{"uint8 with negative d", sql.Uint8, sql.Int8, sql.NewRow(uint8(52), -1), uint8(50), nil},
{"uint8 with float d", sql.Uint8, sql.Float64, sql.NewRow(uint8(5), float32(2.123)), uint8(5), nil},
{"uint8 with float negative d", sql.Uint8, sql.Float64, sql.NewRow(uint8(52), float32(-1)), uint8(50), nil},
{"uint8 with blob d", sql.Uint8, sql.Blob, sql.NewRow(uint8(5), []byte{1, 2, 3}), uint8(5), nil},
{"blob is nil", sql.Blob, sql.Int32, sql.NewRow(nil, nil), nil, nil},
{"blob is ok", sql.Blob, sql.Int32, sql.NewRow([]byte{1, 2, 3}, nil), int32(0), nil},
{"text int without d", sql.Text, sql.Int32, sql.NewRow("5", nil), int32(5), nil},
{"text int with d", sql.Text, sql.Int32, sql.NewRow("5", 2), int32(5), nil},
{"text int with negative d", sql.Text, sql.Int32, sql.NewRow("52", -1), int32(50), nil},
{"text int with float d", sql.Text, sql.Float64, sql.NewRow("5", float32(2.123)), int32(5), nil},
{"text int with float negative d", sql.Text, sql.Float64, sql.NewRow("52", float32(-1)), int32(50), nil},
{"text float without d", sql.Text, sql.Int32, sql.NewRow("5.8", nil), int32(6), nil},
{"text float with d", sql.Text, sql.Int32, sql.NewRow("5.855", 2), int32(5), nil},
{"text float with negative d", sql.Text, sql.Int32, sql.NewRow("52.855", -1), int32(50), nil},
{"text float with float d", sql.Text, sql.Float64, sql.NewRow("5.855", float64(2.123)), int32(5), nil},
{"text float with float negative d", sql.Text, sql.Float64, sql.NewRow("52.855", float64(-1)), int32(50), nil},
{"text float with blob d", sql.Text, sql.Blob, sql.NewRow("5.855", []byte{1, 2, 3}), int32(6), nil},
}
for _, tt := range testCases {
var args = make([]sql.Expression, 2)
args[0] = expression.NewGetField(0, tt.xType, "", false)
args[1] = expression.NewGetField(1, tt.dType, "", false)
f, err := NewRound(args...)
t.Run(tt.name, func(t *testing.T) {
require := require.New(t)
require.Nil(err)
result, err := f.Eval(sql.NewEmptyContext(), tt.row)
if tt.err != nil {
require.Error(err)
require.True(tt.err.Is(err))
} else {
require.NoError(err)
require.Equal(tt.expected, result)
}
switch {
case sql.IsDecimal(tt.xType):
require.True(sql.IsDecimal(f.Type()))
require.False(f.IsNullable())
case sql.IsInteger(tt.xType):
require.True(sql.IsInteger(f.Type()))
require.False(f.IsNullable())
default:
require.True(sql.IsInteger(f.Type()))
require.False(f.IsNullable())
}
})
}
// Test on invalid type return 0
var args = make([]sql.Expression, 2)
args[0] = expression.NewGetField(0, sql.Blob, "", false)
args[1] = expression.NewGetField(1, sql.Int32, "", false)
f, err := NewRound(args...)
req := require.New(t)
req.Nil(err)
exprs := f.Children()
req.True(len(exprs) > 0 && len(exprs) < 3)
req.NotNil(exprs[0])
result, err := f.Eval(sql.NewEmptyContext(), sql.NewRow([]byte{1, 2, 3}, 2))
req.NoError(err)
req.Equal(int32(0), result)
}