@@ -33,7 +33,7 @@ pub(crate) struct Nullable(bool);
33
33
34
34
/// Represents the ways a schema comparison can fail.
35
35
#[ derive( Debug , thiserror:: Error ) ]
36
- pub ( crate ) enum Error {
36
+ pub enum SchemaComparisonError {
37
37
#[ error( "The nullability was tightened for a field" ) ]
38
38
NullabilityTightening ,
39
39
#[ error( "Field names do not match" ) ]
@@ -48,8 +48,8 @@ pub(crate) enum Error {
48
48
TypeMismatch ,
49
49
}
50
50
51
- /// A [`std::result::Result`] that has the schema comparison [`Error `] as the error variant.
52
- pub ( crate ) type SchemaComparisonResult = Result < ( ) , Error > ;
51
+ /// A [`std::result::Result`] that has the schema comparison [`SchemaComparisonError `] as the error variant.
52
+ pub ( crate ) type SchemaComparisonResult = Result < ( ) , SchemaComparisonError > ;
53
53
54
54
/// Represents a schema compatibility check for the type. If `self` can be read as `read_type`,
55
55
/// this function returns `Ok(())`. Otherwise, this function returns `Err`.
@@ -65,7 +65,10 @@ impl SchemaComparison for Nullable {
65
65
// column as non-nullable. So we avoid the case where !read_nullable && nullable
66
66
// Hence we check that !(!read_nullable && existing_nullable)
67
67
// == read_nullable || !existing_nullable
68
- require ! ( read_nullable. 0 || !self . 0 , Error :: NullabilityTightening ) ;
68
+ require ! (
69
+ read_nullable. 0 || !self . 0 ,
70
+ SchemaComparisonError :: NullabilityTightening
71
+ ) ;
69
72
Ok ( ( ) )
70
73
}
71
74
}
@@ -78,7 +81,10 @@ impl SchemaComparison for StructField {
78
81
/// 3. You can read this data type as the `read_field`'s data type.
79
82
fn can_read_as ( & self , read_field : & Self ) -> SchemaComparisonResult {
80
83
Nullable ( self . nullable ) . can_read_as ( & Nullable ( read_field. nullable ) ) ?;
81
- require ! ( self . name( ) == read_field. name( ) , Error :: FieldNameMismatch ) ;
84
+ require ! (
85
+ self . name( ) == read_field. name( ) ,
86
+ SchemaComparisonError :: FieldNameMismatch
87
+ ) ;
82
88
self . data_type ( ) . can_read_as ( read_field. data_type ( ) ) ?;
83
89
Ok ( ( ) )
84
90
}
@@ -100,30 +106,33 @@ impl SchemaComparison for StructType {
100
106
. collect ( ) ;
101
107
require ! (
102
108
lowercase_field_map. len( ) == self . fields. len( ) ,
103
- Error :: InvalidSchema
109
+ SchemaComparisonError :: InvalidSchema
104
110
) ;
105
111
106
112
let lowercase_read_field_names: HashSet < String > =
107
113
read_type. fields . keys ( ) . map ( |x| x. to_lowercase ( ) ) . collect ( ) ;
108
114
require ! (
109
115
lowercase_read_field_names. len( ) == read_type. fields. len( ) ,
110
- Error :: InvalidSchema
116
+ SchemaComparisonError :: InvalidSchema
111
117
) ;
112
118
113
119
// Check that the field names are a subset of the read fields.
114
120
if lowercase_field_map
115
121
. keys ( )
116
122
. any ( |name| !lowercase_read_field_names. contains ( name) )
117
123
{
118
- return Err ( Error :: MissingColumn ) ;
124
+ return Err ( SchemaComparisonError :: MissingColumn ) ;
119
125
}
120
126
for read_field in read_type. fields ( ) {
121
127
match lowercase_field_map. get ( & read_field. name ( ) . to_lowercase ( ) ) {
122
128
Some ( existing_field) => existing_field. can_read_as ( read_field) ?,
123
129
None => {
124
130
// Note: Delta spark does not perform the following check. Hence it ignores
125
131
// non-null fields that exist in the read schema that aren't in this schema.
126
- require ! ( read_field. is_nullable( ) , Error :: NewNonNullableColumn ) ;
132
+ require ! (
133
+ read_field. is_nullable( ) ,
134
+ SchemaComparisonError :: NewNonNullableColumn
135
+ ) ;
127
136
}
128
137
}
129
138
}
@@ -161,7 +170,7 @@ impl SchemaComparison for DataType {
161
170
( a, b) => {
162
171
// TODO: In the future, we will change this to support type widening.
163
172
// See: #623
164
- require ! ( a == b, Error :: TypeMismatch ) ;
173
+ require ! ( a == b, SchemaComparisonError :: TypeMismatch ) ;
165
174
}
166
175
} ;
167
176
Ok ( ( ) )
@@ -170,7 +179,7 @@ impl SchemaComparison for DataType {
170
179
171
180
#[ cfg( test) ]
172
181
mod tests {
173
- use crate :: schema:: compare:: { Error , SchemaComparison } ;
182
+ use crate :: schema:: compare:: { SchemaComparison , SchemaComparisonError } ;
174
183
use crate :: schema:: { ArrayType , DataType , MapType , StructField , StructType } ;
175
184
176
185
#[ test]
@@ -252,7 +261,7 @@ mod tests {
252
261
253
262
assert ! ( matches!(
254
263
existing_schema. can_read_as( & read_schema) ,
255
- Err ( Error :: NullabilityTightening )
264
+ Err ( SchemaComparisonError :: NullabilityTightening )
256
265
) ) ;
257
266
}
258
267
#[ test]
@@ -270,7 +279,7 @@ mod tests {
270
279
] ) ;
271
280
assert ! ( matches!(
272
281
existing_schema. can_read_as( & read_schema) ,
273
- Err ( Error :: FieldNameMismatch )
282
+ Err ( SchemaComparisonError :: FieldNameMismatch )
274
283
) ) ;
275
284
}
276
285
#[ test]
@@ -287,7 +296,7 @@ mod tests {
287
296
] ) ;
288
297
assert ! ( matches!(
289
298
existing_schema. can_read_as( & read_schema) ,
290
- Err ( Error :: TypeMismatch )
299
+ Err ( SchemaComparisonError :: TypeMismatch )
291
300
) ) ;
292
301
}
293
302
#[ test]
@@ -318,7 +327,7 @@ mod tests {
318
327
] ) ;
319
328
assert ! ( matches!(
320
329
existing_schema. can_read_as( & read_schema) ,
321
- Err ( Error :: NullabilityTightening )
330
+ Err ( SchemaComparisonError :: NullabilityTightening )
322
331
) ) ;
323
332
}
324
333
#[ test]
@@ -340,7 +349,10 @@ mod tests {
340
349
assert ! ( a. can_read_as( & b) . is_ok( ) ) ;
341
350
342
351
// Read `b` as `a`. `a` is missing a column that is present in `b`.
343
- assert ! ( matches!( b. can_read_as( & a) , Err ( Error :: MissingColumn ) ) ) ;
352
+ assert ! ( matches!(
353
+ b. can_read_as( & a) ,
354
+ Err ( SchemaComparisonError :: MissingColumn )
355
+ ) ) ;
344
356
}
345
357
#[ test]
346
358
fn differ_by_non_nullable_column ( ) {
@@ -360,11 +372,14 @@ mod tests {
360
372
// Read `a` as `b`. `b` has an extra non-nullable column.
361
373
assert ! ( matches!(
362
374
a. can_read_as( & b) ,
363
- Err ( Error :: NewNonNullableColumn )
375
+ Err ( SchemaComparisonError :: NewNonNullableColumn )
364
376
) ) ;
365
377
366
378
// Read `b` as `a`. `a` is missing a column that is present in `b`.
367
- assert ! ( matches!( b. can_read_as( & a) , Err ( Error :: MissingColumn ) ) ) ;
379
+ assert ! ( matches!(
380
+ b. can_read_as( & a) ,
381
+ Err ( SchemaComparisonError :: MissingColumn )
382
+ ) ) ;
368
383
}
369
384
370
385
#[ test]
@@ -384,13 +399,13 @@ mod tests {
384
399
] ) ;
385
400
assert ! ( matches!(
386
401
existing_schema. can_read_as( & read_schema) ,
387
- Err ( Error :: InvalidSchema )
402
+ Err ( SchemaComparisonError :: InvalidSchema )
388
403
) ) ;
389
404
390
405
// Checks in the inverse order
391
406
assert ! ( matches!(
392
407
read_schema. can_read_as( & existing_schema) ,
393
- Err ( Error :: InvalidSchema )
408
+ Err ( SchemaComparisonError :: InvalidSchema )
394
409
) ) ;
395
410
}
396
411
}
0 commit comments