File tree Expand file tree Collapse file tree 1 file changed +13
-4
lines changed
Expand file tree Collapse file tree 1 file changed +13
-4
lines changed Original file line number Diff line number Diff line change @@ -186,10 +186,19 @@ def from_pyarrow(
186186 fields .append (field .with_nullable (True ))
187187 schema = pa .schema (fields , metadata = schema .metadata )
188188
189- # Absorb metadata from the table
190- schema = schema .with_metadata (table .schema .metadata )
191-
192- table = table .cast (schema )
189+ # Desired resulting schema uses the input table's metadata
190+ desired_schema = schema .with_metadata (table .schema .metadata )
191+
192+ # Fast paths to avoid expensive full casts when unnecessary
193+ if table .schema .equals (schema , check_metadata = False ):
194+ # Field types/names/nullability already match our target
195+ if not table .schema .equals (desired_schema , check_metadata = True ):
196+ # Only metadata differs; replace without touching data buffers
197+ table = table .replace_schema_metadata (desired_schema .metadata )
198+ # else: already perfectly matches; keep as-is
199+ else :
200+ # Fallback: perform Arrow cast to coerce to the desired schema (may raise if names missing)
201+ table = table .cast (desired_schema )
193202 instance = cls (table , ** kwargs )
194203 if validate :
195204 instance .validate ()
You can’t perform that action at this time.
0 commit comments