Skip to content

Commit 6e4255b

Browse files
committed
Optimize reads of record batches by pushing limit to file level
1 parent e891bcd commit 6e4255b

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

pyiceberg/io/pyarrow.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,7 @@ def _task_to_record_batches(
11941194
case_sensitive: bool,
11951195
name_mapping: Optional[NameMapping] = None,
11961196
use_large_types: bool = True,
1197+
limit: Optional[int] = None,
11971198
) -> Iterator[pa.RecordBatch]:
11981199
_, _, path = PyArrowFileIO.parse_location(task.file.file_path)
11991200
arrow_format = ds.ParquetFileFormat(pre_buffer=True, buffer_size=(ONE_MEGABYTE * 8))
@@ -1232,8 +1233,11 @@ def _task_to_record_batches(
12321233
)
12331234

12341235
next_index = 0
1236+
total_rows = 0
12351237
batches = fragment_scanner.to_batches()
12361238
for batch in batches:
1239+
if limit is not None and total_rows >= limit:
1240+
break
12371241
next_index = next_index + len(batch)
12381242
current_index = next_index - len(batch)
12391243
if positional_deletes:
@@ -1250,6 +1254,7 @@ def _task_to_record_batches(
12501254
if len(arrow_table) == 0:
12511255
continue
12521256
batch = arrow_table.to_batches()[0]
1257+
total_rows += len(batch)
12531258
yield _to_requested_schema(
12541259
projected_schema, file_project_schema, batch, downcast_ns_timestamp_to_us=True, use_large_types=use_large_types
12551260
)
@@ -1265,6 +1270,7 @@ def _task_to_table(
12651270
case_sensitive: bool,
12661271
name_mapping: Optional[NameMapping] = None,
12671272
use_large_types: bool = True,
1273+
limit: Optional[int] = None,
12681274
) -> Optional[pa.Table]:
12691275
batches = list(
12701276
_task_to_record_batches(
@@ -1277,6 +1283,7 @@ def _task_to_table(
12771283
case_sensitive,
12781284
name_mapping,
12791285
use_large_types,
1286+
limit,
12801287
)
12811288
)
12821289

@@ -1366,6 +1373,7 @@ def project_table(
13661373
case_sensitive,
13671374
table_metadata.name_mapping(),
13681375
use_large_types,
1376+
limit,
13691377
)
13701378
for task in tasks
13711379
]
@@ -1464,6 +1472,7 @@ def project_batches(
14641472
case_sensitive,
14651473
table_metadata.name_mapping(),
14661474
use_large_types,
1475+
limit,
14671476
)
14681477
for batch in batches:
14691478
if limit is not None:

0 commit comments

Comments
 (0)