pyarrow
FileIO implementation for reading and writing table files that uses pyarrow.fs.
This file contains a FileIO implementation that relies on the filesystem interface provided
by PyArrow. It relies on PyArrow's from_uri method that infers the correct filesystem
type to use. Theoretically, this allows the supported storage types to grow naturally
with the pyarrow library.
ArrowScan
¶
Source code in pyiceberg/io/pyarrow.py
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 | |
to_record_batches(tasks)
¶
Scan the Iceberg table and return an Iterator[pa.RecordBatch].
Returns an Iterator of pa.RecordBatch with data from the Iceberg table by resolving the right columns that match the current table schema. Only data that matches the provided row_filter expression is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tasks
|
Iterable[FileScanTask]
|
FileScanTasks representing the data files and delete files to read from. |
required |
Returns:
| Type | Description |
|---|---|
Iterator[RecordBatch]
|
An Iterator of PyArrow RecordBatches. |
Iterator[RecordBatch]
|
Total number of rows will be capped if specified. |
Raises:
| Type | Description |
|---|---|
ResolveError
|
When a required field cannot be found in the file |
ValueError
|
When a field type in the file cannot be projected to the schema type |
Source code in pyiceberg/io/pyarrow.py
to_table(tasks)
¶
Scan the Iceberg table and return a pa.Table.
Returns a pa.Table with data from the Iceberg table by resolving the right columns that match the current table schema. Only data that matches the provided row_filter expression is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tasks
|
Iterable[FileScanTask]
|
FileScanTasks representing the data files and delete files to read from. |
required |
Returns:
| Type | Description |
|---|---|
Table
|
A PyArrow table. Total number of rows will be capped if specified. |
Raises:
| Type | Description |
|---|---|
ResolveError
|
When a required field cannot be found in the file |
ValueError
|
When a field type in the file cannot be projected to the schema type |
Source code in pyiceberg/io/pyarrow.py
PyArrowFile
¶
Bases: InputFile, OutputFile
A combined InputFile and OutputFile implementation that uses a pyarrow filesystem to generate pyarrow.lib.NativeFile instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
str
|
A URI or a path to a local file. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
location(str) |
The URI or path to a local file for a PyArrowFile instance. |
Examples:
>>> from pyiceberg.io.pyarrow import PyArrowFile
>>> # input_file = PyArrowFile("s3://foo/bar.txt")
>>> # Read the contents of the PyArrowFile instance
>>> # Make sure that you have permissions to read/write
>>> # file_content = input_file.open().read()
>>> # output_file = PyArrowFile("s3://baz/qux.txt")
>>> # Write bytes to a file
>>> # Make sure that you have permissions to read/write
>>> # output_file.create().write(b'foobytes')
Source code in pyiceberg/io/pyarrow.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
__len__()
¶
create(overwrite=False)
¶
Create a writable pyarrow.lib.NativeFile for this PyArrowFile's location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
overwrite
|
bool
|
Whether to overwrite the file if it already exists. |
False
|
Returns:
| Type | Description |
|---|---|
OutputStream
|
pyarrow.lib.NativeFile: A NativeFile instance for the file located at self.location. |
Raises:
| Type | Description |
|---|---|
FileExistsError
|
If the file already exists at |
Note
This retrieves a pyarrow NativeFile by opening an output stream. If overwrite is set to False, a check is first performed to verify that the file does not exist. This is not thread-safe and a possibility does exist that the file can be created by a concurrent process after the existence check yet before the output stream is created. In such a case, the default pyarrow behavior will truncate the contents of the existing file when opening the output stream.
Source code in pyiceberg/io/pyarrow.py
exists()
¶
open(seekable=True)
¶
Open the location using a PyArrow FileSystem inferred from the location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seekable
|
bool
|
If the stream should support seek, or if it is consumed sequential. |
True
|
Returns:
| Type | Description |
|---|---|
InputStream
|
pyarrow.lib.NativeFile: A NativeFile instance for the file located at |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file at self.location does not exist. |
PermissionError
|
If the file at self.location cannot be accessed due to a permission error such as an AWS error code 15. |
Source code in pyiceberg/io/pyarrow.py
to_input_file()
¶
Return a new PyArrowFile for the location of an existing PyArrowFile instance.
This method is included to abide by the OutputFile abstract base class. Since this implementation uses a single PyArrowFile class (as opposed to separate InputFile and OutputFile implementations), this method effectively returns a copy of the same instance.
Source code in pyiceberg/io/pyarrow.py
PyArrowFileIO
¶
Bases: FileIO
Source code in pyiceberg/io/pyarrow.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 | |
__getstate__()
¶
Create a dictionary of the PyArrowFileIO fields used when pickling.
__setstate__(state)
¶
delete(location)
¶
Delete the file at the given location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
Union[str, InputFile, OutputFile]
|
The URI to the file--if an InputFile instance or an OutputFile instance is provided, the location attribute for that instance is used as the location to delete. |
required |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
When the file at the provided location does not exist. |
PermissionError
|
If the file at the provided location cannot be accessed due to a permission error such as an AWS error code 15. |
Source code in pyiceberg/io/pyarrow.py
new_input(location)
¶
Get a PyArrowFile instance to read bytes from the file at the given location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
str
|
A URI or a path to a local file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
PyArrowFile |
PyArrowFile
|
A PyArrowFile instance for the given location. |
Source code in pyiceberg/io/pyarrow.py
new_output(location)
¶
Get a PyArrowFile instance to write bytes to the file at the given location.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
str
|
A URI or a path to a local file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
PyArrowFile |
PyArrowFile
|
A PyArrowFile instance for the given location. |
Source code in pyiceberg/io/pyarrow.py
parse_location(location, properties=EMPTY_DICT)
staticmethod
¶
Return (scheme, netloc, path) for the given location.
Uses DEFAULT_SCHEME and DEFAULT_NETLOC if scheme/netloc are missing.
Source code in pyiceberg/io/pyarrow.py
PyArrowSchemaVisitor
¶
Bases: Generic[T], ABC
Source code in pyiceberg/io/pyarrow.py
after_field(field)
¶
after_list_element(element)
¶
Override this method to perform an action immediately after visiting an element within a ListType.
after_map_key(key)
¶
after_map_value(value)
¶
before_field(field)
¶
before_list_element(element)
¶
Override this method to perform an action immediately before visiting an element within a ListType.
before_map_key(key)
¶
before_map_value(value)
¶
field(field, field_result)
abstractmethod
¶
list(list_type, element_result)
abstractmethod
¶
map(map_type, key_result, value_result)
abstractmethod
¶
primitive(primitive)
abstractmethod
¶
schema(schema, struct_result)
abstractmethod
¶
UnsupportedPyArrowTypeException
¶
Bases: Exception
Cannot convert PyArrow type to corresponding Iceberg type.
Source code in pyiceberg/io/pyarrow.py
compute_statistics_plan(schema, table_properties)
¶
Compute the statistics plan for all columns.
The resulting list is assumed to have the same length and same order as the columns in the pyarrow table. This allows the list to map from the column index to the Iceberg column ID. For each element, the desired metrics collection that was provided by the user in the configuration is computed and then adjusted according to the data type of the column. For nested columns the minimum and maximum values are not computed. And truncation is only applied to text of binary strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_properties
|
from pyiceberg.table.metadata.TableMetadata
|
The Iceberg table metadata properties. They are required to compute the mapping of column position to iceberg schema type id. It's also used to set the mode for column metrics collection |
required |
Source code in pyiceberg/io/pyarrow.py
data_file_statistics_from_parquet_metadata(parquet_metadata, stats_columns, parquet_column_mapping)
¶
Compute and return DataFileStatistics that includes the following.
- record_count
- column_sizes
- value_counts
- null_value_counts
- nan_value_counts
- column_aggregates
- split_offsets
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parquet_metadata
|
FileMetaData
|
A pyarrow metadata object. |
required |
stats_columns
|
Dict[int, StatisticsCollector]
|
The statistics gathering plan. It is required to set the mode for column metrics collection |
required |
parquet_column_mapping
|
Dict[str, int]
|
The mapping of the parquet file name to the field ID |
required |
Source code in pyiceberg/io/pyarrow.py
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 | |
expression_to_pyarrow(expr, schema=None)
¶
Convert an Iceberg boolean expression to a PyArrow expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
BooleanExpression
|
The Iceberg boolean expression to convert. |
required |
schema
|
Schema | None
|
Optional Iceberg schema to resolve full field paths for nested fields. If provided, nested struct fields will use dotted paths (e.g., "parent.child"). |
None
|
Returns:
| Type | Description |
|---|---|
Expression
|
A PyArrow compute expression. |
Source code in pyiceberg/io/pyarrow.py
parquet_path_to_id_mapping(schema)
¶
Compute the mapping of parquet column path to Iceberg ID.
For each column, the parquet file metadata has a path_in_schema attribute that follows a specific naming scheme for nested columns. This function computes a mapping of the full paths to the corresponding Iceberg IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Schema
|
The current table schema. |
required |
Source code in pyiceberg/io/pyarrow.py
visit_pyarrow(obj, visitor)
¶
Apply a pyarrow schema visitor to any point within a schema.
The function traverses the schema in post-order fashion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Union[DataType, Schema]
|
An instance of a Schema or an IcebergType. |
required |
visitor
|
PyArrowSchemaVisitor[T]
|
An instance of an implementation of the generic PyarrowSchemaVisitor base class. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If attempting to visit an unrecognized object type. |