-
Notifications
You must be signed in to change notification settings - Fork 38
/
range_conditions_test.go
361 lines (345 loc) · 10.5 KB
/
range_conditions_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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package dosa_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/uber-go/dosa"
)
// pk = ((a, b), c, d, e)
var testEntityRange = &dosa.EntityDefinition{
Name: "testentityrange",
Key: &dosa.PrimaryKey{
PartitionKeys: []string{"a", "b"},
ClusteringKeys: []*dosa.ClusteringKey{
{
Name: "c",
Descending: true,
},
{
Name: "d",
Descending: true,
},
{
Name: "e",
Descending: true,
},
},
},
Columns: []*dosa.ColumnDefinition{
{
Name: "a",
Type: dosa.TUUID,
},
{
Name: "c",
Type: dosa.Int32,
},
{
Name: "e",
Type: dosa.String,
},
{
Name: "d",
Type: dosa.Timestamp,
},
{
Name: "b",
Type: dosa.Int64,
},
{
Name: "f",
Type: dosa.Blob,
},
},
}
var columnToFieldMap = map[string]string{
"a": "FieldA",
"b": "FieldB",
"c": "FieldC",
"d": "FieldD",
"e": "FieldE",
"f": "FieldF",
}
var simpleTransformer = func(x string) string {
return columnToFieldMap[x]
}
func TestEnsureValidRangeConditions(t *testing.T) {
assert.NoError(t, testEntityRange.EnsureValid()) // sanity check
type validCase struct {
conds map[string][]*dosa.Condition
desc string
}
validCases := []validCase{
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}},
},
desc: "supply only partition keys is allowed, no conditions on clustering keys",
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}},
"c": {{dosa.Eq, int32(99)}},
},
desc: "eq condition on first clustering key, no condition on second and third",
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}},
"c": {{dosa.GtOrEq, int32(99)}, {dosa.Lt, int32(200)}},
},
desc: "close range condition on first clustering key, no condition on second and third",
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}},
"c": {{dosa.Eq, int32(99)}},
"d": {{dosa.LtOrEq, time.Now()}},
},
desc: "open range condition on second clustering key, no restaint on third",
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}},
"c": {{dosa.Eq, int32(99)}},
"d": {{dosa.Eq, time.Unix(100, 0)}},
},
desc: "eq condition on second clustering key, no restaint on third",
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}},
"c": {{dosa.Eq, int32(99)}},
"d": {{dosa.Eq, time.Unix(100, 0)}},
"e": {{dosa.Gt, "aaa"}, {dosa.Lt, "zzz"}},
},
desc: "close range condition on third/last clustering key with < and >",
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}},
"c": {{dosa.Eq, int32(99)}},
"d": {{dosa.Eq, time.Unix(100, 0)}},
"e": {{dosa.GtOrEq, "aaa"}, {dosa.LtOrEq, "zzz"}},
},
desc: "close range condition on third/last clustering key with <= and >=",
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}},
"c": {{dosa.Eq, int32(99)}},
"d": {{dosa.Eq, time.Unix(100, 0)}},
"e": {{dosa.GtOrEq, "aaa"}, {dosa.Lt, "zzz"}},
},
desc: "close range condition on third/last clustering key with < and >=",
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}},
"c": {{dosa.Eq, int32(99)}},
"d": {{dosa.Eq, time.Unix(100, 0)}},
"e": {{dosa.Gt, "aaa"}, {dosa.LtOrEq, "zzz"}},
},
desc: "close range condition on third/last clustering key with <= and >",
},
}
type invalidCase struct {
conds map[string][]*dosa.Condition
desc string
errMsg string
errField string
}
invalidCases := []invalidCase{
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}},
"f": {{dosa.Eq, []byte{1, 2, 3}}},
},
errMsg: "is not in the primary key",
desc: "conditions on non-key column",
errField: columnToFieldMap["f"],
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"c": {{dosa.Gt, int32(100)}},
},
errMsg: "missing Eq condition on partition keys",
desc: "missing partition key condition",
errField: columnToFieldMap["b"],
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Gt, int64(100)}},
},
errMsg: "invalid conditions for partition key",
desc: "Gt condition on partition key",
errField: columnToFieldMap["b"],
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}, {dosa.Eq, int64(200)}},
},
errMsg: "invalid conditions for partition key",
desc: "more than one conditions on partition key",
errField: columnToFieldMap["b"],
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, "100"}},
},
errMsg: "does not have expected type",
desc: "wrong value type for partition key",
errField: columnToFieldMap["b"],
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}},
"c": {{dosa.Eq, "100"}},
},
errMsg: "invalid value for",
desc: "wrong value type for clustering key",
errField: columnToFieldMap["c"],
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}},
"c": {{dosa.Lt, int32(100)}, {dosa.Gt, int32(200)}},
},
errMsg: "invalid or unsupported conditions for clustering key",
desc: "invalid range condition on clustering key",
errField: columnToFieldMap["c"],
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}},
"c": {{dosa.Lt, int32(100)}, {dosa.Gt, int32(200)}},
"d": {{dosa.GtOrEq, time.Now()}},
},
errMsg: "exact one Eq condition can be applied except for the last",
desc: "applying conditions other than eq to clustering keys that's not the last retrained",
errField: columnToFieldMap["c"],
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}},
"c": {{dosa.Lt, int32(100)}, {dosa.Gt, int32(200)}},
"d": {{dosa.Eq, time.Unix(100, 0)}},
"e": {{dosa.Eq, "aaa"}},
},
errMsg: "exact one Eq condition can be applied except for the last",
desc: "applying conditions other than eq to clustering keys that's not the last retrained, " +
"different last retained clusterin key",
errField: columnToFieldMap["c"],
},
{
conds: map[string][]*dosa.Condition{
"a": {{dosa.Eq, dosa.UUID("66DF78EB-C41D-48EF-B366-0C7F91C5CE43")}},
"b": {{dosa.Eq, int64(100)}},
"c": {{dosa.Eq, int32(100)}},
"e": {{dosa.Eq, "aaa"}},
},
errMsg: "conditions must be applied consecutively on clustering keys",
desc: "applying conditions non-consecutively to clustering keys",
errField: columnToFieldMap["e"],
},
}
for _, c := range validCases {
assert.NoError(t, dosa.EnsureValidRangeConditions(testEntityRange, testEntityRange.Key, c.conds, simpleTransformer), c.desc)
}
for _, c := range invalidCases {
err := dosa.EnsureValidRangeConditions(testEntityRange, testEntityRange.Key, c.conds, simpleTransformer)
if assert.Error(t, err, c.desc) {
assert.Contains(t, err.Error(), c.errMsg, c.desc)
assert.Contains(t, err.Error(), c.errField, c.desc)
}
}
}
func TestSortedConditions(t *testing.T) {
conds := map[string][]*dosa.Condition{
"b": {{Op: dosa.Eq, Value: 9}},
"c": {{Op: dosa.Gt, Value: 0}, {Op: dosa.Lt, Value: 1}},
"a": {
{Op: dosa.Gt, Value: 0},
{Op: dosa.LtOrEq, Value: 2},
{Op: dosa.GtOrEq, Value: 3},
{Op: dosa.Lt, Value: 5},
{Op: dosa.Eq, Value: 4},
},
}
expected := []*dosa.ColumnCondition{
{
Name: "a",
Condition: &dosa.Condition{Op: dosa.Eq, Value: 4},
},
{
Name: "a",
Condition: &dosa.Condition{Op: dosa.Lt, Value: 5},
},
{
Name: "a",
Condition: &dosa.Condition{Op: dosa.LtOrEq, Value: 2},
},
{
Name: "a",
Condition: &dosa.Condition{Op: dosa.Gt, Value: 0},
},
{
Name: "a",
Condition: &dosa.Condition{Op: dosa.GtOrEq, Value: 3},
},
{
Name: "b",
Condition: &dosa.Condition{Op: dosa.Eq, Value: 9},
},
{
Name: "c",
Condition: &dosa.Condition{Op: dosa.Lt, Value: 1},
},
{
Name: "c",
Condition: &dosa.Condition{Op: dosa.Gt, Value: 0},
},
}
cc := dosa.NormalizeConditions(conds)
for i, c := range cc {
assert.Equal(t, c.Name, expected[i].Name)
assert.Equal(t, c.Condition, expected[i].Condition)
}
}