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
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 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 |
|
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
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 381 382 383 384 |
|
__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
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 683 684 685 686 |
|
__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
2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 |
|
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. |