Repository navigation
Expand file tree
/
Copy pathschema_compatibility_test.go
More file actions
488 lines (407 loc) · 18.5 KB
/
Copy pathschema_compatibility_test.go
File metadata and controls
488 lines (407 loc) · 18.5 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package iceberg_test
import (
"testing"
"github.com/apache/iceberg-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Ported from Java's TestReadabilityChecks.
func compatPrimitives(t *testing.T) []iceberg.PrimitiveType {
t.Helper()
geomCRS84, err := iceberg.GeometryTypeOf(iceberg.DefaultGeoCRS)
require.NoError(t, err)
geom3857, err := iceberg.GeometryTypeOf("srid:3857")
require.NoError(t, err)
geogCRS84, err := iceberg.GeographyTypeOf(iceberg.DefaultGeoCRS, "spherical")
require.NoError(t, err)
geog4269, err := iceberg.GeographyTypeOf("srid:4269", "spherical")
require.NoError(t, err)
geog4269Karney, err := iceberg.GeographyTypeOf("srid:4269", "karney")
require.NoError(t, err)
return []iceberg.PrimitiveType{
iceberg.PrimitiveTypes.Bool,
iceberg.PrimitiveTypes.Int32,
iceberg.PrimitiveTypes.Int64,
iceberg.PrimitiveTypes.Float32,
iceberg.PrimitiveTypes.Float64,
iceberg.PrimitiveTypes.Date,
iceberg.PrimitiveTypes.Time,
iceberg.PrimitiveTypes.Timestamp,
iceberg.PrimitiveTypes.TimestampTz,
iceberg.PrimitiveTypes.TimestampNs,
iceberg.PrimitiveTypes.TimestampTzNs,
iceberg.PrimitiveTypes.String,
iceberg.PrimitiveTypes.UUID,
iceberg.MustFixedTypeOf(3),
iceberg.MustFixedTypeOf(4),
iceberg.PrimitiveTypes.Binary,
iceberg.MustDecimalTypeOf(9, 2),
iceberg.MustDecimalTypeOf(11, 2),
iceberg.MustDecimalTypeOf(9, 3),
geomCRS84,
geom3857,
geogCRS84,
geog4269,
geog4269Karney,
}
}
func required(id int, name string, typ iceberg.Type) iceberg.NestedField {
return iceberg.NestedField{ID: id, Name: name, Type: typ, Required: true}
}
func optional(id int, name string, typ iceberg.Type) iceberg.NestedField {
return iceberg.NestedField{ID: id, Name: name, Type: typ}
}
func schemaOf(fields ...iceberg.NestedField) *iceberg.Schema {
return iceberg.NewSchema(0, fields...)
}
func writeErrors(t *testing.T, read, write *iceberg.Schema) []string {
t.Helper()
errs, err := iceberg.WriteCompatibilityErrors(read, write, true)
require.NoError(t, err)
return errs
}
func TestCompatibilityPrimitiveTypes(t *testing.T) {
primitives := compatPrimitives(t)
for _, from := range primitives {
fromSchema := schemaOf(required(1, "from_field", from))
for _, to := range primitives {
errs := writeErrors(t, schemaOf(required(1, "to_field", to)), fromSchema)
if iceberg.IsPromotionAllowed(from, to) {
assert.Empty(t, errs, "%s -> %s", from, to)
} else {
require.Len(t, errs, 1, "%s -> %s", from, to)
assert.Contains(t, errs[0], "cannot be promoted to")
}
}
structSchema := schemaOf(required(1, "struct_field", iceberg.NewStructType(required(2, "from", from))))
errs := writeErrors(t, structSchema, fromSchema)
require.Len(t, errs, 1)
assert.Contains(t, errs[0], "cannot be read as a struct")
listSchema := schemaOf(required(1, "list_field", iceberg.NewListType(2, from, true)))
errs = writeErrors(t, listSchema, fromSchema)
require.Len(t, errs, 1)
assert.Contains(t, errs[0], "cannot be read as a list")
mapSchema := schemaOf(required(1, "map_field", iceberg.NewMapType(2, iceberg.PrimitiveTypes.String, 3, from, true)))
errs = writeErrors(t, mapSchema, fromSchema)
require.Len(t, errs, 1)
assert.Contains(t, errs[0], "cannot be read as a map")
}
}
func TestIsPromotionAllowed(t *testing.T) {
tests := []struct {
from, to iceberg.PrimitiveType
allowed bool
}{
{iceberg.PrimitiveTypes.Int32, iceberg.PrimitiveTypes.Int32, true},
{iceberg.PrimitiveTypes.Int32, iceberg.PrimitiveTypes.Int64, true},
{iceberg.PrimitiveTypes.Int64, iceberg.PrimitiveTypes.Int32, false},
{iceberg.PrimitiveTypes.Float32, iceberg.PrimitiveTypes.Float64, true},
{iceberg.PrimitiveTypes.Float64, iceberg.PrimitiveTypes.Float32, false},
{iceberg.MustDecimalTypeOf(9, 2), iceberg.MustDecimalTypeOf(11, 2), true},
{iceberg.MustDecimalTypeOf(11, 2), iceberg.MustDecimalTypeOf(9, 2), false},
{iceberg.MustDecimalTypeOf(9, 2), iceberg.MustDecimalTypeOf(9, 3), false},
{iceberg.PrimitiveTypes.String, iceberg.PrimitiveTypes.Binary, false},
{iceberg.MustFixedTypeOf(16), iceberg.PrimitiveTypes.UUID, false},
{iceberg.PrimitiveTypes.Date, iceberg.PrimitiveTypes.Timestamp, false},
}
for _, tt := range tests {
assert.Equal(t, tt.allowed, iceberg.IsPromotionAllowed(tt.from, tt.to), "%s -> %s", tt.from, tt.to)
}
}
func TestCompatibilityVariantToVariant(t *testing.T) {
errs := writeErrors(t,
schemaOf(required(1, "to_field", iceberg.VariantType{})),
schemaOf(required(1, "from_field", iceberg.VariantType{})))
assert.Empty(t, errs)
}
func TestCompatibilityIncompatibleTypesToVariant(t *testing.T) {
from := []iceberg.Type{
iceberg.NewStructType(required(1, "from", iceberg.PrimitiveTypes.Int32)),
iceberg.NewMapType(1, iceberg.PrimitiveTypes.String, 2, iceberg.PrimitiveTypes.Int32, true),
iceberg.NewListType(1, iceberg.PrimitiveTypes.String, true),
}
for _, p := range compatPrimitives(t) {
from = append(from, p)
}
for _, typ := range from {
errs := writeErrors(t,
schemaOf(required(3, "to_field", iceberg.VariantType{})),
schemaOf(required(3, "from_field", typ)))
require.Len(t, errs, 1, "%s", typ)
assert.Contains(t, errs[0], "cannot be read as a variant")
}
}
func TestCompatibilityRequiredSchemaField(t *testing.T) {
write := schemaOf(optional(1, "from_field", iceberg.PrimitiveTypes.Int32))
read := schemaOf(required(1, "to_field", iceberg.PrimitiveTypes.Int32))
errs := writeErrors(t, read, write)
require.Len(t, errs, 1)
assert.Contains(t, errs[0], "should be required, but is optional")
}
func TestCompatibilityMissingSchemaField(t *testing.T) {
write := schemaOf(required(0, "other_field", iceberg.PrimitiveTypes.Int32))
read := schemaOf(required(1, "to_field", iceberg.PrimitiveTypes.Int32))
errs := writeErrors(t, read, write)
require.Len(t, errs, 1)
assert.Contains(t, errs[0], "is required, but is missing")
}
func nestedStruct(fields ...iceberg.NestedField) *iceberg.StructType {
return iceberg.NewStructType(fields...)
}
func TestCompatibilityRequiredStructField(t *testing.T) {
write := schemaOf(required(0, "nested", nestedStruct(optional(1, "from_field", iceberg.PrimitiveTypes.Int32))))
read := schemaOf(required(0, "nested", nestedStruct(required(1, "to_field", iceberg.PrimitiveTypes.Int32))))
errs := writeErrors(t, read, write)
require.Len(t, errs, 1)
assert.Contains(t, errs[0], "should be required, but is optional")
}
func TestCompatibilityMissingRequiredStructField(t *testing.T) {
write := schemaOf(required(0, "nested", nestedStruct(optional(2, "from_field", iceberg.PrimitiveTypes.Int32))))
read := schemaOf(required(0, "nested", nestedStruct(required(1, "to_field", iceberg.PrimitiveTypes.Int32))))
errs := writeErrors(t, read, write)
require.Len(t, errs, 1)
assert.Contains(t, errs[0], "is required, but is missing")
}
func TestCompatibilityMissingOptionalStructField(t *testing.T) {
write := schemaOf(required(0, "nested", nestedStruct(required(2, "from_field", iceberg.PrimitiveTypes.Int32))))
read := schemaOf(required(0, "nested", nestedStruct(optional(1, "to_field", iceberg.PrimitiveTypes.Int32))))
assert.Empty(t, writeErrors(t, read, write))
}
func TestCompatibilityIncompatibleStructField(t *testing.T) {
write := schemaOf(required(0, "nested", nestedStruct(required(1, "from_field", iceberg.PrimitiveTypes.Int32))))
read := schemaOf(required(0, "nested", nestedStruct(required(1, "to_field", iceberg.PrimitiveTypes.Float32))))
errs := writeErrors(t, read, write)
require.Len(t, errs, 1)
assert.Contains(t, errs[0], "cannot be promoted to float")
}
func TestCompatibilityIncompatibleStructAndPrimitive(t *testing.T) {
write := schemaOf(required(0, "nested", nestedStruct(required(1, "from_field", iceberg.PrimitiveTypes.String))))
read := schemaOf(required(0, "nested", iceberg.PrimitiveTypes.String))
errs := writeErrors(t, read, write)
require.Len(t, errs, 1)
assert.Equal(t, "nested: struct<1: from_field: required string> cannot be read as a string", errs[0])
}
func TestCompatibilityMultipleErrors(t *testing.T) {
// required field is optional and cannot be promoted to the read type
write := schemaOf(required(0, "nested", nestedStruct(optional(1, "from_field", iceberg.PrimitiveTypes.Int32))))
read := schemaOf(required(0, "nested", nestedStruct(required(1, "to_field", iceberg.PrimitiveTypes.Float32))))
errs := writeErrors(t, read, write)
require.Len(t, errs, 2)
assert.Contains(t, errs[0], "should be required, but is optional")
assert.Contains(t, errs[1], "cannot be promoted to float")
}
func TestCompatibilityRequiredMapValue(t *testing.T) {
write := schemaOf(required(0, "map_field", iceberg.NewMapType(1, iceberg.PrimitiveTypes.String, 2, iceberg.PrimitiveTypes.Int32, false)))
read := schemaOf(required(0, "map_field", iceberg.NewMapType(1, iceberg.PrimitiveTypes.String, 2, iceberg.PrimitiveTypes.Int32, true)))
errs := writeErrors(t, read, write)
require.Len(t, errs, 1)
assert.Contains(t, errs[0], "values should be required, but are optional")
}
func TestCompatibilityIncompatibleMapKey(t *testing.T) {
write := schemaOf(required(0, "map_field", iceberg.NewMapType(1, iceberg.PrimitiveTypes.Int32, 2, iceberg.PrimitiveTypes.String, false)))
read := schemaOf(required(0, "map_field", iceberg.NewMapType(1, iceberg.PrimitiveTypes.Float64, 2, iceberg.PrimitiveTypes.String, false)))
errs := writeErrors(t, read, write)
require.Len(t, errs, 1)
assert.Contains(t, errs[0], "cannot be promoted to double")
}
func TestCompatibilityIncompatibleMapValue(t *testing.T) {
write := schemaOf(required(0, "map_field", iceberg.NewMapType(1, iceberg.PrimitiveTypes.String, 2, iceberg.PrimitiveTypes.Int32, false)))
read := schemaOf(required(0, "map_field", iceberg.NewMapType(1, iceberg.PrimitiveTypes.String, 2, iceberg.PrimitiveTypes.Float64, false)))
errs := writeErrors(t, read, write)
require.Len(t, errs, 1)
assert.Contains(t, errs[0], "cannot be promoted to double")
}
func TestCompatibilityIncompatibleMapAndPrimitive(t *testing.T) {
write := schemaOf(required(0, "map_field", iceberg.NewMapType(1, iceberg.PrimitiveTypes.String, 2, iceberg.PrimitiveTypes.Int32, false)))
read := schemaOf(required(0, "map_field", iceberg.PrimitiveTypes.String))
errs := writeErrors(t, read, write)
require.Len(t, errs, 1)
assert.Equal(t, "map_field: map<string, int> cannot be read as a string", errs[0])
}
func TestCompatibilityRequiredListElement(t *testing.T) {
write := schemaOf(required(0, "list_field", iceberg.NewListType(1, iceberg.PrimitiveTypes.Int32, false)))
read := schemaOf(required(0, "list_field", iceberg.NewListType(1, iceberg.PrimitiveTypes.Int32, true)))
errs := writeErrors(t, read, write)
require.Len(t, errs, 1)
assert.Contains(t, errs[0], "elements should be required, but are optional")
}
func TestCompatibilityIncompatibleListElement(t *testing.T) {
write := schemaOf(required(0, "list_field", iceberg.NewListType(1, iceberg.PrimitiveTypes.Int32, false)))
read := schemaOf(required(0, "list_field", iceberg.NewListType(1, iceberg.PrimitiveTypes.String, false)))
errs := writeErrors(t, read, write)
require.Len(t, errs, 1)
assert.Contains(t, errs[0], "cannot be promoted to string")
}
func TestCompatibilityIncompatibleListAndPrimitive(t *testing.T) {
write := schemaOf(required(0, "list_field", iceberg.NewListType(1, iceberg.PrimitiveTypes.Int32, false)))
read := schemaOf(required(0, "list_field", iceberg.PrimitiveTypes.String))
errs := writeErrors(t, read, write)
require.Len(t, errs, 1)
assert.Equal(t, "list_field: list<int> cannot be read as a string", errs[0])
}
func TestCompatibilityIncompatibleNestedListOfMap(t *testing.T) {
write := schemaOf(required(0, "list_field", iceberg.NewListType(1, iceberg.NewMapType(2, iceberg.PrimitiveTypes.String, 3, iceberg.PrimitiveTypes.Int32, false), false)))
read := schemaOf(required(0, "list_field", iceberg.NewListType(1, iceberg.NewMapType(2, iceberg.PrimitiveTypes.String, 3, iceberg.PrimitiveTypes.String, false), false)))
errs := writeErrors(t, read, write)
assert.Equal(t, []string{"list_field: int cannot be promoted to string"}, errs)
}
func TestCompatibilityIncompatibleMapOfStruct(t *testing.T) {
write := schemaOf(required(0, "map_field", iceberg.NewMapType(1, iceberg.PrimitiveTypes.String, 2, nestedStruct(required(3, "x", iceberg.PrimitiveTypes.Int32)), false)))
read := schemaOf(required(0, "map_field", iceberg.NewMapType(1, iceberg.PrimitiveTypes.String, 2, nestedStruct(required(3, "x", iceberg.PrimitiveTypes.Float64)), false)))
errs := writeErrors(t, read, write)
assert.Equal(t, []string{"map_field.x: int cannot be promoted to double"}, errs)
}
func reorderedSchemas() (read, write *iceberg.Schema) {
read = schemaOf(required(0, "nested", nestedStruct(
required(1, "field_a", iceberg.PrimitiveTypes.Int32),
required(2, "field_b", iceberg.PrimitiveTypes.Int32))))
write = schemaOf(required(0, "nested", nestedStruct(
required(2, "field_b", iceberg.PrimitiveTypes.Int32),
required(1, "field_a", iceberg.PrimitiveTypes.Int32))))
return read, write
}
func TestCompatibilityDifferentFieldOrdering(t *testing.T) {
read, write := reorderedSchemas()
errs, err := iceberg.WriteCompatibilityErrors(read, write, false)
require.NoError(t, err)
assert.Empty(t, errs)
}
func TestCompatibilityStructWriteReordering(t *testing.T) {
// writes should not reorder fields
read, write := reorderedSchemas()
errs := writeErrors(t, read, write)
require.Len(t, errs, 1)
assert.Equal(t, "nested.field_b is out of order, before field_a", errs[0])
}
func TestCompatibilityStructWriteReorderingRenamed(t *testing.T) {
// the message should use read-side names only
read, _ := reorderedSchemas()
write := schemaOf(required(0, "nested", nestedStruct(
required(2, "old_b", iceberg.PrimitiveTypes.Int32),
required(1, "old_a", iceberg.PrimitiveTypes.Int32))))
errs := writeErrors(t, read, write)
assert.Equal(t, []string{"nested.field_b is out of order, before field_a"}, errs)
}
func TestCompatibilityStructReadReordering(t *testing.T) {
// reads should allow reordering
read, write := reorderedSchemas()
errs, err := iceberg.ReadCompatibilityErrors(read, write)
require.NoError(t, err)
assert.Empty(t, errs)
}
func TestCompatibilityCheckNullabilityRequiredSchemaField(t *testing.T) {
write := schemaOf(optional(1, "from_field", iceberg.PrimitiveTypes.Int32))
read := schemaOf(required(1, "to_field", iceberg.PrimitiveTypes.Int32))
errs, err := iceberg.TypeCompatibilityErrors(read, write, true)
require.NoError(t, err)
assert.Empty(t, errs)
}
func TestCompatibilityCheckNullabilityRequiredStructField(t *testing.T) {
write := schemaOf(required(0, "nested", nestedStruct(optional(1, "from_field", iceberg.PrimitiveTypes.Int32))))
read := schemaOf(required(0, "nested", nestedStruct(required(1, "to_field", iceberg.PrimitiveTypes.Int32))))
errs, err := iceberg.TypeCompatibilityErrors(read, write, true)
require.NoError(t, err)
assert.Empty(t, errs)
}
func TestCompatibilityTypeErrorsStillReportPromotion(t *testing.T) {
write := schemaOf(optional(1, "f", iceberg.PrimitiveTypes.Int32))
read := schemaOf(required(1, "f", iceberg.PrimitiveTypes.String))
errs, err := iceberg.TypeCompatibilityErrors(read, write, true)
require.NoError(t, err)
assert.Equal(t, []string{"f: int cannot be promoted to string"}, errs)
}
func TestReadCompatibilitySchemaEvolution(t *testing.T) {
current := schemaOf(
required(1, "id", iceberg.PrimitiveTypes.Int32),
optional(2, "name", iceberg.PrimitiveTypes.String),
optional(3, "tags", iceberg.NewListType(4, iceberg.PrimitiveTypes.String, false)),
)
tests := []struct {
name string
proposed *iceberg.Schema
errs []string
}{
{
name: "promote, rename, drop and add optional",
proposed: schemaOf(
required(1, "id", iceberg.PrimitiveTypes.Int64),
optional(2, "full_name", iceberg.PrimitiveTypes.String),
optional(5, "added", iceberg.PrimitiveTypes.Bool),
),
},
{
name: "add required column",
proposed: schemaOf(
required(1, "id", iceberg.PrimitiveTypes.Int32),
required(5, "added", iceberg.PrimitiveTypes.Bool),
),
errs: []string{"added is required, but is missing"},
},
{
name: "make optional column required",
proposed: schemaOf(
required(1, "id", iceberg.PrimitiveTypes.Int32),
required(2, "name", iceberg.PrimitiveTypes.String),
),
errs: []string{"name should be required, but is optional"},
},
{
name: "narrow type",
proposed: schemaOf(
required(1, "id", iceberg.PrimitiveTypes.String),
),
errs: []string{"id: int cannot be promoted to string"},
},
{
name: "nested list element error is prefixed with the field name",
proposed: schemaOf(
required(1, "id", iceberg.PrimitiveTypes.Int32),
optional(3, "tags", iceberg.NewListType(4, iceberg.PrimitiveTypes.Int32, true)),
),
errs: []string{
"tags: elements should be required, but are optional",
"tags: string cannot be promoted to int",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
errs, err := iceberg.ReadCompatibilityErrors(tt.proposed, current)
require.NoError(t, err)
assert.Equal(t, tt.errs, errs)
})
}
}
func TestCompatibilityNestedFieldErrorPath(t *testing.T) {
write := schemaOf(required(0, "outer", nestedStruct(
required(1, "inner", nestedStruct(required(2, "leaf", iceberg.PrimitiveTypes.Int64))))))
read := schemaOf(required(0, "outer", nestedStruct(
required(1, "inner", nestedStruct(required(2, "leaf", iceberg.PrimitiveTypes.Int32))))))
errs, err := iceberg.ReadCompatibilityErrors(read, write)
require.NoError(t, err)
assert.Equal(t, []string{"outer.inner.leaf: long cannot be promoted to int"}, errs)
}
func TestCompatibilityNilSchema(t *testing.T) {
sc := schemaOf(required(1, "id", iceberg.PrimitiveTypes.Int32))
for _, tc := range []struct{ read, write *iceberg.Schema }{{nil, sc}, {sc, nil}} {
_, err := iceberg.ReadCompatibilityErrors(tc.read, tc.write)
assert.ErrorIs(t, err, iceberg.ErrInvalidArgument)
assert.ErrorContains(t, err, "cannot check compatibility against nil schema")
}
}