Fokko commented on code in PR #3447:
URL: https://github.com/apache/iceberg-python/pull/3447#discussion_r4698629772


##########
pyiceberg/table/__init__.py:
##########
@@ -1908,6 +1911,13 @@ def plan_files(self) -> Iterable[ScanTask]: ...
     @abstractmethod
     def to_arrow(self) -> pa.Table: ...
 
+    @abstractmethod
+    def to_arrow_batch_reader(self) -> pa.RecordBatchReader: ...

Review Comment:
   Adding a new abstract method here breaks third party subclasses of 
`BaseScan`/`TableScan` that don't implement it: they now raise a `TypeError` on 
instantiation. Could we define `__arrow_c_stream__` on `DataScan` and 
`IncrementalAppendScan` instead, and leave `BaseScan` unchanged?



##########
pyiceberg/io/pyarrow.py:
##########
@@ -3043,3 +3060,23 @@ def _get_field_from_arrow_table(arrow_table: pa.Table, 
field_path: str) -> pa.Ar
     field_array = arrow_table[path_parts[0]]
     # Navigate into the struct using the remaining path parts
     return pc.struct_field(field_array, path_parts[1:])
+
+
+def _coerce_arrow_input(df: pa.Table | pa.RecordBatchReader | 
ArrowStreamExportable) -> pa.Table | pa.RecordBatchReader:
+    """Normalize Arrow write input to a pa.Table or pa.RecordBatchReader.
+
+    Native pyarrow inputs pass through unchanged; any object implementing the
+    Arrow PyCapsule stream interface (``__arrow_c_stream__``) is imported as a
+    streaming RecordBatchReader.
+    """
+    if isinstance(df, (pa.Table, pa.RecordBatchReader)):
+        return df
+
+    # Any object implementing the Arrow PyCapsule stream interface.
+    if hasattr(df, "__arrow_c_stream__"):
+        return pa.RecordBatchReader.from_stream(df)

Review Comment:
   `_dataframe_to_data_files` rejects a `RecordBatchReader` for partitioned 
tables. Most producers people will pass (Polars DataFrames, arro3 Tables) are 
already in memory, so `tbl.append(polars_df)` fails on a partitioned table 
while `tbl.append(pa.table(polars_df))` works. I'd suggest materializing with 
`pa.table(df)` when the table is partitioned, and streaming only for 
unpartitioned tables.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to