Repository navigation
Expand file tree
/
Copy pathdata_file_codec_fingerprint_test.go
More file actions
141 lines (133 loc) · 5 KB
/
Copy pathdata_file_codec_fingerprint_test.go
File metadata and controls
141 lines (133 loc) · 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
// 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
import (
"strconv"
"strings"
"testing"
"github.com/apache/iceberg-go/internal"
"github.com/stretchr/testify/require"
)
func TestPartitionTypeFingerprintMatchesAvroConversion(t *testing.T) {
// Include currently unsupported types as well: adding support in the Avro
// converter must also make the fingerprint path accept the same type.
types := []Type{
BooleanType{},
Int32Type{},
Int64Type{},
Float32Type{},
Float64Type{},
DateType{},
TimeType{},
TimestampType{},
TimestampTzType{},
TimestampNsType{},
TimestampTzNsType{},
StringType{},
UUIDType{},
BinaryType{},
UnknownType{},
VariantType{},
GeometryType{},
GeographyType{},
MustFixedTypeOf(8),
MustFixedTypeOf(16),
MustDecimalTypeOf(10, 2),
MustDecimalTypeOf(11, 2),
MustDecimalTypeOf(10, 3),
&StructType{FieldList: []NestedField{{ID: 2, Name: "child", Type: StringType{}}}},
&ListType{ElementID: 2, Element: StringType{}},
&MapType{KeyID: 2, KeyType: StringType{}, ValueID: 3, ValueType: Int64Type{}},
}
shapes := make(map[string]string, len(types))
for _, typ := range types {
t.Run(typ.String(), func(t *testing.T) {
var key strings.Builder
fingerprintErr := writePartitionTypeFingerprint(&key, typ)
partition, avroErr := partitionTypeToAvroSchema(&StructType{FieldList: []NestedField{
{ID: 1000, Name: "partition", Type: typ},
}})
if avroErr != nil {
require.EqualError(t, fingerprintErr, avroErr.Error())
return
}
require.NoError(t, fingerprintErr)
if previous, ok := shapes[key.String()]; ok {
require.Equal(t, previous, partition.String(), "fingerprint collision for %s", typ)
} else {
shapes[key.String()] = partition.String()
}
})
}
}
func TestManifestEntrySchemaForMultipleParameterizedFields(t *testing.T) {
price := PartitionField{SourceIDs: []int{1}, FieldID: 1000, Name: "price", Transform: IdentityTransform{}}
code := PartitionField{SourceIDs: []int{2}, FieldID: 1001, Name: "code", Transform: IdentityTransform{}}
for _, version := range []int{1, 2, 3} {
for _, tc := range []struct {
name string
spec PartitionSpec
decimal Type
fixed Type
}{
{"ordered", NewPartitionSpec(price, code), MustDecimalTypeOf(10, 2), MustFixedTypeOf(16)},
{"reversed", NewPartitionSpec(code, price), MustDecimalTypeOf(10, 2), MustFixedTypeOf(16)},
{"precision", NewPartitionSpec(price, code), MustDecimalTypeOf(11, 2), MustFixedTypeOf(16)},
{"scale", NewPartitionSpec(price, code), MustDecimalTypeOf(10, 3), MustFixedTypeOf(16)},
{"fixed_size", NewPartitionSpec(price, code), MustDecimalTypeOf(10, 2), MustFixedTypeOf(8)},
} {
t.Run(tc.name+"/v"+strconv.Itoa(version), func(t *testing.T) {
t.Parallel()
schema := NewSchema(1,
NestedField{ID: 1, Name: "source_price", Type: tc.decimal},
NestedField{ID: 2, Name: "source_code", Type: tc.fixed},
)
partition, err := partitionTypeToAvroSchema(tc.spec.PartitionType(schema))
require.NoError(t, err)
want, err := internal.NewManifestEntrySchema(partition, version)
require.NoError(t, err)
for range 2 {
got, maps, err := manifestEntrySchemaFor(tc.spec, schema, version)
require.NoError(t, err)
require.Equal(t, want.String(), got.String())
require.Equal(t, getFieldIDMap(want), maps)
}
})
}
}
}
func TestPartitionSchemaFingerprintNameBoundaries(t *testing.T) {
schema := NewSchema(1,
NestedField{ID: 1, Name: "source_price", Type: MustDecimalTypeOf(10, 2)},
NestedField{ID: 2, Name: "source_code", Type: MustFixedTypeOf(16)},
)
// Exercise the key's length prefixes independently of Avro name validation.
names := []string{"a", "ab", "1:a", "a:q10:2:", "f16:", "λ"}
keys := make(map[string][2]string, len(names)*len(names))
for _, first := range names {
for _, second := range names {
spec := NewPartitionSpec(
PartitionField{SourceIDs: []int{1}, FieldID: 1000, Name: first, Transform: IdentityTransform{}},
PartitionField{SourceIDs: []int{2}, FieldID: 1001, Name: second, Transform: IdentityTransform{}},
)
key, err := partitionSchemaFingerprint(spec, schema)
require.NoError(t, err)
require.NotContains(t, keys, key, "names %q/%q collide with %q", first, second, keys[key])
keys[key] = [2]string{first, second}
}
}
}