3
3
import logging
4
4
from collections import defaultdict
5
5
from typing import Iterator
6
- from operator import attrgetter
7
6
8
7
import attrs
9
8
@@ -82,7 +81,7 @@ def __attrs_post_init__(self):
82
81
if self .bisection_factor < 2 :
83
82
raise ValueError ("Must have at least two segments per iteration (i.e. bisection_factor >= 2)" )
84
83
85
- def _validate_and_adjust_columns (self , table1 , table2 ):
84
+ def _validate_and_adjust_columns (self , table1 , table2 , * , strict : bool = True ):
86
85
for c1 , c2 in safezip (table1 .relevant_columns , table2 .relevant_columns ):
87
86
if c1 not in table1 ._schema :
88
87
raise ValueError (f"Column '{ c1 } ' not found in schema for table { table1 } " )
@@ -92,23 +91,23 @@ def _validate_and_adjust_columns(self, table1, table2):
92
91
# Update schemas to minimal mutual precision
93
92
col1 = table1 ._schema [c1 ]
94
93
col2 = table2 ._schema [c2 ]
95
- if isinstance (col1 , PrecisionType ):
96
- if not isinstance (col2 , PrecisionType ):
94
+ if isinstance (col1 , PrecisionType ) and isinstance ( col2 , PrecisionType ) :
95
+ if strict and not isinstance (col2 , PrecisionType ):
97
96
raise TypeError (f"Incompatible types for column '{ c1 } ': { col1 } <-> { col2 } " )
98
97
99
- lowest = min (col1 , col2 , key = attrgetter ( " precision" ) )
98
+ lowest = min (col1 , col2 , key = lambda col : col . precision )
100
99
101
100
if col1 .precision != col2 .precision :
102
101
logger .warning (f"Using reduced precision { lowest } for column '{ c1 } '. Types={ col1 } , { col2 } " )
103
102
104
103
table1 ._schema [c1 ] = attrs .evolve (col1 , precision = lowest .precision , rounds = lowest .rounds )
105
104
table2 ._schema [c2 ] = attrs .evolve (col2 , precision = lowest .precision , rounds = lowest .rounds )
106
105
107
- elif isinstance (col1 , (NumericType , Boolean )):
108
- if not isinstance (col2 , (NumericType , Boolean )):
106
+ elif isinstance (col1 , (NumericType , Boolean )) and isinstance ( col2 , ( NumericType , Boolean )) :
107
+ if strict and not isinstance (col2 , (NumericType , Boolean )):
109
108
raise TypeError (f"Incompatible types for column '{ c1 } ': { col1 } <-> { col2 } " )
110
109
111
- lowest = min (col1 , col2 , key = attrgetter ( " precision" ) )
110
+ lowest = min (col1 , col2 , key = lambda col : col . precision )
112
111
113
112
if col1 .precision != col2 .precision :
114
113
logger .warning (f"Using reduced precision { lowest } for column '{ c1 } '. Types={ col1 } , { col2 } " )
@@ -119,11 +118,11 @@ def _validate_and_adjust_columns(self, table1, table2):
119
118
table2 ._schema [c2 ] = attrs .evolve (col2 , precision = lowest .precision )
120
119
121
120
elif isinstance (col1 , ColType_UUID ):
122
- if not isinstance (col2 , ColType_UUID ):
121
+ if strict and not isinstance (col2 , ColType_UUID ):
123
122
raise TypeError (f"Incompatible types for column '{ c1 } ': { col1 } <-> { col2 } " )
124
123
125
124
elif isinstance (col1 , StringType ):
126
- if not isinstance (col2 , StringType ):
125
+ if strict and not isinstance (col2 , StringType ):
127
126
raise TypeError (f"Incompatible types for column '{ c1 } ': { col1 } <-> { col2 } " )
128
127
129
128
for t in [table1 , table2 ]:
0 commit comments