Skip to content

Commit 81dbfc9

Browse files
committed
Add fast path in from_pyarrow that avoids casting when schemas match
1 parent fe56b04 commit 81dbfc9

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

quivr/tables.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff 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()

0 commit comments

Comments
 (0)