-
Notifications
You must be signed in to change notification settings - Fork 672
/
typing.go
371 lines (324 loc) · 11.3 KB
/
typing.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
362
363
364
365
366
367
368
369
370
371
package validators
import (
"strings"
structpb "github.com/golang/protobuf/ptypes/struct"
flyte "github.com/flyteorg/flyte/flyteidl/gen/pb-go/flyteidl/core"
)
type typeChecker interface {
CastsFrom(*flyte.LiteralType) bool
}
type trivialChecker struct {
literalType *flyte.LiteralType
}
// CastsFrom is a trivial type checker merely checks if types match exactly.
func (t trivialChecker) CastsFrom(upstreamType *flyte.LiteralType) bool {
// If upstream is an enum, it can be consumed as a string downstream
if upstreamType.GetEnumType() != nil {
if t.literalType.GetSimple() == flyte.SimpleType_STRING {
return true
}
}
// If t is an enum, it can be created from a string as Enums as just constrained String aliases
if t.literalType.GetEnumType() != nil {
if upstreamType.GetSimple() == flyte.SimpleType_STRING {
return true
}
}
if GetTagForType(upstreamType) != "" && GetTagForType(t.literalType) != GetTagForType(upstreamType) {
return false
}
// Ignore metadata when comparing types.
upstreamTypeCopy := *upstreamType
downstreamTypeCopy := *t.literalType
upstreamTypeCopy.Structure = &flyte.TypeStructure{}
downstreamTypeCopy.Structure = &flyte.TypeStructure{}
upstreamTypeCopy.Metadata = &structpb.Struct{}
downstreamTypeCopy.Metadata = &structpb.Struct{}
upstreamTypeCopy.Annotation = &flyte.TypeAnnotation{}
downstreamTypeCopy.Annotation = &flyte.TypeAnnotation{}
return upstreamTypeCopy.String() == downstreamTypeCopy.String()
}
type noneTypeChecker struct{}
// CastsFrom matches only void
func (t noneTypeChecker) CastsFrom(upstreamType *flyte.LiteralType) bool {
return isNoneType(upstreamType)
}
type mapTypeChecker struct {
literalType *flyte.LiteralType
}
// CastsFrom checks that the target map type can be cast to the current map type. We need to ensure both the key types
// and value types match.
func (t mapTypeChecker) CastsFrom(upstreamType *flyte.LiteralType) bool {
// Empty maps should match any collection.
mapLiteralType := upstreamType.GetMapValueType()
if isNoneType(mapLiteralType) {
return true
} else if mapLiteralType != nil {
return getTypeChecker(t.literalType.GetMapValueType()).CastsFrom(mapLiteralType)
}
return false
}
type collectionTypeChecker struct {
literalType *flyte.LiteralType
}
// CastsFrom checks whether two collection types match. We need to ensure that the nesting is correct and the final
// subtypes match.
func (t collectionTypeChecker) CastsFrom(upstreamType *flyte.LiteralType) bool {
// Empty collections should match any collection.
collectionType := upstreamType.GetCollectionType()
if isNoneType(upstreamType.GetCollectionType()) {
return true
} else if collectionType != nil {
return getTypeChecker(t.literalType.GetCollectionType()).CastsFrom(collectionType)
}
return false
}
type schemaTypeChecker struct {
literalType *flyte.LiteralType
}
// CastsFrom handles type casting to the underlying schema type.
// Schemas are more complex types in the Flyte ecosystem. A schema is considered castable in the following
// cases.
//
// 1. The downstream schema has no column types specified. In such a case, it accepts all schema input since it is
// generic.
//
// 2. The downstream schema has a subset of the upstream columns and they match perfectly.
//
// 3. The upstream type can be Schema type or structured dataset type
func (t schemaTypeChecker) CastsFrom(upstreamType *flyte.LiteralType) bool {
schemaType := upstreamType.GetSchema()
structuredDatasetType := upstreamType.GetStructuredDatasetType()
if structuredDatasetType == nil && schemaType == nil {
return false
}
if schemaType != nil {
return schemaCastFromSchema(schemaType, t.literalType.GetSchema())
}
// Flyte Schema can only be serialized to parquet
if len(structuredDatasetType.Format) != 0 && !strings.EqualFold(structuredDatasetType.Format, "parquet") {
return false
}
return schemaCastFromStructuredDataset(structuredDatasetType, t.literalType.GetSchema())
}
type structuredDatasetChecker struct {
literalType *flyte.LiteralType
}
// CastsFrom for Structured dataset are more complex types in the Flyte ecosystem. A structured dataset is considered
// castable in the following cases:
//
// 1. The downstream structured dataset has no column types specified. In such a case, it accepts all structured dataset input since it is
// generic.
//
// 2. The downstream structured dataset has a subset of the upstream structured dataset columns and they match perfectly.
//
// 3. The upstream type can be Schema type or structured dataset type
func (t structuredDatasetChecker) CastsFrom(upstreamType *flyte.LiteralType) bool {
// structured datasets are nullable
if isNoneType(upstreamType) {
return true
}
structuredDatasetType := upstreamType.GetStructuredDatasetType()
schemaType := upstreamType.GetSchema()
if structuredDatasetType == nil && schemaType == nil {
return false
}
if schemaType != nil {
// Flyte Schema can only be serialized to parquet
format := t.literalType.GetStructuredDatasetType().Format
if len(format) != 0 && !strings.EqualFold(format, "parquet") {
return false
}
return structuredDatasetCastFromSchema(schemaType, t.literalType.GetStructuredDatasetType())
}
return structuredDatasetCastFromStructuredDataset(structuredDatasetType, t.literalType.GetStructuredDatasetType())
}
// Upstream (schema) -> downstream (schema)
func schemaCastFromSchema(upstream *flyte.SchemaType, downstream *flyte.SchemaType) bool {
if len(upstream.Columns) == 0 || len(downstream.Columns) == 0 {
return true
}
nameToTypeMap := make(map[string]flyte.SchemaType_SchemaColumn_SchemaColumnType)
for _, column := range upstream.Columns {
nameToTypeMap[column.Name] = column.Type
}
// Check that the downstream schema is a strict sub-set of the upstream schema.
for _, column := range downstream.Columns {
upstreamType, ok := nameToTypeMap[column.Name]
if !ok {
return false
}
if upstreamType != column.Type {
return false
}
}
return true
}
type unionTypeChecker struct {
literalType *flyte.LiteralType
}
func (t unionTypeChecker) CastsFrom(upstreamType *flyte.LiteralType) bool {
unionType := t.literalType.GetUnionType()
upstreamUnionType := upstreamType.GetUnionType()
if upstreamUnionType != nil {
// For each upstream variant we must find a compatible downstream variant
for _, u := range upstreamUnionType.GetVariants() {
found := false
for _, d := range unionType.GetVariants() {
if AreTypesCastable(u, d) {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// Matches iff we can unambiguously select a variant
foundOne := false
for _, x := range unionType.GetVariants() {
if AreTypesCastable(upstreamType, x) {
if foundOne {
return false
}
foundOne = true
}
}
return foundOne
}
// Upstream (structuredDatasetType) -> downstream (structuredDatasetType)
func structuredDatasetCastFromStructuredDataset(upstream *flyte.StructuredDatasetType, downstream *flyte.StructuredDatasetType) bool {
// Skip the format check here when format is empty. https://github.com/flyteorg/flyte/issues/2864
if len(upstream.Format) != 0 && len(downstream.Format) != 0 && !strings.EqualFold(upstream.Format, downstream.Format) {
return false
}
if len(upstream.Columns) == 0 || len(downstream.Columns) == 0 {
return true
}
nameToTypeMap := make(map[string]*flyte.LiteralType)
for _, column := range upstream.Columns {
nameToTypeMap[column.Name] = column.LiteralType
}
// Check that the downstream structured dataset is a strict sub-set of the upstream structured dataset.
for _, column := range downstream.Columns {
upstreamType, ok := nameToTypeMap[column.Name]
if !ok {
return false
}
if !getTypeChecker(column.LiteralType).CastsFrom(upstreamType) {
return false
}
}
return true
}
// Upstream (schemaType) -> downstream (structuredDatasetType)
func structuredDatasetCastFromSchema(upstream *flyte.SchemaType, downstream *flyte.StructuredDatasetType) bool {
if len(upstream.Columns) == 0 || len(downstream.Columns) == 0 {
return true
}
nameToTypeMap := make(map[string]flyte.SchemaType_SchemaColumn_SchemaColumnType)
for _, column := range upstream.Columns {
nameToTypeMap[column.Name] = column.GetType()
}
// Check that the downstream structuredDataset is a strict sub-set of the upstream schema.
for _, column := range downstream.Columns {
upstreamType, ok := nameToTypeMap[column.Name]
if !ok {
return false
}
if !schemaTypeIsMatchStructuredDatasetType(upstreamType, column.LiteralType.GetSimple()) {
return false
}
}
return true
}
// Upstream (structuredDatasetType) -> downstream (schemaType)
func schemaCastFromStructuredDataset(upstream *flyte.StructuredDatasetType, downstream *flyte.SchemaType) bool {
if len(upstream.Columns) == 0 || len(downstream.Columns) == 0 {
return true
}
nameToTypeMap := make(map[string]flyte.SimpleType)
for _, column := range upstream.Columns {
nameToTypeMap[column.Name] = column.LiteralType.GetSimple()
}
// Check that the downstream schema is a strict sub-set of the upstream structuredDataset.
for _, column := range downstream.Columns {
upstreamType, ok := nameToTypeMap[column.Name]
if !ok {
return false
}
if !schemaTypeIsMatchStructuredDatasetType(column.GetType(), upstreamType) {
return false
}
}
return true
}
func schemaTypeIsMatchStructuredDatasetType(schemaType flyte.SchemaType_SchemaColumn_SchemaColumnType, structuredDatasetType flyte.SimpleType) bool {
switch schemaType {
case flyte.SchemaType_SchemaColumn_INTEGER:
return structuredDatasetType == flyte.SimpleType_INTEGER
case flyte.SchemaType_SchemaColumn_FLOAT:
return structuredDatasetType == flyte.SimpleType_FLOAT
case flyte.SchemaType_SchemaColumn_STRING:
return structuredDatasetType == flyte.SimpleType_STRING
case flyte.SchemaType_SchemaColumn_BOOLEAN:
return structuredDatasetType == flyte.SimpleType_BOOLEAN
case flyte.SchemaType_SchemaColumn_DATETIME:
return structuredDatasetType == flyte.SimpleType_DATETIME
case flyte.SchemaType_SchemaColumn_DURATION:
return structuredDatasetType == flyte.SimpleType_DURATION
}
return false
}
func isNoneType(t *flyte.LiteralType) bool {
switch t.GetType().(type) {
case *flyte.LiteralType_Simple:
return t.GetSimple() == flyte.SimpleType_NONE
default:
return false
}
}
func getTypeChecker(t *flyte.LiteralType) typeChecker {
switch t.GetType().(type) {
case *flyte.LiteralType_CollectionType:
return collectionTypeChecker{
literalType: t,
}
case *flyte.LiteralType_MapValueType:
return mapTypeChecker{
literalType: t,
}
case *flyte.LiteralType_Schema:
return schemaTypeChecker{
literalType: t,
}
case *flyte.LiteralType_UnionType:
return unionTypeChecker{
literalType: t,
}
case *flyte.LiteralType_StructuredDatasetType:
return structuredDatasetChecker{
literalType: t,
}
default:
if isNoneType(t) {
return noneTypeChecker{}
}
return trivialChecker{
literalType: t,
}
}
}
func AreTypesCastable(upstreamType, downstreamType *flyte.LiteralType) bool {
typeChecker := getTypeChecker(downstreamType)
// if upstream is a singular union we check if the downstream type is castable from the union variant
if upstreamType.GetUnionType() != nil && len(upstreamType.GetUnionType().GetVariants()) == 1 {
variants := upstreamType.GetUnionType().GetVariants()
if len(variants) == 1 && typeChecker.CastsFrom(variants[0]) {
return true
}
}
return typeChecker.CastsFrom(upstreamType)
}